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