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