Bug 11349: Change .tmpl -> .tt in scripts using templates
[koha.git] / acqui / pdfformat / layout3pagesfr.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, 2013 BibLibre SARL
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 3 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 #you can use any PDF::API2 module, all you need to do is return the stringifyed pdf object from the printpdf sub.
23 package pdfformat::layout3pagesfr;
24 use vars qw($VERSION @ISA @EXPORT);
25 use Number::Format qw(format_price);
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(GetBranchDetail GetBranchName);
33
34 BEGIN {
35          use Exporter   ();
36          our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
37          # set the version for version checking
38          $VERSION     = 1.00;
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 tranform 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     my $num;
60
61     if ( $cur_format eq 'FR' ) {
62         $num = new Number::Format(
63             'decimal_fill'      => '2',
64             'decimal_point'     => ',',
65             'int_curr_symbol'   => '',
66             'mon_thousands_sep' => ' ',
67             'thousands_sep'     => ' ',
68             'mon_decimal_point' => ','
69         );
70     } else {  # US by default..
71         $num = new Number::Format(
72             'int_curr_symbol'   => '',
73             'mon_thousands_sep' => ',',
74             'mon_decimal_point' => '.'
75         );
76     }
77
78     $pdf->mediabox($height/mm, $width/mm);
79     my $number = 3;
80     for my $basket (@$baskets){
81         my $page = $pdf->page();
82
83         # print basket header (box)
84         my $box = $page->gfx;
85         $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
86         $box->stroke;
87 #         $box->restore();
88
89         # create a text
90         my $text = $page->text;
91         # add basketgroup number
92         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
93         $text->translate(20/mm,  ($height-15)/mm);
94         $text->text("Commande N°".$basketgroup->{'id'}.". Panier N° ".$basket->{basketno}.". ".$basket->{booksellernote});
95         $text->translate(20/mm,  ($height-20)/mm);
96         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
97         $text->text( ( $basket->{billingplace} ? "Facturation à " . C4::Branch::GetBranchName( $basket->{billingplace} ) : "" )
98             . ( $basket->{billingplace} and $basket->{deliveryplace} ? " et " : "" )
99             . ( $basket->{deliveryplace} ? "livraison à " . C4::Branch::GetBranchName( $basket->{deliveryplace}) : "" )
100         );
101
102         my $pdftable = new PDF::Table();
103         my $abaskets;
104         my $arrbasket;
105         my @keys = ('Document', 'Qté', 'Prix', 'Prix net', '% Remise', 'Remise', 'Taux TVA', 'Total HT', 'Total TTC');
106         for my $bkey (@keys) {
107             push(@$arrbasket, $bkey);
108         }
109         push(@$abaskets, $arrbasket);
110
111         my $titleinfo;
112         foreach my $line (@{$orders->{$basket->{basketno}}}) {
113             $arrbasket = undef;
114             $titleinfo = "";
115             if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
116                 $titleinfo =  $line->{title} . " / " . $line->{author} .
117                     ( $line->{isbn} ? " ISBN : " . $line->{isbn} : '' ) .
118                     ( $line->{en} ? " EN : " . $line->{en} : '' ) .
119                     ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
120                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
121                     ( $line->{publishercode} ? ' publié par '. $line->{publishercode} : '') .
122                     ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
123             }
124             else { # MARC21, NORMARC
125                 $titleinfo =  $line->{title} . " " . $line->{author} .
126                     ( $line->{isbn} ? " ISBN : " . $line->{isbn} : '' ) .
127                     ( $line->{en} ? " EN : " . $line->{en} : '' ) .
128                     ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
129                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
130                     ( $line->{publishercode} ? '  publié par '. $line->{publishercode} : '') .
131                     ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
132             }
133
134             push( @$arrbasket,
135                 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote pour le fournisseur : ". $line->{order_vendornote} : '' ),
136                 $line->{quantity},
137                 $num->format_price($line->{rrpgste}),
138                 $num->format_price($line->{rrpgsti}),
139                 $num->format_price($line->{discount}).'%',
140                 $num->format_price($line->{rrpgste} - $line->{ecostgste}),
141                 $num->format_price($line->{gstrate} * 100).'%',
142                 $num->format_price($line->{totalgste}),
143                 $num->format_price($line->{totalgsti}),
144             );
145             push(@$abaskets, $arrbasket);
146         }
147
148         $pdftable->table($pdf, $page, $abaskets,
149                                         x => 10/mm,
150                                         w => ($width - 20)/mm,
151                                         start_y => 270/mm,
152                                         next_y  => 285/mm,
153                                         start_h => 250/mm,
154                                         next_h  => 250/mm,
155                                         padding => 5,
156                                         padding_right => 5,
157                                         background_color_odd  => "lightgray",
158                                         font       => $pdf->corefont("Times", -encoding => "utf8"),
159                                         font_size => 3/mm,
160                                         header_props   =>    {
161                                             font       => $pdf->corefont("Times", -encoding => "utf8"),
162                                             font_size  => 9,
163                                             bg_color   => 'gray',
164                                             repeat     => 1,
165                                         },
166                                         column_props => [
167                                             {
168                                                 min_w => 85/mm,       # Minimum column width.
169                                             },
170                                             {
171                                                 justify => 'right', # One of left|right ,
172                                             },
173                                             {
174                                                 justify => 'right', # One of left|right ,
175                                             },
176                                             {
177                                                 justify => 'right', # One of left|right ,
178                                             },
179                                             {
180                                                 justify => 'right', # One of left|right ,
181                                             },
182                                             {
183                                                 justify => 'right', # One of left|right ,
184                                             },
185                                             {
186                                                 justify => 'right', # One of left|right ,
187                                             },
188                                             {
189                                                 justify => 'right', # One of left|right ,
190                                             },
191                                             {
192                                                 justify => 'right', # One of left|right ,
193                                             },
194                                         ],
195              );
196     }
197     $pdf->mediabox($width/mm, $height/mm);
198 }
199
200 sub printbaskets {
201     my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
202
203     # get library name
204     my $libraryname = C4::Context->preference("LibraryName");
205
206     my $cur_format = C4::Context->preference("CurrencyFormat");
207     my $num;
208
209     if ( $cur_format eq 'FR' ) {
210         $num = new Number::Format(
211             'decimal_fill'      => '2',
212             'decimal_point'     => ',',
213             'int_curr_symbol'   => '',
214             'mon_thousands_sep' => ' ',
215             'thousands_sep'     => ' ',
216             'mon_decimal_point' => ','
217         );
218     } else {  # US by default..
219         $num = new Number::Format(
220             'int_curr_symbol'   => '',
221             'mon_thousands_sep' => ',',
222             'mon_decimal_point' => '.'
223         );
224     }
225
226     $pdf->mediabox($width/mm, $height/mm);
227     my $page = $pdf->openpage(2);
228     # create a text
229     my $text = $page->text;
230
231     # add basketgroup number
232     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
233     $text->translate(($width-40)/mm,  ($height-53)/mm);
234     $text->text("".$basketgroup->{'id'});
235     # print the libraryname in the header
236     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
237     $text->translate(30/mm,  ($height-28.5)/mm);
238     $text->text($libraryname);
239     my $pdftable = new PDF::Table();
240     my $abaskets;
241     my $arrbasket;
242     # header of the table
243     my @keys = ('Lot', 'Panier', 'Prix', 'Prix net', 'Taux TVA', 'TVA', 'Remise', 'Total HT', 'Total TTC');
244     for my $bkey (@keys) {
245         push(@$arrbasket, $bkey);
246     }
247     my ($grandtotalrrpgsti, $grandtotalrrpgste, $grandtotalgsti, $grandtotalgste, $grandtotalgstvalue, $grandtotaldiscount);
248     # calculate each basket total
249     push(@$abaskets, $arrbasket);
250     for my $basket (@$hbaskets) {
251         my @gst;
252         $arrbasket = undef;
253         my ($totalrrpgste, $totalrrpgsti, $totalgste, $totalgsti, $totalgstvalue, $totaldiscount);
254         my $ords = $orders->{$basket->{basketno}};
255         my $ordlength = @$ords;
256         foreach my $ord (@$ords) {
257             $totalgste += $ord->{totalgste};
258             $totalgsti += $ord->{totalgsti};
259             $totalgstvalue += $ord->{gstvalue};
260             $totaldiscount += ($ord->{rrpgste} - $ord->{ecostgste} ) * $ord->{quantity};
261             $totalrrpgste += $ord->{rrpgste} * $ord->{quantity};
262             $totalrrpgsti += $ord->{rrpgsti} * $ord->{quantity};
263             push @gst, $ord->{gstrate};
264         }
265         @gst = uniq map { $_ * 100 } @gst;
266         $totalgsti = $num->round($totalgsti);
267         $totalgste = $num->round($totalgste);
268         $grandtotalrrpgste += $totalrrpgste;
269         $grandtotalrrpgsti += $totalrrpgsti;
270         $grandtotalgsti += $totalgsti;
271         $grandtotalgste += $totalgste;
272         $grandtotalgstvalue += $totalgstvalue;
273         $grandtotaldiscount += $totaldiscount;
274         my @gst_string = map{$num->format_price( $_ ) . '%'} @gst;
275         push(@$arrbasket,
276             $basket->{contractname},
277             $basket->{basketname} . ' (No. ' . $basket->{basketno} . ')',
278             $num->format_price($totalrrpgste),
279             $num->format_price($totalrrpgsti),
280             "@gst_string",
281             $num->format_price($totalgstvalue),
282             $num->format_price($totaldiscount),
283             $num->format_price($totalgste),
284             $num->format_price($totalgsti)
285         );
286         push(@$abaskets, $arrbasket);
287     }
288     # now, push total
289     undef $arrbasket;
290     push @$arrbasket,'','Total', $num->format_price($grandtotalrrpgste), $num->format_price($grandtotalrrpgsti), '', $num->format_price($grandtotalgstvalue), $num->format_price($grandtotaldiscount), $num->format_price($grandtotalgste), $num->format_price($grandtotalgsti);
291     push @$abaskets,$arrbasket;
292     # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
293
294     $pdftable->table($pdf, $page, $abaskets,
295                                     x => 5/mm,
296                                     w => ($width - 10)/mm,
297                                     start_y =>  230/mm,
298                                     next_y  => 230/mm,
299                                     start_h => 230/mm,
300                                     next_h  => 230/mm,
301                                     font       => $pdf->corefont("Times", -encoding => "utf8"),
302                                     font_size => 3/mm,
303                                     padding => 5,
304                                     padding_right => 10,
305                                     background_color_odd  => "lightgray",
306                                     header_props   =>    {
307                                         bg_color   => 'gray',
308                                         repeat     => 1,
309                                     },
310                                     column_props => [
311                                         {
312                                         },
313                                         {
314                                         },
315                                         {
316                                             justify => 'right',
317                                         },
318                                         {
319                                             justify => 'right',
320                                         },
321                                         {
322                                             justify => 'right',
323                                         },
324                                         {
325                                             justify => 'right',
326                                         },
327                                         {
328                                             justify => 'right',
329                                         },
330                                     ],
331     );
332     $pdf->mediabox($height/mm, $width/mm);
333 }
334
335 sub printhead {
336     my ($pdf, $basketgroup, $bookseller) = @_;
337
338     # get library name
339     my $libraryname = C4::Context->preference("LibraryName");
340     # get branch details
341     my $billingdetails  = GetBranchDetail( $basketgroup->{billingplace} );
342     my $deliverydetails = GetBranchDetail( $basketgroup->{deliveryplace} );
343     my $freedeliveryplace = $basketgroup->{freedeliveryplace};
344     # get the subject
345     my $subject;
346
347     # open 1st page (with the header)
348     my $page = $pdf->openpage(1);
349
350     # create a text
351     my $text = $page->text;
352
353     # print the libraryname in the header
354     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
355     $text->translate(30/mm,  ($height-28.5)/mm);
356     $text->text($libraryname);
357
358     # print order info, on the default PDF
359     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
360     $text->translate(100/mm,  ($height-5-48)/mm);
361     $text->text($basketgroup->{'id'});
362
363     # print the date
364     my $today = C4::Dates->today();
365     $text->translate(130/mm,  ($height-5-48)/mm);
366     $text->text($today);
367
368     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
369
370     # print billing infos
371     $text->translate(100/mm,  ($height-86)/mm);
372     $text->text($libraryname);
373     $text->translate(100/mm,  ($height-97)/mm);
374     $text->text($billingdetails->{branchname});
375     $text->translate(100/mm,  ($height-108.5)/mm);
376     $text->text($billingdetails->{branchphone});
377     $text->translate(100/mm,  ($height-115.5)/mm);
378     $text->text($billingdetails->{branchfax});
379     $text->translate(100/mm,  ($height-122.5)/mm);
380     $text->text($billingdetails->{branchaddress1});
381     $text->translate(100/mm,  ($height-127.5)/mm);
382     $text->text($billingdetails->{branchaddress2});
383     $text->translate(100/mm,  ($height-132.5)/mm);
384     $text->text($billingdetails->{branchaddress3});
385     $text->translate(100/mm,  ($height-137.5)/mm);
386     $text->text(join(' ', $billingdetails->{branchzip}, $billingdetails->{branchcity}, $billingdetails->{branchcountry}));
387     $text->translate(100/mm,  ($height-147.5)/mm);
388     $text->text($billingdetails->{branchemail});
389
390     # print subject
391     $text->translate(100/mm,  ($height-145.5)/mm);
392     $text->text($subject);
393
394     # print bookseller infos
395     $text->translate(100/mm,  ($height-180)/mm);
396     $text->text($bookseller->{name});
397     $text->translate(100/mm,  ($height-185)/mm);
398     $text->text($bookseller->{postal});
399     $text->translate(100/mm,  ($height-190)/mm);
400     $text->text($bookseller->{address1});
401     $text->translate(100/mm,  ($height-195)/mm);
402     $text->text($bookseller->{address2});
403     $text->translate(100/mm,  ($height-200)/mm);
404     $text->text($bookseller->{address3});
405     $text->translate(100/mm, ($height-205)/mm);
406     $text->text($bookseller->{accountnumber});
407
408     # print delivery infos
409     $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
410     $text->translate(50/mm,  ($height-237)/mm);
411     if ($freedeliveryplace) {
412         my $start = 242;
413         my @fdp = split('\n', $freedeliveryplace);
414         foreach (@fdp) {
415             $text->text($_);
416             $text->translate( 50 / mm, ( $height - $start ) / mm );
417             $start += 5;
418         }
419     } else {
420         $text->text($deliverydetails->{branchaddress1});
421         $text->translate(50/mm,  ($height-242)/mm);
422         $text->text($deliverydetails->{branchaddress2});
423         $text->translate(50/mm,  ($height-247)/mm);
424         $text->text($deliverydetails->{branchaddress3});
425         $text->translate(50/mm,  ($height-252)/mm);
426         $text->text(join(' ', $deliverydetails->{branchzip}, $deliverydetails->{branchcity}, $deliverydetails->{branchcountry}));
427     }
428     $text->translate(50/mm,  ($height-262)/mm);
429     $text->text($basketgroup->{deliverycomment});
430 }
431
432 sub printfooters {
433         my ($pdf) = @_;
434         for (my $i=1;$i <= $pdf->pages;$i++) {
435         my $page = $pdf->openpage($i);
436         my $text = $page->text;
437         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
438         $text->translate(10/mm,  10/mm);
439         $text->text("Page $i / ".$pdf->pages);
440         }
441 }
442
443 sub printpdf {
444     my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
445     # open the default PDF that will be used for base (1st page already filled)
446     my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pagesfr.pdf';
447     my $pdf = PDF::API2->open($pdf_template);
448     $pdf->pageLabel( 0, {
449         -style => 'roman',
450     } ); # start with roman numbering
451     # fill the 1st page (basketgroup information)
452     printhead($pdf, $basketgroup, $bookseller);
453     # fill the 2nd page (orders summary)
454     printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
455     # fill other pages (orders)
456     printorders($pdf, $basketgroup, $baskets, $orders);
457     # print something on each page (usually the footer, but you could also put a header
458     printfooters($pdf);
459     return $pdf->stringify;
460 }
461
462 1;