fixes invoices Timesheet export for Timeaccounts with a budget
authorPhilipp Schüle <p.schuele@metaways.de>
Wed, 11 Mar 2015 12:14:39 +0000 (13:14 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 12 Mar 2015 09:16:47 +0000 (10:16 +0100)
 * ALL Timesheets had been exported before regardless of billed state

Change-Id: Icff16df77066771db8e4d25a48b5d6f2b6a5f05e
Reviewed-on: http://gerrit.tine20.com/customers/1721
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Timetracker/Model/Timeaccount.php

index 821ef60..87c4bd6 100644 (file)
@@ -148,11 +148,10 @@ class Timetracker_Model_Timeaccount extends Sales_Model_Accountable_Abstract
      * @param mixed $_data
      * @param bool $_bypassFilters
      * @param mixed $_convertDates
-     * @return void
      */
     public function __construct($_data = NULL, $_bypassFilters = false, $_convertDates = true)
     {
-        $this->_filters['budget']  = array('Digits', new Zend_Filter_Empty(NULL));
+        $this->_filters['budget']  = new Zend_Filter_Empty(0);
         $this->_filters['price'] = array(new Zend_Filter_PregReplace('/,/', '.'), new Zend_Filter_Empty(NULL));
         $this->_filters['is_open'] = new Zend_Filter_Empty(0);
         $this->_filters['invoice_id']  = array(new Zend_Filter_Empty(NULL));
@@ -295,6 +294,8 @@ class Timetracker_Model_Timeaccount extends Sales_Model_Accountable_Abstract
             
         } else {
             $filter = $this->_getBillableTimesheetsFilter($date);
+            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);
             
             foreach($timesheets as $timesheet) {
@@ -346,7 +347,9 @@ class Timetracker_Model_Timeaccount extends Sales_Model_Accountable_Abstract
             $filter = $this->_getBillableTimesheetsFilter($date, $contract);
             
             if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
-                Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Use filter in "isBillable"-Method of Timetracker_Model_Timeaccount: ' . print_r($filter->toArray(), 1));
+                Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+                    . ' Use filter in "isBillable"-Method of Timetracker_Model_Timeaccount: '
+                    . print_r($filter->toArray(), 1));
             }
             
             $timesheets = Timetracker_Controller_Timesheet::getInstance()->search($filter, $pagination, FALSE, /* $_onlyIds = */ TRUE);
@@ -401,12 +404,19 @@ class Timetracker_Model_Timeaccount extends Sales_Model_Accountable_Abstract
             // set this ta billed
             $this->invoice_id = $invoice->getId();
             Timetracker_Controller_Timeaccount::getInstance()->update($this, FALSE);
-            
-            // set all ts of this ta billed
-            $filter = new Timetracker_Model_TimesheetFilter(array());
+
+            if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+                . ' TA got budget: set all unbilled TS of this TA billed');
+            if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                . ' TA:' . print_r($this->toArray(), true));
+
+            $filter = new Timetracker_Model_TimesheetFilter(array(
+                array('field' => 'is_cleared', 'operator' => 'equals', 'value' => FALSE),
+                array('field' => 'is_billable', 'operator' => 'equals', 'value' => TRUE),
+            ), 'AND');
+            // NOTE: using text filter here for id (operator equals is not defined in default timeaccount_id filter)
             $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'timeaccount_id', 'operator' => 'equals', 'value' => $this->getId())));
-            
-            
+
             $tsController->updateMultiple($filter, array('invoice_id' => $invoice->getId()));
         } else {
             $ids = $this->_getIdsOfBillables();
@@ -414,7 +424,10 @@ class Timetracker_Model_Timeaccount extends Sales_Model_Accountable_Abstract
             if (! empty($ids)) {
                 $filter = new Timetracker_Model_TimesheetFilter(array());
                 $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'id', 'operator' => 'in', 'value' => $ids)));
-                
+
+                if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+                    . ' Bill ' . count($ids) . ' TS');
+
                 $tsController->updateMultiple($filter, array('invoice_id' => $invoice->getId()));
             }
         }