/**
  * the singleton pattern
  *
  * @return Timetracker_Controller_Timesheet
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 /**
  * calls Timetracker_Controller_Timesheet::findTimesheetsByTimeaccountAndPeriod arguments suitable for async job
  * returns true if cache could be saved.
  * 
  * @param array $args
  * @return boolean
  */
 public function findTimesheetsForReport(array $args)
 {
     $cache = Tinebase_Core::getCache();
     $results = Timetracker_Controller_Timesheet::getInstance()->findTimesheetsByTimeaccountAndPeriod($args['timeaccountId'], $args['startDate'], $args['endDate'], $args['destination'], $args['taCostCenter']);
     $m = str_replace('-', '', $args['month']);
     return $cache->save(array('results' => $results), $args['cacheId'], array($args['cacheId'] . '_' . $m));
 }
Exemplo n.º 3
0
 /**
  * the singleton pattern
  *
  * @return Timetracker_Controller_Timesheet
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new Timetracker_Controller_Timesheet();
     }
     return self::$_instance;
 }
 /**
  * tests the findTimesheetsByTimeaccountAndPeriod method of the timesheet controller
  */
 public function testFindTimesheetsByTimeaccountAndPeriod()
 {
     $i = 0;
     $now = Tinebase_DateTime::now();
     $data = array('account_id' => Tinebase_Core::getUser()->getId(), 'is_cleared' => true, 'timeaccount_id' => $this->_objects['timeaccount']->getId(), 'description' => 'test', 'duration' => 1, 'title' => 'test');
     while ($i < 10) {
         $data['start_date'] = $now->addHour(1);
         $this->_timesheetController->create(new Timetracker_Model_Timesheet($data));
         $i++;
     }
     $sdate = Tinebase_DateTime::now();
     $sdate->subDay(1);
     $edate = $now->addDay(1);
     $result = $this->_timesheetController->findTimesheetsByTimeaccountAndPeriod($this->_objects['timeaccount']->getId(), $sdate, $edate);
     $this->assertEquals(10, count($result));
 }
 /**
  * append acl filter
  *
  * @param Zend_Db_Select $_select
  */
 protected function _appendAclSqlFilter($_select)
 {
     if ($this->getCondition() === self::CONDITION_OR) {
         // ACL filter with OR condition is useless and delivers wrong results!
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . ' No ACL filter for OR condition!');
         }
         return;
     }
     if (Timetracker_Controller_Timesheet::getInstance()->checkRight(Timetracker_Acl_Rights::MANAGE_TIMEACCOUNTS, FALSE, FALSE)) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . ' No ACL filter for MANAGE_TIMEACCOUNTS right!');
         }
         return;
     }
     if (!$this->_isResolved) {
         // get all timeaccounts user has required grants for
         $result = array();
         foreach ($this->_requiredGrants as $grant) {
             if ($grant != Timetracker_Model_TimeaccountGrants::BOOK_OWN) {
                 $result = array_merge($result, Timetracker_Model_TimeaccountGrants::getTimeaccountsByAcl($grant, TRUE));
             }
         }
         $this->_validTimeaccounts = array_unique($result);
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
             Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' valid timeaccounts' . print_r($this->_validTimeaccounts, TRUE) . ' for required grants: ' . print_r($this->_requiredGrants, TRUE));
         }
         $this->_isResolved = TRUE;
     }
     $db = Tinebase_Core::getDb();
     $field = $db->quoteIdentifier('timeaccount_id');
     $where = $db->quoteInto("{$field} IN (?)", empty($this->_validTimeaccounts) ? array('') : $this->_validTimeaccounts);
     // get timeaccounts with BOOK_OWN right (get only if no manual filter is set)
     $bookOwnTS = Timetracker_Model_TimeaccountGrants::getTimeaccountsByAcl(Timetracker_Model_TimeaccountGrants::BOOK_OWN, TRUE);
     if (!empty($bookOwnTS)) {
         $where .= ' OR (' . $db->quoteInto($field . ' IN (?)', $bookOwnTS) . ' AND ' . $db->quoteInto($db->quoteIdentifier('account_id') . ' = ?', Tinebase_Core::getUser()->getId()) . ')';
     }
     $_select->where($where);
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ACL filter: ' . $where);
     }
 }
 /**
  * appends custom/acl filters to a given select object
  * 
  * @param  Zend_Db_Select                    $_select
  * @param  Tinebase_Backend_Sql_Abstract     $_backend
  * @return void
  */
 public function appendFilterSql($_select, $_backend)
 {
     if (Timetracker_Controller_Timesheet::getInstance()->checkRight(Timetracker_Acl_Rights::MANAGE_TIMEACCOUNTS, FALSE, FALSE)) {
         return;
     }
     if (!$this->_isResolved) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Get all timeaccounts for user with required grants: " . print_r($this->_requiredGrants, TRUE));
         }
         $result = Timetracker_Model_TimeaccountGrants::getTimeaccountsByAcl($this->_requiredGrants, TRUE);
         $this->_validTimeaccounts = $result;
         $this->_isResolved = TRUE;
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Got " . count($this->_validTimeaccounts) . ' valid timeaccounts');
         }
     }
     $db = Tinebase_Core::getDb();
     $field = $db->quoteIdentifier('id');
     $where = $db->quoteInto("{$field} IN (?)", empty($this->_validTimeaccounts) ? array('') : $this->_validTimeaccounts);
     $_select->where($where);
 }
 /**
  * tests if timeaccounts/timesheets get cleared if the invoice get billed
  */
 public function testClearing()
 {
     if ($this->_dbIsPgsql()) {
         $this->markTestSkipped('0011670: fix Sales_Invoices Tests with postgresql backend');
     }
     $this->_createFullFixtures();
     // the whole year, 12 months
     $date = clone $this->_referenceDate;
     $date->addMonth(12);
     $this->_invoiceController->createAutoInvoices($date);
     $json = new Sales_Frontend_Json();
     // test if timesheets get cleared
     $invoices = $json->searchInvoices(array(array('field' => 'foreignRecord', 'operator' => 'AND', 'value' => array('appName' => 'Sales', 'linkType' => 'relation', 'modelName' => 'Customer', 'filters' => array(array('field' => 'name', 'operator' => 'equals', 'value' => 'Customer3'))))), array());
     $invoiceIds = array();
     $this->assertEquals(2, $invoices['totalcount']);
     foreach ($invoices['results'] as $invoice) {
         $invoiceIds[] = $invoice['id'];
         // fetch invoice by get to have all relations set
         $invoice = $json->getInvoice($invoice['id']);
         $invoice['cleared'] = 'CLEARED';
         $json->saveInvoice($invoice);
     }
     $tsController = Timetracker_Controller_Timesheet::getInstance();
     $timesheets = $tsController->getAll();
     foreach ($timesheets as $timesheet) {
         $this->assertTrue(in_array($timesheet->invoice_id, $invoiceIds), 'the invoice id must be set!');
         $this->assertEquals(1, $timesheet->is_cleared);
     }
     // test if timeaccounts get cleared
     $invoices = $json->searchInvoices(array(array('field' => 'foreignRecord', 'operator' => 'AND', 'value' => array('appName' => 'Sales', 'linkType' => 'relation', 'modelName' => 'Customer', 'filters' => array(array('field' => 'name', 'operator' => 'equals', 'value' => 'Customer1'))))), array());
     $invoiceIds = array();
     foreach ($invoices['results'] as $invoice) {
         $invoiceIds[] = $invoice['id'];
         // fetch invoice by get to have all relations set
         $invoice = $json->getInvoice($invoice['id']);
         $invoice['cleared'] = 'CLEARED';
         // check set empty number fields to an empty string
         $invoice['sales_tax'] = '';
         $invoice['price_gross'] = '';
         $invoice['price_net'] = '';
         $invoice = $json->saveInvoice($invoice);
         $this->assertEquals(0, $invoice['sales_tax']);
         $this->assertEquals(0, $invoice['price_gross']);
         $this->assertEquals(0, $invoice['price_net']);
     }
     $taController = Timetracker_Controller_Timeaccount::getInstance();
     $filter = new Timetracker_Model_TimeaccountFilter(array(array('field' => 'budget', 'operator' => 'greater', 'value' => 0), array('field' => 'is_open', 'operator' => 'equals', 'value' => 0)));
     $timeaccounts = $taController->search($filter);
     $this->assertEquals(1, $timeaccounts->count());
     foreach ($timeaccounts as $ta) {
         $this->assertTrue(in_array($ta->invoice_id, $invoiceIds), 'the invoice id id must be set!');
         $this->assertEquals('billed', $ta->status);
     }
 }
 /**
  * tests if timesheets get resetted properly after deleting the invoice
  * and recreate the same invoice again containing the same timesheets
  */
 public function testDeleteAndRunAgainInvoice()
 {
     $this->_createFullFixtures();
     $date = clone $this->_referenceDate;
     $date->addMonth(8);
     $i = 0;
     $result = $this->_invoiceController->createAutoInvoices($date);
     $this->assertEquals(6, count($result['created']));
     $tsController = Timetracker_Controller_Timesheet::getInstance();
     // get first valid invoice id from all timesheets
     $tsInvoiceIds = array_unique($tsController->getAll()->invoice_id);
     sort($tsInvoiceIds);
     $tsInvoiceIds = array_reverse($tsInvoiceIds);
     $this->assertTrue(!empty($tsInvoiceIds[0]));
     $myInvoice = $this->_invoiceController->get($tsInvoiceIds[0]);
     $f = new Timetracker_Model_TimesheetFilter(array());
     $f->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'invoice_id', 'operator' => 'equals', 'value' => $myInvoice->getId())));
     $myTimesheets = $tsController->search($f);
     $this->assertEquals(2, $myTimesheets->count(), 'timesheets not found for invoice ' . $myInvoice->getId());
     $this->_invoiceController->delete(array($myInvoice->getId()));
     $allTimesheets = $tsController->getAll();
     foreach ($allTimesheets as $ts) {
         $this->assertSame(NULL, $ts->invoice_id, 'invoice id should be reset');
     }
     $this->_invoiceController->createAutoInvoices($date);
     $tsId = $myTimesheets->getFirstRecord()->getId();
     $myTimesheet = $tsController->get($tsId);
     $f = new Timetracker_Model_TimesheetFilter(array());
     $f->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'invoice_id', 'operator' => 'equals', 'value' => $myTimesheet->invoice_id)));
     $myTimesheets = $tsController->search($f);
     $this->assertEquals(2, $myTimesheets->count());
     foreach ($myTimesheets as $ts) {
         $this->assertEquals(40, strlen($ts->invoice_id));
     }
 }
