From ad9a8bb4dd9e5101bdcb959c4935b8076c487217 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Tue, 27 Aug 2013 11:51:08 +0200 Subject: [PATCH] Use localtime in TS and set createtime for subs. Consequently use server's localtime to align with auto-filled timestamps. Set create_timestamp when creating subscribers. --- lib/NGCP/Panel/Controller/Contract.pm | 17 ++++++++------- lib/NGCP/Panel/Controller/Customer.pm | 8 ++++--- lib/NGCP/Panel/Controller/Reseller.pm | 2 +- lib/NGCP/Panel/Controller/Security.pm | 6 ++---- lib/NGCP/Panel/Controller/Subscriber.pm | 2 ++ lib/NGCP/Panel/Utils/Contract.pm | 4 ++-- lib/NGCP/Panel/Utils/DateTime.pm | 21 +++++++++++++++++++ .../Widget/Plugin/AdminBillingOverview.pm | 2 +- .../Widget/Plugin/ResellerBillingOverview.pm | 2 +- 9 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 lib/NGCP/Panel/Utils/DateTime.pm diff --git a/lib/NGCP/Panel/Controller/Contract.pm b/lib/NGCP/Panel/Controller/Contract.pm index aef8e7d42e..0c7b82f9fd 100644 --- a/lib/NGCP/Panel/Controller/Contract.pm +++ b/lib/NGCP/Panel/Controller/Contract.pm @@ -7,6 +7,7 @@ use NGCP::Panel::Utils::Message; use NGCP::Panel::Utils::Navigation; use NGCP::Panel::Utils::Contract; use NGCP::Panel::Utils::Subscriber; +use NGCP::Panel::Utils::DateTime; sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) :AllowedRole(reseller) { my ($self, $c) = @_; @@ -35,11 +36,11 @@ sub contract_list :Chained('/') :PathPart('contract') :CaptureArgs(0) { '=' => $mapping_rs->search({ contract_id => { -ident => 'me.id' }, start_date => [ -or => - { '<=' => DateTime->now }, + { '<=' => NGCP::Panel::Utils::DateTime::current_local }, { -is => undef }, ], end_date => [ -or => - { '>=' => DateTime->now }, + { '>=' => NGCP::Panel::Utils::DateTime::current_local }, { -is => undef }, ], },{ @@ -107,7 +108,7 @@ sub create :Chained('contract_list') :PathPart('create') :Args(0) { delete $form->params->{contract}; my $bprof_id = $form->params->{billing_profile}{id}; delete $form->params->{billing_profile}; - $form->{create_timestamp} = $form->{modify_timestamp} = DateTime->now; + $form->{create_timestamp} = $form->{modify_timestamp} = NGCP::Panel::Utils::DateTime::current_local; my $contract = $schema->resultset('contracts')->create($form->params); my $billing_profile = $schema->resultset('billing_profiles')->find($bprof_id); $contract->billing_mappings->create({ @@ -202,7 +203,7 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { $schema->txn_do(sub { if($form->values->{billing_profile}{id} != $billing_mapping->billing_profile->id) { $contract->billing_mappings->create({ - start_date => DateTime->now(), + start_date => NGCP::Panel::Utils::DateTime::current_local, billing_profile_id => $form->values->{billing_profile}{id}, }); } @@ -210,7 +211,7 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { delete $form->values->{billing_profile}; $form->values->{contact_id} = $form->values->{contact}{id}; delete $form->values->{contact}; - $form->{modify_timestamp} = DateTime->now; + $form->{modify_timestamp} = NGCP::Panel::Utils::DateTime::current_local; $contract->update($form->values); # if status changed, populate it down the chain @@ -329,7 +330,7 @@ sub peering_create :Chained('peering_list') :PathPart('create') :Args(0) { delete $form->params->{contract}; my $bprof_id = $form->params->{billing_profile}{id}; delete $form->params->{billing_profile}; - $form->{create_timestamp} = $form->{modify_timestamp} = DateTime->now; + $form->{create_timestamp} = $form->{modify_timestamp} = NGCP::Panel::Utils::DateTime::current_local; my $contract = $schema->resultset('contracts')->create($form->params); my $billing_profile = $schema->resultset('billing_profiles')->find($bprof_id); my $product = $schema->resultset('products')->find({ class => 'sippeering' }); @@ -414,7 +415,7 @@ sub customer_create :Chained('customer_list') :PathPart('create') :Args(0) { delete $form->params->{contract}; my $bprof_id = $form->params->{billing_profile}{id}; delete $form->params->{billing_profile}; - $form->{create_timestamp} = $form->{modify_timestamp} = DateTime->now; + $form->{create_timestamp} = $form->{modify_timestamp} = NGCP::Panel::Utils::DateTime::current_local; my $contract = $schema->resultset('contracts')->create($form->params); my $billing_profile = $schema->resultset('billing_profiles')->find($bprof_id); $contract->billing_mappings->create({ @@ -497,7 +498,7 @@ sub reseller_create :Chained('reseller_list') :PathPart('create') :Args(0) { delete $form->params->{contract}; my $bprof_id = $form->params->{billing_profile}{id}; delete $form->params->{billing_profile}; - $form->{create_timestamp} = $form->{modify_timestamp} = DateTime->now; + $form->{create_timestamp} = $form->{modify_timestamp} = NGCP::Panel::Utils::DateTime::current_local; my $contract = $schema->resultset('contracts')->create($form->params); my $billing_profile = $schema->resultset('billing_profiles')->find($bprof_id); my $product = $schema->resultset('products')->find({ class => 'reseller' }); diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index 9a0d6f350e..84006bfb95 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -9,6 +9,7 @@ use NGCP::Panel::Form::CustomerBalance; use NGCP::Panel::Form::CustomerSubscriber; use NGCP::Panel::Utils::Message; use NGCP::Panel::Utils::Navigation; +use NGCP::Panel::Utils::DateTime; use UUID qw/generate unparse/; =head1 NAME @@ -70,7 +71,7 @@ sub base :Chained('list_customer') :PathPart('') :CaptureArgs(1) { }); } - my $stime = DateTime->now->truncate(to => 'month'); + my $stime = NGCP::Panel::Utils::DateTime::current_local()->truncate(to => 'month'); my $etime = $stime->clone->add(months => 1); my $balance = $contract->first->contract_balances ->find({ @@ -85,11 +86,11 @@ sub base :Chained('list_customer') :PathPart('') :CaptureArgs(1) { -and => [ -or => [ start_date => undef, - start_date => { '<=' => DateTime->now }, + start_date => { '<=' => NGCP::Panel::Utils::DateTime::current_local }, ], -or => [ end_date => undef, - end_date => { '>=' => DateTime->now }, + end_date => { '>=' => NGCP::Panel::Utils::DateTime::current_local }, ] ], }, @@ -208,6 +209,7 @@ sub subscriber_create :Chained('base') :PathPart('subscriber/create') :Args(0) { admin => $c->request->params->{administrative} || 0, account_id => $contract->id, domain_id => $prov_domain->id, + create_timestamp => NGCP::Panel::Utils::DateTime::current_local, }); my $voip_preferences = $schema->resultset('voip_preferences')->search({ diff --git a/lib/NGCP/Panel/Controller/Reseller.pm b/lib/NGCP/Panel/Controller/Reseller.pm index bd571ca2ba..c34c6a3816 100644 --- a/lib/NGCP/Panel/Controller/Reseller.pm +++ b/lib/NGCP/Panel/Controller/Reseller.pm @@ -340,7 +340,7 @@ sub create_defaults :Path('create_defaults') :Args(0) { $c->detach('/denied_page') unless $c->request->method eq 'POST'; $c->detach('/denied_page') if($c->user->read_only); - my $now = DateTime->now; + my $now = NGCP::Panel::Utils::DateTime::current_local; my %defaults = ( contacts => { firstname => 'Default', diff --git a/lib/NGCP/Panel/Controller/Security.pm b/lib/NGCP/Panel/Controller/Security.pm index 858024e93f..b151349f69 100644 --- a/lib/NGCP/Panel/Controller/Security.pm +++ b/lib/NGCP/Panel/Controller/Security.pm @@ -7,6 +7,7 @@ use XML::Mini::Document; use URI::Encode; use NGCP::Panel::Utils::Navigation; use NGCP::Panel::Utils::XMLDispatcher; +use NGCP::Panel::Utils::DateTime; sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) { my ($self, $c) = @_; @@ -105,10 +106,7 @@ EOF push @users, { username => $key, auth_count => $usr->{$key}->{auth_count}, - last_auth => DateTime->from_epoch( - epoch => $usr->{$key}->{last_auth}, - time_zone => DateTime::TimeZone->new(name => 'local') - ), + last_auth => NGCP::Panel::Utils::DateTime::epoch_local($usr->{$key}->{last_auth}), } if($usr->{$key}->{auth_count} >= $c->config->{security}->{failed_auth_attempts}); } } diff --git a/lib/NGCP/Panel/Controller/Subscriber.pm b/lib/NGCP/Panel/Controller/Subscriber.pm index 39f6546a87..5f0b28cf7e 100644 --- a/lib/NGCP/Panel/Controller/Subscriber.pm +++ b/lib/NGCP/Panel/Controller/Subscriber.pm @@ -9,6 +9,7 @@ use NGCP::Panel::Utils::Datatables; use NGCP::Panel::Utils::Callflow; use NGCP::Panel::Utils::Preferences; use NGCP::Panel::Utils::Message; +use NGCP::Panel::Utils::DateTime; use NGCP::Panel::Form::Subscriber; use NGCP::Panel::Form::SubscriberEdit; use NGCP::Panel::Form::SubscriberCFSimple; @@ -163,6 +164,7 @@ sub create_list :Chained('sub_list') :PathPart('create') :Args(0) { admin => $c->request->params->{administrative} || 0, account_id => $contract->id, domain_id => $prov_domain->id, + create_timestamp => NGCP::Panel::Utils::DateTime::current_local, }); if($number) { $schema->resultset('dbaliases')->create({ diff --git a/lib/NGCP/Panel/Utils/Contract.pm b/lib/NGCP/Panel/Utils/Contract.pm index a72ebcc85b..7e5c482df3 100644 --- a/lib/NGCP/Panel/Utils/Contract.pm +++ b/lib/NGCP/Panel/Utils/Contract.pm @@ -17,7 +17,7 @@ sub create_contract_balance { # first, calculate start and end time of current billing profile # (we assume billing interval of 1 month) - my $stime = DateTime->now->truncate(to => 'month'); + my $stime = NGCP::Panel::Utils::DateTime::current_local->truncate(to => 'month'); my $etime = $stime->clone->add(months => 1)->subtract(seconds => 1); # calculate free_time/cash ratio @@ -25,7 +25,7 @@ sub create_contract_balance { my $free_cash = $profile->interval_free_cash || 0; if($free_time or $free_cash) { $etime->add(seconds => 1); - my $ctime = DateTime->now->truncate(to => 'day'); + my $ctime = NGCP::Panel::Utils::DateTime::current_local->truncate(to => 'day'); my $ratio = ($etime->epoch - $ctime->epoch) / ($etime->epoch - $stime->epoch); $cash_balance = sprintf("%.4f", $free_cash * $ratio); diff --git a/lib/NGCP/Panel/Utils/DateTime.pm b/lib/NGCP/Panel/Utils/DateTime.pm new file mode 100644 index 0000000000..dd640e33b5 --- /dev/null +++ b/lib/NGCP/Panel/Utils/DateTime.pm @@ -0,0 +1,21 @@ +package NGCP::Panel::Utils::DateTime; + +use Sipwise::Base; + +sub current_local { + return DateTime->now( + time_zone => DateTime::TimeZone->new(name => 'local') + ); +} + +sub epoch_local { + my $epoch = shift; + return DateTime->from_epoch( + time_zone => DateTime::TimeZone->new(name => 'local'), + epoch => $epoch, + ); +} + +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Widget/Plugin/AdminBillingOverview.pm b/lib/NGCP/Panel/Widget/Plugin/AdminBillingOverview.pm index 3d79f0cff2..ca65231aa5 100644 --- a/lib/NGCP/Panel/Widget/Plugin/AdminBillingOverview.pm +++ b/lib/NGCP/Panel/Widget/Plugin/AdminBillingOverview.pm @@ -21,7 +21,7 @@ has 'priority' => ( around handle => sub { my ($foo, $self, $c) = @_; - my $stime = DateTime->now->truncate(to => 'month'); + my $stime = NGCP::Panel::Utils::DateTime::current_local->truncate(to => 'month'); my $etime = $stime->clone->add(months => 1); $c->stash( diff --git a/lib/NGCP/Panel/Widget/Plugin/ResellerBillingOverview.pm b/lib/NGCP/Panel/Widget/Plugin/ResellerBillingOverview.pm index e2bb5a849e..8283f6d4fc 100644 --- a/lib/NGCP/Panel/Widget/Plugin/ResellerBillingOverview.pm +++ b/lib/NGCP/Panel/Widget/Plugin/ResellerBillingOverview.pm @@ -21,7 +21,7 @@ has 'priority' => ( around handle => sub { my ($foo, $self, $c) = @_; - my $stime = DateTime->now->truncate(to => 'month'); + my $stime = NGCP::Panel::Utils::DateTime::current_local->truncate(to => 'month'); my $etime = $stime->clone->add(months => 1); my $reseller = $c->model('DB')->resultset('resellers')->find($c->user->reseller_id);