(mantis 1475) uncertains price, don't set if we find a price in MARC
[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 <<<<<<< HEAD:acqui/pdfformat/example.pm
157     $pdf->mediabox($height/mm, $width/mm);
158     my $page = $pdf->page();
159 =======
160     
161     my $cur_format = C4::Context->preference("CurrencyFormat");
162     my $num;
163     
164     if ( $cur_format eq 'FR' ) {
165         $num = new Number::Format(
166             'decimal_fill'      => '2',
167             'decimal_point'     => ',',
168             'int_curr_symbol'   => '',
169             'mon_thousands_sep' => ' ',
170             'thousands_sep'     => ' ',
171             'mon_decimal_point' => ','
172         );
173     } else {  # US by default..
174         $num = new Number::Format(
175             'int_curr_symbol'   => '',
176             'mon_thousands_sep' => ',',
177             'mon_decimal_point' => '.'
178         );
179     }
180     
181     $pdf->mediabox($width/mm, $height/mm);
182     my $page = $pdf->openpage(2);
183     # create a text
184     my $text = $page->text;
185     # add basketgroup number
186     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
187     $text->translate(($width-40)/mm,  ($height-50)/mm);
188     $text->text("".$basketgroup->{'id'});
189     
190 >>>>>>> paul:acqui/pdfformat/example.pm
191     my $pdftable = new PDF::Table();
192     my $abaskets;
193     my $arrbasket;
194     # header of the table
195 <<<<<<< HEAD:acqui/pdfformat/example.pm
196     my @keys = ('Lot', 'Panier', 'Prix public', 'taux TVA', 'TVA', 'Remise', 'Total TTC');
197 =======
198     my @keys = ('Lot',  'Panier (N°)', 'Prix public TTC', 'Remise', 'Prix remisé','taux TVA', 'Total HT','TVA', 'Total TTC');
199 >>>>>>> paul:acqui/pdfformat/example.pm
200     for my $bkey (@keys) {
201         push(@$arrbasket, $bkey);
202     }
203     my $grandtotal=0;
204 <<<<<<< HEAD:acqui/pdfformat/example.pm
205 =======
206     my $grandgst=0;
207 >>>>>>> paul:acqui/pdfformat/example.pm
208     # calculate each basket total
209     push(@$abaskets, $arrbasket);
210     for my $basket (@$hbaskets) {
211         $arrbasket = undef;
212         my ($total, $gst, $totallist) = (0, 0, 0);
213         my $ords = $orders->{$basket->{basketno}};
214         my $ordlength = @$ords;
215 <<<<<<< HEAD:acqui/pdfformat/example.pm
216         for my $ord (@$ords[1..$ordlength]) {
217         warn "".@$ord[0]."=".@$ord[1]."=".@$ord[2]."=".@$ord[3]."=".@$ord[4]."=".@$ord[5]."=".@$ord[6]."=".@$ord[7]."=";
218             $total = $total + (@$ord[5] * @$ord[6]);
219             $gst   = $gst + ((@$ord[5] * @$ord[6]) * $GSTrate);
220             $totalttc = $totalttc+ @$ord[5] * @$ord[6] * (1 + $GSTrate) * (1 - $bookseller->{discount});
221         }
222         push(@$arrbasket, $basket->{contracname}, $basket->{basketname}, $total, $GSTrate, $gst, $bookseller->{discount}, $total + $gst);
223         push(@$abaskets, $arrbasket);
224     }
225     
226 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
227     my $text = $page->text(5/mm, 5/mm, ($height -10)/mm, ($width - 5)/mm);
228     $text->font( $pdf->corefont( 'Helvetica'), 8/mm );
229     my $txtstr = "REFERENCES DE COMMANDES DU BON DE COMMANDE N°".$basketgroup->{'id'};
230     my $txtwidth = $text->advancewidth($txtstr);
231     $text->translate(($height-$txtwidth*mm)/2/mm,  ($width-8-5)/mm);
232     $text->text($txtstr);
233 =======
234         foreach my $ord (@$ords) {
235             $total += @$ord[5] * @$ord[7];
236             $gst   += (@$ord[5] * @$ord[7]) * $GSTrate/(1+$GSTrate);
237             $totallist += @$ord[5]*@$ord[6];
238         }
239         $total=$num->round($total);
240         $gst=$num->round($gst);
241         $grandtotal +=$total;
242         $grandgst +=$gst;
243         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));
244         push(@$abaskets, $arrbasket);
245     }
246     # now, push total
247     undef $arrbasket;
248     push @$arrbasket,'','','','Total',$num->format_price($grandtotal),'',$num->format_price($grandtotal-$grandgst), $num->format_price($grandgst),$num->format_price($grandtotal);
249     push @$abaskets,$arrbasket;
250     # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
251 >>>>>>> paul:acqui/pdfformat/example.pm
252
253     $pdftable->table($pdf, $page, $abaskets,
254                                     x => 5/mm,
255                                     w => ($width - 10)/mm,
256                                     start_y =>  230/mm,
257                                     next_y  => 230/mm,
258                                     start_h => 230/mm,
259                                     next_h  => 230/mm,
260                                     font       => $pdf->corefont("Times", -encoding => "utf8"),
261                                     font_size => 3/mm,
262                                     padding => 5,
263                                     padding_right => 10,
264                                     background_color_odd  => "lightgray",
265                                     header_props   =>    {
266                                         bg_color   => 'gray',
267                                         repeat     => 1,
268                                     },
269                                     column_props => [
270                                         {
271                                         },
272                                         {
273                                         },
274                                         {
275                                             justify => 'right',
276                                         },
277                                         {
278                                             justify => 'right',
279                                         },
280                                         {
281                                             justify => 'right',
282                                         },
283                                         {
284                                             justify => 'right',
285                                         },
286                                         {
287                                             justify => 'right',
288                                         },
289                                         {
290                                             justify => 'right',
291                                         },
292                                         {
293                                             justify => 'right',
294                                         },
295                                     ],
296     );
297     $pdf->mediabox($height/mm, $width/mm);
298 }
299
300 sub printhead {
301     my ($pdf, $basketgroup, $bookseller, $branch) = @_;
302     # open 1st page (with the header)
303     my $page = $pdf->openpage(1);
304     
305     # create a text
306     my $text = $page->text;
307
308     # print order info, on the default PDF
309     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
310     my $textstr = "Bon de commande N°  $basketgroup->{'id'}";
311     $txtwidth = $text->advancewidth( $textstr );
312     $text->translate(($width-$txtwidth*mm)/2/mm,  ($height-5-48)/mm);
313     $text->text($textstr);
314
315     $box->rectxy(5/mm, ($height - 60)/mm, ($width-5)/mm, ($height-230)/mm);
316     $box->stroke;
317     $box->restore();
318
319     $text->font( $pdf->corefont( 'Helvetica', -encoding => 'utf8', -encode => 'utf8'), 4/mm );
320     $textstr = $branch->{branchaddress1};
321     $txtwidth = $text->advancewidth( $textstr );
322     $text->translate(7/mm,  ($height - 60 - 6)/mm);
323     $text->text($textstr);
324     
325     # print the date
326     my $today = C4::Dates->today();
327     $text->translate(130/mm,  ($height-5-48)/mm);
328     $text->text($today);
329     # print bookseller infos
330     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
331     $text->translate(110/mm,  ($height-170)/mm);
332     $text->text($bookseller->{name});
333     $text->translate(110/mm,  ($height-175)/mm);
334     $text->text($bookseller->{postal});
335     $text->translate(110/mm,  ($height-180)/mm);
336     $text->text($bookseller->{address1});
337     $text->translate(110/mm,  ($height-185)/mm);
338     $text->text($bookseller->{address2});
339     $text->translate(110/mm,  ($height-190)/mm);
340     $text->text($bookseller->{address3});
341 }
342
343 sub printfooters {
344         my ($pdf) = @_;
345         for (my $i=1;$i <= $pdf->pages;$i++) {
346         my $page = $pdf->openpage($i);
347         my $text = $page->text;
348         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
349         $text->translate(10/mm,  10/mm);
350         $text->text("Page $i / ".$pdf->pages);
351         }
352 }
353
354 sub printpdf {
355     my ($basketgroup, $bookseller, $baskets, $branch, $orders, $GST) = @_;
356     # open the default PDF that will be used for base (1st page already filled)
357     my $pdf = PDF::API2->open('pdfformat/example.pdf');
358     $pdf->pageLabel( 0, {
359         -style => 'roman',
360     } ); # start with roman numbering
361     # fill the 1st page (basketgroup information)
362     printhead($pdf, $basketgroup, $bookseller, $branch);
363 <<<<<<< HEAD:acqui/pdfformat/example.pm
364     printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
365     printorders($pdf, $basketgroup, $baskets, $orders);
366 =======
367     # fill the 2nd page (orders summary)
368     printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
369     # fill other pages (orders)
370     printorders($pdf, $basketgroup, $baskets, $orders);
371     # print something on each page (usually the footer, but you could also put a header
372     printfooters($pdf);
373 >>>>>>> paul:acqui/pdfformat/example.pm
374     return $pdf->stringify;
375 }
376
377 1;