diff --git a/lib/NGCP/Panel/Controller/API/TopupCash.pm b/lib/NGCP/Panel/Controller/API/TopupCash.pm index 69136d3875..4f9e4e6a28 100644 --- a/lib/NGCP/Panel/Controller/API/TopupCash.pm +++ b/lib/NGCP/Panel/Controller/API/TopupCash.pm @@ -81,6 +81,7 @@ sub POST :Allow { last unless NGCP::Panel::Utils::Voucher::check_topup(c => $c, now => $now, subscriber_id => $resource->{subscriber_id}, + contract_id => $resource->{contract_id}, package_id => $resource->{package_id}, resource => $resource, entities => $entities, diff --git a/lib/NGCP/Panel/Controller/API/TopupVouchers.pm b/lib/NGCP/Panel/Controller/API/TopupVouchers.pm index c09fd80551..86283afdbe 100644 --- a/lib/NGCP/Panel/Controller/API/TopupVouchers.pm +++ b/lib/NGCP/Panel/Controller/API/TopupVouchers.pm @@ -80,6 +80,7 @@ sub POST :Allow { last unless NGCP::Panel::Utils::Voucher::check_topup(c => $c, now => $now, subscriber_id => $resource->{subscriber_id}, + contract_id => $resource->{contract_id}, plain_code => $resource->{code}, resource => $resource, entities => $entities, diff --git a/lib/NGCP/Panel/Form/Topup/CashAPI.pm b/lib/NGCP/Panel/Form/Topup/CashAPI.pm index 8ac52b762e..f2c85f6276 100644 --- a/lib/NGCP/Panel/Form/Topup/CashAPI.pm +++ b/lib/NGCP/Panel/Form/Topup/CashAPI.pm @@ -16,13 +16,22 @@ has_field 'id' => ( has_field 'subscriber_id' => ( type => 'PosInteger', - required => 1, + required => 0, element_attr => { rel => ['tooltip'], title => ['The subscriber for which to topup the balance.'] }, ); +has_field 'contract_id' => ( + type => 'PosInteger', + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['The contract for which to topup the balance.'] + }, +); + has_field 'package_id' => ( type => 'PosInteger', required => 0, diff --git a/lib/NGCP/Panel/Form/Topup/VoucherAPI.pm b/lib/NGCP/Panel/Form/Topup/VoucherAPI.pm index c2e21005f1..fc8823dcf3 100644 --- a/lib/NGCP/Panel/Form/Topup/VoucherAPI.pm +++ b/lib/NGCP/Panel/Form/Topup/VoucherAPI.pm @@ -16,13 +16,22 @@ has_field 'id' => ( has_field 'subscriber_id' => ( type => 'PosInteger', - required => 1, + required => 0, element_attr => { rel => ['tooltip'], title => ['The subscriber for which to topup the balance.'] }, ); +has_field 'contract_id' => ( + type => 'PosInteger', + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['The contract for which to topup the balance.'] + }, +); + has_field 'code' => ( type => 'Text', required => 1, diff --git a/lib/NGCP/Panel/Utils/Voucher.pm b/lib/NGCP/Panel/Utils/Voucher.pm index 161b270791..1e59f65e43 100644 --- a/lib/NGCP/Panel/Utils/Voucher.pm +++ b/lib/NGCP/Panel/Utils/Voucher.pm @@ -3,6 +3,7 @@ use Sipwise::Base; use Crypt::Rijndael; use MIME::Base64; use NGCP::Panel::Utils::DateTime; +use NGCP::Panel::Utils::Contract qw(); sub encrypt_code { my ($c, $plain) = @_; @@ -44,7 +45,7 @@ sub decrypt_code { sub check_topup { my %params = @_; - my ($c,$plain_code,$voucher_id,$now,$subscriber_id,$contract,$package_id,$schema,$err_code,$entities,$resource) = @params{qw/c plain_code voucher_id now subscriber_id contract package_id schema err_code entities resource/}; + my ($c,$plain_code,$voucher_id,$now,$subscriber_id,$contract_id,$contract,$package_id,$schema,$err_code,$entities,$resource) = @params{qw/c plain_code voucher_id now subscriber_id contract_id contract package_id schema err_code entities resource/}; $schema //= $c->model('DB'); $now //= NGCP::Panel::Utils::DateTime::current_local; @@ -70,6 +71,15 @@ sub check_topup { } $entities->{subscriber} = $subscriber if defined $entities; $contract //= $subscriber->contract; + } elsif (defined $contract_id) { + $contract = NGCP::Panel::Utils::Contract::get_customer_rs(c => $c)->find($contract_id) unless $contract; + unless($contract) { + if (defined $resource) { + $resource->{contract_id} = undef if exists $resource->{contract_id}; + $resource->{contract}->{id} = undef if (exists $resource->{contract} && exists $resource->{contract}->{id}); + } + return 0 unless &{$err_code}("Unknown contract ID $contract_id."); + } } $entities->{contract} = $contract if defined $entities;