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
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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::layout3pages;
25 use vars qw($VERSION @ISA @EXPORT);
26 use Number::Format qw(format_price);
28 use List::MoreUtils qw/uniq/;
33 use C4::Branch qw(GetBranchDetail GetBranchName);
37 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
38 # set the version for version checking
41 @EXPORT = qw(printpdf);
45 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
46 #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.
47 use constant mm => 25.4 / 72;
48 use constant in => 1 / 72;
53 my ($height, $width) = (297, 210);
57 my ($pdf, $basketgroup, $baskets, $orders) = @_;
59 my $cur_format = C4::Context->preference("CurrencyFormat");
62 if ( $cur_format eq 'FR' ) {
63 $num = new Number::Format(
64 'decimal_fill' => '2',
65 'decimal_point' => ',',
66 'int_curr_symbol' => '',
67 'mon_thousands_sep' => ' ',
68 'thousands_sep' => ' ',
69 'mon_decimal_point' => ','
71 } else { # US by default..
72 $num = new Number::Format(
73 'int_curr_symbol' => '',
74 'mon_thousands_sep' => ',',
75 'mon_decimal_point' => '.'
79 $pdf->mediabox($height/mm, $width/mm);
81 for my $basket (@$baskets){
82 my $page = $pdf->page();
84 # print basket header (box)
86 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
91 my $text = $page->text;
92 # add basketgroup number
93 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
94 $text->translate(20/mm, ($height-15)/mm);
95 $text->text("Order no. ".$basketgroup->{'id'}.". Basket no. ".$basket->{basketno}.". ".$basket->{booksellernote});
96 $text->translate(20/mm, ($height-20)/mm);
97 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
98 $text->text( ( $basket->{billingplace} ? "Billing at " . C4::Branch::GetBranchName( $basket->{billingplace} ) : "" )
99 . ( $basket->{billingplace} and $basket->{deliveryplace} ? " and " : "" )
100 . ( $basket->{deliveryplace} ? "delivery at " . C4::Branch::GetBranchName( $basket->{deliveryplace}) : "" )
103 my $pdftable = new PDF::Table();
106 my @keys = ('Document', 'Qty', 'RRP tax exc.', 'RRP tax inc.', 'Discount', 'Discount price', 'GST rate', 'Total tax exc.', 'Total tax inc.');
107 for my $bkey (@keys) {
108 push(@$arrbasket, $bkey);
110 push(@$abaskets, $arrbasket);
113 foreach my $line (@{$orders->{$basket->{basketno}}}) {
116 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
117 $titleinfo = $line->{title} . " / " . $line->{author} .
118 ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
119 ( $line->{en} ? " EN: " . $line->{en} : '' ) .
120 ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
121 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
122 ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
123 ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
125 else { # MARC21, NORMARC
126 $titleinfo = $line->{title} . " " . $line->{author} .
127 ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
128 ( $line->{en} ? " EN: " . $line->{en} : '' ) .
129 ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
130 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
131 ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
132 ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
135 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote for vendor : " . $line->{order_vendornote} : '' ),
137 $num->format_price($line->{rrpgste}),
138 $num->format_price($line->{rrpgsti}),
139 $num->format_price($line->{discount}).'%',
140 $num->format_price($line->{rrpgste} - $line->{ecostgste}),
141 $num->format_price($line->{gstrate} * 100).'%',
142 $num->format_price($line->{totalgste}),
143 $num->format_price($line->{totalgsti}),
145 push(@$abaskets, $arrbasket);
148 $pdftable->table($pdf, $page, $abaskets,
150 w => ($width - 20)/mm,
157 background_color_odd => "lightgray",
158 font => $pdf->corefont("Times", -encoding => "utf8"),
161 font => $pdf->corefont("Times", -encoding => "utf8"),
168 min_w => 85/mm, # Minimum column width.
171 justify => 'right', # One of left|right ,
174 justify => 'right', # One of left|right ,
177 justify => 'right', # One of left|right ,
180 justify => 'right', # One of left|right ,
183 justify => 'right', # One of left|right ,
186 justify => 'right', # One of left|right ,
189 justify => 'right', # One of left|right ,
192 justify => 'right', # One of left|right ,
197 $pdf->mediabox($width/mm, $height/mm);
201 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
204 my $libraryname = C4::Context->preference("LibraryName");
206 my $cur_format = C4::Context->preference("CurrencyFormat");
209 if ( $cur_format eq 'FR' ) {
210 $num = new Number::Format(
211 'decimal_fill' => '2',
212 'decimal_point' => ',',
213 'int_curr_symbol' => '',
214 'mon_thousands_sep' => ' ',
215 'thousands_sep' => ' ',
216 'mon_decimal_point' => ','
218 } else { # US by default..
219 $num = new Number::Format(
220 'int_curr_symbol' => '',
221 'mon_thousands_sep' => ',',
222 'mon_decimal_point' => '.'
226 $pdf->mediabox($width/mm, $height/mm);
227 my $page = $pdf->openpage(2);
229 my $text = $page->text;
231 # add basketgroup number
232 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
233 $text->translate(($width-40)/mm, ($height-53)/mm);
234 $text->text("".$basketgroup->{'id'});
235 # print the libraryname in the header
236 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
237 $text->translate(30/mm, ($height-28.5)/mm);
238 $text->text($libraryname);
239 my $pdftable = new PDF::Table();
242 # header of the table
243 my @keys = ('Lot', 'Basket (No.)','Total RRP tax exc.', 'Total RRP tax inc.', 'GST rate', 'GST', 'Total discount', 'Total tax exc.', 'Total tax inc.');
244 for my $bkey (@keys) {
245 push(@$arrbasket, $bkey);
247 my ($grandtotalrrpgsti, $grandtotalrrpgste, $grandtotalgsti, $grandtotalgste, $grandtotalgstvalue, $grandtotaldiscount);
248 # calculate each basket total
249 push(@$abaskets, $arrbasket);
250 for my $basket (@$hbaskets) {
253 my ($totalrrpgste, $totalrrpgsti, $totalgste, $totalgsti, $totalgstvalue, $totaldiscount);
254 my $ords = $orders->{$basket->{basketno}};
255 my $ordlength = @$ords;
256 foreach my $ord (@$ords) {
257 $totalgste += $ord->{totalgste};
258 $totalgsti += $ord->{totalgsti};
259 $totalgstvalue += $ord->{gstvalue};
260 $totaldiscount += ($ord->{rrpgste} - $ord->{ecostgste} ) * $ord->{quantity};
261 $totalrrpgste += $ord->{rrpgste} * $ord->{quantity};
262 $totalrrpgsti += $ord->{rrpgsti} * $ord->{quantity};
263 push @gst, $ord->{gstrate};
265 @gst = uniq map { $_ * 100 } @gst;
266 $totalgsti = $num->round($totalgsti);
267 $totalgste = $num->round($totalgste);
268 $grandtotalrrpgste += $totalrrpgste;
269 $grandtotalrrpgsti += $totalrrpgsti;
270 $grandtotalgsti += $totalgsti;
271 $grandtotalgste += $totalgste;
272 $grandtotalgstvalue += $totalgstvalue;
273 $grandtotaldiscount += $totaldiscount;
274 my @gst_string = map{$num->format_price( $_ ) . '%'} @gst;
276 $basket->{contractname},
277 $basket->{basketname} . ' (No. ' . $basket->{basketno} . ')',
278 $num->format_price($totalrrpgste),
279 $num->format_price($totalrrpgsti),
281 $num->format_price($totalgstvalue),
282 $num->format_price($totaldiscount),
283 $num->format_price($totalgste),
284 $num->format_price($totalgsti)
286 push(@$abaskets, $arrbasket);
290 push @$arrbasket,'','Total', $num->format_price($grandtotalrrpgste), $num->format_price($grandtotalrrpgsti), '', $num->format_price($grandtotalgstvalue), $num->format_price($grandtotaldiscount), $num->format_price($grandtotalgste), $num->format_price($grandtotalgsti);
291 push @$abaskets,$arrbasket;
292 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
294 $pdftable->table($pdf, $page, $abaskets,
296 w => ($width - 10)/mm,
301 font => $pdf->corefont("Times", -encoding => "utf8"),
305 background_color_odd => "lightgray",
332 $pdf->mediabox($height/mm, $width/mm);
336 my ($pdf, $basketgroup, $bookseller) = @_;
339 my $libraryname = C4::Context->preference("LibraryName");
341 my $billingdetails = GetBranchDetail( $basketgroup->{billingplace} );
342 my $deliverydetails = GetBranchDetail( $basketgroup->{deliveryplace} );
343 my $freedeliveryplace = $basketgroup->{freedeliveryplace};
347 # open 1st page (with the header)
348 my $page = $pdf->openpage(1);
351 my $text = $page->text;
353 # print the libraryname in the header
354 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
355 $text->translate(30/mm, ($height-28.5)/mm);
356 $text->text($libraryname);
358 # print order info, on the default PDF
359 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
360 $text->translate(100/mm, ($height-5-48)/mm);
361 $text->text($basketgroup->{'id'});
364 my $today = C4::Dates->today();
365 $text->translate(130/mm, ($height-5-48)/mm);
368 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
370 # print billing infos
371 $text->translate(100/mm, ($height-86)/mm);
372 $text->text($libraryname);
373 $text->translate(100/mm, ($height-97)/mm);
374 $text->text($billingdetails->{branchname});
375 $text->translate(100/mm, ($height-108.5)/mm);
376 $text->text($billingdetails->{branchphone});
377 $text->translate(100/mm, ($height-115.5)/mm);
378 $text->text($billingdetails->{branchfax});
379 $text->translate(100/mm, ($height-122.5)/mm);
380 $text->text($billingdetails->{branchaddress1});
381 $text->translate(100/mm, ($height-127.5)/mm);
382 $text->text($billingdetails->{branchaddress2});
383 $text->translate(100/mm, ($height-132.5)/mm);
384 $text->text($billingdetails->{branchaddress3});
385 $text->translate(100/mm, ($height-137.5)/mm);
386 $text->text(join(' ', $billingdetails->{branchzip}, $billingdetails->{branchcity}, $billingdetails->{branchcountry}));
387 $text->translate(100/mm, ($height-147.5)/mm);
388 $text->text($billingdetails->{branchemail});
391 $text->translate(100/mm, ($height-145.5)/mm);
392 $text->text($subject);
394 # print bookseller infos
395 $text->translate(100/mm, ($height-180)/mm);
396 $text->text($bookseller->{name});
397 $text->translate(100/mm, ($height-185)/mm);
398 $text->text($bookseller->{postal});
399 $text->translate(100/mm, ($height-190)/mm);
400 $text->text($bookseller->{address1});
401 $text->translate(100/mm, ($height-195)/mm);
402 $text->text($bookseller->{address2});
403 $text->translate(100/mm, ($height-200)/mm);
404 $text->text($bookseller->{address3});
405 $text->translate(100/mm, ($height-205)/mm);
406 $text->text($bookseller->{accountnumber});
408 # print delivery infos
409 $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
410 $text->translate(50/mm, ($height-237)/mm);
411 if ($freedeliveryplace) {
413 my @fdp = split('\n', $freedeliveryplace);
416 $text->translate( 50 / mm, ( $height - $start ) / mm );
420 $text->text($deliverydetails->{branchaddress1});
421 $text->translate(50/mm, ($height-242)/mm);
422 $text->text($deliverydetails->{branchaddress2});
423 $text->translate(50/mm, ($height-247)/mm);
424 $text->text($deliverydetails->{branchaddress3});
425 $text->translate(50/mm, ($height-252)/mm);
426 $text->text(join(' ', $deliverydetails->{branchzip}, $deliverydetails->{branchcity}, $deliverydetails->{branchcountry}));
428 $text->translate(50/mm, ($height-262)/mm);
429 $text->text($basketgroup->{deliverycomment});
434 for (my $i=1;$i <= $pdf->pages;$i++) {
435 my $page = $pdf->openpage($i);
436 my $text = $page->text;
437 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
438 $text->translate(10/mm, 10/mm);
439 $text->text("Page $i / ".$pdf->pages);
444 my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
445 # open the default PDF that will be used for base (1st page already filled)
446 my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pages.pdf';
447 my $pdf = PDF::API2->open($pdf_template);
448 $pdf->pageLabel( 0, {
450 } ); # start with roman numbering
451 # fill the 1st page (basketgroup information)
452 printhead($pdf, $basketgroup, $bookseller);
453 # fill the 2nd page (orders summary)
454 printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
455 # fill other pages (orders)
456 printorders($pdf, $basketgroup, $baskets, $orders);
457 # print something on each page (usually the footer, but you could also put a header
459 return $pdf->stringify;