/**
  * 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.soinvoice');
     }
     return self::$so;
 }
    ?>
_ctrl_toggle_invoice"><?php 
    echo lang('invoice');
    ?>
</label>
			<select name="invoice_id" id="<?php 
    echo $list_id;
    ?>
_ctrl_toggle_invoice">
				<?php 
    $date_format = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
    $decimal_places = isset($GLOBALS['phpgw_info']['user']['preferences']['rental']['currency_decimal_places']) && $GLOBALS['phpgw_info']['user']['preferences']['rental']['currency_decimal_places'] ? $GLOBALS['phpgw_info']['user']['preferences']['rental']['currency_decimal_places'] : 2;
    $decimal_separator = isset($GLOBALS['phpgw_info']['user']['preferences']['rental']['decimal_separator']) && $GLOBALS['phpgw_info']['user']['preferences']['rental']['decimal_separator'] ? $GLOBALS['phpgw_info']['user']['preferences']['rental']['decimal_separator'] : ',';
    $thousands_separator = lang('currency_thousands_separator');
    $currency_suffix = isset($config->config_data['currency_suffix']) && $config->config_data['currency_suffix'] ? $config->config_data['currency_suffix'] : 'NOK';
    $invoices = rental_soinvoice::get_instance()->get(null, null, null, false, null, null, array('contract_id' => $contract->get_id()));
    if ($invoices != null && count($invoices) > 0) {
        $keys = array_keys($invoices);
        $url_add_on .= "&amp;invoice_id={$invoices[$keys[0]]->get_id()}";
        foreach ($invoices as $invoice) {
            $serial = $invoice->get_serial_number();
            $serial_number = isset($serial) ? " - " . $invoice->get_serial_number() : "";
            ?>
							<option value="<?php 
            echo $invoice->get_id();
            ?>
"><?php 
            echo "{$invoice->get_billing_title()} - " . date($date_format, $invoice->get_timestamp_created()) . " - " . number_format($invoice->get_total_sum(), $decimal_places, $decimal_separator, $thousands_separator) . " {$currency_suffix}" . $serial_number;
            ?>
</option>
							<?php 
 public function query()
 {
     if (!$this->isExecutiveOfficer()) {
         $this->render('permission_denied.php');
         return;
     }
     if ($GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'] > 0) {
         $user_rows_per_page = $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
     } else {
         $user_rows_per_page = 10;
     }
     // YUI variables for paging and sorting
     $start_index = phpgw::get_var('startIndex', 'int');
     $num_of_objects = phpgw::get_var('results', 'int', 'GET', $user_rows_per_page);
     $sort_field = phpgw::get_var('sort');
     $sort_ascending = phpgw::get_var('dir') == 'desc' ? false : true;
     // Form variables
     $search_for = phpgw::get_var('query');
     $search_type = phpgw::get_var('search_option');
     // Create an empty result set
     $result_objects = array();
     $result_count = 0;
     //Retrieve the type of query and perform type specific logic
     $query_type = phpgw::get_var('type');
     $exp_param = phpgw::get_var('export');
     $export = false;
     if (isset($exp_param)) {
         $export = true;
         $num_of_objects = null;
     }
     switch ($query_type) {
         case 'all_billings':
             $filters = array();
             if (!$sort_field) {
                 $sort_field = 'timestamp_stop';
                 $sort_ascending = false;
             } else {
                 if ($sort_field == 'responsibility_title') {
                     $sort_field = 'location_id';
                 }
             }
             $result_objects = rental_sobilling::get_instance()->get($start_index, $num_of_objects, $sort_field, $sort_ascending, $search_for, $search_type, $filters);
             $object_count = rental_sobilling::get_instance()->get_count($search_for, $search_type, $filters);
             break;
         case 'invoices':
             if ($sort_field == 'term_label') {
                 $sort_field = 'term_id';
             }
             $filters = array('billing_id' => phpgw::get_var('billing_id'));
             $result_objects = rental_soinvoice::get_instance()->get($start_index, $num_of_objects, $sort_field, $sort_ascending, $search_for, $search_type, $filters);
             $object_count = rental_soinvoice::get_instance()->get_count($search_for, $search_type, $filters);
             break;
     }
     //Create an empty row set
     $rows = array();
     foreach ($result_objects as $result) {
         if (isset($result)) {
             if ($result->has_permission(PHPGW_ACL_READ)) {
                 // ... add a serialized result
                 $rows[] = $result->serialize();
             }
         }
     }
     // ... add result data
     $result_data = array('results' => $rows, 'total_records' => $object_count);
     if (!$export) {
         //Add action column to each row in result table
         array_walk($result_data['results'], array($this, 'add_actions'), array($query_type));
     }
     return $this->yui_results($result_data, 'total_records', 'results');
 }
 /**
  * Does all the dirty work by building all the lines of Agresso contents
  * from the billing job.
  */
 protected function run()
 {
     $this->lines = array();
     $decimal_separator = isset($GLOBALS['phpgw_info']['user']['preferences']['rental']['decimal_separator']) ? $GLOBALS['phpgw_info']['user']['preferences']['rental']['decimal_separator'] : ',';
     $thousands_separator = isset($GLOBALS['phpgw_info']['user']['preferences']['rental']['thousands_separator']) ? $GLOBALS['phpgw_info']['user']['preferences']['rental']['thousands_separator'] : '.';
     // We need all invoices for this billing
     $invoices = rental_soinvoice::get_instance()->get(null, null, 'id', true, null, null, array('billing_id' => $this->billing_job->get_id()));
     foreach ($invoices as $invoice) {
         // We need all price items in the invoice
         $price_items = rental_soinvoice_price_item::get_instance()->get(null, null, null, null, null, null, array('invoice_id' => $invoice->get_id()));
         // HACK to get the needed location code for the building
         $building_location_code = rental_socomposite::get_instance()->get_building_location_code($invoice->get_contract_id());
         $description = "{$invoice->get_old_contract_id()}, " . number_format($invoice->get_total_area(), 1, $decimal_separator, $thousands_separator) . " m2 - {$invoice->get_header()}";
         $responsibility_in = isset($GLOBALS['phpgw_info']['user']['preferences']['rental']['responsibility']) ? $GLOBALS['phpgw_info']['user']['preferences']['rental']['responsibility'] : '028120';
         $project_id_in = isset($GLOBALS['phpgw_info']['user']['preferences']['rental']['project_id']) ? $GLOBALS['phpgw_info']['user']['preferences']['rental']['project_id'] : '9';
         // The income side
         foreach ($price_items as $price_item) {
             $this->lines[] = $this->get_line($invoice->get_account_in(), $responsibility_in, $invoice->get_service_id(), $building_location_code, $project_id_in, $price_item->get_agresso_id(), -1.0 * $price_item->get_total_price(), $description, $invoice->get_contract_id(), $this->billing_job->get_year(), $this->billing_job->get_month());
         }
         // The receiver's outlay side
         $this->lines[] = $this->get_line($invoice->get_account_out(), $invoice->get_responsibility_id(), $invoice->get_service_id(), $building_location_code, $invoice->get_project_id(), '', $invoice->get_total_sum(), $description, $invoice->get_contract_id(), $this->billing_job->get_year(), $this->billing_job->get_month());
     }
 }
 /**
  * Create invoice
  * 
  * @param int $decimals	the number of decimals on the total sum of the onvoice
  * @param int $billing_id	the billing this invoice is part of
  * @param int $contract_id	the contract
  * @param bool $override	flag to indicate if the invoice start period should be overridden with the billing start date of contract
  * @param int $timestamp_invoice_start	the startdate of the invoice period
  * @param int $timestamp_invoice_end	the enddate of the invoice period
  * @param bool $bill_only_one_time	flag to indicate if the the invoice should only bil one time price elements
  * @return rental_invoice	the newly created invoice
  */
 public static function create_invoice(int $decimals, int $billing_id, int $contract_id, bool $override, int $timestamp_invoice_start, int $timestamp_invoice_end, $bill_only_one_time, $dry_run = false, $billing_term = 0)
 {
     $contract = rental_socontract::get_instance()->get_single($contract_id);
     // If the invoice period should be overriden with the biling start date
     if ($override) {
         $timestamp_invoice_start = $contract->get_billing_start_date();
     }
     // If no account out is specified: check if the contract type defines any data to be used in this field (AGRESSO specific logic)
     $account_out = $contract->get_account_out();
     if (!isset($account_out) || $account_out == '') {
         //If no account out - check the contract type for default
         $account_tmp = rental_socontract::get_instance()->get_contract_type_account($contract->get_contract_type_id());
         if (isset($account_tmp) && $account_tmp != '') {
             $account_out = $account_tmp;
         } else {
             $account_out = rental_socontract::get_instance()->get_default_account($contract->get_location_id(), false);
         }
     }
     // Create invoice ...
     $invoice = new rental_invoice(-1, $billing_id, $contract_id, time(), $timestamp_invoice_start, $timestamp_invoice_end, 0, $contract->get_rented_area(), $contract->get_invoice_header(), $contract->get_account_in(), $account_out, $contract->get_service_id(), $contract->get_responsibility_id());
     // ... and add party identifier, project number and the old contract identifier
     $invoice->set_party_id($contract->get_payer_id());
     $invoice->set_project_id($contract->get_project_id());
     $invoice->set_old_contract_id($contract->get_old_contract_id());
     if (!$dry_run) {
         rental_soinvoice::get_instance()->store($invoice);
         // We must store the invoice at this point to have an id to give to the price item
     }
     // Retrieve the contract price items: only one-time or all
     if ($bill_only_one_time) {
         $filters2 = array('contract_id' => $contract->get_id(), 'contract_ids_one_time' => true, 'billing_term_id' => $billing_term, 'year' => date('Y', $timestamp_invoice_start), 'month' => date('m', $timestamp_invoice_start));
         //$contract_price_items = $socontract_price_item->get($start_index, $num_of_objects, $sort_field, $sort_ascending, $search_for, $search_type, $filters2);
         $contract_price_items = rental_socontract_price_item::get_instance()->get(null, null, null, null, null, null, $filters2);
         //$contract_price_items = rental_socontract_price_item::get_instance()->get(null, null, null, null, null, null, array('contract_id' => $contract->get_id(), 'one_time' => true));
     } else {
         $contract_price_items = rental_socontract_price_item::get_instance()->get(null, null, null, null, null, null, array('contract_id' => $contract->get_id()));
     }
     $total_sum = 0;
     // Holding the total price of the invoice
     $contract_dates = $contract->get_contract_date();
     if (isset($contract_dates)) {
         $contract_start = $contract->get_contract_date()->get_start_date();
         $contract_end = $contract->get_contract_date()->get_end_date();
     }
     // Run through the contract price items
     foreach ($contract_price_items as $contract_price_item) {
         // ---- Period calculation ---
         // Determine start date for price item
         $contract_price_item_start = $contract_price_item->get_date_start();
         if ($contract_price_item_start == null || $contract_price_item_start == '') {
             // We just use the invoice date for our calculations
             $contract_price_item_start = $timestamp_invoice_start;
         }
         // Determine end date for price item
         $contract_price_item_end = $contract_price_item->get_date_end();
         if ($contract_price_item_end == null || $contract_price_item_end == '') {
             // We just use the invoice date for our calculations
             $contract_price_item_end = $timestamp_invoice_end;
         }
         // Sanity check - end date should never be before start date
         if ($contract_price_item_end < $contract_price_item_start) {
             continue;
             // We don't add this price item - continue to next
         }
         // Checking the start date against the invoice dates
         if ($contract_price_item_start < $timestamp_invoice_start) {
             $invoice_price_item_start = $timestamp_invoice_start;
             // We use the invoice start
         } else {
             if ($contract_price_item_start > $timestamp_invoice_end) {
                 continue;
                 // We don't add this price item - continue to next
             } else {
                 $invoice_price_item_start = $contract_price_item_start;
                 // We use the price item start
             }
         }
         // Checking the end date against invoice dates
         if ($contract_price_item_end < $timestamp_invoice_start) {
             continue;
             // We don't add this price item - continue to next
         } else {
             if ($contract_price_item_end < $timestamp_invoice_end) {
                 $invoice_price_item_end = $contract_price_item_end;
                 // We use the price item end
             } else {
                 $invoice_price_item_end = $timestamp_invoice_end;
                 // We use the invoice end
             }
         }
         // Checking the contract dates against the temporary price item dates
         if (isset($contract_start) && !$contract_price_item->is_one_time()) {
             if ($contract_start > $timestamp_invoice_end) {
                 continue;
                 //No price items for this contract will be billed
             }
             if ($contract_start > $invoice_price_item_start) {
                 $invoice_price_item_start = $contract_start;
             }
         }
         if (isset($contract_end) && !$contract_price_item->is_one_time()) {
             if ($contract_end < $timestamp_invoice_start) {
                 continue;
                 //No price items for this contract will be billed
             }
             if ($contract_end < $invoice_price_item_end) {
                 $invoice_price_item_end = $contract_end;
             }
         }
         // --- End of period calculation ---
         // Create a new invoice price item
         $invoice_price_item = new rental_invoice_price_item($decimals, -1, $invoice->get_id(), $contract_price_item->get_title(), $contract_price_item->get_agresso_id(), $contract_price_item->is_area(), $contract_price_item->get_price(), $contract_price_item->get_area(), $contract_price_item->get_count(), $invoice_price_item_start, $invoice_price_item_end);
         // If the contract price item is of type one-time and it's dates are within the invoice period ...
         if ($contract_price_item->is_one_time()) {
             if ($contract_price_item_start >= $timestamp_invoice_start && $contract_price_item_start <= $timestamp_invoice_end) {
                 // ... set the total price of the invoice price item to the total price of the contract price item
                 $invoice_price_item->set_total_price($contract_price_item->get_total_price());
                 // ... and set the contract price item as billed
                 $contract_price_item->set_is_billed(true);
                 if (!$dry_run) {
                     rental_socontract_price_item::get_instance()->store($contract_price_item);
                 }
             }
         }
         if (!$dry_run) {
             // Store the invoice price item
             rental_soinvoice_price_item::get_instance()->store($invoice_price_item);
         }
         // Add the price item to the invoice
         $invoice->add_invoice_price_item($invoice_price_item);
         // Add this price item's total sum to the tota sum of the invoice
         $total_sum += $invoice_price_item->get_total_price();
     }
     // end of looping through the contract price items
     // Set the total sum of the invoice rounded to the specified number of decimals
     $invoice->set_total_sum(round($total_sum, $decimals));
     if (!$dry_run) {
         // ... and store the invoice
         rental_soinvoice::get_instance()->store($invoice);
     }
     return $invoice;
 }
 protected function run_excel_export($excel_export_type)
 {
     switch ($excel_export_type) {
         case 'bk':
             $get_order_excel = 'get_order_excel_bk';
             break;
         case 'nlsh':
             $get_order_excel = 'get_order_excel_nlsh';
             break;
         default:
             $get_order_excel = 'get_order_excel_bk';
             break;
     }
     $this->orders = array();
     $decimal_separator = isset($GLOBALS['phpgw_info']['user']['preferences']['rental']['decimal_separator']) ? $GLOBALS['phpgw_info']['user']['preferences']['rental']['decimal_separator'] : ',';
     $thousands_separator = isset($GLOBALS['phpgw_info']['user']['preferences']['rental']['thousands_separator']) ? $GLOBALS['phpgw_info']['user']['preferences']['rental']['thousands_separator'] : '.';
     // We need all invoices for this billing
     $invoices = rental_soinvoice::get_instance()->get(null, null, 'id', true, null, null, array('billing_id' => $this->billing_job->get_id()));
     $dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
     foreach ($invoices as $invoice) {
         // We need all price items in the invoice
         $price_items = rental_soinvoice_price_item::get_instance()->get(null, null, null, null, null, null, array('invoice_id' => $invoice->get_id()));
         $composite_name = '';
         // We need to get the composites to get a composite name for the Agresso export
         $composites = rental_socomposite::get_instance()->get(null, null, null, null, null, null, array('contract_id' => $invoice->get_contract_id()));
         if ($composites != null && count($composites) > 0) {
             $keys = array_keys($composites);
             $composite_name = $composites[$keys[0]]->get_name();
         }
         // HACK to get the needed location code for the building
         $building_location_code = rental_socomposite::get_instance()->get_building_location_code($invoice->get_contract_id());
         /**Sigurd:Start contract type**/
         $contract = rental_socontract::get_instance()->get_single($invoice->get_contract_id());
         $current_contract_type_id = $contract->get_contract_type_id();
         $contract_type_label = lang(rental_socontract::get_instance()->get_contract_type_label($current_contract_type_id));
         $contract_id = $contract->get_old_contract_id();
         $party_names = explode('<br/>', rtrim($contract->get_party_name(), '<br/>'));
         $start_date = $GLOBALS['phpgw']->common->show_date($contract->get_contract_date()->get_start_date(), $dateformat);
         $end_date = $GLOBALS['phpgw']->common->show_date($contract->get_contract_date()->get_end_date(), $dateformat);
         $billing_start_date = $GLOBALS['phpgw']->common->show_date($contract->get_billing_start_date(), $dateformat);
         $billing_end_date = $GLOBALS['phpgw']->common->show_date($contract->get_billing_end_date(), $dateformat);
         /**End contract type**/
         $price_item_data = array();
         $price_item_counter = 0;
         foreach ($price_items as $price_item) {
             $data = array();
             $data['amount'] = $price_item->get_total_price();
             $description = $price_item->get_title();
             $start = $price_item->get_timestamp_start();
             $stop = $price_item->get_timestamp_end();
             if (isset($start) && isset($stop)) {
                 $description .= ' ' . date('j/n', $start) . '-' . date('j/n', $stop);
             }
             $data['article_description'] = $description;
             $data['article_code'] = $price_item->get_agresso_id();
             $price_item_data[] = $data;
             $serialized_party = $invoice->get_party()->serialize();
             $party_name = $serialized_party['name'];
             $_party_names = array();
             if (count($party_names) > 1) {
                 foreach ($party_names as $value) {
                     if ($party_name == $value) {
                         continue;
                     }
                     $_party_names[] = $value;
                 }
             } else {
                 $_party_names = $party_names;
             }
             $party_full_name = implode(', ', $_party_names);
             $this->orders[] = $this->{$get_order_excel}($start_date, $end_date, $billing_start_date, $billing_end_date, $invoice->get_header(), $invoice->get_party()->get_identifier(), $party_name, $serialized_party['address'], $party_full_name, $invoice->get_id(), $this->billing_job->get_year(), $this->billing_job->get_month(), $invoice->get_account_out(), $data, $invoice->get_responsibility_id(), $invoice->get_service_id(), $building_location_code, $invoice->get_project_id(), $composite_name, $invoice->get_reference(), $price_item_counter, $invoice->get_account_in(), $invoice->get_responsibility_id(), $contract_type_label, $contract_id);
             $price_item_counter++;
         }
     }
 }