fmsystem-commits
[Top][All Lists]
Advanced

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

[Fmsystem-commits] [15188] rental: invoice credits as separate job


From: sigurdne
Subject: [Fmsystem-commits] [15188] rental: invoice credits as separate job
Date: Mon, 23 May 2016 14:15:24 +0000 (UTC)

Revision: 15188
          http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=15188
Author:   sigurdne
Date:     2016-05-23 14:15:23 +0000 (Mon, 23 May 2016)
Log Message:
-----------
rental: invoice credits as separate job

Modified Paths:
--------------
    trunk/rental/inc/class.socontract_price_item.inc.php
    trunk/rental/inc/class.uibilling.inc.php
    trunk/rental/inc/model/class.invoice.inc.php
    trunk/rental/setup/phpgw_no.lang

Modified: trunk/rental/inc/class.socontract_price_item.inc.php
===================================================================
--- trunk/rental/inc/class.socontract_price_item.inc.php        2016-05-23 
10:12:05 UTC (rev 15187)
+++ trunk/rental/inc/class.socontract_price_item.inc.php        2016-05-23 
14:15:23 UTC (rev 15188)
@@ -49,35 +49,43 @@
                        }
                        if (isset($filters['contract_ids_one_time']))
                        {
-                               $billing_term_id = 
(int)$filters['billing_term_id'];
-                               $sql = "SELECT months FROM rental_billing_term 
WHERE id = {$billing_term_id}";
-                               $result = $this->db->query($sql);
-                               if (!$result)
+                               if($filters['credits'])
                                {
-                                       return;
+                                       $filter_clauses[] = "is_one_time";
+                                       $filter_clauses[] = 
"rental_contract_price_item.total_price < 0.00";    
                                }
-                               if (!$this->db->next_record())
-                               {
-                                       return;
-                               }
-                               $month = (int)$filters['month'];
-                               $year = (int)$filters['year'];
-                               $months = 
$this->unmarshal($this->db->f('months', true), 'int');
-                               $timestamp_end = 
strtotime("{$year}-{$month}-01"); // The first day in the month to bill for
-                               if ($months == 1)
-                               {
-                                       $timestamp_start = $timestamp_end; // 
The first day of the period to bill for
-                               }
                                else
                                {
-                                       $months = $months - 1;
-                                       $timestamp_start = 
strtotime("-{$months} months", $timestamp_end); // The first day of the period 
to bill for
+                                       $billing_term_id = 
(int)$filters['billing_term_id'];
+                                       $sql = "SELECT months FROM 
rental_billing_term WHERE id = {$billing_term_id}";
+                                       $result = $this->db->query($sql);
+                                       if (!$result)
+                                       {
+                                               return;
+                                       }
+                                       if (!$this->db->next_record())
+                                       {
+                                               return;
+                                       }
+                                       $month = (int)$filters['month'];
+                                       $year = (int)$filters['year'];
+                                       $months = 
$this->unmarshal($this->db->f('months', true), 'int');
+                                       $timestamp_end = 
strtotime("{$year}-{$month}-01"); // The first day in the month to bill for
+                                       if ($months == 1)
+                                       {
+                                               $timestamp_start = 
$timestamp_end; // The first day of the period to bill for
+                                       }
+                                       else
+                                       {
+                                               $months = $months - 1;
+                                               $timestamp_start = 
strtotime("-{$months} months", $timestamp_end); // The first day of the period 
to bill for
+                                       }
+                                       $timestamp_end = strtotime('+1 month', 
$timestamp_end); // The first day in the month after the one to bill for
+
+                                       $filter_clauses[] = "is_one_time";
+                                       $filter_clauses[] = "date_start < 
{$timestamp_end}";
+                                       $filter_clauses[] = "date_start >= 
{$timestamp_start}";
                                }
-                               $timestamp_end = strtotime('+1 month', 
$timestamp_end); // The first day in the month after the one to bill for
-
-                               $filter_clauses[] = "is_one_time";
-                               $filter_clauses[] = "date_start < 
{$timestamp_end}";
-                               $filter_clauses[] = "date_start >= 
{$timestamp_start}";
                        }
                        if (isset($filters['one_time']) && 
$filters['include_billed'])
                        {
@@ -342,6 +350,10 @@
                public function get_total_price_invoice( $contract_id, 
$billing_term, $month, $year )
                {
                        $billing_term_id = (int)$billing_term;
+                       if($billing_term == 5 )
+                       {
+                               return 
$this->get_total_price_invoice_credit($contract_id);
+                       }
                        $sql = "SELECT months FROM rental_billing_term WHERE id 
= {$billing_term_id}";
                        $result = $this->db->query($sql);
                        if (!$result)
@@ -378,6 +390,20 @@
                                return $total_price;
                        }
                }
