/**
  * Get a static reference to the storage object associated with this model object
  * 
  * @return the storage object
  */
 public static function get_instance()
 {
     if (self::$so == null) {
         self::$so = CreateObject('rental.sobilling_info');
     }
     return self::$so;
 }
 public function download_export()
 {
     if (!$this->isExecutiveOfficer()) {
         $this->render('permission_denied.php');
         return;
     }
     //$browser = CreateObject('phpgwapi.browser');
     //$browser->content_header('export.txt','text/plain');
     $stop = phpgw::get_var('date');
     $cs15 = phpgw::get_var('generate_cs15');
     $toExcel = phpgw::get_var('toExcel');
     if ($cs15 == null) {
         if ($toExcel == null) {
             $export_format = explode('_', phpgw::get_var('export_format'));
             $file_ending = $export_format[1];
             if ($file_ending == 'gl07') {
                 $type = 'intern';
             } else {
                 if ($file_ending == 'lg04') {
                     $type = 'faktura';
                 }
             }
             $date = date('Ymd', $stop);
             header('Content-type: text/plain');
             header("Content-Disposition: attachment; filename=PE_{$type}_{$date}.{$file_ending}");
             $id = phpgw::get_var('id');
             $path = "/rental/billings/{$id}";
             $vfs = CreateObject('phpgwapi.vfs');
             $vfs->override_acl = 1;
             print $vfs->read(array('string' => $path, RELATIVE_NONE));
             //print rental_sobilling::get_instance()->get_export_data((int)phpgw::get_var('id'));
         } else {
             $billing_job = rental_sobilling::get_instance()->get_single((int) phpgw::get_var('id'));
             $billing_info_array = rental_sobilling_info::get_instance()->get(null, null, null, null, null, null, array('billing_id' => phpgw::get_var('id')));
             $type = phpgw::get_var('type', 'string', 'GET', 'bk');
             if ($billing_job == null) {
                 $errorMsgs[] = lang('Could not find specified billing job.');
             } else {
                 //Loop through  billing info array to find the first month
                 $month = 12;
                 foreach ($billing_info_array as $billing_info) {
                     $year = $billing_info->get_year();
                     if ($month > $billing_info->get_month()) {
                         $month = $billing_info->get_month();
                     }
                 }
                 $billing_job->set_year($year);
                 $billing_job->set_month($month);
                 $list = rental_sobilling::get_instance()->generate_export($billing_job, $type);
                 //_debug_array($list[0]);
                 /*foreach ($list as $l)
                   {
                       _debug_array($l);
                   }*/
                 if (isset($list)) {
                     $infoMsgs[] = lang('Export generated.');
                     $keys = array();
                     if (count($list[0]) > 0) {
                         foreach ($list[0] as $key => $value) {
                             if (!is_array($value)) {
                                 array_push($keys, $key);
                             }
                         }
                     }
                     // Remove newlines from output
                     //                                    $count = count($list);
                     //                                    for($i = 0; $i < $count; $i++)
                     //                                    {
                     //                                        foreach ($list[$i] as $key => &$data)
                     //                                        {
                     //                                                $data = str_replace(array("\n","\r\n", "<br>"),'',$data);
                     //                                        }
                     //                                    }
                     // Use keys as headings
                     $headings = array();
                     $count_keys = count($keys);
                     for ($j = 0; $j < $count_keys; $j++) {
                         array_push($headings, lang($keys[$j]));
                     }
                     //                                    _debug_array($list);
                     $property_common = CreateObject('property.bocommon');
                     $property_common->download($list, $keys, $headings);
                 } else {
                     $errorMsgs = lang('Export failed.');
                 }
             }
         }
     } else {
         $file_ending = 'cs15';
         $type = 'kundefil';
         $date = date('Ymd', $stop);
         header('Content-type: text/plain');
         header("Content-Disposition: attachment; filename=PE_{$type}_{$date}.{$file_ending}");
         print rental_sobilling::get_instance()->generate_customer_export((int) phpgw::get_var('id'));
     }
 }
 public function create_billing(int $decimals, int $contract_type, int $billing_term, int $year, int $month, $title, int $created_by, array $contracts_to_bill, array $contracts_overriding_billing_start, string $export_format, int $existing_billing, array $contracts_bill_only_one_time)
 {
     if ($contracts_overriding_billing_start == null) {
         $contracts_overriding_billing_start = array();
     }
     if ($contracts_bill_only_one_time == null) {
         $contracts_bill_only_one_time = array();
     }
     // We start a transaction before running the billing
     $this->db->transaction_begin();
     if ($existing_billing < 1) {
         //new billing
         $billing = new rental_billing(-1, $contract_type, $title, $created_by);
         // The billing job itself
         $billing->set_timestamp_start(time());
         // Start of run
         $billing->set_export_format($export_format);
         $billing->set_title($title);
         $this->store($billing);
         // Store job as it is
         $billing_end_timestamp = strtotime('-1 day', strtotime(($month == 12 ? $year + 1 : $year) . '-' . ($month == 12 ? '01' : $month + 1) . '-01'));
         // Last day of billing period is the last day of the month we're billing
         $counter = 0;
         $total_sum = 0;
     } else {
         $billing = $this->get_single($existing_billing);
         $billing_end_timestamp = strtotime('-1 day', strtotime(($month == 12 ? $year + 1 : $year) . '-' . ($month == 12 ? '01' : $month + 1) . '-01'));
         // Last day of billing period is the last day of the month we're billing
         $total_sum = $billing->get_total_sum();
     }
     $billing_info = new rental_billing_info(null, $billing->get_id(), $contract_type, $billing_term, $year, $month);
     $res = rental_sobilling_info::get_instance()->store($billing_info);
     // Get the number of months in selected term for contract
     $months = rental_socontract::get_instance()->get_months_in_term($billing_term);
     // The billing should start from the first date of the periode (term) we're billing for
     $first_day_of_selected_month = strtotime($year . '-' . $month . '-01');
     $bill_from_timestamp = strtotime('-' . ($months - 1) . ' month', $first_day_of_selected_month);
     foreach ($contracts_to_bill as $contract_id) {
         $invoice = rental_invoice::create_invoice($decimals, $billing->get_id(), $contract_id, in_array($contract_id, $contracts_overriding_billing_start) ? true : false, $bill_from_timestamp, $billing_end_timestamp, in_array($contract_id, $contracts_bill_only_one_time) ? true : false, false, $billing_term);
         // Creates an invoice of the contract
         if ($invoice != null) {
             $total_sum += $invoice->get_total_sum();
         }
     }
     $billing->set_total_sum(round($total_sum, $decimals));
     $billing->set_timestamp_stop(time());
     //  End of run
     $billing->set_success(true);
     // Billing job is a success
     $this->store($billing);
     // Store job now that we're done
     // End of transaction!
     if ($this->db->transaction_commit()) {
         return $billing;
     }
     throw new UnexpectedValueException('Transaction failed.');
 }