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/;
31 use Koha::Number::Price;
37 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
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;
51 my ($height, $width) = (297, 210);
55 my ($pdf, $basketgroup, $baskets, $orders) = @_;
57 my $cur_format = C4
::Context
->preference("CurrencyFormat");
59 $pdf->mediabox($height/mm, $width/mm);
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)
68 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
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
();
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);
95 foreach my $line (@
{$orders->{$basket->{basketno
}}}) {
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
} : '');
117 $titleinfo. ($line->{order_vendornote
} ?
"\n----------------\nNote for vendor : " . $line->{order_vendornote
} : '' ),
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,
132 w
=> ($width - 20)/mm
,
139 background_color_odd
=> "lightgray",
140 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
143 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
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);
183 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
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);
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
();
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) {
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;
237 map { Koha
::Number
::Price
->new($_)->format . '%' } @gst;
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,
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);
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,
268 w
=> ($width - 10)/mm
,
273 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
277 background_color_odd
=> "lightgray",
304 $pdf->mediabox($height/mm, $width/mm);
308 my ($pdf, $basketgroup, $bookseller) = @_;
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
};
318 # open 1st page (with the header)
319 my $page = $pdf->openpage(1);
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'});
335 my $today = output_pref
({ dt
=> dt_from_string
, dateonly
=> 1 });
336 $text->translate(130/mm, ($height-5-48)/mm);
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);
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) {
384 my @fdp = split('\n', $freedeliveryplace);
387 $text->translate( 50 / mm, ( $height - $start ) / mm
);
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
});
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);
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, {
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
430 return $pdf->stringify;