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>.
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.
40 The bookseller who we want to display the baskets (and basketgroups) of.
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
;
62 our ($template, $loggedinuser, $cookie)
63 = get_template_and_user
({template_name
=> "acqui/basketgroup.tt",
67 flagsrequired
=> {acquisition
=> 'group_manage'},
73 my $bookseller = shift;
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
} );
81 $total = $total + ( $order->{ecost_tax_excluded
} * $order->{quantity
} );
84 $total .= " " . ($bookseller->{invoiceprice
} // 0);
88 #displays all basketgroups and all closed baskets (in their respective groups)
89 sub displaybasketgroups
{
90 my $basketgroups = shift;
91 my $bookseller = shift;
93 if (scalar @
$basketgroups != 0) {
94 foreach my $basketgroup (@
$basketgroups){
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);
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);
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'){
131 eval "require $pdfformat";
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";
145 my $basketgroup = GetBasketgroup
($basketgroupid);
146 my $bookseller = Koha
::Acquisition
::Bookseller
->fetch({ id
=> $basketgroup->{booksellerid
} });
147 my $baskets = GetBasketsByBasketgroup
($basketgroupid);
150 for my $basket (@
$baskets) {
152 my @ords = &GetOrders
($basket->{basketno
});
153 for my $ord (@ords) {
155 next unless ( $ord->{biblionumber
} or $ord->{quantity
}> 0 );
171 $ord->{total_tax_included
} = $ord->{ecost_tax_included
} * $ord->{quantity
};
172 $ord->{total_tax_excluded
} = $ord->{ecost_tax_excluded
} * $ord->{quantity
};
174 my $bib = GetBiblioData
($ord->{biblionumber
});
175 my $itemtypes = GetItemTypes
();
178 # 0 1 2 3 4 5 6 7 8 9
179 #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount tax_rate
184 my $marcrecord=eval{MARC
::Record
::new_from_xml
( $ord->{marcxml
},'UTF-8' )};
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' );
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;
199 push(@ba_orders, $ord);
201 $orders{$basket->{basketno
}} = \
@ba_orders;
203 print $input->header(
204 -type
=> 'application/pdf',
205 -attachment
=> ( $basketgroup->{name
} || $basketgroupid ) . '.pdf'
207 my $pdf = printpdf
($basketgroup, $bookseller, $baskets, \
%orders, $bookseller->{tax_rate
} // C4
::Context
->preference("gist")) || die "pdf generation failed";
212 sub generate_edifact_orders
{
213 my $basketgroupid = shift;
214 my $baskets = GetBasketsByBasketgroup
($basketgroupid);
215 my $ean = get_edifact_ean
();
217 for my $basket ( @
{$baskets} ) {
218 create_edi_order
( { ean
=> $ean, basketno
=> $basket->{basketno
}, } );
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);
238 if ( $op eq "add" ) {
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
244 my $bookseller = Koha
::Acquisition
::Bookseller
->fetch({ id
=> $booksellerid });
245 my $basketgroupid = $input->param('basketgroupid');
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);
255 $template->param(basketgroupid
=> $basketgroupid,
256 selectedbaskets
=> $selecteds);
258 # Get general informations about the basket group to prefill the form
259 my $basketgroup = GetBasketgroup
($basketgroupid);
261 name
=> $basketgroup->{name
},
262 deliverycomment
=> $basketgroup->{deliverycomment
},
263 freedeliveryplace
=> $basketgroup->{freedeliveryplace
},
265 $billingplace = $basketgroup->{billingplace
};
266 $deliveryplace = $basketgroup->{deliveryplace
};
267 $freedeliveryplace = $basketgroup->{freedeliveryplace
};
268 $template->param( closedbg
=> ($basketgroup ->{'closed'}) ?
1 : 0);
270 $template->param( closedbg
=> 0);
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'};
277 $template->param( billingplace
=> $billingplace );
278 $template->param( deliveryplace
=> $deliveryplace );
279 $template->param( booksellerid
=> $booksellerid );
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') {
288 # edit an individual basket contained in this basketgroup
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') {
297 # close an open basketgroup and generates a pdf
299 my $basketgroupid = $input->param('basketgroupid');
300 CloseBasketgroup
($basketgroupid);
301 printbasketgrouppdf
($basketgroupid);
303 }elsif ($op eq 'print'){
305 # print a closed basketgroup
307 my $basketgroupid = $input->param('basketgroupid');
308 printbasketgrouppdf
($basketgroupid);
310 }elsif ( $op eq "export" ) {
312 # export a closed basketgroup in csv
314 my $basketgroupid = $input->param('basketgroupid');
315 print $input->header(
317 -attachment
=> 'basketgroup' . $basketgroupid . '.csv',
319 print GetBasketGroupAsCSV
( $basketgroupid, $input );
321 }elsif( $op eq "delete"){
323 # delete an closed basketgroup
325 my $basketgroupid = $input->param('basketgroupid');
326 DelBasketgroup
($basketgroupid);
327 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&listclosed=1');
328 }elsif ( $op eq 'reopen'){
330 # reopen a closed basketgroup
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&basketgroupid='.$basketgroupid.'&booksellerid='.$booksellerid : '/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' .$booksellerid.'&listclosed=1';
336 print $input->redirect($redirectpath);
337 } elsif ( $op eq 'attachbasket') {
339 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
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
355 name
=> $basketgroupname,
356 id
=> $basketgroupid,
357 basketlist
=> \
@baskets,
358 billingplace
=> $billingplace,
359 deliveryplace
=> $deliveryplace,
360 freedeliveryplace
=> $freedeliveryplace,
361 deliverycomment
=> $deliverycomment,
364 ModBasketgroup
($basketgroup);
369 # we create a new basketgroup (with a closed basket)
371 name
=> $basketgroupname,
372 booksellerid
=> $booksellerid,
373 basketlist
=> \
@baskets,
374 billingplace
=> $billingplace,
375 deliveryplace
=> $deliveryplace,
376 freedeliveryplace
=> $freedeliveryplace,
377 deliverycomment
=> $deliverycomment,
380 $basketgroupid = NewBasketgroup
($basketgroup);
382 my $redirectpath = ((defined $input->param('mode')) && ($input->param('mode') eq 'singlebg')) ?
'/cgi-bin/koha/acqui/basketgroup.pl?op=add&basketgroupid='.$basketgroupid.'&booksellerid='.$booksellerid : '/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid;
383 $redirectpath .= "&listclosed=1" if $closedbg ;
384 print $input->redirect($redirectpath );
386 } elsif ( $op eq 'ediprint') {
387 my $basketgroupid = $input->param('basketgroupid');
388 generate_edifact_orders
( $basketgroupid );
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);
396 displaybasketgroups
($basketgroups, $bookseller, $baskets);
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;