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