Bug 13321: Fix the prices calculation method
[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
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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::Auth;
51 use C4::Output;
52 use CGI qw ( -utf8 );
53
54 use C4::Acquisition qw/CloseBasketgroup ReOpenBasketgroup GetOrders GetBasketsByBasketgroup GetBasketsByBookseller ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroups ModBasket GetBasketgroup GetBasket GetBasketGroupAsCSV/;
55 use C4::Members qw/GetMember/;
56 use Koha::EDI qw/create_edi_order get_edifact_ean/;
57
58 use Koha::Acquisition::Bookseller;
59
60 our $input=new CGI;
61
62 our ($template, $loggedinuser, $cookie)
63     = get_template_and_user({template_name => "acqui/basketgroup.tt",
64                              query => $input,
65                              type => "intranet",
66                              authnotrequired => 0,
67                              flagsrequired => {acquisition => 'group_manage'},
68                              debug => 1,
69                 });
70
71 sub BasketTotal {
72     my $basketno = shift;
73     my $bookseller = shift;
74     my $total = 0;
75     my @orders = GetOrders($basketno);
76     for my $order (@orders){
77         # FIXME The following is wrong
78         if ( $bookseller->{listincgst} ) {
79             $total = $total + ( $order->{ecost_tax_included} * $order->{quantity} );
80         } else {
81             $total = $total + ( $order->{ecost_tax_excluded} * $order->{quantity} );
82         }
83     }
84     $total .= " " . ($bookseller->{invoiceprice} // 0);
85     return $total;
86 }
87
88 #displays all basketgroups and all closed baskets (in their respective groups)
89 sub displaybasketgroups {
90     my $basketgroups = shift;
91     my $bookseller = shift;
92     my $baskets = shift;
93     if (scalar @$basketgroups != 0) {
94         foreach my $basketgroup (@$basketgroups){
95             my $i = 0;
96             my $basketsqty = 0;
97             while($i < scalar(@$baskets)){
98                 my $basket = @$baskets[$i];
99                 if($basket->{'basketgroupid'} && $basket->{'basketgroupid'} == $basketgroup->{'id'}){
100                     $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
101                     push(@{$basketgroup->{'baskets'}}, $basket);
102                     splice(@$baskets, $i, 1);
103                     ++$basketsqty;
104                     --$i;
105                 }
106                 ++$i;
107             }
108             $basketgroup -> {'basketsqty'} = $basketsqty;
109         }
110         $template->param(basketgroups => $basketgroups);
111     }
112     for(my $i=0; $i < scalar @$baskets; ++$i) {
113         if( ! @$baskets[$i]->{'closedate'} ) {
114             splice(@$baskets, $i, 1);
115             --$i;
116         }else{
117             @$baskets[$i]->{total} = BasketTotal(@$baskets[$i]->{basketno}, $bookseller);
118         }
119     }
120     $template->param(baskets => $baskets);
121     $template->param( booksellername => $bookseller ->{'name'});
122 }
123
124 sub printbasketgrouppdf{
125     my ($basketgroupid) = @_;
126     
127     my $pdfformat = C4::Context->preference("OrderPdfFormat");
128     if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages' || $pdfformat eq 'pdfformat::layout3pagesfr'
129         || $pdfformat eq 'pdfformat::layout2pagesde'){
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 = Koha::Acquisition::Bookseller->fetch({ id => $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->{total_tax_included} = $ord->{ecost_tax_included} * $ord->{quantity};
172             $ord->{total_tax_excluded} = $ord->{ecost_tax_excluded} * $ord->{quantity};
173
174             my $bib = GetBiblioData($ord->{biblionumber});
175             my $itemtypes = GetItemTypes();
176
177             #FIXME DELETE ME
178             # 0      1        2        3         4            5         6       7      8        9
179             #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount tax_rate
180
181             # Editor Number
182             my $en;
183             my $edition;
184             my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
185             if ($marcrecord){
186                 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
187                     $en = $marcrecord->subfield( '345', "b" );
188                     $edition = $marcrecord->subfield( '205', 'a' );
189                 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
190                     $en = $marcrecord->subfield( '037', "a" );
191                     $edition = $marcrecord->subfield( '250', 'a' );
192                 }
193             }
194
195             $ord->{itemtype} = ( $ord->{itemtype} and $bib->{itemtype} ) ? $itemtypes->{$bib->{itemtype}}->{description} : undef;
196             $ord->{en} = $en ? $en : undef;
197             $ord->{edition} = $edition ? $edition : undef;
198
199             push(@ba_orders, $ord);
200         }
201         $orders{$basket->{basketno}} = \@ba_orders;
202     }
203     print $input->header(
204         -type       => 'application/pdf',
205         -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
206     );
207     my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{tax_rate} // C4::Context->preference("gist")) || die "pdf generation failed";
208     print $pdf;
209
210 }
211
212 sub generate_edifact_orders {
213     my $basketgroupid = shift;
214     my $baskets       = GetBasketsByBasketgroup($basketgroupid);
215     my $ean           = get_edifact_ean();
216
217     for my $basket ( @{$baskets} ) {
218         create_edi_order( { ean => $ean, basketno => $basket->{basketno}, } );
219     }
220     return;
221 }
222
223 my $op = $input->param('op') || 'display';
224 # possible values of $op :
225 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
226 # - mod_basket : modify an individual basket of the basketgroup
227 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
228 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
229 # - ediprint : generate edi order messages for the baskets in the group
230 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
231 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
232 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
233 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
234 # - display : display the list of all basketgroups for a vendor
235 my $booksellerid = $input->param('booksellerid');
236 $template->param(booksellerid => $booksellerid);
237
238 if ( $op eq "add" ) {
239 #
240 # if no param('basketgroupid') is not defined, adds a new basketgroup
241 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
242 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
243 #
244     my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
245     my $basketgroupid = $input->param('basketgroupid');
246     my $billingplace;
247     my $deliveryplace;
248     my $freedeliveryplace;
249     if ( $basketgroupid ) {
250         # Get the selected baskets in the basketgroup to display them
251         my $selecteds = GetBasketsByBasketgroup($basketgroupid);
252         foreach my $basket(@{$selecteds}){
253             $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
254         }
255         $template->param(basketgroupid => $basketgroupid,
256                          selectedbaskets => $selecteds);
257
258         # Get general informations about the basket group to prefill the form
259         my $basketgroup = GetBasketgroup($basketgroupid);
260         $template->param(
261             name            => $basketgroup->{name},
262             deliverycomment => $basketgroup->{deliverycomment},
263             freedeliveryplace => $basketgroup->{freedeliveryplace},
264         );
265         $billingplace  = $basketgroup->{billingplace};
266         $deliveryplace = $basketgroup->{deliveryplace};
267         $freedeliveryplace = $basketgroup->{freedeliveryplace};
268         $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
269     } else {
270         $template->param( closedbg => 0);
271     }
272     # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
273     my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) );
274     $billingplace  = $billingplace  || $borrower->{'branchcode'};
275     $deliveryplace = $deliveryplace || $borrower->{'branchcode'};
276
277     $template->param( billingplace => $billingplace );
278     $template->param( deliveryplace => $deliveryplace );
279     $template->param( booksellerid => $booksellerid );
280
281     # the template will display a unique basketgroup
282     $template->param(grouping => 1);
283     my $basketgroups = &GetBasketgroups($booksellerid);
284     my $baskets = &GetBasketsByBookseller($booksellerid);
285     displaybasketgroups($basketgroups, $bookseller, $baskets);
286 } elsif ($op eq 'mod_basket') {
287 #
288 # edit an individual basket contained in this basketgroup
289 #
290   my $basketno=$input->param('basketno');
291   my $basketgroupid=$input->param('basketgroupid');
292   ModBasket( { basketno => $basketno,
293                          basketgroupid => $basketgroupid } );
294   print $input->redirect("basket.pl?basketno=" . $basketno);
295 } elsif ( $op eq 'closeandprint') {
296 #
297 # close an open basketgroup and generates a pdf
298 #
299     my $basketgroupid = $input->param('basketgroupid');
300     CloseBasketgroup($basketgroupid);
301     printbasketgrouppdf($basketgroupid);
302     exit;
303 }elsif ($op eq 'print'){
304 #
305 # print a closed basketgroup
306 #
307     my $basketgroupid = $input->param('basketgroupid');
308     printbasketgrouppdf($basketgroupid);
309     exit;
310 }elsif ( $op eq "export" ) {
311 #
312 # export a closed basketgroup in csv
313 #
314     my $basketgroupid = $input->param('basketgroupid');
315     print $input->header(
316         -type       => 'text/csv',
317         -attachment => 'basketgroup' . $basketgroupid . '.csv',
318     );
319     print GetBasketGroupAsCSV( $basketgroupid, $input );
320     exit;
321 }elsif( $op eq "delete"){
322 #
323 # delete an closed basketgroup
324 #
325     my $basketgroupid = $input->param('basketgroupid');
326     DelBasketgroup($basketgroupid);
327     print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&amp;listclosed=1');
328 }elsif ( $op eq 'reopen'){
329 #
330 # reopen a closed basketgroup
331 #
332     my $basketgroupid   = $input->param('basketgroupid');
333     my $booksellerid    = $input->param('booksellerid');
334     ReOpenBasketgroup($basketgroupid);
335     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';
336     print $input->redirect($redirectpath);
337 } elsif ( $op eq 'attachbasket') {
338 #
339 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
340 #
341     # Getting parameters
342     my $basketgroup       = {};
343     my @baskets           = $input->multi_param('basket');
344     my $basketgroupid     = $input->param('basketgroupid');
345     my $basketgroupname   = $input->param('basketgroupname');
346     my $booksellerid      = $input->param('booksellerid');
347     my $billingplace      = $input->param('billingplace');
348     my $deliveryplace     = $input->param('deliveryplace');
349     my $freedeliveryplace = $input->param('freedeliveryplace');
350     my $deliverycomment   = $input->param('deliverycomment');
351     my $closedbg          = $input->param('closedbg') ? 1 : 0;
352     if ($basketgroupid) {
353     # If we have a basketgroupid we edit the basketgroup
354         $basketgroup = {
355               name              => $basketgroupname,
356               id                => $basketgroupid,
357               basketlist        => \@baskets,
358               billingplace      => $billingplace,
359               deliveryplace     => $deliveryplace,
360               freedeliveryplace => $freedeliveryplace,
361               deliverycomment   => $deliverycomment,
362               closed            => $closedbg,
363         };
364         ModBasketgroup($basketgroup);
365         if($closedbg){
366 # FIXME
367         }
368     }else{
369     # we create a new basketgroup (with a closed basket)
370         $basketgroup = {
371             name              => $basketgroupname,
372             booksellerid      => $booksellerid,
373             basketlist        => \@baskets,
374             billingplace      => $billingplace,
375             deliveryplace     => $deliveryplace,
376             freedeliveryplace => $freedeliveryplace,
377             deliverycomment   => $deliverycomment,
378             closed            => $closedbg,
379         };
380         $basketgroupid = NewBasketgroup($basketgroup);
381     }
382     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;
383     $redirectpath .=  "&amp;listclosed=1" if $closedbg ;
384     print $input->redirect($redirectpath );
385     
386 } elsif ( $op eq 'ediprint') {
387     my $basketgroupid = $input->param('basketgroupid');
388     generate_edifact_orders( $basketgroupid );
389     exit;
390 }else{
391 # no param : display the list of all basketgroups for a given vendor
392     my $basketgroups = &GetBasketgroups($booksellerid);
393     my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
394     my $baskets = &GetBasketsByBookseller($booksellerid);
395
396     displaybasketgroups($basketgroups, $bookseller, $baskets);
397 }
398 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
399 #prolly won't use all these, maybe just use print, the rest can be done inside validate
400 output_html_with_http_headers $input, $cookie, $template->output;