diff --git a/lib/NGCP/Panel/Utils/InvoiceTemplate.pm b/lib/NGCP/Panel/Utils/InvoiceTemplate.pm index 539627aefd..11b9b77d01 100644 --- a/lib/NGCP/Panel/Utils/InvoiceTemplate.pm +++ b/lib/NGCP/Panel/Utils/InvoiceTemplate.pm @@ -30,7 +30,8 @@ sub convertSvg2Pdf{ #my($fh, $tempfilename) = tempfile(); $tempdirbase = join('/',File::Spec->tmpdir,@$in{qw/provider_id tt_type tt_sourcestate/}, $out->{tt_id}); ! -e $tempdirbase and mkpath( $tempdirbase, 0, 0777 ); - $tempdir = tempdir( DIR => $tempdirbase , CLEANUP => 1 ); + $tempdir = tempdir( DIR => $tempdirbase , CLEANUP => 0 ); + #print "tempdirbase=$tempdirbase; tempdir=$tempdir;$!;\n\n\n"; $c and $c->log->debug("tempdirbase=$tempdirbase; tempdir=$tempdir;"); #try{ #} catch($e){ @@ -61,11 +62,12 @@ sub convertSvg2Pdf{ } my $cmd = "rsvg-convert -f pdf ".join(" ", @pagefiles); + #print $cmd; + #die(); $c and $c->log->debug($cmd); - #`chmod ugo+rwx $filename`; - #binmode(STDOUT); + #$cmd = "chmod ugo+rwx $filename"; #binmode(STDIN); - #$out->{tt_string} = `$cmd`; + #$out->{tt_string_pdf} = `$cmd`; { #$cmd = "fc-list"; open B, "$cmd |"; diff --git a/share/templates/invoice/invoice_template_aux.tt b/share/templates/invoice/invoice_template_aux.tt index e3f8d4f6ae..dc91682fcc 100644 --- a/share/templates/invoice/invoice_template_aux.tt +++ b/share/templates/invoice/invoice_template_aux.tt @@ -159,40 +159,25 @@ [% allcallpages = allmidcallpages + ( lastcallrows ? 1 : 0 ) %] - [%IF ( pagetype == 'title' || pagetype=='all') %] - [% pagenum = 1%] - [% document_header()%] - - [% titlepage( - ( titlezonerows > 0 && invoice_details_zones.size > 0 ) ? invoice_details_zones.slice(0, titlezonerows - 1 ) : [], - - ( titlecallrows > 0 && invoice_details_calls.size > 0 ) ? invoice_details_calls.slice(0, titlecallrows - 1 ) : [], - pagenum, - titlezoneinterval - titlecallinterval - ) -%] - [% bgpage(pagenum) -%] - [% document_footer()%] - [%END-%] [%IF ( pagetype == 'zone' || pagetype=='all' ) && allzonerowsnumber %] [% pages = pagenum_in ? [ pagenum_in ] : [ 1 .. allmidzonepages ] %] [%FOREACH pagenum IN pages %] [% pagerowsstart = titlezonerows + midzonerows * ( pagenum - 1 )%] [% pagerowsend = titlezonerows + midzonerows * pagenum - 1 %] [%# pagerowsend = pagerowsend > invoice_details_zones.size() - 1 ? invoice_details_zones.size() - 1 : pagerowsend %] - [% document_header()%] - [% zonepage( invoice_details_zones.slice( pagerowsstart, pagerowsend ), total, pagenum + 1, pagenum, allzonepages, midzoneinterval ) -%] + [% output = output _ document_header()%] + [% output = output _ zonepage( invoice_details_zones.slice( pagerowsstart, pagerowsend ), total, pagenum + 1, pagenum, allzonepages, midzoneinterval ) -%] [%#+1 because of 1 for titlepage %] - [% bgpage(pagenum + 1, pagenum, allzonepages) -%] - [% document_footer()%] + [% output = output _ bgpage(pagenum + 1, pagenum, allzonepages) -%] + [% output = output _ document_footer()%] [%END-%] [%IF lastzonerows > 0 %] [%#2 because zonepages started from 1, not from 0, and we need add 1 for titlepage %] [% pagenum = 2 + allmidzonepages %] - [% document_header()%] - [% zonepage( invoice_details_zones.slice( allzonerowsnumber - lastzonerows ), total, pagenum, allzonepages, allzonepages, midzoneinterval ) -%] - [% bgpage(pagenum, allzonepages, allzonepages) -%] - [% document_footer()%] + [% output = output _ document_header()%] + [% output = output _ zonepage( invoice_details_zones.slice( allzonerowsnumber - lastzonerows ), total, pagenum, allzonepages, allzonepages, midzoneinterval ) -%] + [% output = output _ bgpage(pagenum, allzonepages, allzonepages) -%] + [% output = output _ document_footer()%] [%END-%] [%END-%] @@ -207,21 +192,39 @@ [%FOREACH pagenum IN pages %] [% pagerowsstart = titlecallrows + midcallrows * ( pagenum - 1 )%] [% pagerowsend = titlecallrows + midcallrows * pagenum - 1 %] - [% document_header()%] - [% callpage( invoice_details_calls.slice( pagerowsstart, pagerowsend ), total, pagenum + 1 + allzonepages, pagenum, allcallpages, midcallinterval ) -%] + [% output = output _ document_header()%] + [% output = output _ callpage( invoice_details_calls.slice( pagerowsstart, pagerowsend ), total, pagenum + 1 + allzonepages, pagenum, allcallpages, midcallinterval ) -%] [%#+1 because of 1 for titlepage %] - [% bgpage(pagenum + 1 + allzonepages, pagenum, allcallpages ) -%] - [% document_footer()%] + [% output = output _ bgpage(pagenum + 1 + allzonepages, pagenum, allcallpages ) -%] + [% output = output _ document_footer()%] [%END-%] [%IF lastcallrows > 0 %] [%#2 because callpages started from 1, not from 0, and we need add 1 for titlepage %] [% pagenum = 2 + allmidcallpages + ( allzonepages ? 1 : 0 ) %] - [% document_header()%] - [% callpage( invoice_details_calls.slice( allcallrowsnumber - lastcallrows ), total, pagenum, allcallpages, allcallpages, midcallinterval ) -%] - [% bgpage(pagenum, allcallpages, allcallpages) -%] - [% document_footer()%] + [% output = output _ document_header()%] + [% output = output _ callpage( invoice_details_calls.slice( allcallrowsnumber - lastcallrows ), total, pagenum, allcallpages, allcallpages, midcallinterval ) -%] + [% output = output _ bgpage(pagenum, allcallpages, allcallpages) -%] + [% output = output _ document_footer()%] [%END-%] [%END-%] + [% total.pagetype.call.global = total.pagetype.call.global.import(total.global)%] [% total.pagetype.call.perpage = total.pagetype.call.perpage.import(total.perpage)%] + + [%IF ( pagetype == 'title' || pagetype=='all') %] + [% pagenum = 1%] + [% output_title = output_title _ document_header()%] + + [% output_title = output_title _ titlepage( + ( titlezonerows > 0 && invoice_details_zones.size > 0 ) ? invoice_details_zones.slice(0, titlezonerows - 1 ) : [], + + ( titlecallrows > 0 && invoice_details_calls.size > 0 ) ? invoice_details_calls.slice(0, titlecallrows - 1 ) : [], + pagenum, + titlezoneinterval + titlecallinterval + ) -%] + [% output_title = output_title _ bgpage(pagenum) -%] + [% output_title = output_title _ document_footer()-%] + [%END-%] + [% output_title _ output-%] [%END-%] diff --git a/share/templates/invoice/invoice_template_lorem.tt b/share/templates/invoice/invoice_template_lorem.tt index 92b370cdce..bf96dff9ad 100644 --- a/share/templates/invoice/invoice_template_lorem.tt +++ b/share/templates/invoice/invoice_template_lorem.tt @@ -16,7 +16,7 @@ DEFAULT invoice.date.month=date.format(date.now(),'%B'); DEFAULT invoice.debit='500'; DEFAULT invoice.discount_service='10'; DEFAULT invoice.discount_wire_transfer='40'; -DEFAULT invoice.number=Math.int(Math.rand(999999))|format('%06d'); +DEFAULT invoice.serial=Math.int(Math.rand(999999))|format('%06d'); DEFAULT invoice.serviceallowance='69'; DEFAULT invoice.voice_termination_fees='10'; END; diff --git a/share/templates/invoice/invoice_template_svg.tt b/share/templates/invoice/invoice_template_svg.tt index 84eb791245..2512b725db 100644 --- a/share/templates/invoice/invoice_template_svg.tt +++ b/share/templates/invoice/invoice_template_svg.tt @@ -56,7 +56,7 @@ g, text, tspan { [%provider.city%], [%date_format(date.now)%] - Invoice [%invoice.number%] + Invoice [%invoice.serial%] diff --git a/share/tools/generate_invoices.pl b/share/tools/generate_invoices.pl index 1a3031858f..10be34af9e 100644 --- a/share/tools/generate_invoices.pl +++ b/share/tools/generate_invoices.pl @@ -6,15 +6,15 @@ use DBI; use Data::Dumper; use NGCP::Panel::Utils::DateTime; use DateTime::TimeZone; -use Net::Domain qw(hostfqdn); -use LWP::UserAgent; +#use Net::Domain qw(hostfqdn); +#use LWP::UserAgent; use NGCP::Panel::Utils::Contract; use NGCP::Panel::Utils::InvoiceTemplate; use NGCP::Panel; use NGCP::Panel::View::SVG; use Test::MockObject; use Sipwise::Base; -my $debug = 1; +my $debug = 0; my ($dbuser, $dbpass); my $mfile = '/etc/mysql/sipwise.cnf'; @@ -45,48 +45,37 @@ print "using user '$dbuser' with pass '$dbpass'\n" #die(); my $dbh = DBI->connect('dbi:mysql:billing;host=localhost', $dbuser, $dbpass) or die "failed to connect to billing DB\n"; -#$dbh->trace(SQL); - -#copypasted from tests -#my $uri = $ENV{CATALYST_SERVER} || ('https://'.hostfqdn.':4443'); -#my $valid_ssl_client_cert = $ENV{API_SSL_CLIENT_CERT} || -# "/etc/ssl/ngcp/api/NGCP-API-client-certificate.pem"; -#my $valid_ssl_client_key = $ENV{API_SSL_CLIENT_KEY} || -# $valid_ssl_client_cert; -#my $ssl_ca_cert = $ENV{API_SSL_CA_CERT} || "/etc/ssl/ngcp/api/ca-cert.pem"; -#$ua->ssl_opts( -# SSL_cert_file => $valid_ssl_client_cert, -# SSL_key_file => $valid_ssl_client_key, -# SSL_ca_file => $ssl_ca_cert, -#); my $stime = NGCP::Panel::Utils::DateTime::current_local()->truncate( to => 'month' ); my $etime_plus = $stime->clone->add( months => 1 ); my $etime = $etime_plus->clone->subtract( seconds => 1 ); + my $c_mock = Test::MockObject->new(); $c_mock->set_false(qw/debug/); my $view = NGCP::Panel::View::SVG->new($c_mock,{}); my $svg_default = $view->getTemplateContent(undef,'invoice/invoice_template_svg.tt'); -NGCP::Panel::Utils::InvoiceTemplate::preprocessInvoiceTemplateSvg({no_fake_data => 1},\$svg_default); +NGCP::Panel::Utils::InvoiceTemplate::preprocessInvoiceTemplateSvg( {no_fake_data => 1}, \$svg_default); #print $etime->ymd; foreach my $provider_contract( @{$dbh->selectall_arrayref('select contracts.*,resellers.id as reseller_core_id from resellers inner join contracts on resellers.contract_id=contracts.id where resellers.status != "terminated"', { Slice => {} } ) } ){ my $provider_contact = $dbh->selectrow_hashref('select * from contacts where id=?', undef, $provider_contract->{contact_id} ); + foreach my $client_contact (@{ $dbh->selectall_arrayref('select contacts.* from contacts where reseller_id = ?', { Slice => {} }, $provider_contract->{reseller_core_id} ) } ){ my $client_contract = $dbh->selectrow_hashref('select contracts.* from contracts where contracts.contact_id=? ', undef, $client_contact->{id} ); - if( !(my $billing_profile = $dbh->selectrow_hashref('select billing_profiles.* + + if( my $billing_profile = $dbh->selectrow_hashref('select billing_profiles.* from billing_mappings inner join billing_profiles on billing_mappings.billing_profile_id=billing_profiles.id 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.status != "terminated" + contracts.status != "terminated" and contracts.contact_id=? and (billing_mappings.start_date <= ? OR billing_mappings.start_date IS NULL) and (billing_mappings.end_date >= ? OR billing_mappings.end_date IS NULL)' , undef, $client_contract->{id}, $etime->epoch, $stime->epoch -) ) ){ +) ){ my $invoice; if(!(my $contract_balance = $dbh->selectrow_hashref('select * from contract_balances where contract_id=? and date(start)=? and date(end)=?',undef,$client_contract->{id},$stime->ymd,$etime->ymd))){ @@ -96,13 +85,13 @@ foreach my $provider_contract( @{$dbh->selectall_arrayref('select contracts.*,re stime => $stime, etime => $etime->datetime, ); - $dbh->do('insert into contract_balances(contract_id,cash_balance,cash_balance_interval,free_time_balance,free_time_balance_interval,start,end,invoice_id)values(?,?,?,?,?,?,?,?)',undef,$client_contract->{id},@$contract_balance{qw/cash_balance cash_balance_interval free_time_balance free_time_balance_interval/},$stime->datetime, $etime->datetime,undef ); $invoice = create_invoice($client_contract->{id},$stime, $etime); $contract_balance = $dbh->selectrow_hashref('select * from contract_balances where id=?',undef,$dbh->last_insert_id(undef,'billing','contract_balances','id')); }else{ - $invoice = $dbh->selectrow_hashref('select * from invoices where id=?',undef,$contract_balance->{invoice_id} ); - $invoice = create_invoice($client_contract->{id},$stime, $etime); + if(!$contract_balance->{invoice_id} || !( $invoice = $dbh->selectrow_hashref('select * from invoices where id=?',undef,$contract_balance->{invoice_id} ))){ + $invoice = create_invoice($client_contract->{id},$stime, $etime); + } } my $invoice_details_calls = $dbh->selectall_arrayref('select cdr.*,bzh.zone, bzh.detail as zone_detail from accounting.cdr @@ -114,7 +103,7 @@ foreach my $provider_contract( @{$dbh->selectall_arrayref('select contracts.*,re -- and cdr.start_time >= ? -- and cdr.start_time <= ? order by cdr.start_time - limit 5' + limit 25' , { Slice => {} } #, $client_contract->{id},$stime->epoch,$etime->epoch ); @@ -137,10 +126,6 @@ foreach my $provider_contract( @{$dbh->selectall_arrayref('select contracts.*,re $invoice_details_calls = [map{[$i++,$_]} (@$invoice_details_calls) x 1]; $i = 1; $invoice_details_zones = [map{[$i++,$_]} (@$invoice_details_zones) x 1]; - - #my $controller = NGCP::Panel->controller('Invoice'); - #my $c = NGCP::Panel->prepare(); - # Monkey with $c to set up a fake context (set req->uri, or params) my ($in, $out); #tt_id used only as part in temporary directory $in = { @@ -168,11 +153,11 @@ foreach my $provider_contract( @{$dbh->selectall_arrayref('select contracts.*,re NGCP::Panel::Utils::InvoiceTemplate::preprocessInvoiceTemplateSvg($in,\$svg); } $svg = $view->getTemplateProcessed($c_mock,\$svg, $stash ); - print $svg; - die(); NGCP::Panel::Utils::InvoiceTemplate::convertSvg2Pdf(undef,\$svg,$in,$out); - #print $out->{tt_string_pdf}; + binmode(STDOUT); + print $out->{tt_string_pdf}; die; + #my $result = $c->forward($controller, 'invoice_templaet', [] ); #$req = HTTP::Request->new('OPTIONS', $uri.'/invoice/'..'/');