Bug 18309: Add missing fields in biblio framework
[koha.git] / Koha / pdfformat / layout3pages.pm
blob400a8837206e4597f132b360018bc548062f7fe9
1 package Koha::pdfformat::layout3pages;
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 use vars qw(@ISA @EXPORT);
25 use MIME::Base64;
26 use List::MoreUtils qw/uniq/;
27 use Modern::Perl;
28 use utf8;
30 use C4::Acquisition;
31 use Koha::Number::Price;
32 use Koha::DateUtils;
33 use Koha::Libraries;
35 BEGIN {
36 use Exporter ();
37 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
38 @ISA = qw(Exporter);
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 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.
45 use constant mm => 25.4 / 72;
46 use constant in => 1 / 72;
47 use constant pt => 1;
49 use PDF::API2;
50 #A4 paper specs
51 my ($height, $width) = (297, 210);
52 use PDF::Table;
54 sub printorders {
55 my ($pdf, $basketgroup, $baskets, $orders) = @_;
57 my $cur_format = C4::Context->preference("CurrencyFormat");
59 $pdf->mediabox($height/mm, $width/mm);
60 my $number = 3;
61 for my $basket (@$baskets){
62 my $page = $pdf->page();
63 my $billing_library = Koha::Libraries->find( $basket->{billingplace} );
64 my $delivery_library = Koha::Libraries->find( $basket->{deliveryplace} );
66 # print basket header (box)
67 my $box = $page->gfx;
68 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
69 $box->stroke;
70 # $box->restore();
72 # create a text
73 my $text = $page->text;
74 # add basketgroup number
75 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
76 $text->translate(20/mm, ($height-15)/mm);
77 $text->text("Order no. ".$basketgroup->{'id'}.". Basket no. ".$basket->{basketno}.". ".$basket->{booksellernote});
78 $text->translate(20/mm, ($height-20)/mm);
79 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
80 $text->text( ( $billing_library ? "Billing at " . $billing_library->branchname : "" )
81 . ( $billing_library and $delivery_library ? " and " : "" )
82 . ( $delivery_library ? "delivery at " . $delivery_library->branchname : "" )
85 my $pdftable = new PDF::Table();
86 my $abaskets;
87 my $arrbasket;
88 my @keys = ('Document', 'Qty', 'RRP tax exc.', 'RRP tax inc.', 'Discount', 'Discount price', 'GST rate', 'Total tax exc.', 'Total tax inc.');
89 for my $bkey (@keys) {
90 push(@$arrbasket, $bkey);
92 push(@$abaskets, $arrbasket);
94 my $titleinfo;
95 foreach my $line (@{$orders->{$basket->{basketno}}}) {
96 $arrbasket = undef;
97 $titleinfo = "";
98 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
99 $titleinfo = $line->{title} . " / " . $line->{author} .
100 ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
101 ( $line->{en} ? " EN: " . $line->{en} : '' ) .
102 ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
103 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
104 ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
105 ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
107 else { # MARC21, NORMARC
108 $titleinfo = $line->{title} . " " . $line->{author} .
109 ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
110 ( $line->{en} ? " EN: " . $line->{en} : '' ) .
111 ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
112 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
113 ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
114 ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
116 push( @$arrbasket,
117 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote for vendor : " . $line->{order_vendornote} : '' ),
118 $line->{quantity},
119 Koha::Number::Price->new( $line->{rrp_tax_excluded} )->format,
120 Koha::Number::Price->new( $line->{rrp_tax_included} )->format,
121 Koha::Number::Price->new( $line->{discount} )->format . '%',
122 Koha::Number::Price->new( $line->{rrp_tax_excluded} - $line->{ecost_tax_excluded})->format,
123 Koha::Number::Price->new( $line->{tax_rate} * 100 )->format . '%',
124 Koha::Number::Price->new( $line->{total_tax_excluded} )->format,
125 Koha::Number::Price->new( $line->{total_tax_included} )->format,
127 push(@$abaskets, $arrbasket);
130 $pdftable->table($pdf, $page, $abaskets,
131 x => 10/mm,
132 w => ($width - 20)/mm,
133 start_y => 270/mm,
134 next_y => 285/mm,
135 start_h => 250/mm,
136 next_h => 250/mm,
137 padding => 5,
138 padding_right => 5,
139 background_color_odd => "lightgray",
140 font => $pdf->corefont("Times", -encoding => "utf8"),
141 font_size => 3/mm,
142 header_props => {
143 font => $pdf->corefont("Times", -encoding => "utf8"),
144 font_size => 9,
145 bg_color => 'gray',
146 repeat => 1,
148 column_props => [
150 min_w => 85/mm, # Minimum column width.
153 justify => 'right', # One of left|right ,
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 ,
179 $pdf->mediabox($width/mm, $height/mm);
182 sub printbaskets {
183 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
185 # get library name
186 my $libraryname = C4::Context->preference("LibraryName");
188 my $cur_format = C4::Context->preference("CurrencyFormat");
190 $pdf->mediabox($width/mm, $height/mm);
191 my $page = $pdf->openpage(2);
192 # create a text
193 my $text = $page->text;
195 # add basketgroup number
196 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
197 $text->translate(($width-40)/mm, ($height-53)/mm);
198 $text->text("".$basketgroup->{'id'});
199 # print the libraryname in the header
200 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
201 $text->translate(30/mm, ($height-28.5)/mm);
202 $text->text($libraryname);
203 my $pdftable = new PDF::Table();
204 my $abaskets;
205 my $arrbasket;
206 # header of the table
207 my @keys = ('Lot', 'Basket (No.)','Total RRP tax exc.', 'Total RRP tax inc.', 'GST rate', 'GST', 'Total discount', 'Total tax exc.', 'Total tax inc.');
208 for my $bkey (@keys) {
209 push(@$arrbasket, $bkey);
211 my ($grandtotal_rrp_tax_included, $grandtotal_rrp_tax_excluded, $grandtotal_tax_included, $grandtotal_tax_excluded, $grandtotaltax_value, $grandtotaldiscount);
212 # calculate each basket total
213 push(@$abaskets, $arrbasket);
214 for my $basket (@$hbaskets) {
215 my @gst;
216 $arrbasket = undef;
217 my ($total_rrp_tax_excluded, $total_rrp_tax_included, $total_tax_excluded, $total_tax_included, $totaltax_value, $totaldiscount);
218 my $ords = $orders->{$basket->{basketno}};
219 my $ordlength = @$ords;
220 foreach my $ord (@$ords) {
221 $total_tax_excluded += $ord->{total_tax_excluded};
222 $total_tax_included += $ord->{total_tax_included};
223 $totaltax_value += $ord->{tax_value};
224 $totaldiscount += (get_rounded_price($ord->{rrp_tax_excluded}) - get_rounded_price($ord->{ecost_tax_excluded}) ) * $ord->{quantity};
225 $total_rrp_tax_excluded += get_rounded_price($ord->{rrp_tax_excluded}) * $ord->{quantity};
226 $total_rrp_tax_included += get_rounded_price($ord->{rrp_tax_included}) * $ord->{quantity};
227 push @gst, $ord->{tax_rate};
229 @gst = uniq map { $_ * 100 } @gst;
230 $grandtotal_rrp_tax_excluded += $total_rrp_tax_excluded;
231 $grandtotal_rrp_tax_included += $total_rrp_tax_included;
232 $grandtotal_tax_included += $total_tax_included;
233 $grandtotal_tax_excluded += $total_tax_excluded;
234 $grandtotaltax_value += $totaltax_value;
235 $grandtotaldiscount += $totaldiscount;
236 my @gst_string =
237 map { Koha::Number::Price->new($_)->format . '%' } @gst;
238 push(@$arrbasket,
239 $basket->{contractname},
240 $basket->{basketname} . ' (No. ' . $basket->{basketno} . ')',
241 Koha::Number::Price->new( $total_rrp_tax_excluded )->format,
242 Koha::Number::Price->new( $total_rrp_tax_included )->format,
243 "@gst_string",
244 Koha::Number::Price->new( $totaltax_value )->format,
245 Koha::Number::Price->new( $totaldiscount )->format,
246 Koha::Number::Price->new( $total_tax_excluded )->format,
247 Koha::Number::Price->new( $total_tax_included )->format,
249 push(@$abaskets, $arrbasket);
251 # now, push total
252 undef $arrbasket;
253 push @$arrbasket,
255 'Total',
256 Koha::Number::Price->new( $grandtotal_rrp_tax_excluded )->format,
257 Koha::Number::Price->new( $grandtotal_rrp_tax_included )->format,
259 Koha::Number::Price->new( $grandtotaltax_value )->format,
260 Koha::Number::Price->new( $grandtotaldiscount )->format,
261 Koha::Number::Price->new( $grandtotal_tax_excluded )->format,
262 Koha::Number::Price->new( $grandtotal_tax_included )->format;
263 push @$abaskets,$arrbasket;
264 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
266 $pdftable->table($pdf, $page, $abaskets,
267 x => 5/mm,
268 w => ($width - 10)/mm,
269 start_y => 230/mm,
270 next_y => 230/mm,
271 start_h => 230/mm,
272 next_h => 230/mm,
273 font => $pdf->corefont("Times", -encoding => "utf8"),
274 font_size => 3/mm,
275 padding => 5,
276 padding_right => 10,
277 background_color_odd => "lightgray",
278 header_props => {
279 bg_color => 'gray',
280 repeat => 1,
282 column_props => [
288 justify => 'right',
291 justify => 'right',
294 justify => 'right',
297 justify => 'right',
300 justify => 'right',
304 $pdf->mediabox($height/mm, $width/mm);
307 sub printhead {
308 my ($pdf, $basketgroup, $bookseller) = @_;
310 # get library name
311 my $libraryname = C4::Context->preference("LibraryName");
312 my $billing_library = Koha::Libraries->find( $basketgroup->{billingplace} );
313 my $delivery_library = Koha::Libraries->find( $basketgroup->{deliveryplace} );
314 my $freedeliveryplace = $basketgroup->{freedeliveryplace};
315 # get the subject
316 my $subject;
318 # open 1st page (with the header)
319 my $page = $pdf->openpage(1);
321 # create a text
322 my $text = $page->text;
324 # print the libraryname in the header
325 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
326 $text->translate(30/mm, ($height-28.5)/mm);
327 $text->text($libraryname);
329 # print order info, on the default PDF
330 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
331 $text->translate(100/mm, ($height-5-48)/mm);
332 $text->text($basketgroup->{'id'});
334 # print the date
335 my $today = output_pref({ dt => dt_from_string, dateonly => 1 });
336 $text->translate(130/mm, ($height-5-48)/mm);
337 $text->text($today);
339 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
341 # print billing infos
342 $text->translate(100/mm, ($height-86)/mm);
343 $text->text($libraryname);
344 $text->translate(100/mm, ($height-97)/mm);
345 $text->text($billing_library->branchname);
346 $text->translate(100/mm, ($height-108.5)/mm);
347 $text->text($billing_library->branchphone);
348 $text->translate(100/mm, ($height-115.5)/mm);
349 $text->text($billing_library->branchfax);
350 $text->translate(100/mm, ($height-122.5)/mm);
351 $text->text($billing_library->branchaddress1);
352 $text->translate(100/mm, ($height-127.5)/mm);
353 $text->text($billing_library->branchaddress2);
354 $text->translate(100/mm, ($height-132.5)/mm);
355 $text->text($billing_library->branchaddress3);
356 $text->translate(100/mm, ($height-137.5)/mm);
357 $text->text(join(' ', $billing_library->branchzip, $billing_library->branchcity, $billing_library->branchcountry));
358 $text->translate(100/mm, ($height-147.5)/mm);
359 $text->text($billing_library->branchemail);
361 # print subject
362 $text->translate(100/mm, ($height-145.5)/mm);
363 $text->text($subject);
365 # print bookseller infos
366 $text->translate(100/mm, ($height-180)/mm);
367 $text->text($bookseller->name);
368 $text->translate(100/mm, ($height-185)/mm);
369 $text->text($bookseller->postal);
370 $text->translate(100/mm, ($height-190)/mm);
371 $text->text($bookseller->address1);
372 $text->translate(100/mm, ($height-195)/mm);
373 $text->text($bookseller->address2);
374 $text->translate(100/mm, ($height-200)/mm);
375 $text->text($bookseller->address3);
376 $text->translate(100/mm, ($height-205)/mm);
377 $text->text($bookseller->accountnumber);
379 # print delivery infos
380 $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
381 $text->translate(50/mm, ($height-237)/mm);
382 if ($freedeliveryplace) {
383 my $start = 242;
384 my @fdp = split('\n', $freedeliveryplace);
385 foreach (@fdp) {
386 $text->text($_);
387 $text->translate( 50 / mm, ( $height - $start ) / mm );
388 $start += 5;
390 } else {
391 $text->text($delivery_library->branchaddress1);
392 $text->translate(50/mm, ($height-242)/mm);
393 $text->text($delivery_library->branchaddress2);
394 $text->translate(50/mm, ($height-247)/mm);
395 $text->text($delivery_library->branchaddress3);
396 $text->translate(50/mm, ($height-252)/mm);
397 $text->text(join(' ', $delivery_library->branchzip, $delivery_library->branchcity, $delivery_library->branchcountry));
399 $text->translate(50/mm, ($height-262)/mm);
400 $text->text($basketgroup->{deliverycomment});
403 sub printfooters {
404 my ($pdf) = @_;
405 for (my $i=1;$i <= $pdf->pages;$i++) {
406 my $page = $pdf->openpage($i);
407 my $text = $page->text;
408 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
409 $text->translate(10/mm, 10/mm);
410 $text->text("Page $i / ".$pdf->pages);
414 sub printpdf {
415 my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
416 # open the default PDF that will be used for base (1st page already filled)
417 my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pages.pdf';
418 my $pdf = PDF::API2->open($pdf_template);
419 $pdf->pageLabel( 0, {
420 -style => 'roman',
421 } ); # start with roman numbering
422 # fill the 1st page (basketgroup information)
423 printhead($pdf, $basketgroup, $bookseller);
424 # fill the 2nd page (orders summary)
425 printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
426 # fill other pages (orders)
427 printorders($pdf, $basketgroup, $baskets, $orders);
428 # print something on each page (usually the footer, but you could also put a header
429 printfooters($pdf);
430 return $pdf->stringify;