/**
  * create new contract
  *
  */
 public function testCreateContract()
 {
     $contract = $this->_getContract();
     $created = $this->_backend->create($contract);
     $this->assertEquals($created->title, $contract->title);
     $this->assertGreaterThan(0, $created->number);
     $this->assertEquals($created->container_id, Sales_Controller_Contract::getSharedContractsContainer()->getId());
     $this->_backend->delete($contract);
     $this->_decreaseNumber();
 }
 /**
  * returns all ids of contracts by interval. last_autobill doesn't get respected here.
  *
  * @param Tinebase_DateTime $date
  * @return array
  */
 public function getBillableContractIds(Tinebase_DateTime $date)
 {
     $date = clone $date;
     $date->setTimezone('UTC');
     $date->setDate($date->format('Y'), $date->format('m'), 1);
     $date->setTime(0, 0, 0);
     $date->submonth(6);
     $be = new Sales_Backend_Contract();
     $db = $be->getAdapter();
     $sql = 'SELECT ' . $db->quoteIdentifier('id') . ' FROM ' . $db->quoteIdentifier(SQL_TABLE_PREFIX . 'sales_contracts') . ' WHERE (' . $db->quoteInto($db->quoteIdentifier('end_date') . ' >= ?', $date) . ' OR ' . $db->quoteIdentifier('end_date') . ' IS NULL ) ';
     $date->addMonth(7);
     $date->subSecond(1);
     $sql .= ' AND ' . $db->quoteInto($db->quoteIdentifier('start_date') . ' <= ?', $date) . ' AND ' . $db->quoteIdentifier('start_date') . ' IS NOT NULL' . ' AND ' . $db->quoteIdentifier('start_date') . ' <> "0000-00-00 00:00:00"';
     return array_keys($db->fetchAssoc($sql));
 }
 /**
  * creates the auto invoices, gets called by cli
  * 
  * @param Tinebase_DateTime $currentDate
  * @param Sales_Model_Contract $contract
  * @param boolean $merge
  */
 public function createAutoInvoices(Tinebase_DateTime $currentDate, Sales_Model_Contract $contract = NULL, $merge = false)
 {
     $this->_autoInvoiceIterationResults = array();
     $this->_autoInvoiceIterationDetailResults = array();
     $this->_autoInvoiceIterationFailures = array();
     $contractBackend = new Sales_Backend_Contract();
     $ids = $contract ? array($contract->getId()) : $contractBackend->getBillableContractIds($currentDate);
     $filter = new Sales_Model_ContractFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'id', 'operator' => 'in', 'value' => $ids)));
     $iterator = new Tinebase_Record_Iterator(array('iteratable' => $this, 'controller' => Sales_Controller_Contract::getInstance(), 'filter' => $filter, 'options' => array('getRelations' => TRUE, 'limit' => $this->_autoInvoiceIterationLimit), 'function' => 'processAutoInvoiceIteration'));
     $iterator->iterate($currentDate, $merge);
     unset($this->_autoInvoiceIterationDetailResults);
     $result = array('failures' => $this->_autoInvoiceIterationFailures, 'failures_count' => count($this->_autoInvoiceIterationFailures), 'created' => $this->_autoInvoiceIterationResults, 'created_count' => count($this->_autoInvoiceIterationResults));
     return $result;
 }
 /**
  * update from 5.4 -> 5.5
  * - set cleared to text
  * - change default values for cleared, status 
  * 
  * @return void
  */
 public function update_4()
 {
     $declaration = new Setup_Backend_Schema_Field_Xml('
         <field>
             <name>cleared</name>
             <type>text</type>
             <length>64</length>
             <default>not yet cleared</default>
         </field>');
     $this->_backend->alterCol('sales_contracts', $declaration);
     $declaration = new Setup_Backend_Schema_Field_Xml('
         <field>
             <name>status</name>
             <type>text</type>
             <length>64</length>
             <default>open</default>
         </field>');
     $this->_backend->alterCol('sales_contracts', $declaration);
     $this->setTableVersion('sales_contracts', 4);
     // transfer cleared value
     $be = new Sales_Backend_Contract();
     $filter = new Sales_Model_ContractFilter(array(), 'AND');
     $filter->addFilter(new Tinebase_Model_Filter_Text('cleared', 'equals', '0'));
     $results = $be->search($filter, null, false, true);
     $be->updateMultiple($results, array('cleared' => 'NOTCLEARED'));
     $filter = new Sales_Model_ContractFilter(array(), 'AND');
     $filter->addFilter(new Tinebase_Model_Filter_Text('cleared', 'equals', '1'));
     $results = $be->search($filter, null, false, true);
     $be->updateMultiple($results, array('cleared' => 'CLEARED'));
     // keyfieldconfigs
     $cb = new Tinebase_Backend_Sql(array('modelName' => 'Tinebase_Model_Config', 'tableName' => 'config'));
     $appId = Tinebase_Application::getInstance()->getApplicationByName('Sales')->getId();
     $salesStatusConfig = array('name' => Sales_Config::CONTRACT_STATUS, 'records' => array(array('id' => 'OPEN', 'value' => 'open', 'icon' => 'images/oxygen/16x16/places/folder-green.png', 'system' => true), array('id' => 'CLOSED', 'value' => 'closed', 'icon' => 'images/oxygen/16x16/places/folder-red.png', 'system' => true)));
     $cb->create(new Tinebase_Model_Config(array('application_id' => $appId, 'name' => Sales_Config::CONTRACT_STATUS, 'value' => json_encode($salesStatusConfig))));
     $salesClearedConfig = array('name' => Sales_Config::CONTRACT_CLEARED, 'records' => array(array('id' => 'TOCLEAR', 'value' => 'to clear', 'icon' => 'images/oxygen/16x16/actions/dialog-warning.png', 'system' => true), array('id' => 'NOTCLEARED', 'value' => 'not yet cleared', 'icon' => 'images/oxygen/16x16/actions/edit-delete.png', 'system' => true), array('id' => 'CLEARED', 'value' => 'cleared', 'icon' => 'images/oxygen/16x16/actions/dialog-ok-apply.png', 'system' => true)));
     $cb->create(new Tinebase_Model_Config(array('application_id' => $appId, 'name' => Sales_Config::CONTRACT_CLEARED, 'value' => json_encode($salesClearedConfig))));
     $this->setApplicationVersion('Sales', '5.5');
 }