3 #example script to print a basketgroup
4 #written 07/11/08 by john.soros@biblibre.com and paul.poulain@biblibre.com
6 # Copyright 2008-2009 BibLibre SARL
8 # This file is part of Koha.
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
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.
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
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);
33 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
34 # set the version for version checking
37 @EXPORT = qw(printpdf);
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;
49 my ($height, $width) = (297, 210);
53 my ($pdf, $basketgroup, $baskets, $orders) = @_;
55 my $cur_format = C4::Context->preference("CurrencyFormat");
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' => ','
67 } else { # US by default..
68 $num = new Number::Format(
69 'int_curr_symbol' => '',
70 'mon_thousands_sep' => ',',
71 'mon_decimal_point' => '.'
75 $pdf->mediabox($height/mm, $width/mm);
77 for my $basket (@$baskets){
78 my $page = $pdf->page();
80 # print basket header (box)
82 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
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});
93 my $pdftable = new PDF::Table();
96 my @keys = ('Document','Qté','Prix public TTC','Remise','Prix remisé HT','TVA', 'Total TTC');
97 for my $bkey (@keys) {
98 push(@$arrbasket, $bkey);
100 push(@$abaskets, $arrbasket);
101 # @{$orders->{$basket->{basketno}}});
102 foreach my $line (@{$orders->{$basket->{basketno}}}) {
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);
108 $pdftable->table($pdf, $page, $abaskets,
110 w => ($width - 20)/mm,
117 background_color_odd => "lightgray",
118 font => $pdf->corefont("Times", -encoding => "utf8"),
121 font => $pdf->corefont("Times", -encoding => "utf8"),
128 min_w => 100/mm, # Minimum column width.
131 justify => 'right', # One of left|right ,
134 justify => 'right', # One of left|right ,
137 justify => 'right', # One of left|right ,
140 justify => 'right', # One of left|right ,
143 justify => 'right', # One of left|right ,
146 justify => 'right', # One of left|right ,
151 $pdf->mediabox($width/mm, $height/mm);
155 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
157 my $cur_format = C4::Context->preference("CurrencyFormat");
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' => ','
169 } else { # US by default..
170 $num = new Number::Format(
171 'int_curr_symbol' => '',
172 'mon_thousands_sep' => ',',
173 'mon_decimal_point' => '.'
177 $pdf->mediabox($width/mm, $height/mm);
178 my $page = $pdf->openpage(2);
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'});
186 my $pdftable = new PDF::Table();
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);
196 # calculate each basket total
197 push(@$abaskets, $arrbasket);
198 for my $basket (@$hbaskets) {
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];
208 $total=$num->round($total);
209 $gst=$num->round($gst);
210 $grandtotal +=$total;
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);
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.
221 $pdftable->table($pdf, $page, $abaskets,
223 w => ($width - 10)/mm,
228 font => $pdf->corefont("Times", -encoding => "utf8"),
232 background_color_odd => "lightgray",
265 $pdf->mediabox($height/mm, $width/mm);
269 my ($pdf, $basketgroup, $bookseller, $branch) = @_;
270 # open 1st page (with the header)
271 my $page = $pdf->openpage(1);
274 my $text = $page->text;
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'});
282 my $today = C4::Dates->today();
283 $text->translate(130/mm, ($height-5-48)/mm);
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});
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);
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, {
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
325 return $pdf->stringify;