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