From fd8d5974d4c30818a7ba88716117e6432ca39098 Mon Sep 17 00:00:00 2001 From: Kirill Solomko Date: Fri, 7 Nov 2014 00:27:01 +0100 Subject: [PATCH] MT#8645 - add billing_profile terminate functionality for API --- lib/NGCP/Panel/Form/BillingProfile/Admin.pm | 3 ++- .../Panel/Form/BillingProfile/Reseller.pm | 11 +++++++- lib/NGCP/Panel/Role/API/BillingProfiles.pm | 25 +++++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/NGCP/Panel/Form/BillingProfile/Admin.pm b/lib/NGCP/Panel/Form/BillingProfile/Admin.pm index 881f15c45f..13165f2d6b 100644 --- a/lib/NGCP/Panel/Form/BillingProfile/Admin.pm +++ b/lib/NGCP/Panel/Form/BillingProfile/Admin.pm @@ -18,7 +18,8 @@ has_block 'fields' => ( render_list => [qw/reseller handle name prepaid interval_charge interval_free_time interval_free_cash fraud_interval_limit fraud_interval_lock fraud_interval_notify fraud_daily_limit fraud_daily_lock fraud_daily_notify - currency id/], + currency id + status/], ); diff --git a/lib/NGCP/Panel/Form/BillingProfile/Reseller.pm b/lib/NGCP/Panel/Form/BillingProfile/Reseller.pm index 44916ad3d3..af5084d6f7 100644 --- a/lib/NGCP/Panel/Form/BillingProfile/Reseller.pm +++ b/lib/NGCP/Panel/Form/BillingProfile/Reseller.pm @@ -148,6 +148,14 @@ has_field 'currency' => ( }, ); +has_field 'status' => ( + type => 'Hidden', + options => [ + { label => 'active', value => 'active' }, + { label => 'terminated', value => 'terminated' }, + ], +); + has_field 'save' => ( type => 'Submit', value => 'Save', @@ -161,7 +169,8 @@ has_block 'fields' => ( render_list => [qw/handle name prepaid interval_charge interval_free_time interval_free_cash fraud_interval_limit fraud_interval_lock fraud_interval_notify fraud_daily_limit fraud_daily_lock fraud_daily_notify - currency id/], + currency id + status/], ); has_block 'actions' => ( diff --git a/lib/NGCP/Panel/Role/API/BillingProfiles.pm b/lib/NGCP/Panel/Role/API/BillingProfiles.pm index f5d952d3e5..8e6bc0e6c0 100644 --- a/lib/NGCP/Panel/Role/API/BillingProfiles.pm +++ b/lib/NGCP/Panel/Role/API/BillingProfiles.pm @@ -20,11 +20,21 @@ sub item_rs { my ($self, $c) = @_; my $item_rs = $c->model('DB')->resultset('billing_profiles'); + my $search_xtra = { + join => { 'billing_mappings' => 'contract_active' }, + '+select' => { count => 'contract_active.status', -as => 'used' }, + 'group_by' => [ qw(me.id) ] }; if($c->user->roles eq "admin") { + $item_rs = $item_rs->search({ 'me.status' => { '!=' => 'terminated' } }, + $search_xtra); } elsif($c->user->roles eq "reseller") { - $item_rs = $item_rs->search({ reseller_id => $c->user->reseller_id }); + $item_rs = $item_rs->search({ reseller_id => $c->user->reseller_id, + 'me.status' => { '!=' => 'terminated' } }, + $search_xtra); } else { - $item_rs = $item_rs->search({ reseller_id => $c->user->contract->contact->reseller_id}); + $item_rs = $item_rs->search({ reseller_id => $c->user->contract->contact->reseller_id, + 'me.status' => { '!=' => 'terminated' } }, + $search_xtra); } return $item_rs; } @@ -101,6 +111,17 @@ sub update_profile { } } + if(exists $resource->{status} && $resource->{status} eq 'terminated') { + my $profile_used = {$profile->get_inflated_columns}->{used}; + if ($profile_used) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, + "Cannnot terminate billing_profile that is used (count: $profile_used)"); + return; + } else { + $resource->{terminate_timestamp} = NGCP::Panel::Utils::DateTime::current_local; + } + } + my $old_prepaid = $profile->prepaid; $profile->update($resource);