Exemplo n.º 9
0
 /**
  * the constructor
  *
  */
 public function __construct()
 {
     $this->_applicationName = 'Timetracker';
     $this->_timesheetController = Timetracker_Controller_Timesheet::getInstance();
     $this->_timeaccountController = Timetracker_Controller_Timeaccount::getInstance();
 }
 /**
  * creates and persists a timesheet
  * 
  * @param array $data
  * @return Tinebase_Record_Interface
  */
 protected function _createTimesheet($data)
 {
     $ts = new Timetracker_Model_Timesheet($data);
     return $this->_tsController->create($ts);
 }
 /**
  * tests the corret handling of the usertimezone in the date filter
  */
 public function testDateIntervalFilter()
 {
     $taController = Timetracker_Controller_Timeaccount::getInstance();
     $tsController = Timetracker_Controller_Timesheet::getInstance();
     $dateString = '2014-07-14 00:15:00';
     $date = new Tinebase_DateTime($dateString, Tinebase_Core::getUserTimezone());
     $ta = $taController->create(new Timetracker_Model_Timeaccount(array('number' => '123', 'title' => 'test')));
     $r = new Timetracker_Model_Timesheet(array('timeaccount_id' => $ta->getId(), 'account_id' => Tinebase_Core::getUser()->getId(), 'description' => 'lazy boring', 'start_date' => $date, 'duration' => 30));
     $r->setTimezone('UTC');
     $ts = $tsController->create($r);
     $filter = new Timetracker_Model_TimesheetFilter(array());
     $dateFilter = new Tinebase_Model_Filter_DateMock(array('field' => 'start_date', 'operator' => "within", "value" => "weekThis"));
     $dateFilter::$testDate = $date;
     $filter->addFilter($dateFilter);
     $results = $tsController->search($filter);
     $this->assertEquals(1, $results->count());
 }
 /**
  * processAutoInvoiceIteration
  * 
  * @param Tinebase_Record_RecordSet $contracts
  * @param Tinebase_DateTime $currentDate
  * @param boolean $merge
  */
 public function processAutoInvoiceIteration($contracts, $currentDate, $merge)
 {
     Timetracker_Controller_Timeaccount::getInstance()->resolveCustomfields(FALSE);
     Timetracker_Controller_Timesheet::getInstance()->resolveCustomfields(FALSE);
     Sales_Controller_Contract::getInstance()->resolveCustomfields(FALSE);
     Sales_Controller_Contract::getInstance()->setHandleDependentRecords(FALSE);
     Sales_Controller_ProductAggregate::getInstance()->resolveCustomfields(FALSE);
     $contracts->setTimezone(Tinebase_Core::getUserTimezone());
     foreach ($contracts as $contract) {
         $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb());
         try {
             $this->_createAutoInvoicesForContract($contract, clone $currentDate, $merge);
             Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId);
         } catch (Exception $e) {
             Tinebase_TransactionManager::getInstance()->rollBack();
             $failure = 'Could not create auto invoice for contract "' . $contract->title . '" Exception: ' . $e->getCode() . ' has been thrown: "' . $e->getMessage() . '".';
             $this->_autoInvoiceIterationFailures[] = $failure;
             Tinebase_Exception::log($e, FALSE);
         }
     }
     Sales_Controller_Contract::getInstance()->setHandleDependentRecords(TRUE);
     Sales_Controller_Contract::getInstance()->resolveCustomfields(TRUE);
     Sales_Controller_ProductAggregate::getInstance()->resolveCustomfields(TRUE);
     Timetracker_Controller_Timeaccount::getInstance()->resolveCustomfields(TRUE);
     Timetracker_Controller_Timesheet::getInstance()->resolveCustomfields(TRUE);
 }
 /**
  * creates/updates a record
  *
  * @param  array $recordData
  * @param  array $context
  * @return array created/updated record
  */
 public function saveTimesheet($recordData, array $context = array())
 {
     $this->_timesheetController->setRequestContext($context);
     return $this->_save($recordData, $this->_timesheetController, 'Timesheet');
 }
 /**
  * 
  * @param array $recordData
  * @return Tinebase_Record_RecordSet
  */
 protected function _createTimesheets($recordData = NULL)
 {
     $this->_timesheetController = Timetracker_Controller_Timesheet::getInstance();
     if (!$this->_timesheetRecords) {
         $this->_timesheetRecords = new Tinebase_Record_RecordSet('Timetracker_Model_Timesheet');
     }
     if (!$recordData) {
         if (!$this->_timeaccountRecords) {
             $this->_createTimeaccounts();
         }
         $tsDate = clone $this->_referenceDate;
         $tsDate->addMonth(4)->addDay(5);
         // this is a ts on 20xx-05-06
         $timesheet = new Timetracker_Model_Timesheet(array('account_id' => Tinebase_Core::getUser()->getId(), 'timeaccount_id' => $this->_timeaccountRecords->filter('title', 'TA-for-Customer3')->getFirstRecord()->getId(), 'start_date' => $tsDate, 'duration' => 105, 'description' => 'ts from ' . (string) $tsDate));
         $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
         // this is a ts on 20xx-05-07
         $timesheet->id = NULL;
         $timesheet->start_date = $tsDate->addDay(1);
         $timesheet->description = 'ts from ' . (string) $tsDate;
         $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
         // this is a ts on 20xx-09-07
         $timesheet->id = NULL;
         $timesheet->start_date = $tsDate->addMonth(4);
         $timesheet->description = 'ts from ' . (string) $tsDate;
         $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
         // this is a ts on 20xx-09-08
         $timesheet->id = NULL;
         $timesheet->start_date = $tsDate->addDay(1);
         $timesheet->description = 'ts from ' . (string) $tsDate;
         $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
     } else {
         foreach ($recordData as $tsData) {
             $timesheet = new Timetracker_Model_Timesheet($tsData);
             $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
         }
     }
     return $this->_timesheetRecords;
 }
 /**
  * create tine timesheet
  *
  * @param Timetracker_Model_Timesheet $_record
  * @param string $_timeaccountId
  * @return Timetracker_Model_Timesheet 
  * 
  */
 protected function _createTimesheet($_record, $_timeaccountId)
 {
     $_record->timeaccount_id = $_timeaccountId;
     // check if user is available
     try {
         Tinebase_User::getInstance()->getUserById($_record->account_id);
     } catch (Tinebase_Exception_NotFound $enf) {
         //echo "  Couldn't find user with id " . $_record->account_id . "\n";
         return NULL;
     }
     if ($_record->isValid()) {
         Timetracker_Controller_Timesheet::getInstance()->create($_record);
     } else {
         echo "  Timesheet invalid: " . print_r($_record->getValidationErrors(), true) . "\n";
         return NULL;
     }
     $this->_counters['timesheets']++;
 }
 /**
  * transfers timesheets from one timeaccount (need id in params) to another
  * - params: timeaccountId=xxx, dryrun=0|1
  * 
  * @param Zend_Console_Getopt $_opts
  * @return boolean
  * 
  * @todo allow to configure mapping elsewhere
  */
 public function transferTimesheetsToDifferentTimeaccounts(Zend_Console_Getopt $_opts)
 {
     $params = $this->_parseArgs($_opts, array('timeaccountId'));
     // transfer timeaccounts depending on user group membership
     $mappingGroupToTimeaccount = array();
     // do not transfer timesheets of this users
     $accountExceptions = array();
     // get timesheets
     $filter = new Timetracker_Model_TimesheetFilter(array(array('field' => 'timeaccount_id', 'operator' => 'AND', 'value' => array(array('field' => 'id', 'operator' => 'equals', 'value' => $params['timeaccountId'])))));
     $pagination = new Tinebase_Model_Pagination(array('start' => 0, 'limit' => 100));
     $timesheets = Timetracker_Controller_Timesheet::getInstance()->search($filter, $pagination);
     // loop timesheets
     $transferCount = 0;
     while (count($timesheets) > 0) {
         foreach ($timesheets as $timesheet) {
             if (in_array($timesheet->account_id, $accountExceptions)) {
                 echo "Skipping user {$timesheet->account_id}.\n";
                 continue;
             }
             // get user groups
             $groupMemberships = Tinebase_Group::getInstance()->getGroupMemberships($timesheet->account_id);
             // transfer timesheet
             $found = FALSE;
             foreach ($mappingGroupToTimeaccount as $groupId => $timeaccountId) {
                 if (in_array($groupId, $groupMemberships)) {
                     echo 'Transfering timesheet of user ' . $timesheet->account_id . ' to timeaccount ' . $timeaccountId . ".\n";
                     if ((isset($params['dryrun']) || array_key_exists('dryrun', $params)) && $params['dryrun'] == 0) {
                         $timesheet->timeaccount_id = $timeaccountId;
                         Timetracker_Controller_Timesheet::getInstance()->update($timesheet);
                         $transferCount++;
                     }
                     $found = TRUE;
                     break;
                 }
             }
             if (!$found) {
                 echo "No valid mapping found for timesheet.\n";
             }
         }
         $pagination->start = $pagination->start + 100;
         $timesheets = Timetracker_Controller_Timesheet::getInstance()->search($filter, $pagination);
     }
     echo 'Transfered ' . $transferCount . " timesheets.\n";
     return TRUE;
 }
 /**
  * set each billable of this accountable billed
  *
  * @param Sales_Model_Invoice $invoice
  */
 public function clearBillables(Sales_Model_Invoice $invoice)
 {
     $tsController = Timetracker_Controller_Timesheet::getInstance();
     $this->_disableTimesheetChecks($tsController);
     $filter = new Timetracker_Model_TimesheetFilter(array(), 'AND');
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'is_cleared', 'operator' => 'equals', 'value' => 0)));
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'timeaccount_id', 'operator' => 'equals', 'value' => $this->getId())));
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'invoice_id', 'operator' => 'equals', 'value' => $invoice->getId())));
     // if this timeaccount has a budget, close and bill this and set cleared at date
     if (intval($this->budget) > 0) {
         $this->is_open = 0;
         $this->status = 'billed';
         $this->cleared_at = Tinebase_DateTime::now();
         Timetracker_Controller_Timeaccount::getInstance()->update($this);
         // also clear all timesheets belonging to this invoice and timeaccount
         $tsController->updateMultiple($filter, array('is_cleared' => 1));
     } else {
         // otherwise clear all timesheets of this invoice
         $tsController->updateMultiple($filter, array('is_cleared' => 1));
     }
     $this->_enableTimesheetChecks($tsController);
 }
