/**
  * make sure that timesheets get created for the right month
  */
 public function test2MonthIntervalTimesheetOnMonthEndAndBegin()
 {
     $dates = array(clone $this->_referenceDate, clone $this->_referenceDate, clone $this->_referenceDate, clone $this->_referenceDate);
     // 0: 1.1.xxxx, 1: 31.1.xxxx, 2: 1.2.xxxx, 3: 28/29.2.xxxx
     $dates[1]->addMonth(1)->subDay(1);
     $dates[2]->addMonth(1);
     $dates[3]->addMonth(2)->subDay(1);
     // create much more timesheets
     $dt = clone $this->_referenceDate;
     for ($i = 0; $i < 80; $i++) {
         $dt->addHour(12);
         $dates[] = clone $dt;
     }
     $customer = $this->_createCustomers(1)->getFirstRecord();
     $this->_createCostCenters();
     // has no budget
     $ta = $this->_createTimeaccounts(array(array('title' => 'TaTest', 'description' => 'blabla', 'is_open' => 1, 'status' => 'not yet billed', 'budget' => null)))->getFirstRecord();
     foreach ($dates as $date) {
         $this->_createTimesheets(array(array('account_id' => Tinebase_Core::getUser()->getId(), 'timeaccount_id' => $ta->getId(), 'start_date' => $date, 'duration' => 105, 'description' => 'ts from ' . (string) $date)));
     }
     $this->assertEquals(84, $this->_timesheetRecords->count());
     $this->_createProducts();
     $csDate = clone $this->_referenceDate;
     $csDate->subMonth(10);
     $lab = clone $this->_referenceDate;
     $this->_createContracts(array(array('number' => 100, 'title' => 'MyContract', 'description' => 'unittest', 'container_id' => $this->_sharedContractsContainerId, 'billing_point' => 'begin', 'billing_address_id' => $this->_addressRecords->filter('customer_id', $customer->getId())->filter('type', 'billing')->getFirstRecord()->getId(), 'start_date' => $csDate, 'end_date' => NULL, 'products' => array(array('start_date' => $csDate, 'end_date' => NULL, 'quantity' => 1, 'interval' => 1, 'billing_point' => 'end', 'product_id' => $this->_productRecords->filter('name', 'Hours')->getFirstRecord()->getId())))));
     $json = new Sales_Frontend_Json();
     $date = clone $this->_referenceDate;
     // this is set by cli if called by cli
     $date->setTime(3, 0, 0);
     $result = $this->_invoiceController->createAutoInvoices($date);
     $this->assertEquals(0, $result['created_count']);
     $date->addMonth(1);
     $result = $this->_invoiceController->createAutoInvoices($date);
     $this->assertEquals(1, $result['created_count']);
     $invoice1Id = $result['created'][0];
     $filter = new Timetracker_Model_TimesheetFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'invoice_id', 'operator' => 'equals', 'value' => $invoice1Id)));
     $timesheets = $this->_timesheetController->search($filter);
     $this->assertEquals(63, $timesheets->count());
     $date->addMonth(1);
     $result = $this->_invoiceController->createAutoInvoices($date);
     $this->assertEquals(1, $result['created_count'], (string) $date);
     $invoice2Id = $result['created'][0];
     $invoice = $json->getInvoice($invoice2Id);
     $this->assertEquals(1, count($invoice['positions']));
     $date->addMonth(1);
     $result = $this->_invoiceController->createAutoInvoices($date);
     $this->assertEquals(0, $result['created_count']);
     $filter = new Timetracker_Model_TimesheetFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'invoice_id', 'operator' => 'equals', 'value' => $invoice2Id)));
     $timesheets = $this->_timesheetController->search($filter);
     $this->assertEquals(21, $timesheets->count());
 }
 /**
  * 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);
     }
 }