/**
  * tests adding and removing of products to a contract
  */
 public function testAddDeleteProducts()
 {
     $prodTest = new Sales_ProductControllerTest();
     $productOne = $prodTest->testCreateProduct();
     $productTwo = $prodTest->testCreateProduct();
     $contractData = $this->_getContract();
     $contractData->products = array(array('product_id' => $productOne->getId(), 'quantity' => 1, 'interval' => 1, 'billing_point' => 1), array('product_id' => $productTwo->getId(), 'quantity' => 1, 'interval' => 1, 'billing_point' => 1));
     $this->_backend->create($contractData);
     $contract = $this->_backend->get($contractData->getId());
     // checks
     $this->assertEquals($contractData->getId(), $contract->getId());
     $this->assertGreaterThan(0, $contract->number);
     $this->assertEquals(Tinebase_Core::getUser()->getId(), $contract->created_by);
     // check count of product aggregates
     $filter = new Sales_Model_ProductAggregateFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'contract_id', 'operator' => 'equals', 'value' => $contract->getId())));
     $productAggregates = Sales_Controller_ProductAggregate::getInstance()->search($filter);
     $this->assertEquals(2, count($productAggregates));
     $contractData->products = array(array('product_id' => $productOne->getId(), 'quantity' => 1, 'interval' => 1, 'billing_point' => 1));
     $this->_backend->update($contractData);
     $contract = $this->_backend->get($contractData->getId());
     // check count of product aggregates
     $filter = new Sales_Model_ProductAggregateFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'contract_id', 'operator' => 'equals', 'value' => $contract->getId())));
     $productAggregates = Sales_Controller_ProductAggregate::getInstance()->search($filter);
     $this->assertEquals(1, count($productAggregates));
     // cleanup
     $this->_backend->delete($contract->getId());
     $this->_decreaseNumber();
     $prodTest->getUit()->delete(array($productOne->getId(), $productTwo->getId()));
 }
 /**
  * creates the contracts - no containers, just "shared"
  */
 protected function _createSharedContracts()
 {
     $cNumber = 1;
     $container = $this->_contractController->getSharedContractsContainer();
     $cid = $container->getId();
     $ccs = array($this->_developmentCostCenter, $this->_marketingCostCenter);
     $i = 0;
     $this->_setReferenceDate();
     $customers = Sales_Controller_Customer::getInstance()->getAll();
     $addresses = Sales_Controller_Address::getInstance()->getAll();
     $customersCount = $customers->count();
     $ccIndex = 0;
     while ($i < $customersCount) {
         $costcenter = $ccs[$i % 2];
         $i++;
         $customer = $customers->getByIndex($ccIndex);
         $address = $addresses->filter('customer_id', $customer->getId())->filter('type', 'billing')->getFirstRecord();
         $addressId = $address ? $address->getId() : NULL;
         $title = self::$_de ? 'Vertrag für KST ' . $costcenter->number . ' - ' . $costcenter->remark : 'Contract for costcenter ' . $costcenter->number . ' - ' . $costcenter->remark . ' ' . Tinebase_Record_Abstract::generateUID(3);
         $ccid = $costcenter->getId();
         $contract = new Sales_Model_Contract(array('number' => $cNumber, 'title' => $title, 'description' => 'Created by Tine 2.0 DemoData', 'container_id' => $cid, 'status' => 'OPEN', 'cleared' => 'NOT_YET_CLEARED', 'start_date' => clone $this->_referenceDate, 'billing_address_id' => $addressId));
         $relations = array(array('own_model' => 'Sales_Model_Contract', 'own_backend' => Tasks_Backend_Factory::SQL, 'own_id' => NULL, 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_model' => 'Sales_Model_CostCenter', 'related_backend' => Tasks_Backend_Factory::SQL, 'related_id' => $ccid, 'type' => 'LEAD_COST_CENTER'), array('own_model' => 'Sales_Model_Contract', 'own_backend' => Tasks_Backend_Factory::SQL, 'own_id' => NULL, 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_model' => 'Sales_Model_Customer', 'related_backend' => Tasks_Backend_Factory::SQL, 'related_id' => $customer->getId(), 'type' => 'CUSTOMER'));
         $genericProduct = Sales_Controller_Product::getInstance()->create(new Sales_Model_Product(self::$_de ? array('name' => 'Generisches Produkt', 'description' => 'ein generisches produkt aus den demo daten', 'price' => 100) : array('name' => 'Generic Product', 'description' => 'this is a generic product used in demo data', 'price' => 100)));
         $contract->products = array(array('product_id' => $genericProduct->getId(), 'quantity' => 1));
         $contract->relations = $relations;
         $this->_contractController->create($contract);
         $cNumber++;
         $ccIndex++;
         if ($ccIndex == $customersCount) {
             $ccIndex = 0;
         }
     }
 }
 /**
  * try to get a contract
  *
  */
 public function testGetContract()
 {
     $contractData = $this->_getContract();
     $this->_backend->create($contractData);
     $contract = $this->_backend->get($contractData->getId());
     // checks
     $this->assertEquals($contractData->getId(), $contract->getId());
     $this->assertGreaterThan(0, $contract->number);
     $this->assertEquals(Tinebase_Core::getUser()->getId(), $contract->created_by);
     // cleanup
     $this->_backend->delete($contract->getId());
     $this->_decreaseNumber();
 }
 /**
  * create contracts, auto add timeaccounts if there are any
  * 
  * @param array $contractData
  * @return Tinebase_Record_RecordSet
  */
 protected function _createContracts($contractData = NULL)
 {
     // 1.1.20xx
     $startDate = clone $this->_referenceDate;
     $endDate = clone $startDate;
     // 1.8.20xx
     $endDate->addMonth(7);
     $this->_contractController = Sales_Controller_Contract::getInstance();
     $container = $this->_contractController->getSharedContractsContainer();
     $this->_sharedContractsContainerId = $container->getId();
     if (!$contractData) {
         if (!$this->_costcenterRecords) {
             $this->_createCostCenters();
         }
         if (!$this->_productRecords) {
             $this->_createProducts();
         }
         if (!$this->_customerRecords) {
             $this->_createCustomers();
         }
         if (!$this->_timesheetRecords) {
             $this->_createTimesheets();
         }
         $contractData = array(array('number' => 1, 'title' => Tinebase_Record_Abstract::generateUID(), 'description' => '1 unittest begin', 'container_id' => $this->_sharedContractsContainerId, 'billing_address_id' => $this->_addressRecords->filter('customer_id', $this->_customerRecords->filter('name', 'Customer1')->getFirstRecord()->getId())->filter('type', 'billing')->getFirstRecord()->getId(), 'start_date' => clone $startDate, 'end_date' => NULL, 'products' => array(array('start_date' => $startDate, 'end_date' => NULL, 'quantity' => 1, 'interval' => 1, 'billing_point' => 'begin', 'product_id' => $this->_productRecords->filter('name', 'Hours')->getFirstRecord()->getId()))), array('number' => 2, 'title' => Tinebase_Record_Abstract::generateUID(), 'description' => '2 unittest end', 'container_id' => $this->_sharedContractsContainerId, 'billing_address_id' => $this->_addressRecords->filter('customer_id', $this->_customerRecords->filter('name', 'Customer2')->getFirstRecord()->getId())->filter('type', 'billing')->getFirstRecord()->getId(), 'start_date' => clone $startDate, 'end_date' => clone $endDate, 'products' => array(array('start_date' => clone $startDate, 'end_date' => clone $endDate, 'quantity' => 1, 'interval' => 4, 'billing_point' => 'end', 'product_id' => $this->_productRecords->filter('name', 'Hours')->getFirstRecord()->getId()))), array('number' => 3, 'title' => Tinebase_Record_Abstract::generateUID(), 'description' => '3 unittest end', 'container_id' => $this->_sharedContractsContainerId, 'billing_address_id' => $this->_addressRecords->filter('customer_id', $this->_customerRecords->filter('name', 'Customer3')->getFirstRecord()->getId())->filter('type', 'billing')->getFirstRecord()->getId(), 'start_date' => clone $startDate, 'end_date' => NULL, 'products' => array(array('start_date' => clone $startDate, 'end_date' => NULL, 'quantity' => 1, 'interval' => 3, 'billing_point' => 'end', 'product_id' => $this->_productRecords->filter('name', 'Hours')->getFirstRecord()->getId()))), array('number' => 4, 'title' => Tinebase_Record_Abstract::generateUID(), 'description' => '4 unittest products', 'container_id' => $this->_sharedContractsContainerId, 'billing_address_id' => $this->_addressRecords->filter('customer_id', $this->_customerRecords->filter('name', 'Customer4')->getFirstRecord()->getId())->filter('type', 'billing')->getFirstRecord()->getId(), 'start_date' => clone $startDate, 'end_date' => NULL, 'products' => array(array('start_date' => clone $startDate, 'end_date' => NULL, 'quantity' => 1, 'interval' => 6, 'billing_point' => 'begin', 'product_id' => $this->_productRecords->filter('name', 'billhalfyearly')->getFirstRecord()->getId()), array('start_date' => clone $startDate, 'end_date' => NULL, 'quantity' => 1, 'interval' => 3, 'billing_point' => 'begin', 'product_id' => $this->_productRecords->filter('name', 'billeachquarter')->getFirstRecord()->getId()))));
     }
     $this->_contractRecords = new Tinebase_Record_RecordSet('Sales_Model_Contract');
     $i = 0;
     foreach ($contractData as $cd) {
         $costcenter = $this->_costcenterRecords->getByIndex($i);
         $customer = $this->_customerRecords->getByIndex($i);
         if ($this->_timeaccountRecords) {
             $timeaccount = $this->_timeaccountRecords->getByIndex($i);
         }
         $i++;
         $contract = new Sales_Model_Contract($cd);
         $contract->setTimezone('UTC');
         $contract->relations = array(array('own_model' => 'Sales_Model_Contract', 'own_backend' => Tasks_Backend_Factory::SQL, 'own_id' => NULL, 'related_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_model' => 'Sales_Model_CostCenter', 'related_backend' => Tasks_Backend_Factory::SQL, 'related_id' => $costcenter->getId(), 'type' => 'LEAD_COST_CENTER'), array('own_model' => 'Sales_Model_Contract', 'own_backend' => Tasks_Backend_Factory::SQL, 'own_id' => NULL, 'related_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_model' => 'Sales_Model_Customer', 'related_backend' => Tasks_Backend_Factory::SQL, 'related_id' => $customer->getId(), 'type' => 'CUSTOMER'));
         if ($this->_timeaccountRecords) {
             $contract->relations = array_merge($contract->relations, array(array('own_model' => 'Sales_Model_Contract', 'own_backend' => Tasks_Backend_Factory::SQL, 'own_id' => NULL, 'related_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_model' => 'Timetracker_Model_Timeaccount', 'related_backend' => Tasks_Backend_Factory::SQL, 'related_id' => $timeaccount->getId(), 'type' => 'TIME_ACCOUNT')));
         }
         $this->_contractRecords->addRecord($this->_contractController->create($contract));
     }
     return $this->_contractRecords;
 }