MT#5879 Pages generated by template Toolkit first version

ipeshinskaya/InvoiceTemplate5
Irina Peshinskaya 11 years ago committed by Victor Seva
parent 8834411f8e
commit d36699c10e

@ -354,16 +354,16 @@ sub base :Chained('list_customer') :PathPart('') :CaptureArgs(1) {
$c->stash(template => 'customer/details.tt');
$c->stash(contract => $contract_first);
$c->stash(contract_rs => $contract_rs);
#FAKE FAKE FAKE FAKE
$zonecalls_rs = [$zonecalls_rs->all()];
my @array = @$zonecalls_rs;
##@array = (@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array);
#@array = (@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array);
$zonecalls_rs = [@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array];
##$zonecalls_rs = [ 1..100 ];
my $i = 1;
$zonecalls_rs = [map{[$i++,$_]}@$zonecalls_rs];
#$zonecalls_rs = [ 1..100 ];
use irka;
use Data::Dumper;
irka::loglong(Dumper($zonecalls_rs) );
$c->stash(zonecalls_rs => $zonecalls_rs );
$c->stash(billing_mapping => $billing_mapping );
@ -835,7 +835,10 @@ sub calls :Chained('base') :PathPart('calls') :Args(0) {
stime => $stime,
etime => $etime,
);
$c->stash(zonecalls_rs => $zonecalls_rs);
#my @array = $zonecalls_rs->all();
#s$zonecalls_rs = [@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array,@array];
#$c->stash(zonecalls_rs => $zonecalls_rs);
$c->stash(zonecalls_rs => [1..100] );
}
$c->stash(template => 'customer/calls.tt');
}
@ -854,7 +857,7 @@ sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
my($validator,$backend,$in);
#input
(undef,undef,@$in{qw/tt_type tt_viewmode tt_sourcestate tt_id/}) = @_ ;
(undef,undef,@$in{qw/tt_type tt_viewmode tt_sourcestate tt_output_type tt_id/}) = @_ ;
$in->{contract_id} = $c->stash->{contract}->id;
$in->{tt_string} = $c->request->body_parameters->{template} || '';
@ -874,10 +877,12 @@ sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
#really, we don't need a form here at all
#just use as already implemented fields checking and defaults applying
$validator->setup_form(
#$validator->setup_form(
$validator->process(
posted => 1,
params => $in,
);
#$validator->validate_form();
#multi return...
$c->log->debug("validated=".$validator->validated.";\n");
@ -894,18 +899,21 @@ sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
$in = $in_validated;
#dirty hack 2
#validate methods don't work in form configuration, will find why later
#validate methods in form configuration don't change fields values, will find why later
if($in->{tt_type} eq 'svgpdf'){
$in->{tt_type} = 'svg';
$in->{tt_output_type} = 'pdf';
}elsif($in->{tt_type} eq 'html'){
$in->{tt_output_type} = 'html';
}
irka::loglong(Dumper($in));
#model logic
my $tt_string_default = '';
my $tt_string_customer = '';
my $tt_string_force_default = $in->{tt_sourcestate} eq 'default';
my $tt_string_force_default = ( $in->{tt_sourcestate} eq 'default' );
$c->log->debug("force_default=$tt_string_force_default;");
if(!$in->{tt_string} && !$tt_string_force_default){
#here we also may be better should contact model, not DB directly. Will return to this separation later
#at the end - we can figure out rather basic controller behaviour
@ -916,7 +924,7 @@ sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
if($in->{tt_string} || !$tt_string_customer || $tt_string_force_default ){
try{
#Utils... mmm - if it were model - there would be no necessity in utils using
NGCP::Panel::Utils::InvoiceTemplate::getDefaultInvoiceTemplate( c => $c, result => \$tt_string_default );
NGCP::Panel::Utils::InvoiceTemplate::getDefaultInvoiceTemplate( c => $c, type => $in->{tt_type}, result => \$tt_string_default );
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
@ -952,6 +960,7 @@ sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
$output_string = $tt_string_sanitized;
}elsif(!$tt_string_customer || $tt_string_force_default){
$output_string = $tt_string_default;
$c->log->debug("apply default;");
}
}else{#we have customer template, we don't have dynamic template string, we weren't requested to show default
$output_string = $tt_string_customer;
@ -964,6 +973,8 @@ sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
$c->response->content_type('image/svg+xml');
}elsif($in->{tt_output_type} eq 'pdf'){
$c->response->content_type('application/pdf');
}elsif($in->{tt_output_type} eq 'html'){
$c->response->content_type('text/html');
}
if($in->{tt_viewmode} eq 'raw'){
#$c->stash->{VIEW_NO_TT_PROCESS} = 1;
@ -983,19 +994,47 @@ sub calls_svg :Chained('base') :PathPart('calls/template') :Args {
$c->stash( provider => $contacts->first );
if($in->{tt_output_type} eq 'svg'){
if($in->{tt_output_type} eq 'svg' || $in->{tt_output_type} eq 'html'){
#$c->response->content_type('image/svg+xml');
$c->stash( template => \$output_string );
$c->detach($c->view('SVG'));
}elsif($in->{tt_output_type} eq 'pdf'){
$c->response->content_type('application/pdf');
my $svg = $c->view('SVG')->getTemplateProcessed($c,\$output_string, $c->stash );
$c->log->debug($svg);
my $kit = PDF::WebKit->new(\$svg, page_size => 'Letter');
#$c->log->debug($svg);
my $kit = PDF::WebKit->new(\$svg, page_size => 'A4');
#push @{ $kit->stylesheets }, "/path/to/css/file";
# Get an inline PDF
$output_string = $kit->to_pdf;
$c->response->body($output_string);
use File::Temp qw/tempfile/;
my($fh, $filename) = tempfile();
#$fh->unlink_on_destroy( 0 );
#my $filename = "/tmp/bbb.svg";
#open my $fh, ">$filename";
#binmode $fh;
#print $fh $svg;
#close $fh;
#my $cmd = "/tmp/wkhtmltox/bin/wkhtmltopdf $filename - ";
#$c->log->debug($cmd);
#`chmod ugo+rwx $filename`;
#binmode(STDOUT);
#binmode(STDIN);
#$output_string = `$cmd`;
#open B, "$cmd |";
#binmode B;
#$/ = undef;
#$output_string = <B>;
#close B;
#$output_string = `cat $filename `;
}
$c->stash( template => \$output_string );
$c->detach($c->view('SVG'));
}
}

@ -72,30 +72,18 @@ has_field 'tt_id' => (
#sub validate_tt_string{
#here could be following: take default from file and get all variables and validate variables from customer string
#};
sub validate_tt_type {
my ( $self, $field ) = @_; # self is the form
use irka;
use Data::Dumper;
irka::loglong("\n\n\nin validate\naaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaa\n");
die();
if( $self->field('tt_type')->value eq 'svgpdf'){
$self->field('tt_output_type')->value('pdf');
$self->field('tt_type')->value('svg');
}
return 1;
};
sub validate {
my $self = shift;
use irka;
use Data::Dumper;
irka::loglong("\n\n\nin validate\naaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaa\n");
die();
if( $self->field('tt_type')->value eq 'svgpdf'){
$self->field('tt_output_type')->value('pdf');
$self->field('tt_type')->value('svg');
}
return 1;
}
#sub validate {
# my ( $self ) = @_; # self is the form
# if( $self->field('tt_type')->value eq 'svgpdf'){
# use irka;
# use Data::Dumper;
# irka::loglong("\n\n\nin validate\nBBBBBBBBBBBBBBBBBBBBB\naaaaaaaaaaaaaaa\n");
# #die();
# $self->field('tt_output_type')->value('pdf');
# $self->field('tt_type')->value('svg');
# }
# return 1;
#};
1;

@ -4764,8 +4764,7 @@ this.setSvgString = function(xmlString) {
svgroot.appendChild(svgcontent);
//var startre=;
//svgroot.outerSvgTag = outerSvgTag;
svgroot.outerSvgTagStart = xmlString.match(/^(?:[\n\r\t\s]|.)*?(?:\<svg)[^\>]*\>/);
svgroot.outerSvgTagEnd = xmlString.match(/<\/svg>(?:[\n\r\t\s]|.)*$/);
svgroot.outerSvgTag = xmlString.match(/\<svg[^\>]*\>/);
//console.log(outerSvgTag[0]);
//alert(outerSvgTag[0]);
//alert('svgroot.innerHTML='+svgroot.outerHTML);

@ -1,7 +1,6 @@
<!--{[%PROCESS 'customer/calls_template.tt' -%]}-->
<!--{[%row_vertical_interval = 10 -%]}-->
<!--{[%MACRO document_header BLOCK-%]}-->
<svg width="215mm" height="297mm" viewBox="0 0 215 297" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" stroke-width="0" stroke="#000000" fill="#000000" font-size="4pt" font-family="HelveticaNeue-Light" xml:space="preserve" text-anchor="start">
<style>
<![CDATA[
@ -42,6 +41,9 @@ g, text, tspan {
</style>
<!--pageSet>
<page-->
<!--{[%END-%]}-->
<!--{[%MACRO titlepage(callsdata) BLOCK-%]}-->
<g y="0" x="0" width="215" height="297" id="titlepage">
<title>TitlePage</title>
@ -63,8 +65,7 @@ g, text, tspan {
<text x="2" y="25" style="font-family: 'Delicious'; font-weight:normal; font-style: normal">Text using CSS @font-face</text>
</g>
<g y="40" x="105" width="100" height="28" transform="translate(105, 40)" class="address" id="titlepage_companyaddress_sipwise">
<g y="40" x="105" width="100" height="28" transform="translate(105, 40)" class="address" font-size="2pt" id="titlepage_companyaddress_sipwise">
<rect x="0" y="0" width="100" height="28" stroke-width="0.25" fill="#FFFFFF" id="titlepage_companyaddress_sipwise__border"/>
<text x="2" y="5" id="titlepage_companyaddress_sipwise__company" class="company">{<!--{[%}-->client.company<!--{%]}-->}</text>
<text x="2" y="10" id="titlepage_companyaddress_sipwise__address">{<!--{[%}-->client.street<!--{%]}-->}, Postcode {<!--{[%}-->client.postcode<!--{%]}-->} </text>
@ -78,8 +79,7 @@ g, text, tspan {
<text x="5" y="17" id="titlepage_companyaddress_custom_2">address line 2</text>
<text x="5" y="27" id="titlepage_companyaddress_custom_3">address line 3</text>
</g>
<g y="135" x="10" width="195" height="150" id="titlepage_invoicedetails_head" transform="translate(10, 135)">
<g y="135" x="10" width="195" height="150" id="titlepage_invoicedetails" transform="translate(10, 135)" rows="5">
<!--{[%MACRO titlerow BLOCK-%]}-->
<text id="calls_title" text-anchor="end">
<tspan x="5" id="calls_number" text-anchor="start">#</tspan>
@ -108,27 +108,41 @@ g, text, tspan {
<tspan x="190" id="calls_total_cost"><!--{[% ( total_cost / 100 )|format('%.2f') %]}--><!--{[%#}-->0.00<!--{%]}--></tspan>
</text>
<!--{[%END-%]}-->
<!--{[% titlerow -%]}-->
<!--{[% total = {} -%]}-->
<!--{[% list_calls(zonecalls_rs, 'datarow', total, 'svg', row_vertical_interval) -%]}-->
<!--{[% list_calls(callsdata, 'datarow', total, 'svg', row_vertical_interval) -%]}-->
<!--{[% adjustrow (total, 'totalrow', 'svg', row_vertical_interval, zonecalls_rs.size) -%]}-->
</g>
</g>
<!--{[%END-%]}-->
<!--/page>
<page-->
<!--{[%MACRO midpage BLOCK-%]}-->
<g id="midpage">
<title>MidPage</title>
<g y="15" x="10" width="195" height="150" id="midpage_invoicedetails" transform="translate(10, 15)" rows="15">
<!--{[% titlerow -%]}-->
<!--{[% list_calls(zonecalls_rs, 'datarow', total, 'svg', row_vertical_interval) -%]}-->
<!--{[% adjustrow (total, 'totalrow', 'svg', row_vertical_interval, zonecalls_rs.size) -%]}-->
</g>
</g>
<!--{[%END-%]}-->
<!--{[%MACRO lastpage BLOCK-%]}-->
<g id="lastpage">
<title>LastPage</title>
<g y="15" x="10" width="195" height="150" id="lastpage_invoicedetails" transform="translate(10, 15)" rows="10">
<!--{[% titlerow -%]}-->
<!--{[% list_calls(zonecalls_rs, 'datarow', total, 'svg', row_vertical_interval) -%]}-->
<!--{[% adjustrow (total, 'totalrow', 'svg', row_vertical_interval, zonecalls_rs.size) -%]}-->
</g>
</g>
<!--{[%END-%]}-->
<!--{[%MACRO bgpage BLOCK-%]}-->
<g id="bgpage">
<title>Background</title>
<g y="270" x="5" width="205" height="18" transform="translate(5, 270)" class="address" id="bgpage_companyaddress_sipwise">
<g y="270" x="5" width="205" height="18" transform="translate(5, 270)" class="address" font-size="2pt" id="bgpage_companyaddress_sipwise">
<rect x="0" y="0" width="205" height="18" stroke-width="0.25" fill="#FFFFFF" id="bgpage_companyaddress_sipwise__border"/>
<text x="2" y="5" id="bgpage_companyaddress_sipwise__company" class="company">{<!--{[%}-->provider.company<!--{%]}-->}</text>
<text x="2" y="10" id="bgpage_companyaddress_sipwise__address">{<!--{[%}-->provider.street<!--{%]}-->}, Postcode {<!--{[%}-->provider.postcode<!--{%]}-->} {<!--{[%}-->provider.city<!--{%]}-->} {<!--{[%}-->provider.country<!--{%]}-->}</text>
@ -137,6 +151,14 @@ g, text, tspan {
<text x="203" y="15" id="bgpage_companyaddress_sipwise__page_num" text-anchor="end" >{<!--{[%}-->page<!--{%]}-->}</text>
</g>
</g>
<!--{[%END-%]}-->
<!--{[%MACRO document_footer BLOCK-%]}-->
<!--/page>
</pageSet-->
</svg>
<!--{[%END-%]}-->
<!--{[% show_pages(zonecalls_rs,'title')-%]}-->
<!--{[%# show_pages('mid')-%]}-->
<!--{[%# show_pages('last')-%]}-->
<!--{[%# show_pages('all')-%]}-->

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 9.2 KiB

@ -1,26 +1,72 @@
[%MACRO adjustrow(data, rowtype, tt_type, row_vertical_interval, rownumber) BLOCK -%]
[%# USE Dumper%]
[%# Dumper.dump_html(data)%]
[%USE Dumper%]
[%USE Math%]
[%MACRO row_y_re(tt_type) BLOCK -%]
[% IF tt_type == 'svg' -%]
[% y_re = '(?s)(<text[^>]*\s+y\s?=.*?)(\d+)(.*)' -%]
[%END%]
[%y_re%]
[%END%]
[%MACRO page_rows_re(tt_type) BLOCK -%]
[% IF tt_type == 'svg' -%]
[%# page_rows_re = '(?si)<g\s+[^>]*(?:\s+rows\s*=.*?(\d+))?[^>]*\s+id\s*=.*?(?:page_invoicedetails)[^>]*(?:\s+rows\s*=.*?(\d+))?[^>]*>' -%]
[% page_rows_re = 'rows="([0-9]+)"' -%]
[%END%]
[%page_rows_re%]
[%END%]
[%MACRO page_g_y_and_height_re(tt_type) BLOCK -%]
[%#macro intended to be used when no rows specified in data table grouping g %]
[%#all <g y and heights supposed to be taken and from them attempted to define free place for data row%]
[% IF tt_type == 'svg' -%]
[% page_rows_re = '(?s)<g [^>]*\s+\s?=.*?(\d+)[^>]*>' -%]
[%END%]
[%page_rows_re%]
[%END%]
[%MACRO get_row(data, rowtype) BLOCK -%]
[%#use this macro until no symbolic references in tt %]
[%#data can be empty, if we just need y - it doesn't depend on data %]
[%IF rowtype == 'datarow' %]
[%row = datarow(data)%]
[%row = datarow(data)%]
[%ELSIF rowtype == 'totalrow' %]
[%row = totalrow(data)%]
[%row = totalrow(data)%]
[%END%]
[%# row = $rowtype(data) %]
[%tt_type%]
[%row%]
[%END%]
[%MACRO get_page(pagetype) BLOCK -%]
[%#use this macro until no symbolic references in tt %]
[%#data can be empty, if we just need y - it doesn't depend on data %]
[%IF pagetype == 'titlepage' %]
[%page = titlepage()%]
[%ELSIF pagetype == 'midpage' %]
[%page = midpage()%]
[%ELSIF pagetype == 'lastpage' %]
[%page = lastpage()%]
[%END%]
[%page%]
[%END%]
[%MACRO adjustrow(data, rowtype, tt_type, row_vertical_interval, rownumber) BLOCK -%]
[%# y_re = row_y_re(tt_type) %]
[% row = get_row(data, rowtype) %]
[% IF tt_type == 'svg' -%]
[% heights = row.match('(?s)(<text[^>]*\s+y\s?=.*?)(\d+)(.*)' ) -%]
[%END -%]
[%row_vertical_interval%]
[% IF heights.size > 0 -%]
[% y = heights.1 + ( row_vertical_interval * ( rownumber - 1 ) ) %]
[% row = heights.0 _ y _ heights.2 %]
[% y_re = '(?s)(<text[^>]*\s+y\s?=.*?)(\d+)(.*)' -%]
[%END%]
[% matches = row.match( y_re ) -%]
[% IF matches.size > 0 -%]
[% y = matches.1 + ( row_vertical_interval * ( rownumber - 1 ) ) %]
[% row = matches.0 _ y _ matches.2 %]
[%END -%]
[%row%]
[%END -%]
[%MACRO list_calls(zonecalls_rs, rowtype, total, tt_type, row_vertical_interval) BLOCK-%]
[% FOR call IN zonecalls_rs -%]
[%MACRO list_calls(callsdata, rowtype, total, tt_type, row_vertical_interval) BLOCK-%]
[% FOR call IN callsdata -%][%#zonecalls_rs%]
[% total.number = total.number + call.get_column('number') -%]
[% total.duration = total.duration + call.get_column('duration') -%]
[% total.free_time = total.free_time + call.get_column('free_time') -%]
@ -28,3 +74,64 @@
[% adjustrow(call, rowtype, tt_type, row_vertical_interval, loop.count) -%]
[%END -%]
[%END -%]
[%MACRO get_page_rows_number(pagetype, tt_type, row_vertical_interval) BLOCK-%]
[%#doesn't work %]
[% page_rows_re = page_rows_re(tt_type) %]
[% IF tt_type == 'svg' -%]
[%# page_rows_re = '(?si)<g\s+[^>]*(?:\s+rows\s*=.*?(\d+))?[^>]*\s+id\s*=.*?(?:page_invoicedetails)[^>]*(?:\s+rows\s*=.*?(\d+))?[^>]*>' -%]
[% page_rows_re = 'rows="([0-9]+)"' -%]
[%END%]
[% page = get_page(pagetype) %]
[% matches = page.match( page_rows_re ) -%]
[%#page=Dumper.dump(page)%]
[%#page_rows_re=Dumper.dump(page_rows_re)%]
[%#matches=Dumper.dump(matches)%]
[% rows = matches.0 || matches.1 %]
[%#IF !rows%]
[%# all_g_y_re = page_y_re(tt_type) %]
[%# matches = page.match( page_rows_re ) -%]
[%#END%]
[%IF !rows ; rows = 10; END%]
[%rows = Math.int(rows)%]
[%rows%]
[%END -%]
[%MACRO show_pages(zonecalls_rs, pagetype, pagenum_in) BLOCK-%]
[% allrowsnumber = zonecalls_rs.size() %]
[% titlerows = get_page_rows_number('titlepage','svg') || 10 %]
[% midrows = get_page_rows_number('midpage','svg') || 30 %]
[% allmidpages = ( (allrowsnumber - titlerows) / midrows )|format('%d') %]
[% allmidrows = allmidpages * midrows %]
[% lastrows = allrowsnumber - allmidrows - titlerows %]
[%IF ( pagetype == 'title' || pagetype=='all') %]
[% document_header()%]
[% titlepage( zonecalls_rs.slice(0, ( titlerows - 1 ) )) -%]
[% bgpage() -%]
[% document_footer()%]
[%END-%]
[%IF ( pagetype == 'mid' || pagetype=='all' ) && allmidrows %]
[% allmidpages = allmidrows / midrows %]
[% pages = pagenum_in ? [ pagenum_in ] : [ 1 .. ${allmidpages} ] %]
[%FOREACH pagenum IN pages %]
[% pagerowsstart = titlerows + midrows*(pagenum - 1)%]
[% pagerowsend = titlerows + midrows*pagenum%]
[% document_header()%]
[% midpage( zonecalls_rs.slice(pagerowsstart, pagerowsend )) -%]
[% bgpage() -%]
[% document_footer()%]
[%END-%]
[%END-%]
[%IF ( pagetype == 'last' || pagetype=='all' ) %]
[% document_header()%]
[% lastpage( zonecalls_rs.slice(lastrows) ) -%]
[% bgpage() -%]
[% document_footer()%]
[%END-%]
[%END-%]

Loading…
Cancel
Save