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::ffzg;
25 use vars qw($VERSION @ISA @EXPORT);
27 use List::MoreUtils qw/uniq/;
32 use Koha::Number::Price;
35 use Data::Dump qw(dump); # FIXME
39 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
40 # set the version for version checking
43 @EXPORT = qw(printpdf);
47 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
48 #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.
49 use constant mm => 25.4 / 72;
50 use constant in => 1 / 72;
55 my ($height, $width) = (297, 210);
59 my ($pdf, $font) = @_;
60 # $font = Times, Times-Bold
63 'Times' => '/usr/share/fonts/truetype/dejavu/DejaVuSansCondensed.ttf',
64 'Times-Bold' => '/usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-Bold.ttf',
67 if ( my $ttf = $mapping->{$font} ) {
68 # warn "XXX using $ttf font insted of $font";
69 return $pdf->ttfont( $ttf );
72 warn "ERROR: can't find ttf font $font, fallback to corefont";
73 return $pdf->corefont($font, -encoding => "utf8");
80 my ($pdf, $basketgroup, $bookseller) = @_;
83 my $libraryname = C4::Context->preference("LibraryName");
85 my $freedeliveryplace = $basketgroup->{freedeliveryplace};
86 warn "XXX freedeliveryplace = ", dump($freedeliveryplace);
88 # open 1st page (with the header)
89 my $page = $pdf->openpage(1);
92 my $text = $page->text;
93 $text->fillcolor('black');
94 # $text->fillcolor('red');
97 # print order info, on the default PDF
98 $text->font( _pdf_font( $pdf, "Times-Bold" ), 12/pt );
99 # $text->translate(100/mm, ($height-24)/mm);
100 $text->translate(283/pt, 774/pt);
101 $text->text( $basketgroup->{'id'} . '-' . $basketgroup->{'ffzg_nr'} . '/' . $basketgroup->{'ffzg_year'} );
103 $text->font( _pdf_font( $pdf, "Times" ), 10/pt );
105 # print the date FIXME
106 # $text->translate(140/mm, ($height-24)/mm);
107 $text->translate(420.75/pt, 773.93/pt);
108 $text->text($basketgroup->{ffzg_date});
110 #warn "XXX bookseller ", dump( $bookseller );
112 # print bookseller infos
114 foreach my $line ( $bookseller->{name}, split(/[\n\r]+/, $bookseller->{postal}) ) {
115 $text->translate(336/pt, $y_pt/pt);
120 # Lokacija za isporuku -- Zbirka...
121 $text->translate(62/pt, 590/pt);
122 $text->text($basketgroup->{deliverycomment});
124 $text->translate(58/pt, 556/pt);
125 $text->text("Narudžbenicu ispisala: " . C4::Context->userenv->{"firstname"} . " " . C4::Context->userenv->{"surname"} );
126 # $text->translate(58/pt, 556/pt - 10/pt);
127 # $text->text("basketgroup: " . $basketgroup->{'id'});
132 my ($pdf, $basketgroup, $baskets, $orders) = @_;
134 my $cur_format = C4::Context->preference("CurrencyFormat");
138 'košarica / narudžba',
155 for my $basket (@$baskets){
157 #warn "XXX basket = ",dump( $basket );
162 foreach my $order (@{$orders->{$basket->{basketno}}}) {
163 #warn "XXX order = ",dump($order);
165 $order->{discount_value} = $order->{rrpgste} - $order->{ecostgste};
166 $basket_total->{$_} += $order->{$_} foreach (qw( totalgste totalgsti gstvalue quantity ));
167 $basket_total->{$_} += $order->{$_} * $order->{quantity} foreach (qw( rrpgste rrpgsti ecostgste discount_value ));
168 push @{$basket_total->{gstrate}}, $order->{gstrate};
171 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
172 $titleinfo = $order->{title} . " / " . $order->{author} .
173 ( $order->{isbn} ? " ISBN: " . $order->{isbn} : '' ) .
174 ( $order->{en} ? " EN: " . $order->{en} : '' ) .
175 ( $order->{itemtype} ? ", " . $order->{itemtype} : '' ) .
176 ( $order->{edition} ? ", " . $order->{edition} : '' ) .
177 ( $order->{publishercode} ? ' published by '. $order->{publishercode} : '') .
178 ( $order->{publicationyear} ? ', '. $order->{publicationyear} : '');
180 else { # MARC21, NORMARC
181 $titleinfo = $order->{title} . " " . $order->{author} .
182 ( $order->{isbn} ? " ISBN: " . $order->{isbn} : '' ) .
183 ( $order->{en} ? " EN: " . $order->{en} : '' ) .
184 ( $order->{itemtype} ? " " . $order->{itemtype} : '' ) .
185 ( $order->{edition} ? ", " . $order->{edition} : '' ) .
186 ( $order->{publishercode} ? ' published by '. $order->{publishercode} : '') .
187 ( $order->{copyrightdate} ? ' '. $order->{copyrightdate} : '');
192 $basket->{basketno} . ' / ' . $order->{ordernumber},
193 $titleinfo, #. ($order->{order_vendornote} ? "\n----------------\nNote for vendor : " . $order->{order_vendornote} : '' ),
195 Koha::Number::Price->new( $order->{rrpgste} )->format,
196 Koha::Number::Price->new( $order->{rrpgsti} )->format,
197 Koha::Number::Price->new( $order->{discount} )->format . '%',
198 Koha::Number::Price->new( $order->{discount_value})->format,
199 Koha::Number::Price->new( $order->{gstrate} * 100 )->format . '%',
200 Koha::Number::Price->new( $order->{totalgste} )->format,
201 Koha::Number::Price->new( $order->{totalgsti} )->format,
205 $basket_total->{$_} = $basket->{$_} foreach qw( basketname basketno );
206 $basket_total->{gstrate} = join(" ", map { Koha::Number::Price->new($_ * 100)->format . '%' } uniq @{ $basket_total->{gstrate} } );
208 push @$basket_totals, $basket_total;
210 $order_total->{$_} += $basket_total->{$_} foreach qw( rrpgste rrpgsti totalgste totalgsti gstvalue quantity ecostgste discount_value );
211 push @{$order_total->{gstrate}}, $basket_total->{gstrate};
214 $order_total->{gstrate} = join(" ", uniq @{ $order_total->{gstrate} } );
216 warn "XXX basket_totals = ",dump( $basket_totals );
217 warn "XXX order_total = ", dump( $order_total );
223 $order_total->{quantity},
224 Koha::Number::Price->new( $order_total->{rrpgste} )->format,
225 Koha::Number::Price->new( $order_total->{rrpgsti} )->format,
227 Koha::Number::Price->new( $order_total->{discount_value})->format,
229 Koha::Number::Price->new( $order_total->{totalgste} )->format,
230 Koha::Number::Price->new( $order_total->{totalgsti} )->format,
244 foreach my $basket_total ( @$basket_totals ) {
245 push @$a_baskets, [ map {
247 ? $basket_total->{$_}
248 : Koha::Number::Price->new($basket_total->{$_})->format . ( m/^gstrate$/ ? '%' : '' )
261 push @$a_baskets, [ '', 'Ukupno', map { Koha::Number::Price->new($order_total->{$_})->format } qw(
270 $a_baskets->[-1]->[4] = ''; # remove gstreate from total
272 $pdf->mediabox($height/mm, $width/mm);
273 my $page = $pdf->openpage(1);
274 my $pdftable = new PDF::Table();
276 my ($end_page, $pages_spanned, $table_bot_y) =
277 $pdftable->table($pdf, $page, $a_titles,
279 w => ($width - 20)/mm,
281 next_y => $height/mm - 25/mm,
282 start_h => $height/mm - 470/pt, # - 25/mm,
283 next_h => $height/mm - ( 2 * 25/mm ),
286 background_color_odd => "lightgray",
287 font => _pdf_font( $pdf, "Times" ),
290 font => _pdf_font( $pdf, "Times" ),
292 font_color => 'white',
300 { justify => 'right' },
301 { justify => 'right' },
302 { justify => 'right' },
303 { justify => 'right' },
304 { justify => 'right' },
305 { justify => 'right' },
306 { justify => 'right' },
307 { justify => 'right' },
311 $table_bot_y -= 10/mm;
313 if ( $table_bot_y < 25/mm + ( 3 + @$a_baskets ) * 12/pt ) {
314 warn "XXX force baskets to next page\n";
315 $table_bot_y = $height/mm - 25/mm;
316 $end_page = $pdf->page();
319 my $text = $end_page->text;
320 $text->font( _pdf_font( $pdf, "Times" ), 10/pt );
321 $text->translate(20/mm, $table_bot_y );
322 # $text->fillcolor('red');
323 $text->text("Trošak po košaricama");
325 $table_bot_y -= 5/mm;
328 $pdftable->table($pdf, $page, $a_baskets,
330 w => ($width - 20)/mm,
331 start_y => $table_bot_y,
337 background_color_odd => "lightgray",
338 font => _pdf_font( $pdf, "Times" ),
341 font => _pdf_font( $pdf, "Times" ),
343 font_color => 'white',
350 { justify => 'right' },
351 { justify => 'right' },
352 { justify => 'right' },
353 { justify => 'right' },
354 { justify => 'right' },
355 { justify => 'right' },
356 { justify => 'right' },
362 my ($pdf, $basketgroup) = @_;
363 for (my $i=1;$i <= $pdf->pages;$i++) {
364 my $page = $pdf->openpage($i);
365 my $text = $page->text;
366 $text->font( _pdf_font( $pdf, "Times" ), 3/mm );
367 $text->translate(10/mm, 10/mm);
368 $text->text("Page $i / ".$pdf->pages);
369 # $text->text("Page $i / ".$pdf->pages . " basketgroup: " . $basketgroup->{id});
370 $text->translate($width/mm - 40/mm, 10/mm);
371 $text->text("basketgroup: " . $basketgroup->{id});
376 my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
377 warn "XXX basketgroup = ",dump($basketgroup);
379 if ( ! $basketgroup->{ffzg_year} ) {
380 if ( $basketgroup->{ffzg_date} =~ m/^(\d\d\d\d)-(\d\d)-(\d\d)\s.*/ ) {
381 my ( $yyyy, $mm, $dd ) = ( $1, $2, $3 );
382 my $dbh = C4::Context->dbh;
384 my $sth = $dbh->prepare(qq{ select max(ffzg_nr) from aqbasketgroups where ffzg_year = ? });
385 $sth->execute( $yyyy );
386 my ($nr) = $sth->fetchrow_array;
388 $sth = $dbh->prepare(qq{ update aqbasketgroups set ffzg_year = ?, ffzg_nr = ? where id = ? });
389 $sth->execute( $yyyy, $nr, $basketgroup->{id} );
392 warn "XXX ",$basketgroup->{id}, " basketgroup got $yyyy-$nr\n";
394 $basketgroup->{ffzg_year} = $yyyy;
395 $basketgroup->{ffzg_nr} = $nr;
399 $basketgroup->{ffzg_date} =~ s/\s.+//; # strip time from datetime
401 # open the default PDF that will be used for base (1st page already filled)
402 my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/ffzg-narudzbenica.pdf';
403 my $pdf = PDF::API2->open($pdf_template);
404 $pdf->pageLabel( 0, {
406 } ); # start with roman numbering
407 # fill the 1st page (basketgroup information)
408 printhead($pdf, $basketgroup, $bookseller);
410 print_tables($pdf, $basketgroup, $baskets, $orders);
413 # print something on each page (usually the footer, but you could also put a header
414 printfooters($pdf, $basketgroup);
415 return $pdf->stringify;