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