/** * creates shared tas */ protected function _createSharedTimeaccounts() { // create 2 timeaccounts for each cc $taNumber = 1; $userGroup = Tinebase_Group::getInstance()->getGroupByName('Users'); $developmentString = self::$_de ? 'Entwicklung' : 'Development'; if (!$userGroup) { die('Could not find userGroup "Users", stopping.'); } $grants = array(array('account_id' => $userGroup->getId(), 'account_type' => 'group', 'bookOwnGrant' => TRUE, 'viewAllGrant' => TRUE, 'bookAllGrant' => TRUE, 'manageBillableGrant' => TRUE, 'exportGrant' => TRUE, 'adminGrant' => TRUE)); $contractsIndex = 0; foreach ($this->_costCenters as $costcenter) { $this->_timeAccounts[$costcenter->getId()] = new Tinebase_Record_RecordSet('Timetracker_Model_Timeaccount'); $i = 0; while ($i < 2) { $i++; $ta = new Timetracker_Model_Timeaccount(array('number' => $taNumber, 'title' => Tinebase_Record_Abstract::generateUID(3), 'grants' => $grants, 'status' => 'billed', 'cleared_at' => $this->_clearedDate, 'budget' => NULL, 'description' => 'Created By Tine 2.0 DEMO DATA')); if ($costcenter->remark == 'Marketing' || $costcenter->remark == $developmentString) { $contract = $costcenter->remark == 'Marketing' ? $this->_contractsMarketing->getByIndex(rand(0, $this->_contractsMarketing->count() - 1)) : $this->_contractsDevelopment->getByIndex(rand(0, $this->_contractsDevelopment->count() - 1)); $ta->budget = $costcenter->remark == 'Marketing' ? 100 : NULL; $ta->relations = array(array('own_model' => 'Timetracker_Model_Timeaccount', 'own_backend' => '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' => 'COST_CENTER'), array('own_model' => 'Timetracker_Model_Timeaccount', 'own_backend' => 'SQL', 'own_id' => NULL, 'related_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_model' => 'Sales_Model_Contract', 'related_backend' => Tasks_Backend_Factory::SQL, 'related_id' => $contract->getId(), 'type' => 'TIME_ACCOUNT')); $ta->title = (self::$_de ? 'Zeitkonto mit ' : 'Timeaccount for ') . $contract->getTitle(); } else { $ta->title = (self::$_de ? 'Zeitkonto mit KST ' : 'Timeaccount for CC ') . $costcenter->getTitle(); $ta->relations = array(array('own_model' => 'Timetracker_Model_Timeaccount', 'own_backend' => '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' => 'COST_CENTER')); } $this->_timeAccounts[$costcenter->getId()]->addRecord($this->_taController->create($ta)); $taNumber++; } } }
/** * 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; }
/** * appends foreign recordSet (1:n relation) to given recordSet * * @param Tinebase_Record_RecordSet $_recordSet Records to append the foreign records to * @param string $_appendTo Property in the records where to append the foreign records to * @param string $_recordKey Property in the records where the foreign key value is in * @param string $_foreignKey Key property in foreign table of the records to append * @param Tinebase_Backend_Sql_Abstract $_foreignBackend Foreign table backend */ public function appendForeignRecordSetToRecordSet($_recordSet, $_appendTo, $_recordKey, $_foreignKey, $_foreignBackend) { $idxRecordKeyMap = $_recordSet->{$_recordKey}; $recordKeyIdxMap = array_flip($idxRecordKeyMap); $allForeignRecords = $_foreignBackend->getMultipleByProperty($idxRecordKeyMap, $_foreignKey); $foreignRecordsClassName = $allForeignRecords->getRecordClassName(); foreach ($_recordSet as $record) { $record->{$_appendTo} = new Tinebase_Record_RecordSet($foreignRecordsClassName); } foreach ($allForeignRecords as $foreignRecord) { $record = $_recordSet->getByIndex($recordKeyIdxMap[$foreignRecord->{$_foreignKey}]); $foreignRecordSet = $record->{$_appendTo}; $foreignRecordSet->addRecord($foreignRecord); } }
/** * compares two recordsets / only compares the ids / returns all records that are different in an array: * - removed -> all records that are in $this but not in $_recordSet * - added -> all records that are in $_recordSet but not in $this * - modified -> array of diffs for all different records that are in both record sets * * @param Tinebase_Record_RecordSet $recordSet * @return Tinebase_Record_RecordSetDiff */ public function diff($recordSet) { if (!$recordSet instanceof Tinebase_Record_RecordSet) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Did not get Tinebase_Record_RecordSet, skipping diff(' . $this->_recordClass . ')'); } return new Tinebase_Record_RecordSetDiff(array('model' => $this->getRecordClassName())); } if ($this->getRecordClassName() !== $recordSet->getRecordClassName()) { throw new Tinebase_Exception_InvalidArgument('can only compare recordsets with the same type of records'); } $existingRecordsIds = $this->getArrayOfIds(); $toCompareWithRecordsIds = $recordSet->getArrayOfIds(); $removedIds = array_diff($existingRecordsIds, $toCompareWithRecordsIds); $addedIds = array_diff($toCompareWithRecordsIds, $existingRecordsIds); $modifiedIds = array_intersect($existingRecordsIds, $toCompareWithRecordsIds); $removed = new Tinebase_Record_RecordSet($this->getRecordClassName()); $added = new Tinebase_Record_RecordSet($this->getRecordClassName()); $modified = new Tinebase_Record_RecordSet('Tinebase_Record_Diff'); foreach ($addedIds as $id) { $added->addRecord($recordSet->getById($id)); } // consider records without id, too foreach ($recordSet->getIdLessIndexes() as $index) { $added->addRecord($recordSet->getByIndex($index)); } foreach ($removedIds as $id) { $removed->addRecord($this->getById($id)); } // consider records without id, too foreach ($this->getIdLessIndexes() as $index) { $removed->addRecord($this->getByIndex($index)); } foreach ($modifiedIds as $id) { $diff = $this->getById($id)->diff($recordSet->getById($id)); if (!$diff->isEmpty()) { $modified->addRecord($diff); } } $result = new Tinebase_Record_RecordSetDiff(array('model' => $this->getRecordClassName(), 'added' => $added, 'removed' => $removed, 'modified' => $modified)); return $result; }