/**
  * 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);
 }
 /**
  * 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;
 }