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 get_rounded_price/;
55 use Koha::EDI qw/create_edi_order get_edifact_ean/;
57 use Koha::Biblioitems;
58 use Koha::Acquisition::Booksellers;
64 our ($template, $loggedinuser, $cookie)
65 = get_template_and_user({template_name => "acqui/basketgroup.tt",
68 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 + ( get_rounded_price($order->{ecost_tax_included}) * $order->{quantity} );
81 $total = $total + ( get_rounded_price($order->{ecost_tax_excluded}) * $order->{quantity} );
87 #displays all basketgroups and all closed baskets (in their respective groups)
88 sub displaybasketgroups {
89 my $basketgroups = shift;
90 my $bookseller = shift;
92 if (scalar @$basketgroups != 0) {
93 foreach my $basketgroup (@$basketgroups){
96 while($i < scalar(@$baskets)){
97 my $basket = @$baskets[$i];
98 if($basket->{'basketgroupid'} && $basket->{'basketgroupid'} == $basketgroup->{'id'}){
99 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
100 push(@{$basketgroup->{'baskets'}}, $basket);
101 splice(@$baskets, $i, 1);
107 $basketgroup -> {'basketsqty'} = $basketsqty;
109 $template->param(basketgroups => $basketgroups);
111 for(my $i=0; $i < scalar @$baskets; ++$i) {
112 if( ! @$baskets[$i]->{'closedate'} ) {
113 splice(@$baskets, $i, 1);
116 @$baskets[$i]->{total} = BasketTotal(@$baskets[$i]->{basketno}, $bookseller);
119 $template->param(baskets => $baskets);
120 $template->param( booksellername => $bookseller->name);
123 sub printbasketgrouppdf{
124 my ($basketgroupid) = @_;
126 my $pdfformat = C4::Context->preference("OrderPdfFormat");
127 my @valid_pdfformats = qw(pdfformat::layout3pages pdfformat::layout2pages pdfformat::layout3pagesfr pdfformat::layout2pagesde);
128 if (grep {$_ eq $pdfformat} @valid_pdfformats) {
129 $pdfformat = "Koha::$pdfformat";
130 my $pdfformat_filepath = File::Spec->catfile(split /::/, $pdfformat) . '.pm';
131 require $pdfformat_filepath;
132 import $pdfformat qw(printpdf);
135 print $input->header;
136 print $input->start_html; # FIXME Should do a nicer page
137 print "<h1>Invalid PDF Format set</h1>";
138 print "Please go to the systempreferences and set a valid pdfformat";
142 my $basketgroup = GetBasketgroup($basketgroupid);
143 my $bookseller = Koha::Acquisition::Booksellers->find( $basketgroup->{booksellerid} );
144 my $baskets = GetBasketsByBasketgroup($basketgroupid);
147 for my $basket (@$baskets) {
149 my @ords = &GetOrders($basket->{basketno});
150 for my $ord (@ords) {
152 next unless ( $ord->{biblionumber} or $ord->{quantity}> 0 );
168 $ord->{tax_value} = $ord->{tax_value_on_ordering};
169 $ord->{tax_rate} = $ord->{tax_rate_on_ordering};
170 $ord->{total_tax_included} = get_rounded_price($ord->{ecost_tax_included}) * $ord->{quantity};
171 $ord->{total_tax_excluded} = get_rounded_price($ord->{ecost_tax_excluded}) * $ord->{quantity};
173 my $biblioitem = Koha::Biblioitems->search({ biblionumber => $ord->{biblionumber} })->next;
176 # 0 1 2 3 4 5 6 7 8 9
177 #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount tax_rate
182 $ord->{marcxml} = C4::Biblio::GetXmlBiblio( $ord->{biblionumber} );
183 my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
185 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
186 $en = $marcrecord->subfield( '345', "b" );
187 $edition = $marcrecord->subfield( '205', 'a' );
188 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
189 $en = $marcrecord->subfield( '037', "a" );
190 $edition = $marcrecord->subfield( '250', 'a' );
194 my $itemtype = ( $ord->{itemtype} and $biblioitem->itemtype )
195 ? Koha::ItemTypes->find( $biblioitem->itemtype )
197 $ord->{itemtype} = $itemtype ? $itemtype->description : undef;
199 $ord->{en} = $en ? $en : undef;
200 $ord->{edition} = $edition ? $edition : undef;
202 push(@ba_orders, $ord);
204 $orders{$basket->{basketno}} = \@ba_orders;
206 print $input->header(
207 -type => 'application/pdf',
208 -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
210 my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->tax_rate // C4::Context->preference("TaxRates")) || die "pdf generation failed";
215 sub generate_edifact_orders {
216 my $basketgroupid = shift;
217 my $baskets = GetBasketsByBasketgroup($basketgroupid);
218 my $ean = get_edifact_ean();
221 for my $basket ( @{$baskets} ) {
222 create_edi_order( { ean => $ean, basketno => $basket->{basketno}, } );
225 my $booksellerid = $input->param('booksellerid') || 0;
226 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' .
228 '&message=No%20EDIFACT%20Setup');
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 # - ediprint : generate edi order messages for the baskets in the group
240 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
241 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
242 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
243 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
244 # - display : display the list of all basketgroups for a vendor
245 my $booksellerid = $input->param('booksellerid');
246 $template->param(booksellerid => $booksellerid);
247 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
249 my $schema = Koha::Database->new()->schema();
250 my $rs = $schema->resultset('VendorEdiAccount')->search(
251 { vendor_id => $booksellerid, } );
252 $template->param( ediaccount => ($rs->count > 0));
254 if ( $op eq "add" ) {
256 # if no param('basketgroupid') is not defined, adds a new basketgroup
257 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
258 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
260 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
261 my $basketgroupid = $input->param('basketgroupid');
264 my $freedeliveryplace;
265 if ( $basketgroupid ) {
266 # Get the selected baskets in the basketgroup to display them
267 my $selecteds = GetBasketsByBasketgroup($basketgroupid);
268 foreach my $basket(@{$selecteds}){
269 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
271 $template->param(basketgroupid => $basketgroupid,
272 selectedbaskets => $selecteds);
274 # Get general informations about the basket group to prefill the form
275 my $basketgroup = GetBasketgroup($basketgroupid);
277 name => $basketgroup->{name},
278 deliverycomment => $basketgroup->{deliverycomment},
279 freedeliveryplace => $basketgroup->{freedeliveryplace},
281 $billingplace = $basketgroup->{billingplace};
282 $deliveryplace = $basketgroup->{deliveryplace};
283 $freedeliveryplace = $basketgroup->{freedeliveryplace};
284 $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
286 # When creating a new basket group preselect billing and delivery place based on logged-in user
287 my $patron = Koha::Patrons->find( $loggedinuser );
288 $billingplace = $patron->branchcode;
289 $deliveryplace = $patron->branchcode;
290 $template->param( closedbg => 0);
293 $template->param( billingplace => $billingplace );
294 $template->param( deliveryplace => $deliveryplace );
295 $template->param( booksellerid => $booksellerid );
297 # the template will display a unique basketgroup
298 $template->param(grouping => 1);
299 my $basketgroups = &GetBasketgroups($booksellerid);
300 my $baskets = &GetBasketsByBookseller($booksellerid);
301 displaybasketgroups($basketgroups, $bookseller, $baskets);
302 } elsif ($op eq 'mod_basket') {
304 # edit an individual basket contained in this basketgroup
306 my $basketno=$input->param('basketno');
307 my $basketgroupid=$input->param('basketgroupid');
308 ModBasket( { basketno => $basketno,
309 basketgroupid => $basketgroupid } );
310 print $input->redirect("basket.pl?basketno=" . $basketno);
311 } elsif ( $op eq 'closeandprint') {
313 # close an open basketgroup and generates a pdf
315 my $basketgroupid = $input->param('basketgroupid');
316 CloseBasketgroup($basketgroupid);
317 printbasketgrouppdf($basketgroupid);
319 }elsif ($op eq 'print'){
321 # print a closed basketgroup
323 my $basketgroupid = $input->param('basketgroupid');
324 printbasketgrouppdf($basketgroupid);
326 }elsif ( $op eq "export" ) {
328 # export a closed basketgroup in csv
330 my $basketgroupid = $input->param('basketgroupid');
331 my $basketgroup = GetBasketgroup($basketgroupid);
332 print $input->header(
334 -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.csv'
336 print GetBasketGroupAsCSV( $basketgroupid, $input );
338 }elsif( $op eq "delete"){
340 # delete an closed basketgroup
342 my $basketgroupid = $input->param('basketgroupid');
343 DelBasketgroup($basketgroupid);
344 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&listclosed=1');
345 }elsif ( $op eq 'reopen'){
347 # reopen a closed basketgroup
349 my $basketgroupid = $input->param('basketgroupid');
350 my $booksellerid = $input->param('booksellerid');
351 ReOpenBasketgroup($basketgroupid);
352 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';
353 print $input->redirect($redirectpath);
354 } elsif ( $op eq 'attachbasket') {
356 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
359 my $basketgroup = {};
360 my @baskets = $input->multi_param('basket');
361 my $basketgroupid = $input->param('basketgroupid');
362 my $basketgroupname = $input->param('basketgroupname');
363 my $booksellerid = $input->param('booksellerid');
364 my $billingplace = $input->param('billingplace');
365 my $deliveryplace = $input->param('deliveryplace');
366 my $freedeliveryplace = $input->param('freedeliveryplace');
367 my $deliverycomment = $input->param('deliverycomment');
368 my $closedbg = $input->param('closedbg') ? 1 : 0;
369 if ($basketgroupid) {
370 # If we have a basketgroupid we edit the basketgroup
372 name => $basketgroupname,
373 id => $basketgroupid,
374 basketlist => \@baskets,
375 billingplace => $billingplace,
376 deliveryplace => $deliveryplace,
377 freedeliveryplace => $freedeliveryplace,
378 deliverycomment => $deliverycomment,
381 ModBasketgroup($basketgroup);
386 # we create a new basketgroup (with a closed basket)
388 name => $basketgroupname,
389 booksellerid => $booksellerid,
390 basketlist => \@baskets,
391 billingplace => $billingplace,
392 deliveryplace => $deliveryplace,
393 freedeliveryplace => $freedeliveryplace,
394 deliverycomment => $deliverycomment,
397 $basketgroupid = NewBasketgroup($basketgroup);
399 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;
400 $redirectpath .= "&listclosed=1" if $closedbg ;
401 print $input->redirect($redirectpath );
403 } elsif ( $op eq 'ediprint') {
404 my $basketgroupid = $input->param('basketgroupid');
405 if ($template->param( 'ediaccount' )) {
406 generate_edifact_orders( $basketgroupid );
409 $template->param('NoEDIMessage' => 1);
410 my $basketgroups = &GetBasketgroups($booksellerid);
411 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
412 my $baskets = &GetBasketsByBookseller($booksellerid);
414 displaybasketgroups($basketgroups, $bookseller, $baskets);
417 # no param : display the list of all basketgroups for a given vendor
418 my $basketgroups = &GetBasketgroups($booksellerid);
419 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
420 my $baskets = &GetBasketsByBookseller($booksellerid);
422 displaybasketgroups($basketgroups, $bookseller, $baskets);
424 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
425 #prolly won't use all these, maybe just use print, the rest can be done inside validate
426 output_html_with_http_headers $input, $cookie, $template->output;