fmsystem-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Fmsystem-commits] [15206] added is_one_time field on tables_current


From: erikhl
Subject: [Fmsystem-commits] [15206] added is_one_time field on tables_current
Date: Thu, 26 May 2016 06:48:37 +0000 (UTC)

Revision: 15206
          http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=15206
Author:   erikhl
Date:     2016-05-26 06:48:36 +0000 (Thu, 26 May 2016)
Log Message:
-----------
added is_one_time field on tables_current

Modified Paths:
--------------
    trunk/rental/inc/class.soinvoice_price_item.inc.php
    trunk/rental/inc/model/class.agresso_lg04.inc.php
    trunk/rental/inc/model/class.invoice.inc.php
    trunk/rental/inc/model/class.invoice_price_item.inc.php
    trunk/rental/setup/tables_current.inc.php
    trunk/rental/setup/tables_update.inc.php

Modified: trunk/rental/inc/class.soinvoice_price_item.inc.php
===================================================================
--- trunk/rental/inc/class.soinvoice_price_item.inc.php 2016-05-26 01:41:27 UTC 
(rev 15205)
+++ trunk/rental/inc/class.soinvoice_price_item.inc.php 2016-05-26 06:48:36 UTC 
(rev 15206)
@@ -1,119 +1,119 @@
-<?php
-       phpgw::import_class('rental.socommon');
-
-       class rental_soinvoice_price_item extends rental_socommon
-       {
-
-               protected static $so;
-
-               /**
-                * 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_price_item');
-                       }
-                       return self::$so;
-               }
-
-               protected function get_id_field_name()
-               {
-                       return 'id';
-               }
-
-               protected function get_query( string $sort_field, bool 
$ascending, string $search_for, string $search_type, array $filters, bool 
$return_count )
-               {
-                       $clauses = array('1=1');
-                       if (isset($filters[$this->get_id_field_name()]))
-                       {
-                               $filter_clauses[] = 
"{$this->marshal($this->get_id_field_name(), 'field')} = 
{$this->marshal($filters[$this->get_id_field_name()], 'int')}";
-                       }
-                       if (isset($filters['invoice_id']))
-                       {
-                               $filter_clauses[] = "invoice_id = 
{$this->marshal($filters['invoice_id'], 'int')}";
-                       }
-                       if (isset($filters['billing_id']))
-                       {
-                               $filter_clauses[] = "billing_id = 
{$this->marshal($filters['billing_id'], 'int')}";
-                       }
-                       if (count($filter_clauses))
-                       {
-                               $clauses[] = join(' AND ', $filter_clauses);
-                       }
-                       $condition = join(' AND ', $clauses);
-
-                       $tables = "rental_invoice_price_item";
-                       $joins = "      {$this->left_join} rental_invoice ON 
(rental_invoice.id = rental_invoice_price_item.invoice_id)";
-                       if ($return_count) // We should only return a count
-                       {
-                               $cols = 
'COUNT(DISTINCT(rental_invoice_price_item.id)) AS count';
-                       }
-                       else
-                       {
-                               $cols = 'rental_invoice_price_item.id, 
invoice_id, title, area, count, agresso_id, is_area, is_one_time, price, 
total_price, date_start, date_end';
-                       }
-                       $dir = $ascending ? 'ASC' : 'DESC';
-                       $order = $sort_field ? "ORDER BY 
{$this->marshal($sort_field, 'field')} $dir " : ($return_count ? '' : 'ORDER BY 
rental_invoice_price_item.id ASC');
-                       return "SELECT {$cols} FROM {$tables} {$joins} WHERE 
{$condition} {$order}";
-               }
-
-               protected function populate( int $price_item_id, &$price_item )
-               {
-                       if ($price_item == null)
-                       {
-                               $price_item = new rental_invoice_price_item(
-                                       0,
-                                       $this->db->f('id', true),
-                                       $this->db->f('invoice_id', true),
-                                       $this->db->f('title', true),
-                                       $this->db->f('agresso_id', true),
-                                       $this->db->f('is_area', true),
-                                       $this->db->f('price', true),
-                                       $this->db->f('area', true),
-                                       $this->db->f('count', true),
-                                       strtotime($this->db->f('date_start', 
true)),
-                                       strtotime($this->db->f('date_end', 
true))
-                               );
-       
-                               
$price_item->set_total_price($this->db->f('total_price', true));
-                               
$price_item->set_is_one_time((bool)$this->db->f('is_one_time'));
-                       }
-                       return $price_item;
-               }
-
-               public function add( &$invoice_price_item )
-               {
-                       $values = array
-                               (
-                               
$this->marshal($invoice_price_item->get_invoice_id(), 'int'),
-                               
$this->marshal($invoice_price_item->get_title(), 'string'),
-                               
$this->marshal($invoice_price_item->get_agresso_id(), 'string'),
-                               $invoice_price_item->is_area() ? 'true' : 
'false',
-                               
$this->marshal($invoice_price_item->get_price(), 'float'),
-                               $this->marshal($invoice_price_item->get_area(), 
'float'),
-                               
$this->marshal($invoice_price_item->get_count(), 'int'),
-                               
$this->marshal($invoice_price_item->get_total_price(), 'float'),
-                               $this->marshal(date('Y-m-d', 
$invoice_price_item->get_timestamp_start()), 'date'),
-                               $this->marshal(date('Y-m-d', 
$invoice_price_item->get_timestamp_end()), 'date'),
-                               $invoice_price_item->is_one_time() ? 'true' : 
'false',
-                       );
-                       $query = "INSERT INTO rental_invoice_price_item 
(invoice_id, title, agresso_id, is_area, price, area, count, total_price, 
date_start, date_end,is_one_time) VALUES (" . join(',', $values) . ")";
-                       $receipt = null;
-                       if ($this->db->query($query))
-                       {
-                               $receipt = array();
-                               $receipt['id'] = 
$this->db->get_last_insert_id('rental_invoice_price_item', 'id');
-                               $invoice_price_item->set_id($receipt['id']);
-                       }
-                       return $receipt;
-               }
-
-               protected function update( $object )
-               {
-                       throw new Exception("Not implemented");
-               }
-       }
\ No newline at end of file
+<?php
+       phpgw::import_class('rental.socommon');
+
+       class rental_soinvoice_price_item extends rental_socommon
+       {
+
+               protected static $so;
+
+               /**
+                * 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_price_item');
+                       }
+                       return self::$so;
+               }
+
+               protected function get_id_field_name()
+               {
+                       return 'id';
+               }
+
+               protected function get_query( string $sort_field, bool 
$ascending, string $search_for, string $search_type, array $filters, bool 
$return_count )
+               {
+                       $clauses = array('1=1');
+                       if (isset($filters[$this->get_id_field_name()]))
+                       {
+                               $filter_clauses[] = 
"{$this->marshal($this->get_id_field_name(), 'field')} = 
{$this->marshal($filters[$this->get_id_field_name()], 'int')}";
+                       }
+                       if (isset($filters['invoice_id']))
+                       {
+                               $filter_clauses[] = "invoice_id = 
{$this->marshal($filters['invoice_id'], 'int')}";
+                       }
+                       if (isset($filters['billing_id']))
+                       {
+                               $filter_clauses[] = "billing_id = 
{$this->marshal($filters['billing_id'], 'int')}";
+                       }
+                       if (count($filter_clauses))
+                       {
+                               $clauses[] = join(' AND ', $filter_clauses);
+                       }
+                       $condition = join(' AND ', $clauses);
+
+                       $tables = "rental_invoice_price_item";
+                       $joins = "      {$this->left_join} rental_invoice ON 
(rental_invoice.id = rental_invoice_price_item.invoice_id)";
+                       if ($return_count) // We should only return a count
+                       {
+                               $cols = 
'COUNT(DISTINCT(rental_invoice_price_item.id)) AS count';
+                       }
+                       else
+                       {
+                               $cols = 'rental_invoice_price_item.id, 
invoice_id, title, area, count, agresso_id, is_area, is_one_time, price, 
total_price, date_start, date_end';
+                       }
+                       $dir = $ascending ? 'ASC' : 'DESC';
+                       $order = $sort_field ? "ORDER BY 
{$this->marshal($sort_field, 'field')} $dir " : ($return_count ? '' : 'ORDER BY 
rental_invoice_price_item.id ASC');
+                       return "SELECT {$cols} FROM {$tables} {$joins} WHERE 
{$condition} {$order}";
+               }
+
+               protected function populate( int $price_item_id, &$price_item )
+               {
+                       if ($price_item == null)
+                       {
+                               $price_item = new rental_invoice_price_item(
+                                       0,
+                                       $this->db->f('id', true),
+                                       $this->db->f('invoice_id', true),
+                                       $this->db->f('title', true),
+                                       $this->db->f('agresso_id', true),
+                                       $this->db->f('is_area', true),
+                                       $this->db->f('price', true),
+                                       $this->db->f('area', true),
+                                       $this->db->f('count', true),
+                                       strtotime($this->db->f('date_start', 
true)),
+                                       strtotime($this->db->f('date_end', 
true))
+                               );
+       
+                               
$price_item->set_total_price($this->db->f('total_price', true));
+                               
$price_item->set_is_one_time((bool)$this->db->f('is_one_time'));
+                       }
+                       return $price_item;
+               }
+
+               public function add( &$invoice_price_item )
+               {
+                       $values = array
+                               (
+                               
$this->marshal($invoice_price_item->get_invoice_id(), 'int'),
+                               
$this->marshal($invoice_price_item->get_title(), 'string'),
+                               
$this->marshal($invoice_price_item->get_agresso_id(), 'string'),
+                               $invoice_price_item->is_area() ? 'true' : 
'false',
+                               
$this->marshal($invoice_price_item->get_price(), 'float'),
+                               $this->marshal($invoice_price_item->get_area(), 
'float'),
+                               
$this->marshal($invoice_price_item->get_count(), 'int'),
+                               
$this->marshal($invoice_price_item->get_total_price(), 'float'),
+                               $this->marshal(date('Y-m-d', 
$invoice_price_item->get_timestamp_start()), 'date'),
+                               $this->marshal(date('Y-m-d', 
$invoice_price_item->get_timestamp_end()), 'date'),
+                               $invoice_price_item->is_one_time() ? 'true' : 
'false',
+                       );
+                       $query = "INSERT INTO rental_invoice_price_item 
(invoice_id, title, agresso_id, is_area, price, area, count, total_price, 
date_start, date_end,is_one_time) VALUES (" . join(',', $values) . ")";
+                       $receipt = null;
+                       if ($this->db->query($query))
+                       {
+                               $receipt = array();
+                               $receipt['id'] = 
$this->db->get_last_insert_id('rental_invoice_price_item', 'id');
+                               $invoice_price_item->set_id($receipt['id']);
+                       }
+                       return $receipt;
+               }
+
+               protected function update( $object )
+               {
+                       throw new Exception("Not implemented");
+               }
+       }

Modified: trunk/rental/inc/model/class.agresso_lg04.inc.php
===================================================================
--- trunk/rental/inc/model/class.agresso_lg04.inc.php   2016-05-26 01:41:27 UTC 
(rev 15205)
+++ trunk/rental/inc/model/class.agresso_lg04.inc.php   2016-05-26 06:48:36 UTC 
(rev 15206)
@@ -1,578 +1,578 @@
-<?php
-       phpgw::import_class('rental.socomposite');
-       phpgw::import_class('rental.socontract');
-       include_class('rental', 'exportable', 'inc/model/');
-
-       class rental_agresso_lg04 implements rental_exportable
-       {
-
-               protected $billing_job;
-               protected $date_str;
-               protected $orders;
-
-               public function __construct( $billing_job )
-               {
-                       $this->billing_job = $billing_job;
-                       $this->date_str = date('ymd', 
$billing_job->get_timestamp_stop());
-                       $this->orders = null;
-               }
-
-               /**
-                * @see rental_exportable
-                */
-               public function get_id()
-               {
-                       return 'Agresso LG04';
-               }
-
-               /**
-                * Returns the file contents as a string.
-                * 
-                * @see rental_exportable
-                */
-               public function get_contents()
-               {
-                       $contents = '';
-                       if ($this->orders == null) // Data hasn't been created 
yet
-                       {
-                               $this->run();
-                       }
-                       foreach ($this->orders as $order)
-                       {
-                               foreach ($order as $line)
-                               {
-                                       $contents .= "{$line}\n";
-                               }
-                       }
-                       return $contents;
-               }
-
-               public function get_contents_excel( $excel_export_type )
-               {
-                       if ($this->orders == null) // Data hasn't been created 
yet
-                       {
-                               $this->run_excel_export($excel_export_type);
-                       }
-                       return $this->orders;
-               }
-
-               public function get_missing_billing_info( $contract )
-               {
-                       $missing_billing_info = array();
-                       $contract_parties = $contract->get_parties();
-                       if ($contract_parties == null || 
count($contract_parties) < 1)
-                       {
-                               $missing_billing_info[] = 'Missing contract 
party.';
-                       }
-
-                       $payer_id = $contract->get_payer_id();
-                       if ($payer_id == null || $payer_id = 0)
-                       {
-                               $missing_billing_info[] = 'Missing payer id.';
-                       }
-
-                       if(!$contract->get_billing_start_date())
-                       {
-                               $missing_billing_info[] = 'Missing start_date.';
-                       }
-                       return $missing_billing_info;
-               }
-
-               /**
-                * Does all the dirty work by building all the lines of Agresso 
contents
-                * from the billing job.
-                */
-               protected function run()
-               {
-                       $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(0, 0, 
'id', true, '', '', array(
-                               'billing_id' => $this->billing_job->get_id()));
-
-
-                       $config = CreateObject('phpgwapi.config', 'rental');
-                       $config->read();
-                       $serial_config_start = 
$config->config_data['serial_start'];
-                       $serial_config_stop = 
$config->config_data['serial_stop'];
-
-
-                       if (isset($serial_config_start) && 
is_numeric($serial_config_start) &&
-                               isset($serial_config_stop) && 
is_numeric($serial_config_stop))
-                       {
-                               $max_serial_number_used = 
rental_soinvoice::get_instance()->get_max_serial_number_used($serial_config_start,
 $serial_config_stop);
-
-                               if (isset($max_serial_number_used) && 
is_numeric($max_serial_number_used) && $max_serial_number_used > 0)
-                               {
-                                       $serial_number = 
$max_serial_number_used + 1;
-                               }
-                               else
-                               {
-                                       $serial_number = $serial_config_start;
-                               }
-
-                               $number_left_in_sequence = $serial_config_stop 
- $serial_number;
-
-                               if ($number_left_in_sequence < count($invoices))
-                               {
-                                       //var_dump("Out of sequence numbers");
-                                       //Give error message (out of sequence 
numbers) and return
-                               }
-                       }
-                       else
-                       {
-                               //var_dump("Not configured properly");
-                               //Give error message (not configured properly) 
and return
-                       }
-
-                       foreach ($invoices as $invoice) // Runs through all 
invoices
-                       {
-                               // We need all price items in the invoice
-                               $price_items = 
rental_soinvoice_price_item::get_instance()->get(0, 0, '', false, '', '', 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(0, 0, '', false, '', '', 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());
-
-                               $price_item_data = array();
-                               foreach ($price_items as $price_item) // Runs 
through all items
-                               {
-                                       $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 (!$price_item->get_is_one_time() && 
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;
-                               }
-                               $this->orders[] = 
$this->get_order($invoice->get_header(), 
$invoice->get_party()->get_identifier(), $invoice->get_id(), 
$this->billing_job->get_year(), $this->billing_job->get_month(), 
$invoice->get_account_out(), $price_item_data, 
$invoice->get_responsibility_id(), $invoice->get_service_id(), 
$building_location_code, $invoice->get_project_id(), $composite_name, 
$serial_number, $invoice->get_reference());
-                               $invoice->set_serial_number($serial_number);
-                               $serial_number++;
-                       }
-
-                       $so_invoice = rental_soinvoice::get_instance();
-
-                       $so_invoice->transaction_begin();
-                       //Store invoices with serial numbers
-                       foreach ($invoices as $invoice) // Runs through all 
invoices
-                       {
-                               $so_invoice->store($invoice);
-                       }
-                       return $so_invoice->transaction_commit();
-               }
-
-               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(0, 0, 
'id', true, '', '', array(
-                               'billing_id' => $this->billing_job->get_id()));
-                       $dateformat = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
-
-                       foreach ($invoices as $invoice) // Runs through all 
invoices
-                       {
-                               // We need all price items in the invoice
-                               $price_items = 
rental_soinvoice_price_item::get_instance()->get(0, 0, '', false, '', '', 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(0, 0, '', false, '', '', 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) // Runs 
through all items
-                               {
-                                       $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(), //ny
-         $invoice->get_responsibility_id(), //ny
-         $contract_type_label, //ny
-         $contract_id //ny
-                                       );
-                                       $price_item_counter++;
-                               }
-                       }
-               }
-
-               /**
-                * Builds one single order of the Agresso file.
-                * 
-                */
-               protected function get_order( $header, $party_id, $order_id, 
$bill_year, $bill_month, $account, $product_items, $responsibility, $service, 
$building, $project, $text, $serial_number, $client_ref )
-               {
-
-                       //$order_id = $order_id + 39500000;
-                       // XXX: Which charsets do Agresso accept/expect? Do we 
need to something regarding padding and UTF-8?
-                       $order = array();
-
-
-                       $order[] = // Header line
-                               '1'  //  1              accept_flag
-                               . sprintf("%8s", '')  //        2               
just white space..
-                               . sprintf("%20s", '') //  3             
accountable
-                               . sprintf("%160s", '') //  4            address
-                               . sprintf("%20s", '') //        5-7             
just white space..
-                               . sprintf("%08s", '') //  8             apar_id
-                               . sprintf("%30s", '') //  9             
apar_name
-                               . sprintf("%50s", '') //        10-11   just 
white space..
-                               . sprintf("%2s", '')  //        12              
att_1_id
-                               . sprintf("%2s", '')  //        13              
att_2_id
-                               . sprintf("%2s", '')  //        14              
att_3_id
-                               . sprintf("%2s", '')  //        15              
att_4_id
-                               . sprintf("%2s", '')  //        16              
att_5_id
-                               . sprintf("%2s", '')  //        17              
att_6_id
-                               . sprintf("%2s", '')  //        18              
att_7_id
-                               . sprintf("%35s", '') //        19              
bank_account
-                               . sprintf("%-12s", "BKBPE{$this->date_str}")  
//        20              batch_id                                DATA
-                               . 'BY'  //      21              client          
                        DATA
-                               . sprintf("%2s", '')  //        22              
client_ref
-                               . sprintf("%-17s", "{$this->date_str}")   //    
23              confirm_date                    DATA
-                               . sprintf("%1s", '')  //        24              
control
-                               . sprintf("%17s", '') //        25              
just white space..
-                               . 'NOK'  //     26              currency        
                        DATA
-                               . sprintf("%60s", '') //        27              
del_met_descr
-                               . sprintf("%60s", '') //        28              
del_term_descr
-                               . sprintf("%255s", '') //       29              
deliv_addr
-                               . sprintf("%50s", '') //        30              
deliv_attention
-                               . sprintf("%3s", '')  //        31              
deliv_countr
-                               . sprintf("%-17s", "{$this->date_str}")   //    
32              deliv_date                              DATA
-                               . sprintf("%8s", '')  //        33              
deliv_method
-                               . sprintf("%8s", '')  //        34              
deliv_terms
-                               . sprintf("%52s", '') //        35-41   just 
white space..
-                               . sprintf("%-12.12s", $account)  //     42      
        dim_value_1                             DATA
-                               . sprintf("%12s", '') //        43              
dim_value_2
-                               . sprintf("%12s", '') //        44              
dim_value_3
-                               . sprintf("%12s", '') //        45              
dim_value_4
-                               . sprintf("%12s", '') //        46              
dim_value_5
-                               . sprintf("%12s", '') //        47              
dim_value_6
-                               . sprintf("%12s", '') //        48              
dim_value_7
-                               . sprintf("%17s", '') //        49-50   just 
white space..
-                               . sprintf("%017s", '') //       51              
exch_rate
-                               . sprintf("%-15.15s", $client_ref) //   52      
        ext_ord_ref
-                               . sprintf("%6s", '')  //        53              
intrule_id
-                               . sprintf("%8s", '')  //        54-55   just 
white space..
-                               . sprintf("%-120.120s", utf8_decode($header)) 
//        56              long_info1                              DATA
-                               . sprintf("%120s", '') //       57              
long_info2
-                               . sprintf("%10s", '') //        58              
just white space..
-                               . sprintf("%08s", '') //        59              
main_apar_id
-                               . sprintf("%50s", '') //        60              
mark_attention
-                               . sprintf("%3s", '')  //        61              
mark_ctry_cd
-                               . sprintf("%120s", '') //       62              
markings
-                               . sprintf("%-17s", '') //       63              
obs_date
-                               . sprintf("%-17s", '') //       64              
order_date
-                               . sprintf("%09.9s", $serial_number) //  65      
        order_id                                DATA
-                               . 'FS'  //      66              order_type      
                        DATA
-                               . 'IP'  //      67              pay_method      
                        DATA
-                               //      (68)
-                               . sprintf("%02s", '')
-                               . sprintf("%04.4s", $bill_year)
-                               . sprintf("%02.2s", $bill_month)  //    69      
        period                                  DATA
-                               . sprintf("%30s", '') //        70              
place
-                               . sprintf("%40s", '') //        71              
province
-                               . sprintf("%12s", '') //        72              
just white space..
-                               . sprintf("%-8s", 'BKBPE')   //         73      
        responsible                             DATA
-                               . sprintf("%-8s", 'BKBPE')   //         74      
        responsible2                    DATA
-                               . sprintf("%8s", '')  //        75              
just white space..
-                               . sprintf("%-08s", '') //       76              
sequence_ref
-                               . sprintf("%80s", '') //        77-78   just 
white space..
-                               . 'N'  //       79              status          
                        DATA
-                               . sprintf("%4s", '')  //        80-82   just 
white space..
-                               . sprintf("%08s", '') //        83              
template_id
-                               . sprintf("%2s", '')  //        84              
terms_id
-                               . sprintf("%12s", '') //        85              
tekx1
-                               . sprintf("%-12s", $party_id)  //       86      
        tekst2                                  DATA
-                               . sprintf("%12s", '') //        87              
tekst3
-                               . sprintf("%12s", '') //        88              
text4
-                               . '42'  //      89              trans_type      
                        DATA
-                               . sprintf("%70s", '') //        90-92   just 
white space..
-                               . sprintf("%09s", '') //        93              
voucher_ref
-                               . 'FU'  //      94              voucher_type    
                DATA
-                               . sprintf("%4s", '')  //        95              
just white space..
-                               . sprintf("%15s", '') //        96              
zip_code
-                       ;
-                       $item_counter = 0;
-                       foreach ($product_items as $item) // All products 
(=price items)
-                       {
-                               $order[] = // Product line
-                                       '0'  // 1               0 for 
påfølgende linjer etter ordrehde
-                                       . sprintf("%8s", '') //  2              
account
-                                       . sprintf("%180s", '')   //     3-4     
        just white space..
-                                       . sprintf("%2s", '') //  5              
allocation_key
-                                       . 
$this->get_formatted_amount($item['amount'])   //  6          amount            
                      DATA
-                                       . '1' //  7             amount_set
-                                       . sprintf("%38s", '')   //      8-9     
        just white space..
-                                       . sprintf("%-35.35s", 
utf8_decode($item['article_description']))  //    10      art_descr             
  DATA
-                                       . sprintf("%-15.15s", 
$item['article_code']) //         11              article                       
          DATA
-                                       . sprintf("%49s", '')   //      12-19   
just white space..
-                                       . sprintf("%-12s", 
"BKBPE{$this->date_str}") //         20              batch_id                   
             DATA
-                                       . 'BY' //       21              client  
                                DATA
-                                       . sprintf("%20s", '')   //      22-24   
just white space..
-                                       . sprintf("%017s", '')   //     25      
        cur_amount
-                                       . sprintf("%464s", '')   //     26-34   
just white space..
-                                       . sprintf("%-8.8s", $responsibility)   
//       35              dim_1                                   DATA
-                                       . sprintf("%-8.8s", $service) //        
36              dim_2                                   DATA
-                                       . sprintf("%8s", '') //         37      
        dim_3
-                                       . sprintf("%8s", '') //         38      
        dim_4
-                                       . sprintf("%-12.12s", $project) //      
39              dim_5                                   DATA
-                                       . sprintf("%4s", '') //         40      
        dim_6
-                                       . sprintf("%4s", '') //         41      
        dim_7
-                                       . sprintf("%84s", '')   //      42-48   
just white space..
-                                       . sprintf("%017s", '')   //     49      
        disc_percent
-                                       //      (50)
-                                       . sprintf("%017s", '')   //     51      
        exch_rate
-                                       . sprintf("%21s", '')   //      52-53   
just white space..
-                                       . sprintf("%04.4s", ++$item_counter)   
//       54              line_no                                 DATA
-                                       . sprintf("%4s", '') //         55      
        location
-                                       . sprintf("%240s", '')   //     56-57   
just white space..
-                                       . sprintf("%10s", '')   //      58      
        lot
-                                       . sprintf("%215s", '')   //     59-64   
just white space..
-                                       . sprintf("%09.9s", $serial_number)   
//        65      order_id                                        DATA
-                                       . sprintf("%4s", '') // 66-67   just 
white space..
-                                       //      (68)
-                                       . sprintf("%02s", '')
-                                       . sprintf("%04.4s", $bill_year)
-                                       . sprintf("%02.2s", $bill_month) //     
69              period                                  DATA
-                                       . sprintf("%70s", '')   //      70-71   
just white space..
-                                       . sprintf("%12s", '')   //      72      
        rel_value
-                                       . sprintf("%16s", '')   //      73-74   
just white space..
-                                       . sprintf("%08s", '')   //      75      
        sequence_no
-                                       . sprintf("%8s", '') // 76              
just white space..
-                                       . sprintf("%20s", '')   //      77      
        serial_no
-                                       . sprintf("%60s", '')   //      78      
        just white space..
-                                       . 'N' //        79              status  
                                DATA
-                                       //      (80)
-                                       . sprintf("%2s", '') //         81      
        tax_code
-                                       . sprintf("%2s", '') //         82      
        tax_system
-                                       . sprintf("%-08s", '')   //     83      
        template_id
-                                       . sprintf("%50s", '')   //      84-88   
just white space..
-                                       . '42' //       89              
trans_type
-                                       . sprintf("%3s", '') //         90      
        unit_code
-                                       . sprintf("%50s", '')   //      91      
        unit_descr
-                                       . sprintf("%017s", 1 * 100)  //         
92              value_1                                 DATA
-                                       . sprintf("%9s", '') // 93              
just white space..
-                                       . 'FU' //       94              
voucher_type                    DATA
-                                       . sprintf("%4s", '') //         95      
        warehouse
-                                       . sprintf("%15s", '')   //      96      
        just white space..
-                               ;
-                               $order[] = // Text line
-                                       '0' . //        1
-                                       sprintf("%345s", '')   //       2-19    
just white space..              DATA
-                                       . sprintf("%-12s", 
"BKBPE{$this->date_str}") //         20              batch_id                   
             DATA
-                                       . 'BY' //       21              client  
                                DATA
-                                       . sprintf("%692s", '')   //     22-53   
just white space..
-                                       . sprintf("%04.4s", $item_counter)   // 
        54              line_no                                 DATA
-                                       . sprintf("%469s", '')   //     55-64   
just white space..
-                                       . sprintf("%09.9s", $serial_number)   
//        65              order_id                                DATA
-                                       . sprintf("%110s", '')   //     66-74   
just white space..
-                                       . sprintf("%08s", 1) //         75      
        sequence_no                             DATA
-                                       . sprintf("%28s", '')   //      76-77   
just white space..
-                                       . sprintf("%-60.60s", 
utf8_decode($text)) //    78              shot_info                             
  DATA
-                                       . sprintf("%63s", '')   //      79-88   
just white space..
-                                       . '42' //       89              
trans_type                              DATA
-                                       . sprintf("%79s", '')   //      90-93   
just white space..
-                                       . 'FU' //       94              
voucher_type                    DATA
-                                       . sprintf("%19s", '')   //      95-96   
just white space..
-                               ;
-                       }
-                       return str_replace(array("\n", "\r"), '', $order);
-               }
-
-               /**
-                * Builds one single order of the excel file.
-                * 
-                */
-               protected function get_order_excel_bk(
-               $start_date, $end_date, $billing_start_date, $billing_end_date, 
$header, $party_id, $party_name, $party_address, $party_full_name, $order_id, 
$bill_year, $bill_month, $account, $product_item, $responsibility, $service, 
$building, $project, $text, $client_ref, $counter, $account_in, 
$responsibility_id, $contract_type_label, $contract_id )
-               {
-
-                       //$order_id = $order_id + 39500000;
-                       // XXX: Which charsets do Agresso accept/expect? Do we 
need to something regarding padding and UTF-8?
-                       //$order = array();
-
-                       $item_counter = $counter;
-                       $order = array(
-                               'contract_id' => $contract_id,
-                               'account' => $account,
-                               'client_ref' => $client_ref,
-                               'header' => utf8_decode($header),
-                               'bill_year' => $bill_year,
-                               'bill_month' => $bill_month,
-                               'Ansvar' => 'BKBPE',
-                               'Ansvar2' => 'BKBPE',
-                               'Party' => $party_id,
-                               'name' => $party_name,
-                               'amount' => 
$this->get_formatted_amount_excel($product_item['amount']),
-//                        'amount' => 
$this->get_formatted_amount($product_items[0]['amount']),
-                               'article description' => 
utf8_decode($product_item['article_description']),
-                               'article_code' => $product_item['article_code'],
-                               'batch_id' => "BKBPE{$this->date_str}",
-                               'client' => 'BY',
-                               'responsibility' => $responsibility,
-                               'service' => $service,
-                               'project' => $project,
-                               'counter' => ++$item_counter,
-                               'bill_year' => $bill_year,
-                               'bill_month' => $bill_month,
-                               'batch_id' => "BKBPE{$this->date_str}",
-                               'client' => 'BY',
-                               'item_counter' => $item_counter,
-                               'text' => utf8_decode($text)
-                       );
-
-                       return str_replace(array("\n", "\r"), '', $order);
-               }
-
-               protected function get_order_excel_nlsh(
-               $start_date, $end_date, $billing_start_date, $billing_end_date, 
$header, $party_id, $party_name, $party_address, $party_full_name, $order_id, 
$bill_year, $bill_month, $account_out, $product_item, $responsibility, 
$service, $building, $project, $text, $client_ref, $counter, $account_in, 
$responsibility_id, $contract_type_label, $contract_id )
-               {
-
-//_debug_array(func_get_args());
-                       $item_counter = $counter;
-                       $order = array
-                               (
-                               'contract_id' => $contract_id,
-                               'date_start' => $start_date,
-                               'date_end' => $end_date,
-                               'billing_start' => $billing_start_date,
-                               'billing_end' => $billing_end_date,
-                               'Kontraktstype' => $contract_type_label, //FIXME
-                               'Art/konto inntektsside' => $account_in,
-                               'Art/konto utgiftsside' => $account_out, //FIXME
-                               'client_ref' => $client_ref,
-                               'header' => $header,
-                               'bill_year' => $bill_year,
-                               'bill_month' => $bill_month,
-                               'Ansvar' => $responsibility_id, //FIXME
-//                             'Ansvar2'                                => 
'BKBPE',//FIXME
-                               'Party' => $party_id,
-                               'name' => $party_name,
-                               'address' => $party_address,
-                               'Leieboer' => $party_full_name,
-                               'amount' => 
$this->get_formatted_amount_excel($product_item['amount']),
-                               'article description' => 
$product_item['article_description'],
-                               'article_code' => $product_item['article_code'],
-                               'batch_id' => "BKBPE{$this->date_str}",
-                               'client' => 'BY',
-                               'responsibility' => $responsibility,
-                               'service' => $service,
-                               'project' => $project,
-                               'counter' => ++$item_counter,
-                               'bill_year' => $bill_year,
-                               'bill_month' => $bill_month,
-                               'batch_id' => "BKBPE{$this->date_str}",
-                               'client' => 'BY',
-                               'item_counter' => $item_counter,
-                               'text' => $text,
-                               'Kommentar' => 'Kommentar', //FIXME
-                       );
-
-                       return str_replace(array("\n", "\r"), '', $order);
-               }
-
-               protected function get_formatted_amount( $amount )
-               {
-                       $amount = round($amount, 2) * 100;
-                       if ($amount < 0) // Negative number
-                       {
-                               return '-' . sprintf("%016.16s", abs($amount)); 
// We have to have the sign at the start of the string
-                       }
-                       return sprintf("%017.17s", $amount);
-               }
-
-               protected function get_formatted_amount_excel( $amount )
-               {
-//            var_dump($amount);
-//            var_dump($belop);
-                       $amount = round($amount, 2) * 100;
-                       $belop = substr($amount, 0, strlen($amount) - 2) . '.' 
. substr($amount, -2);
-                       if ($amount < 0) // Negative number
-                       {
-                               return '-' . sprintf("%016.16s", abs($belop)); 
// We have to have the sign at the start of the string
-                       }
-                       return sprintf("%017.17s", $belop);
-               }
+<?php
+       phpgw::import_class('rental.socomposite');
+       phpgw::import_class('rental.socontract');
+       include_class('rental', 'exportable', 'inc/model/');
+
+       class rental_agresso_lg04 implements rental_exportable
+       {
+
+               protected $billing_job;
+               protected $date_str;
+               protected $orders;
+
+               public function __construct( $billing_job )
+               {
+                       $this->billing_job = $billing_job;
+                       $this->date_str = date('ymd', 
$billing_job->get_timestamp_stop());
+                       $this->orders = null;
+               }
+
+               /**
+                * @see rental_exportable
+                */
+               public function get_id()
+               {
+                       return 'Agresso LG04';
+               }
+
+               /**
+                * Returns the file contents as a string.
+                * 
+                * @see rental_exportable
+                */
+               public function get_contents()
+               {
+                       $contents = '';
+                       if ($this->orders == null) // Data hasn't been created 
yet
+                       {
+                               $this->run();
+                       }
+                       foreach ($this->orders as $order)
+                       {
+                               foreach ($order as $line)
+                               {
+                                       $contents .= "{$line}\n";
+                               }
+                       }
+                       return $contents;
+               }
+
+               public function get_contents_excel( $excel_export_type )
+               {
+                       if ($this->orders == null) // Data hasn't been created 
yet
+                       {
+                               $this->run_excel_export($excel_export_type);
+                       }
+                       return $this->orders;
+               }
+
+               public function get_missing_billing_info( $contract )
+               {
+                       $missing_billing_info = array();
+                       $contract_parties = $contract->get_parties();
+                       if ($contract_parties == null || 
count($contract_parties) < 1)
+                       {
+                               $missing_billing_info[] = 'Missing contract 
party.';
+                       }
+
+                       $payer_id = $contract->get_payer_id();
+                       if ($payer_id == null || $payer_id = 0)
+                       {
+                               $missing_billing_info[] = 'Missing payer id.';
+                       }
+
+                       if(!$contract->get_billing_start_date())
+                       {
+                               $missing_billing_info[] = 'Missing start_date.';
+                       }
+                       return $missing_billing_info;
+               }
+
+               /**
+                * Does all the dirty work by building all the lines of Agresso 
contents
+                * from the billing job.
+                */
+               protected function run()
+               {
+                       $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(0, 0, 
'id', true, '', '', array(
+                               'billing_id' => $this->billing_job->get_id()));
+
+
+                       $config = CreateObject('phpgwapi.config', 'rental');
+                       $config->read();
+                       $serial_config_start = 
$config->config_data['serial_start'];
+                       $serial_config_stop = 
$config->config_data['serial_stop'];
+
+
+                       if (isset($serial_config_start) && 
is_numeric($serial_config_start) &&
+                               isset($serial_config_stop) && 
is_numeric($serial_config_stop))
+                       {
+                               $max_serial_number_used = 
rental_soinvoice::get_instance()->get_max_serial_number_used($serial_config_start,
 $serial_config_stop);
+
+                               if (isset($max_serial_number_used) && 
is_numeric($max_serial_number_used) && $max_serial_number_used > 0)
+                               {
+                                       $serial_number = 
$max_serial_number_used + 1;
+                               }
+                               else
+                               {
+                                       $serial_number = $serial_config_start;
+                               }
+
+                               $number_left_in_sequence = $serial_config_stop 
- $serial_number;
+
+                               if ($number_left_in_sequence < count($invoices))
+                               {
+                                       //var_dump("Out of sequence numbers");
+                                       //Give error message (out of sequence 
numbers) and return
+                               }
+                       }
+                       else
+                       {
+                               //var_dump("Not configured properly");
+                               //Give error message (not configured properly) 
and return
+                       }
+
+                       foreach ($invoices as $invoice) // Runs through all 
invoices
+                       {
+                               // We need all price items in the invoice
+                               $price_items = 
rental_soinvoice_price_item::get_instance()->get(0, 0, '', false, '', '', 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(0, 0, '', false, '', '', 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());
+
+                               $price_item_data = array();
+                               foreach ($price_items as $price_item) // Runs 
through all items
+                               {
+                                       $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 (!$price_item->get_is_one_time() && 
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;
+                               }
+                               $this->orders[] = 
$this->get_order($invoice->get_header(), 
$invoice->get_party()->get_identifier(), $invoice->get_id(), 
$this->billing_job->get_year(), $this->billing_job->get_month(), 
$invoice->get_account_out(), $price_item_data, 
$invoice->get_responsibility_id(), $invoice->get_service_id(), 
$building_location_code, $invoice->get_project_id(), $composite_name, 
$serial_number, $invoice->get_reference());
+                               $invoice->set_serial_number($serial_number);
+                               $serial_number++;
+                       }
+
+                       $so_invoice = rental_soinvoice::get_instance();
+
+                       $so_invoice->transaction_begin();
+                       //Store invoices with serial numbers
+                       foreach ($invoices as $invoice) // Runs through all 
invoices
+                       {
+                               $so_invoice->store($invoice);
+                       }
+                       return $so_invoice->transaction_commit();
+               }
+
+               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(0, 0, 
'id', true, '', '', array(
+                               'billing_id' => $this->billing_job->get_id()));
+                       $dateformat = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
+
+                       foreach ($invoices as $invoice) // Runs through all 
invoices
+                       {
+                               // We need all price items in the invoice
+                               $price_items = 
rental_soinvoice_price_item::get_instance()->get(0, 0, '', false, '', '', 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(0, 0, '', false, '', '', 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) // Runs 
through all items
+                               {
+                                       $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(), //ny
+         $invoice->get_responsibility_id(), //ny
+         $contract_type_label, //ny
+         $contract_id //ny
+                                       );
+                                       $price_item_counter++;
+                               }
+                       }
+               }
+
+               /**
+                * Builds one single order of the Agresso file.
+                * 
+                */
+               protected function get_order( $header, $party_id, $order_id, 
$bill_year, $bill_month, $account, $product_items, $responsibility, $service, 
$building, $project, $text, $serial_number, $client_ref )
+               {
+
+                       //$order_id = $order_id + 39500000;
+                       // XXX: Which charsets do Agresso accept/expect? Do we 
need to something regarding padding and UTF-8?
+                       $order = array();
+
+
+                       $order[] = // Header line
+                               '1'  //  1              accept_flag
+                               . sprintf("%8s", '')  //        2               
just white space..
+                               . sprintf("%20s", '') //  3             
accountable
+                               . sprintf("%160s", '') //  4            address
+                               . sprintf("%20s", '') //        5-7             
just white space..
+                               . sprintf("%08s", '') //  8             apar_id
+                               . sprintf("%30s", '') //  9             
apar_name
+                               . sprintf("%50s", '') //        10-11   just 
white space..
+                               . sprintf("%2s", '')  //        12              
att_1_id
+                               . sprintf("%2s", '')  //        13              
att_2_id
+                               . sprintf("%2s", '')  //        14              
att_3_id
+                               . sprintf("%2s", '')  //        15              
att_4_id
+                               . sprintf("%2s", '')  //        16              
att_5_id
+                               . sprintf("%2s", '')  //        17              
att_6_id
+                               . sprintf("%2s", '')  //        18              
att_7_id
+                               . sprintf("%35s", '') //        19              
bank_account
+                               . sprintf("%-12s", "BKBPE{$this->date_str}")  
//        20              batch_id                                DATA
+                               . 'BY'  //      21              client          
                        DATA
+                               . sprintf("%2s", '')  //        22              
client_ref
+                               . sprintf("%-17s", "{$this->date_str}")   //    
23              confirm_date                    DATA
+                               . sprintf("%1s", '')  //        24              
control
+                               . sprintf("%17s", '') //        25              
just white space..
+                               . 'NOK'  //     26              currency        
                        DATA
+                               . sprintf("%60s", '') //        27              
del_met_descr
+                               . sprintf("%60s", '') //        28              
del_term_descr
+                               . sprintf("%255s", '') //       29              
deliv_addr
+                               . sprintf("%50s", '') //        30              
deliv_attention
+                               . sprintf("%3s", '')  //        31              
deliv_countr
+                               . sprintf("%-17s", "{$this->date_str}")   //    
32              deliv_date                              DATA
+                               . sprintf("%8s", '')  //        33              
deliv_method
+                               . sprintf("%8s", '')  //        34              
deliv_terms
+                               . sprintf("%52s", '') //        35-41   just 
white space..
+                               . sprintf("%-12.12s", $account)  //     42      
        dim_value_1                             DATA
+                               . sprintf("%12s", '') //        43              
dim_value_2
+                               . sprintf("%12s", '') //        44              
dim_value_3
+                               . sprintf("%12s", '') //        45              
dim_value_4
+                               . sprintf("%12s", '') //        46              
dim_value_5
+                               . sprintf("%12s", '') //        47              
dim_value_6
+                               . sprintf("%12s", '') //        48              
dim_value_7
+                               . sprintf("%17s", '') //        49-50   just 
white space..
+                               . sprintf("%017s", '') //       51              
exch_rate
+                               . sprintf("%-15.15s", $client_ref) //   52      
        ext_ord_ref
+                               . sprintf("%6s", '')  //        53              
intrule_id
+                               . sprintf("%8s", '')  //        54-55   just 
white space..
+                               . sprintf("%-120.120s", utf8_decode($header)) 
//        56              long_info1                              DATA
+                               . sprintf("%120s", '') //       57              
long_info2
+                               . sprintf("%10s", '') //        58              
just white space..
+                               . sprintf("%08s", '') //        59              
main_apar_id
+                               . sprintf("%50s", '') //        60              
mark_attention
+                               . sprintf("%3s", '')  //        61              
mark_ctry_cd
+                               . sprintf("%120s", '') //       62              
markings
+                               . sprintf("%-17s", '') //       63              
obs_date
+                               . sprintf("%-17s", '') //       64              
order_date
+                               . sprintf("%09.9s", $serial_number) //  65      
        order_id                                DATA
+                               . 'FS'  //      66              order_type      
                        DATA
+                               . 'IP'  //      67              pay_method      
                        DATA
+                               //      (68)
+                               . sprintf("%02s", '')
+                               . sprintf("%04.4s", $bill_year)
+                               . sprintf("%02.2s", $bill_month)  //    69      
        period                                  DATA
+                               . sprintf("%30s", '') //        70              
place
+                               . sprintf("%40s", '') //        71              
province
+                               . sprintf("%12s", '') //        72              
just white space..
+                               . sprintf("%-8s", 'BKBPE')   //         73      
        responsible                             DATA
+                               . sprintf("%-8s", 'BKBPE')   //         74      
        responsible2                    DATA
+                               . sprintf("%8s", '')  //        75              
just white space..
+                               . sprintf("%-08s", '') //       76              
sequence_ref
+                               . sprintf("%80s", '') //        77-78   just 
white space..
+                               . 'N'  //       79              status          
                        DATA
+                               . sprintf("%4s", '')  //        80-82   just 
white space..
+                               . sprintf("%08s", '') //        83              
template_id
+                               . sprintf("%2s", '')  //        84              
terms_id
+                               . sprintf("%12s", '') //        85              
tekx1
+                               . sprintf("%-12s", $party_id)  //       86      
        tekst2                                  DATA
+                               . sprintf("%12s", '') //        87              
tekst3
+                               . sprintf("%12s", '') //        88              
text4
+                               . '42'  //      89              trans_type      
                        DATA
+                               . sprintf("%70s", '') //        90-92   just 
white space..
+                               . sprintf("%09s", '') //        93              
voucher_ref
+                               . 'FU'  //      94              voucher_type    
                DATA
+                               . sprintf("%4s", '')  //        95              
just white space..
+                               . sprintf("%15s", '') //        96              
zip_code
+                       ;
+                       $item_counter = 0;
+                       foreach ($product_items as $item) // All products 
(=price items)
+                       {
+                               $order[] = // Product line
+                                       '0'  // 1               0 for 
påfølgende linjer etter ordrehde
+                                       . sprintf("%8s", '') //  2              
account
+                                       . sprintf("%180s", '')   //     3-4     
        just white space..
+                                       . sprintf("%2s", '') //  5              
allocation_key
+                                       . 
$this->get_formatted_amount($item['amount'])   //  6          amount            
                      DATA
+                                       . '1' //  7             amount_set
+                                       . sprintf("%38s", '')   //      8-9     
        just white space..
+                                       . sprintf("%-35.35s", 
utf8_decode($item['article_description']))  //    10      art_descr             
  DATA
+                                       . sprintf("%-15.15s", 
$item['article_code']) //         11              article                       
          DATA
+                                       . sprintf("%49s", '')   //      12-19   
just white space..
+                                       . sprintf("%-12s", 
"BKBPE{$this->date_str}") //         20              batch_id                   
             DATA
+                                       . 'BY' //       21              client  
                                DATA
+                                       . sprintf("%20s", '')   //      22-24   
just white space..
+                                       . sprintf("%017s", '')   //     25      
        cur_amount
+                                       . sprintf("%464s", '')   //     26-34   
just white space..
+                                       . sprintf("%-8.8s", $responsibility)   
//       35              dim_1                                   DATA
+                                       . sprintf("%-8.8s", $service) //        
36              dim_2                                   DATA
+                                       . sprintf("%8s", '') //         37      
        dim_3
+                                       . sprintf("%8s", '') //         38      
        dim_4
+                                       . sprintf("%-12.12s", $project) //      
39              dim_5                                   DATA
+                                       . sprintf("%4s", '') //         40      
        dim_6
+                                       . sprintf("%4s", '') //         41      
        dim_7
+                                       . sprintf("%84s", '')   //      42-48   
just white space..
+                                       . sprintf("%017s", '')   //     49      
        disc_percent
+                                       //      (50)
+                                       . sprintf("%017s", '')   //     51      
        exch_rate
+                                       . sprintf("%21s", '')   //      52-53   
just white space..
+                                       . sprintf("%04.4s", ++$item_counter)   
//       54              line_no                                 DATA
+                                       . sprintf("%4s", '') //         55      
        location
+                                       . sprintf("%240s", '')   //     56-57   
just white space..
+                                       . sprintf("%10s", '')   //      58      
        lot
+                                       . sprintf("%215s", '')   //     59-64   
just white space..
+                                       . sprintf("%09.9s", $serial_number)   
//        65      order_id                                        DATA
+                                       . sprintf("%4s", '') // 66-67   just 
white space..
+                                       //      (68)
+                                       . sprintf("%02s", '')
+                                       . sprintf("%04.4s", $bill_year)
+                                       . sprintf("%02.2s", $bill_month) //     
69              period                                  DATA
+                                       . sprintf("%70s", '')   //      70-71   
just white space..
+                                       . sprintf("%12s", '')   //      72      
        rel_value
+                                       . sprintf("%16s", '')   //      73-74   
just white space..
+                                       . sprintf("%08s", '')   //      75      
        sequence_no
+                                       . sprintf("%8s", '') // 76              
just white space..
+                                       . sprintf("%20s", '')   //      77      
        serial_no
+                                       . sprintf("%60s", '')   //      78      
        just white space..
+                                       . 'N' //        79              status  
                                DATA
+                                       //      (80)
+                                       . sprintf("%2s", '') //         81      
        tax_code
+                                       . sprintf("%2s", '') //         82      
        tax_system
+                                       . sprintf("%-08s", '')   //     83      
        template_id
+                                       . sprintf("%50s", '')   //      84-88   
just white space..
+                                       . '42' //       89              
trans_type
+                                       . sprintf("%3s", '') //         90      
        unit_code
+                                       . sprintf("%50s", '')   //      91      
        unit_descr
+                                       . sprintf("%017s", 1 * 100)  //         
92              value_1                                 DATA
+                                       . sprintf("%9s", '') // 93              
just white space..
+                                       . 'FU' //       94              
voucher_type                    DATA
+                                       . sprintf("%4s", '') //         95      
        warehouse
+                                       . sprintf("%15s", '')   //      96      
        just white space..
+                               ;
+                               $order[] = // Text line
+                                       '0' . //        1
+                                       sprintf("%345s", '')   //       2-19    
just white space..              DATA
+                                       . sprintf("%-12s", 
"BKBPE{$this->date_str}") //         20              batch_id                   
             DATA
+                                       . 'BY' //       21              client  
                                DATA
+                                       . sprintf("%692s", '')   //     22-53   
just white space..
+                                       . sprintf("%04.4s", $item_counter)   // 
        54              line_no                                 DATA
+                                       . sprintf("%469s", '')   //     55-64   
just white space..
+                                       . sprintf("%09.9s", $serial_number)   
//        65              order_id                                DATA
+                                       . sprintf("%110s", '')   //     66-74   
just white space..
+                                       . sprintf("%08s", 1) //         75      
        sequence_no                             DATA
+                                       . sprintf("%28s", '')   //      76-77   
just white space..
+                                       . sprintf("%-60.60s", 
utf8_decode($text)) //    78              shot_info                             
  DATA
+                                       . sprintf("%63s", '')   //      79-88   
just white space..
+                                       . '42' //       89              
trans_type                              DATA
+                                       . sprintf("%79s", '')   //      90-93   
just white space..
+                                       . 'FU' //       94              
voucher_type                    DATA
+                                       . sprintf("%19s", '')   //      95-96   
just white space..
+                               ;
+                       }
+                       return str_replace(array("\n", "\r"), '', $order);
+               }
+
+               /**
+                * Builds one single order of the excel file.
+                * 
+                */
+               protected function get_order_excel_bk(
+               $start_date, $end_date, $billing_start_date, $billing_end_date, 
$header, $party_id, $party_name, $party_address, $party_full_name, $order_id, 
$bill_year, $bill_month, $account, $product_item, $responsibility, $service, 
$building, $project, $text, $client_ref, $counter, $account_in, 
$responsibility_id, $contract_type_label, $contract_id )
+               {
+
+                       //$order_id = $order_id + 39500000;
+                       // XXX: Which charsets do Agresso accept/expect? Do we 
need to something regarding padding and UTF-8?
+                       //$order = array();
+
+                       $item_counter = $counter;
+                       $order = array(
+                               'contract_id' => $contract_id,
+                               'account' => $account,
+                               'client_ref' => $client_ref,
+                               'header' => utf8_decode($header),
+                               'bill_year' => $bill_year,
+                               'bill_month' => $bill_month,
+                               'Ansvar' => 'BKBPE',
+                               'Ansvar2' => 'BKBPE',
+                               'Party' => $party_id,
+                               'name' => $party_name,
+                               'amount' => 
$this->get_formatted_amount_excel($product_item['amount']),
+//                        'amount' => 
$this->get_formatted_amount($product_items[0]['amount']),
+                               'article description' => 
utf8_decode($product_item['article_description']),
+                               'article_code' => $product_item['article_code'],
+                               'batch_id' => "BKBPE{$this->date_str}",
+                               'client' => 'BY',
+                               'responsibility' => $responsibility,
+                               'service' => $service,
+                               'project' => $project,
+                               'counter' => ++$item_counter,
+                               'bill_year' => $bill_year,
+                               'bill_month' => $bill_month,
+                               'batch_id' => "BKBPE{$this->date_str}",
+                               'client' => 'BY',
+                               'item_counter' => $item_counter,
+                               'text' => utf8_decode($text)
+                       );
+
+                       return str_replace(array("\n", "\r"), '', $order);
+               }
+
+               protected function get_order_excel_nlsh(
+               $start_date, $end_date, $billing_start_date, $billing_end_date, 
$header, $party_id, $party_name, $party_address, $party_full_name, $order_id, 
$bill_year, $bill_month, $account_out, $product_item, $responsibility, 
$service, $building, $project, $text, $client_ref, $counter, $account_in, 
$responsibility_id, $contract_type_label, $contract_id )
+               {
+
+//_debug_array(func_get_args());
+                       $item_counter = $counter;
+                       $order = array
+                               (
+                               'contract_id' => $contract_id,
+                               'date_start' => $start_date,
+                               'date_end' => $end_date,
+                               'billing_start' => $billing_start_date,
+                               'billing_end' => $billing_end_date,
+                               'Kontraktstype' => $contract_type_label, //FIXME
+                               'Art/konto inntektsside' => $account_in,
+                               'Art/konto utgiftsside' => $account_out, //FIXME
+                               'client_ref' => $client_ref,
+                               'header' => $header,
+                               'bill_year' => $bill_year,
+                               'bill_month' => $bill_month,
+                               'Ansvar' => $responsibility_id, //FIXME
+//                             'Ansvar2'                                => 
'BKBPE',//FIXME
+                               'Party' => $party_id,
+                               'name' => $party_name,
+                               'address' => $party_address,
+                               'Leieboer' => $party_full_name,
+                               'amount' => 
$this->get_formatted_amount_excel($product_item['amount']),
+                               'article description' => 
$product_item['article_description'],
+                               'article_code' => $product_item['article_code'],
+                               'batch_id' => "BKBPE{$this->date_str}",
+                               'client' => 'BY',
+                               'responsibility' => $responsibility,
+                               'service' => $service,
+                               'project' => $project,
+                               'counter' => ++$item_counter,
+                               'bill_year' => $bill_year,
+                               'bill_month' => $bill_month,
+                               'batch_id' => "BKBPE{$this->date_str}",
+                               'client' => 'BY',
+                               'item_counter' => $item_counter,
+                               'text' => $text,
+                               'Kommentar' => 'Kommentar', //FIXME
+                       );
+
+                       return str_replace(array("\n", "\r"), '', $order);
+               }
+
+               protected function get_formatted_amount( $amount )
+               {
+                       $amount = round($amount, 2) * 100;
+                       if ($amount < 0) // Negative number
+                       {
+                               return '-' . sprintf("%016.16s", abs($amount)); 
// We have to have the sign at the start of the string
+                       }
+                       return sprintf("%017.17s", $amount);
+               }
+
+               protected function get_formatted_amount_excel( $amount )
+               {
+//            var_dump($amount);
+//            var_dump($belop);
+                       $amount = round($amount, 2) * 100;
+                       $belop = substr($amount, 0, strlen($amount) - 2) . '.' 
. substr($amount, -2);
+                       if ($amount < 0) // Negative number
+                       {
+                               return '-' . sprintf("%016.16s", abs($belop)); 
// We have to have the sign at the start of the string
+                       }
+                       return sprintf("%017.17s", $belop);
+               }
        }
\ No newline at end of file

Modified: trunk/rental/inc/model/class.invoice.inc.php
===================================================================
--- trunk/rental/inc/model/class.invoice.inc.php        2016-05-26 01:41:27 UTC 
(rev 15205)
+++ trunk/rental/inc/model/class.invoice.inc.php        2016-05-26 06:48:36 UTC 
(rev 15206)
@@ -1,588 +1,588 @@
-<?php
-       phpgw::import_class('rental.socontract_price_item');
-       phpgw::import_class('rental.soinvoice_price_item');
-       include_class('rental', 'model', 'inc/model/');
-       include_class('rental', 'contract', 'inc/model/');
-       include_class('rental', 'invoice_price_item', 'inc/model/');
-
-       class rental_invoice extends rental_model
-       {
-
-               protected $id;
-               protected $billing_id; // The billing job that created this 
invoice
-               protected $contract_id; // Contract that this invoice belongs to
-               protected $party_id; // Party that is the recepient of this 
invoice
-               protected $party;
-               protected $timestamp_created; // Billing date
-               protected $timestamp_start; // Start date of invoice
-               protected $timestamp_end; // End date of invoice
-               protected $invoice_price_items;
-               protected $total_sum;
-               protected $total_area;
-               protected $header;
-               protected $account_in; // 'Art' for the income side
-               protected $account_out; // 'Art' for the outlay side
-               protected $composite_names; // From composite - not part of 
invoice db data
-               protected $project_id;
-               protected $service_id;
-               protected $responsibility_id;
-               protected $old_contract_id;
-               protected $term_id;
-               protected $term_label;
-               protected $billing_title;
-               protected $serial_number;
-               protected $reference;
-               public static $so;
-
-               public function __construct( int $id, int $billing_id, int 
$contract_id, int $timestamp_created, int $timestamp_start, int $timestamp_end, 
float $total_sum, float $total_area, string $header, string $account_in, string 
$account_out, string $service_id, string $responsibility_id )
-               {
-                       $this->id = (int)$id;
-                       $this->billing_id = (int)$billing_id;
-                       $this->contract_id = (int)$contract_id;
-                       $this->timestamp_created = (int)$timestamp_created;
-                       $this->timestamp_start = (int)$timestamp_start;
-                       $this->timestamp_end = (int)$timestamp_end;
-                       $this->total_sum = (float)$total_sum;
-                       $this->total_area = (float)$total_area;
-                       $this->invoice_price_items = null;
-                       $this->header = $header;
-                       $this->account_in = $account_in;
-                       $this->account_out = $account_out;
-                       $this->service_id = $service_id;
-                       $this->responsibility_id = $responsibility_id;
-                       $this->composite_names = array();
-               }
-
-               public function set_id( $id )
-               {
-                       $this->id = $id;
-               }
-
-               public function get_id()
-               {
-                       return $this->id;
-               }
-
-               public function set_billing_id( $billing_id )
-               {
-                       $this->billing_id = $billing_id;
-               }
-
-               public function get_billing_id()
-               {
-                       return $this->billing_id;
-               }
-
-               public function set_contract_id( $contract_id )
-               {
-                       $this->contract_id = $contract_id;
-               }
-
-               public function get_contract_id()
-               {
-                       return $this->contract_id;
-               }
-
-               public function set_timestamp_created( $timestamp_created )
-               {
-                       $this->timestamp_created = $timestamp_created;
-               }
-
-               public function get_timestamp_created()
-               {
-                       return $this->timestamp_created;
-               }
-
-               public function set_party_id( $party_id )
-               {
-                       $this->party_id = $party_id;
-               }
-
-               public function get_party_id()
-               {
-                       return $this->party_id;
-               }
-
-               public function set_party( rental_party $party )
-               {
-                       $this->party = $party;
-               }
-
-               public function get_party()
-               {
-                       return $this->party;
-               }
-
-               public function set_timestamp_start( $timestamp_start )
-               {
-                       $this->timestamp_start = $timestamp_start;
-               }
-
-               public function get_timestamp_start()
-               {
-                       return $this->timestamp_start;
-               }
-
-               public function set_timestamp_end( $timestamp_end )
-               {
-                       $this->timestamp_end = $timestamp_end;
-               }
-
-               public function get_timestamp_end()
-               {
-                       return $this->timestamp_end;
-               }
-
-               /**
-                * Adds a invoice price item to the invoice.
-                * NOTE: The price item must store itself. The invoice object 
does
-                * nothing with its items while storing itself.
-                * 
-                * @param $invoice_price_item rental_invoice_price_item to add.
-                */
-               public function add_invoice_price_item( 
rental_invoice_price_item &$invoice_price_item )
-               {
-                       if ($invoice_price_items == null)
-                       {
-                               $invoice_price_items = array();
-                       }
-                       $invoice_price_items[] = $invoice_price_item;
-               }
-
-               public function set_total_sum( float $total_sum )
-               {
-                       $this->total_sum = (float)$total_sum;
-               }
-
-               public function get_total_sum()
-               {
-                       return $this->total_sum;
-               }
-
-               public function set_total_area( float $total_area )
-               {
-                       $this->$total_area = (float)$total_area;
-               }
-
-               public function get_total_area()
-               {
-                       return $this->total_area;
-               }
-
-               public function add_composite_name( string $name )
-               {
-                       if (!in_array($name, $this->composite_names))
-                       {
-                               $this->composite_names[] = $name;
-                       }
-               }
-
-               public function set_header( $header )
-               {
-                       $this->header = $header;
-               }
-
-               public function get_header()
-               {
-                       return $this->header;
-               }
-
-               public function set_account_in( $account_in )
-               {
-                       $this->account_in = $account_in;
-               }
-
-               public function get_account_in()
-               {
-                       return $this->account_in;
-               }
-
-               public function set_account_out( $account_out )
-               {
-                       $this->account_out = $account_out;
-               }
-
-               public function get_account_out()
-               {
-                       return $this->account_out;
-               }
-
-               public function set_service_id( $service_id )
-               {
-                       $this->service_id = $service_id;
-               }
-
-               public function get_service_id()
-               {
-                       return $this->service_id;
-               }
-
-               public function set_responsibility_id( $responsibility_id )
-               {
-                       $this->responsibility_id = $responsibility_id;
-               }
-
-               public function get_responsibility_id()
-               {
-                       return $this->responsibility_id;
-               }
-
-               public function set_project_id( $project_id )
-               {
-                       $this->project_id = $project_id;
-               }
-
-               public function get_project_id()
-               {
-                       return $this->project_id;
-               }
-
-               public function set_old_contract_id( $old_contract_id )
-               {
-                       $this->old_contract_id = $old_contract_id;
-               }
-
-               public function get_old_contract_id()
-               {
-                       return $this->old_contract_id;
-               }
-
-               public function get_composite_names()
-               {
-                       $names = '';
-                       foreach ($this->composite_names as $name)
-                       {
-                               $names .= "{$name}<br/>";
-                       }
-                       return $names;
-               }
-
-               public function set_term_id( $term_id )
-               {
-                       $this->term_id = $term_id;
-               }
-
-               public function get_term_id()
-               {
-                       return $this->term_id;
-               }
-
-               public function set_serial_number( $serial_number )
-               {
-                       $this->serial_number = $serial_number;
-               }
-
-               public function get_serial_number()
-               {
-                       return $this->serial_number;
-               }
-
-               public function set_term_label( $term_label )
-               {
-                       $this->term_label = $term_label;
-               }
-
-               public function get_term_label()
-               {
-                       return $this->term_label;
-               }
-
-               public function set_month( $month )
-               {
-                       $this->month = $month;
-               }
-
-               public function get_month()
-               {
-                       return $this->month;
-               }
-
-               public function set_billing_title( $billing_title )
-               {
-                       $this->billing_title = $billing_title;
-               }
-
-               public function get_reference()
-               {
-                       return $this->reference;
-               }
-
-               public function set_reference( $reference )
-               {
-                       $this->reference = $reference;
-               }
-
-               public function get_billing_title()
-               {
-                       return $this->billing_title;
-               }
-
-               /**
-                * 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, // no identifier
-                               $billing_id, // the billing identifier
-                               $contract_id, // the contract identifier
-                               time(), // the creation time
-                               $timestamp_invoice_start, // the invoice start 
date
-                               $timestamp_invoice_end, // the invoice end date
-                               0, // the total sum of invoice (not calculated 
yet)
-                               $contract->get_rented_area(), // the area 
rented on the contract
-                               $contract->get_invoice_header(), // the invoice 
header
-                               $contract->get_account_in(), // the ingoing 
account number
-                               $account_out, // the outgoing account number
-                               $contract->get_service_id(), // the service 
identifier (internal)
-                               $contract->get_responsibility_id() // the 
responsibility identifier (internal)
-                       );
-
-                       // ... 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)
-                       {
-                               if($billing_term == 5)//credits
-                               {
-                                       $filters2 = array(
-                                               'contract_id' => 
$contract->get_id(),
-                                               'contract_ids_one_time' => 
true, 'credits' => true
-                                       );
-                               }
-                               else
-                               {
-                                       $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(0, 0, '', false, '', '', 
$filters2);
-                               //$contract_price_items = 
rental_socontract_price_item::get_instance()->get(0, 0, '', false, '', '', 
array('contract_id' => $contract->get_id(), 'one_time' => true));
-                       }
-                       else
-                       {
-                               $contract_price_items = 
rental_socontract_price_item::get_instance()->get(0, 0, '', false, '', '', 
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 == '') // Date not set
-                               {
-                                       // 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 == '') // Date not set
-                               {
-                                       // 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) // Start of price item before invoice start
-                               {
-                                       $invoice_price_item_start = 
$timestamp_invoice_start; // We use the invoice start
-                               }
-                               else if ($contract_price_item_start > 
$timestamp_invoice_end && $billing_term != 5) // Start of price item after this 
invoice ends
-                               {
-                                       continue; // We don't add this price 
item - continue to next
-                               }
-                               else // Price item start date is somewhere 
between start and end
-                               {
-                                       $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 && $billing_term != 5) // End of price item before 
this invoice starts
-                               {
-                                       continue; // We don't add this price 
item - continue to next
-                               }
-                               else if ($contract_price_item_end < 
$timestamp_invoice_end) // End of price item before invoice end
-                               {
-                                       $invoice_price_item_end = 
$contract_price_item_end; // We use the price item end
-                               }
-                               else // Price item end date is somewhere after 
invoice end
-                               {
-                                       $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) // The start of the contract is after the billing 
period (should never happen)
-                                       {
-                                               continue; //No price items for 
this contract will be billed
-                                       }
-
-                                       if ($contract_start > 
$invoice_price_item_start) // The contract start is after the start of the 
price item
-                                       {
-                                               $invoice_price_item_start = 
$contract_start;
-                                       }
-                               }
-
-                               if (isset($contract_end) && 
!$contract_price_item->is_one_time())
-                               {
-                                       if ($contract_end < 
$timestamp_invoice_start) // The end of the contract is before the billing 
period (should never happen)
-                                       {
-                                               continue; //No price items for 
this contract will be billed
-                                       }
-
-                                       if ($contract_end < 
$invoice_price_item_end) // The contract start is after the start of the price 
item
-                                       {
-                                               $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, // the number of decimals to 
use for the total price of the price item
-                                        -1, // no price item identifier
-                                        $invoice->get_id(), // the invoice 
identifier
-                                        $contract_price_item->get_title(), // 
the contract price item title
-                                        
$contract_price_item->get_agresso_id(), // the contract price item agresso 
identifier
-                                        $contract_price_item->is_area(), // 
flag for specifying if the contract is of area/piece
-                                        $contract_price_item->get_price(), // 
the price of the contract price item
-                                        $contract_price_item->get_area(), // 
the rented area on this contract (derived from contract)
-                                        $contract_price_item->get_count(), // 
the number of items on this price item
-                                        $invoice_price_item_start, // the 
start date from which this price item should be calculated
-                                        $invoice_price_item_end   // the end 
date to which this price item should be calculated
-                               );
-
-                               // 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())
-                               {
-                                       
$invoice_price_item->set_is_one_time(true);
-                                       if ($billing_term == 5 || 
($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);
-
-                               //FIXME
-                               if($billing_term == 5)
-                               {
-                                       
$invoice->set_header($contract_price_item->get_title());
-                               }
-
-                               // 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;
-               }
-
-               public function serialize()
-               {
-                       $party_name = '';
-                       if ($this->get_party() != null)
-                       {
-                               $serialized_party = 
$this->get_party()->serialize();
-                               $party_name = $serialized_party['name'];
-                       }
-                       $date_format = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
-                       return array(
-                               'id' => $this->get_id(),
-                               'contract_id' => $this->get_contract_id(),
-                               'term_label' => $this->get_term_label(),
-                               'timestamp_created' => date($date_format, 
$this->get_timestamp_created()),
-                               'composite_name' => 
$this->get_composite_names(),
-                               'party_name' => $party_name,
-                               'total_sum' => $this->get_total_sum(),
-                               'old_contract_id' => 
$this->get_old_contract_id(),
-                               'serial_number' => $this->get_serial_number()
-                       );
-               }
-       }
\ No newline at end of file
+<?php
+       phpgw::import_class('rental.socontract_price_item');
+       phpgw::import_class('rental.soinvoice_price_item');
+       include_class('rental', 'model', 'inc/model/');
+       include_class('rental', 'contract', 'inc/model/');
+       include_class('rental', 'invoice_price_item', 'inc/model/');
+
+       class rental_invoice extends rental_model
+       {
+
+               protected $id;
+               protected $billing_id; // The billing job that created this 
invoice
+               protected $contract_id; // Contract that this invoice belongs to
+               protected $party_id; // Party that is the recepient of this 
invoice
+               protected $party;
+               protected $timestamp_created; // Billing date
+               protected $timestamp_start; // Start date of invoice
+               protected $timestamp_end; // End date of invoice
+               protected $invoice_price_items;
+               protected $total_sum;
+               protected $total_area;
+               protected $header;
+               protected $account_in; // 'Art' for the income side
+               protected $account_out; // 'Art' for the outlay side
+               protected $composite_names; // From composite - not part of 
invoice db data
+               protected $project_id;
+               protected $service_id;
+               protected $responsibility_id;
+               protected $old_contract_id;
+               protected $term_id;
+               protected $term_label;
+               protected $billing_title;
+               protected $serial_number;
+               protected $reference;
+               public static $so;
+
+               public function __construct( int $id, int $billing_id, int 
$contract_id, int $timestamp_created, int $timestamp_start, int $timestamp_end, 
float $total_sum, float $total_area, string $header, string $account_in, string 
$account_out, string $service_id, string $responsibility_id )
+               {
+                       $this->id = (int)$id;
+                       $this->billing_id = (int)$billing_id;
+                       $this->contract_id = (int)$contract_id;
+                       $this->timestamp_created = (int)$timestamp_created;
+                       $this->timestamp_start = (int)$timestamp_start;
+                       $this->timestamp_end = (int)$timestamp_end;
+                       $this->total_sum = (float)$total_sum;
+                       $this->total_area = (float)$total_area;
+                       $this->invoice_price_items = null;
+                       $this->header = $header;
+                       $this->account_in = $account_in;
+                       $this->account_out = $account_out;
+                       $this->service_id = $service_id;
+                       $this->responsibility_id = $responsibility_id;
+                       $this->composite_names = array();
+               }
+
+               public function set_id( $id )
+               {
+                       $this->id = $id;
+               }
+
+               public function get_id()
+               {
+                       return $this->id;
+               }
+
+               public function set_billing_id( $billing_id )
+               {
+                       $this->billing_id = $billing_id;
+               }
+
+               public function get_billing_id()
+               {
+                       return $this->billing_id;
+               }
+
+               public function set_contract_id( $contract_id )
+               {
+                       $this->contract_id = $contract_id;
+               }
+
+               public function get_contract_id()
+               {
+                       return $this->contract_id;
+               }
+
+               public function set_timestamp_created( $timestamp_created )
+               {
+                       $this->timestamp_created = $timestamp_created;
+               }
+
+               public function get_timestamp_created()
+               {
+                       return $this->timestamp_created;
+               }
+
+               public function set_party_id( $party_id )
+               {
+                       $this->party_id = $party_id;
+               }
+
+               public function get_party_id()
+               {
+                       return $this->party_id;
+               }
+
+               public function set_party( rental_party $party )
+               {
+                       $this->party = $party;
+               }
+
+               public function get_party()
+               {
+                       return $this->party;
+               }
+
+               public function set_timestamp_start( $timestamp_start )
+               {
+                       $this->timestamp_start = $timestamp_start;
+               }
+
+               public function get_timestamp_start()
+               {
+                       return $this->timestamp_start;
+               }
+
+               public function set_timestamp_end( $timestamp_end )
+               {
+                       $this->timestamp_end = $timestamp_end;
+               }
+
+               public function get_timestamp_end()
+               {
+                       return $this->timestamp_end;
+               }
+
+               /**
+                * Adds a invoice price item to the invoice.
+                * NOTE: The price item must store itself. The invoice object 
does
+                * nothing with its items while storing itself.
+                * 
+                * @param $invoice_price_item rental_invoice_price_item to add.
+                */
+               public function add_invoice_price_item( 
rental_invoice_price_item &$invoice_price_item )
+               {
+                       if ($invoice_price_items == null)
+                       {
+                               $invoice_price_items = array();
+                       }
+                       $invoice_price_items[] = $invoice_price_item;
+               }
+
+               public function set_total_sum( float $total_sum )
+               {
+                       $this->total_sum = (float)$total_sum;
+               }
+
+               public function get_total_sum()
+               {
+                       return $this->total_sum;
+               }
+
+               public function set_total_area( float $total_area )
+               {
+                       $this->$total_area = (float)$total_area;
+               }
+
+               public function get_total_area()
+               {
+                       return $this->total_area;
+               }
+
+               public function add_composite_name( string $name )
+               {
+                       if (!in_array($name, $this->composite_names))
+                       {
+                               $this->composite_names[] = $name;
+                       }
+               }
+
+               public function set_header( $header )
+               {
+                       $this->header = $header;
+               }
+
+               public function get_header()
+               {
+                       return $this->header;
+               }
+
+               public function set_account_in( $account_in )
+               {
+                       $this->account_in = $account_in;
+               }
+
+               public function get_account_in()
+               {
+                       return $this->account_in;
+               }
+
+               public function set_account_out( $account_out )
+               {
+                       $this->account_out = $account_out;
+               }
+
+               public function get_account_out()
+               {
+                       return $this->account_out;
+               }
+
+               public function set_service_id( $service_id )
+               {
+                       $this->service_id = $service_id;
+               }
+
+               public function get_service_id()
+               {
+                       return $this->service_id;
+               }
+
+               public function set_responsibility_id( $responsibility_id )
+               {
+                       $this->responsibility_id = $responsibility_id;
+               }
+
+               public function get_responsibility_id()
+               {
+                       return $this->responsibility_id;
+               }
+
+               public function set_project_id( $project_id )
+               {
+                       $this->project_id = $project_id;
+               }
+
+               public function get_project_id()
+               {
+                       return $this->project_id;
+               }
+
+               public function set_old_contract_id( $old_contract_id )
+               {
+                       $this->old_contract_id = $old_contract_id;
+               }
+
+               public function get_old_contract_id()
+               {
+                       return $this->old_contract_id;
+               }
+
+               public function get_composite_names()
+               {
+                       $names = '';
+                       foreach ($this->composite_names as $name)
+                       {
+                               $names .= "{$name}<br/>";
+                       }
+                       return $names;
+               }
+
+               public function set_term_id( $term_id )
+               {
+                       $this->term_id = $term_id;
+               }
+
+               public function get_term_id()
+               {
+                       return $this->term_id;
+               }
+
+               public function set_serial_number( $serial_number )
+               {
+                       $this->serial_number = $serial_number;
+               }
+
+               public function get_serial_number()
+               {
+                       return $this->serial_number;
+               }
+
+               public function set_term_label( $term_label )
+               {
+                       $this->term_label = $term_label;
+               }
+
+               public function get_term_label()
+               {
+                       return $this->term_label;
+               }
+
+               public function set_month( $month )
+               {
+                       $this->month = $month;
+               }
+
+               public function get_month()
+               {
+                       return $this->month;
+               }
+
+               public function set_billing_title( $billing_title )
+               {
+                       $this->billing_title = $billing_title;
+               }
+
+               public function get_reference()
+               {
+                       return $this->reference;
+               }
+
+               public function set_reference( $reference )
+               {
+                       $this->reference = $reference;
+               }
+
+               public function get_billing_title()
+               {
+                       return $this->billing_title;
+               }
+
+               /**
+                * 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, // no identifier
+                               $billing_id, // the billing identifier
+                               $contract_id, // the contract identifier
+                               time(), // the creation time
+                               $timestamp_invoice_start, // the invoice start 
date
+                               $timestamp_invoice_end, // the invoice end date
+                               0, // the total sum of invoice (not calculated 
yet)
+                               $contract->get_rented_area(), // the area 
rented on the contract
+                               $contract->get_invoice_header(), // the invoice 
header
+                               $contract->get_account_in(), // the ingoing 
account number
+                               $account_out, // the outgoing account number
+                               $contract->get_service_id(), // the service 
identifier (internal)
+                               $contract->get_responsibility_id() // the 
responsibility identifier (internal)
+                       );
+
+                       // ... 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)
+                       {
+                               if($billing_term == 5)//credits
+                               {
+                                       $filters2 = array(
+                                               'contract_id' => 
$contract->get_id(),
+                                               'contract_ids_one_time' => 
true, 'credits' => true
+                                       );
+                               }
+                               else
+                               {
+                                       $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(0, 0, '', false, '', '', 
$filters2);
+                               //$contract_price_items = 
rental_socontract_price_item::get_instance()->get(0, 0, '', false, '', '', 
array('contract_id' => $contract->get_id(), 'one_time' => true));
+                       }
+                       else
+                       {
+                               $contract_price_items = 
rental_socontract_price_item::get_instance()->get(0, 0, '', false, '', '', 
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 == '') // Date not set
+                               {
+                                       // 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 == '') // Date not set
+                               {
+                                       // 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) // Start of price item before invoice start
+                               {
+                                       $invoice_price_item_start = 
$timestamp_invoice_start; // We use the invoice start
+                               }
+                               else if ($contract_price_item_start > 
$timestamp_invoice_end && $billing_term != 5) // Start of price item after this 
invoice ends
+                               {
+                                       continue; // We don't add this price 
item - continue to next
+                               }
+                               else // Price item start date is somewhere 
between start and end
+                               {
+                                       $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 && $billing_term != 5) // End of price item before 
this invoice starts
+                               {
+                                       continue; // We don't add this price 
item - continue to next
+                               }
+                               else if ($contract_price_item_end < 
$timestamp_invoice_end) // End of price item before invoice end
+                               {
+                                       $invoice_price_item_end = 
$contract_price_item_end; // We use the price item end
+                               }
+                               else // Price item end date is somewhere after 
invoice end
+                               {
+                                       $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) // The start of the contract is after the billing 
period (should never happen)
+                                       {
+                                               continue; //No price items for 
this contract will be billed
+                                       }
+
+                                       if ($contract_start > 
$invoice_price_item_start) // The contract start is after the start of the 
price item
+                                       {
+                                               $invoice_price_item_start = 
$contract_start;
+                                       }
+                               }
+
+                               if (isset($contract_end) && 
!$contract_price_item->is_one_time())
+                               {
+                                       if ($contract_end < 
$timestamp_invoice_start) // The end of the contract is before the billing 
period (should never happen)
+                                       {
+                                               continue; //No price items for 
this contract will be billed
+                                       }
+
+                                       if ($contract_end < 
$invoice_price_item_end) // The contract start is after the start of the price 
item
+                                       {
+                                               $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, // the number of decimals to 
use for the total price of the price item
+                                        -1, // no price item identifier
+                                        $invoice->get_id(), // the invoice 
identifier
+                                        $contract_price_item->get_title(), // 
the contract price item title
+                                        
$contract_price_item->get_agresso_id(), // the contract price item agresso 
identifier
+                                        $contract_price_item->is_area(), // 
flag for specifying if the contract is of area/piece
+                                        $contract_price_item->get_price(), // 
the price of the contract price item
+                                        $contract_price_item->get_area(), // 
the rented area on this contract (derived from contract)
+                                        $contract_price_item->get_count(), // 
the number of items on this price item
+                                        $invoice_price_item_start, // the 
start date from which this price item should be calculated
+                                        $invoice_price_item_end   // the end 
date to which this price item should be calculated
+                               );
+
+                               // 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())
+                               {
+                                       
$invoice_price_item->set_is_one_time(true);
+                                       if ($billing_term == 5 || 
($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);
+
+                               //FIXME
+                               if($billing_term == 5)
+                               {
+                                       
$invoice->set_header($contract_price_item->get_title());
+                               }
+
+                               // 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;
+               }
+
+               public function serialize()
+               {
+                       $party_name = '';
+                       if ($this->get_party() != null)
+                       {
+                               $serialized_party = 
$this->get_party()->serialize();
+                               $party_name = $serialized_party['name'];
+                       }
+                       $date_format = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
+                       return array(
+                               'id' => $this->get_id(),
+                               'contract_id' => $this->get_contract_id(),
+                               'term_label' => $this->get_term_label(),
+                               'timestamp_created' => date($date_format, 
$this->get_timestamp_created()),
+                               'composite_name' => 
$this->get_composite_names(),
+                               'party_name' => $party_name,
+                               'total_sum' => $this->get_total_sum(),
+                               'old_contract_id' => 
$this->get_old_contract_id(),
+                               'serial_number' => $this->get_serial_number()
+                       );
+               }
+       }

Modified: trunk/rental/inc/model/class.invoice_price_item.inc.php
===================================================================
--- trunk/rental/inc/model/class.invoice_price_item.inc.php     2016-05-26 
01:41:27 UTC (rev 15205)
+++ trunk/rental/inc/model/class.invoice_price_item.inc.php     2016-05-26 
06:48:36 UTC (rev 15206)
@@ -1,260 +1,260 @@
-<?php
-       include_class('rental', 'contract', 'inc/model/');
-       include_class('rental', 'price_item', 'inc/model/');
-
-       /**
-        * Represents a price item in an invoice. The data is typically built 
from
-        * an instance of rental_contract_price_item.
-        *
-        */
-       class rental_invoice_price_item extends rental_price_item
-       {
-
-               protected $decimals;
-               protected $invoice_id;
-               protected $is_area;
-               protected $is_one_time;
-               protected $price_per_year;
-               protected $area;
-               protected $count;
-               protected $total_price;
-               protected $timestamp_start; // Start date for the given invoice
-               protected $timestamp_end; // End date for the given invoice
-               public static $so;
-
-               public function __construct( int $decimals, int $id, int 
$invoice_id, string $title, string $agresso_id, bool $is_area, float 
$price_per_year, float $area, int $count, int $timestamp_start, int 
$timestamp_end )
-               {
-                       $this->decimals = (int)$decimals;
-                       $this->id = (int)$id;
-                       $this->invoice_id = (int)$invoice_id;
-                       $this->title = $title;
-                       $this->agresso_id = $agresso_id;
-                       $this->is_area = (bool)$is_area;
-                       $this->price_per_year = (float)$price_per_year;
-                       $this->area = (float)$area;
-                       $this->count = (int)$count;
-                       $this->timestamp_start = (int)$timestamp_start;
-                       $this->timestamp_end = (int)$timestamp_end;
-                       $this->total_price = null; // Needs to be re-calculated
-               }
-
-               public function set_invoice_id( int $invoice_id )
-               {
-                       $this->invoice_id = (int)$invoice_id;
-               }
-
-               public function get_invoice_id()
-               {
-                       return $this->invoice_id;
-               }
-
-               public function set_is_area( bool $is_area )
-               {
-                       $this->is_area = (bool)$is_area;
-                       $this->total_price = null; // Needs to be re-calculated
-               }
-
-               public function is_area()
-               {
-                       return $this->is_area;
-               }
-
-               public function set_count( int $count )
-               {
-                       $this->count = (int)$count;
-                       $this->total_price = null; // Needs to be re-calculated
-               }
-
-               public function set_price( $price_per_year )
-               {
-                       $this->price_per_year = (float)$price_per_year;
-                       $this->total_price = null; // Needs to be re-calculated
-               }
-
-               public function get_price()
-               {
-                       return $this->price_per_year;
-               }
-
-               public function set_area( $area )
-               {
-                       $this->area = (float)$area;
-                       $this->total_price = null; // Needs to be re-calculated
-               }
-
-               public function get_area()
-               {
-                       return $this->area;
-               }
-
-               public function get_count()
-               {
-                       return $this->count;
-               }
-
-               public function set_total_price( float $total_price )
-               {
-                       $this->total_price = (float)$total_price;
-               }
-
-               /**
-                * This method calculated the total price of the invoice price 
item if it hasn't been done before
-                * 
-                * @return float        the total price of the price item
-                */
-               public function get_total_price()
-               {
-                       if ($this->total_price == null) // Needs to be 
calculated
-                       {
-                               // The calculation of the price for complete 
months (meaning the item applies for the whole month) ..
-                               $num_of_complete_months = 0;
-
-                               // ..is different than the calculation for 
incomplete months
-                               $incomplete_months = array();
-
-                               // Get the year, month and day from the 
startdate timestamp
-                               $date_start = array();
-                               $date_start['year'] = (int)date('Y', 
$this->get_timestamp_start());
-                               $date_start['month'] = (int)date('n', 
$this->get_timestamp_start());
-                               $date_start['day'] = (int)date('j', 
$this->get_timestamp_start());
-
-                               // Get the year, month and day from the enddate 
timestamp
-                               $date_end = array();
-                               $date_end['year'] = (int)date('Y', 
$this->get_timestamp_end());
-                               $date_end['month'] = (int)date('n', 
$this->get_timestamp_end());
-                               $date_end['day'] = (int)date('j', 
$this->get_timestamp_end());
-
-                               // Runs through all the years this price item 
goes for
-                               for ($current_year = $date_end['year']; 
$current_year >= $date_start['year']; $current_year--)
-                               {
-                                       // Within each year: which months do 
the price item run for
-                                       // First we set the defaults (whole 
year)
-                                       $current_start_month = 1; // January
-                                       $current_end_month = 12; // December
-                                       // If we are at the start year, use the 
start month of this year as start month
-                                       if ($current_year == 
$date_start['year'])
-                                       {
-                                               $current_start_month = 
$date_start['month'];
-                                       }
-
-                                       // If we are at the start year, use the 
end month of this year as end month
-                                       if ($current_year == $date_end['year'])
-                                       {
-                                               $current_end_month = 
$date_end['month'];
-                                       }
-
-                                       // Runs through all of the months of 
the current year (we go backwards since we go backwards with the years)
-                                       for ($current_month = 
$current_end_month; $current_month >= $current_start_month; $current_month--)
-                                       {
-                                               // Retrive the number of days 
in the current month
-                                               $num_of_days_in_current_month = 
date('t', strtotime($current_year . '-' . $current_month . '-01'));
-
-                                               // Set the defaults (whole 
month)
-                                               $first_day = 1;
-                                               $last_day = 
$num_of_days_in_current_month;
-
-                                               // If we are at the start month 
in the start year, use day in this month as first day
-                                               if ($current_year == 
$date_start['year'] && $current_month == $date_start['month'])
-                                               {
-                                                       $first_day = 
$date_start['day'];
-                                               }
-
-                                               // If we are at the end month 
in the end year, use the day in this month as end day
-                                               if ($current_year == 
$date_end['year'] && $current_month == $date_end['month'])
-                                               {
-                                                       $last_day = 
$date_end['day']; // The end date's day is the item's end day
-                                               }
-
-                                               // Increase counter: complete 
months or incomplete months (number of days in this year and number of days )
-                                               if ($first_day === 1 && 
$last_day == $num_of_days_in_current_month)
-                                               { // This is a whole month
-                                                       
$num_of_complete_months++;
-                                               }
-                                               else // Incomplete month
-                                               {
-                                                       // YYY: There must be a 
better day to do this!?
-                                                       
$num_of_days_in_current_year = (date('L', strtotime($current_year . '01-01')) 
== 0) ? 365 : 366;
-                                                       $num_of_days = 
$last_day - $first_day + 1;
-                                                       $incomplete_months[] = 
array($num_of_days_in_current_year, $num_of_days);
-                                               }
-                                       }
-                               }
-                               // ---- Calculate complemete months
-                               // Retrieve the amount: rented area of contract 
or the number of items (depending on type of price element)
-                               $amount = $this->is_area() ? $this->get_area() 
: $this->get_count();
-
-                               // The total price of this price element for 
complete months
-                               $this->total_price = (($this->get_price() * 
$num_of_complete_months) / 12.0) * $amount;
-
-                               // ---- Calculate incomplete months
-
-                               $price_per_year = $this->get_price() * $amount;
-
-                               // Run through all the incomplete months ...
-                               foreach ($incomplete_months as $day_factors)
-                               {
-                                       // ... and add the sum of each 
incomplete month to the total price of the price item
-                                       // Calculation: Price per day (price 
per year divided with number of days in year) multiplied with number of days in 
incomplete month
-                                       $this->total_price += ($price_per_year 
/ $day_factors[0]) * $day_factors[1];
-                               }
-                               // We round the total price for each price item 
with the specified number of decimals precision
-                               $this->total_price = round($this->total_price, 
$this->decimals);
-                       }
-                       return $this->total_price;
-               }
-
-               public function set_timestamp_start( int $timestamp_start )
-               {
-                       $this->timestamp_start = (int)$timestamp_start;
-                       $this->total_price = null; // Needs to be re-calculated
-               }
-
-               public function get_timestamp_start()
-               {
-                       return $this->timestamp_start;
-               }
-
-               public function set_timestamp_end( int $timestamp_end )
-               {
-                       $this->timestamp_end = (int)$timestamp_end;
-                       $this->total_price = null; // Needs to be re-calculated
-               }
-
-               public function get_timestamp_end()
-               {
-                       return $this->timestamp_end;
-               }
-
-               public function set_is_one_time( $is_one_time )
-               {
-                       $this->is_one_time = (bool)$is_one_time;
-               }
-
-               public function is_one_time()
-               {
-                       return $this->is_one_time;
-               }
-
-               public function get_is_one_time()
-               {
-                       return $this->is_one_time;
-               }
-
-               public function serialize()
-               {
-                       $date_format = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
-                       return array
-                               (
-                               'title' => $this->get_title(),
-                               'agresso_id' => $this->get_agresso_id(),
-                               'is_area' => $this->get_type_text(),
-                               'is_one_time' => $this->get_is_one_time(),
-                               'price' => $this->get_price(),
-                               'area' => $this->get_area(),
-                               'count' => $this->get_count(),
-                               'total_price' => $this->get_total_price(),
-                               'timestamp_start' => date($date_format, 
$this->get_timestamp_start()),
-                               'timestamp_end' => date($date_format, 
$this->get_timestamp_end()),
-                       );
-               }
-       }
\ No newline at end of file
+<?php
+       include_class('rental', 'contract', 'inc/model/');
+       include_class('rental', 'price_item', 'inc/model/');
+
+       /**
+        * Represents a price item in an invoice. The data is typically built 
from
+        * an instance of rental_contract_price_item.
+        *
+        */
+       class rental_invoice_price_item extends rental_price_item
+       {
+
+               protected $decimals;
+               protected $invoice_id;
+               protected $is_area;
+               protected $is_one_time;
+               protected $price_per_year;
+               protected $area;
+               protected $count;
+               protected $total_price;
+               protected $timestamp_start; // Start date for the given invoice
+               protected $timestamp_end; // End date for the given invoice
+               public static $so;
+
+               public function __construct( int $decimals, int $id, int 
$invoice_id, string $title, string $agresso_id, bool $is_area, float 
$price_per_year, float $area, int $count, int $timestamp_start, int 
$timestamp_end )
+               {
+                       $this->decimals = (int)$decimals;
+                       $this->id = (int)$id;
+                       $this->invoice_id = (int)$invoice_id;
+                       $this->title = $title;
+                       $this->agresso_id = $agresso_id;
+                       $this->is_area = (bool)$is_area;
+                       $this->price_per_year = (float)$price_per_year;
+                       $this->area = (float)$area;
+                       $this->count = (int)$count;
+                       $this->timestamp_start = (int)$timestamp_start;
+                       $this->timestamp_end = (int)$timestamp_end;
+                       $this->total_price = null; // Needs to be re-calculated
+               }
+
+               public function set_invoice_id( int $invoice_id )
+               {
+                       $this->invoice_id = (int)$invoice_id;
+               }
+
+               public function get_invoice_id()
+               {
+                       return $this->invoice_id;
+               }
+
+               public function set_is_area( bool $is_area )
+               {
+                       $this->is_area = (bool)$is_area;
+                       $this->total_price = null; // Needs to be re-calculated
+               }
+
+               public function is_area()
+               {
+                       return $this->is_area;
+               }
+
+               public function set_count( int $count )
+               {
+                       $this->count = (int)$count;
+                       $this->total_price = null; // Needs to be re-calculated
+               }
+
+               public function set_price( $price_per_year )
+               {
+                       $this->price_per_year = (float)$price_per_year;
+                       $this->total_price = null; // Needs to be re-calculated
+               }
+
+               public function get_price()
+               {
+                       return $this->price_per_year;
+               }
+
+               public function set_area( $area )
+               {
+                       $this->area = (float)$area;
+                       $this->total_price = null; // Needs to be re-calculated
+               }
+
+               public function get_area()
+               {
+                       return $this->area;
+               }
+
+               public function get_count()
+               {
+                       return $this->count;
+               }
+
+               public function set_total_price( float $total_price )
+               {
+                       $this->total_price = (float)$total_price;
+               }
+
+               /**
+                * This method calculated the total price of the invoice price 
item if it hasn't been done before
+                * 
+                * @return float        the total price of the price item
+                */
+               public function get_total_price()
+               {
+                       if ($this->total_price == null) // Needs to be 
calculated
+                       {
+                               // The calculation of the price for complete 
months (meaning the item applies for the whole month) ..
+                               $num_of_complete_months = 0;
+
+                               // ..is different than the calculation for 
incomplete months
+                               $incomplete_months = array();
+
+                               // Get the year, month and day from the 
startdate timestamp
+                               $date_start = array();
+                               $date_start['year'] = (int)date('Y', 
$this->get_timestamp_start());
+                               $date_start['month'] = (int)date('n', 
$this->get_timestamp_start());
+                               $date_start['day'] = (int)date('j', 
$this->get_timestamp_start());
+
+                               // Get the year, month and day from the enddate 
timestamp
+                               $date_end = array();
+                               $date_end['year'] = (int)date('Y', 
$this->get_timestamp_end());
+                               $date_end['month'] = (int)date('n', 
$this->get_timestamp_end());
+                               $date_end['day'] = (int)date('j', 
$this->get_timestamp_end());
+
+                               // Runs through all the years this price item 
goes for
+                               for ($current_year = $date_end['year']; 
$current_year >= $date_start['year']; $current_year--)
+                               {
+                                       // Within each year: which months do 
the price item run for
+                                       // First we set the defaults (whole 
year)
+                                       $current_start_month = 1; // January
+                                       $current_end_month = 12; // December
+                                       // If we are at the start year, use the 
start month of this year as start month
+                                       if ($current_year == 
$date_start['year'])
+                                       {
+                                               $current_start_month = 
$date_start['month'];
+                                       }
+
+                                       // If we are at the start year, use the 
end month of this year as end month
+                                       if ($current_year == $date_end['year'])
+                                       {
+                                               $current_end_month = 
$date_end['month'];
+                                       }
+
+                                       // Runs through all of the months of 
the current year (we go backwards since we go backwards with the years)
+                                       for ($current_month = 
$current_end_month; $current_month >= $current_start_month; $current_month--)
+                                       {
+                                               // Retrive the number of days 
in the current month
+                                               $num_of_days_in_current_month = 
date('t', strtotime($current_year . '-' . $current_month . '-01'));
+
+                                               // Set the defaults (whole 
month)
+                                               $first_day = 1;
+                                               $last_day = 
$num_of_days_in_current_month;
+
+                                               // If we are at the start month 
in the start year, use day in this month as first day
+                                               if ($current_year == 
$date_start['year'] && $current_month == $date_start['month'])
+                                               {
+                                                       $first_day = 
$date_start['day'];
+                                               }
+
+                                               // If we are at the end month 
in the end year, use the day in this month as end day
+                                               if ($current_year == 
$date_end['year'] && $current_month == $date_end['month'])
+                                               {
+                                                       $last_day = 
$date_end['day']; // The end date's day is the item's end day
+                                               }
+
+                                               // Increase counter: complete 
months or incomplete months (number of days in this year and number of days )
+                                               if ($first_day === 1 && 
$last_day == $num_of_days_in_current_month)
+                                               { // This is a whole month
+                                                       
$num_of_complete_months++;
+                                               }
+                                               else // Incomplete month
+                                               {
+                                                       // YYY: There must be a 
better day to do this!?
+                                                       
$num_of_days_in_current_year = (date('L', strtotime($current_year . '01-01')) 
== 0) ? 365 : 366;
+                                                       $num_of_days = 
$last_day - $first_day + 1;
+                                                       $incomplete_months[] = 
array($num_of_days_in_current_year, $num_of_days);
+                                               }
+                                       }
+                               }
+                               // ---- Calculate complemete months
+                               // Retrieve the amount: rented area of contract 
or the number of items (depending on type of price element)
+                               $amount = $this->is_area() ? $this->get_area() 
: $this->get_count();
+
+                               // The total price of this price element for 
complete months
+                               $this->total_price = (($this->get_price() * 
$num_of_complete_months) / 12.0) * $amount;
+
+                               // ---- Calculate incomplete months
+
+                               $price_per_year = $this->get_price() * $amount;
+
+                               // Run through all the incomplete months ...
+                               foreach ($incomplete_months as $day_factors)
+                               {
+                                       // ... and add the sum of each 
incomplete month to the total price of the price item
+                                       // Calculation: Price per day (price 
per year divided with number of days in year) multiplied with number of days in 
incomplete month
+                                       $this->total_price += ($price_per_year 
/ $day_factors[0]) * $day_factors[1];
+                               }
+                               // We round the total price for each price item 
with the specified number of decimals precision
+                               $this->total_price = round($this->total_price, 
$this->decimals);
+                       }
+                       return $this->total_price;
+               }
+
+               public function set_timestamp_start( int $timestamp_start )
+               {
+                       $this->timestamp_start = (int)$timestamp_start;
+                       $this->total_price = null; // Needs to be re-calculated
+               }
+
+               public function get_timestamp_start()
+               {
+                       return $this->timestamp_start;
+               }
+
+               public function set_timestamp_end( int $timestamp_end )
+               {
+                       $this->timestamp_end = (int)$timestamp_end;
+                       $this->total_price = null; // Needs to be re-calculated
+               }
+
+               public function get_timestamp_end()
+               {
+                       return $this->timestamp_end;
+               }
+
+               public function set_is_one_time( $is_one_time )
+               {
+                       $this->is_one_time = (bool)$is_one_time;
+               }
+
+               public function is_one_time()
+               {
+                       return $this->is_one_time;
+               }
+
+               public function get_is_one_time()
+               {
+                       return $this->is_one_time;
+               }
+
+               public function serialize()
+               {
+                       $date_format = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
+                       return array
+                               (
+                               'title' => $this->get_title(),
+                               'agresso_id' => $this->get_agresso_id(),
+                               'is_area' => $this->get_type_text(),
+                               'is_one_time' => $this->get_is_one_time(),
+                               'price' => $this->get_price(),
+                               'area' => $this->get_area(),
+                               'count' => $this->get_count(),
+                               'total_price' => $this->get_total_price(),
+                               'timestamp_start' => date($date_format, 
$this->get_timestamp_start()),
+                               'timestamp_end' => date($date_format, 
$this->get_timestamp_end()),
+                       );
+               }
+       }

