From f31c2fcc88b82c74bef24da8fd06add22cc2cc68 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Thu, 16 Jan 2014 11:15:22 +0100 Subject: [PATCH] MT#5661 Update prepaid pref on bilprof change If prepaid flag is changed in billing profile, all subscribers of all contracts currently having this profile are updated. If customer changes from prepaid profile to postpaid and vice versa, all subscribers of this contract are updated. If subscriber is created either in subscriber or customer view, the prepaid pref is set if billing profile of its customer is prepaid. --- lib/NGCP/Panel/Controller/Billing.pm | 55 ++++++++++++++++++++++++- lib/NGCP/Panel/Controller/Customer.pm | 35 ++++++++++++++++ lib/NGCP/Panel/Controller/Subscriber.pm | 15 +++++++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/lib/NGCP/Panel/Controller/Billing.pm b/lib/NGCP/Panel/Controller/Billing.pm index ba6f6adafe..a7ab4b866f 100644 --- a/lib/NGCP/Panel/Controller/Billing.pm +++ b/lib/NGCP/Panel/Controller/Billing.pm @@ -16,6 +16,8 @@ use NGCP::Panel::Form::BillingFeeUpload; use NGCP::Panel::Utils::Message; use NGCP::Panel::Utils::Navigation; use NGCP::Panel::Utils::Datatables; +use NGCP::Panel::Utils::Preferences; +use NGCP::Panel::Utils::DateTime; my @WEEKDAYS = map { langinfo($_) } (DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7, DAY_1); #Monday Tuesday Wednesday Thursday Friday Saturday Sunday @@ -127,7 +129,58 @@ sub edit :Chained('base') :PathPart('edit') { $form->values->{reseller_id} = $c->user->reseller_id; } delete $form->values->{reseller}; - $c->stash->{profile_result}->update($form->values); + my $old_prepaid = $c->stash->{profile_result}->prepaid; + + my $schema = $c->model('DB'); + $schema->txn_do(sub { + $c->stash->{profile_result}->update($form->values); + + # if prepaid flag changed, update all subscribers for customers + # who currently have the billing profile active + my $rs = $schema->resultset('billing_mappings')->search({ + start_date => [ -or => + { '<=' => NGCP::Panel::Utils::DateTime::current_local }, + { -is => undef }, + ], + end_date => [ -or => + { '>=' => NGCP::Panel::Utils::DateTime::current_local }, + { -is => undef }, + ], + billing_profile_id => $c->stash->{profile_result}->id, + }); + if($old_prepaid && !$c->stash->{profile_result}->prepaid) { + foreach my $map($rs->all) { + my $contract = $map->contract; + next unless($contract->contact->reseller_id); # skip non-customers + foreach my $sub($contract->voip_subscribers->all) { + my $prov_sub = $sub->provisioning_voip_subscriber; + next unless($sub->provisioning_voip_subscriber); + my $pref = NGCP::Panel::Utils::Preferences::get_usr_preference_rs( + c => $c, attribute => 'prepaid', prov_subscriber => $prov_sub); + if($pref->first) { + $pref->first->delete; + } + } + } + } elsif(!$old_prepaid && $c->stash->{profile_result}->prepaid) { + foreach my $map($rs->all) { + my $contract = $map->contract; + next unless($contract->contact->reseller_id); # skip non-customers + foreach my $sub($contract->voip_subscribers->all) { + my $prov_sub = $sub->provisioning_voip_subscriber; + next unless($prov_sub); + my $pref = NGCP::Panel::Utils::Preferences::get_usr_preference_rs( + c => $c, attribute => 'prepaid', prov_subscriber => $prov_sub); + if($pref->first) { + $pref->first->update({ value => 1 }); + } else { + $pref->create({ value => 1 }); + } + } + } + } + + }); delete $c->session->{created_objects}->{reseller}; $c->flash(messages => [{type => 'success', text => 'Billing profile successfully updated'}]); diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index 14f046e23e..659add7545 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -358,6 +358,7 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { } my $old_bprof_id = $billing_mapping->billing_profile_id; $c->log->debug(">>>>>>>>>>> old bprof_id=$old_bprof_id"); + my $old_prepaid = $billing_mapping->billing_profile->prepaid; $contract->update($form->params); if($bprof_id != $old_bprof_id) { $contract->billing_mappings->create({ @@ -365,6 +366,30 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { product_id => $product_id, start_date => NGCP::Panel::Utils::DateTime::current_local, }); + my $new_billing_profile = $c->model('DB')->resultset('billing_profiles')->find($bprof_id); + if($old_prepaid && !$new_billing_profile->prepaid) { + foreach my $sub($contract->voip_subscribers->all) { + my $prov_sub = $sub->provisioning_voip_subscriber; + next unless($prov_sub); + my $pref = NGCP::Panel::Utils::Preferences::get_usr_preference_rs( + c => $c, attribute => 'prepaid', prov_subscriber => $prov_sub); + if($pref->first) { + $pref->first->delete; + } + } + } elsif(!$old_prepaid && $new_billing_profile->prepaid) { + foreach my $sub($contract->voip_subscribers->all) { + my $prov_sub = $sub->provisioning_voip_subscriber; + next unless($prov_sub); + my $pref = NGCP::Panel::Utils::Preferences::get_usr_preference_rs( + c => $c, attribute => 'prepaid', prov_subscriber => $prov_sub); + if($pref->first) { + $pref->first->update({ value => 1 }); + } else { + $pref->create({ value => 1 }); + } + } + } } unless ( defined $schema->resultset('billing_profiles') @@ -549,6 +574,16 @@ sub subscriber_create :Chained('base') :PathPart('subscriber/create') :Args(0) { preferences => $preferences, ); + if($c->stash->{billing_mapping}->billing_profile->prepaid) { + my $pref = NGCP::Panel::Utils::Preferences::get_usr_preference_rs( + c => $c, attribute => 'prepaid', prov_subscriber => $billing_subscriber->provisioning_voip_subscriber); + if($pref->first) { + $pref->first->update({ 'value' => 1 }); + } else { + $pref->create({ 'value' => 1 }); + } + } + NGCP::Panel::Utils::Subscriber::update_pbx_group_prefs( c => $c, schema => $schema, diff --git a/lib/NGCP/Panel/Controller/Subscriber.pm b/lib/NGCP/Panel/Controller/Subscriber.pm index 8dea718170..ec8971eb4b 100644 --- a/lib/NGCP/Panel/Controller/Subscriber.pm +++ b/lib/NGCP/Panel/Controller/Subscriber.pm @@ -189,6 +189,21 @@ sub create_list :Chained('sub_list') :PathPart('create') :Args(0) :Does(ACL) :AC my $voip_preferences = $schema->resultset('voip_preferences')->search({ 'usr_pref' => 1, }); + + my $rs = NGCP::Panel::Utils::Contract::get_contract_rs( + schema => $c->model('DB')); + my $billing_contract = $rs->find($contract->id); + use Data::Printer; p $billing_contract; + my $billing_mapping = $c->model('DB')->resultset('billing_mappings')->find( + $billing_contract->get_column('billing_mapping_id')); + + if($billing_mapping->billing_profile->prepaid) { + $voip_preferences->find({ 'attribute' => 'prepaid' }) + ->voip_usr_preferences->create({ + 'subscriber_id' => $prov_subscriber->id, + 'value' => 1, + }); + } $voip_preferences->find({ 'attribute' => 'account_id' }) ->voip_usr_preferences->create({ 'subscriber_id' => $prov_subscriber->id,