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, 2013 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 3 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, see <http://www.gnu.org/licenses>.
22 #you can use any PDF::API2 module, all you need to do is return the stringifyed pdf object from the printpdf sub.
23 package pdfformat
::layout3pagesfr
;
24 use vars
qw($VERSION @ISA @EXPORT);
25 use Number::Format qw(format_price);
27 use List
::MoreUtils qw
/uniq/;
32 use C4
::Branch
qw(GetBranchDetail GetBranchName);
36 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
37 # set the version for version checking
40 @EXPORT = qw(printpdf);
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 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.
46 use constant mm
=> 25.4 / 72;
47 use constant
in => 1 / 72;
52 my ($height, $width) = (297, 210);
56 my ($pdf, $basketgroup, $baskets, $orders) = @_;
58 my $cur_format = C4
::Context
->preference("CurrencyFormat");
61 if ( $cur_format eq 'FR' ) {
62 $num = new Number
::Format
(
63 'decimal_fill' => '2',
64 'decimal_point' => ',',
65 'int_curr_symbol' => '',
66 'mon_thousands_sep' => ' ',
67 'thousands_sep' => ' ',
68 'mon_decimal_point' => ','
70 } else { # US by default..
71 $num = new Number
::Format
(
72 'int_curr_symbol' => '',
73 'mon_thousands_sep' => ',',
74 'mon_decimal_point' => '.'
78 $pdf->mediabox($height/mm, $width/mm);
80 for my $basket (@
$baskets){
81 my $page = $pdf->page();
83 # print basket header (box)
85 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
90 my $text = $page->text;
91 # add basketgroup number
92 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
93 $text->translate(20/mm, ($height-15)/mm);
94 $text->text("Commande N°".$basketgroup->{'id'}.". Panier N° ".$basket->{basketno
}.". ".$basket->{booksellernote
});
95 $text->translate(20/mm, ($height-20)/mm);
96 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 4/mm
);
97 $text->text( ( $basket->{billingplace
} ?
"Facturation à " . C4
::Branch
::GetBranchName
( $basket->{billingplace
} ) : "" )
98 . ( $basket->{billingplace
} and $basket->{deliveryplace
} ?
" et " : "" )
99 . ( $basket->{deliveryplace
} ?
"livraison à " . C4
::Branch
::GetBranchName
( $basket->{deliveryplace
}) : "" )
102 my $pdftable = new PDF
::Table
();
105 my @keys = ('Document', 'Qté', 'Prix', 'Prix net', '% Remise', 'Remise', 'Taux TVA', 'Total HT', 'Total TTC');
106 for my $bkey (@keys) {
107 push(@
$arrbasket, $bkey);
109 push(@
$abaskets, $arrbasket);
112 foreach my $line (@
{$orders->{$basket->{basketno
}}}) {
115 if ( C4
::Context
->preference("marcflavour") eq 'UNIMARC' ) {
116 $titleinfo = $line->{title
} . " / " . $line->{author
} .
117 ( $line->{isbn
} ?
" ISBN : " . $line->{isbn
} : '' ) .
118 ( $line->{en
} ?
" EN : " . $line->{en
} : '' ) .
119 ( $line->{itemtype
} ?
", " . $line->{itemtype
} : '' ) .
120 ( $line->{edition
} ?
", " . $line->{edition
} : '' ) .
121 ( $line->{publishercode
} ?
' publié par '. $line->{publishercode
} : '') .
122 ( $line->{publicationyear
} ?
', '. $line->{publicationyear
} : '');
124 else { # MARC21, NORMARC
125 $titleinfo = $line->{title
} . " " . $line->{author
} .
126 ( $line->{isbn
} ?
" ISBN : " . $line->{isbn
} : '' ) .
127 ( $line->{en
} ?
" EN : " . $line->{en
} : '' ) .
128 ( $line->{itemtype
} ?
" " . $line->{itemtype
} : '' ) .
129 ( $line->{edition
} ?
", " . $line->{edition
} : '' ) .
130 ( $line->{publishercode
} ?
' publié par '. $line->{publishercode
} : '') .
131 ( $line->{copyrightdate
} ?
' '. $line->{copyrightdate
} : '');
135 $titleinfo. ($line->{order_vendornote
} ?
"\n----------------\nNote pour le fournisseur : ". $line->{order_vendornote
} : '' ),
137 $num->format_price($line->{rrpgste
}),
138 $num->format_price($line->{rrpgsti
}),
139 $num->format_price($line->{discount
}).'%',
140 $num->format_price($line->{rrpgste
} - $line->{ecostgste
}),
141 $num->format_price($line->{gstrate
} * 100).'%',
142 $num->format_price($line->{totalgste
}),
143 $num->format_price($line->{totalgsti
}),
145 push(@
$abaskets, $arrbasket);
148 $pdftable->table($pdf, $page, $abaskets,
150 w
=> ($width - 20)/mm
,
157 background_color_odd
=> "lightgray",
158 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
161 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
168 min_w
=> 85/mm
, # Minimum column width.
171 justify
=> 'right', # One of left|right ,
174 justify
=> 'right', # One of left|right ,
177 justify
=> 'right', # One of left|right ,
180 justify
=> 'right', # One of left|right ,
183 justify
=> 'right', # One of left|right ,
186 justify
=> 'right', # One of left|right ,
189 justify
=> 'right', # One of left|right ,
192 justify
=> 'right', # One of left|right ,
197 $pdf->mediabox($width/mm, $height/mm);
201 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
204 my $libraryname = C4
::Context
->preference("LibraryName");
206 my $cur_format = C4
::Context
->preference("CurrencyFormat");
209 if ( $cur_format eq 'FR' ) {
210 $num = new Number
::Format
(
211 'decimal_fill' => '2',
212 'decimal_point' => ',',
213 'int_curr_symbol' => '',
214 'mon_thousands_sep' => ' ',
215 'thousands_sep' => ' ',
216 'mon_decimal_point' => ','
218 } else { # US by default..
219 $num = new Number
::Format
(
220 'int_curr_symbol' => '',
221 'mon_thousands_sep' => ',',
222 'mon_decimal_point' => '.'
226 $pdf->mediabox($width/mm, $height/mm);
227 my $page = $pdf->openpage(2);
229 my $text = $page->text;
231 # add basketgroup number
232 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
233 $text->translate(($width-40)/mm, ($height-53)/mm);
234 $text->text("".$basketgroup->{'id'});
235 # print the libraryname in the header
236 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
237 $text->translate(30/mm, ($height-28.5)/mm);
238 $text->text($libraryname);
239 my $pdftable = new PDF
::Table
();
242 # header of the table
243 my @keys = ('Lot', 'Panier', 'Prix', 'Prix net', 'Taux TVA', 'TVA', 'Remise', 'Total HT', 'Total TTC');
244 for my $bkey (@keys) {
245 push(@
$arrbasket, $bkey);
247 my ($grandtotalrrpgsti, $grandtotalrrpgste, $grandtotalgsti, $grandtotalgste, $grandtotalgstvalue, $grandtotaldiscount);
248 # calculate each basket total
249 push(@
$abaskets, $arrbasket);
250 for my $basket (@
$hbaskets) {
253 my ($totalrrpgste, $totalrrpgsti, $totalgste, $totalgsti, $totalgstvalue, $totaldiscount);
254 my $ords = $orders->{$basket->{basketno
}};
255 my $ordlength = @
$ords;
256 foreach my $ord (@
$ords) {
257 $totalgste += $ord->{totalgste
};
258 $totalgsti += $ord->{totalgsti
};
259 $totalgstvalue += $ord->{gstvalue
};
260 $totaldiscount += ($ord->{rrpgste
} - $ord->{ecostgste
} ) * $ord->{quantity
};
261 $totalrrpgste += $ord->{rrpgste
} * $ord->{quantity
};
262 $totalrrpgsti += $ord->{rrpgsti
} * $ord->{quantity
};
263 push @gst, $ord->{gstrate
};
265 @gst = uniq
map { $_ * 100 } @gst;
266 $totalgsti = $num->round($totalgsti);
267 $totalgste = $num->round($totalgste);
268 $grandtotalrrpgste += $totalrrpgste;
269 $grandtotalrrpgsti += $totalrrpgsti;
270 $grandtotalgsti += $totalgsti;
271 $grandtotalgste += $totalgste;
272 $grandtotalgstvalue += $totalgstvalue;
273 $grandtotaldiscount += $totaldiscount;
274 my @gst_string = map{$num->format_price( $_ ) . '%'} @gst;
276 $basket->{contractname
},
277 $basket->{basketname
} . ' (No. ' . $basket->{basketno
} . ')',
278 $num->format_price($totalrrpgste),
279 $num->format_price($totalrrpgsti),
281 $num->format_price($totalgstvalue),
282 $num->format_price($totaldiscount),
283 $num->format_price($totalgste),
284 $num->format_price($totalgsti)
286 push(@
$abaskets, $arrbasket);
290 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);
291 push @
$abaskets,$arrbasket;
292 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
294 $pdftable->table($pdf, $page, $abaskets,
296 w
=> ($width - 10)/mm
,
301 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
305 background_color_odd
=> "lightgray",
332 $pdf->mediabox($height/mm, $width/mm);
336 my ($pdf, $basketgroup, $bookseller) = @_;
339 my $libraryname = C4
::Context
->preference("LibraryName");
341 my $billingdetails = GetBranchDetail
( $basketgroup->{billingplace
} );
342 my $deliverydetails = GetBranchDetail
( $basketgroup->{deliveryplace
} );
343 my $freedeliveryplace = $basketgroup->{freedeliveryplace
};
347 # open 1st page (with the header)
348 my $page = $pdf->openpage(1);
351 my $text = $page->text;
353 # print the libraryname in the header
354 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
355 $text->translate(30/mm, ($height-28.5)/mm);
356 $text->text($libraryname);
358 # print order info, on the default PDF
359 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 8/mm
);
360 $text->translate(100/mm, ($height-5-48)/mm);
361 $text->text($basketgroup->{'id'});
364 my $today = C4
::Dates
->today();
365 $text->translate(130/mm, ($height-5-48)/mm);
368 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 4/mm
);
370 # print billing infos
371 $text->translate(100/mm, ($height-86)/mm);
372 $text->text($libraryname);
373 $text->translate(100/mm, ($height-97)/mm);
374 $text->text($billingdetails->{branchname
});
375 $text->translate(100/mm, ($height-108.5)/mm);
376 $text->text($billingdetails->{branchphone
});
377 $text->translate(100/mm, ($height-115.5)/mm);
378 $text->text($billingdetails->{branchfax
});
379 $text->translate(100/mm, ($height-122.5)/mm);
380 $text->text($billingdetails->{branchaddress1
});
381 $text->translate(100/mm, ($height-127.5)/mm);
382 $text->text($billingdetails->{branchaddress2
});
383 $text->translate(100/mm, ($height-132.5)/mm);
384 $text->text($billingdetails->{branchaddress3
});
385 $text->translate(100/mm, ($height-137.5)/mm);
386 $text->text(join(' ', $billingdetails->{branchzip
}, $billingdetails->{branchcity
}, $billingdetails->{branchcountry
}));
387 $text->translate(100/mm, ($height-147.5)/mm);
388 $text->text($billingdetails->{branchemail
});
391 $text->translate(100/mm, ($height-145.5)/mm);
392 $text->text($subject);
394 # print bookseller infos
395 $text->translate(100/mm, ($height-180)/mm);
396 $text->text($bookseller->{name
});
397 $text->translate(100/mm, ($height-185)/mm);
398 $text->text($bookseller->{postal
});
399 $text->translate(100/mm, ($height-190)/mm);
400 $text->text($bookseller->{address1
});
401 $text->translate(100/mm, ($height-195)/mm);
402 $text->text($bookseller->{address2
});
403 $text->translate(100/mm, ($height-200)/mm);
404 $text->text($bookseller->{address3
});
405 $text->translate(100/mm, ($height-205)/mm);
406 $text->text($bookseller->{accountnumber
});
408 # print delivery infos
409 $text->font( $pdf->corefont("Times-Bold", -encoding
=> "utf8"), 4/mm
);
410 $text->translate(50/mm, ($height-237)/mm);
411 if ($freedeliveryplace) {
413 my @fdp = split('\n', $freedeliveryplace);
416 $text->translate( 50 / mm, ( $height - $start ) / mm
);
420 $text->text($deliverydetails->{branchaddress1
});
421 $text->translate(50/mm, ($height-242)/mm);
422 $text->text($deliverydetails->{branchaddress2
});
423 $text->translate(50/mm, ($height-247)/mm);
424 $text->text($deliverydetails->{branchaddress3
});
425 $text->translate(50/mm, ($height-252)/mm);
426 $text->text(join(' ', $deliverydetails->{branchzip
}, $deliverydetails->{branchcity
}, $deliverydetails->{branchcountry
}));
428 $text->translate(50/mm, ($height-262)/mm);
429 $text->text($basketgroup->{deliverycomment
});
434 for (my $i=1;$i <= $pdf->pages;$i++) {
435 my $page = $pdf->openpage($i);
436 my $text = $page->text;
437 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 3/mm
);
438 $text->translate(10/mm, 10/mm);
439 $text->text("Page $i / ".$pdf->pages);
444 my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
445 # open the default PDF that will be used for base (1st page already filled)
446 my $pdf_template = C4
::Context
->config('intrahtdocs') . '/' . C4
::Context
->preference('template') . '/pdf/layout3pagesfr.pdf';
447 my $pdf = PDF
::API2
->open($pdf_template);
448 $pdf->pageLabel( 0, {
450 } ); # start with roman numbering
451 # fill the 1st page (basketgroup information)
452 printhead
($pdf, $basketgroup, $bookseller);
453 # fill the 2nd page (orders summary)
454 printbaskets
($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
455 # fill other pages (orders)
456 printorders
($pdf, $basketgroup, $baskets, $orders);
457 # print something on each page (usually the footer, but you could also put a header
459 return $pdf->stringify;