Modified: trunk/rental/setup/tables_current.inc.php
===================================================================
--- trunk/rental/setup/tables_current.inc.php   2016-05-26 01:41:27 UTC (rev 
15205)
+++ trunk/rental/setup/tables_current.inc.php   2016-05-26 06:48:36 UTC (rev 
15206)
@@ -399,7 +399,8 @@
                                'total_price' => array('type' => 'decimal', 
'precision' => '20', 'scale' => '2',
                                        'nullable' => true),
                                'date_start' => array('type' => 'date'),
-                               'date_end' => array('type' => 'date')
+                               'date_end' => array('type' => 'date'),
+                                'is_one_time' => array('type' => 'bool', 
'nullable' => false, 'default' => 'true')
                        ),
                        'pk' => array('id'),
                        'fk' => array(

Modified: trunk/rental/setup/tables_update.inc.php
===================================================================
--- trunk/rental/setup/tables_update.inc.php    2016-05-26 01:41:27 UTC (rev 
15205)
+++ trunk/rental/setup/tables_update.inc.php    2016-05-26 06:48:36 UTC (rev 
15206)
@@ -1,412 +1,412 @@
-<?php
-       /**
-        * Update Rental from v 0.0.27 to 0.1.0
-        */
-       $test[] = '0.0.27';
-
-       function rental_upgrade0_0_27()
-       {
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_party', 
'org_enhet_id', array(
-                       'type' => 'int', 'precision' => 8, 'nullable' => true));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0';
-
-       function rental_upgrade0_1_0()
-       {
-               $GLOBALS['phpgw_setup']->oProc->AlterColumn('rental_contract', 
'adjustment_share', array(
-                       'type' => 'int',
-                       'precision' => '4',
-                       'nullable' => true,
-                       'default' => 100
-               ));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.1';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.1';
-
-       function rental_upgrade0_1_0_1()
-       {
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_adjustment', 
'adjustment_type', array(
-                       'type' => 'varchar', 'precision' => '255', 'nullable' 
=> true));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.2';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.2';
-
-       function rental_upgrade0_1_0_2()
-       {
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_adjustment', 
'is_executed', array(
-                       'type' => 'bool', 'nullable' => false, 'default' => 
'false'));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.3';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.3';
-
-       function rental_upgrade0_1_0_3()
-       {
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_contract', 
'publish_comment', array(
-                       'type' => 'bool', 'nullable' => true, 'default' => 
'false'));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.4';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.4';
-
-       function rental_upgrade0_1_0_4()
-       {
-               $asyncservice = CreateObject('phpgwapi.asyncservice');
-               $asyncservice->set_timer(
-                       array('day' => "*/1"), 'rental_run_adjustments', 
'rental.soadjustment.run_adjustments', null
-               );
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.5';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.5';
-
-       function rental_upgrade0_1_0_5()
-       {
-               
$GLOBALS['phpgw_setup']->oProc->AlterColumn('rental_notification_workbench', 
'notification_id', array(
-                       'type' => 'int',
-                       'precision' => '4',
-                       'nullable' => true
-               ));
-
-               
$GLOBALS['phpgw_setup']->oProc->AddColumn('rental_notification_workbench', 
'workbench_message', array(
-                       'type' => 'text'));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.6';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.6';
-
-       function rental_upgrade0_1_0_6()
-       {
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_invoice', 
'serial_number', array(
-                       'type' => 'int',
-                       'precision' => '8',
-                       'nullable' => true
-               ));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.7';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.7';
-
-       function rental_upgrade0_1_0_7()
-       {
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_price_item', 
'standard', array(
-                       'type' => 'bool',
-                       'nullable' => true,
-                       'default' => 'false'
-               ));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.8';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.8';
-
-       function rental_upgrade0_1_0_8()
-       {
-               
$GLOBALS['phpgw_setup']->oProc->RenameColumn('rental_contract_responsibility', 
'agresso_export_format', 'export_format');
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.9';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.9';
-
-       function rental_upgrade0_1_0_9()
-       {
-
-
-               $sql = 'SELECT config_name,config_value FROM phpgw_config'
-                       . " WHERE config_name = 'files_dir'"
-                       . " OR config_name = 'file_repository'";
-
-               $GLOBALS['phpgw_setup']->oProc->query($sql, __LINE__, __FILE__);
-               while ($GLOBALS['phpgw_setup']->oProc->next_record())
-               {
-                       
$GLOBALS['phpgw_info']['server'][$GLOBALS['phpgw_setup']->oProc->f('config_name',
 true)] = $GLOBALS['phpgw_setup']->oProc->f('config_value', true);
-               }
-               $GLOBALS['phpgw']->db = & $GLOBALS['phpgw_setup']->oProc->m_odb;
-               $acl = CreateObject('phpgwapi.acl');
-
-               $admins = $acl->get_ids_for_location('run', 1, 'admin');
-               $GLOBALS['phpgw_info']['user']['account_id'] = $admins[0];
-
-               //used in vfs
-               define('PHPGW_ACL_READ', 1);
-               define('PHPGW_ACL_ADD', 2);
-               define('PHPGW_ACL_EDIT', 4);
-               define('PHPGW_ACL_DELETE', 8);
-
-               $GLOBALS['phpgw']->session = createObject('phpgwapi.sessions');
-
-               //Prepare paths
-               $vfs = CreateObject('phpgwapi.vfs');
-               $vfs->override_acl = 1;
-
-               $path = "/rental";
-               $dir = array('string' => $path, 'relatives' => array( 
RELATIVE_NONE));
-               if (!$vfs->file_exists($dir))
-               {
-                       if (!$vfs->mkdir($dir))
-                       {
-                               return;
-                       }
-               }
-
-               $path .= "/billings";
-               $dir = array('string' => $path, 'relatives' => array( 
RELATIVE_NONE));
-               if (!$vfs->file_exists($dir))
-               {
-                       if (!$vfs->mkdir($dir))
-                       {
-                               return;
-                       }
-               }
-
-               $sql = "SELECT id, export_data FROM rental_billing";
-               $db = clone $GLOBALS['phpgw']->db;
-               $result = $db->query($sql, __LINE__, __FILE__);
-
-               while ($db->next_record())
-               {
-                       $id = $db->f('id', true);
-                       $export_data = $db->f('export_data', 'string');
-                       $file_path = $path . "/{$id}";
-                       if ($export_data != "")
-                       {
-                               $result = $vfs->write
-                                       (
-                                       array
-                                               (
-                                               'string' => $file_path,
-                                               'relatives' => array( 
RELATIVE_NONE),
-                                               'content' => $export_data
-                                       )
-                               );
-                       }
-               }
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.10';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.10';
-
-       function rental_upgrade0_1_0_10()
-       {
-               
$GLOBALS['phpgw_setup']->oProc->RenameColumn('rental_adjustment', 'interval', 
'adjustment_interval');
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.11';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.11';
-
-       function rental_upgrade0_1_0_11()
-       {
-               // Add adjustment year column
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_adjustment', 
'year', array(
-                       'type' => 'int',
-                       'nullable' => true,
-                       'precision' => '4'
-               ));
-
-               // Update year column to match the adjustment_date of all 
existing adjustments
-               $so = CreateObject('rental.soadjustment');
-               foreach ($so->get(0, NULL, NULL, true, NULL, NULL, NULL) as 
$adjustment)
-               {
-                       $year = strftime('%Y', 
$adjustment->get_adjustment_date());
-                       $adjustment->set_year($year);
-                       $so->store($adjustment);
-               }
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.12';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.12';
-
-       function rental_upgrade0_1_0_12()
-       {
-               $asyncservice = CreateObject('phpgwapi.asyncservice');
-               $asyncservice->set_timer(
-                       array('day' => "*/1"), 'rental_sync_party_name', 
'rental.uiparty.syncronize_party_name', null
-               );
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.13';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.13';
-
-       function rental_upgrade0_1_0_13()
-       {
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_composite', 
'furnish_type_id', array(
-                       'type' => 'int',
-                       'precision' => 4,
-                       'nullable' => 'True'
-               ));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.14';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.14';
-
-       function rental_upgrade0_1_0_14()
-       {
-               // Add unit_leader column
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_party', 
'unit_leader', array(
-                       'type' => 'varchar',
-                       'nullable' => true,
-                       'precision' => '255'
-               ));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.15';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       /*
-        * function moved to so-class
-        */
-       $test[] = '0.1.0.15';
-
-       function rental_upgrade0_1_0_15()
-       {
-               $asyncservice = CreateObject('phpgwapi.asyncservice');
-               $asyncservice->delete('rental_sync_party_name');
-               $asyncservice->set_timer(
-                       array('day' => "*/1"), 'rental_sync_party_name', 
'rental.soparty.syncronize_party_name', null
-               );
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.16';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.16';
-
-       function rental_upgrade0_1_0_16()
-       {
-               $sql = "INSERT INTO rental_billing_term (title, months) VALUES 
('free_of_charge','0')";
-               $db = clone $GLOBALS['phpgw']->db;
-               $result = $db->query($sql, __LINE__, __FILE__);
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.17';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.17';
-
-       function rental_upgrade0_1_0_17()
-       {
-               $GLOBALS['phpgw']->locations->add('.admin', 'Admin section', 
'rental');
-
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_composite', 
'standard_id', array(
-                       'type' => 'int',
-                       'precision' => 4,
-                       'nullable' => 'True'
-               ));
-
-               $GLOBALS['phpgw_setup']->oProc->CreateTable(
-                       'rental_composite_standard', array(
-                       'fd' => array(
-                               'id' => array('type' => 'int', 'precision' => 
'4', 'nullable' => false),
-                               'name' => array('type' => 'varchar', 
'precision' => '255', 'nullable' => false),
-                               'factor' => array('type' => 'decimal', 
'precision' => '20', 'scale' => '2', 'nullable' => true),
-                       ),
-                       'pk' => array('id'),
-                       'fk' => array(),
-                       'ix' => array(),
-                       'uc' => array()
-                       )
-               );
-
-               $GLOBALS['phpgw_setup']->oProc->CreateTable(
-                       'rental_contract_responsibility_unit', array(
-                       'fd' => array(
-                               'id' => array('type' => 'int', 'precision' => 
'4', 'nullable' => false),
-                               'name' => array('type' => 'varchar', 
'precision' => '255', 'nullable' => false)
-                       ),
-                       'pk' => array('id'),
-                       'fk' => array(),
-                       'ix' => array(),
-                       'uc' => array()
-                       )
-               );
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.18';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.18';
-
-       function rental_upgrade0_1_0_18()
-       {
-
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_contract', 
'billing_end', array(
-                       'type' => 'int',
-                       'precision' => 8,
-                       'nullable' => 'True'
-               ));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.19';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.19';
-
-       function rental_upgrade0_1_0_19()
-       {
-               // Add unit_leader column
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_adjustment', 
'extra_adjustment', array(
-                       'type' => 'bool',
-                       'nullable' => true,
-                       'default' => 'false'
-               ));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.20';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.20';
-
-       function rental_upgrade0_1_0_20()
-       {
-               $GLOBALS['phpgw']->locations->add('.contract', 'Contract', 
'rental', $allow_grant = false, $custom_tbl = false, $c_function = true);
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.21';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-       $test[] = '0.1.0.21';
-
-       function rental_upgrade0_1_0_21()
-       {
-
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_price_item', 
'type', array(
-                       'type' => 'int',
-                       'precision' => 2,
-                       'nullable' => false,
-                       'default' => 1
-               ));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.22';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-
-       $test[] = '0.1.0.22';
-       function rental_upgrade0_1_0_22()
-       {
-               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_contract', 
'override_adjustment_start', array(
-                       'type' => 'int',
-                       'precision' => 4,
-                       'nullable' => true
-               ));
-
-               
$GLOBALS['phpgw_setup']->oProc->AddColumn('rental_invoice_price_item', 
'is_one_time', array(
-                       'type' => 'bool',
-                       'nullable' => true,
-                       'default' => 'false'
-               ));
-
-               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.23';
-               return $GLOBALS['setup_info']['rental']['currentver'];
-       }
-
-
+<?php
+       /**
+        * Update Rental from v 0.0.27 to 0.1.0
+        */
+       $test[] = '0.0.27';
+
+       function rental_upgrade0_0_27()
+       {
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_party', 
'org_enhet_id', array(
+                       'type' => 'int', 'precision' => 8, 'nullable' => true));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0';
+
+       function rental_upgrade0_1_0()
+       {
+               $GLOBALS['phpgw_setup']->oProc->AlterColumn('rental_contract', 
'adjustment_share', array(
+                       'type' => 'int',
+                       'precision' => '4',
+                       'nullable' => true,
+                       'default' => 100
+               ));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.1';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.1';
+
+       function rental_upgrade0_1_0_1()
+       {
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_adjustment', 
'adjustment_type', array(
+                       'type' => 'varchar', 'precision' => '255', 'nullable' 
=> true));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.2';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.2';
+
+       function rental_upgrade0_1_0_2()
+       {
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_adjustment', 
'is_executed', array(
+                       'type' => 'bool', 'nullable' => false, 'default' => 
'false'));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.3';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.3';
+
+       function rental_upgrade0_1_0_3()
+       {
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_contract', 
'publish_comment', array(
+                       'type' => 'bool', 'nullable' => true, 'default' => 
'false'));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.4';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.4';
+
+       function rental_upgrade0_1_0_4()
+       {
+               $asyncservice = CreateObject('phpgwapi.asyncservice');
+               $asyncservice->set_timer(
+                       array('day' => "*/1"), 'rental_run_adjustments', 
'rental.soadjustment.run_adjustments', null
+               );
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.5';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.5';
+
+       function rental_upgrade0_1_0_5()
+       {
+               
$GLOBALS['phpgw_setup']->oProc->AlterColumn('rental_notification_workbench', 
'notification_id', array(
+                       'type' => 'int',
+                       'precision' => '4',
+                       'nullable' => true
+               ));
+
+               
$GLOBALS['phpgw_setup']->oProc->AddColumn('rental_notification_workbench', 
'workbench_message', array(
+                       'type' => 'text'));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.6';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.6';
+
+       function rental_upgrade0_1_0_6()
+       {
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_invoice', 
'serial_number', array(
+                       'type' => 'int',
+                       'precision' => '8',
+                       'nullable' => true
+               ));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.7';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.7';
+
+       function rental_upgrade0_1_0_7()
+       {
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_price_item', 
'standard', array(
+                       'type' => 'bool',
+                       'nullable' => true,
+                       'default' => 'false'
+               ));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.8';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.8';
+
+       function rental_upgrade0_1_0_8()
+       {
+               
$GLOBALS['phpgw_setup']->oProc->RenameColumn('rental_contract_responsibility', 
'agresso_export_format', 'export_format');
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.9';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.9';
+
+       function rental_upgrade0_1_0_9()
+       {
+
+
+               $sql = 'SELECT config_name,config_value FROM phpgw_config'
+                       . " WHERE config_name = 'files_dir'"
+                       . " OR config_name = 'file_repository'";
+
+               $GLOBALS['phpgw_setup']->oProc->query($sql, __LINE__, __FILE__);
+               while ($GLOBALS['phpgw_setup']->oProc->next_record())
+               {
+                       
$GLOBALS['phpgw_info']['server'][$GLOBALS['phpgw_setup']->oProc->f('config_name',
 true)] = $GLOBALS['phpgw_setup']->oProc->f('config_value', true);
+               }
+               $GLOBALS['phpgw']->db = & $GLOBALS['phpgw_setup']->oProc->m_odb;
+               $acl = CreateObject('phpgwapi.acl');
+
+               $admins = $acl->get_ids_for_location('run', 1, 'admin');
+               $GLOBALS['phpgw_info']['user']['account_id'] = $admins[0];
+
+               //used in vfs
+               define('PHPGW_ACL_READ', 1);
+               define('PHPGW_ACL_ADD', 2);
+               define('PHPGW_ACL_EDIT', 4);
+               define('PHPGW_ACL_DELETE', 8);
+
+               $GLOBALS['phpgw']->session = createObject('phpgwapi.sessions');
+
+               //Prepare paths
+               $vfs = CreateObject('phpgwapi.vfs');
+               $vfs->override_acl = 1;
+
+               $path = "/rental";
+               $dir = array('string' => $path, 'relatives' => array( 
RELATIVE_NONE));
+               if (!$vfs->file_exists($dir))
+               {
+                       if (!$vfs->mkdir($dir))
+                       {
+                               return;
+                       }
+               }
+
+               $path .= "/billings";
+               $dir = array('string' => $path, 'relatives' => array( 
RELATIVE_NONE));
+               if (!$vfs->file_exists($dir))
+               {
+                       if (!$vfs->mkdir($dir))
+                       {
+                               return;
+                       }
+               }
+
+               $sql = "SELECT id, export_data FROM rental_billing";
+               $db = clone $GLOBALS['phpgw']->db;
+               $result = $db->query($sql, __LINE__, __FILE__);
+
+               while ($db->next_record())
+               {
+                       $id = $db->f('id', true);
+                       $export_data = $db->f('export_data', 'string');
+                       $file_path = $path . "/{$id}";
+                       if ($export_data != "")
+                       {
+                               $result = $vfs->write
+                                       (
+                                       array
+                                               (
+                                               'string' => $file_path,
+                                               'relatives' => array( 
RELATIVE_NONE),
+                                               'content' => $export_data
+                                       )
+                               );
+                       }
+               }
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.10';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.10';
+
+       function rental_upgrade0_1_0_10()
+       {
+               
$GLOBALS['phpgw_setup']->oProc->RenameColumn('rental_adjustment', 'interval', 
'adjustment_interval');
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.11';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.11';
+
+       function rental_upgrade0_1_0_11()
+       {
+               // Add adjustment year column
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_adjustment', 
'year', array(
+                       'type' => 'int',
+                       'nullable' => true,
+                       'precision' => '4'
+               ));
+
+               // Update year column to match the adjustment_date of all 
existing adjustments
+               $so = CreateObject('rental.soadjustment');
+               foreach ($so->get(0, NULL, NULL, true, NULL, NULL, NULL) as 
$adjustment)
+               {
+                       $year = strftime('%Y', 
$adjustment->get_adjustment_date());
+                       $adjustment->set_year($year);
+                       $so->store($adjustment);
+               }
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.12';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.12';
+
+       function rental_upgrade0_1_0_12()
+       {
+               $asyncservice = CreateObject('phpgwapi.asyncservice');
+               $asyncservice->set_timer(
+                       array('day' => "*/1"), 'rental_sync_party_name', 
'rental.uiparty.syncronize_party_name', null
+               );
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.13';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.13';
+
+       function rental_upgrade0_1_0_13()
+       {
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_composite', 
'furnish_type_id', array(
+                       'type' => 'int',
+                       'precision' => 4,
+                       'nullable' => 'True'
+               ));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.14';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.14';
+
+       function rental_upgrade0_1_0_14()
+       {
+               // Add unit_leader column
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_party', 
'unit_leader', array(
+                       'type' => 'varchar',
+                       'nullable' => true,
+                       'precision' => '255'
+               ));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.15';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       /*
+        * function moved to so-class
+        */
+       $test[] = '0.1.0.15';
+
+       function rental_upgrade0_1_0_15()
+       {
+               $asyncservice = CreateObject('phpgwapi.asyncservice');
+               $asyncservice->delete('rental_sync_party_name');
+               $asyncservice->set_timer(
+                       array('day' => "*/1"), 'rental_sync_party_name', 
'rental.soparty.syncronize_party_name', null
+               );
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.16';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.16';
+
+       function rental_upgrade0_1_0_16()
+       {
+               $sql = "INSERT INTO rental_billing_term (title, months) VALUES 
('free_of_charge','0')";
+               $db = clone $GLOBALS['phpgw']->db;
+               $result = $db->query($sql, __LINE__, __FILE__);
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.17';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.17';
+
+       function rental_upgrade0_1_0_17()
+       {
+               $GLOBALS['phpgw']->locations->add('.admin', 'Admin section', 
'rental');
+
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_composite', 
'standard_id', array(
+                       'type' => 'int',
+                       'precision' => 4,
+                       'nullable' => 'True'
+               ));
+
+               $GLOBALS['phpgw_setup']->oProc->CreateTable(
+                       'rental_composite_standard', array(
+                       'fd' => array(
+                               'id' => array('type' => 'int', 'precision' => 
'4', 'nullable' => false),
+                               'name' => array('type' => 'varchar', 
'precision' => '255', 'nullable' => false),
+                               'factor' => array('type' => 'decimal', 
'precision' => '20', 'scale' => '2', 'nullable' => true),
+                       ),
+                       'pk' => array('id'),
+                       'fk' => array(),
+                       'ix' => array(),
+                       'uc' => array()
+                       )
+               );
+
+               $GLOBALS['phpgw_setup']->oProc->CreateTable(
+                       'rental_contract_responsibility_unit', array(
+                       'fd' => array(
+                               'id' => array('type' => 'int', 'precision' => 
'4', 'nullable' => false),
+                               'name' => array('type' => 'varchar', 
'precision' => '255', 'nullable' => false)
+                       ),
+                       'pk' => array('id'),
+                       'fk' => array(),
+                       'ix' => array(),
+                       'uc' => array()
+                       )
+               );
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.18';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.18';
+
+       function rental_upgrade0_1_0_18()
+       {
+
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_contract', 
'billing_end', array(
+                       'type' => 'int',
+                       'precision' => 8,
+                       'nullable' => 'True'
+               ));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.19';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.19';
+
+       function rental_upgrade0_1_0_19()
+       {
+               // Add unit_leader column
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_adjustment', 
'extra_adjustment', array(
+                       'type' => 'bool',
+                       'nullable' => true,
+                       'default' => 'false'
+               ));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.20';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.20';
+
+       function rental_upgrade0_1_0_20()
+       {
+               $GLOBALS['phpgw']->locations->add('.contract', 'Contract', 
'rental', $allow_grant = false, $custom_tbl = false, $c_function = true);
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.21';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+       $test[] = '0.1.0.21';
+
+       function rental_upgrade0_1_0_21()
+       {
+
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_price_item', 
'type', array(
+                       'type' => 'int',
+                       'precision' => 2,
+                       'nullable' => false,
+                       'default' => 1
+               ));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.22';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+
+       $test[] = '0.1.0.22';
+       function rental_upgrade0_1_0_22()
+       {
+               $GLOBALS['phpgw_setup']->oProc->AddColumn('rental_contract', 
'override_adjustment_start', array(
+                       'type' => 'int',
+                       'precision' => 4,
+                       'nullable' => true
+               ));
+
+               
$GLOBALS['phpgw_setup']->oProc->AddColumn('rental_invoice_price_item', 
'is_one_time', array(
+                       'type' => 'bool',
+                       'nullable' => true,
+                       'default' => 'false'
+               ));
+
+               $GLOBALS['setup_info']['rental']['currentver'] = '0.1.0.23';
+               return $GLOBALS['setup_info']['rental']['currentver'];
+       }
+
+




reply via email to

[Prev in Thread] Current Thread [Next in Thread]