Bug 17578 [QA Followup] - Update number of tests
[koha.git] / acqui / basketgroup.pl
blob9ad5482e45bdad5692d757c780da963736fe959a
1 #!/usr/bin/perl
3 #script to group (closed) baskets into basket groups for easier order management
4 #written by john.soros@biblibre.com 01/10/2008
6 # Copyright 2008 - 2009 BibLibre SARL
7 # Parts Copyright Catalyst 2010
9 # This file is part of Koha.
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.
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.
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>.
25 =head1 NAME
27 basketgroup.pl
29 =head1 DESCRIPTION
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.
34 =head1 CGI PARAMETERS
36 =over 4
38 =item $booksellerid
40 The bookseller who we want to display the baskets (and basketgroups) of.
42 =back
44 =cut
46 use strict;
47 use warnings;
48 use Carp;
50 use C4::Auth;
51 use C4::Output;
52 use CGI qw ( -utf8 );
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/;
58 use Koha::Acquisition::Bookseller;
60 our $input=new CGI;
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 });
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} );
84 $total .= " " . ($bookseller->{invoiceprice} // 0);
85 return $total;
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;
106 ++$i;
108 $basketgroup -> {'basketsqty'} = $basketsqty;
110 $template->param(basketgroups => $basketgroups);
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);
120 $template->param(baskets => $baskets);
121 $template->param( booksellername => $bookseller ->{'name'});
124 sub printbasketgrouppdf{
125 my ($basketgroupid) = @_;
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;
134 if ($@){
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;
145 my $basketgroup = GetBasketgroup($basketgroupid);
146 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basketgroup->{booksellerid} });
147 my $baskets = GetBasketsByBasketgroup($basketgroupid);
149 my %orders;
150 for my $basket (@$baskets) {
151 my @ba_orders;
152 my @ords = &GetOrders($basket->{basketno});
153 for my $ord (@ords) {
155 next unless ( $ord->{biblionumber} or $ord->{quantity}> 0 );
156 eval {
157 require C4::Biblio;
158 import C4::Biblio;
160 if ($@){
161 croak $@;
163 eval {
164 require C4::Koha;
165 import C4::Koha;
167 if ($@){
168 croak $@;
171 $ord->{tax_value} = $ord->{tax_value_on_ordering};
172 $ord->{tax_rate} = $ord->{tax_rate_on_ordering};
173 $ord->{total_tax_included} = $ord->{ecost_tax_included} * $ord->{quantity};
174 $ord->{total_tax_excluded} = $ord->{ecost_tax_excluded} * $ord->{quantity};
176 my $bib = GetBiblioData($ord->{biblionumber});
177 my $itemtypes = GetItemTypes();
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
183 # Editor Number
184 my $en;
185 my $edition;
186 my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
187 if ($marcrecord){
188 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
189 $en = $marcrecord->subfield( '345', "b" );
190 $edition = $marcrecord->subfield( '205', 'a' );
191 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
192 $en = $marcrecord->subfield( '037', "a" );
193 $edition = $marcrecord->subfield( '250', 'a' );
197 $ord->{itemtype} = ( $ord->{itemtype} and $bib->{itemtype} ) ? $itemtypes->{$bib->{itemtype}}->{description} : undef;
198 $ord->{en} = $en ? $en : undef;
199 $ord->{edition} = $edition ? $edition : undef;
201 push(@ba_orders, $ord);
203 $orders{$basket->{basketno}} = \@ba_orders;
205 print $input->header(
206 -type => 'application/pdf',
207 -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
209 my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{tax_rate} // C4::Context->preference("gist")) || die "pdf generation failed";
210 print $pdf;
214 sub generate_edifact_orders {
215 my $basketgroupid = shift;
216 my $baskets = GetBasketsByBasketgroup($basketgroupid);
217 my $ean = get_edifact_ean();
219 for my $basket ( @{$baskets} ) {
220 create_edi_order( { ean => $ean, basketno => $basket->{basketno}, } );
222 return;
225 my $op = $input->param('op') || 'display';
226 # possible values of $op :
227 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
228 # - mod_basket : modify an individual basket of the basketgroup
229 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
230 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
231 # - ediprint : generate edi order messages for the baskets in the group
232 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
233 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
234 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
235 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
236 # - display : display the list of all basketgroups for a vendor
237 my $booksellerid = $input->param('booksellerid');
238 $template->param(booksellerid => $booksellerid);
240 if ( $op eq "add" ) {
242 # if no param('basketgroupid') is not defined, adds a new basketgroup
243 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
244 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
246 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
247 my $basketgroupid = $input->param('basketgroupid');
248 my $billingplace;
249 my $deliveryplace;
250 my $freedeliveryplace;
251 if ( $basketgroupid ) {
252 # Get the selected baskets in the basketgroup to display them
253 my $selecteds = GetBasketsByBasketgroup($basketgroupid);
254 foreach my $basket(@{$selecteds}){
255 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
257 $template->param(basketgroupid => $basketgroupid,
258 selectedbaskets => $selecteds);
260 # Get general informations about the basket group to prefill the form
261 my $basketgroup = GetBasketgroup($basketgroupid);
262 $template->param(
263 name => $basketgroup->{name},
264 deliverycomment => $basketgroup->{deliverycomment},
265 freedeliveryplace => $basketgroup->{freedeliveryplace},
267 $billingplace = $basketgroup->{billingplace};
268 $deliveryplace = $basketgroup->{deliveryplace};
269 $freedeliveryplace = $basketgroup->{freedeliveryplace};
270 $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
271 } else {
272 $template->param( closedbg => 0);
274 # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
275 my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) );
276 $billingplace = $billingplace || $borrower->{'branchcode'};
277 $deliveryplace = $deliveryplace || $borrower->{'branchcode'};
279 $template->param( billingplace => $billingplace );
280 $template->param( deliveryplace => $deliveryplace );
281 $template->param( booksellerid => $booksellerid );
283 # the template will display a unique basketgroup
284 $template->param(grouping => 1);
285 my $basketgroups = &GetBasketgroups($booksellerid);
286 my $baskets = &GetBasketsByBookseller($booksellerid);
287 displaybasketgroups($basketgroups, $bookseller, $baskets);
288 } elsif ($op eq 'mod_basket') {
290 # edit an individual basket contained in this basketgroup
292 my $basketno=$input->param('basketno');
293 my $basketgroupid=$input->param('basketgroupid');
294 ModBasket( { basketno => $basketno,
295 basketgroupid => $basketgroupid } );
296 print $input->redirect("basket.pl?basketno=" . $basketno);
297 } elsif ( $op eq 'closeandprint') {
299 # close an open basketgroup and generates a pdf
301 my $basketgroupid = $input->param('basketgroupid');
302 CloseBasketgroup($basketgroupid);
303 printbasketgrouppdf($basketgroupid);
304 exit;
305 }elsif ($op eq 'print'){
307 # print a closed basketgroup
309 my $basketgroupid = $input->param('basketgroupid');
310 printbasketgrouppdf($basketgroupid);
311 exit;
312 }elsif ( $op eq "export" ) {
314 # export a closed basketgroup in csv
316 my $basketgroupid = $input->param('basketgroupid');
317 print $input->header(
318 -type => 'text/csv',
319 -attachment => 'basketgroup' . $basketgroupid . '.csv',
321 print GetBasketGroupAsCSV( $basketgroupid, $input );
322 exit;
323 }elsif( $op eq "delete"){
325 # delete an closed basketgroup
327 my $basketgroupid = $input->param('basketgroupid');
328 DelBasketgroup($basketgroupid);
329 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&amp;listclosed=1');
330 }elsif ( $op eq 'reopen'){
332 # reopen a closed basketgroup
334 my $basketgroupid = $input->param('basketgroupid');
335 my $booksellerid = $input->param('booksellerid');
336 ReOpenBasketgroup($basketgroupid);
337 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';
338 print $input->redirect($redirectpath);
339 } elsif ( $op eq 'attachbasket') {
341 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
343 # Getting parameters
344 my $basketgroup = {};
345 my @baskets = $input->multi_param('basket');
346 my $basketgroupid = $input->param('basketgroupid');
347 my $basketgroupname = $input->param('basketgroupname');
348 my $booksellerid = $input->param('booksellerid');
349 my $billingplace = $input->param('billingplace');
350 my $deliveryplace = $input->param('deliveryplace');
351 my $freedeliveryplace = $input->param('freedeliveryplace');
352 my $deliverycomment = $input->param('deliverycomment');
353 my $closedbg = $input->param('closedbg') ? 1 : 0;
354 if ($basketgroupid) {
355 # If we have a basketgroupid we edit the basketgroup
356 $basketgroup = {
357 name => $basketgroupname,
358 id => $basketgroupid,
359 basketlist => \@baskets,
360 billingplace => $billingplace,
361 deliveryplace => $deliveryplace,
362 freedeliveryplace => $freedeliveryplace,
363 deliverycomment => $deliverycomment,
364 closed => $closedbg,
366 ModBasketgroup($basketgroup);
367 if($closedbg){
368 # FIXME
370 }else{
371 # we create a new basketgroup (with a closed basket)
372 $basketgroup = {
373 name => $basketgroupname,
374 booksellerid => $booksellerid,
375 basketlist => \@baskets,
376 billingplace => $billingplace,
377 deliveryplace => $deliveryplace,
378 freedeliveryplace => $freedeliveryplace,
379 deliverycomment => $deliverycomment,
380 closed => $closedbg,
382 $basketgroupid = NewBasketgroup($basketgroup);
384 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;
385 $redirectpath .= "&amp;listclosed=1" if $closedbg ;
386 print $input->redirect($redirectpath );
388 } elsif ( $op eq 'ediprint') {
389 my $basketgroupid = $input->param('basketgroupid');
390 generate_edifact_orders( $basketgroupid );
391 exit;
392 }else{
393 # no param : display the list of all basketgroups for a given vendor
394 my $basketgroups = &GetBasketgroups($booksellerid);
395 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
396 my $baskets = &GetBasketsByBookseller($booksellerid);
398 displaybasketgroups($basketgroups, $bookseller, $baskets);
400 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
401 #prolly won't use all these, maybe just use print, the rest can be done inside validate
402 output_html_with_http_headers $input, $cookie, $template->output;