/**
  * returns the product aggregate for a given accountable
  * 
  * @param Sales_Model_Accountable_Interface $record
  */
 public function findProductAggregate(Sales_Model_Accountable_Interface $record)
 {
     $accountableClassName = get_class($record);
     $filter = new Sales_Model_ProductFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'accountable', 'operator' => 'equals', 'value' => $accountableClassName)));
     $products = Sales_Controller_Product::getInstance()->search($filter);
     $filter = new Sales_Model_ProductAggregateFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'product_id', 'operator' => 'in', 'value' => $products->getId())));
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'contract_id', 'operator' => 'equals', 'value' => $this->getId())));
     $pas = Sales_Controller_ProductAggregate::getInstance()->search($filter);
     if ($pas->count() < 1) {
         throw new Tinebase_Exception_Data('A contract aggregate could not be found!');
     } elseif ($pas->count() > 1) {
         throw new Tinebase_Exception_Data('At the moment a contract may have only one product aggregate for the same product, not more!');
     }
     return $pas->getFirstRecord();
 }
 /**
  * add body rows
  * 
  * @alternate, kind of POC or VIP, overwrites the default one
  *
  * @param Tinebase_Record_RecordSet $records
  */
 public function processIteration($_records)
 {
     $json = new Tinebase_Convert_Json();
     $productAggregateIds = $_records->accountable_id;
     $paFilter = new Sales_Model_ProductAggregateFilter();
     $paFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'id', 'operator' => 'in', 'value' => $productAggregateIds)));
     $productAggregates = Sales_Controller_ProductAggregate::getInstance()->search($paFilter);
     $pFilter = new Sales_Model_ProductFilter();
     $pFilter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'id', 'operator' => 'in', 'value' => array_unique($productAggregates->product_id))));
     $products = Sales_Controller_Product::getInstance()->search($pFilter);
     $resolved = $json->fromTine20RecordSet($_records);
     foreach ($resolved as $record) {
         $record['accountable_id'] = $productAggregates->getById($record['accountable_id'])->toArray();
         $record['product_id'] = $products->getById($record['accountable_id']['product_id'])->toArray();
         $row = $this->_activeTable->appendRow();
         $i18n = $this->_translate->getAdapter();
         foreach ($this->_config->columns->column as $field) {
             $identifier = $field->identifier;
             // TODO: use ModelConfig here to get the POC
             // get type and value for cell
             $cellType = $this->_getCellType($field->type);
             switch ($identifier) {
                 case 'quantity':
                     $value = intval($record[$identifier]) * intval($record['accountable_id']['quantity']);
                     break;
                 case 'month':
                     $value = $record[$identifier];
                     break;
                 default:
                     $value = $record['product_id'][$identifier];
             }
             // create cell with type and value and add style
             $cell = $row->appendCell($value, $cellType);
             if ($field->customStyle) {
                 $cell->setStyle((string) $field->customStyle);
             }
         }
     }
 }
 /**
  * if no productaggregates are defined for a contract, but 
  * accountables are related, use default billing Info from accountable
  * (product will be created if it does not exist - is needed in the invoice position)
  */
 public function testDefaultAutobillInterval()
 {
     $startDate = clone $this->_referenceDate;
     $startDate->subYear(1);
     $this->_createCustomers(1);
     $this->_createCostCenters();
     $this->_createTimeaccounts(array(array('title' => 'TA', 'description' => 'blabla', 'is_open' => 1, 'status' => 'to bill', 'budget' => 100)));
     $addressId = $this->_addressRecords->filter('customer_id', $this->_customerRecords->filter('name', 'Customer1')->getFirstRecord()->getId())->filter('type', 'billing')->getFirstRecord()->getId();
     // this contract begins 6 months before the first invoice will be created
     $this->_createContracts(array(array('number' => 100, 'title' => 'MyContract', 'description' => 'unittest', 'container_id' => $this->_sharedContractsContainerId, 'billing_point' => 'begin', 'billing_address_id' => $addressId, 'start_date' => $startDate, 'end_date' => NULL)));
     $startDate = clone $this->_referenceDate;
     $startDate->subMonth(1);
     $startDate = clone $this->_referenceDate;
     $startDate->addDay(5);
     $result = $this->_invoiceController->createAutoInvoices($startDate);
     $this->assertEquals(1, $result['created_count']);
     $filter = new Sales_Model_ProductFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'accountable', 'operator' => 'equals', 'value' => 'Timetracker_Model_Timeaccount')));
     $products = Sales_Controller_Product::getInstance()->search($filter);
     $this->assertEquals(1, $products->count());
     $this->assertEquals('Timetracker_Model_Timeaccount', $products->getFirstRecord()->accountable);
     $filter = new Sales_Model_InvoicePositionFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'invoice_id', 'operator' => 'equals', 'value' => $result['created'][0])));
     $invoicePositions = Sales_Controller_InvoicePosition::getInstance()->search($filter);
     $this->assertEquals(1, $invoicePositions->count());
 }