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