diff --git a/lib/NGCP/Panel/Controller/Contact.pm b/lib/NGCP/Panel/Controller/Contact.pm index d2e16da6f8..3a78943198 100644 --- a/lib/NGCP/Panel/Controller/Contact.pm +++ b/lib/NGCP/Panel/Controller/Contact.pm @@ -276,36 +276,42 @@ sub delete :Chained('base') :PathPart('delete') :Args(0) { sub ajax :Chained('list_contact') :PathPart('ajax') :Args(0) { my ($self, $c) = @_; - - NGCP::Panel::Utils::Datatables::process( - $c, - $c->stash->{contacts}->search_rs(undef, {prefetch=>"contracts"}), - $c->stash->{contact_dt_columns}, - sub { - my ($result) = @_; - my $contract_rs = $result->contracts->search({ - status => { '!=' => 'terminated' }, - }); - my $subscriber_rs = $c->model('DB')->resultset('voip_subscribers')->search({ - contact_id => $result->id, - status => { '!=' => 'terminated' }, - }); - my %data = (deletable => ($contract_rs->first or $subscriber_rs->first) ? 0 : 1); - return %data - }, - ); + $self->ajax_list_contacts($c, {'reseller' => 'any'}); $c->detach( $c->view("JSON") ); } sub ajax_noreseller :Chained('list_contact') :PathPart('ajax_noreseller') :Args(0) { my ($self, $c) = @_; + $self->ajax_list_contacts($c, {'reseller' => 'no_reseller'}); + $c->detach( $c->view("JSON") ); +} +sub ajax_reseller :Chained('list_contact') :PathPart('ajax_reseller') :Args(0) { + my ($self, $c) = @_; + $self->ajax_list_contacts($c, {'reseller' => 'not_empty'}); + $c->detach( $c->view("JSON") ); +} + +sub ajax_list_contacts{ + my ($self, $c, $params) = @_; + $params //= {}; + my $reseller_query = []; + if('any' eq $params->{reseller}){ + $reseller_query->[0] = undef; + }elsif('no_reseller' eq $params->{reseller}){ + $reseller_query->[0] = { reseller_id => undef,}; + }elsif('not_empty' eq $params->{reseller}){ + $reseller_query->[0] = { reseller_id => { '!=' => undef },}; + } NGCP::Panel::Utils::Datatables::process( $c, - $c->stash->{contacts}->search_rs({ - reseller_id => undef, - }, {prefetch=>"contracts"}), + $c->stash->{contacts}->search_rs( + $reseller_query->[0], + $reseller_query->[1] ? $reseller_query->[1] : { + prefetch=>"contracts" + } + ), $c->stash->{contact_dt_columns}, sub { my ($result) = @_; @@ -321,7 +327,6 @@ sub ajax_noreseller :Chained('list_contact') :PathPart('ajax_noreseller') :Args( }, ); - $c->detach( $c->view("JSON") ); } sub countries_ajax :Chained('/') :PathPart('contact/country/ajax') :Args(0) { diff --git a/lib/NGCP/Panel/Field/ContactWithReseller.pm b/lib/NGCP/Panel/Field/ContactWithReseller.pm new file mode 100644 index 0000000000..d3ddf318dc --- /dev/null +++ b/lib/NGCP/Panel/Field/ContactWithReseller.pm @@ -0,0 +1,28 @@ +package NGCP::Panel::Field::ContactWithReseller; +use Moose; +use HTML::FormHandler::Moose; +extends 'HTML::FormHandler::Field::Compound'; + +has_field 'id' => ( + type => '+NGCP::Panel::Field::DataTable', + label => 'Contact', + do_label => 0, + do_wrapper => 0, + required => 1, + template => 'helpers/datatables_field.tt', + ajax_src => '/contact/ajax_reseller', + table_titles => ['#', 'Reseller', 'First Name', 'Last Name', 'Email'], + table_fields => ['id', 'reseller_name', 'firstname', 'lastname', 'email'], +); + +has_field 'create' => ( + type => 'Button', + do_label => 0, + value => 'Create Contact', + element_class => [qw/btn btn-tertiary pull-right/], +); + +no Moose; +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Form/Contract/Customer.pm b/lib/NGCP/Panel/Form/Contract/Customer.pm index 946ab8fcc3..26f26b577c 100644 --- a/lib/NGCP/Panel/Form/Contract/Customer.pm +++ b/lib/NGCP/Panel/Form/Contract/Customer.pm @@ -5,7 +5,7 @@ use Storable qw(); extends 'NGCP::Panel::Form::Contract::Base'; has_field 'contact' => ( - type => '+NGCP::Panel::Field::Contact', + type => '+NGCP::Panel::Field::ContactWithReseller', label => 'Contact', validate_when_empty => 1, element_attr => {