From a72adaea52b79089350b61275f1131a22ed1db14 Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Thu, 12 Jun 2014 06:12:03 +0300 Subject: [PATCH] MT#5879 Invoice generation fixes. Use correct name for zone fees cost. Fix condition for char column in script. Add root element to xml before XPath parsing to avoid errors about the same elements on root level. Set timezone to local. Maybe it shouldn't be, but then should be changed in both used methods. Todo: Country name for generation from web. Clarify and implement if neccessary generation of existing invoice from web. Now invoice for the same period created. --- lib/NGCP/Panel/Controller/Invoice.pm | 48 ++++++------------- lib/NGCP/Panel/Utils/Contract.pm | 40 ++++++++++++++++ lib/NGCP/Panel/Utils/DateTime.pm | 1 + lib/NGCP/Panel/Utils/InvoiceTemplate.pm | 10 ++-- .../invoice/default/invoice_template_svg.tt | 2 +- share/tools/generate_invoices.pl | 11 ++--- 6 files changed, 68 insertions(+), 44 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Invoice.pm b/lib/NGCP/Panel/Controller/Invoice.pm index ed5d438ee7..f58d9586e0 100644 --- a/lib/NGCP/Panel/Controller/Invoice.pm +++ b/lib/NGCP/Panel/Controller/Invoice.pm @@ -170,7 +170,19 @@ sub create :Chained('inv_list') :PathPart('create') :Args() :Does(ACL) :ACLDetac out => 1, group_by_detail => 1, ); - + my $calllist_rs = NGCP::Panel::Utils::Contract::get_contract_calls_rs( + c => $c, + customer_contract_id => $contract_id, + stime => $stime, + etime => $etime, + ); + my $calllist = [ map { + my $call = {$_->get_inflated_columns}; + $call->{start_time} = $call->{start_time}->epoch; + $call->{source_customer_cost} += 0.0; # make sure it's a number + $call; + } $calllist_rs->all ]; + my $billing_mapping = $customer->billing_mappings->find($customer->get_column('bmid')); my $billing_profile = $billing_mapping->billing_profile; @@ -194,13 +206,10 @@ sub create :Chained('inv_list') :PathPart('create') :Args() :Does(ACL) :ACLDetac $form->params->{period_start} = $stime->epoch; $form->params->{period_end} = $etime->epoch; - my $vat = $customer->vat_rate // 0; - $form->params->{amount_net} = ($balance->cash_balance_interval ? $balance->cash_balance_interval / 100 : 0) + ($billing_profile->interval_charge // 0); # TODO: if not a full month, calculate fraction? - $form->params->{amount_vat} = $customer->add_vat ? $form->params->{amount_net} * ($vat/100) : 0; $form->params->{amount_total} = $form->params->{amount_net} + $form->params->{amount_vat}; @@ -239,36 +248,7 @@ sub create :Chained('inv_list') :PathPart('create') :Args() :Does(ACL) :ACLDetac my $vars = {}; - my $calllist_rs = $c->model('DB')->resultset('cdr')->search({ - source_account_id => $customer->id, - call_status => 'ok', - start_time => { '>=' => $stime->epoch }, - start_time => { '<=' => $etime->epoch }, - },{ - select => [qw/ - source_user source_domain source_cli - destination_user_in - start_time duration call_type - source_customer_cost - source_customer_billing_zones_history.zone - source_customer_billing_zones_history.detail - /], - as => [qw/ - source_user source_domain source_cli - destination_user_in - start_time duration call_type - source_customer_cost - zone - zone_detail - /], - join => 'source_customer_billing_zones_history', - }); - my $calllist = [ map { - my $call = {$_->get_inflated_columns}; - $call->{start_time} = $call->{start_time}->epoch; - $call->{source_customer_cost} += 0.0; # make sure it's a number - $call; - } $calllist_rs->all ]; + # TODO: index 170 seems the upper limit here, then the calllist breaks diff --git a/lib/NGCP/Panel/Utils/Contract.pm b/lib/NGCP/Panel/Utils/Contract.pm index 6e4506e3dc..4455ba7000 100644 --- a/lib/NGCP/Panel/Utils/Contract.pm +++ b/lib/NGCP/Panel/Utils/Contract.pm @@ -392,6 +392,46 @@ sub get_contract_zonesfees { return \%allzones; } +sub get_contract_calls_rs{ + my %params = @_; + (my($c,$customer_contract_id,$stime,$etime)) = @params{qw/c customer_contract_id stime etime/}; + + $stime ||= NGCP::Panel::Utils::DateTime::current_local()->truncate( to => 'month' ); + $etime ||= $stime->clone->add( months => 1 ); + + my $calls_rs = $c->model('DB')->resultset('cdr')->search( { +# source_user_id => { 'in' => [ map {$_->uuid} @{$contract->{subscriber}} ] }, + 'call_status' => 'ok', + 'source_user_id' => { '!=' => '0' }, + 'start_time' => + [ -and => + { '>=' => $stime->epoch}, + { '<=' => $etime->epoch}, + ], + 'source_account_id' => $customer_contract_id, + },{ + select => [qw/ + source_user source_domain source_cli + destination_user_in + start_time duration call_type + source_customer_cost + source_customer_billing_zones_history.zone + source_customer_billing_zones_history.detail + /], + as => [qw/ + source_user source_domain source_cli + destination_user_in + start_time duration call_type + source_customer_cost + zone + zone_detail + /], + 'join' => 'source_customer_billing_zones_history', + 'order_by' => 'start_time', + } ); + + return $calls_rs; +} 1; __END__ diff --git a/lib/NGCP/Panel/Utils/DateTime.pm b/lib/NGCP/Panel/Utils/DateTime.pm index d785810ef7..729c68df8f 100644 --- a/lib/NGCP/Panel/Utils/DateTime.pm +++ b/lib/NGCP/Panel/Utils/DateTime.pm @@ -27,6 +27,7 @@ sub from_string { # convert it to xxxx-xx-xxTxx:xx:xx $s =~ s/^(\d{4}\-\d{2}\-\d{2})\s+(\d.+)$/$1T$2/; my $ts = DateTime::Format::ISO8601->parse_datetime($s); + $ts->set_time_zone( DateTime::TimeZone->new(name => 'local') ); return $ts; } diff --git a/lib/NGCP/Panel/Utils/InvoiceTemplate.pm b/lib/NGCP/Panel/Utils/InvoiceTemplate.pm index 31174bbaaa..934b5d87ad 100644 --- a/lib/NGCP/Panel/Utils/InvoiceTemplate.pm +++ b/lib/NGCP/Panel/Utils/InvoiceTemplate.pm @@ -56,6 +56,8 @@ sub svg_pdf { my $cmd = 'rsvg-convert'; my $cmd_full = $cmd.' '.join(' ', @cmd_args); $c and $c->log->debug( $cmd_full ); + print $cmd_full.";\n"; + $$pdf_ref = capturex([0], $cmd, @cmd_args); return 1; } @@ -64,7 +66,8 @@ sub preprocess_svg { my($svg_ref) = @_; $$svg_ref=~s/(?:{\s*)?(?:\s*})?//gs; - + $$svg_ref = ''.$$svg_ref.''; + my $xp = XML::XPath->new($$svg_ref); my $g = $xp->find('//g[@class="page"]'); @@ -75,9 +78,10 @@ sub preprocess_svg { } $$svg_ref = ($xp->findnodes('/'))[0]->toString(); + $$svg_ref =~s/^|<\/root>$//; - $$svg_ref=~s/<(g .*?)(?:display\s*=\s*["']*none["'[:blank:]]+)(.*?id *=["' ]+page[^"' ]*["' ]+)([^>]*)>/<$1$2$3>/gs; - $$svg_ref=~s/<(g .*?)(id *=["' ]+page[^"' ]*["' ]+.*?)(?:display\s*=\s*["']*none["'[:blank:]]+)([^>]*)>/<$1$2$3>/gs; + #$$svg_ref=~s/<(g .*?)(?:display\s*=\s*["']*none["'[:blank:]]+)(.*?id *=["' ]+page[^"' ]*["' ]+)([^>]*)>/<$1$2$3>/gs; + #$$svg_ref=~s/<(g .*?)(id *=["' ]+page[^"' ]*["' ]+.*?)(?:display\s*=\s*["']*none["'[:blank:]]+)([^>]*)>/<$1$2$3>/gs; } sub sanitize_svg { diff --git a/share/templates/invoice/default/invoice_template_svg.tt b/share/templates/invoice/default/invoice_template_svg.tt index f692b5bb27..e99890f133 100644 --- a/share/templates/invoice/default/invoice_template_svg.tt +++ b/share/templates/invoice/default/invoice_template_svg.tt @@ -99,7 +99,7 @@ Total Price in [% cur %] - + diff --git a/share/tools/generate_invoices.pl b/share/tools/generate_invoices.pl index 5138973645..43d3a4248e 100644 --- a/share/tools/generate_invoices.pl +++ b/share/tools/generate_invoices.pl @@ -162,12 +162,11 @@ sub get_billing_profile{ } sub get_invoice_data_raw{ my($client_contract, $stime, $etime) = @_; - my $invoice_details_calls = $dbh->selectall_arrayref('select cdr.*,from_unixtime(cdr.start_time) as start_time,bzh.zone, bzh.detail as zone_detail from accounting.cdr - LEFT JOIN billing.billing_zones_history bzh ON bzh.id = cdr.source_customer_billing_zone_id + LEFT JOIN billing.billing_zones_history bzh ON bzh.bz_id = cdr.source_customer_billing_zone_id where - cdr.source_user_id != 0 + cdr.source_user_id != "0" and cdr.call_status="ok" and cdr.source_account_id=? and cdr.start_time >= ? @@ -177,11 +176,11 @@ sub get_invoice_data_raw{ , { Slice => {} } , $client_contract->{id},$stime->epoch,$etime->epoch ); - my $invoice_details_zones = $dbh->selectall_arrayref('select SUM(cdr.source_customer_cost) AS cost, COUNT(*) AS number, SUM(cdr.duration) AS duration,sum(cdr.source_customer_free_time) as free_time, bzh.zone + my $invoice_details_zones = $dbh->selectall_arrayref('select SUM(cdr.source_customer_cost) AS customercost, COUNT(*) AS number, SUM(cdr.duration) AS duration,sum(cdr.source_customer_free_time) as free_time, bzh.zone from accounting.cdr - LEFT JOIN billing.billing_zones_history bzh ON bzh.id = cdr.source_customer_billing_zone_id + LEFT JOIN billing.billing_zones_history bzh ON bzh.bz_id = cdr.source_customer_billing_zone_id where - cdr.source_user_id != 0 + cdr.source_user_id != "0" and cdr.call_status="ok" and cdr.source_account_id=? and cdr.start_time >= ?