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);
31 use C4::Branch qw(GetBranchDetail);
35 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
36 # set the version for version checking
39 @EXPORT = qw(printpdf);
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;
51 my ($height, $width) = (297, 210);
55 my ($pdf, $basketgroup, $baskets, $orders) = @_;
57 my $cur_format = C4::Context->preference("CurrencyFormat");
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' => ','
69 } else { # US by default..
70 $num = new Number::Format(
71 'int_curr_symbol' => '',
72 'mon_thousands_sep' => ',',
73 'mon_decimal_point' => '.'
77 $pdf->mediabox($height/mm, $width/mm);
79 for my $basket (@$baskets){
80 my $page = $pdf->page();
82 # print basket header (box)
84 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
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});
95 my $pdftable = new PDF::Table();
98 my @keys = ('Document','Qté','Prix public TTC','Remise','Prix remisé HT','TVA', 'Total TTC');
99 for my $bkey (@keys) {
100 push(@$arrbasket, $bkey);
102 push(@$abaskets, $arrbasket);
103 # @{$orders->{$basket->{basketno}}});
104 foreach my $line (@{$orders->{$basket->{basketno}}}) {
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);
110 $pdftable->table($pdf, $page, $abaskets,
112 w => ($width - 20)/mm,
119 background_color_odd => "lightgray",
120 font => $pdf->corefont("Times", -encoding => "utf8"),
123 font => $pdf->corefont("Times", -encoding => "utf8"),
130 min_w => 100/mm, # Minimum column width.
133 justify => 'right', # One of left|right ,
136 justify => 'right', # One of left|right ,
139 justify => 'right', # One of left|right ,
142 justify => 'right', # One of left|right ,
145 justify => 'right', # One of left|right ,
148 justify => 'right', # One of left|right ,
153 $pdf->mediabox($width/mm, $height/mm);
157 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
159 my $cur_format = C4::Context->preference("CurrencyFormat");
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' => ','
171 } else { # US by default..
172 $num = new Number::Format(
173 'int_curr_symbol' => '',
174 'mon_thousands_sep' => ',',
175 'mon_decimal_point' => '.'
179 $pdf->mediabox($width/mm, $height/mm);
180 my $page = $pdf->openpage(2);
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'});
188 my $pdftable = new PDF::Table();
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);
198 # calculate each basket total
199 push(@$abaskets, $arrbasket);
200 for my $basket (@$hbaskets) {
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];
210 $total=$num->round($total);
211 $gst=$num->round($gst);
212 $grandtotal +=$total;
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);
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.
223 $pdftable->table($pdf, $page, $abaskets,
225 w => ($width - 10)/mm,
230 font => $pdf->corefont("Times", -encoding => "utf8"),
234 background_color_odd => "lightgray",
267 $pdf->mediabox($height/mm, $width/mm);
271 my ($pdf, $basketgroup, $bookseller, $branch) = @_;
274 my $branchdetails = GetBranchDetail( $basketgroup->{'deliveryplace'} );
276 # open 1st page (with the header)
277 my $page = $pdf->openpage(1);
280 my $text = $page->text;
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'});
288 my $today = C4::Dates->today();
289 $text->translate(130/mm, ($height-5-48)/mm);
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'});
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);
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, {
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
341 return $pdf->stringify;