Exemplo n.º 18
0
 /**
  * try to get a Timesheet
  *
  */
 public function testDeleteTimesheet()
 {
     $timesheet = $this->_getTimesheet();
     $timesheetData = $this->_json->saveTimesheet($timesheet->toArray());
     // delete
     $this->_json->deleteTimesheets($timesheetData['id']);
     $timesheets = Timetracker_Controller_Timesheet::getInstance()->getTimesheetsByTimeaccountId($timesheetData['timeaccount_id']['id']);
     // checks
     $this->assertEquals(0, count($timesheets));
     // cleanup
     $this->_json->deleteTimeaccounts($timesheetData['timeaccount_id']['id']);
 }
 /**
  * returns true if this invoice needs to be recreated because data changed
  *
  * @param Tinebase_DateTime $date
  * @param Sales_Model_ProductAggregate $productAggregate
  * @param Sales_Model_Invoice $invoice
  * @param Sales_Model_Contract $contract
  * @return boolean
  */
 public function needsInvoiceRecreation(Tinebase_DateTime $date, Sales_Model_ProductAggregate $productAggregate, Sales_Model_Invoice $invoice, Sales_Model_Contract $contract)
 {
     $filter = new Timetracker_Model_TimesheetFilter(array(), 'AND');
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'invoice_id', 'operator' => 'equals', 'value' => $invoice->getId())));
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' TS Filter: ' . print_r($filter->toArray(), true));
     }
     $timesheets = Timetracker_Controller_Timesheet::getInstance()->search($filter);
     $timesheets->setTimezone(Tinebase_Core::getUserTimezone());
     foreach ($timesheets as $timesheet) {
         if ($timesheet->last_modified_time && $timesheet->last_modified_time->isLater($invoice->creation_time)) {
             return true;
         }
     }
     return false;
 }
 /**
  * delete linked objects / timesheets
  *
  * @param Tinebase_Record_Interface $_record
  */
 protected function _deleteLinkedObjects(Tinebase_Record_Interface $_record)
 {
     // delete linked timesheets
     $timesheets = Timetracker_Controller_Timesheet::getInstance()->getTimesheetsByTimeaccountId($_record->getId());
     Timetracker_Controller_Timesheet::getInstance()->delete($timesheets->getArrayOfIds());
     // delete other linked objects
     parent::_deleteLinkedObjects($_record);
 }
 /**
  * tests auto invoice creation
  *
  * TODO should be refactored/fixed:  line 97: $this->assertEquals(6, $half); // $half is completely random
  */
 public function testExportInvoice()
 {
     $this->markTestSkipped('FIXME: this test currently produces random results');
     $this->_createFullFixtures();
     $date = clone $this->_referenceDate;
     $i = 0;
     // until 1.7
     while ($i < 8) {
         $date->addMonth(1);
         $this->_invoiceController->createAutoInvoices($date);
         $i++;
     }
     $all = $this->_invoiceController->getAll();
     $cc3 = $this->_costcenterRecords->filter('remark', 'unittest3')->getFirstRecord();
     $cc4 = $this->_costcenterRecords->filter('remark', 'unittest4')->getFirstRecord();
     $all->setTimezone(Tinebase_Core::getUserTimezone());
     $customer3Invoices = $all->filter('costcenter_id', $cc3->getId())->sort('start_date');
     $customer4Invoices = $all->filter('costcenter_id', $cc4->getId())->sort('start_date');
     // there are timesheets in 2 intervals, so no empty invoice should be generated
     $this->assertEquals(1, $customer3Invoices->count(), 'Customer 3 must have 1 invoice!');
     // there are 2 products, interval 3,6 -> so every quarter in this and the first quarter of next year must be found
     $this->assertEquals(3, $customer4Invoices->count(), 'Customer 4 must have 3 invoices!');
     // test products export
     $definition = dirname(dirname(dirname(dirname(__FILE__)))) . '/tine20/Sales/Export/definitions/invoiceposition_default_ods.xml';
     $filter = new Sales_Model_InvoicePositionFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'invoice_id', 'operator' => 'equals', 'value' => $customer4Invoices->getFirstRecord()->getId())));
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'model', 'operator' => 'equals', 'value' => 'Sales_Model_ProductAggregate')));
     $exporter = new Sales_Export_Ods_InvoicePosition($filter, Sales_Controller_InvoicePosition::getInstance(), array('definitionFilename' => $definition));
     $doc = $exporter->generate();
     $xml = $this->_getContentXML($doc);
     $ns = $xml->getNamespaces(true);
     $spreadsheetXml = $xml->children($ns['office'])->{'body'}->{'spreadsheet'};
     // the product should be found here
     $half = 0;
     $quarter = 0;
     $i = 2;
     while ($i < 11) {
         $value = (string) $spreadsheetXml->children($ns['table'])->{'table'}->{'table-row'}->{$i}->children($ns['table'])->{'table-cell'}->{0}->children($ns['text'])->{0};
         $this->assertTrue(in_array($value, array('billhalfyearly', 'billeachquarter')), $value);
         if ($value == 'billhalfyearly') {
             $half++;
         } else {
             $quarter++;
         }
         $i++;
     }
     $this->assertEquals(6, $half);
     $this->assertEquals(3, $quarter);
     unlink($doc);
     // test timesheets export
     $definition = dirname(dirname(dirname(dirname(__FILE__)))) . '/tine20/Timetracker/Export/definitions/ts_default_ods.xml';
     $filter = new Timetracker_Model_TimesheetFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'invoice_id', 'operator' => 'equals', 'value' => $customer3Invoices->getFirstRecord()->getId())));
     $exporter = new Timetracker_Export_Ods_Timesheet($filter, Timetracker_Controller_Timesheet::getInstance(), array('definitionFilename' => $definition));
     $doc = $exporter->generate();
     $xml = $this->_getContentXML($doc);
     $spreadsheetXml = $xml->children($ns['office'])->{'body'}->{'spreadsheet'};
     $firstContentRow = $spreadsheetXml->children($ns['table'])->{'table'}->{'table-row'}->{2};
     // the timesheet should be found here
     $this->assertEquals($this->_referenceYear . '-05-06', (string) $firstContentRow->children($ns['table'])->{'table-cell'}->{0}->children($ns['text'])->{0});
     $this->assertEquals('ts from ' . $this->_referenceYear . '-05-06 00:00:00', (string) $firstContentRow->children($ns['table'])->{'table-cell'}->{1}->children($ns['text'])->{0});
     $this->assertEquals('TA-for-Customer3', (string) $firstContentRow->children($ns['table'])->{'table-cell'}->{3}->children($ns['text'])->{0});
     $this->assertEquals('1.75', (string) $firstContentRow->children($ns['table'])->{'table-cell'}->{5}->children($ns['text'])->{0});
     unlink($doc);
 }
 /**
  * resolve timeaccount ids
  */
 protected function _resolve()
 {
     if ($this->_isResolved) {
         //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' already resolved');
         return;
     }
     $this->_value = (array) $this->_value;
     // we only need to resolve the timaccount ids if user has no MANAGE_TIMEACCOUNTS grant
     if (!Timetracker_Controller_Timesheet::getInstance()->checkRight(Timetracker_Acl_Rights::MANAGE_TIMEACCOUNTS, FALSE, FALSE)) {
         // get all timeaccounts user has required grants for
         $result = array();
         foreach ($this->_requiredGrants as $grant) {
             //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' value:' . $this->_value);
             $result = array_merge($result, Timetracker_Model_TimeaccountGrants::getTimeaccountsByAcl($grant, TRUE));
         }
         $result = array_unique($result);
         //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($result, TRUE));
         // finally compute timeaccount_ids which match the filter and required grants
         switch ($this->_operator) {
             case 'equals':
             case 'in':
                 $this->_value = array_intersect($this->_value, $result);
                 break;
             case 'all':
                 $this->_value = $result;
                 break;
         }
     }
     $this->_isResolved = TRUE;
 }
Exemplo n.º 23
0
 /**
  * append acl filter
  *
  * @param Zend_Db_Select $_select
  */
 protected function _appendAclSqlFilter($_select)
 {
     if (Timetracker_Controller_Timesheet::getInstance()->checkRight(Timetracker_Acl_Rights::MANAGE_TIMEACCOUNTS, FALSE, FALSE)) {
         return;
     }
     if (!$this->_isResolved) {
         // get all timeaccounts user has required grants for
         $result = array();
         foreach ($this->_requiredGrants as $grant) {
             $result = array_merge($result, Timetracker_Model_TimeaccountGrants::getTimeaccountsByAcl($grant, TRUE));
         }
         $this->_validTimeaccounts = array_unique($result);
         $this->_isResolved = TRUE;
     }
     $db = Tinebase_Core::getDb();
     $field = $db->quoteIdentifier('id');
     $where = $db->quoteInto("{$field} IN (?)", empty($this->_validTimeaccounts) ? array('') : $this->_validTimeaccounts);
     $_select->where($where);
 }