Bug 31257: Add a new English 1 page layout to export basketgroup
[koha.git] / Koha / pdfformat / layout1page.pm
1 package Koha::pdfformat::layout1page;
2
3 # Copyright 2022 Catalyst IT <aleisha@catalyst.net.nz>
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 #you can use any PDF::API2 module, all you need to do is return the stringifyed pdf object from the printpdf sub.
21 use vars qw(@ISA @EXPORT);
22 use Modern::Perl;
23 use utf8;
24
25 use Koha::Number::Price;
26 use Koha::DateUtils qw( dt_from_string output_pref );
27 use Koha::Libraries;
28
29 BEGIN {
30     use Exporter   ();
31     our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
32     @ISA    = qw(Exporter);
33     @EXPORT = qw(printpdf);
34 }
35
36
37 #be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
38 #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.
39 use constant mm => 25.4 / 72;
40 use constant in => 1 / 72;
41 use constant pt => 1;
42
43 use PDF::API2;
44 #A4 paper specs
45 my ($height, $width) = (297, 210);
46 use PDF::Table;
47
48 sub printorders {
49     my ($pdf, $basketgroup, $baskets, $orders) = @_;
50
51     my $cur_format = C4::Context->preference("CurrencyFormat");
52
53     $pdf->mediabox($height/mm, $width/mm);
54     my $page = $pdf->openpage(1);
55
56     my $pdftable = PDF::Table->new();
57
58     my $abaskets;
59     my $arrbasket;
60     my @keys = ('Document', 'Qty', 'RRP tax inc.', 'Discount', 'Tax', 'Total tax exc.', 'Total tax inc.');
61     for my $bkey (@keys) {
62         push(@$arrbasket, $bkey);
63     }
64     push(@$abaskets, $arrbasket);
65
66     my $titleinfo;
67     for my $basket (@$baskets){
68         for my $line (@{$orders->{$basket->{basketno}}}) {
69             $arrbasket = undef;
70             $titleinfo = "";
71             if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
72                 $titleinfo =  $line->{title} . " / " . $line->{author} .
73                     ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
74                     ( $line->{en} ? " EN: " . $line->{en} : '' ) .
75                     ( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
76                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
77                     ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
78                     ( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
79             }
80             else { # MARC21
81                 $titleinfo =  $line->{title} . " " . $line->{author} .
82                     ( $line->{isbn} ? " ISBN: " . $line->{isbn} : '' ) .
83                     ( $line->{en} ? " EN: " . $line->{en} : '' ) .
84                     ( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
85                     ( $line->{edition} ? ", " . $line->{edition} : '' ) .
86                     ( $line->{publishercode} ? ' published by '. $line->{publishercode} : '') .
87                     ( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
88             }
89             push( @$arrbasket,
90                 $titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote for vendor : " . $line->{order_vendornote} : '' ),
91                 $line->{quantity},
92                 Koha::Number::Price->new( $line->{rrp_tax_included} )->format,
93                 Koha::Number::Price->new( $line->{discount} )->format . '%',
94                 Koha::Number::Price->new( $line->{tax_rate} * 100 )->format . '%',
95                 Koha::Number::Price->new( $line->{total_tax_excluded} )->format,
96                 Koha::Number::Price->new( $line->{total_tax_included} )->format,
97             );
98             push(@$abaskets, $arrbasket);
99         }
100     }
101
102     $pdftable->table($pdf, $page, $abaskets,
103         x => 10/mm,
104         w => ($width - 20)/mm,
105         start_y => 170/mm,
106         next_y  => 170/mm,
107         start_h => 260/mm,
108         next_h  => 260/mm,
109         padding => 5,
110         padding_right => 5,
111         background_color_odd  => "lightgray",
112         font       => $pdf->corefont("Times", -encoding => "utf8"),
113         font_size => 3/mm,
114         header_props   => {
115             font       => $pdf->corefont("Times", -encoding => "utf8"),
116             font_size  => 10,
117             bg_color   => 'gray',
118             repeat     => 1,
119         },
120         column_props => [
121             { justify => 'left'  },
122             { min_w   => 90/mm   },
123             { justify => 'right' },
124             { justify => 'right' },
125             { justify => 'right' },
126             { justify => 'right' },
127             { justify => 'right' },
128             { justify => 'right' },
129         ],
130     );
131
132     $pdf->mediabox($width/mm, $height/mm);
133 }
134
135 sub printhead {
136     my ($pdf, $basketgroup, $bookseller) = @_;
137
138     # get library name
139     my $libraryname = C4::Context->preference("LibraryName");
140     my $billing_library  = Koha::Libraries->find( $basketgroup->{billingplace} );
141     my $delivery_library = Koha::Libraries->find( $basketgroup->{deliveryplace} );
142     my $freedeliveryplace = $basketgroup->{freedeliveryplace};
143
144     # open 1st page (with the header)
145     my $page = $pdf->openpage(1);
146
147     # create a text
148     my $text = $page->text;
149     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
150
151     # print order info, on the default PDF
152     $text->translate(42/mm, ($height-62)/mm);
153     $text->text($basketgroup->{'id'});
154
155     # print the date
156     my $today = output_pref({ dt => dt_from_string, dateonly => 1 });
157     $text->translate(41/mm, ($height-67)/mm);
158     $text->text($today);
159
160     # print billing infos
161     $text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
162     $text->translate(107/mm, ($height-67)/mm);
163     $text->text($libraryname);
164
165     $text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
166     $text->translate(107/mm, ($height-71)/mm);
167     $text->text($billing_library->branchname);
168     $text->translate(116/mm, ($height-97)/mm);
169     $text->text($billing_library->branchphone);
170     $text->translate(155/mm, ($height-97)/mm);
171     $text->text($billing_library->branchfax);
172
173     # print bookseller infos
174     $text->translate(20/mm, ($height-33)/mm);
175     $text->text($bookseller->name);
176     $text->translate(20/mm, ($height-37)/mm);
177     if ( $bookseller->postal ) {
178         my $start = 41;
179         my @postal = split('\n', $bookseller->postal);
180         foreach (@postal) {
181             $text->text($_);
182             $text->translate( 20 / mm, ( $height - $start ) / mm );
183             $start += 4;
184         }
185     }
186     $text->translate(20/mm, ($height-50)/mm);
187     $text->text($bookseller->accountnumber);
188
189     # print delivery infos
190     $text->translate(107/mm, ($height-75)/mm);
191     if ($freedeliveryplace) {
192         my $start = 79;
193         my @fdp = split('\n', $freedeliveryplace);
194         foreach (@fdp) {
195             $text->text($_);
196             $text->translate( 107 / mm, ( $height - $start ) / mm );
197             $start += 4;
198         }
199     } else {
200         $text->text( $delivery_library->branchaddress1 );
201         $text->translate( 107 / mm, ( $height - 75 ) / mm );
202         $text->text( $delivery_library->branchaddress2 );
203         $text->translate( 107 / mm, ( $height - 79 ) / mm );
204         $text->text( $delivery_library->branchaddress3 );
205         $text->translate( 107 / mm, ( $height - 83 ) / mm );
206         $text->text( join( ' ', $delivery_library->branchcity, $delivery_library->branchzip, $delivery_library->branchcountry ) );
207     }
208     $text->translate( 20 / mm, ( $height - 120 ) / mm );
209     $text->text($basketgroup->{deliverycomment});
210 }
211
212 sub printfooters {
213     my $pdf = shift;
214     for ( 1..$pdf->pages ) {
215         my $page = $pdf->openpage($_);
216         my $text = $page->text;
217         $text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
218         $text->translate(10/mm,  10/mm);
219         $text->text("Page $_ / ".$pdf->pages);
220     }
221 }
222
223 sub printpdf {
224     my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
225     # open the default PDF that will be used for base (1st page already filled)
226     my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout1page.pdf';
227     my $pdf = PDF::API2->open($pdf_template);
228     $pdf->pageLabel( 0, {
229         -style => 'roman',
230     } ); # start with roman numbering
231     # fill the 1st page (basketgroup information)
232     printhead($pdf, $basketgroup, $bookseller);
233     # fill other pages (orders)
234     printorders($pdf, $basketgroup, $baskets, $orders);
235     # print something on each page (usually the footer, but you could also put a header
236     printfooters($pdf);
237     return $pdf->stringify;
238 }
239
240 1;