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);
26 use List::MoreUtils qw/uniq/;
30 use Koha::Number::Price;
36 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
38 @EXPORT = qw(printpdf);
42 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
43 #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.
44 use constant mm
=> 25.4 / 72;
45 use constant
in => 1 / 72;
50 my ($height, $width) = (297, 210);
54 my ($pdf, $basketgroup, $baskets, $orders) = @_;
56 my $cur_format = C4
::Context
->preference("CurrencyFormat");
58 $pdf->mediabox($height/mm, $width/mm);
60 for my $basket (@
$baskets){
61 my $page = $pdf->page();
62 my $billing_library = Koha
::Libraries
->find( $basket->{billingplace
} );
63 my $delivery_library = Koha
::Libraries
->find( $basket->{deliveryplace
} );
65 # print basket header (box)
67 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
72 my $text = $page->text;
73 # add basketgroup number
74 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
75 $text->translate(20/mm, ($height-15)/mm);
76 $text->text("Order no. ".$basketgroup->{'id'}.". Basket no. ".$basket->{basketno
}.". ".$basket->{booksellernote
});
77 $text->translate(20/mm, ($height-20)/mm);
78 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 4/mm
);
79 $text->text( ( $billing_library ?
"Billing at " . $billing_library->branchname : "" )
80 . ( $billing_library and $delivery_library ?
" and " : "" )
81 . ( $delivery_library ?
"delivery at " . $delivery_library->branchname : "" )
84 my $pdftable = new PDF
::Table
();
87 my @keys = ('Document', 'Qty', 'RRP tax exc.', 'RRP tax inc.', 'Discount', 'Discount price', 'GST rate', 'Total tax exc.', 'Total tax inc.');
88 for my $bkey (@keys) {
89 push(@
$arrbasket, $bkey);
91 push(@
$abaskets, $arrbasket);
94 foreach my $line (@
{$orders->{$basket->{basketno
}}}) {
97 if ( C4
::Context
->preference("marcflavour") eq 'UNIMARC' ) {
98 $titleinfo = $line->{title
} . " / " . $line->{author
} .
99 ( $line->{isbn
} ?
" ISBN: " . $line->{isbn
} : '' ) .
100 ( $line->{en
} ?
" EN: " . $line->{en
} : '' ) .
101 ( $line->{itemtype
} ?
", " . $line->{itemtype
} : '' ) .
102 ( $line->{edition
} ?
", " . $line->{edition
} : '' ) .
103 ( $line->{publishercode
} ?
' published by '. $line->{publishercode
} : '') .
104 ( $line->{publicationyear
} ?
', '. $line->{publicationyear
} : '');
106 else { # MARC21, NORMARC
107 $titleinfo = $line->{title
} . " " . $line->{author
} .
108 ( $line->{isbn
} ?
" ISBN: " . $line->{isbn
} : '' ) .
109 ( $line->{en
} ?
" EN: " . $line->{en
} : '' ) .
110 ( $line->{itemtype
} ?
" " . $line->{itemtype
} : '' ) .
111 ( $line->{edition
} ?
", " . $line->{edition
} : '' ) .
112 ( $line->{publishercode
} ?
' published by '. $line->{publishercode
} : '') .
113 ( $line->{copyrightdate
} ?
' '. $line->{copyrightdate
} : '');
116 $titleinfo. ($line->{order_vendornote
} ?
"\n----------------\nNote for vendor : " . $line->{order_vendornote
} : '' ),
118 Koha
::Number
::Price
->new( $line->{rrp_tax_excluded
} )->format,
119 Koha
::Number
::Price
->new( $line->{rrp_tax_included
} )->format,
120 Koha
::Number
::Price
->new( $line->{discount
} )->format . '%',
121 Koha
::Number
::Price
->new( $line->{rrp_tax_excluded
} - $line->{ecost_tax_excluded
})->format,
122 Koha
::Number
::Price
->new( $line->{tax_rate
} * 100 )->format . '%',
123 Koha
::Number
::Price
->new( $line->{total_tax_excluded
} )->format,
124 Koha
::Number
::Price
->new( $line->{total_tax_included
} )->format,
126 push(@
$abaskets, $arrbasket);
129 $pdftable->table($pdf, $page, $abaskets,
131 w
=> ($width - 20)/mm
,
138 background_color_odd
=> "lightgray",
139 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
142 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
149 min_w
=> 85/mm
, # Minimum column width.
152 justify
=> 'right', # One of left|right ,
155 justify
=> 'right', # One of left|right ,
158 justify
=> 'right', # One of left|right ,
161 justify
=> 'right', # One of left|right ,
164 justify
=> 'right', # One of left|right ,
167 justify
=> 'right', # One of left|right ,
170 justify
=> 'right', # One of left|right ,
173 justify
=> 'right', # One of left|right ,
178 $pdf->mediabox($width/mm, $height/mm);
182 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
185 my $libraryname = C4
::Context
->preference("LibraryName");
187 my $cur_format = C4
::Context
->preference("CurrencyFormat");
189 $pdf->mediabox($width/mm, $height/mm);
190 my $page = $pdf->openpage(2);
192 my $text = $page->text;
194 # add basketgroup number
195 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
196 $text->translate(($width-40)/mm, ($height-53)/mm);
197 $text->text("".$basketgroup->{'id'});
198 # print the libraryname in the header
199 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
200 $text->translate(30/mm, ($height-28.5)/mm);
201 $text->text($libraryname);
202 my $pdftable = new PDF
::Table
();
205 # header of the table
206 my @keys = ('Lot', 'Basket (No.)','Total RRP tax exc.', 'Total RRP tax inc.', 'GST rate', 'GST', 'Total discount', 'Total tax exc.', 'Total tax inc.');
207 for my $bkey (@keys) {
208 push(@
$arrbasket, $bkey);
210 my ($grandtotal_rrp_tax_included, $grandtotal_rrp_tax_excluded, $grandtotal_tax_included, $grandtotal_tax_excluded, $grandtotaltax_value, $grandtotaldiscount);
211 # calculate each basket total
212 push(@
$abaskets, $arrbasket);
213 for my $basket (@
$hbaskets) {
216 my ($total_rrp_tax_excluded, $total_rrp_tax_included, $total_tax_excluded, $total_tax_included, $totaltax_value, $totaldiscount);
217 my $ords = $orders->{$basket->{basketno
}};
218 my $ordlength = @
$ords;
219 foreach my $ord (@
$ords) {
220 $total_tax_excluded += $ord->{total_tax_excluded
};
221 $total_tax_included += $ord->{total_tax_included
};
222 $totaltax_value += $ord->{tax_value
};
223 $totaldiscount += ($ord->{rrp_tax_excluded
} - $ord->{ecost_tax_excluded
} ) * $ord->{quantity
};
224 $total_rrp_tax_excluded += $ord->{rrp_tax_excluded
} * $ord->{quantity
};
225 $total_rrp_tax_included += $ord->{rrp_tax_included
} * $ord->{quantity
};
226 push @gst, $ord->{tax_rate
};
228 @gst = uniq
map { $_ * 100 } @gst;
229 $grandtotal_rrp_tax_excluded += $total_rrp_tax_excluded;
230 $grandtotal_rrp_tax_included += $total_rrp_tax_included;
231 $grandtotal_tax_included += $total_tax_included;
232 $grandtotal_tax_excluded += $total_tax_excluded;
233 $grandtotaltax_value += $totaltax_value;
234 $grandtotaldiscount += $totaldiscount;
236 map { Koha
::Number
::Price
->new($_)->format . '%' } @gst;
238 $basket->{contractname
},
239 $basket->{basketname
} . ' (No. ' . $basket->{basketno
} . ')',
240 Koha
::Number
::Price
->new( $total_rrp_tax_excluded )->format,
241 Koha
::Number
::Price
->new( $total_rrp_tax_included )->format,
243 Koha
::Number
::Price
->new( $totaltax_value )->format,
244 Koha
::Number
::Price
->new( $totaldiscount )->format,
245 Koha
::Number
::Price
->new( $total_tax_excluded )->format,
246 Koha
::Number
::Price
->new( $total_tax_included )->format,
248 push(@
$abaskets, $arrbasket);
255 Koha
::Number
::Price
->new( $grandtotal_rrp_tax_excluded )->format,
256 Koha
::Number
::Price
->new( $grandtotal_rrp_tax_included )->format,
258 Koha
::Number
::Price
->new( $grandtotaltax_value )->format,
259 Koha
::Number
::Price
->new( $grandtotaldiscount )->format,
260 Koha
::Number
::Price
->new( $grandtotal_tax_excluded )->format,
261 Koha
::Number
::Price
->new( $grandtotal_tax_included )->format;
262 push @
$abaskets,$arrbasket;
263 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
265 $pdftable->table($pdf, $page, $abaskets,
267 w
=> ($width - 10)/mm
,
272 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
276 background_color_odd
=> "lightgray",
303 $pdf->mediabox($height/mm, $width/mm);
307 my ($pdf, $basketgroup, $bookseller) = @_;
310 my $libraryname = C4
::Context
->preference("LibraryName");
311 my $billing_library = Koha
::Libraries
->find( $basketgroup->{billingplace
} );
312 my $delivery_library = Koha
::Libraries
->find( $basketgroup->{deliveryplace
} );
313 my $freedeliveryplace = $basketgroup->{freedeliveryplace
};
317 # open 1st page (with the header)
318 my $page = $pdf->openpage(1);
321 my $text = $page->text;
323 # print the libraryname in the header
324 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
325 $text->translate(30/mm, ($height-28.5)/mm);
326 $text->text($libraryname);
328 # print order info, on the default PDF
329 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 8/mm
);
330 $text->translate(100/mm, ($height-5-48)/mm);
331 $text->text($basketgroup->{'id'});
334 my $today = output_pref
({ dt
=> dt_from_string
, dateonly
=> 1 });
335 $text->translate(130/mm, ($height-5-48)/mm);
338 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 4/mm
);
340 # print billing infos
341 $text->translate(100/mm, ($height-86)/mm);
342 $text->text($libraryname);
343 $text->translate(100/mm, ($height-97)/mm);
344 $text->text($billing_library->branchname);
345 $text->translate(100/mm, ($height-108.5)/mm);
346 $text->text($billing_library->branchphone);
347 $text->translate(100/mm, ($height-115.5)/mm);
348 $text->text($billing_library->branchfax);
349 $text->translate(100/mm, ($height-122.5)/mm);
350 $text->text($billing_library->branchaddress1);
351 $text->translate(100/mm, ($height-127.5)/mm);
352 $text->text($billing_library->branchaddress2);
353 $text->translate(100/mm, ($height-132.5)/mm);
354 $text->text($billing_library->branchaddress3);
355 $text->translate(100/mm, ($height-137.5)/mm);
356 $text->text(join(' ', $billing_library->branchzip, $billing_library->branchcity, $billing_library->branchcountry));
357 $text->translate(100/mm, ($height-147.5)/mm);
358 $text->text($billing_library->branchemail);
361 $text->translate(100/mm, ($height-145.5)/mm);
362 $text->text($subject);
364 # print bookseller infos
365 $text->translate(100/mm, ($height-180)/mm);
366 $text->text($bookseller->name);
367 $text->translate(100/mm, ($height-185)/mm);
368 $text->text($bookseller->postal);
369 $text->translate(100/mm, ($height-190)/mm);
370 $text->text($bookseller->address1);
371 $text->translate(100/mm, ($height-195)/mm);
372 $text->text($bookseller->address2);
373 $text->translate(100/mm, ($height-200)/mm);
374 $text->text($bookseller->address3);
375 $text->translate(100/mm, ($height-205)/mm);
376 $text->text($bookseller->accountnumber);
378 # print delivery infos
379 $text->font( $pdf->corefont("Times-Bold", -encoding
=> "utf8"), 4/mm
);
380 $text->translate(50/mm, ($height-237)/mm);
381 if ($freedeliveryplace) {
383 my @fdp = split('\n', $freedeliveryplace);
386 $text->translate( 50 / mm, ( $height - $start ) / mm
);
390 $text->text($delivery_library->branchaddress1);
391 $text->translate(50/mm, ($height-242)/mm);
392 $text->text($delivery_library->branchaddress2);
393 $text->translate(50/mm, ($height-247)/mm);
394 $text->text($delivery_library->branchaddress3);
395 $text->translate(50/mm, ($height-252)/mm);
396 $text->text(join(' ', $delivery_library->branchzip, $delivery_library->branchcity, $delivery_library->branchcountry));
398 $text->translate(50/mm, ($height-262)/mm);
399 $text->text($basketgroup->{deliverycomment
});
404 for (my $i=1;$i <= $pdf->pages;$i++) {
405 my $page = $pdf->openpage($i);
406 my $text = $page->text;
407 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 3/mm
);
408 $text->translate(10/mm, 10/mm);
409 $text->text("Page $i / ".$pdf->pages);
414 my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
415 # open the default PDF that will be used for base (1st page already filled)
416 my $pdf_template = C4
::Context
->config('intrahtdocs') . '/' . C4
::Context
->preference('template') . '/pdf/layout3pages.pdf';
417 my $pdf = PDF
::API2
->open($pdf_template);
418 $pdf->pageLabel( 0, {
420 } ); # start with roman numbering
421 # fill the 1st page (basketgroup information)
422 printhead
($pdf, $basketgroup, $bookseller);
423 # fill the 2nd page (orders summary)
424 printbaskets
($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
425 # fill other pages (orders)
426 printorders
($pdf, $basketgroup, $baskets, $orders);
427 # print something on each page (usually the footer, but you could also put a header
429 return $pdf->stringify;