+               public function get_total_price_invoice_credit( int 
$contract_id )
+               {
+                       $q_total_price = "SELECT sum(total_price::numeric) AS 
sum_total ";
+                       $q_total_price .= "FROM rental_contract_price_item ";
+                       $q_total_price .= "WHERE contract_id={$contract_id} ";
+                       $q_total_price .= "AND NOT is_billed ";
+                       $q_total_price .= "AND total_price < 0.00 ";
+                       $this->db->query($q_total_price);
+                       if ($this->db->next_record())
+                       {
+                               $total_price = $this->db->f('sum_total');
+                               return $total_price;
+                       }
+               }
 
                /**
                 * Select max area of all "active" price-items on a contract.

Modified: trunk/rental/inc/class.uibilling.inc.php
===================================================================
--- trunk/rental/inc/class.uibilling.inc.php    2016-05-23 10:12:05 UTC (rev 
15187)
+++ trunk/rental/inc/class.uibilling.inc.php    2016-05-23 14:15:23 UTC (rev 
15188)
@@ -217,6 +217,19 @@
                                                                
$billing_term_label = lang('second_half');
                                                        }
                                                }
+                                               else if ($billing_term == '5')
+                                               { // half year
+                                                       if ($billing_month == 
'1')
+                                                       {
+                                                               $month = 1;
+                                                               
$billing_term_label = lang('free_of_charge');
+                                                       }
+                                                       else
+                                                       {
+                                                               $month = 2;
+                                                               
$billing_term_label = lang('credits');
+                                                       }
+                                               }
                                                else // yearly
                                                {
                                                        $month = 12;
@@ -473,6 +486,19 @@
                                                $billing_term_label = 
lang('second_half');
                                        }
                                }
+                               else if ($billing_term == '5')
+                               { // half year
+                                       if ($billing_month == '1')
+                                       {
+                                               $month = 1;
+                                               $billing_term_label = 
lang('free_of_charge');
+                                       }
+                                       else
+                                       {
+                                               $month = 2;
+                                               $billing_term_label = 
lang('credits');
+                                       }
+                               }
                                else // yearly
                                {
                                        $month = 12;
@@ -524,14 +550,22 @@
 
                                        $socontract_price_item = 
rental_socontract_price_item::get_instance();
 
-                                       //... 1. Contracts following regular 
billing cycle
-                                       $filters = 
array('contracts_for_billing' => true, 'contract_type' => $contract_type,
-                                               'billing_term_id' => 
$billing_term, 'year' => $year, 'month' => $month);
-                                       $contracts = 
rental_socontract::get_instance()->get($start_index, $num_of_objects, 
$sort_field, $sort_ascending, $search_for, $search_type, $filters);
 
                                        //... 2. Contracts with one-time price 
items
-                                       $filters2 = 
array('contract_ids_one_time' => true, 'billing_term_id' => $billing_term,
-                                               'year' => $year, 'month' => 
$month);
+                                       if($billing_term == 5)
+                                       {
+                                               $filters2 = 
array('contract_ids_one_time' => true, 'credits' => true);
+                                               $contracts = array();
+                                       }
+                                       else
+                                       {
+                                               //... 1. Contracts following 
regular billing cycle
+                                               $filters = 
array('contracts_for_billing' => true, 'contract_type' => $contract_type,
+                                                       'billing_term_id' => 
$billing_term, 'year' => $year, 'month' => $month);
+                                               $contracts = 
rental_socontract::get_instance()->get($start_index, $num_of_objects, 
$sort_field, $sort_ascending, $search_for, $search_type, $filters);
+                                               $filters2 = 
array('contract_ids_one_time' => true, 'billing_term_id' => $billing_term,
+                                                       'year' => $year, 
'month' => $month);
+                                       }
                                        $contract_price_items = 
$socontract_price_item->get($start_index, $num_of_objects, $sort_field, 
$sort_ascending, $search_for, $search_type, $filters2);
 
                                        foreach ($contract_price_items as 
$contract_price_item)
@@ -548,6 +582,10 @@
                                                                
//$contracts[$contract_price_item->get_contract_id()] = $c;
                                                                
//$contracts_with_one_time[$contract_price_item->get_contract_id()] = $c; // 
used for information purposes
                                                        }
+                                                       else
+                                                       {
+                                                               continue;
+                                                       }
                                                }
                                                else
                                                {
@@ -886,6 +924,7 @@
                                        else
                                        {
                                                $options[] = array('id' => 
$term_id . '-1', 'name' => lang($term_title), 'selected' => (($term_id . '-1' 
== $billing_term_selection) ? 1 : 0));
+                                               $options[] = array('id' => 
$term_id . '-2', 'name' => 'Kreditering', 'selected' => (($term_id . '-2' == 
$billing_term_selection) ? 1 : 0));
                                        }
                                        $current++;
                                        $billing_term_group_options[] = 
array('label' => lang($term_title), 'options' => $options);

Modified: trunk/rental/inc/model/class.invoice.inc.php
===================================================================
--- trunk/rental/inc/model/class.invoice.inc.php        2016-05-23 10:12:05 UTC 
(rev 15187)
+++ trunk/rental/inc/model/class.invoice.inc.php        2016-05-23 14:15:23 UTC 
(rev 15188)
@@ -358,18 +358,18 @@
                        // 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)
+                               $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
@@ -385,11 +385,22 @@
                        // Retrieve the contract price items: only one-time or 
all
                        if ($bill_only_one_time)
                        {
-                               $filters2 = array('contract_id' => 
$contract->get_id(),
-                                       'contract_ids_one_time' => true,
-                                       'billing_term_id' => $billing_term,
-                                       'year' => date('Y', 
$timestamp_invoice_start),
-                                       'month' => date('m', 
$timestamp_invoice_start));
+                               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);
@@ -441,7 +452,7 @@
                                {
                                        $invoice_price_item_start = 
$timestamp_invoice_start; // We use the invoice start
                                }
-                               else if ($contract_price_item_start > 
$timestamp_invoice_end) // Start of price item after this invoice ends
+                               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
                                }
@@ -451,7 +462,7 @@
                                }
 
                                // Checking the end date against invoice dates
-                               if ($contract_price_item_end < 
$timestamp_invoice_start) // End of price item before this invoice starts
+                               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
                                }
@@ -495,22 +506,22 @@
                                // 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
+                                        -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())
                                {
-                                       if ($contract_price_item_start >= 
$timestamp_invoice_start && $contract_price_item_start <= 
$timestamp_invoice_end)
+                                       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());
@@ -532,6 +543,12 @@
                                // 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

Modified: trunk/rental/setup/phpgw_no.lang
===================================================================
--- trunk/rental/setup/phpgw_no.lang    2016-05-23 10:12:05 UTC (rev 15187)
+++ trunk/rental/setup/phpgw_no.lang    2016-05-23 14:15:23 UTC (rev 15188)
@@ -617,4 +617,5 @@
 download excel export bk       rental  no      Last ned Excel-eksport BK
 serial_number  rental  no      Serienummer
 Name or company is required    rental  no      Navn på person ELLER navn på 
foretak er obligatorisk
-simulation     rental  no      Simulering
\ No newline at end of file
+simulation     rental  no      Simulering
+credits        rental  no      Kreditering
\ No newline at end of file




reply via email to

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