/**
  * checks if all properties are resolved and saved properly
  */
 public function testCRUD()
 {
     $this->_createCustomers();
     $this->_createContracts();
     $json = new Sales_Frontend_Json();
     $customer = $json->getCustomer($this->_customerRecords->filter('name', 'Customer3')->getFirstRecord()->getId());
     $this->assertTrue(is_array($customer['postal_id']));
     $this->assertEquals($customer['adr_id'], $customer['postal_id']['id']);
     $this->assertTrue(is_array($customer['billing']));
     $this->assertEquals(1, count($customer['billing']));
     $this->assertTrue(is_array($customer['delivery']));
     $this->assertEquals(1, count($customer['delivery']));
     $this->assertTrue(is_array($customer['relations']));
     $this->assertTrue(is_array($customer['relations'][0]['related_record']));
     $this->assertEquals(1, count($customer['relations']));
     $this->assertEquals('CUSTOMER', $customer['relations'][0]['type']);
     $customer['billing'][1] = array('prefix1' => 'Dr.', 'prefix2' => 'George Harbottle', 'street' => 'Glasgow Str. 432', 'postalcode' => '532 45', 'locality' => 'Birmingham', 'type' => 'billing');
     $customer['delivery'][1] = array('prefix1' => 'Mr.', 'prefix2' => 'Peter Harbottle', 'street' => 'London Str. 123', 'postalcode' => '532 23', 'locality' => 'Birmingham', 'type' => 'delivery');
     $customer = $json->saveCustomer($customer);
     $this->assertEquals(2, count($customer['billing']));
     $this->assertEquals(2, count($customer['delivery']));
     // remove contracts, otherwise deleting customers having an contract-assigned billing address would fail
     $this->_contractController->delete($this->_contractRecords->getId());
     $json->deleteCustomers(array($customer['id']));
     $this->setExpectedException('Tinebase_Exception_NotFound');
     $json->getCustomer($customer['id']);
 }
コード例 #2
0
 /**
  * checks cleared state and sets the date to the current date, also sets all billables billed
  * 
  * @param Tinebase_Record_Interface $record
  * @param Tinebase_Record_Interface $oldRecord
  */
 protected function _checkCleared(Tinebase_Record_Interface &$record, Tinebase_Record_Interface $oldRecord = NULL)
 {
     $foundCustomer = NULL;
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Invoice: ' . print_r($record->toArray(), true));
     }
     if (is_array($record->relations)) {
         foreach ($record->relations as $relation) {
             if ($relation['related_model'] == 'Sales_Model_Customer') {
                 $foundCustomer = $relation['related_record'];
                 break;
             }
             if ($relation['related_model'] == 'Sales_Model_Contract') {
                 $foundContractRecord = Sales_Controller_Contract::getInstance()->get($relation['related_record']['id']);
                 if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
                     Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Contract: ' . print_r($foundContractRecord->toArray(), true));
                 }
                 foreach ($foundContractRecord->relations as $relation) {
                     if ($relation['related_model'] == 'Sales_Model_Customer') {
                         $foundCustomer = $relation['related_record'];
                         break;
                     }
                 }
             }
         }
     }
     if (empty($record->address_id) && $foundCustomer) {
         $json = new Sales_Frontend_Json();
         $resolved = $json->getCustomer($foundCustomer->getId());
         if (!empty($resolved['billing'])) {
             $record->address_id = $resolved['billing'][0]['id'];
         } else {
             throw new Tinebase_Exception_Data('You have to set a billing address!');
         }
     }
     // if the record hasn't been cleared before, clear billables
     if ($record->cleared == 'CLEARED' && (!$oldRecord || $oldRecord->cleared != 'CLEARED')) {
         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
             Tinebase_Core::getLogger()->log(__METHOD__ . '::' . __LINE__ . ' Clearing Invoice ' . print_r($record->toArray(), 1), Zend_Log::INFO);
         }
         if (!$record->date) {
             $record->date = new Tinebase_DateTime();
         }
         $this->_setNextNumber($record, isset($oldRecord));
         $address = Sales_Controller_Address::getInstance()->get(is_string($record->address_id) ? $record->address_id : $record->address_id . id);
         $string = $foundCustomer['name'] . PHP_EOL;
         $string .= $address->prefix1 ? $address->prefix1 . "\n" : '';
         $string .= $address->prefix2 ? $address->prefix2 . "\n" : '';
         $string .= $address->pobox ? $address->pobox . "\n" : '';
         $string .= $address->street ? $address->street . "\n" : '';
         $poloc = $address->postalcode ? $address->postalcode . " " : '';
         $poloc .= $address->locality ? $address->locality : '';
         if (!empty($poloc)) {
             $string .= $poloc . PHP_EOL;
         }
         $string .= $address->countryname ? $address->countryname : '';
         $record->fixed_address = $string;
         // clear all billables
         if (!empty($record->relations)) {
             foreach ($record->relations as $relation) {
                 if (in_array('Sales_Model_Accountable_Interface', class_implements($relation['related_model']))) {
                     if (is_array($relation['related_record'])) {
                         $rr = new $relation['related_model']($relation['related_record']);
                     } else {
                         $rr = $relation['related_record'];
                     }
                     $rr->clearBillables($record);
                     if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                         Tinebase_Core::getLogger()->log(__METHOD__ . '::' . __LINE__ . ' Clearing billables ' . print_r($rr->toArray(), 1), Zend_Log::INFO);
                     }
                 }
             }
         }
     }
 }