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