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