Bug 11471: Display the currency for baskets in a basketgroup
[koha.git] / acqui / basketgroup.pl
1 #!/usr/bin/perl
2
3 #script to group (closed) baskets into basket groups for easier order management
4 #written by john.soros@biblibre.com 01/10/2008
5
6 # Copyright 2008 - 2009 BibLibre SARL
7 # Parts Copyright Catalyst 2010
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24
25 =head1 NAME
26
27 basketgroup.pl
28
29 =head1 DESCRIPTION
30
31  This script lets the user group (closed) baskets into basket groups for easier order management. Note that the grouped baskets have to be from the same bookseller and
32  have to be closed to be printed or exported.
33
34 =head1 CGI PARAMETERS
35
36 =over 4
37
38 =item $booksellerid
39
40 The bookseller who we want to display the baskets (and basketgroups) of.
41
42 =back
43
44 =cut
45
46 use strict;
47 use warnings;
48 use Carp;
49
50 use C4::Input;
51 use C4::Auth;
52 use C4::Output;
53 use CGI;
54
55 use C4::Bookseller qw/GetBookSellerFromId/;
56 use C4::Budgets qw/ConvertCurrency/;
57 use C4::Acquisition qw/CloseBasketgroup ReOpenBasketgroup GetOrders GetBasketsByBasketgroup GetBasketsByBookseller ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroups ModBasket GetBasketgroup GetBasket GetBasketGroupAsCSV/;
58 use C4::Bookseller qw/GetBookSellerFromId/;
59 use C4::Branch qw/GetBranches/;
60 use C4::Members qw/GetMember/;
61
62 our $input=new CGI;
63
64 our ($template, $loggedinuser, $cookie)
65     = get_template_and_user({template_name => "acqui/basketgroup.tmpl",
66                              query => $input,
67                              type => "intranet",
68                              authnotrequired => 0,
69                              flagsrequired => {acquisition => 'group_manage'},
70                              debug => 1,
71                 });
72
73 sub BasketTotal {
74     my $basketno = shift;
75     my $bookseller = shift;
76     my $total = 0;
77     my @orders = GetOrders($basketno);
78     for my $order (@orders){
79         $total = $total + ( $order->{ecost} * $order->{quantity} );
80         if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} // C4::Context->preference("gist") )) {
81             my $gst = $bookseller->{gstrate} // C4::Context->preference("gist");
82             $total = $total * ( $gst / 100 +1);
83         }
84     }
85     $total .= " " . ($bookseller->{invoiceprice} // 0);
86     return $total;
87 }
88
89 #displays all basketgroups and all closed baskets (in their respective groups)
90 sub displaybasketgroups {
91     my $basketgroups = shift;
92     my $bookseller = shift;
93     my $baskets = shift;
94     if (scalar @$basketgroups != 0) {
95         foreach my $basketgroup (@$basketgroups){
96             my $i = 0;
97             my $basketsqty = 0;
98             while($i < scalar(@$baskets)){
99                 my $basket = @$baskets[$i];
100                 if($basket->{'basketgroupid'} && $basket->{'basketgroupid'} == $basketgroup->{'id'}){
101                     $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
102                     push(@{$basketgroup->{'baskets'}}, $basket);
103                     splice(@$baskets, $i, 1);
104                     ++$basketsqty;
105                     --$i;
106                 }
107                 ++$i;
108             }
109             $basketgroup -> {'basketsqty'} = $basketsqty;
110         }
111         $template->param(basketgroups => $basketgroups);
112     }
113     for(my $i=0; $i < scalar @$baskets; ++$i) {
114         if( ! @$baskets[$i]->{'closedate'} ) {
115             splice(@$baskets, $i, 1);
116             --$i;
117         }else{
118             @$baskets[$i]->{total} = BasketTotal(@$baskets[$i]->{basketno}, $bookseller);
119         }
120     }
121     $template->param(baskets => $baskets);
122     $template->param( booksellername => $bookseller ->{'name'});
123 }
124
125 sub printbasketgrouppdf{
126     my ($basketgroupid) = @_;
127     
128     my $pdfformat = C4::Context->preference("OrderPdfFormat");
129     if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages' || $pdfformat eq 'pdfformat::layout3pagesfr'){
130         eval {
131         eval "require $pdfformat";
132             import $pdfformat;
133         };
134         if ($@){
135         }
136     }
137     else {
138         print $input->header;  
139         print $input->start_html;  # FIXME Should do a nicer page
140         print "<h1>Invalid PDF Format set</h1>";
141         print "Please go to the systempreferences and set a valid pdfformat";
142         exit;
143     }
144     
145     my $basketgroup = GetBasketgroup($basketgroupid);
146     my $bookseller = GetBookSellerFromId($basketgroup->{'booksellerid'});
147     my $baskets = GetBasketsByBasketgroup($basketgroupid);
148     
149     my %orders;
150     for my $basket (@$baskets) {
151         my @ba_orders;
152         my @ords = &GetOrders($basket->{basketno});
153         for my $ord (@ords) {
154
155             next unless ( $ord->{biblionumber} or $ord->{quantity}> 0 );
156             eval {
157                 require C4::Biblio;
158                 import C4::Biblio;
159             };
160             if ($@){
161                 croak $@;
162             }
163             eval {
164                 require C4::Koha;
165                 import C4::Koha;
166             };
167             if ($@){
168                 croak $@;
169             }
170
171             $ord->{rrp} = ConvertCurrency( $ord->{'currency'}, $ord->{rrp} );
172             if ( $bookseller->{'listincgst'} ) {
173                 $ord->{rrpgsti} = sprintf( "%.2f", $ord->{rrp} );
174                 $ord->{gstgsti} = sprintf( "%.2f", $ord->{gstrate} * 100 );
175                 $ord->{rrpgste} = sprintf( "%.2f", $ord->{rrp} / ( 1 + ( $ord->{gstgsti} / 100 ) ) );
176                 $ord->{gstgste} = sprintf( "%.2f", $ord->{gstgsti} / ( 1 + ( $ord->{gstgsti} / 100 ) ) );
177                 $ord->{ecostgsti} = sprintf( "%.2f", $ord->{ecost} );
178                 $ord->{ecostgste} = sprintf( "%.2f", $ord->{ecost} / ( 1 + ( $ord->{gstgsti} / 100 ) ) );
179                 $ord->{gstvalue} = sprintf( "%.2f", ( $ord->{ecostgsti} - $ord->{ecostgste} ) * $ord->{quantity});
180                 $ord->{totalgste} = sprintf( "%.2f", $ord->{quantity} * $ord->{ecostgste} );
181                 $ord->{totalgsti} = sprintf( "%.2f", $ord->{quantity} * $ord->{ecostgsti} );
182             } else {
183                 $ord->{rrpgsti} = sprintf( "%.2f", $ord->{rrp} * ( 1 + ( $ord->{gstrate} ) ) );
184                 $ord->{rrpgste} = sprintf( "%.2f", $ord->{rrp} );
185                 $ord->{gstgsti} = sprintf( "%.2f", $ord->{gstrate} * 100 );
186                 $ord->{gstgste} = sprintf( "%.2f", $ord->{gstrate} * 100 );
187                 $ord->{ecostgsti} = sprintf( "%.2f", $ord->{ecost} * ( 1 + ( $ord->{gstrate} ) ) );
188                 $ord->{ecostgste} = sprintf( "%.2f", $ord->{ecost} );
189                 $ord->{gstvalue} = sprintf( "%.2f", ( $ord->{ecostgsti} - $ord->{ecostgste} ) * $ord->{quantity});
190                 $ord->{totalgste} = sprintf( "%.2f", $ord->{quantity} * $ord->{ecostgste} );
191                 $ord->{totalgsti} = sprintf( "%.2f", $ord->{quantity} * $ord->{ecostgsti} );
192             }
193             my $bib = GetBiblioData($ord->{biblionumber});
194             my $itemtypes = GetItemTypes();
195
196             #FIXME DELETE ME
197             # 0      1        2        3         4            5         6       7      8        9
198             #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount gstrate
199
200             # Editor Number
201             my $en;
202             my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
203             if ($marcrecord){
204                 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
205                     $en = $marcrecord->subfield( '345', "b" );
206                 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
207                     $en = $marcrecord->subfield( '037', "a" );
208                 }
209             }
210
211             my $ba_order = {
212                 isbn => ($ord->{isbn} ? $ord->{isbn} : undef),
213                 itemtype => ( $ord->{itemtype} and $bib->{itemtype} ? $itemtypes->{$bib->{itemtype}}->{description} : undef ),
214                 en => ( $en ? $en : undef ),
215             };
216             for my $key ( qw/ gstrate author title itemtype publishercode discount quantity rrpgsti rrpgste gstgsti gstgste ecostgsti ecostgste gstvalue totalgste totalgsti / ) {
217                 $ba_order->{$key} = $ord->{$key};
218             }
219
220             push(@ba_orders, $ba_order);
221         }
222         $orders{$basket->{basketno}} = \@ba_orders;
223     }
224     print $input->header(
225         -type       => 'application/pdf',
226         -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
227     );
228     my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} // C4::Context->preference("gist")) || die "pdf generation failed";
229     print $pdf;
230
231 }
232
233 my $op = $input->param('op') || 'display';
234 # possible values of $op :
235 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
236 # - mod_basket : modify an individual basket of the basketgroup
237 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
238 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
239 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
240 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
241 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
242 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
243 # - display : display the list of all basketgroups for a vendor
244 my $booksellerid = $input->param('booksellerid');
245 $template->param(booksellerid => $booksellerid);
246
247 if ( $op eq "add" ) {
248 #
249 # if no param('basketgroupid') is not defined, adds a new basketgroup
250 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
251 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
252 #
253     my $bookseller = &GetBookSellerFromId($booksellerid);
254     if(! $booksellerid){
255 # Unknown bookseller
256 # FIXME : ungroupedlist does not seem to be used in this file nor in template
257         $template->param( ungroupedlist => 1);
258         my @booksellers = GetBookSeller('');
259        for (my $i=0; $i < scalar @booksellers; $i++) {
260             my $baskets = &GetBasketsByBookseller($booksellers[$i]->{id});
261             for (my $j=0; $j < scalar @$baskets; $j++) {
262                 if(! @$baskets[$i]->{closedate} || @$baskets[$i]->{basketgroupid}) {
263                     splice(@$baskets, $j, 1);
264                     $j--;
265                 }
266             }
267             if (scalar @$baskets == 0){
268                 splice(@booksellers, $i, 1);
269                 $i--;
270             }
271         }
272     } else {
273 # Known bookseller
274         my $basketgroupid = $input->param('basketgroupid');
275         my $billingplace;
276         my $deliveryplace;
277         my $freedeliveryplace;
278         if ( $basketgroupid ) {
279             # Get the selected baskets in the basketgroup to display them
280             my $selecteds = GetBasketsByBasketgroup($basketgroupid);
281             foreach my $basket(@{$selecteds}){
282                 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
283             }
284             $template->param(basketgroupid => $basketgroupid,
285                              selectedbaskets => $selecteds);
286
287             # Get general informations about the basket group to prefill the form
288             my $basketgroup = GetBasketgroup($basketgroupid);
289             $template->param(
290                 name            => $basketgroup->{name},
291                 deliverycomment => $basketgroup->{deliverycomment},
292                 freedeliveryplace => $basketgroup->{freedeliveryplace},
293             );
294             $billingplace  = $basketgroup->{billingplace};
295             $deliveryplace = $basketgroup->{deliveryplace};
296             $freedeliveryplace = $basketgroup->{freedeliveryplace};
297             $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
298         } else {
299             $template->param( closedbg => 0);
300         }
301         # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
302         my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) );
303         $billingplace  = $billingplace  || $borrower->{'branchcode'};
304         $deliveryplace = $deliveryplace || $borrower->{'branchcode'};
305
306         my $branches = C4::Branch::GetBranchesLoop( $billingplace );
307         $template->param( billingplaceloop => $branches );
308         $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
309         $template->param( deliveryplaceloop => $branches );
310         $template->param( booksellerid => $booksellerid );
311     }
312     # the template will display a unique basketgroup
313     $template->param(grouping => 1);
314     my $basketgroups = &GetBasketgroups($booksellerid);
315     my $baskets = &GetBasketsByBookseller($booksellerid);
316     displaybasketgroups($basketgroups, $bookseller, $baskets);
317 } elsif ($op eq 'mod_basket') {
318 #
319 # edit an individual basket contained in this basketgroup
320 #
321   my $basketno=$input->param('basketno');
322   my $basketgroupid=$input->param('basketgroupid');
323   ModBasket( { basketno => $basketno,
324                          basketgroupid => $basketgroupid } );
325   print $input->redirect("basket.pl?basketno=" . $basketno);
326 } elsif ( $op eq 'closeandprint') {
327 #
328 # close an open basketgroup and generates a pdf
329 #
330     my $basketgroupid = $input->param('basketgroupid');
331     CloseBasketgroup($basketgroupid);
332     printbasketgrouppdf($basketgroupid);
333     exit;
334 }elsif ($op eq 'print'){
335 #
336 # print a closed basketgroup
337 #
338     my $basketgroupid = $input->param('basketgroupid');
339     printbasketgrouppdf($basketgroupid);
340     exit;
341 }elsif ( $op eq "export" ) {
342 #
343 # export a closed basketgroup in csv
344 #
345     my $basketgroupid = $input->param('basketgroupid');
346     print $input->header(
347         -type       => 'text/csv',
348         -attachment => 'basketgroup' . $basketgroupid . '.csv',
349     );
350     print GetBasketGroupAsCSV( $basketgroupid, $input );
351     exit;
352 }elsif( $op eq "delete"){
353 #
354 # delete an closed basketgroup
355 #
356     my $basketgroupid = $input->param('basketgroupid');
357     DelBasketgroup($basketgroupid);
358     print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&amp;listclosed=1');
359 }elsif ( $op eq 'reopen'){
360 #
361 # reopen a closed basketgroup
362 #
363     my $basketgroupid   = $input->param('basketgroupid');
364     my $booksellerid    = $input->param('booksellerid');
365     ReOpenBasketgroup($basketgroupid);
366     my $redirectpath = ((defined $input->param('mode'))&& ($input->param('mode') eq 'singlebg')) ?'/cgi-bin/koha/acqui/basketgroup.pl?op=add&amp;basketgroupid='.$basketgroupid.'&amp;booksellerid='.$booksellerid : '/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' .$booksellerid.'&amp;listclosed=1';
367     print $input->redirect($redirectpath);
368 } elsif ( $op eq 'attachbasket') {
369 #
370 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
371 #
372     # Getting parameters
373     my $basketgroup       = {};
374     my @baskets           = $input->param('basket');
375     my $basketgroupid     = $input->param('basketgroupid');
376     my $basketgroupname   = $input->param('basketgroupname');
377     my $booksellerid      = $input->param('booksellerid');
378     my $billingplace      = $input->param('billingplace');
379     my $deliveryplace     = $input->param('deliveryplace');
380     my $freedeliveryplace = $input->param('freedeliveryplace');
381     my $deliverycomment   = $input->param('deliverycomment');
382     my $closedbg          = $input->param('closedbg') ? 1 : 0;
383     if ($basketgroupid) {
384     # If we have a basketgroupid we edit the basketgroup
385         $basketgroup = {
386               name              => $basketgroupname,
387               id                => $basketgroupid,
388               basketlist        => \@baskets,
389               billingplace      => $billingplace,
390               deliveryplace     => $deliveryplace,
391               freedeliveryplace => $freedeliveryplace,
392               deliverycomment   => $deliverycomment,
393               closed            => $closedbg,
394         };
395         ModBasketgroup($basketgroup);
396         if($closedbg){
397 # FIXME
398         }
399     }else{
400     # we create a new basketgroup (whith a closed basket)
401         $basketgroup = {
402             name              => $basketgroupname,
403             booksellerid      => $booksellerid,
404             basketlist        => \@baskets,
405             billingplace      => $billingplace,
406             deliveryplace     => $deliveryplace,
407             freedeliveryplace => $freedeliveryplace,
408             deliverycomment   => $deliverycomment,
409             closed            => $closedbg,
410         };
411         $basketgroupid = NewBasketgroup($basketgroup);
412     }
413     my $redirectpath = ((defined $input->param('mode')) && ($input->param('mode') eq 'singlebg')) ?'/cgi-bin/koha/acqui/basketgroup.pl?op=add&amp;basketgroupid='.$basketgroupid.'&amp;booksellerid='.$booksellerid : '/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid;
414     $redirectpath .=  "&amp;listclosed=1" if $closedbg ;
415     print $input->redirect($redirectpath );
416     
417 }else{
418 # no param : display the list of all basketgroups for a given vendor
419     my $basketgroups = &GetBasketgroups($booksellerid);
420     my $bookseller = &GetBookSellerFromId($booksellerid);
421     my $baskets = &GetBasketsByBookseller($booksellerid);
422
423     displaybasketgroups($basketgroups, $bookseller, $baskets);
424 }
425 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
426 #prolly won't use all these, maybe just use print, the rest can be done inside validate
427 output_html_with_http_headers $input, $cookie, $template->output;