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