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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
::layout3pages
;
25 use vars
qw($VERSION @ISA @EXPORT);
26 use Number::Format qw(format_price);
28 use List
::MoreUtils qw
/uniq/;
33 use C4
::Branch
qw(GetBranchDetail GetBranchName);
37 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
38 # set the version for version checking
41 @EXPORT = qw(printpdf);
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 tranform 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;
53 my ($height, $width) = (297, 210);
57 my ($pdf, $basketgroup, $baskets, $orders) = @_;
59 my $cur_format = C4
::Context
->preference("CurrencyFormat");
62 if ( $cur_format eq 'FR' ) {
63 $num = new Number
::Format
(
64 'decimal_fill' => '2',
65 'decimal_point' => ',',
66 'int_curr_symbol' => '',
67 'mon_thousands_sep' => ' ',
68 'thousands_sep' => ' ',
69 'mon_decimal_point' => ','
71 } else { # US by default..
72 $num = new Number
::Format
(
73 'int_curr_symbol' => '',
74 'mon_thousands_sep' => ',',
75 'mon_decimal_point' => '.'
79 $pdf->mediabox($height/mm, $width/mm);
81 for my $basket (@
$baskets){
82 my $page = $pdf->page();
84 # print basket header (box)
86 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
91 my $text = $page->text;
92 # add basketgroup number
93 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
94 $text->translate(20/mm, ($height-15)/mm);
95 $text->text("Order no. ".$basketgroup->{'id'}.". Basket no. ".$basket->{basketno
}.". ".$basket->{booksellernote
});
96 $text->translate(20/mm, ($height-20)/mm);
97 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 4/mm
);
98 $text->text( ( $basket->{billingplace
} ?
"Billing at " . C4
::Branch
::GetBranchName
( $basket->{billingplace
} ) : "" )
99 . ( $basket->{billingplace
} and $basket->{deliveryplace
} ?
" and " : "" )
100 . ( $basket->{deliveryplace
} ?
"delivery at " . C4
::Branch
::GetBranchName
( $basket->{deliveryplace
}) : "" )
103 my $pdftable = new PDF
::Table
();
106 my @keys = ('Document', 'Qty', 'RRP tax exc.', 'RRP tax inc.', 'Discount', 'Discount price', 'GST rate', 'Total tax exc.', 'Total tax inc.');
107 for my $bkey (@keys) {
108 push(@
$arrbasket, $bkey);
110 push(@
$abaskets, $arrbasket);
111 foreach my $line (@
{$orders->{$basket->{basketno
}}}) {
114 $line->{title
} . " / " . $line->{author
} . ( $line->{isbn
} ?
" ISBN : " . $line->{isbn
} : '' ) . ( $line->{en
} ?
" EN : " . $line->{en
} : '' ) . ", " . $line->{itemtype
} . ( $line->{publishercode
} ?
' published by '. $line->{publishercode
} : ""),
116 $num->format_price($line->{rrpgste
}),
117 $num->format_price($line->{rrpgsti
}),
118 $num->format_price($line->{discount
}).'%',
119 $num->format_price($line->{rrpgste
} - $line->{ecostgste
}),
120 $num->format_price($line->{gstrate
} * 100).'%',
121 $num->format_price($line->{totalgste
}),
122 $num->format_price($line->{totalgsti
}),
124 push(@
$abaskets, $arrbasket);
127 $pdftable->table($pdf, $page, $abaskets,
129 w
=> ($width - 20)/mm
,
136 background_color_odd
=> "lightgray",
137 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
140 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
147 min_w
=> 85/mm
, # Minimum column width.
150 justify
=> 'right', # One of left|right ,
153 justify
=> 'right', # One of left|right ,
156 justify
=> 'right', # One of left|right ,
159 justify
=> 'right', # One of left|right ,
162 justify
=> 'right', # One of left|right ,
165 justify
=> 'right', # One of left|right ,
168 justify
=> 'right', # One of left|right ,
171 justify
=> 'right', # One of left|right ,
176 $pdf->mediabox($width/mm, $height/mm);
180 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
183 my $libraryname = C4
::Context
->preference("LibraryName");
185 my $cur_format = C4
::Context
->preference("CurrencyFormat");
188 if ( $cur_format eq 'FR' ) {
189 $num = new Number
::Format
(
190 'decimal_fill' => '2',
191 'decimal_point' => ',',
192 'int_curr_symbol' => '',
193 'mon_thousands_sep' => ' ',
194 'thousands_sep' => ' ',
195 'mon_decimal_point' => ','
197 } else { # US by default..
198 $num = new Number
::Format
(
199 'int_curr_symbol' => '',
200 'mon_thousands_sep' => ',',
201 'mon_decimal_point' => '.'
205 $pdf->mediabox($width/mm, $height/mm);
206 my $page = $pdf->openpage(2);
208 my $text = $page->text;
210 # add basketgroup number
211 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
212 $text->translate(($width-40)/mm, ($height-53)/mm);
213 $text->text("".$basketgroup->{'id'});
214 # print the libraryname in the header
215 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
216 $text->translate(30/mm, ($height-28.5)/mm);
217 $text->text($libraryname);
218 my $pdftable = new PDF
::Table
();
221 # header of the table
222 my @keys = ('Lot', 'Basket (No.)','Total RRP tax exc.', 'Total RRP tax inc.', 'GST rate', 'GST', 'Total discount', 'Total tax exc.', 'Total tax inc.');
223 for my $bkey (@keys) {
224 push(@
$arrbasket, $bkey);
226 my ($grandtotalrrpgsti, $grandtotalrrpgste, $grandtotalgsti, $grandtotalgste, $grandtotalgstvalue, $grandtotaldiscount);
227 # calculate each basket total
228 push(@
$abaskets, $arrbasket);
229 for my $basket (@
$hbaskets) {
232 my ($totalrrpgste, $totalrrpgsti, $totalgste, $totalgsti, $totalgstvalue, $totaldiscount);
233 my $ords = $orders->{$basket->{basketno
}};
234 my $ordlength = @
$ords;
235 foreach my $ord (@
$ords) {
236 $totalgste += $ord->{totalgste
};
237 $totalgsti += $ord->{totalgsti
};
238 $totalgstvalue += $ord->{gstvalue
};
239 $totaldiscount += ($ord->{rrpgste
} - $ord->{ecostgste
} ) * $ord->{quantity
};
240 $totalrrpgste += $ord->{rrpgste
} * $ord->{quantity
};
241 $totalrrpgsti += $ord->{rrpgsti
} * $ord->{quantity
};
242 push @gst, $ord->{gstrate
};
244 @gst = uniq
map { $_ * 100 } @gst;
245 $totalgsti = $num->round($totalgsti);
246 $totalgste = $num->round($totalgste);
247 $grandtotalrrpgste += $totalrrpgste;
248 $grandtotalrrpgsti += $totalrrpgsti;
249 $grandtotalgsti += $totalgsti;
250 $grandtotalgste += $totalgste;
251 $grandtotalgstvalue += $totalgstvalue;
252 $grandtotaldiscount += $totaldiscount;
253 my @gst_string = map{$num->format_price( $_ ) . '%'} @gst;
255 $basket->{contractname
},
256 $basket->{basketname
} . ' (No. ' . $basket->{basketno
} . ')',
257 $num->format_price($totalrrpgste),
258 $num->format_price($totalrrpgsti),
260 $num->format_price($totalgstvalue),
261 $num->format_price($totaldiscount),
262 $num->format_price($totalgste),
263 $num->format_price($totalgsti)
265 push(@
$abaskets, $arrbasket);
269 push @
$arrbasket,'','Total', $num->format_price($grandtotalrrpgste), $num->format_price($grandtotalrrpgsti), '', $num->format_price($grandtotalgstvalue), $num->format_price($grandtotaldiscount), $num->format_price($grandtotalgste), $num->format_price($grandtotalgsti);
270 push @
$abaskets,$arrbasket;
271 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
273 $pdftable->table($pdf, $page, $abaskets,
275 w
=> ($width - 10)/mm
,
280 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
284 background_color_odd
=> "lightgray",
311 $pdf->mediabox($height/mm, $width/mm);
315 my ($pdf, $basketgroup, $bookseller) = @_;
318 my $libraryname = C4
::Context
->preference("LibraryName");
320 my $billingdetails = GetBranchDetail
( $basketgroup->{billingplace
} );
321 my $deliverydetails = GetBranchDetail
( $basketgroup->{deliveryplace
} );
322 my $freedeliveryplace = $basketgroup->{freedeliveryplace
};
326 # open 1st page (with the header)
327 my $page = $pdf->openpage(1);
330 my $text = $page->text;
332 # print the libraryname in the header
333 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
334 $text->translate(30/mm, ($height-28.5)/mm);
335 $text->text($libraryname);
337 # print order info, on the default PDF
338 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 8/mm
);
339 $text->translate(100/mm, ($height-5-48)/mm);
340 $text->text($basketgroup->{'id'});
343 my $today = C4
::Dates
->today();
344 $text->translate(130/mm, ($height-5-48)/mm);
347 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 4/mm
);
349 # print billing infos
350 $text->translate(100/mm, ($height-86)/mm);
351 $text->text($libraryname);
352 $text->translate(100/mm, ($height-97)/mm);
353 $text->text($billingdetails->{branchname
});
354 $text->translate(100/mm, ($height-108.5)/mm);
355 $text->text($billingdetails->{branchphone
});
356 $text->translate(100/mm, ($height-115.5)/mm);
357 $text->text($billingdetails->{branchfax
});
358 $text->translate(100/mm, ($height-122.5)/mm);
359 $text->text($billingdetails->{branchaddress1
});
360 $text->translate(100/mm, ($height-127.5)/mm);
361 $text->text($billingdetails->{branchaddress2
});
362 $text->translate(100/mm, ($height-132.5)/mm);
363 $text->text($billingdetails->{branchaddress3
});
364 $text->translate(100/mm, ($height-137.5)/mm);
365 $text->text(join(' ', $billingdetails->{branchzip
}, $billingdetails->{branchcity
}, $billingdetails->{branchcountry
}));
366 $text->translate(100/mm, ($height-147.5)/mm);
367 $text->text($billingdetails->{branchemail
});
370 $text->translate(100/mm, ($height-145.5)/mm);
371 $text->text($subject);
373 # print bookseller infos
374 $text->translate(100/mm, ($height-180)/mm);
375 $text->text($bookseller->{name
});
376 $text->translate(100/mm, ($height-185)/mm);
377 $text->text($bookseller->{postal
});
378 $text->translate(100/mm, ($height-190)/mm);
379 $text->text($bookseller->{address1
});
380 $text->translate(100/mm, ($height-195)/mm);
381 $text->text($bookseller->{address2
});
382 $text->translate(100/mm, ($height-200)/mm);
383 $text->text($bookseller->{address3
});
385 # print delivery infos
386 $text->font( $pdf->corefont("Times-Bold", -encoding
=> "utf8"), 4/mm
);
387 $text->translate(50/mm, ($height-237)/mm);
388 if ($freedeliveryplace) {
390 my @fdp = split('\n', $freedeliveryplace);
393 $text->translate( 50 / mm, ( $height - $start ) / mm
);
397 $text->text($deliverydetails->{branchaddress1
});
398 $text->translate(50/mm, ($height-242)/mm);
399 $text->text($deliverydetails->{branchaddress2
});
400 $text->translate(50/mm, ($height-247)/mm);
401 $text->text($deliverydetails->{branchaddress3
});
402 $text->translate(50/mm, ($height-252)/mm);
403 $text->text(join(' ', $deliverydetails->{branchzip
}, $deliverydetails->{branchcity
}, $deliverydetails->{branchcountry
}));
405 $text->translate(50/mm, ($height-262)/mm);
406 $text->text($basketgroup->{deliverycomment
});
411 for (my $i=1;$i <= $pdf->pages;$i++) {
412 my $page = $pdf->openpage($i);
413 my $text = $page->text;
414 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 3/mm
);
415 $text->translate(10/mm, 10/mm);
416 $text->text("Page $i / ".$pdf->pages);
421 my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
422 # open the default PDF that will be used for base (1st page already filled)
423 my $pdf_template = C4
::Context
->config('intrahtdocs') . '/' . C4
::Context
->preference('template') . '/pdf/layout3pages.pdf';
424 my $pdf = PDF
::API2
->open($pdf_template);
425 $pdf->pageLabel( 0, {
427 } ); # start with roman numbering
428 # fill the 1st page (basketgroup information)
429 printhead
($pdf, $basketgroup, $bookseller);
430 # fill the 2nd page (orders summary)
431 printbaskets
($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
432 # fill other pages (orders)
433 printorders
($pdf, $basketgroup, $baskets, $orders);
434 # print something on each page (usually the footer, but you could also put a header
436 return $pdf->stringify;