diff --git a/lib/NGCP/Panel/Controller/API/Customers.pm b/lib/NGCP/Panel/Controller/API/Customers.pm index c04de95d99..0a19b37837 100644 --- a/lib/NGCP/Panel/Controller/API/Customers.pm +++ b/lib/NGCP/Panel/Controller/API/Customers.pm @@ -226,6 +226,11 @@ sub POST :Allow { $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'passreset_email_template_id', doesn't exist for reseller assigned to customer contact"); return; } + if($customer->invoice_email_template_id && + $customer->invoice_email_template->reseller_id != $customer->contact->reseller_id) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'invoice_email_template_id', doesn't exist for reseller assigned to customer contact"); + return; + } try { $customer->billing_mappings->create({ diff --git a/lib/NGCP/Panel/Controller/API/EmailTemplatesItem.pm b/lib/NGCP/Panel/Controller/API/EmailTemplatesItem.pm index ee3990cd98..687db02ae1 100644 --- a/lib/NGCP/Panel/Controller/API/EmailTemplatesItem.pm +++ b/lib/NGCP/Panel/Controller/API/EmailTemplatesItem.pm @@ -172,7 +172,13 @@ sub DELETE :Allow { { my $item = $self->item_by_id($c, $id); last unless $self->resource_exists($c, emailtemplate => $item); - + #foreach(qw/subscriber_email_template_id passreset_email_template_id invoice_email_template_id/){ + # $c->model('DB')->resultset('contracts')->search({ + # $_ => $item->id, + # })->update({ + # $_ => undef, + # }); + #} $c->model('DB')->resultset('contracts')->search({ subscriber_email_template_id => $item->id, })->update({ @@ -183,6 +189,11 @@ sub DELETE :Allow { })->update({ passreset_email_template_id => undef, }); + $c->model('DB')->resultset('contracts')->search({ + invoice_email_template_id => $item->id, + })->update({ + invoice_email_template_id => undef, + }); $item->delete; diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index f1fb3fbb2c..4daff976c9 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -124,7 +124,11 @@ sub create :Chained('list_customer') :PathPart('create') :Args(0) { c => $c, form => $form, fields => {'contact.create' => $c->uri_for('/contact/create'), - 'billing_profile.create' => $c->uri_for('/billing/create')}, + 'billing_profile.create' => $c->uri_for('/billing/create'), + 'subscriber_email_template.create' => $c->uri_for('/emailtemplate/create'), + 'passreset_email_template.create' => $c->uri_for('/emailtemplate/create'), + 'invoice_email_template.create' => $c->uri_for('/emailtemplate/create'), + }, back_uri => $c->req->uri, ); if($posted && $form->validated) { @@ -355,6 +359,7 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { $params->{billing_profile}{id} = $billing_mapping->billing_profile_id; $params->{subscriber_email_template}{id} = delete $params->{subscriber_email_template_id}; $params->{passreset_email_template}{id} = delete $params->{passreset_email_template_id}; + $params->{invoice_email_template}{id} = delete $params->{invoice_email_template_id}; $params = $params->merge($c->session->{created_objects}); if($c->config->{features}->{cloudpbx}) { $form = NGCP::Panel::Form::Contract::ProductSelect->new(ctx => $c); @@ -370,7 +375,11 @@ sub edit :Chained('base') :PathPart('edit') :Args(0) { c => $c, form => $form, fields => {'contact.create' => $c->uri_for('/contact/create'), - 'billing_profile.create' => $c->uri_for('/billing/create')}, + 'billing_profile.create' => $c->uri_for('/billing/create'), + 'subscriber_email_template.create' => $c->uri_for('/emailtemplate/create'), + 'passreset_email_template.create' => $c->uri_for('/emailtemplate/create'), + 'invoice_email_template.create' => $c->uri_for('/emailtemplate/create'), + }, back_uri => $c->req->uri, ); if($posted && $form->validated) { diff --git a/lib/NGCP/Panel/Controller/EmailTemplate.pm b/lib/NGCP/Panel/Controller/EmailTemplate.pm index 597ff580c1..22975b8531 100644 --- a/lib/NGCP/Panel/Controller/EmailTemplate.pm +++ b/lib/NGCP/Panel/Controller/EmailTemplate.pm @@ -156,6 +156,13 @@ sub tmpl_delete :Chained('tmpl_base') :PathPart('delete') { my ($self, $c) = @_; try { + #foreach(qw/subscriber_email_template_id passreset_email_template_id invoice_email_template_id/){ + # $c->model('DB')->resultset('contracts')->search({ + # $_ => $c->stash->{tmpl}->id, + # })->update({ + # $_ => undef, + # }); + #} $c->model('DB')->resultset('contracts')->search({ subscriber_email_template_id => $c->stash->{tmpl}->id, })->update({ @@ -166,6 +173,11 @@ sub tmpl_delete :Chained('tmpl_base') :PathPart('delete') { })->update({ passreset_email_template_id => undef, }); + $c->model('DB')->resultset('contracts')->search({ + invoice_email_template_id => $c->stash->{tmpl}->id, + })->update({ + invoice_email_template_id => undef, + }); $c->stash->{tmpl}->delete; $c->flash(messages => [{type => 'success', text => $c->loc('Email template successfully deleted') }]); } catch ($e) { diff --git a/lib/NGCP/Panel/Form/Contract/ProductOptional.pm b/lib/NGCP/Panel/Form/Contract/ProductOptional.pm index a85829a006..0967473430 100644 --- a/lib/NGCP/Panel/Form/Contract/ProductOptional.pm +++ b/lib/NGCP/Panel/Form/Contract/ProductOptional.pm @@ -22,7 +22,7 @@ has_field 'max_subscribers' => ( has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/contact billing_profile product max_subscribers status external_id subscriber_email_template passreset_email_template vat_rate add_vat/], + render_list => [qw/contact billing_profile product max_subscribers status external_id subscriber_email_template passreset_email_template invoice_email_template vat_rate add_vat/], ); 1; diff --git a/lib/NGCP/Panel/Form/Contract/ProductSelect.pm b/lib/NGCP/Panel/Form/Contract/ProductSelect.pm index b7d640a982..ef52c1096c 100644 --- a/lib/NGCP/Panel/Form/Contract/ProductSelect.pm +++ b/lib/NGCP/Panel/Form/Contract/ProductSelect.pm @@ -22,7 +22,7 @@ has_field 'max_subscribers' => ( has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/contact billing_profile product max_subscribers status external_id subscriber_email_template passreset_email_template vat_rate add_vat/], + render_list => [qw/contact billing_profile product max_subscribers status external_id subscriber_email_template passreset_email_template invoice_email_template vat_rate add_vat/], ); 1; diff --git a/lib/NGCP/Panel/Role/API/Customers.pm b/lib/NGCP/Panel/Role/API/Customers.pm index 0d9585dd60..d291a84fd7 100644 --- a/lib/NGCP/Panel/Role/API/Customers.pm +++ b/lib/NGCP/Panel/Role/API/Customers.pm @@ -77,6 +77,7 @@ sub hal_from_customer { Data::HAL::Link->new(relation => 'ngcp:contractbalances', href => sprintf("/api/contractbalances/%d", $contract_balance->id)), $customer->subscriber_email_template_id ? (Data::HAL::Link->new(relation => 'ngcp:subscriberemailtemplates', href => sprintf("/api/emailtemplates/%d", $customer->subscriber_email_template_id))) : (), $customer->passreset_email_template_id ? (Data::HAL::Link->new(relation => 'ngcp:passresetemailtemplates', href => sprintf("/api/emailtemplates/%d", $customer->passreset_email_template_id))) : (), + $customer->invoice_email_template_id ? (Data::HAL::Link->new(relation => 'ngcp:invoiceemailtemplates', href => sprintf("/api/emailtemplates/%d", $customer->invoice_email_template_id))) : (), Data::HAL::Link->new(relation => 'ngcp:calls', href => sprintf("/api/calls/?customer_id=%d", $customer->id)), ], relation => 'ngcp:'.$self->resource_name, @@ -187,6 +188,17 @@ sub update_customer { return; } } + my $oldinvtmpl = $old_resource->{invoice_email_template_id} // 0; + if($resource->{invoice_email_template_id} && + $oldinvtmpl != $resource->{invoice_email_template_id}) { + my $tmpl = $c->model('DB')->resultset('email_templates') + ->search({ reseller_id => $custcontact->reseller_id }) + ->find($resource->{invoice_email_template_id}); + unless($tmpl) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'invoice_email_template_id', doesn't exist for reseller assigned to customer contact"); + return; + } + } my $old_ext_id = $customer->external_id // '';