Bug 21637: Fixed upercase letter in EasyAnalyticalRecords syspref
[koha.git] / Koha / pdfformat / layout3pagesfr.pm
blobb4bdf7874dd3f9621fd8920711431bb63c0ef6cd
1 package Koha::pdfformat::layout3pagesfr;
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(@ISA @EXPORT);
25 use MIME::Base64;
26 use List::MoreUtils qw/uniq/;
27 use Modern::Perl;
28 use utf8;
30 use Koha::Number::Price;
31 use Koha::DateUtils;
32 use Koha::Libraries;
34 BEGIN {
35 use Exporter ();
36 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
37 @ISA = qw(Exporter);
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;
46 use constant pt => 1;
48 use PDF::API2;
49 #A4 paper specs
50 my ($height, $width) = (297, 210);
51 use PDF::Table;
53 sub printorders {
54 my ($pdf, $basketgroup, $baskets, $orders) = @_;
56 my $cur_format = C4::Context->preference("CurrencyFormat");
58 $pdf->mediabox($height/mm, $width/mm);
59 my $number = 3;
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)
66 my $box = $page->gfx;
67 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
68 $box->stroke;
69 # $box->restore();
71 # create a text
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("Commande N°".$basketgroup->{'id'}.". Panier N° ".$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 ? "Facturation à " . $billing_library->branchname : "" )
80 . ( $billing_library and $delivery_library ? " et " : "" )
81 . ( $delivery_library ? "livraison à " . $delivery_library->branchname : "" )
84 my $pdftable = new PDF::Table();
85 my $abaskets;
86 my $arrbasket;
87 my @keys = ('Document', 'Qté', 'Prix', 'Prix net', '% Remise', 'Remise', 'Taux TVA', 'Total HT', 'Total TTC');
88 for my $bkey (@keys) {
89 push(@$arrbasket, $bkey);
91 push(@$abaskets, $arrbasket);
93 my $titleinfo;
94 foreach my $line (@{$orders->{$basket->{basketno}}}) {
95 $arrbasket = undef;
96 $titleinfo = "";
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} ? ' publié par '. $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} ? ' publié par '. $line->{publishercode} : '') .
113 ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
116 push( @$arrbasket,
117 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote pour le fournisseur : ". $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', 'Panier', 'Prix', 'Prix net', 'Taux TVA', 'TVA', 'Remise', 'Total HT', 'Total TTC');
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 += ($ord->{rrp_tax_excluded} - $ord->{ecost_tax_excluded} ) * $ord->{quantity};
225 $total_rrp_tax_excluded += $ord->{rrp_tax_excluded} * $ord->{quantity};
226 $total_rrp_tax_included += $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/layout3pagesfr.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;