Bug 16011: $VERSION - remove use vars $VERSION
[koha.git] / acqui / pdfformat / layout3pages.pm
bloba35af190fb737f2ef980dc814d21855c32727928
1 #!/usr/bin/perl
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
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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(@ISA @EXPORT);
26 use MIME::Base64;
27 use List::MoreUtils qw/uniq/;
28 use strict;
29 use warnings;
30 use utf8;
32 use C4::Branch qw(GetBranchName);
34 use Koha::Number::Price;
35 use Koha::DateUtils;
36 use Koha::Libraries;
38 BEGIN {
39 use Exporter ();
40 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
41 # set the version for version checking
42 $VERSION = 1.00;
43 @ISA = qw(Exporter);
44 @EXPORT = qw(printpdf);
48 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
49 #The constants exported transform 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.
50 use constant mm => 25.4 / 72;
51 use constant in => 1 / 72;
52 use constant pt => 1;
54 use PDF::API2;
55 #A4 paper specs
56 my ($height, $width) = (297, 210);
57 use PDF::Table;
59 sub printorders {
60 my ($pdf, $basketgroup, $baskets, $orders) = @_;
62 my $cur_format = C4::Context->preference("CurrencyFormat");
64 $pdf->mediabox($height/mm, $width/mm);
65 my $number = 3;
66 for my $basket (@$baskets){
67 my $page = $pdf->page();
69 # print basket header (box)
70 my $box = $page->gfx;
71 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
72 $box->stroke;
73 # $box->restore();
75 # create a text
76 my $text = $page->text;
77 # add basketgroup number
78 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
79 $text->translate(20/mm, ($height-15)/mm);
80 $text->text("Order no. ".$basketgroup->{'id'}.". Basket no. ".$basket->{basketno}.". ".$basket->{booksellernote});
81 $text->translate(20/mm, ($height-20)/mm);
82 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
83 $text->text( ( $basket->{billingplace} ? "Billing at " . C4::Branch::GetBranchName( $basket->{billingplace} ) : "" )
84 . ( $basket->{billingplace} and $basket->{deliveryplace} ? " and " : "" )
85 . ( $basket->{deliveryplace} ? "delivery at " . C4::Branch::GetBranchName( $basket->{deliveryplace}) : "" )
88 my $pdftable = new PDF::Table();
89 my $abaskets;
90 my $arrbasket;
91 my @keys = ('Document', 'Qty', 'RRP tax exc.', 'RRP tax inc.', 'Discount', 'Discount price', 'GST rate', 'Total tax exc.', 'Total tax inc.');
92 for my $bkey (@keys) {
93 push(@$arrbasket, $bkey);
95 push(@$abaskets, $arrbasket);
97 my $titleinfo;
98 foreach my $line (@{$orders->{$basket->{basketno}}}) {
99 $arrbasket = undef;
100 $titleinfo = "";
101 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
102 $titleinfo = $line->{title} . " / " . $line->{author} .
103 ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
104 ( $line->{en} ? " EN: " . $line->{en} : '' ) .
105 ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
106 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
107 ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
108 ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
110 else { # MARC21, NORMARC
111 $titleinfo = $line->{title} . " " . $line->{author} .
112 ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
113 ( $line->{en} ? " EN: " . $line->{en} : '' ) .
114 ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
115 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
116 ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
117 ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
119 push( @$arrbasket,
120 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote for vendor : " . $line->{order_vendornote} : '' ),
121 $line->{quantity},
122 Koha::Number::Price->new( $line->{rrpgste} )->format,
123 Koha::Number::Price->new( $line->{rrpgsti} )->format,
124 Koha::Number::Price->new( $line->{discount} )->format . '%',
125 Koha::Number::Price->new( $line->{rrpgste} - $line->{ecostgste})->format,
126 Koha::Number::Price->new( $line->{gstrate} * 100 )->format . '%',
127 Koha::Number::Price->new( $line->{totalgste} )->format,
128 Koha::Number::Price->new( $line->{totalgsti} )->format,
130 push(@$abaskets, $arrbasket);
133 $pdftable->table($pdf, $page, $abaskets,
134 x => 10/mm,
135 w => ($width - 20)/mm,
136 start_y => 270/mm,
137 next_y => 285/mm,
138 start_h => 250/mm,
139 next_h => 250/mm,
140 padding => 5,
141 padding_right => 5,
142 background_color_odd => "lightgray",
143 font => $pdf->corefont("Times", -encoding => "utf8"),
144 font_size => 3/mm,
145 header_props => {
146 font => $pdf->corefont("Times", -encoding => "utf8"),
147 font_size => 9,
148 bg_color => 'gray',
149 repeat => 1,
151 column_props => [
153 min_w => 85/mm, # Minimum column width.
156 justify => 'right', # One of left|right ,
159 justify => 'right', # One of left|right ,
162 justify => 'right', # One of left|right ,
165 justify => 'right', # One of left|right ,
168 justify => 'right', # One of left|right ,
171 justify => 'right', # One of left|right ,
174 justify => 'right', # One of left|right ,
177 justify => 'right', # One of left|right ,
182 $pdf->mediabox($width/mm, $height/mm);
185 sub printbaskets {
186 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
188 # get library name
189 my $libraryname = C4::Context->preference("LibraryName");
191 my $cur_format = C4::Context->preference("CurrencyFormat");
193 $pdf->mediabox($width/mm, $height/mm);
194 my $page = $pdf->openpage(2);
195 # create a text
196 my $text = $page->text;
198 # add basketgroup number
199 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
200 $text->translate(($width-40)/mm, ($height-53)/mm);
201 $text->text("".$basketgroup->{'id'});
202 # print the libraryname in the header
203 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
204 $text->translate(30/mm, ($height-28.5)/mm);
205 $text->text($libraryname);
206 my $pdftable = new PDF::Table();
207 my $abaskets;
208 my $arrbasket;
209 # header of the table
210 my @keys = ('Lot', 'Basket (No.)','Total RRP tax exc.', 'Total RRP tax inc.', 'GST rate', 'GST', 'Total discount', 'Total tax exc.', 'Total tax inc.');
211 for my $bkey (@keys) {
212 push(@$arrbasket, $bkey);
214 my ($grandtotalrrpgsti, $grandtotalrrpgste, $grandtotalgsti, $grandtotalgste, $grandtotalgstvalue, $grandtotaldiscount);
215 # calculate each basket total
216 push(@$abaskets, $arrbasket);
217 for my $basket (@$hbaskets) {
218 my @gst;
219 $arrbasket = undef;
220 my ($totalrrpgste, $totalrrpgsti, $totalgste, $totalgsti, $totalgstvalue, $totaldiscount);
221 my $ords = $orders->{$basket->{basketno}};
222 my $ordlength = @$ords;
223 foreach my $ord (@$ords) {
224 $totalgste += $ord->{totalgste};
225 $totalgsti += $ord->{totalgsti};
226 $totalgstvalue += $ord->{gstvalue};
227 $totaldiscount += ($ord->{rrpgste} - $ord->{ecostgste} ) * $ord->{quantity};
228 $totalrrpgste += $ord->{rrpgste} * $ord->{quantity};
229 $totalrrpgsti += $ord->{rrpgsti} * $ord->{quantity};
230 push @gst, $ord->{gstrate};
232 @gst = uniq map { $_ * 100 } @gst;
233 $grandtotalrrpgste += $totalrrpgste;
234 $grandtotalrrpgsti += $totalrrpgsti;
235 $grandtotalgsti += $totalgsti;
236 $grandtotalgste += $totalgste;
237 $grandtotalgstvalue += $totalgstvalue;
238 $grandtotaldiscount += $totaldiscount;
239 my @gst_string =
240 map { Koha::Number::Price->new($_)->format . '%' } @gst;
241 push(@$arrbasket,
242 $basket->{contractname},
243 $basket->{basketname} . ' (No. ' . $basket->{basketno} . ')',
244 Koha::Number::Price->new( $totalrrpgste )->format,
245 Koha::Number::Price->new( $totalrrpgsti )->format,
246 "@gst_string",
247 Koha::Number::Price->new( $totalgstvalue )->format,
248 Koha::Number::Price->new( $totaldiscount )->format,
249 Koha::Number::Price->new( $totalgste )->format,
250 Koha::Number::Price->new( $totalgsti )->format,
252 push(@$abaskets, $arrbasket);
254 # now, push total
255 undef $arrbasket;
256 push @$arrbasket,
258 'Total',
259 Koha::Number::Price->new( $grandtotalrrpgste )->format,
260 Koha::Number::Price->new( $grandtotalrrpgsti )->format,
262 Koha::Number::Price->new( $grandtotalgstvalue )->format,
263 Koha::Number::Price->new( $grandtotaldiscount )->format,
264 Koha::Number::Price->new( $grandtotalgste )->format,
265 Koha::Number::Price->new( $grandtotalgsti )->format;
266 push @$abaskets,$arrbasket;
267 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
269 $pdftable->table($pdf, $page, $abaskets,
270 x => 5/mm,
271 w => ($width - 10)/mm,
272 start_y => 230/mm,
273 next_y => 230/mm,
274 start_h => 230/mm,
275 next_h => 230/mm,
276 font => $pdf->corefont("Times", -encoding => "utf8"),
277 font_size => 3/mm,
278 padding => 5,
279 padding_right => 10,
280 background_color_odd => "lightgray",
281 header_props => {
282 bg_color => 'gray',
283 repeat => 1,
285 column_props => [
291 justify => 'right',
294 justify => 'right',
297 justify => 'right',
300 justify => 'right',
303 justify => 'right',
307 $pdf->mediabox($height/mm, $width/mm);
310 sub printhead {
311 my ($pdf, $basketgroup, $bookseller) = @_;
313 # get library name
314 my $libraryname = C4::Context->preference("LibraryName");
315 my $billing_library = Koha::Libraries->find( $basketgroup->{billingplace} );
316 my $delivery_library = Koha::Libraries->find( $basketgroup->{deliveryplace} );
317 my $freedeliveryplace = $basketgroup->{freedeliveryplace};
318 # get the subject
319 my $subject;
321 # open 1st page (with the header)
322 my $page = $pdf->openpage(1);
324 # create a text
325 my $text = $page->text;
327 # print the libraryname in the header
328 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
329 $text->translate(30/mm, ($height-28.5)/mm);
330 $text->text($libraryname);
332 # print order info, on the default PDF
333 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
334 $text->translate(100/mm, ($height-5-48)/mm);
335 $text->text($basketgroup->{'id'});
337 # print the date
338 my $today = output_pref({ dt => dt_from_string, dateonly => 1 });
339 $text->translate(130/mm, ($height-5-48)/mm);
340 $text->text($today);
342 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
344 # print billing infos
345 $text->translate(100/mm, ($height-86)/mm);
346 $text->text($libraryname);
347 $text->translate(100/mm, ($height-97)/mm);
348 $text->text($billing_library->branchname);
349 $text->translate(100/mm, ($height-108.5)/mm);
350 $text->text($billing_library->branchphone);
351 $text->translate(100/mm, ($height-115.5)/mm);
352 $text->text($billing_library->branchfax);
353 $text->translate(100/mm, ($height-122.5)/mm);
354 $text->text($billing_library->branchaddress1);
355 $text->translate(100/mm, ($height-127.5)/mm);
356 $text->text($billing_library->branchaddress2);
357 $text->translate(100/mm, ($height-132.5)/mm);
358 $text->text($billing_library->branchaddress3);
359 $text->translate(100/mm, ($height-137.5)/mm);
360 $text->text(join(' ', $billing_library->branchzip, $billing_library->branchcity, $billing_library->branchcountry));
361 $text->translate(100/mm, ($height-147.5)/mm);
362 $text->text($billing_library->branchemail);
364 # print subject
365 $text->translate(100/mm, ($height-145.5)/mm);
366 $text->text($subject);
368 # print bookseller infos
369 $text->translate(100/mm, ($height-180)/mm);
370 $text->text($bookseller->{name});
371 $text->translate(100/mm, ($height-185)/mm);
372 $text->text($bookseller->{postal});
373 $text->translate(100/mm, ($height-190)/mm);
374 $text->text($bookseller->{address1});
375 $text->translate(100/mm, ($height-195)/mm);
376 $text->text($bookseller->{address2});
377 $text->translate(100/mm, ($height-200)/mm);
378 $text->text($bookseller->{address3});
379 $text->translate(100/mm, ($height-205)/mm);
380 $text->text($bookseller->{accountnumber});
382 # print delivery infos
383 $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
384 $text->translate(50/mm, ($height-237)/mm);
385 if ($freedeliveryplace) {
386 my $start = 242;
387 my @fdp = split('\n', $freedeliveryplace);
388 foreach (@fdp) {
389 $text->text($_);
390 $text->translate( 50 / mm, ( $height - $start ) / mm );
391 $start += 5;
393 } else {
394 $text->text($delivery_library->branchaddress1);
395 $text->translate(50/mm, ($height-242)/mm);
396 $text->text($delivery_library->branchaddress2);
397 $text->translate(50/mm, ($height-247)/mm);
398 $text->text($delivery_library->branchaddress3);
399 $text->translate(50/mm, ($height-252)/mm);
400 $text->text(join(' ', $delivery_library->branchzip, $delivery_library->branchcity, $delivery_library->branchcountry));
402 $text->translate(50/mm, ($height-262)/mm);
403 $text->text($basketgroup->{deliverycomment});
406 sub printfooters {
407 my ($pdf) = @_;
408 for (my $i=1;$i <= $pdf->pages;$i++) {
409 my $page = $pdf->openpage($i);
410 my $text = $page->text;
411 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
412 $text->translate(10/mm, 10/mm);
413 $text->text("Page $i / ".$pdf->pages);
417 sub printpdf {
418 my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
419 # open the default PDF that will be used for base (1st page already filled)
420 my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pages.pdf';
421 my $pdf = PDF::API2->open($pdf_template);
422 $pdf->pageLabel( 0, {
423 -style => 'roman',
424 } ); # start with roman numbering
425 # fill the 1st page (basketgroup information)
426 printhead($pdf, $basketgroup, $bookseller);
427 # fill the 2nd page (orders summary)
428 printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
429 # fill other pages (orders)
430 printorders($pdf, $basketgroup, $baskets, $orders);
431 # print something on each page (usually the footer, but you could also put a header
432 printfooters($pdf);
433 return $pdf->stringify;