Bug 13618: Fix for system preference description
[koha.git] / acqui / pdfformat / layout3pagesfr.pm
bloba3d509f281e81f08dd9d13ee250d1a829141e132
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, 2013 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 3 of the License, or (at your option) any later
13 # version.
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, see <http://www.gnu.org/licenses>.
22 #you can use any PDF::API2 module, all you need to do is return the stringifyed pdf object from the printpdf sub.
23 package pdfformat::layout3pagesfr;
24 use vars qw($VERSION @ISA @EXPORT);
25 use MIME::Base64;
26 use List::MoreUtils qw/uniq/;
27 use strict;
28 use warnings;
29 use utf8;
31 use C4::Branch qw(GetBranchDetail GetBranchName);
33 use Koha::Number::Price;
34 use Koha::DateUtils;
36 BEGIN {
37 use Exporter ();
38 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
39 # set the version for version checking
40 $VERSION = 1.00;
41 @ISA = qw(Exporter);
42 @EXPORT = qw(printpdf);
46 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
47 #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.
48 use constant mm => 25.4 / 72;
49 use constant in => 1 / 72;
50 use constant pt => 1;
52 use PDF::API2;
53 #A4 paper specs
54 my ($height, $width) = (297, 210);
55 use PDF::Table;
57 sub printorders {
58 my ($pdf, $basketgroup, $baskets, $orders) = @_;
60 my $cur_format = C4::Context->preference("CurrencyFormat");
62 $pdf->mediabox($height/mm, $width/mm);
63 my $number = 3;
64 for my $basket (@$baskets){
65 my $page = $pdf->page();
67 # print basket header (box)
68 my $box = $page->gfx;
69 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
70 $box->stroke;
71 # $box->restore();
73 # create a text
74 my $text = $page->text;
75 # add basketgroup number
76 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
77 $text->translate(20/mm, ($height-15)/mm);
78 $text->text("Commande N°".$basketgroup->{'id'}.". Panier N° ".$basket->{basketno}.". ".$basket->{booksellernote});
79 $text->translate(20/mm, ($height-20)/mm);
80 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
81 $text->text( ( $basket->{billingplace} ? "Facturation à " . C4::Branch::GetBranchName( $basket->{billingplace} ) : "" )
82 . ( $basket->{billingplace} and $basket->{deliveryplace} ? " et " : "" )
83 . ( $basket->{deliveryplace} ? "livraison à " . C4::Branch::GetBranchName( $basket->{deliveryplace}) : "" )
86 my $pdftable = new PDF::Table();
87 my $abaskets;
88 my $arrbasket;
89 my @keys = ('Document', 'Qté', 'Prix', 'Prix net', '% Remise', 'Remise', 'Taux TVA', 'Total HT', 'Total TTC');
90 for my $bkey (@keys) {
91 push(@$arrbasket, $bkey);
93 push(@$abaskets, $arrbasket);
95 my $titleinfo;
96 foreach my $line (@{$orders->{$basket->{basketno}}}) {
97 $arrbasket = undef;
98 $titleinfo = "";
99 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
100 $titleinfo = $line->{title} . " / " . $line->{author} .
101 ( $line->{isbn} ? " ISBN : " . $line->{isbn} : '' ) .
102 ( $line->{en} ? " EN : " . $line->{en} : '' ) .
103 ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
104 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
105 ( $line->{publishercode} ? ' publié par '. $line->{publishercode} : '') .
106 ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
108 else { # MARC21, NORMARC
109 $titleinfo = $line->{title} . " " . $line->{author} .
110 ( $line->{isbn} ? " ISBN : " . $line->{isbn} : '' ) .
111 ( $line->{en} ? " EN : " . $line->{en} : '' ) .
112 ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
113 ( $line->{edition} ? ", " . $line->{edition} : '' ) .
114 ( $line->{publishercode} ? ' publié par '. $line->{publishercode} : '') .
115 ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
118 push( @$arrbasket,
119 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote pour le fournisseur : ". $line->{order_vendornote} : '' ),
120 $line->{quantity},
121 Koha::Number::Price->new( $line->{rrpgste})->format,
122 Koha::Number::Price->new( $line->{rrpgsti})->format,
123 Koha::Number::Price->new( $line->{discount})->format.'%',
124 Koha::Number::Price->new( $line->{rrpgste} - $line->{ecostgste})->format,
125 Koha::Number::Price->new( $line->{gstrate} * 100)->format.'%',
126 Koha::Number::Price->new( $line->{totalgste})->format,
127 Koha::Number::Price->new( $line->{totalgsti})->format,
129 push(@$abaskets, $arrbasket);
132 $pdftable->table($pdf, $page, $abaskets,
133 x => 10/mm,
134 w => ($width - 20)/mm,
135 start_y => 270/mm,
136 next_y => 285/mm,
137 start_h => 250/mm,
138 next_h => 250/mm,
139 padding => 5,
140 padding_right => 5,
141 background_color_odd => "lightgray",
142 font => $pdf->corefont("Times", -encoding => "utf8"),
143 font_size => 3/mm,
144 header_props => {
145 font => $pdf->corefont("Times", -encoding => "utf8"),
146 font_size => 9,
147 bg_color => 'gray',
148 repeat => 1,
150 column_props => [
152 min_w => 85/mm, # Minimum column width.
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 ,
176 justify => 'right', # One of left|right ,
181 $pdf->mediabox($width/mm, $height/mm);
184 sub printbaskets {
185 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
187 # get library name
188 my $libraryname = C4::Context->preference("LibraryName");
190 my $cur_format = C4::Context->preference("CurrencyFormat");
192 $pdf->mediabox($width/mm, $height/mm);
193 my $page = $pdf->openpage(2);
194 # create a text
195 my $text = $page->text;
197 # add basketgroup number
198 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
199 $text->translate(($width-40)/mm, ($height-53)/mm);
200 $text->text("".$basketgroup->{'id'});
201 # print the libraryname in the header
202 $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
203 $text->translate(30/mm, ($height-28.5)/mm);
204 $text->text($libraryname);
205 my $pdftable = new PDF::Table();
206 my $abaskets;
207 my $arrbasket;
208 # header of the table
209 my @keys = ('Lot', 'Panier', 'Prix', 'Prix net', 'Taux TVA', 'TVA', 'Remise', 'Total HT', 'Total TTC');
210 for my $bkey (@keys) {
211 push(@$arrbasket, $bkey);
213 my ($grandtotalrrpgsti, $grandtotalrrpgste, $grandtotalgsti, $grandtotalgste, $grandtotalgstvalue, $grandtotaldiscount);
214 # calculate each basket total
215 push(@$abaskets, $arrbasket);
216 for my $basket (@$hbaskets) {
217 my @gst;
218 $arrbasket = undef;
219 my ($totalrrpgste, $totalrrpgsti, $totalgste, $totalgsti, $totalgstvalue, $totaldiscount);
220 my $ords = $orders->{$basket->{basketno}};
221 my $ordlength = @$ords;
222 foreach my $ord (@$ords) {
223 $totalgste += $ord->{totalgste};
224 $totalgsti += $ord->{totalgsti};
225 $totalgstvalue += $ord->{gstvalue};
226 $totaldiscount += ($ord->{rrpgste} - $ord->{ecostgste} ) * $ord->{quantity};
227 $totalrrpgste += $ord->{rrpgste} * $ord->{quantity};
228 $totalrrpgsti += $ord->{rrpgsti} * $ord->{quantity};
229 push @gst, $ord->{gstrate};
231 @gst = uniq map { $_ * 100 } @gst;
232 $grandtotalrrpgste += $totalrrpgste;
233 $grandtotalrrpgsti += $totalrrpgsti;
234 $grandtotalgsti += $totalgsti;
235 $grandtotalgste += $totalgste;
236 $grandtotalgstvalue += $totalgstvalue;
237 $grandtotaldiscount += $totaldiscount;
238 my @gst_string =
239 map { Koha::Number::Price->new($_)->format . '%' } @gst;
240 push(@$arrbasket,
241 $basket->{contractname},
242 $basket->{basketname} . ' (No. ' . $basket->{basketno} . ')',
243 Koha::Number::Price->new( $totalrrpgste )->format,
244 Koha::Number::Price->new( $totalrrpgsti )->format,
245 "@gst_string",
246 Koha::Number::Price->new( $totalgstvalue )->format,
247 Koha::Number::Price->new( $totaldiscount )->format,
248 Koha::Number::Price->new( $totalgste )->format,
249 Koha::Number::Price->new( $totalgsti )->format,
251 push(@$abaskets, $arrbasket);
253 # now, push total
254 undef $arrbasket;
255 push @$arrbasket,
257 'Total',
258 Koha::Number::Price->new( $grandtotalrrpgste )->format,
259 Koha::Number::Price->new( $grandtotalrrpgsti )->format,
261 Koha::Number::Price->new( $grandtotalgstvalue )->format,
262 Koha::Number::Price->new( $grandtotaldiscount )->format,
263 Koha::Number::Price->new( $grandtotalgste )->format,
264 Koha::Number::Price->new( $grandtotalgsti )->format;
265 push @$abaskets,$arrbasket;
266 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
268 $pdftable->table($pdf, $page, $abaskets,
269 x => 5/mm,
270 w => ($width - 10)/mm,
271 start_y => 230/mm,
272 next_y => 230/mm,
273 start_h => 230/mm,
274 next_h => 230/mm,
275 font => $pdf->corefont("Times", -encoding => "utf8"),
276 font_size => 3/mm,
277 padding => 5,
278 padding_right => 10,
279 background_color_odd => "lightgray",
280 header_props => {
281 bg_color => 'gray',
282 repeat => 1,
284 column_props => [
290 justify => 'right',
293 justify => 'right',
296 justify => 'right',
299 justify => 'right',
302 justify => 'right',
306 $pdf->mediabox($height/mm, $width/mm);
309 sub printhead {
310 my ($pdf, $basketgroup, $bookseller) = @_;
312 # get library name
313 my $libraryname = C4::Context->preference("LibraryName");
314 # get branch details
315 my $billingdetails = GetBranchDetail( $basketgroup->{billingplace} );
316 my $deliverydetails = GetBranchDetail( $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($billingdetails->{branchname});
349 $text->translate(100/mm, ($height-108.5)/mm);
350 $text->text($billingdetails->{branchphone});
351 $text->translate(100/mm, ($height-115.5)/mm);
352 $text->text($billingdetails->{branchfax});
353 $text->translate(100/mm, ($height-122.5)/mm);
354 $text->text($billingdetails->{branchaddress1});
355 $text->translate(100/mm, ($height-127.5)/mm);
356 $text->text($billingdetails->{branchaddress2});
357 $text->translate(100/mm, ($height-132.5)/mm);
358 $text->text($billingdetails->{branchaddress3});
359 $text->translate(100/mm, ($height-137.5)/mm);
360 $text->text(join(' ', $billingdetails->{branchzip}, $billingdetails->{branchcity}, $billingdetails->{branchcountry}));
361 $text->translate(100/mm, ($height-147.5)/mm);
362 $text->text($billingdetails->{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($deliverydetails->{branchaddress1});
395 $text->translate(50/mm, ($height-242)/mm);
396 $text->text($deliverydetails->{branchaddress2});
397 $text->translate(50/mm, ($height-247)/mm);
398 $text->text($deliverydetails->{branchaddress3});
399 $text->translate(50/mm, ($height-252)/mm);
400 $text->text(join(' ', $deliverydetails->{branchzip}, $deliverydetails->{branchcity}, $deliverydetails->{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/layout3pagesfr.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;