Bug 15005: Fix pagination_bar calls
[koha.git] / acqui / pdfformat / layout2pagesde.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
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.
14 #
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.
19 #
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>.
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::layout2pagesde;
25 use vars qw($VERSION @ISA @EXPORT);
26 use MIME::Base64;
27 use strict;
28 use warnings;
29 use utf8;
30
31 use C4::Branch qw(GetBranchDetail);
32
33 use Koha::Number::Price;
34 use Koha::DateUtils;
35
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);
43 }
44
45
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;
51
52 use PDF::API2;
53 #A4 paper specs
54 my ($height, $width) = (297, 210);
55 use PDF::Table;
56
57 sub printorders {
58     my ($pdf, $basketgroup, $baskets, $orders) = @_;
59     
60     my $cur_format = C4::Context->preference("CurrencyFormat");
61
62     $pdf->mediabox($height/mm, $width/mm);
63     my $page = $pdf->page();
64     
65     my $pdftable = new PDF::Table();
66     
67     my $abaskets;
68     my $arrbasket;
69     my @keys = ('Bestellung', 'Titel', 'Anz.', 'Preis inkl. MWSt.', 'Rabatt', 'MWSt.', 'Gesamt, exkl. MWSt.', 'Gesamt inkl. MWSt.');
70     for my $bkey (@keys) {
71         push(@$arrbasket, $bkey);
72     }
73     push(@$abaskets, $arrbasket);
74
75     my $titleinfo;
76     for my $basket (@$baskets){
77         for my $line (@{$orders->{$basket->{basketno}}}) {
78             $arrbasket = undef;
79             $titleinfo = "";
80             if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
81                 $titleinfo =  $line->{title} . " / " . $line->{author} .
82                     ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
83                     ( $line->{en} ? " EN: " . $line->{en} : '' ) .
84                     ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
85                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
86                     ( $line->{publishercode} ? ' Verlag: '. $line->{publishercode} : '') .
87                     ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
88             }
89             else { # MARC21, NORMARC
90                 $titleinfo =  $line->{title} . " " . $line->{author} .
91                     ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
92                     ( $line->{en} ? " EN: " . $line->{en} : '' ) .
93                     ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
94                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
95                     ( $line->{publishercode} ? ' Verlag: '. $line->{publishercode} : '') .
96                     ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
97             }
98             push( @$arrbasket,
99                 $basket->{basketno},
100                 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nLieferantennotiz : ". $line->{order_vendornote} : '' ),                $line->{quantity},
101                 $line->{quantity},
102                 Koha::Number::Price->new( $line->{rrpgsti} )->format,
103                 Koha::Number::Price->new( $line->{discount} )->format . '%',
104                 Koha::Number::Price->new( $line->{gstrate} * 100 )->format . '%',
105                 Koha::Number::Price->new( $line->{totalgste} )->format,
106                 Koha::Number::Price->new( $line->{totalgsti} )->format,
107             );
108             push(@$abaskets, $arrbasket);
109         }
110     }
111     
112     $pdftable->table($pdf, $page, $abaskets,
113         x => 10/mm,
114         w => ($width - 20)/mm,
115         start_y => 285/mm,
116         next_y  => 285/mm,
117         start_h => 260/mm,
118         next_h  => 260/mm,
119         padding => 5,
120         padding_right => 5,
121         background_color_odd  => "lightgray",
122         font       => $pdf->corefont("Times", -encoding => "utf8"),
123         font_size => 3/mm,
124         header_props   => {
125             font       => $pdf->corefont("Times", -encoding => "utf8"),
126             font_size  => 10,
127             bg_color   => 'gray',
128             repeat     => 1,
129         },
130         column_props => [
131             { justify => 'left'  },
132             { min_w   => 90/mm   },
133             { justify => 'right' },
134             { justify => 'right' },
135             { justify => 'right' },
136             { justify => 'right' },
137             { justify => 'right' },
138             { justify => 'right' },
139         ],
140     );
141     
142     $pdf->mediabox($width/mm, $height/mm);
143 }
144
145 sub printhead {
146     my ($pdf, $basketgroup, $bookseller) = @_;
147
148     # get library name
149     my $libraryname = C4::Context->preference("LibraryName");
150     # get branch details
151     my $billingdetails  = GetBranchDetail( $basketgroup->{billingplace} );
152     my $deliverydetails = GetBranchDetail( $basketgroup->{deliveryplace} );
153     my $freedeliveryplace = $basketgroup->{freedeliveryplace};
154     # get the subject
155     my $subject;
156
157     # open 1st page (with the header)
158     my $page = $pdf->openpage(1);
159     
160     # create a text
161     my $text = $page->text;
162     
163     # print the libraryname in the header
164     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
165     $text->translate(30/mm, ($height-28.5)/mm);
166     $text->text($libraryname);
167
168     # print order info, on the default PDF
169     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
170     $text->translate(100/mm, ($height-5-48)/mm);
171     $text->text($basketgroup->{'id'});
172     
173     # print the date
174     my $today = output_pref({ dt => dt_from_string, dateonly => 1 });
175     $text->translate(130/mm,  ($height-5-48)/mm);
176     $text->text($today);
177     
178     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
179     
180     # print billing infos
181     $text->translate(100/mm, ($height-86)/mm);
182     $text->text($libraryname);
183     $text->translate(100/mm, ($height-97)/mm);
184     $text->text($billingdetails->{branchname});
185     $text->translate(100/mm, ($height-108.5)/mm);
186     $text->text($billingdetails->{branchphone});
187     $text->translate(100/mm, ($height-115.5)/mm);
188     $text->text($billingdetails->{branchfax});
189     $text->translate(100/mm, ($height-122.5)/mm);
190     $text->text($billingdetails->{branchaddress1});
191     $text->translate(100/mm, ($height-127.5)/mm);
192     $text->text($billingdetails->{branchaddress2});
193     $text->translate(100/mm, ($height-132.5)/mm);
194     $text->text($billingdetails->{branchaddress3});
195     $text->translate(100/mm, ($height-137.5)/mm);
196     $text->text(join(' ', $billingdetails->{branchzip}, $billingdetails->{branchcity}, $billingdetails->{branchcountry}));
197     $text->translate(100/mm, ($height-147.5)/mm);
198     $text->text($billingdetails->{branchemail});
199     
200     # print subject
201     $text->translate(100/mm, ($height-145.5)/mm);
202     $text->text($subject);
203     
204     # print bookseller infos
205     $text->translate(100/mm, ($height-180)/mm);
206     $text->text($bookseller->{name});
207     $text->translate(100/mm, ($height-185)/mm);
208     $text->text($bookseller->{postal});
209     $text->translate(100/mm, ($height-190)/mm);
210     $text->text($bookseller->{address1});
211     $text->translate(100/mm, ($height-195)/mm);
212     $text->text($bookseller->{address2});
213     $text->translate(100/mm, ($height-200)/mm);
214     $text->text($bookseller->{address3});
215     $text->translate(100/mm, ($height-205)/mm);
216     $text->text($bookseller->{accountnumber});
217     
218     # print delivery infos
219     $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
220     $text->translate(50/mm, ($height-237)/mm);
221     if ($freedeliveryplace) {
222         my $start = 242;
223         my @fdp = split('\n', $freedeliveryplace);
224         foreach (@fdp) {
225             $text->text($_);
226             $text->translate( 50 / mm, ( $height - $start ) / mm );
227             $start += 5;
228         }
229     } else {
230         $text->text( $deliverydetails->{branchaddress1} );
231         $text->translate( 50 / mm, ( $height - 242 ) / mm );
232         $text->text( $deliverydetails->{branchaddress2} );
233         $text->translate( 50 / mm, ( $height - 247 ) / mm );
234         $text->text( $deliverydetails->{branchaddress3} );
235         $text->translate( 50 / mm, ( $height - 252 ) / mm );
236         $text->text( join( ' ', $deliverydetails->{branchzip}, $deliverydetails->{branchcity}, $deliverydetails->{branchcountry} ) );
237     }
238     $text->translate(50/mm, ($height-262)/mm);
239     $text->text($basketgroup->{deliverycomment});
240 }
241
242 sub printfooters {
243     my $pdf = shift;
244     for ( 1..$pdf->pages ) {
245         my $page = $pdf->openpage($_);
246         my $text = $page->text;
247         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
248         $text->translate(10/mm,  10/mm);
249         $text->text("Seite $_ / ".$pdf->pages);
250     }
251 }
252
253 sub printpdf {
254     my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
255     # open the default PDF that will be used for base (1st page already filled)
256     my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout2pagesde.pdf';
257     my $pdf = PDF::API2->open($pdf_template);
258     $pdf->pageLabel( 0, {
259         -style => 'roman',
260     } ); # start with roman numbering
261     # fill the 1st page (basketgroup information)
262     printhead($pdf, $basketgroup, $bookseller);
263     # fill other pages (orders)
264     printorders($pdf, $basketgroup, $baskets, $orders);
265     # print something on each page (usually the footer, but you could also put a header
266     printfooters($pdf);
267     return $pdf->stringify;
268 }
269
270 1;