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);
27 use List::MoreUtils qw/uniq/;
32 use C4::Branch qw(GetBranchDetail GetBranchName);
34 use Koha
::Number
::Price
;
38 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
39 # set the version for version checking
42 @EXPORT = qw(printpdf);
46 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
47 #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.
48 use constant mm
=> 25.4 / 72;
49 use constant
in => 1 / 72;
54 my ($height, $width) = (297, 210);
58 my ($pdf, $basketgroup, $baskets, $orders) = @_;
60 my $cur_format = C4
::Context
->preference("CurrencyFormat");
62 $pdf->mediabox($height/mm, $width/mm);
64 for my $basket (@
$baskets){
65 my $page = $pdf->page();
67 # print basket header (box)
69 $box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
74 my $text = $page->text;
75 # add basketgroup number
76 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
77 $text->translate(20/mm, ($height-15)/mm);
78 $text->text("Order no. ".$basketgroup->{'id'}.". Basket no. ".$basket->{basketno
}.". ".$basket->{booksellernote
});
79 $text->translate(20/mm, ($height-20)/mm);
80 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 4/mm
);
81 $text->text( ( $basket->{billingplace
} ?
"Billing at " . C4
::Branch
::GetBranchName
( $basket->{billingplace
} ) : "" )
82 . ( $basket->{billingplace
} and $basket->{deliveryplace
} ?
" and " : "" )
83 . ( $basket->{deliveryplace
} ?
"delivery at " . C4
::Branch
::GetBranchName
( $basket->{deliveryplace
}) : "" )
86 my $pdftable = new PDF
::Table
();
89 my @keys = ('Document', 'Qty', 'RRP tax exc.', 'RRP tax inc.', 'Discount', 'Discount price', 'GST rate', 'Total tax exc.', 'Total tax inc.');
90 for my $bkey (@keys) {
91 push(@
$arrbasket, $bkey);
93 push(@
$abaskets, $arrbasket);
96 foreach my $line (@
{$orders->{$basket->{basketno
}}}) {
99 if ( C4
::Context
->preference("marcflavour") eq 'UNIMARC' ) {
100 $titleinfo = $line->{title
} . " / " . $line->{author
} .
101 ( $line->{isbn
} ?
" ISBN: " . $line->{isbn
} : '' ) .
102 ( $line->{en
} ?
" EN: " . $line->{en
} : '' ) .
103 ( $line->{itemtype
} ?
", " . $line->{itemtype
} : '' ) .
104 ( $line->{edition
} ?
", " . $line->{edition
} : '' ) .
105 ( $line->{publishercode
} ?
' published by '. $line->{publishercode
} : '') .
106 ( $line->{publicationyear
} ?
', '. $line->{publicationyear
} : '');
108 else { # MARC21, NORMARC
109 $titleinfo = $line->{title
} . " " . $line->{author
} .
110 ( $line->{isbn
} ?
" ISBN: " . $line->{isbn
} : '' ) .
111 ( $line->{en
} ?
" EN: " . $line->{en
} : '' ) .
112 ( $line->{itemtype
} ?
" " . $line->{itemtype
} : '' ) .
113 ( $line->{edition
} ?
", " . $line->{edition
} : '' ) .
114 ( $line->{publishercode
} ?
' published by '. $line->{publishercode
} : '') .
115 ( $line->{copyrightdate
} ?
' '. $line->{copyrightdate
} : '');
118 $titleinfo. ($line->{order_vendornote
} ?
"\n----------------\nNote for vendor : " . $line->{order_vendornote
} : '' ),
120 Koha
::Number
::Price
->new( $line->{rrpgste
} )->format,
121 Koha
::Number
::Price
->new( $line->{rrpgsti
} )->format,
122 Koha
::Number
::Price
->new( $line->{discount
} )->format . '%',
123 Koha
::Number
::Price
->new( $line->{rrpgste
} - $line->{ecostgste
})->format,
124 Koha
::Number
::Price
->new( $line->{gstrate
} * 100 )->format . '%',
125 Koha
::Number
::Price
->new( $line->{totalgste
} )->format,
126 Koha
::Number
::Price
->new( $line->{totalgsti
} )->format,
128 push(@
$abaskets, $arrbasket);
131 $pdftable->table($pdf, $page, $abaskets,
133 w
=> ($width - 20)/mm
,
140 background_color_odd
=> "lightgray",
141 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
144 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
151 min_w
=> 85/mm
, # Minimum column width.
154 justify
=> 'right', # One of left|right ,
157 justify
=> 'right', # One of left|right ,
160 justify
=> 'right', # One of left|right ,
163 justify
=> 'right', # One of left|right ,
166 justify
=> 'right', # One of left|right ,
169 justify
=> 'right', # One of left|right ,
172 justify
=> 'right', # One of left|right ,
175 justify
=> 'right', # One of left|right ,
180 $pdf->mediabox($width/mm, $height/mm);
184 my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
187 my $libraryname = C4
::Context
->preference("LibraryName");
189 my $cur_format = C4
::Context
->preference("CurrencyFormat");
191 $pdf->mediabox($width/mm, $height/mm);
192 my $page = $pdf->openpage(2);
194 my $text = $page->text;
196 # add basketgroup number
197 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
198 $text->translate(($width-40)/mm, ($height-53)/mm);
199 $text->text("".$basketgroup->{'id'});
200 # print the libraryname in the header
201 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
202 $text->translate(30/mm, ($height-28.5)/mm);
203 $text->text($libraryname);
204 my $pdftable = new PDF
::Table
();
207 # header of the table
208 my @keys = ('Lot', 'Basket (No.)','Total RRP tax exc.', 'Total RRP tax inc.', 'GST rate', 'GST', 'Total discount', 'Total tax exc.', 'Total tax inc.');
209 for my $bkey (@keys) {
210 push(@
$arrbasket, $bkey);
212 my ($grandtotalrrpgsti, $grandtotalrrpgste, $grandtotalgsti, $grandtotalgste, $grandtotalgstvalue, $grandtotaldiscount);
213 # calculate each basket total
214 push(@
$abaskets, $arrbasket);
215 for my $basket (@
$hbaskets) {
218 my ($totalrrpgste, $totalrrpgsti, $totalgste, $totalgsti, $totalgstvalue, $totaldiscount);
219 my $ords = $orders->{$basket->{basketno
}};
220 my $ordlength = @
$ords;
221 foreach my $ord (@
$ords) {
222 $totalgste += $ord->{totalgste
};
223 $totalgsti += $ord->{totalgsti
};
224 $totalgstvalue += $ord->{gstvalue
};
225 $totaldiscount += ($ord->{rrpgste
} - $ord->{ecostgste
} ) * $ord->{quantity
};
226 $totalrrpgste += $ord->{rrpgste
} * $ord->{quantity
};
227 $totalrrpgsti += $ord->{rrpgsti
} * $ord->{quantity
};
228 push @gst, $ord->{gstrate
};
230 @gst = uniq
map { $_ * 100 } @gst;
231 $grandtotalrrpgste += $totalrrpgste;
232 $grandtotalrrpgsti += $totalrrpgsti;
233 $grandtotalgsti += $totalgsti;
234 $grandtotalgste += $totalgste;
235 $grandtotalgstvalue += $totalgstvalue;
236 $grandtotaldiscount += $totaldiscount;
238 map { Koha
::Number
::Price
->new($_)->format . '%' } @gst;
240 $basket->{contractname
},
241 $basket->{basketname
} . ' (No. ' . $basket->{basketno
} . ')',
242 Koha
::Number
::Price
->new( $totalrrpgste )->format,
243 Koha
::Number
::Price
->new( $totalrrpgsti )->format,
245 Koha
::Number
::Price
->new( $totalgstvalue )->format,
246 Koha
::Number
::Price
->new( $totaldiscount )->format,
247 Koha
::Number
::Price
->new( $totalgste )->format,
248 Koha
::Number
::Price
->new( $totalgsti )->format,
250 push(@
$abaskets, $arrbasket);
257 Koha
::Number
::Price
->new( $grandtotalrrpgste )->format,
258 Koha
::Number
::Price
->new( $grandtotalrrpgsti )->format,
260 Koha
::Number
::Price
->new( $grandtotalgstvalue )->format,
261 Koha
::Number
::Price
->new( $grandtotaldiscount )->format,
262 Koha
::Number
::Price
->new( $grandtotalgste )->format,
263 Koha
::Number
::Price
->new( $grandtotalgsti )->format;
264 push @
$abaskets,$arrbasket;
265 # height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
267 $pdftable->table($pdf, $page, $abaskets,
269 w
=> ($width - 10)/mm
,
274 font
=> $pdf->corefont("Times", -encoding
=> "utf8"),
278 background_color_odd
=> "lightgray",
305 $pdf->mediabox($height/mm, $width/mm);
309 my ($pdf, $basketgroup, $bookseller) = @_;
312 my $libraryname = C4
::Context
->preference("LibraryName");
314 my $billingdetails = GetBranchDetail
( $basketgroup->{billingplace
} );
315 my $deliverydetails = GetBranchDetail
( $basketgroup->{deliveryplace
} );
316 my $freedeliveryplace = $basketgroup->{freedeliveryplace
};
320 # open 1st page (with the header)
321 my $page = $pdf->openpage(1);
324 my $text = $page->text;
326 # print the libraryname in the header
327 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 6/mm
);
328 $text->translate(30/mm, ($height-28.5)/mm);
329 $text->text($libraryname);
331 # print order info, on the default PDF
332 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 8/mm
);
333 $text->translate(100/mm, ($height-5-48)/mm);
334 $text->text($basketgroup->{'id'});
337 my $today = C4
::Dates
->today();
338 $text->translate(130/mm, ($height-5-48)/mm);
341 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 4/mm
);
343 # print billing infos
344 $text->translate(100/mm, ($height-86)/mm);
345 $text->text($libraryname);
346 $text->translate(100/mm, ($height-97)/mm);
347 $text->text($billingdetails->{branchname
});
348 $text->translate(100/mm, ($height-108.5)/mm);
349 $text->text($billingdetails->{branchphone
});
350 $text->translate(100/mm, ($height-115.5)/mm);
351 $text->text($billingdetails->{branchfax
});
352 $text->translate(100/mm, ($height-122.5)/mm);
353 $text->text($billingdetails->{branchaddress1
});
354 $text->translate(100/mm, ($height-127.5)/mm);
355 $text->text($billingdetails->{branchaddress2
});
356 $text->translate(100/mm, ($height-132.5)/mm);
357 $text->text($billingdetails->{branchaddress3
});
358 $text->translate(100/mm, ($height-137.5)/mm);
359 $text->text(join(' ', $billingdetails->{branchzip
}, $billingdetails->{branchcity
}, $billingdetails->{branchcountry
}));
360 $text->translate(100/mm, ($height-147.5)/mm);
361 $text->text($billingdetails->{branchemail
});
364 $text->translate(100/mm, ($height-145.5)/mm);
365 $text->text($subject);
367 # print bookseller infos
368 $text->translate(100/mm, ($height-180)/mm);
369 $text->text($bookseller->{name
});
370 $text->translate(100/mm, ($height-185)/mm);
371 $text->text($bookseller->{postal
});
372 $text->translate(100/mm, ($height-190)/mm);
373 $text->text($bookseller->{address1
});
374 $text->translate(100/mm, ($height-195)/mm);
375 $text->text($bookseller->{address2
});
376 $text->translate(100/mm, ($height-200)/mm);
377 $text->text($bookseller->{address3
});
378 $text->translate(100/mm, ($height-205)/mm);
379 $text->text($bookseller->{accountnumber
});
381 # print delivery infos
382 $text->font( $pdf->corefont("Times-Bold", -encoding
=> "utf8"), 4/mm
);
383 $text->translate(50/mm, ($height-237)/mm);
384 if ($freedeliveryplace) {
386 my @fdp = split('\n', $freedeliveryplace);
389 $text->translate( 50 / mm, ( $height - $start ) / mm
);
393 $text->text($deliverydetails->{branchaddress1
});
394 $text->translate(50/mm, ($height-242)/mm);
395 $text->text($deliverydetails->{branchaddress2
});
396 $text->translate(50/mm, ($height-247)/mm);
397 $text->text($deliverydetails->{branchaddress3
});
398 $text->translate(50/mm, ($height-252)/mm);
399 $text->text(join(' ', $deliverydetails->{branchzip
}, $deliverydetails->{branchcity
}, $deliverydetails->{branchcountry
}));
401 $text->translate(50/mm, ($height-262)/mm);
402 $text->text($basketgroup->{deliverycomment
});
407 for (my $i=1;$i <= $pdf->pages;$i++) {
408 my $page = $pdf->openpage($i);
409 my $text = $page->text;
410 $text->font( $pdf->corefont("Times", -encoding
=> "utf8"), 3/mm
);
411 $text->translate(10/mm, 10/mm);
412 $text->text("Page $i / ".$pdf->pages);
417 my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
418 # open the default PDF that will be used for base (1st page already filled)
419 my $pdf_template = C4
::Context
->config('intrahtdocs') . '/' . C4
::Context
->preference('template') . '/pdf/layout3pages.pdf';
420 my $pdf = PDF
::API2
->open($pdf_template);
421 $pdf->pageLabel( 0, {
423 } ); # start with roman numbering
424 # fill the 1st page (basketgroup information)
425 printhead
($pdf, $basketgroup, $bookseller);
426 # fill the 2nd page (orders summary)
427 printbaskets
($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
428 # fill other pages (orders)
429 printorders
($pdf, $basketgroup, $baskets, $orders);
430 # print something on each page (usually the footer, but you could also put a header
432 return $pdf->stringify;