diff --git a/lib/NGCP/Panel/Role/API/BillingProfiles.pm b/lib/NGCP/Panel/Role/API/BillingProfiles.pm index 4d7b9d2ddc..1866751a7a 100644 --- a/lib/NGCP/Panel/Role/API/BillingProfiles.pm +++ b/lib/NGCP/Panel/Role/API/BillingProfiles.pm @@ -9,6 +9,7 @@ use Data::HAL::Link qw(); use HTTP::Status qw(:constants); use NGCP::Panel::Utils::DateTime; use NGCP::Panel::Utils::Contract; +use NGCP::Panel::Utils::Preferences; use NGCP::Panel::Form::BillingProfile::Admin qw(); sub hal_from_profile { @@ -89,8 +90,52 @@ sub update_profile { } } + my $old_prepaid = $profile->prepaid; $profile->update($resource); + # if prepaid flag changed, update all subscribers for customers + # who currently have the billing profile active + my $rs = $c->model('DB')->resultset('billing_mappings')->search({ + billing_profile_id => $profile->id, + }); + my $contract_rs = NGCP::Panel::Utils::Contract::get_contract_rs( +schema => $c->model('DB')); + if($old_prepaid && !$profile->prepaid) { + foreach my $map($rs->all) { + my $contract = $map->contract; + next unless($contract->contact->reseller_id); # skip non-customers + my $chosen_contract = $contract_rs->find({id => $contract->id}); + next unless( defined $chosen_contract && $chosen_contract->get_column('billing_mapping_id') == $map->id ); # is not current mapping + 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 && $profile->prepaid) { + foreach my $map($rs->all) { + my $contract = $map->contract; + next unless($contract->contact->reseller_id); # skip non-customers + my $chosen_contract = $contract_rs->find({id => $contract->id}); + next unless( defined $chosen_contract && $chosen_contract->get_column('billing_mapping_id') == $map->id ); # is not current mapping + 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 }); + } + } + } + } + return $profile; } diff --git a/lib/NGCP/Panel/Role/API/Customers.pm b/lib/NGCP/Panel/Role/API/Customers.pm index d6291a1069..2ca71ce1d6 100644 --- a/lib/NGCP/Panel/Role/API/Customers.pm +++ b/lib/NGCP/Panel/Role/API/Customers.pm @@ -9,6 +9,7 @@ use Data::HAL::Link qw(); use HTTP::Status qw(:constants); use NGCP::Panel::Utils::DateTime; use NGCP::Panel::Utils::Contract; +use NGCP::Panel::Utils::Preferences; use NGCP::Panel::Form::Contract::ProductSelect qw(); sub hal_from_customer { @@ -118,7 +119,8 @@ sub update_customer { my ($self, $c, $customer, $old_resource, $resource, $form) = @_; my $billing_mapping = $customer->billing_mappings->find($customer->get_column('bmid')); - $old_resource->{billing_profile_id} = $billing_mapping->billing_profile_id; + $old_resource->{billing_profile_id} = $billing_mapping->billing_profile_id; + $old_resource->{prepaid} = $billing_mapping->billing_profile->prepaid; unless($resource->{billing_profile_id}) { $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id', not defined"); return; @@ -183,6 +185,32 @@ sub update_customer { ); } + if($billing_profile) { # check prepaid change if billing profile changed + if($old_resource->{prepaid} && !$billing_profile->prepaid) { + foreach my $sub($customer->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_resource->{prepaid} && $billing_profile->prepaid) { + foreach my $sub($customer->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 }); + } + } + } + } + # TODO: what about changed product, do we allow it? return $customer;