Fix FSF address in directory acqui/
[wip/koha-chris_n.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 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::layout3pages;
25 use vars qw($VERSION @ISA @EXPORT);
26 use Number::Format qw(format_price);
27 use MIME::Base64;
28 use strict;
29 use warnings;
30 use utf8;
31
32 use C4::Branch qw(GetBranchDetail);
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("Order N°".$basketgroup->{'id'}.". Basket N° ".$basket->{basketno}.". ".$basket->{booksellernote});
95         
96         my $pdftable = new PDF::Table();
97         my $abaskets;
98         my $arrbasket;
99         my @keys = ('Document','Qty','RRT GST Inc.','Discount','Discount price GST Exc.','GST', 'Total GST Inc.'); 
100         for my $bkey (@keys) {
101             push(@$arrbasket, $bkey);
102         }
103         push(@$abaskets, $arrbasket);
104 #         @{$orders->{$basket->{basketno}}});
105         foreach my $line (@{$orders->{$basket->{basketno}}}) {
106             $arrbasket = undef;
107             push(@$arrbasket, @$line[3]." / ".@$line[2].(@$line[0]?" ISBN : ".@$line[0]:'').(@$line[10]?" EN : ".@$line[10]:'').", ".@$line[1].(@$line[4]?' publié par '.@$line[4]:''), @$line[5],$num->format_price(@$line[6]),$num->format_price(@$line[8]).'%',$num->format_price(@$line[7]/(1+@$line[9]/100)),$num->format_price(@$line[9]).'%',$num->format_price($num->round(@$line[7])*@$line[5]));
108             push(@$abaskets, $arrbasket);
109         }
110         
111         $pdftable->table($pdf, $page, $abaskets,
112                                         x => 10/mm,
113                                         w => ($width - 20)/mm,
114                                         start_y => 270/mm,
115                                         next_y  => 285/mm,
116                                         start_h => 250/mm,
117                                         next_h  => 250/mm,
118                                         padding => 5,
119                                         padding_right => 5,
120                                         background_color_odd  => "lightgray",
121                                         font       => $pdf->corefont("Times", -encoding => "utf8"),
122                                         font_size => 3/mm,
123                                         header_props   =>    {
124                                             font       => $pdf->corefont("Times", -encoding => "utf8"),
125                                             font_size  => 10,
126                                             bg_color   => 'gray',
127                                             repeat     => 1,
128                                         },
129                                         column_props => [
130                                             {
131                                                 min_w => 100/mm,       # Minimum column width.
132                                             },
133                                             {
134                                                 justify => 'right', # One of left|right ,
135                                             },
136                                             {
137                                                 justify => 'right', # One of left|right ,
138                                             },
139                                             {
140                                                 justify => 'right', # One of left|right ,
141                                             },
142                                             {
143                                                 justify => 'right', # One of left|right ,
144                                             },
145                                             {
146                                                 justify => 'right', # One of left|right ,
147                                             },
148                                             {
149                                                 justify => 'right', # One of left|right ,
150                                             },
151                                         ],
152              );
153     }
154     $pdf->mediabox($width/mm, $height/mm);
155 }
156
157 sub printbaskets {
158     my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
159     
160     # get library name
161     my $libraryname = C4::Context->preference("LibraryName");
162     
163     my $cur_format = C4::Context->preference("CurrencyFormat");
164     my $num;
165     
166     if ( $cur_format eq 'FR' ) {
167         $num = new Number::Format(
168             'decimal_fill'      => '2',
169             'decimal_point'     => ',',
170             'int_curr_symbol'   => '',
171             'mon_thousands_sep' => ' ',
172             'thousands_sep'     => ' ',
173             'mon_decimal_point' => ','
174         );
175     } else {  # US by default..
176         $num = new Number::Format(
177             'int_curr_symbol'   => '',
178             'mon_thousands_sep' => ',',
179             'mon_decimal_point' => '.'
180         );
181     }
182     
183     $pdf->mediabox($width/mm, $height/mm);
184     my $page = $pdf->openpage(2);
185     # create a text
186     my $text = $page->text;
187     
188     # add basketgroup number
189     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
190     $text->translate(($width-40)/mm,  ($height-53)/mm);
191     $text->text("".$basketgroup->{'id'});
192     # print the libraryname in the header
193     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
194     $text->translate(30/mm,  ($height-28.5)/mm);
195     $text->text($libraryname);
196     my $pdftable = new PDF::Table();
197     my $abaskets;
198     my $arrbasket;
199     # header of the table
200     my @keys = ('Lot',  'Basket (N°)', 'RRT GST Inc.', 'Discount', 'Discount price','GST rate', 'Total GST exc.','GST', 'Total GST Inc.');
201     for my $bkey (@keys) {
202         push(@$arrbasket, $bkey);
203     }
204     my $grandtotal=0;
205     my $grandgst=0;
206     # calculate each basket total
207     push(@$abaskets, $arrbasket);
208     for my $basket (@$hbaskets) {
209         $arrbasket = undef;
210         my ($total, $gst, $totallist) = (0, 0, 0);
211         my $ords = $orders->{$basket->{basketno}};
212         my $ordlength = @$ords;
213         foreach my $ord (@$ords) {
214             $total += @$ord[5] * @$ord[7];
215             $gst   += (@$ord[5] * @$ord[7]) * $GSTrate/(1+$GSTrate);
216             $totallist += @$ord[5]*@$ord[6];
217         }
218         $total=$num->round($total);
219         $gst=$num->round($gst);
220         $grandtotal +=$total;
221         $grandgst +=$gst;
222         push(@$arrbasket, $basket->{contractname}, $basket->{basketname}.'(N°'.$basket->{basketno}.')',$num->format_price($totallist), $num->format_price($bookseller->{discount}).'%', $num->format_price($total), $num->format_price($GSTrate*100).'%', $num->format_price($total-$gst), $num->format_price($gst), $num->format_price($total));
223         push(@$abaskets, $arrbasket);
224     }
225     # now, push total
226     undef $arrbasket;
227     push @$arrbasket,'','','','Total',$num->format_price($grandtotal),'',$num->format_price($grandtotal-$grandgst), $num->format_price($grandgst),$num->format_price($grandtotal);
228     push @$abaskets,$arrbasket;
229     # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
230
231     $pdftable->table($pdf, $page, $abaskets,
232                                     x => 5/mm,
233                                     w => ($width - 10)/mm,
234                                     start_y =>  230/mm,
235                                     next_y  => 230/mm,
236                                     start_h => 230/mm,
237                                     next_h  => 230/mm,
238                                     font       => $pdf->corefont("Times", -encoding => "utf8"),
239                                     font_size => 3/mm,
240                                     padding => 5,
241                                     padding_right => 10,
242                                     background_color_odd  => "lightgray",
243                                     header_props   =>    {
244                                         bg_color   => 'gray',
245                                         repeat     => 1,
246                                     },
247                                     column_props => [
248                                         {
249                                         },
250                                         {
251                                         },
252                                         {
253                                             justify => 'right',
254                                         },
255                                         {
256                                             justify => 'right',
257                                         },
258                                         {
259                                             justify => 'right',
260                                         },
261                                         {
262                                             justify => 'right',
263                                         },
264                                         {
265                                             justify => 'right',
266                                         },
267                                         {
268                                             justify => 'right',
269                                         },
270                                         {
271                                             justify => 'right',
272                                         },
273                                     ],
274     );
275     $pdf->mediabox($height/mm, $width/mm);
276 }
277
278 sub printhead {
279     my ($pdf, $basketgroup, $bookseller) = @_;
280
281     # get library name
282     my $libraryname = C4::Context->preference("LibraryName");
283     # get branch details
284     my $billingdetails  = GetBranchDetail( $basketgroup->{billingplace} );
285     my $deliverydetails = GetBranchDetail( $basketgroup->{deliveryplace} );
286     # get the subject
287     my $subject;
288
289     # open 1st page (with the header)
290     my $page = $pdf->openpage(1);
291     
292     # create a text
293     my $text = $page->text;
294     
295     # print the libraryname in the header
296     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
297     $text->translate(30/mm,  ($height-28.5)/mm);
298     $text->text($libraryname);
299
300     # print order info, on the default PDF
301     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
302     $text->translate(100/mm,  ($height-5-48)/mm);
303     $text->text($basketgroup->{'id'});
304     
305     # print the date
306     my $today = C4::Dates->today();
307     $text->translate(130/mm,  ($height-5-48)/mm);
308     $text->text($today);
309     
310     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
311     
312     # print billing infos
313     $text->translate(100/mm,  ($height-86)/mm);
314     $text->text($libraryname);
315     $text->translate(100/mm,  ($height-97)/mm);
316     $text->text($billingdetails->{branchname});
317     $text->translate(100/mm,  ($height-108.5)/mm);
318     $text->text($billingdetails->{branchphone});
319     $text->translate(100/mm,  ($height-115.5)/mm);
320     $text->text($billingdetails->{branchfax});
321     $text->translate(100/mm,  ($height-122.5)/mm);
322     $text->text($billingdetails->{branchaddress1});
323     $text->translate(100/mm,  ($height-127.5)/mm);
324     $text->text($billingdetails->{branchaddress2});
325     $text->translate(100/mm,  ($height-132.5)/mm);
326     $text->text($billingdetails->{branchaddress3});
327     $text->translate(100/mm,  ($height-137.5)/mm);
328     $text->text(join(' ', $billingdetails->{branchzip}, $billingdetails->{branchcity}, $billingdetails->{branchcountry}));
329     $text->translate(100/mm,  ($height-147.5)/mm);
330     $text->text($billingdetails->{branchemail});
331     
332     # print subject
333     $text->translate(100/mm,  ($height-145.5)/mm);
334     $text->text($subject);
335     
336     # print bookseller infos
337     $text->translate(100/mm,  ($height-180)/mm);
338     $text->text($bookseller->{name});
339     $text->translate(100/mm,  ($height-185)/mm);
340     $text->text($bookseller->{postal});
341     $text->translate(100/mm,  ($height-190)/mm);
342     $text->text($bookseller->{address1});
343     $text->translate(100/mm,  ($height-195)/mm);
344     $text->text($bookseller->{address2});
345     $text->translate(100/mm,  ($height-200)/mm);
346     $text->text($bookseller->{address3});
347     
348     # print delivery infos
349     $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
350     $text->translate(50/mm,  ($height-237)/mm);
351     $text->text($deliverydetails->{branchaddress1});
352     $text->translate(50/mm,  ($height-242)/mm);
353     $text->text($deliverydetails->{branchaddress2});
354     $text->translate(50/mm,  ($height-247)/mm);
355     $text->text($deliverydetails->{branchaddress3});
356     $text->translate(50/mm,  ($height-252)/mm);
357     $text->text(join(' ', $deliverydetails->{branchzip}, $deliverydetails->{branchcity}, $deliverydetails->{branchcountry}));
358     $text->translate(50/mm,  ($height-262)/mm);
359     $text->text($basketgroup->{deliverycomment});
360 }
361
362 sub printfooters {
363         my ($pdf) = @_;
364         for (my $i=1;$i <= $pdf->pages;$i++) {
365         my $page = $pdf->openpage($i);
366         my $text = $page->text;
367         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
368         $text->translate(10/mm,  10/mm);
369         $text->text("Page $i / ".$pdf->pages);
370         }
371 }
372
373 sub printpdf {
374     my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
375     # open the default PDF that will be used for base (1st page already filled)
376     my $template = C4::Context->preference("OrderPdfTemplate");
377     $template = decode_base64($template);
378     my $pdf = PDF::API2->openScalar($template);
379     $pdf->pageLabel( 0, {
380         -style => 'roman',
381     } ); # start with roman numbering
382     # fill the 1st page (basketgroup information)
383     printhead($pdf, $basketgroup, $bookseller);
384     # fill the 2nd page (orders summary)
385     printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
386     # fill other pages (orders)
387     printorders($pdf, $basketgroup, $baskets, $orders);
388     # print something on each page (usually the footer, but you could also put a header
389     printfooters($pdf);
390     return $pdf->stringify;
391 }
392
393 1;