[3.2.x](bug #3624) Per-basketgroup delivery place
[koha.git] / acqui / pdfformat / example.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 with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 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::example;
25 use vars qw($VERSION @ISA @EXPORT);
26 use Number::Format qw(format_price);      
27 use strict;
28 use warnings;
29 use utf8;
30
31 use C4::Branch qw(GetBranchDetail);
32
33 BEGIN {
34          use Exporter   ();
35          our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
36         # set the version for version checking
37          $VERSION     = 1.00;
38         @ISA    = qw(Exporter);
39         @EXPORT = qw(printpdf);
40 }
41
42
43 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
44 #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.
45 use constant mm => 25.4 / 72;
46 use constant in => 1 / 72;
47 use constant pt => 1;
48
49 use PDF::API2;
50 #A4 paper specs
51 my ($height, $width) = (297, 210);
52 use PDF::Table;
53
54 sub printorders {
55     my ($pdf, $basketgroup, $baskets, $orders) = @_;
56     
57     my $cur_format = C4::Context->preference("CurrencyFormat");
58     my $num;
59     
60     if ( $cur_format eq 'FR' ) {
61         $num = new Number::Format(
62             'decimal_fill'      => '2',
63             'decimal_point'     => ',',
64             'int_curr_symbol'   => '',
65             'mon_thousands_sep' => ' ',
66             'thousands_sep'     => ' ',
67             'mon_decimal_point' => ','
68         );
69     } else {  # US by default..
70         $num = new Number::Format(
71             'int_curr_symbol'   => '',
72             'mon_thousands_sep' => ',',
73             'mon_decimal_point' => '.'
74         );
75     }
76
77     $pdf->mediabox($height/mm, $width/mm);
78     my $number = 3;
79     for my $basket (@$baskets){
80         my $page = $pdf->page();
81         
82         # print basket header (box)
83         my $box = $page->gfx;
84         $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
85         $box->stroke;
86 #         $box->restore();
87         
88         # create a text
89         my $text = $page->text;
90         # add basketgroup number
91         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
92         $text->translate(20/mm,  ($height-15)/mm);
93         $text->text("Commande N°".$basketgroup->{'id'}.". Panier N° ".$basket->{basketno}.". ".$basket->{booksellernote});
94         
95         my $pdftable = new PDF::Table();
96         my $abaskets;
97         my $arrbasket;
98         my @keys = ('Document','Qté','Prix public TTC','Remise','Prix remisé HT','TVA', 'Total TTC'); 
99         for my $bkey (@keys) {
100             push(@$arrbasket, $bkey);
101         }
102         push(@$abaskets, $arrbasket);
103 #         @{$orders->{$basket->{basketno}}});
104         foreach my $line (@{$orders->{$basket->{basketno}}}) {
105             $arrbasket = undef;
106             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]));
107             push(@$abaskets, $arrbasket);
108         }
109         
110         $pdftable->table($pdf, $page, $abaskets,
111                                         x => 10/mm,
112                                         w => ($width - 20)/mm,
113                                         start_y => 270/mm,
114                                         next_y  => 285/mm,
115                                         start_h => 250/mm,
116                                         next_h  => 250/mm,
117                                         padding => 5,
118                                         padding_right => 5,
119                                         background_color_odd  => "lightgray",
120                                         font       => $pdf->corefont("Times", -encoding => "utf8"),
121                                         font_size => 3/mm,
122                                         header_props   =>    {
123                                             font       => $pdf->corefont("Times", -encoding => "utf8"),
124                                             font_size  => 10,
125                                             bg_color   => 'gray',
126                                             repeat     => 1,
127                                         },
128                                         column_props => [
129                                             {
130                                                 min_w => 100/mm,       # Minimum column width.
131                                             },
132                                             {
133                                                 justify => 'right', # One of left|right ,
134                                             },
135                                             {
136                                                 justify => 'right', # One of left|right ,
137                                             },
138                                             {
139                                                 justify => 'right', # One of left|right ,
140                                             },
141                                             {
142                                                 justify => 'right', # One of left|right ,
143                                             },
144                                             {
145                                                 justify => 'right', # One of left|right ,
146                                             },
147                                             {
148                                                 justify => 'right', # One of left|right ,
149                                             },
150                                         ],
151              );
152     }
153     $pdf->mediabox($width/mm, $height/mm);
154 }
155
156 sub printbaskets {
157     my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
158     
159     my $cur_format = C4::Context->preference("CurrencyFormat");
160     my $num;
161     
162     if ( $cur_format eq 'FR' ) {
163         $num = new Number::Format(
164             'decimal_fill'      => '2',
165             'decimal_point'     => ',',
166             'int_curr_symbol'   => '',
167             'mon_thousands_sep' => ' ',
168             'thousands_sep'     => ' ',
169             'mon_decimal_point' => ','
170         );
171     } else {  # US by default..
172         $num = new Number::Format(
173             'int_curr_symbol'   => '',
174             'mon_thousands_sep' => ',',
175             'mon_decimal_point' => '.'
176         );
177     }
178     
179     $pdf->mediabox($width/mm, $height/mm);
180     my $page = $pdf->openpage(2);
181     # create a text
182     my $text = $page->text;
183     # add basketgroup number
184     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
185     $text->translate(($width-40)/mm,  ($height-50)/mm);
186     $text->text("".$basketgroup->{'id'});
187     
188     my $pdftable = new PDF::Table();
189     my $abaskets;
190     my $arrbasket;
191     # header of the table
192     my @keys = ('Lot',  'Panier (N°)', 'Prix public TTC', 'Remise', 'Prix remisé','taux TVA', 'Total HT','TVA', 'Total TTC');
193     for my $bkey (@keys) {
194         push(@$arrbasket, $bkey);
195     }
196     my $grandtotal=0;
197     my $grandgst=0;
198     # calculate each basket total
199     push(@$abaskets, $arrbasket);
200     for my $basket (@$hbaskets) {
201         $arrbasket = undef;
202         my ($total, $gst, $totallist) = (0, 0, 0);
203         my $ords = $orders->{$basket->{basketno}};
204         my $ordlength = @$ords;
205         foreach my $ord (@$ords) {
206             $total += @$ord[5] * @$ord[7];
207             $gst   += (@$ord[5] * @$ord[7]) * $GSTrate/(1+$GSTrate);
208             $totallist += @$ord[5]*@$ord[6];
209         }
210         $total=$num->round($total);
211         $gst=$num->round($gst);
212         $grandtotal +=$total;
213         $grandgst +=$gst;
214         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));
215         push(@$abaskets, $arrbasket);
216     }
217     # now, push total
218     undef $arrbasket;
219     push @$arrbasket,'','','','Total',$num->format_price($grandtotal),'',$num->format_price($grandtotal-$grandgst), $num->format_price($grandgst),$num->format_price($grandtotal);
220     push @$abaskets,$arrbasket;
221     # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
222
223     $pdftable->table($pdf, $page, $abaskets,
224                                     x => 5/mm,
225                                     w => ($width - 10)/mm,
226                                     start_y =>  230/mm,
227                                     next_y  => 230/mm,
228                                     start_h => 230/mm,
229                                     next_h  => 230/mm,
230                                     font       => $pdf->corefont("Times", -encoding => "utf8"),
231                                     font_size => 3/mm,
232                                     padding => 5,
233                                     padding_right => 10,
234                                     background_color_odd  => "lightgray",
235                                     header_props   =>    {
236                                         bg_color   => 'gray',
237                                         repeat     => 1,
238                                     },
239                                     column_props => [
240                                         {
241                                         },
242                                         {
243                                         },
244                                         {
245                                             justify => 'right',
246                                         },
247                                         {
248                                             justify => 'right',
249                                         },
250                                         {
251                                             justify => 'right',
252                                         },
253                                         {
254                                             justify => 'right',
255                                         },
256                                         {
257                                             justify => 'right',
258                                         },
259                                         {
260                                             justify => 'right',
261                                         },
262                                         {
263                                             justify => 'right',
264                                         },
265                                     ],
266     );
267     $pdf->mediabox($height/mm, $width/mm);
268 }
269
270 sub printhead {
271     my ($pdf, $basketgroup, $bookseller, $branch) = @_;
272
273     # get branch details
274     my $branchdetails = GetBranchDetail( $basketgroup->{'deliveryplace'} );
275
276     # open 1st page (with the header)
277     my $page = $pdf->openpage(1);
278     
279     # create a text
280     my $text = $page->text;
281
282     # print order info, on the default PDF
283     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
284     $text->translate(100/mm,  ($height-5-48)/mm);
285     $text->text($basketgroup->{'id'});
286     
287     # print the date
288     my $today = C4::Dates->today();
289     $text->translate(130/mm,  ($height-5-48)/mm);
290     $text->text($today);
291     # print bookseller infos
292     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
293     $text->translate(110/mm,  ($height-170)/mm);
294     $text->text($bookseller->{name});
295     $text->translate(110/mm,  ($height-175)/mm);
296     $text->text($bookseller->{postal});
297     $text->translate(110/mm,  ($height-180)/mm);
298     $text->text($bookseller->{address1});
299     $text->translate(110/mm,  ($height-185)/mm);
300     $text->text($bookseller->{address2});
301     $text->translate(110/mm,  ($height-190)/mm);
302     $text->text($bookseller->{address3});
303     # print delivery infos
304     $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
305     $text->translate(50/mm,  ($height-230)/mm);
306     $text->text($branchdetails->{branchaddress1});
307     $text->translate(50/mm,  ($height-235)/mm);
308     $text->text($branchdetails->{branchaddress2});
309     $text->translate(50/mm,  ($height-240)/mm);
310     $text->text($branchdetails->{branchaddress3});
311     $text->translate(50/mm,  ($height-245)/mm);
312     $text->text($basketgroup->{'deliverycomment'});
313 }
314
315 sub printfooters {
316         my ($pdf) = @_;
317         for (my $i=1;$i <= $pdf->pages;$i++) {
318         my $page = $pdf->openpage($i);
319         my $text = $page->text;
320         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
321         $text->translate(10/mm,  10/mm);
322         $text->text("Page $i / ".$pdf->pages);
323         }
324 }
325
326 sub printpdf {
327     my ($basketgroup, $bookseller, $baskets, $branch, $orders, $GST) = @_;
328     # open the default PDF that will be used for base (1st page already filled)
329     my $pdf = PDF::API2->open('pdfformat/example.pdf');
330     $pdf->pageLabel( 0, {
331         -style => 'roman',
332     } ); # start with roman numbering
333     # fill the 1st page (basketgroup information)
334     printhead($pdf, $basketgroup, $bookseller, $branch);
335     # fill the 2nd page (orders summary)
336     printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
337     # fill other pages (orders)
338     printorders($pdf, $basketgroup, $baskets, $orders);
339     # print something on each page (usually the footer, but you could also put a header
340     printfooters($pdf);
341     return $pdf->stringify;
342 }
343
344 1;