Bug 13321: Rename variables
[koha.git] / acqui / pdfformat / layout3pages.pm
1 #!/usr/bin/perl
2
3 #example script to print a basketgroup
4 #written 07/11/08 by john.soros@biblibre.com and paul.poulain@biblibre.com
5
6 # Copyright 2008-2009 BibLibre SARL
7 #
8 # This file is part of Koha.
9 #
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.
14 #
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.
19 #
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>.
22
23 #you can use any PDF::API2 module, all you need to do is return the stringifyed pdf object from the printpdf sub.
24 package pdfformat::layout3pages;
25 use vars qw(@ISA @EXPORT);
26 use MIME::Base64;
27 use List::MoreUtils qw/uniq/;
28 use strict;
29 use warnings;
30 use utf8;
31
32 use Koha::Number::Price;
33 use Koha::DateUtils;
34 use Koha::Libraries;
35
36 BEGIN {
37          use Exporter   ();
38          our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
39         @ISA    = qw(Exporter);
40         @EXPORT = qw(printpdf);
41 }
42
43
44 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
45 #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.
46 use constant mm => 25.4 / 72;
47 use constant in => 1 / 72;
48 use constant pt => 1;
49
50 use PDF::API2;
51 #A4 paper specs
52 my ($height, $width) = (297, 210);
53 use PDF::Table;
54
55 sub printorders {
56     my ($pdf, $basketgroup, $baskets, $orders) = @_;
57     
58     my $cur_format = C4::Context->preference("CurrencyFormat");
59
60     $pdf->mediabox($height/mm, $width/mm);
61     my $number = 3;
62     for my $basket (@$baskets){
63         my $page = $pdf->page();
64         my $billing_library = Koha::Libraries->find( $basket->{billingplace} );
65         my $delivery_library = Koha::Libraries->find( $basket->{deliveryplace} );
66
67         # print basket header (box)
68         my $box = $page->gfx;
69         $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
70         $box->stroke;
71 #         $box->restore();
72         
73         # create a text
74         my $text = $page->text;
75         # add basketgroup number
76         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
77         $text->translate(20/mm,  ($height-15)/mm);
78         $text->text("Order no. ".$basketgroup->{'id'}.". Basket no. ".$basket->{basketno}.". ".$basket->{booksellernote});
79         $text->translate(20/mm,  ($height-20)/mm);
80         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
81         $text->text( ( $billing_library ? "Billing at " . $billing_library->branchname : "" )
82             . ( $billing_library and $delivery_library ? " and " : "" )
83             . ( $delivery_library ? "delivery at " . $delivery_library->branchname : "" )
84         );
85
86         my $pdftable = new PDF::Table();
87         my $abaskets;
88         my $arrbasket;
89         my @keys = ('Document', 'Qty', 'RRP tax exc.', 'RRP tax inc.', 'Discount', 'Discount price', 'GST rate', 'Total tax exc.', 'Total tax inc.');
90         for my $bkey (@keys) {
91             push(@$arrbasket, $bkey);
92         }
93         push(@$abaskets, $arrbasket);
94
95         my $titleinfo;
96         foreach my $line (@{$orders->{$basket->{basketno}}}) {
97             $arrbasket = undef;
98             $titleinfo = "";
99             if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
100                 $titleinfo =  $line->{title} . " / " . $line->{author} .
101                     ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
102                     ( $line->{en} ? " EN: " . $line->{en} : '' ) .
103                     ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
104                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
105                     ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
106                     ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
107             }
108             else { # MARC21, NORMARC
109                 $titleinfo =  $line->{title} . " " . $line->{author} .
110                     ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
111                     ( $line->{en} ? " EN: " . $line->{en} : '' ) .
112                     ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
113                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
114                     ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
115                     ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
116             }
117             push( @$arrbasket,
118                 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote for vendor : " . $line->{order_vendornote} : '' ),
119                 $line->{quantity},
120                 Koha::Number::Price->new( $line->{rrp_tax_excluded} )->format,
121                 Koha::Number::Price->new( $line->{rrp_tax_included} )->format,
122                 Koha::Number::Price->new( $line->{discount} )->format . '%',
123                 Koha::Number::Price->new( $line->{rrp_tax_excluded} - $line->{ecost_tax_excluded})->format,
124                 Koha::Number::Price->new( $line->{tax_rate} * 100 )->format . '%',
125                 Koha::Number::Price->new( $line->{total_tax_excluded} )->format,
126                 Koha::Number::Price->new( $line->{total_tax_included} )->format,
127             );
128             push(@$abaskets, $arrbasket);
129         }
130
131         $pdftable->table($pdf, $page, $abaskets,
132                                         x => 10/mm,
133                                         w => ($width - 20)/mm,
134                                         start_y => 270/mm,
135                                         next_y  => 285/mm,
136                                         start_h => 250/mm,
137                                         next_h  => 250/mm,
138                                         padding => 5,
139                                         padding_right => 5,
140                                         background_color_odd  => "lightgray",
141                                         font       => $pdf->corefont("Times", -encoding => "utf8"),
142                                         font_size => 3/mm,
143                                         header_props   =>    {
144                                             font       => $pdf->corefont("Times", -encoding => "utf8"),
145                                             font_size  => 9,
146                                             bg_color   => 'gray',
147                                             repeat     => 1,
148                                         },
149                                         column_props => [
150                                             {
151                                                 min_w => 85/mm,       # Minimum column width.
152                                             },
153                                             {
154                                                 justify => 'right', # One of left|right ,
155                                             },
156                                             {
157                                                 justify => 'right', # One of left|right ,
158                                             },
159                                             {
160                                                 justify => 'right', # One of left|right ,
161                                             },
162                                             {
163                                                 justify => 'right', # One of left|right ,
164                                             },
165                                             {
166                                                 justify => 'right', # One of left|right ,
167                                             },
168                                             {
169                                                 justify => 'right', # One of left|right ,
170                                             },
171                                             {
172                                                 justify => 'right', # One of left|right ,
173                                             },
174                                             {
175                                                 justify => 'right', # One of left|right ,
176                                             },
177                                         ],
178              );
179     }
180     $pdf->mediabox($width/mm, $height/mm);
181 }
182
183 sub printbaskets {
184     my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
185     
186     # get library name
187     my $libraryname = C4::Context->preference("LibraryName");
188     
189     my $cur_format = C4::Context->preference("CurrencyFormat");
190
191     $pdf->mediabox($width/mm, $height/mm);
192     my $page = $pdf->openpage(2);
193     # create a text
194     my $text = $page->text;
195     
196     # add basketgroup number
197     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
198     $text->translate(($width-40)/mm,  ($height-53)/mm);
199     $text->text("".$basketgroup->{'id'});
200     # print the libraryname in the header
201     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
202     $text->translate(30/mm,  ($height-28.5)/mm);
203     $text->text($libraryname);
204     my $pdftable = new PDF::Table();
205     my $abaskets;
206     my $arrbasket;
207     # header of the table
208     my @keys = ('Lot',  'Basket (No.)','Total RRP tax exc.', 'Total RRP tax inc.', 'GST rate', 'GST', 'Total discount', 'Total tax exc.', 'Total tax inc.');
209     for my $bkey (@keys) {
210         push(@$arrbasket, $bkey);
211     }
212     my ($grandtotal_rrp_tax_included, $grandtotal_rrp_tax_excluded, $grandtotal_tax_included, $grandtotal_tax_excluded, $grandtotaltax_value, $grandtotaldiscount);
213     # calculate each basket total
214     push(@$abaskets, $arrbasket);
215     for my $basket (@$hbaskets) {
216         my @gst;
217         $arrbasket = undef;
218         my ($total_rrp_tax_excluded, $total_rrp_tax_included, $total_tax_excluded, $total_tax_included, $totaltax_value, $totaldiscount);
219         my $ords = $orders->{$basket->{basketno}};
220         my $ordlength = @$ords;
221         foreach my $ord (@$ords) {
222             $total_tax_excluded += $ord->{total_tax_excluded};
223             $total_tax_included += $ord->{total_tax_included};
224             $totaltax_value += $ord->{tax_value};
225             $totaldiscount += ($ord->{rrp_tax_excluded} - $ord->{ecost_tax_excluded} ) * $ord->{quantity};
226             $total_rrp_tax_excluded += $ord->{rrp_tax_excluded} * $ord->{quantity};
227             $total_rrp_tax_included += $ord->{rrp_tax_included} * $ord->{quantity};
228             push @gst, $ord->{tax_rate};
229         }
230         @gst = uniq map { $_ * 100 } @gst;
231         $grandtotal_rrp_tax_excluded += $total_rrp_tax_excluded;
232         $grandtotal_rrp_tax_included += $total_rrp_tax_included;
233         $grandtotal_tax_included += $total_tax_included;
234         $grandtotal_tax_excluded += $total_tax_excluded;
235         $grandtotaltax_value += $totaltax_value;
236         $grandtotaldiscount += $totaldiscount;
237         my @gst_string =
238           map { Koha::Number::Price->new($_)->format . '%' } @gst;
239         push(@$arrbasket,
240             $basket->{contractname},
241             $basket->{basketname} . ' (No. ' . $basket->{basketno} . ')',
242             Koha::Number::Price->new( $total_rrp_tax_excluded )->format,
243             Koha::Number::Price->new( $total_rrp_tax_included )->format,
244             "@gst_string",
245             Koha::Number::Price->new( $totaltax_value )->format,
246             Koha::Number::Price->new( $totaldiscount )->format,
247             Koha::Number::Price->new( $total_tax_excluded )->format,
248             Koha::Number::Price->new( $total_tax_included )->format,
249         );
250         push(@$abaskets, $arrbasket);
251     }
252     # now, push total
253     undef $arrbasket;
254     push @$arrbasket,
255       '',
256       'Total',
257       Koha::Number::Price->new( $grandtotal_rrp_tax_excluded )->format,
258       Koha::Number::Price->new( $grandtotal_rrp_tax_included )->format,
259       '',
260       Koha::Number::Price->new( $grandtotaltax_value )->format,
261       Koha::Number::Price->new( $grandtotaldiscount )->format,
262       Koha::Number::Price->new( $grandtotal_tax_excluded )->format,
263       Koha::Number::Price->new( $grandtotal_tax_included )->format;
264     push @$abaskets,$arrbasket;
265     # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
266
267     $pdftable->table($pdf, $page, $abaskets,
268                                     x => 5/mm,
269                                     w => ($width - 10)/mm,
270                                     start_y =>  230/mm,
271                                     next_y  => 230/mm,
272                                     start_h => 230/mm,
273                                     next_h  => 230/mm,
274                                     font       => $pdf->corefont("Times", -encoding => "utf8"),
275                                     font_size => 3/mm,
276                                     padding => 5,
277                                     padding_right => 10,
278                                     background_color_odd  => "lightgray",
279                                     header_props   =>    {
280                                         bg_color   => 'gray',
281                                         repeat     => 1,
282                                     },
283                                     column_props => [
284                                         {
285                                         },
286                                         {
287                                         },
288                                         {
289                                             justify => 'right',
290                                         },
291                                         {
292                                             justify => 'right',
293                                         },
294                                         {
295                                             justify => 'right',
296                                         },
297                                         {
298                                             justify => 'right',
299                                         },
300                                         {
301                                             justify => 'right',
302                                         },
303                                     ],
304     );
305     $pdf->mediabox($height/mm, $width/mm);
306 }
307
308 sub printhead {
309     my ($pdf, $basketgroup, $bookseller) = @_;
310
311     # get library name
312     my $libraryname = C4::Context->preference("LibraryName");
313     my $billing_library  = Koha::Libraries->find( $basketgroup->{billingplace} );
314     my $delivery_library = Koha::Libraries->find( $basketgroup->{deliveryplace} );
315     my $freedeliveryplace = $basketgroup->{freedeliveryplace};
316     # get the subject
317     my $subject;
318
319     # open 1st page (with the header)
320     my $page = $pdf->openpage(1);
321     
322     # create a text
323     my $text = $page->text;
324     
325     # print the libraryname in the header
326     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
327     $text->translate(30/mm,  ($height-28.5)/mm);
328     $text->text($libraryname);
329
330     # print order info, on the default PDF
331     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
332     $text->translate(100/mm,  ($height-5-48)/mm);
333     $text->text($basketgroup->{'id'});
334     
335     # print the date
336     my $today = output_pref({ dt => dt_from_string, dateonly => 1 });
337     $text->translate(130/mm,  ($height-5-48)/mm);
338     $text->text($today);
339     
340     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
341     
342     # print billing infos
343     $text->translate(100/mm,  ($height-86)/mm);
344     $text->text($libraryname);
345     $text->translate(100/mm,  ($height-97)/mm);
346     $text->text($billing_library->branchname);
347     $text->translate(100/mm,  ($height-108.5)/mm);
348     $text->text($billing_library->branchphone);
349     $text->translate(100/mm,  ($height-115.5)/mm);
350     $text->text($billing_library->branchfax);
351     $text->translate(100/mm,  ($height-122.5)/mm);
352     $text->text($billing_library->branchaddress1);
353     $text->translate(100/mm,  ($height-127.5)/mm);
354     $text->text($billing_library->branchaddress2);
355     $text->translate(100/mm,  ($height-132.5)/mm);
356     $text->text($billing_library->branchaddress3);
357     $text->translate(100/mm,  ($height-137.5)/mm);
358     $text->text(join(' ', $billing_library->branchzip, $billing_library->branchcity, $billing_library->branchcountry));
359     $text->translate(100/mm,  ($height-147.5)/mm);
360     $text->text($billing_library->branchemail);
361     
362     # print subject
363     $text->translate(100/mm,  ($height-145.5)/mm);
364     $text->text($subject);
365     
366     # print bookseller infos
367     $text->translate(100/mm,  ($height-180)/mm);
368     $text->text($bookseller->{name});
369     $text->translate(100/mm,  ($height-185)/mm);
370     $text->text($bookseller->{postal});
371     $text->translate(100/mm,  ($height-190)/mm);
372     $text->text($bookseller->{address1});
373     $text->translate(100/mm,  ($height-195)/mm);
374     $text->text($bookseller->{address2});
375     $text->translate(100/mm,  ($height-200)/mm);
376     $text->text($bookseller->{address3});
377     $text->translate(100/mm, ($height-205)/mm);
378     $text->text($bookseller->{accountnumber});
379     
380     # print delivery infos
381     $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
382     $text->translate(50/mm,  ($height-237)/mm);
383     if ($freedeliveryplace) {
384         my $start = 242;
385         my @fdp = split('\n', $freedeliveryplace);
386         foreach (@fdp) {
387             $text->text($_);
388             $text->translate( 50 / mm, ( $height - $start ) / mm );
389             $start += 5;
390         }
391     } else {
392         $text->text($delivery_library->branchaddress1);
393         $text->translate(50/mm,  ($height-242)/mm);
394         $text->text($delivery_library->branchaddress2);
395         $text->translate(50/mm,  ($height-247)/mm);
396         $text->text($delivery_library->branchaddress3);
397         $text->translate(50/mm,  ($height-252)/mm);
398         $text->text(join(' ', $delivery_library->branchzip, $delivery_library->branchcity, $delivery_library->branchcountry));
399     }
400     $text->translate(50/mm,  ($height-262)/mm);
401     $text->text($basketgroup->{deliverycomment});
402 }
403
404 sub printfooters {
405         my ($pdf) = @_;
406         for (my $i=1;$i <= $pdf->pages;$i++) {
407         my $page = $pdf->openpage($i);
408         my $text = $page->text;
409         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
410         $text->translate(10/mm,  10/mm);
411         $text->text("Page $i / ".$pdf->pages);
412         }
413 }
414
415 sub printpdf {
416     my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
417     # open the default PDF that will be used for base (1st page already filled)
418     my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pages.pdf';
419     my $pdf = PDF::API2->open($pdf_template);
420     $pdf->pageLabel( 0, {
421         -style => 'roman',
422     } ); # start with roman numbering
423     # fill the 1st page (basketgroup information)
424     printhead($pdf, $basketgroup, $bookseller);
425     # fill the 2nd page (orders summary)
426     printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
427     # fill other pages (orders)
428     printorders($pdf, $basketgroup, $baskets, $orders);
429     # print something on each page (usually the footer, but you could also put a header
430     printfooters($pdf);
431     return $pdf->stringify;
432 }
433
434 1;