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