From 7a29024cfe36cdc49685c43ae5a52cd34a2273c5 Mon Sep 17 00:00:00 2001 From: Lars Dieckow Date: Wed, 26 Jun 2013 16:35:59 +0200 Subject: [PATCH] DateTime API --- lib/NGCP/Panel/Controller/Contract.pm | 31 +++++++-------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Contract.pm b/lib/NGCP/Panel/Controller/Contract.pm index 2eaadb85f7..24107c040b 100644 --- a/lib/NGCP/Panel/Controller/Contract.pm +++ b/lib/NGCP/Panel/Controller/Contract.pm @@ -71,38 +71,23 @@ sub create :Chained('contract_list') :PathPart('create') :Args(0) { # first, calculate start and end time of current billing profile # (we assume billing interval of 1 month) - my ($cyear, $cmonth, $cday) = (localtime time)[5,4,3]; - $cyear += 1900; $cmonth += 1; - my $stime = DateTime->new(year => $cyear, month => $cmonth, day => 1, - hour => 0, minute => 0, second => 0, nanosecond => 0) - ->epoch(); - my $eyear = $cyear; - my $emonth = $cmonth + 1; # next month - if($emonth == 12) { - $emonth = 0; - $eyear++; - } - my $etime = DateTime->new(year => $eyear, month => $emonth, day => 1, - hour => 0, minute => 0, second => 0, nanosecond => 0) - ->epoch(); - $etime--; + my $stime = DateTime->now->truncate(to => 'month'); + my $etime = $stime->clone->add(months => 1)->subtract(seconds => 1); # calculate free_time/cash ratio my $free_time = $profile->interval_free_time || 0; my $free_cash = $profile->interval_free_cash || 0; if($free_time or $free_cash) { - $etime++; - my $ctime = DateTime->new(year => $cyear, month => $cmonth, day => $cday, - hour => 0, minute => 0, second => 0, nanosecond => 0) - ->epoch(); - my $ratio = ($etime - $ctime) / ($etime - $stime); + $etime->add(seconds => 1); + my $ctime = DateTime->now->truncate(to => 'day'); + my $ratio = ($etime->epoch - $ctime->epoch) / ($etime->epoch - $stime->epoch); $cash_balance = sprintf("%.4f", $free_cash * $ratio); $cash_balance_interval = 0; $free_time_balance = sprintf("%.0f", $free_time * $ratio); $free_time_balance_interval = 0; - $etime--; + $etime->subtract(seconds => 1); } $c->model('billing')->resultset('contract_balances')->create({ @@ -111,8 +96,8 @@ sub create :Chained('contract_list') :PathPart('create') :Args(0) { cash_balance_interval => $cash_balance_interval, free_time_balance => $free_time_balance, free_time_balance_interval => $free_time_balance_interval, - start => DateTime->from_epoch(epoch => $stime), - end => DateTime->from_epoch(epoch => $etime), + start => $stime, + end => $etime, }); # TODO: catch insert error and roll back billing_mapping and contracts entry