fmsystem-commits
[Top][All Lists]
Advanced

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

[Fmsystem-commits] [15578] Rental: add support for price per day


From: sigurdne
Subject: [Fmsystem-commits] [15578] Rental: add support for price per day
Date: Sun, 4 Sep 2016 17:07:10 +0000 (UTC)

Revision: 15578
          http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=15578
Author:   sigurdne
Date:     2016-09-04 17:07:10 +0000 (Sun, 04 Sep 2016)
Log Message:
-----------
Rental: add support for price per day

Modified Paths:
--------------
    trunk/rental/inc/model/class.invoice_price_item.inc.php
    trunk/rental/inc/model/class.price_item.inc.php

Modified: trunk/rental/inc/model/class.invoice_price_item.inc.php
===================================================================
--- trunk/rental/inc/model/class.invoice_price_item.inc.php     2016-09-04 
16:24:35 UTC (rev 15577)
+++ trunk/rental/inc/model/class.invoice_price_item.inc.php     2016-09-04 
17:07:10 UTC (rev 15578)
@@ -136,68 +136,71 @@
                                $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--)
+                               if($this->price_type_id != 
rental_price_item::PRICE_TYPE_DAY)
                                {
-                                       // 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'])
+                                       for ($current_year = $date_end['year']; 
$current_year >= $date_start['year']; $current_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'])
+                                               // 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'])
                                                {
-                                                       $first_day = 
$date_start['day'];
+                                                       $current_start_month = 
$date_start['month'];
                                                }
 
-                                               // 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'])
+                                               // If we are at the start year, 
use the end month of this year as end month
+                                               if ($current_year == 
$date_end['year'])
                                                {
-                                                       $last_day = 
$date_end['day']; // The end date's day is the item's end day
+                                                       $current_end_month = 
$date_end['month'];
                                                }
 
-                                               // 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
+                                               // 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--)
                                                {
-                                                       // YYY: There must be a 
better day to do this!?
-                                                       if($this->price_type_id 
== 1) // year
+                                                       // 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'])
                                                        {
-                                                               
$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);
+                                                               $first_day = 
$date_start['day'];
                                                        }
-                                                       else 
if($this->price_type_id == 2) //month
+
+                                                       // 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'])
                                                        {
-                                                               $num_of_days = 
$last_day - $first_day + 1;
-                                                               
$incomplete_months[] = array($num_of_days_in_current_month, $num_of_days);
+                                                               $last_day = 
$date_end['day']; // The end date's day is the item's end day
                                                        }
-                                                       else
+
+                                                       // 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
                                                        {
-                                                               throw new 
Exception('Price type not supported');
+                                                               // YYY: There 
must be a better day to do this!?
+                                                               
if($this->price_type_id == rental_price_item::PRICE_TYPE_YEAR)
+                                                               {
+                                                                       
$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);
+                                                               }
+                                                               else 
if($this->price_type_id == rental_price_item::PRICE_TYPE_MONTH)
+                                                               {
+                                                                       
$num_of_days = $last_day - $first_day + 1;
+                                                                       
$incomplete_months[] = array($num_of_days_in_current_month, $num_of_days);
+                                                               }
+                                                               else
+                                                               {
+                                                                       throw 
new Exception('Price type not supported');
+                                                               }
                                                        }
                                                }
                                        }
@@ -208,14 +211,23 @@
 
                                // The total price of this price element for 
complete months
                                
-                               if($this->price_type_id == 1) // year
+                               if($this->price_type_id == 
rental_price_item::PRICE_TYPE_YEAR)
                                {
                                        $this->total_price = 
(($this->get_price() * $num_of_complete_months) / 12.0) * $amount;
                                }
-                               else if($this->price_type_id == 2) //month
+                               else if($this->price_type_id == 
rental_price_item::PRICE_TYPE_MONTH)
                                {
                                        $this->total_price = 
($this->get_price() * $num_of_complete_months) * $amount;
                                }
+                               else if($this->price_type_id == 
rental_price_item::PRICE_TYPE_DAY)
+                               {
+                                       $date1 = new DateTime(date('Y-m-d', 
$this->get_timestamp_start()));
+                                       $date2 = new DateTime(date('Y-m-d', 
$this->get_timestamp_end()));
+                                       $total_num_of_days = 
$date2->diff($date1)->format("%a");
+                                       $this->total_price = 
($this->get_price() * $total_num_of_days) * $amount;
+                                       //block the year and month correction
+                                       $incomplete_months = array();
+                               }
                                else
                                {
                                        throw new Exception('Price type not 
supported');
@@ -225,7 +237,7 @@
 
                                $price_per_type = $this->get_price() * $amount;
 
-                               // Run through all the incomplete months ...
+                               // Run through all the incomplete months 
...blocked if rental_price_item::PRICE_TYPE_DAY
                                foreach ($incomplete_months as $day_factors)
                                {
                                        // ... and add the sum of each 
incomplete month to the total price of the price item

Modified: trunk/rental/inc/model/class.price_item.inc.php
===================================================================
--- trunk/rental/inc/model/class.price_item.inc.php     2016-09-04 16:24:35 UTC 
(rev 15577)
+++ trunk/rental/inc/model/class.price_item.inc.php     2016-09-04 17:07:10 UTC 
(rev 15578)
@@ -8,6 +8,11 @@
        class rental_price_item extends rental_model
        {
 
+               const PRICE_TYPE_YEAR = 1;
+               const PRICE_TYPE_MONTH = 2;
+               const PRICE_TYPE_DAY = 3;
+               const PRICE_TYPE_HOUR = 4;
+
                public static $so;
                protected $id;
                protected $title;
@@ -24,7 +29,7 @@
                protected $price_types = array(
                        1 => 'year',
                        2 => 'month',
-//                     3 => 'day',
+                       3 => 'day',
 //                     4 => 'hour',
                );
 




reply via email to

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