From 8d43a6ae9284352ead6fbf59fbbe395bf3666b58 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Tue, 4 Jun 2013 18:22:00 +0200 Subject: [PATCH] select only one billing_mapping per contract select the billing_mapping which has an start_date and end_date closest to the current time. do not select one that does not match the current time. also prevent db error about multiple id columns by using 'me.id' in the root controller --- lib/NGCP/Panel/Controller/Contract.pm | 40 ++++++++++++++++++++------- lib/NGCP/Panel/Controller/Root.pm | 6 ++-- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Contract.pm b/lib/NGCP/Panel/Controller/Contract.pm index 50088518c0..3a5c01c73b 100644 --- a/lib/NGCP/Panel/Controller/Contract.pm +++ b/lib/NGCP/Panel/Controller/Contract.pm @@ -7,7 +7,34 @@ use NGCP::Panel::Utils; sub contract_list :Chained('/') :PathPart('contract') :CaptureArgs(0) { my ($self, $c) = @_; + + my $mapping_rs = $c->model('billing')->resultset('billing_mappings'); + my $rs = $c->model('billing')->resultset('contracts') + ->search({ + 'billing_mappings.id' => { + '=' => $mapping_rs->search({ + contract_id => { -ident => 'me.id' }, + start_date => [ -or => + { '<=' => {-ident => 'now()'}}, + { -is => undef }, + ], + end_date => [ -or => + { '>=' => {-ident => 'now()'}}, + { -is => undef }, + ], + },{ + alias => 'sub_query', + rows => 1, + order_by => {-desc => ['start_date', 'id']}, + })->get_column('id')->as_query, + }, + },{ + 'join' => 'billing_mappings', + '+select' => 'billing_mappings.billing_profile_id', + '+as' => 'billing_profile', + }); + $c->stash(contract_select_rs => $rs); $c->stash(ajax_uri => $c->uri_for_action("/contract/ajax")); $c->stash(template => 'contract/list.tt'); @@ -118,12 +145,7 @@ sub delete :Chained('base') :PathPart('delete') :Args(0) { sub ajax :Chained('contract_list') :PathPart('ajax') :Args(0) { my ($self, $c) = @_; - my $rs = $c->model('billing')->resultset('contracts') - ->search(undef, { - 'join' => 'billing_mappings', - '+select' => 'billing_mappings.billing_profile_id', - '+as' => 'billing_profile', - }); + my $rs = $c->stash->{contract_select_rs}; $c->forward( "/ajax_process_resultset", [$rs, ["id","contact_id","billing_profile","status"], @@ -145,13 +167,11 @@ sub peering_root :Chained('peering_list') :PathPart('') :Args(0) { sub peering_ajax :Chained('peering_list') :PathPart('ajax') :Args(0) { my ($self, $c) = @_; - my $rs = $c->model('billing')->resultset('contracts') - ->search({ + my $base_rs = $c->stash->{contract_select_rs}; + my $rs = $base_rs->search({ 'product.class' => 'sippeering', }, { 'join' => {'billing_mappings' => 'product'}, - '+select' => 'billing_mappings.billing_profile_id', - '+as' => 'billing_profile', }); $c->forward( "/ajax_process_resultset", [$rs, diff --git a/lib/NGCP/Panel/Controller/Root.pm b/lib/NGCP/Panel/Controller/Root.pm index ff380a0602..6dc8ffb220 100644 --- a/lib/NGCP/Panel/Controller/Root.pm +++ b/lib/NGCP/Panel/Controller/Root.pm @@ -161,6 +161,8 @@ sub ajax_process_resultset :Private { my @searchdata = map{ +{ $_ => { like => '%'.$sSearch.'%' } } } @$columns[@$searchable]; $rs = $rs->search(\@searchdata); + my $totalDisplayRecords = $rs->count; + #potentially selected Id as first element if (defined($iIdOnTop)) { my $topRow = $rs->find($iIdOnTop); @@ -169,7 +171,7 @@ sub ajax_process_resultset :Private { my @tmpTopRowArray = @tmpTopRow{@$columns}; push @$aaData, \@tmpTopRowArray; - $rs = $rs->search({ id => { '!=', $iIdOnTop}}); + $rs = $rs->search({ 'me.id' => { '!=', $iIdOnTop}}); } } @@ -182,8 +184,6 @@ sub ajax_process_resultset :Private { $rs = $rs->search(undef, $sortdata); } - my $totalDisplayRecords = $rs->count; - #Pagination if (defined($iDisplayStart) && $iDisplayLength) { $rs = $rs->search(