diff --git a/lib/NGCP/Panel/Controller/Invoice.pm b/lib/NGCP/Panel/Controller/Invoice.pm index bb87378b59..20f94ec460 100644 --- a/lib/NGCP/Panel/Controller/Invoice.pm +++ b/lib/NGCP/Panel/Controller/Invoice.pm @@ -90,21 +90,19 @@ sub base :Chained('invoice') :PathPart('') :CaptureArgs(1) { sub invoice_details_zones :Chained('base') :PathPart('') :CaptureArgs(0) { - my ($self, $c) = @_; + my ($self, $c, $in) = @_; $c->log->debug('invoice_details_zones'); - my($validator,$backend,$in,$out); - $backend = NGCP::Panel::Model::DB::InvoiceTemplate->new( schema => $c->model('DB') ); + my $backend = NGCP::Panel::Model::DB::InvoiceTemplate->new( schema => $c->model('DB') ); my $provider_id = $c->stash->{provider}->id; - my $client_id = $c->stash->{client} ? $c->stash->{client}->id : undef; - my $stime = NGCP::Panel::Utils::DateTime::current_local()->truncate(to => 'month'); - my $etime = $stime->clone->add(months => 1); + my $stime = $in->{stime} || NGCP::Panel::Utils::DateTime::current_local()->truncate(to => 'month'); + my $etime = $in->{etime} || $stime->clone->add(months => 1); #look, NGCP::Panel::Utils::Contract - it is kind of backend separation here #my $form = NGCP::Panel::Form::Invoice::Template->new( ); my $invoice_details_zones = $backend->get_contract_zonesfees_rs( c => $c, provider_id => $provider_id, - client_id => $client_id, + %$in, stime => $stime, etime => $etime, ); @@ -112,26 +110,25 @@ sub invoice_details_zones :Chained('base') :PathPart('') :CaptureArgs(0) { my $invoice_details_zones_ajax = $invoice_details_zones; $invoice_details_zones = [$invoice_details_zones_ajax->all()]; my $i = 1; - $invoice_details_zones = [map{[$i++,$_]} (@$invoice_details_zones) x 21]; + $invoice_details_zones = [map{[$i++,$_]} (@$invoice_details_zones) x 1]; $c->stash( invoice_details_zones => $invoice_details_zones ); $c->stash( invoice_details_zones_ajax => $invoice_details_zones_ajax ); } sub invoice_details_calls :Chained('invoice_details_zones') :PathPart('') :CaptureArgs(0) { - my ($self, $c) = @_; + my ($self, $c, $in) = @_; $c->log->debug('invoice_details_calls'); my $backend = NGCP::Panel::Model::DB::InvoiceTemplate->new( schema => $c->model('DB') ); my $provider_id = $c->stash->{provider}->id; - my $client_id = $c->stash->{client} ? $c->stash->{client}->id : undef; - my $stime = NGCP::Panel::Utils::DateTime::current_local()->truncate(to => 'month'); - my $etime = $stime->clone->add(months => 1); + my $stime = $in->{stime} || NGCP::Panel::Utils::DateTime::current_local()->truncate(to => 'month'); + my $etime = $in->{etime} || $stime->clone->add(months => 1); #look, NGCP::Panel::Utils::Contract - it is kind of backend separation here #my $form = NGCP::Panel::Form::Invoice::Template->new( ); my $invoice_details_calls = $backend->get_contract_calls_rs( c => $c, provider_id => $provider_id, - client_id => $client_id, + %$in, stime => $stime, etime => $etime, ); @@ -267,20 +264,11 @@ sub invoice_generate :Chained('base') :PathPart('generate') :Args(0) { if($posted){ $c->log->debug("validated=".$validator->validated.";"); if($validator->validated) { - #copy/pasted from NGCP\Panel\Role\API\Customers.pm - - #use irka; - #use Data::Dumper; - #irka::loglong(Dumper($in)); - - #$c->log->debug('getInvoice:contract_id='.Dumper($in).';'); $c->forward('generate_invoice',[$in, $out]); + try { - #$backend->storeInvoiceTemplateInfo(%$in_validated); $c->flash(messages => [{type => 'success', text => $c->loc( - $in->{invoice_id} - ?'Invoice template updated' - :'Invoice template created' + 'Invoice generated' ) }]); } catch($e) { NGCP::Panel::Utils::Message->error( @@ -351,8 +339,8 @@ sub generate_invoice :Private{ ); $c->log->debug('createInvoice:invoice.id='.$invoice->get_column('id').';'); } - $c->forward('invoice_details_calls'); - $c->forward('invoice_details_zones'); + $c->forward('invoice_details_calls',[$in]); + $c->forward('invoice_details_zones',[$in]); #additions for generations $in = { %$in, @@ -380,7 +368,7 @@ sub generate_invoice :Private{ } $svg = $c->view('SVG')->getTemplateProcessed($c,\$svg, $stash ); NGCP::Panel::Utils::InvoiceTemplate::convertSvg2Pdf($c,\$svg,$in,$out); - $backend->storeInvoiceData('invoice'=>$invoice,'data'=>\$out->{tt_string_pdf}); + #$backend->storeInvoiceData('invoice'=>$invoice,'data'=>\$out->{tt_string_pdf}); } sub template_base :Chained('base') :PathPart('template') :CaptureArgs(0) { diff --git a/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm b/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm index 9b195d3349..d9af01e9ea 100644 --- a/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm +++ b/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm @@ -427,125 +427,34 @@ sub getInvoiceProviderClients{ })->as_query }); } - -sub get_contract_calls_rs{ +sub call_owner_condition{ my $self = shift; - my %params = @_; - (my($c,$provider_id,$client_contact_id,$client_contract_id,$stime,$etime)) = @params{qw/c provider_id client_contact_id client_contract_id stime etime/};#I think it may be a record (very long var name) + my ($params) = @_; + (my($c,$provider_id,$client_contact_id,$client_contract_id)) = @$params{qw/c provider_id client_contact_id client_contract_id/}; my %source_account_id_condition; - $stime ||= NGCP::Panel::Utils::DateTime::current_local()->truncate( to => 'month' ); - $etime ||= $stime->clone->add( months => 1 ); if($client_contract_id){ - %source_account_id_condition = ( 'source_user_id' => $client_contract_id ); + %source_account_id_condition = ( 'source_account_id' => $client_contract_id ); }elsif($client_contact_id){ - %source_account_id_condition = ( 'source_account.contact_id' => $client_contact_id ); + %source_account_id_condition = ( + 'source_account.contact_id' => $client_contact_id, + 'contact.reseller_id' => { '!=' => undef }, + ); }elsif($provider_id){ - %source_account_id_condition = ( 'contact.reseller_id' => $client_contact_id ); + %source_account_id_condition = ( + 'contact.reseller_id' => $client_contact_id, + ); } - # $source_account_id_condition = { - # 'in' => $self->getInvoiceProviderClients(%params)->search_rs({},{ - # 'select' => 'me.id', - # })->as_query() - # }; - # $source_account_id_condition = { 'in' => $self->getInvoiceProviderClients( - # provider_contact_id => $provider_contact_id, - # stime => $stime, - # etime => $etime, - # )->search_rs(undef,{ - # 'select' => 'me.id', - # })->as_query }; - #} - ##I find that sql is more compact and clear - #$sql_of_all_provider_contract_id_clients_contact_ids_sip_pbx = [' - #select contacts.* - #from contacts - #where - # contacts.reseller_id=? - # and exists (select * - # from billing_mappings - # inner join contracts on contracts.id=billing_mappings.contract_id - # inner join products on billing_mappings.product_id=products.id and products.class in("sipaccount","pbxaccount") - # where contracts.contact_id=contacts.id - # and (start_date <= ? OR start_date IS NULL) - # and (end_date >= ? OR end_date IS NULL) - #)',$provider_contract_id, $etime->epoch, $stime->epoch]; - #$sql_of_all_client_contract_id_calls = ' - #select cdr.* - #from accounting.cdr - # inner join contracts on cdr.source_account_id=contracts.id - # and contracts.status != "terminated" - #where - # cdr.source_user_id != 0 - # and cdr.call_status="ok" - # and exists (select * - # from billing_mappings - # inner join products on billing_mappings.product_id=products.id and products.class in("sipaccount","pbxaccount") - # where contracts.id=billing_mappings.contract_id - # and (billing_mappings.start_date >= now() OR start_date IS NULL) - # and (billing_mappings.end_date <= now() OR end_date IS NULL) - # ) - # and contracts.contact_id=22 - # order by cdr.start_time - #'; - # - #$sql_of_all_provider_contact_id_clients_calls = ' - #select cdr.* - #from contacts - # inner join contracts on contracts.contact_id=contacts.id - # and contacts.reseller_id=2 and contacts.reseller_id!=contacts.id - # inner join accounting.cdr on cdr.source_account_id=contracts.id - # and contracts.status != "terminated" - #where - # cdr.source_user_id != 0 - # and cdr.call_status="ok" - # and exists (select * - # from billing_mappings - # inner join products on billing_mappings.product_id=products.id and products.class in("sipaccount","pbxaccount") - # where contracts.id=billing_mappings.contract_id - # and (billing_mappings.start_date >= now() OR start_date IS NULL) - # and (billing_mappings.end_date <= now() OR end_date IS NULL) - # ) - # order by cdr.start_time - #'; - #my $calls_rs = $self->schema->resultset('cdr')->search( { -# # source_user_id => { 'in' => [ map {$_->uuid} @{$contract->{subscriber}} ] }, - # 'call_status' => 'ok', - # 'source_user_id' => { '!=' => '0' }, - # 'contact.id' => { '!=' => $provider_contact_id }, - # '-and' => [ - # #'contact.reseller_id' => $provider_id, #$client_contract_id - contract of the client - # 'contact.reseller_id' => { '!=' => undef }, - # ], - # 'source_account.status' => { '!=' => 'terminated'}, - # '-exists' => $self->schema->resultset('billing_mappings')->search({ - # 'contract_id' => \'= source_account.id', - # 'product.class' => [ "sipaccount", "pbxaccount" ], - # 'start_date' => [ -or => - # { '<=' => 'cdr.start_time' }, - # { -is => undef }, - # ], - # 'end_date' => [ -or => - # { '>=' => 'cdr.start_time' }, - # { -is => undef }, - # ], - # },{ - # alias => 'billing_mappings_top', - # join => 'product', - # })->as_query, - #},{ - # '+select' => [ - # 'source_customer_billing_zones_history.zone', - # 'source_customer_billing_zones_history.detail', - # 'destination_user_in', - # ], - # '+as' => [qw/zone zone_detail destination/], - # 'join' => [ - # { - # 'source_account' => 'contact', - # }, - # 'source_customer_billing_zones_history', - # ] - #} ); + return \%source_account_id_condition; +} +sub get_contract_calls_rs{ + my $self = shift; + my %params = @_; + (my($c,$provider_id,$client_contact_id,$client_contract_id,$stime,$etime)) = @params{qw/c provider_id client_contact_id client_contract_id stime etime/}; + + $stime ||= NGCP::Panel::Utils::DateTime::current_local()->truncate( to => 'month' ); + $etime ||= $stime->clone->add( months => 1 ); + + my %source_account_id_condition = %{$self->call_owner_condition(\%params)}; my $calls_rs = $self->schema->resultset('cdr')->search( { # source_user_id => { 'in' => [ map {$_->uuid} @{$contract->{subscriber}} ] }, @@ -570,7 +479,8 @@ sub get_contract_calls_rs{ 'source_account' => 'contact', }, 'source_customer_billing_zones_history', - ] + ], + 'order_by' => { '-desc' => 'start_time'}, } ); return $calls_rs; @@ -578,16 +488,12 @@ sub get_contract_calls_rs{ sub get_contract_zonesfees_rs { my $self = shift; my %params = @_; - (my ($c,$provider_id,$client_contact_id,$client_contract_id,$stime,$etime)) = @params{qw/c provider_id client_contact_id client_contract_id stime etime/}; - my %source_account_id_condition; - if(!$client_id){ - $source_account_id_condition = { 'in' => $self->getInvoiceProviderClients(%params)->search_rs({},{ - 'select' => 'me.id', - })->as_query() }; - }else{ - $source_account_id_condition = $client_id; - } - + (my($c,$provider_id,$client_contact_id,$client_contract_id,$stime,$etime)) = @params{qw/c provider_id client_contact_id client_contract_id stime etime/}; + + $stime ||= NGCP::Panel::Utils::DateTime::current_local()->truncate( to => 'month' ); + $etime ||= $stime->clone->add( months => 1 ); + + my %source_account_id_condition = %{$self->call_owner_condition(\%params)}; # SELECT 'out' as direction, SUM(c.source_customer_cost) AS cost, b.zone, # COUNT(*) AS number, SUM(c.duration) AS duration # FROM accounting.cdr c @@ -600,16 +506,14 @@ sub get_contract_zonesfees_rs { my $zonecalls_rs = $self->schema->resultset('cdr')->search( { # source_user_id => { 'in' => [ map {$_->uuid} @{$contract->{subscriber}} ] }, - call_status => 'ok', - source_user_id => { '!=' => '0' }, - #source_account_id => $source_account_id_condition, - - # start_time => - # [ -and => - # { '>=' => $stime->epoch}, - # { '<=' => $etime->epoch}, - # ], - + 'call_status' => 'ok', + 'source_user_id' => { '!=' => '0' }, + start_time => + [ -and => + { '>=' => $stime->epoch}, + { '<=' => $etime->epoch}, + ], + %source_account_id_condition },{ '+select' => [ { sum => 'me.source_customer_cost', -as => 'cost', },