Bug 17835: Replace GetItemTypes with Koha::ItemTypes
[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::Booksellers;
59 use Koha::ItemTypes;
60
61 our $input=new CGI;
62
63 our ($template, $loggedinuser, $cookie)
64     = get_template_and_user({template_name => "acqui/basketgroup.tt",
65                              query => $input,
66                              type => "intranet",
67                              authnotrequired => 0,
68                              flagsrequired => {acquisition => 'group_manage'},
69                              debug => 1,
70                 });
71
72 sub BasketTotal {
73     my $basketno = shift;
74     my $bookseller = shift;
75     my $total = 0;
76     my @orders = GetOrders($basketno);
77     for my $order (@orders){
78         # FIXME The following is wrong
79         if ( $bookseller->listincgst ) {
80             $total = $total + ( $order->{ecost_tax_included} * $order->{quantity} );
81         } else {
82             $total = $total + ( $order->{ecost_tax_excluded} * $order->{quantity} );
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         || $pdfformat eq 'pdfformat::layout2pagesde'){
131         eval {
132         eval "require $pdfformat";
133             import $pdfformat;
134         };
135         if ($@){
136         }
137     }
138     else {
139         print $input->header;  
140         print $input->start_html;  # FIXME Should do a nicer page
141         print "<h1>Invalid PDF Format set</h1>";
142         print "Please go to the systempreferences and set a valid pdfformat";
143         exit;
144     }
145     
146     my $basketgroup = GetBasketgroup($basketgroupid);
147     my $bookseller = Koha::Acquisition::Booksellers->find( $basketgroup->{booksellerid} );
148     my $baskets = GetBasketsByBasketgroup($basketgroupid);
149     
150     my %orders;
151     for my $basket (@$baskets) {
152         my @ba_orders;
153         my @ords = &GetOrders($basket->{basketno});
154         for my $ord (@ords) {
155
156             next unless ( $ord->{biblionumber} or $ord->{quantity}> 0 );
157             eval {
158                 require C4::Biblio;
159                 import C4::Biblio;
160             };
161             if ($@){
162                 croak $@;
163             }
164             eval {
165                 require C4::Koha;
166                 import C4::Koha;
167             };
168             if ($@){
169                 croak $@;
170             }
171
172             $ord->{tax_value} = $ord->{tax_value_on_ordering};
173             $ord->{tax_rate} = $ord->{tax_rate_on_ordering};
174             $ord->{total_tax_included} = $ord->{ecost_tax_included} * $ord->{quantity};
175             $ord->{total_tax_excluded} = $ord->{ecost_tax_excluded} * $ord->{quantity};
176
177             my $bib = GetBiblioData($ord->{biblionumber});
178
179             #FIXME DELETE ME
180             # 0      1        2        3         4            5         6       7      8        9
181             #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount tax_rate
182
183             # Editor Number
184             my $en;
185             my $edition;
186             my $ord->{marcxml} = C4::Biblio::GetXmlBiblio( $ord->{biblionumber} );
187             my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
188             if ($marcrecord){
189                 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
190                     $en = $marcrecord->subfield( '345', "b" );
191                     $edition = $marcrecord->subfield( '205', 'a' );
192                 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
193                     $en = $marcrecord->subfield( '037', "a" );
194                     $edition = $marcrecord->subfield( '250', 'a' );
195                 }
196             }
197
198             $ord->{itemtype} = ( $ord->{itemtype} and $bib->{itemtype} ) ? Koha::ItemTypes->find( $bib->{itemtype} )->description : undef;
199             $ord->{en} = $en ? $en : undef;
200             $ord->{edition} = $edition ? $edition : undef;
201
202             push(@ba_orders, $ord);
203         }
204         $orders{$basket->{basketno}} = \@ba_orders;
205     }
206     print $input->header(
207         -type       => 'application/pdf',
208         -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
209     );
210     my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->tax_rate // C4::Context->preference("gist")) || die "pdf generation failed";
211     print $pdf;
212
213 }
214
215 sub generate_edifact_orders {
216     my $basketgroupid = shift;
217     my $baskets       = GetBasketsByBasketgroup($basketgroupid);
218     my $ean           = get_edifact_ean();
219
220     for my $basket ( @{$baskets} ) {
221         create_edi_order( { ean => $ean, basketno => $basket->{basketno}, } );
222     }
223     return;
224 }
225
226 my $op = $input->param('op') || 'display';
227 # possible values of $op :
228 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
229 # - mod_basket : modify an individual basket of the basketgroup
230 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
231 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
232 # - ediprint : generate edi order messages for the baskets in the group
233 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
234 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
235 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
236 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
237 # - display : display the list of all basketgroups for a vendor
238 my $booksellerid = $input->param('booksellerid');
239 $template->param(booksellerid => $booksellerid);
240
241 if ( $op eq "add" ) {
242 #
243 # if no param('basketgroupid') is not defined, adds a new basketgroup
244 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
245 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
246 #
247     my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
248     my $basketgroupid = $input->param('basketgroupid');
249     my $billingplace;
250     my $deliveryplace;
251     my $freedeliveryplace;
252     if ( $basketgroupid ) {
253         # Get the selected baskets in the basketgroup to display them
254         my $selecteds = GetBasketsByBasketgroup($basketgroupid);
255         foreach my $basket(@{$selecteds}){
256             $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
257         }
258         $template->param(basketgroupid => $basketgroupid,
259                          selectedbaskets => $selecteds);
260
261         # Get general informations about the basket group to prefill the form
262         my $basketgroup = GetBasketgroup($basketgroupid);
263         $template->param(
264             name            => $basketgroup->{name},
265             deliverycomment => $basketgroup->{deliverycomment},
266             freedeliveryplace => $basketgroup->{freedeliveryplace},
267         );
268         $billingplace  = $basketgroup->{billingplace};
269         $deliveryplace = $basketgroup->{deliveryplace};
270         $freedeliveryplace = $basketgroup->{freedeliveryplace};
271         $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
272     } else {
273         $template->param( closedbg => 0);
274     }
275     # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
276     my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) );
277     $billingplace  = $billingplace  || $borrower->{'branchcode'};
278     $deliveryplace = $deliveryplace || $borrower->{'branchcode'};
279
280     $template->param( billingplace => $billingplace );
281     $template->param( deliveryplace => $deliveryplace );
282     $template->param( booksellerid => $booksellerid );
283
284     # the template will display a unique basketgroup
285     $template->param(grouping => 1);
286     my $basketgroups = &GetBasketgroups($booksellerid);
287     my $baskets = &GetBasketsByBookseller($booksellerid);
288     displaybasketgroups($basketgroups, $bookseller, $baskets);
289 } elsif ($op eq 'mod_basket') {
290 #
291 # edit an individual basket contained in this basketgroup
292 #
293   my $basketno=$input->param('basketno');
294   my $basketgroupid=$input->param('basketgroupid');
295   ModBasket( { basketno => $basketno,
296                          basketgroupid => $basketgroupid } );
297   print $input->redirect("basket.pl?basketno=" . $basketno);
298 } elsif ( $op eq 'closeandprint') {
299 #
300 # close an open basketgroup and generates a pdf
301 #
302     my $basketgroupid = $input->param('basketgroupid');
303     CloseBasketgroup($basketgroupid);
304     printbasketgrouppdf($basketgroupid);
305     exit;
306 }elsif ($op eq 'print'){
307 #
308 # print a closed basketgroup
309 #
310     my $basketgroupid = $input->param('basketgroupid');
311     printbasketgrouppdf($basketgroupid);
312     exit;
313 }elsif ( $op eq "export" ) {
314 #
315 # export a closed basketgroup in csv
316 #
317     my $basketgroupid = $input->param('basketgroupid');
318     print $input->header(
319         -type       => 'text/csv',
320         -attachment => 'basketgroup' . $basketgroupid . '.csv',
321     );
322     print GetBasketGroupAsCSV( $basketgroupid, $input );
323     exit;
324 }elsif( $op eq "delete"){
325 #
326 # delete an closed basketgroup
327 #
328     my $basketgroupid = $input->param('basketgroupid');
329     DelBasketgroup($basketgroupid);
330     print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&amp;listclosed=1');
331 }elsif ( $op eq 'reopen'){
332 #
333 # reopen a closed basketgroup
334 #
335     my $basketgroupid   = $input->param('basketgroupid');
336     my $booksellerid    = $input->param('booksellerid');
337     ReOpenBasketgroup($basketgroupid);
338     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';
339     print $input->redirect($redirectpath);
340 } elsif ( $op eq 'attachbasket') {
341 #
342 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
343 #
344     # Getting parameters
345     my $basketgroup       = {};
346     my @baskets           = $input->multi_param('basket');
347     my $basketgroupid     = $input->param('basketgroupid');
348     my $basketgroupname   = $input->param('basketgroupname');
349     my $booksellerid      = $input->param('booksellerid');
350     my $billingplace      = $input->param('billingplace');
351     my $deliveryplace     = $input->param('deliveryplace');
352     my $freedeliveryplace = $input->param('freedeliveryplace');
353     my $deliverycomment   = $input->param('deliverycomment');
354     my $closedbg          = $input->param('closedbg') ? 1 : 0;
355     if ($basketgroupid) {
356     # If we have a basketgroupid we edit the basketgroup
357         $basketgroup = {
358               name              => $basketgroupname,
359               id                => $basketgroupid,
360               basketlist        => \@baskets,
361               billingplace      => $billingplace,
362               deliveryplace     => $deliveryplace,
363               freedeliveryplace => $freedeliveryplace,
364               deliverycomment   => $deliverycomment,
365               closed            => $closedbg,
366         };
367         ModBasketgroup($basketgroup);
368         if($closedbg){
369 # FIXME
370         }
371     }else{
372     # we create a new basketgroup (with a closed basket)
373         $basketgroup = {
374             name              => $basketgroupname,
375             booksellerid      => $booksellerid,
376             basketlist        => \@baskets,
377             billingplace      => $billingplace,
378             deliveryplace     => $deliveryplace,
379             freedeliveryplace => $freedeliveryplace,
380             deliverycomment   => $deliverycomment,
381             closed            => $closedbg,
382         };
383         $basketgroupid = NewBasketgroup($basketgroup);
384     }
385     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;
386     $redirectpath .=  "&amp;listclosed=1" if $closedbg ;
387     print $input->redirect($redirectpath );
388     
389 } elsif ( $op eq 'ediprint') {
390     my $basketgroupid = $input->param('basketgroupid');
391     generate_edifact_orders( $basketgroupid );
392     exit;
393 }else{
394 # no param : display the list of all basketgroups for a given vendor
395     my $basketgroups = &GetBasketgroups($booksellerid);
396     my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
397     my $baskets = &GetBasketsByBookseller($booksellerid);
398
399     displaybasketgroups($basketgroups, $bookseller, $baskets);
400 }
401 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
402 #prolly won't use all these, maybe just use print, the rest can be done inside validate
403 output_html_with_http_headers $input, $cookie, $template->output;