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