Bug 12110: (follow-up) add French and German translation
[koha.git] / acqui / pdfformat / layout2pages.pm
1 #!/usr/bin/perl
2
3 #example script to print a basketgroup
4 #written 07/11/08 by john.soros@biblibre.com and paul.poulain@biblibre.com
5
6 # Copyright 2008-2009 BibLibre SARL
7 #
8 # This file is part of Koha.
9 #
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 2 of the License, or (at your option) any later
13 # version.
14 #
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.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 #you can use any PDF::API2 module, all you need to do is return the stringifyed pdf object from the printpdf sub.
24 package pdfformat::layout2pages;
25 use vars qw($VERSION @ISA @EXPORT);
26 use Number::Format qw(format_price);
27 use MIME::Base64;
28 use strict;
29 use warnings;
30 use utf8;
31
32 use C4::Branch qw(GetBranchDetail);
33
34 BEGIN {
35          use Exporter   ();
36          our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
37         # set the version for version checking
38          $VERSION     = 1.00;
39         @ISA    = qw(Exporter);
40         @EXPORT = qw(printpdf);
41 }
42
43
44 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
45 #The constants exported tranform 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.
46 use constant mm => 25.4 / 72;
47 use constant in => 1 / 72;
48 use constant pt => 1;
49
50 use PDF::API2;
51 #A4 paper specs
52 my ($height, $width) = (297, 210);
53 use PDF::Table;
54
55 sub printorders {
56     my ($pdf, $basketgroup, $baskets, $orders) = @_;
57     
58     my $cur_format = C4::Context->preference("CurrencyFormat");
59     my $num;
60     
61     if ( $cur_format eq 'FR' ) {
62         $num = new Number::Format(
63             'decimal_fill'      => '2',
64             'decimal_point'     => ',',
65             'int_curr_symbol'   => '',
66             'mon_thousands_sep' => ' ',
67             'thousands_sep'     => ' ',
68             'mon_decimal_point' => ','
69         );
70     } else {  # US by default..
71         $num = new Number::Format(
72             'int_curr_symbol'   => '',
73             'mon_thousands_sep' => ',',
74             'mon_decimal_point' => '.'
75         );
76     }
77
78     $pdf->mediabox($height/mm, $width/mm);
79     my $page = $pdf->page();
80     
81     my $pdftable = new PDF::Table();
82     
83     my $abaskets;
84     my $arrbasket;
85     my @keys = ('Basket (No.)', 'Document', 'Qty', 'RRP tax inc.', 'Discount', 'GST', 'Total tax exc.', 'Total tax inc.');
86     for my $bkey (@keys) {
87         push(@$arrbasket, $bkey);
88     }
89     push(@$abaskets, $arrbasket);
90
91     my $titleinfo;
92     for my $basket (@$baskets){
93         for my $line (@{$orders->{$basket->{basketno}}}) {
94             $arrbasket = undef;
95             $titleinfo = "";
96             if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
97                 $titleinfo =  $line->{title} . " / " . $line->{author} .
98                     ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
99                     ( $line->{en} ? " EN: " . $line->{en} : '' ) .
100                     ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
101                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
102                     ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
103                     ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
104             }
105             else { # MARC21, NORMARC
106                 $titleinfo =  $line->{title} . " " . $line->{author} .
107                     ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
108                     ( $line->{en} ? " EN: " . $line->{en} : '' ) .
109                     ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
110                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
111                     ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
112                     ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
113             }
114             push( @$arrbasket,
115                 $basket->{basketno},
116                 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote for vendor : " . $line->{order_vendornote} : '' ),
117                 $line->{quantity},
118                 $num->format_price($line->{rrpgsti}),
119                 $num->format_price($line->{discount}).'%',
120                 $num->format_price($line->{gstrate} * 100).'%',
121                 $num->format_price($line->{totalgste}),
122                 $num->format_price($line->{totalgsti}),
123             );
124             push(@$abaskets, $arrbasket);
125         }
126     }
127     
128     $pdftable->table($pdf, $page, $abaskets,
129         x => 10/mm,
130         w => ($width - 20)/mm,
131         start_y => 285/mm,
132         next_y  => 285/mm,
133         start_h => 260/mm,
134         next_h  => 260/mm,
135         padding => 5,
136         padding_right => 5,
137         background_color_odd  => "lightgray",
138         font       => $pdf->corefont("Times", -encoding => "utf8"),
139         font_size => 3/mm,
140         header_props   => {
141             font       => $pdf->corefont("Times", -encoding => "utf8"),
142             font_size  => 10,
143             bg_color   => 'gray',
144             repeat     => 1,
145         },
146         column_props => [
147             { justify => 'left'  },
148             { min_w   => 90/mm   },
149             { justify => 'right' },
150             { justify => 'right' },
151             { justify => 'right' },
152             { justify => 'right' },
153             { justify => 'right' },
154             { justify => 'right' },
155         ],
156     );
157     
158     $pdf->mediabox($width/mm, $height/mm);
159 }
160
161 sub printhead {
162     my ($pdf, $basketgroup, $bookseller) = @_;
163
164     # get library name
165     my $libraryname = C4::Context->preference("LibraryName");
166     # get branch details
167     my $billingdetails  = GetBranchDetail( $basketgroup->{billingplace} );
168     my $deliverydetails = GetBranchDetail( $basketgroup->{deliveryplace} );
169     my $freedeliveryplace = $basketgroup->{freedeliveryplace};
170     # get the subject
171     my $subject;
172
173     # open 1st page (with the header)
174     my $page = $pdf->openpage(1);
175     
176     # create a text
177     my $text = $page->text;
178     
179     # print the libraryname in the header
180     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
181     $text->translate(30/mm, ($height-28.5)/mm);
182     $text->text($libraryname);
183
184     # print order info, on the default PDF
185     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
186     $text->translate(100/mm, ($height-5-48)/mm);
187     $text->text($basketgroup->{'id'});
188     
189     # print the date
190     my $today = C4::Dates->today();
191     $text->translate(130/mm,  ($height-5-48)/mm);
192     $text->text($today);
193     
194     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
195     
196     # print billing infos
197     $text->translate(100/mm, ($height-86)/mm);
198     $text->text($libraryname);
199     $text->translate(100/mm, ($height-97)/mm);
200     $text->text($billingdetails->{branchname});
201     $text->translate(100/mm, ($height-108.5)/mm);
202     $text->text($billingdetails->{branchphone});
203     $text->translate(100/mm, ($height-115.5)/mm);
204     $text->text($billingdetails->{branchfax});
205     $text->translate(100/mm, ($height-122.5)/mm);
206     $text->text($billingdetails->{branchaddress1});
207     $text->translate(100/mm, ($height-127.5)/mm);
208     $text->text($billingdetails->{branchaddress2});
209     $text->translate(100/mm, ($height-132.5)/mm);
210     $text->text($billingdetails->{branchaddress3});
211     $text->translate(100/mm, ($height-137.5)/mm);
212     $text->text(join(' ', $billingdetails->{branchzip}, $billingdetails->{branchcity}, $billingdetails->{branchcountry}));
213     $text->translate(100/mm, ($height-147.5)/mm);
214     $text->text($billingdetails->{branchemail});
215     
216     # print subject
217     $text->translate(100/mm, ($height-145.5)/mm);
218     $text->text($subject);
219     
220     # print bookseller infos
221     $text->translate(100/mm, ($height-180)/mm);
222     $text->text($bookseller->{name});
223     $text->translate(100/mm, ($height-185)/mm);
224     $text->text($bookseller->{postal});
225     $text->translate(100/mm, ($height-190)/mm);
226     $text->text($bookseller->{address1});
227     $text->translate(100/mm, ($height-195)/mm);
228     $text->text($bookseller->{address2});
229     $text->translate(100/mm, ($height-200)/mm);
230     $text->text($bookseller->{address3});
231     $text->translate(100/mm, ($height-205)/mm);
232     $text->text($bookseller->{accountnumber});
233     
234     # print delivery infos
235     $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
236     $text->translate(50/mm, ($height-237)/mm);
237     if ($freedeliveryplace) {
238         my $start = 242;
239         my @fdp = split('\n', $freedeliveryplace);
240         foreach (@fdp) {
241             $text->text($_);
242             $text->translate( 50 / mm, ( $height - $start ) / mm );
243             $start += 5;
244         }
245     } else {
246         $text->text( $deliverydetails->{branchaddress1} );
247         $text->translate( 50 / mm, ( $height - 242 ) / mm );
248         $text->text( $deliverydetails->{branchaddress2} );
249         $text->translate( 50 / mm, ( $height - 247 ) / mm );
250         $text->text( $deliverydetails->{branchaddress3} );
251         $text->translate( 50 / mm, ( $height - 252 ) / mm );
252         $text->text( join( ' ', $deliverydetails->{branchzip}, $deliverydetails->{branchcity}, $deliverydetails->{branchcountry} ) );
253     }
254     $text->translate(50/mm, ($height-262)/mm);
255     $text->text($basketgroup->{deliverycomment});
256 }
257
258 sub printfooters {
259     my $pdf = shift;
260     for ( 1..$pdf->pages ) {
261         my $page = $pdf->openpage($_);
262         my $text = $page->text;
263         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
264         $text->translate(10/mm,  10/mm);
265         $text->text("Page $_ / ".$pdf->pages);
266     }
267 }
268
269 sub printpdf {
270     my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
271     # open the default PDF that will be used for base (1st page already filled)
272     my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout2pages.pdf';
273     my $pdf = PDF::API2->open($pdf_template);
274     $pdf->pageLabel( 0, {
275         -style => 'roman',
276     } ); # start with roman numbering
277     # fill the 1st page (basketgroup information)
278     printhead($pdf, $basketgroup, $bookseller);
279     # fill other pages (orders)
280     printorders($pdf, $basketgroup, $baskets, $orders);
281     # print something on each page (usually the footer, but you could also put a header
282     printfooters($pdf);
283     return $pdf->stringify;
284 }
285
286 1;