Bug 21853: Fix PDF export of basketgroups
[koha.git] / Koha / pdfformat / layout3pages.pm
blobd02160ebcd5ff23a86fc74c18cd9b36fa1dbd896
1 package Koha::pdfformat::layout3pages;
3 #example script to print a basketgroup
4 #written 07/11/08 by john.soros@biblibre.com and paul.poulain@biblibre.com
6 # Copyright 2008-2009 BibLibre SARL
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 #you can use any PDF::API2 module, all you need to do is return the stringifyed pdf object from the printpdf sub.
24 use vars qw(@ISA @EXPORT);
25 use MIME::Base64;
26 use List::MoreUtils qw/uniq/;
27 use Modern::Perl;
28 use utf8;
30 use Koha::Number::Price;
31 use Koha::DateUtils;
32 use Koha::Libraries;
34 BEGIN {
35 use Exporter ();
36 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
37 @ISA = qw(Exporter);
38 @EXPORT = qw(printpdf);
42 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
43 #The constants exported transform that into PostScript points (/mm for milimeter, /in for inch, pt is postscript point, and as so is there only to show what is happening.
44 use constant mm => 25.4 / 72;
45 use constant in => 1 / 72;
46 use constant pt => 1;
48 use PDF::API2;
49 #A4 paper specs
50 my ($height, $width) = (297, 210);
51 use PDF::Table;
53 sub printorders {
54 my ($pdf, $basketgroup, $baskets, $orders) = @_;
56 my $cur_format = C4::Context->preference("CurrencyFormat");
58 $pdf->mediabox($height/mm, $width/mm);
59 my $number = 3;
60 for my $basket (@$baskets){
61 my $page = $pdf->page();
62 my $billing_library = Koha::Libraries->find( $basket->{billingplace} );
63 my $delivery_library = Koha::Libraries->find( $basket->{deliveryplace} );
65 # print basket header (box)
66 my $box = $page->gfx;
67 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
68 $box->stroke;
69 # $box->restore();
71 # create a text
72 my $text = $page->text;
73 # add basketgroup number
74 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
75 $text->translate(20/mm, ($height-15)/mm);
76 $text->text("Order no. ".$basketgroup->{'id'}.". Basket no. ".$basket->{basketno}.". ".$basket->{booksellernote});
77 $text->translate(20/mm, ($height-20)/mm);
78 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
79 $text->text( ( $billing_library ? "Billing at " . $billing_library->branchname : "" )
80 . ( $billing_library and $delivery_library ? " and " : "" )
81 . ( $delivery_library ? "delivery at " . $delivery_library->branchname : "" )
84 my $pdftable = new PDF::Table();
85 my $abaskets;
86 my $arrbasket;
87 my @keys = ('Document', 'Qty', 'RRP tax exc.', 'RRP tax inc.', 'Discount', 'Discount price', 'GST rate', 'Total tax exc.', 'Total tax inc.');
88 for my $bkey (@keys) {
89 push(@$arrbasket, $bkey);
91 push(@$abaskets, $arrbasket);
93 my $titleinfo;
94 foreach my $line (@{$orders->{$basket->{basketno}}}) {
95 $arrbasket = undef;
96 $titleinfo = "";
97 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
98 $titleinfo = $line->{title} . " / " . $line->{author} .
99 ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
100 ( $line->{en} ? " EN: " . $line->{en} : '' ) .
101 ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
102 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
103 ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
104 ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
106 else { # MARC21, NORMARC
107 $titleinfo = $line->{title} . " " . $line->{author} .
108 ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
109 ( $line->{en} ? " EN: " . $line->{en} : '' ) .
110 ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
111 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
112 ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
113 ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
115 push( @$arrbasket,
116 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote for vendor : " . $line->{order_vendornote} : '' ),
117 $line->{quantity},
118 Koha::Number::Price->new( $line->{rrp_tax_excluded} )->format,
119 Koha::Number::Price->new( $line->{rrp_tax_included} )->format,
120 Koha::Number::Price->new( $line->{discount} )->format . '%',
121 Koha::Number::Price->new( $line->{rrp_tax_excluded} - $line->{ecost_tax_excluded})->format,
122 Koha::Number::Price->new( $line->{tax_rate} * 100 )->format . '%',
123 Koha::Number::Price->new( $line->{total_tax_excluded} )->format,
124 Koha::Number::Price->new( $line->{total_tax_included} )->format,
126 push(@$abaskets, $arrbasket);
129 $pdftable->table($pdf, $page, $abaskets,
130 x => 10/mm,
131 w => ($width - 20)/mm,
132 start_y => 270/mm,
133 next_y => 285/mm,
134 start_h => 250/mm,
135 next_h => 250/mm,
136 padding => 5,
137 padding_right => 5,
138 background_color_odd => "lightgray",
139 font => $pdf->corefont("Times", -encoding => "utf8"),
140 font_size => 3/mm,
141 header_props => {
142 font => $pdf->corefont("Times", -encoding => "utf8"),
143 font_size => 9,
144 bg_color => 'gray',
145 repeat => 1,
147 column_props => [
149 min_w => 85/mm, # Minimum column width.
152 justify => 'right', # One of left|right ,
155 justify => 'right', # One of left|right ,
158 justify => 'right', # One of left|right ,
161 justify => 'right', # One of left|right ,
164 justify => 'right', # One of left|right ,
167 justify => 'right', # One of left|right ,
170 justify => 'right', # One of left|right ,
173 justify => 'right', # One of left|right ,
178 $pdf->mediabox($width/mm, $height/mm);
181 sub printbaskets {
182 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
184 # get library name
185 my $libraryname = C4::Context->preference("LibraryName");
187 my $cur_format = C4::Context->preference("CurrencyFormat");
189 $pdf->mediabox($width/mm, $height/mm);
190 my $page = $pdf->openpage(2);
191 # create a text
192 my $text = $page->text;
194 # add basketgroup number
195 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
196 $text->translate(($width-40)/mm, ($height-53)/mm);
197 $text->text("".$basketgroup->{'id'});
198 # print the libraryname in the header
199 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
200 $text->translate(30/mm, ($height-28.5)/mm);
201 $text->text($libraryname);
202 my $pdftable = new PDF::Table();
203 my $abaskets;
204 my $arrbasket;
205 # header of the table
206 my @keys = ('Lot', 'Basket (No.)','Total RRP tax exc.', 'Total RRP tax inc.', 'GST rate', 'GST', 'Total discount', 'Total tax exc.', 'Total tax inc.');
207 for my $bkey (@keys) {
208 push(@$arrbasket, $bkey);
210 my ($grandtotal_rrp_tax_included, $grandtotal_rrp_tax_excluded, $grandtotal_tax_included, $grandtotal_tax_excluded, $grandtotaltax_value, $grandtotaldiscount);
211 # calculate each basket total
212 push(@$abaskets, $arrbasket);
213 for my $basket (@$hbaskets) {
214 my @gst;
215 $arrbasket = undef;
216 my ($total_rrp_tax_excluded, $total_rrp_tax_included, $total_tax_excluded, $total_tax_included, $totaltax_value, $totaldiscount);
217 my $ords = $orders->{$basket->{basketno}};
218 my $ordlength = @$ords;
219 foreach my $ord (@$ords) {
220 $total_tax_excluded += $ord->{total_tax_excluded};
221 $total_tax_included += $ord->{total_tax_included};
222 $totaltax_value += $ord->{tax_value};
223 $totaldiscount += ($ord->{rrp_tax_excluded} - $ord->{ecost_tax_excluded} ) * $ord->{quantity};
224 $total_rrp_tax_excluded += $ord->{rrp_tax_excluded} * $ord->{quantity};
225 $total_rrp_tax_included += $ord->{rrp_tax_included} * $ord->{quantity};
226 push @gst, $ord->{tax_rate};
228 @gst = uniq map { $_ * 100 } @gst;
229 $grandtotal_rrp_tax_excluded += $total_rrp_tax_excluded;
230 $grandtotal_rrp_tax_included += $total_rrp_tax_included;
231 $grandtotal_tax_included += $total_tax_included;
232 $grandtotal_tax_excluded += $total_tax_excluded;
233 $grandtotaltax_value += $totaltax_value;
234 $grandtotaldiscount += $totaldiscount;
235 my @gst_string =
236 map { Koha::Number::Price->new($_)->format . '%' } @gst;
237 push(@$arrbasket,
238 $basket->{contractname},
239 $basket->{basketname} . ' (No. ' . $basket->{basketno} . ')',
240 Koha::Number::Price->new( $total_rrp_tax_excluded )->format,
241 Koha::Number::Price->new( $total_rrp_tax_included )->format,
242 "@gst_string",
243 Koha::Number::Price->new( $totaltax_value )->format,
244 Koha::Number::Price->new( $totaldiscount )->format,
245 Koha::Number::Price->new( $total_tax_excluded )->format,
246 Koha::Number::Price->new( $total_tax_included )->format,
248 push(@$abaskets, $arrbasket);
250 # now, push total
251 undef $arrbasket;
252 push @$arrbasket,
254 'Total',
255 Koha::Number::Price->new( $grandtotal_rrp_tax_excluded )->format,
256 Koha::Number::Price->new( $grandtotal_rrp_tax_included )->format,
258 Koha::Number::Price->new( $grandtotaltax_value )->format,
259 Koha::Number::Price->new( $grandtotaldiscount )->format,
260 Koha::Number::Price->new( $grandtotal_tax_excluded )->format,
261 Koha::Number::Price->new( $grandtotal_tax_included )->format;
262 push @$abaskets,$arrbasket;
263 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
265 $pdftable->table($pdf, $page, $abaskets,
266 x => 5/mm,
267 w => ($width - 10)/mm,
268 start_y => 230/mm,
269 next_y => 230/mm,
270 start_h => 230/mm,
271 next_h => 230/mm,
272 font => $pdf->corefont("Times", -encoding => "utf8"),
273 font_size => 3/mm,
274 padding => 5,
275 padding_right => 10,
276 background_color_odd => "lightgray",
277 header_props => {
278 bg_color => 'gray',
279 repeat => 1,
281 column_props => [
287 justify => 'right',
290 justify => 'right',
293 justify => 'right',
296 justify => 'right',
299 justify => 'right',
303 $pdf->mediabox($height/mm, $width/mm);
306 sub printhead {
307 my ($pdf, $basketgroup, $bookseller) = @_;
309 # get library name
310 my $libraryname = C4::Context->preference("LibraryName");
311 my $billing_library = Koha::Libraries->find( $basketgroup->{billingplace} );
312 my $delivery_library = Koha::Libraries->find( $basketgroup->{deliveryplace} );
313 my $freedeliveryplace = $basketgroup->{freedeliveryplace};
314 # get the subject
315 my $subject;
317 # open 1st page (with the header)
318 my $page = $pdf->openpage(1);
320 # create a text
321 my $text = $page->text;
323 # print the libraryname in the header
324 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
325 $text->translate(30/mm, ($height-28.5)/mm);
326 $text->text($libraryname);
328 # print order info, on the default PDF
329 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
330 $text->translate(100/mm, ($height-5-48)/mm);
331 $text->text($basketgroup->{'id'});
333 # print the date
334 my $today = output_pref({ dt => dt_from_string, dateonly => 1 });
335 $text->translate(130/mm, ($height-5-48)/mm);
336 $text->text($today);
338 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
340 # print billing infos
341 $text->translate(100/mm, ($height-86)/mm);
342 $text->text($libraryname);
343 $text->translate(100/mm, ($height-97)/mm);
344 $text->text($billing_library->branchname);
345 $text->translate(100/mm, ($height-108.5)/mm);
346 $text->text($billing_library->branchphone);
347 $text->translate(100/mm, ($height-115.5)/mm);
348 $text->text($billing_library->branchfax);
349 $text->translate(100/mm, ($height-122.5)/mm);
350 $text->text($billing_library->branchaddress1);
351 $text->translate(100/mm, ($height-127.5)/mm);
352 $text->text($billing_library->branchaddress2);
353 $text->translate(100/mm, ($height-132.5)/mm);
354 $text->text($billing_library->branchaddress3);
355 $text->translate(100/mm, ($height-137.5)/mm);
356 $text->text(join(' ', $billing_library->branchzip, $billing_library->branchcity, $billing_library->branchcountry));
357 $text->translate(100/mm, ($height-147.5)/mm);
358 $text->text($billing_library->branchemail);
360 # print subject
361 $text->translate(100/mm, ($height-145.5)/mm);
362 $text->text($subject);
364 # print bookseller infos
365 $text->translate(100/mm, ($height-180)/mm);
366 $text->text($bookseller->name);
367 $text->translate(100/mm, ($height-185)/mm);
368 $text->text($bookseller->postal);
369 $text->translate(100/mm, ($height-190)/mm);
370 $text->text($bookseller->address1);
371 $text->translate(100/mm, ($height-195)/mm);
372 $text->text($bookseller->address2);
373 $text->translate(100/mm, ($height-200)/mm);
374 $text->text($bookseller->address3);
375 $text->translate(100/mm, ($height-205)/mm);
376 $text->text($bookseller->accountnumber);
378 # print delivery infos
379 $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
380 $text->translate(50/mm, ($height-237)/mm);
381 if ($freedeliveryplace) {
382 my $start = 242;
383 my @fdp = split('\n', $freedeliveryplace);
384 foreach (@fdp) {
385 $text->text($_);
386 $text->translate( 50 / mm, ( $height - $start ) / mm );
387 $start += 5;
389 } else {
390 $text->text($delivery_library->branchaddress1);
391 $text->translate(50/mm, ($height-242)/mm);
392 $text->text($delivery_library->branchaddress2);
393 $text->translate(50/mm, ($height-247)/mm);
394 $text->text($delivery_library->branchaddress3);
395 $text->translate(50/mm, ($height-252)/mm);
396 $text->text(join(' ', $delivery_library->branchzip, $delivery_library->branchcity, $delivery_library->branchcountry));
398 $text->translate(50/mm, ($height-262)/mm);
399 $text->text($basketgroup->{deliverycomment});
402 sub printfooters {
403 my ($pdf) = @_;
404 for (my $i=1;$i <= $pdf->pages;$i++) {
405 my $page = $pdf->openpage($i);
406 my $text = $page->text;
407 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
408 $text->translate(10/mm, 10/mm);
409 $text->text("Page $i / ".$pdf->pages);
413 sub printpdf {
414 my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
415 # open the default PDF that will be used for base (1st page already filled)
416 my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pages.pdf';
417 my $pdf = PDF::API2->open($pdf_template);
418 $pdf->pageLabel( 0, {
419 -style => 'roman',
420 } ); # start with roman numbering
421 # fill the 1st page (basketgroup information)
422 printhead($pdf, $basketgroup, $bookseller);
423 # fill the 2nd page (orders summary)
424 printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
425 # fill other pages (orders)
426 printorders($pdf, $basketgroup, $baskets, $orders);
427 # print something on each page (usually the footer, but you could also put a header
428 printfooters($pdf);
429 return $pdf->stringify;