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 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",
69 flagsrequired => {acquisition => 'group_manage'},
75 my $bookseller = shift;
77 my @orders = GetOrders($basketno);
78 for my $order (@orders){
79 # FIXME The following is wrong
80 if ( $bookseller->listincgst ) {
81 $total = $total + ( $order->{ecost_tax_included} * $order->{quantity} );
83 $total = $total + ( $order->{ecost_tax_excluded} * $order->{quantity} );
86 $total .= " " . ($bookseller->invoiceprice // 0);
90 #displays all basketgroups and all closed baskets (in their respective groups)
91 sub displaybasketgroups {
92 my $basketgroups = shift;
93 my $bookseller = shift;
95 if (scalar @$basketgroups != 0) {
96 foreach my $basketgroup (@$basketgroups){
99 while($i < scalar(@$baskets)){
100 my $basket = @$baskets[$i];
101 if($basket->{'basketgroupid'} && $basket->{'basketgroupid'} == $basketgroup->{'id'}){
102 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
103 push(@{$basketgroup->{'baskets'}}, $basket);
104 splice(@$baskets, $i, 1);
110 $basketgroup -> {'basketsqty'} = $basketsqty;
112 $template->param(basketgroups => $basketgroups);
114 for(my $i=0; $i < scalar @$baskets; ++$i) {
115 if( ! @$baskets[$i]->{'closedate'} ) {
116 splice(@$baskets, $i, 1);
119 @$baskets[$i]->{total} = BasketTotal(@$baskets[$i]->{basketno}, $bookseller);
122 $template->param(baskets => $baskets);
123 $template->param( booksellername => $bookseller->name);
126 sub printbasketgrouppdf{
127 my ($basketgroupid) = @_;
129 my $pdfformat = C4::Context->preference("OrderPdfFormat");
130 if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages' || $pdfformat eq 'pdfformat::layout3pagesfr'
131 || $pdfformat eq 'pdfformat::layout2pagesde'){
133 eval "require $pdfformat";
140 print $input->header;
141 print $input->start_html; # FIXME Should do a nicer page
142 print "<h1>Invalid PDF Format set</h1>";
143 print "Please go to the systempreferences and set a valid pdfformat";
147 my $basketgroup = GetBasketgroup($basketgroupid);
148 my $bookseller = Koha::Acquisition::Booksellers->find( $basketgroup->{booksellerid} );
149 my $baskets = GetBasketsByBasketgroup($basketgroupid);
152 for my $basket (@$baskets) {
154 my @ords = &GetOrders($basket->{basketno});
155 for my $ord (@ords) {
157 next unless ( $ord->{biblionumber} or $ord->{quantity}> 0 );
173 $ord->{tax_value} = $ord->{tax_value_on_ordering};
174 $ord->{tax_rate} = $ord->{tax_rate_on_ordering};
175 $ord->{total_tax_included} = $ord->{ecost_tax_included} * $ord->{quantity};
176 $ord->{total_tax_excluded} = $ord->{ecost_tax_excluded} * $ord->{quantity};
178 my $biblioitem = Koha::Biblioitems->search({ biblionumber => $ord->{biblionumber} })->next;
181 # 0 1 2 3 4 5 6 7 8 9
182 #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount tax_rate
187 $ord->{marcxml} = C4::Biblio::GetXmlBiblio( $ord->{biblionumber} );
188 my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
190 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
191 $en = $marcrecord->subfield( '345', "b" );
192 $edition = $marcrecord->subfield( '205', 'a' );
193 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
194 $en = $marcrecord->subfield( '037', "a" );
195 $edition = $marcrecord->subfield( '250', 'a' );
199 my $itemtype = ( $ord->{itemtype} and $biblioitem->itemtype )
200 ? Koha::ItemTypes->find( $biblioitem->itemtype )
202 $ord->{itemtype} = $itemtype ? $itemtype->description : undef;
204 $ord->{en} = $en ? $en : undef;
205 $ord->{edition} = $edition ? $edition : undef;
207 push(@ba_orders, $ord);
209 $orders{$basket->{basketno}} = \@ba_orders;
211 print $input->header(
212 -type => 'application/pdf',
213 -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
215 my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->tax_rate // C4::Context->preference("gist")) || die "pdf generation failed";
220 sub generate_edifact_orders {
221 my $basketgroupid = shift;
222 my $baskets = GetBasketsByBasketgroup($basketgroupid);
223 my $ean = get_edifact_ean();
226 for my $basket ( @{$baskets} ) {
227 create_edi_order( { ean => $ean, basketno => $basket->{basketno}, } );
230 my $booksellerid = $input->param('booksellerid') || 0;
231 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' .
233 '&message=No%20EDIFACT%20Setup');
238 my $op = $input->param('op') || 'display';
239 # possible values of $op :
240 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
241 # - mod_basket : modify an individual basket of the basketgroup
242 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
243 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
244 # - ediprint : generate edi order messages for the baskets in the group
245 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
246 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
247 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
248 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
249 # - display : display the list of all basketgroups for a vendor
250 my $booksellerid = $input->param('booksellerid');
251 $template->param(booksellerid => $booksellerid);
252 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
254 my $schema = Koha::Database->new()->schema();
255 my $rs = $schema->resultset('VendorEdiAccount')->search(
256 { vendor_id => $booksellerid, } );
257 $template->param( ediaccount => ($rs->count > 0));
259 if ( $op eq "add" ) {
261 # if no param('basketgroupid') is not defined, adds a new basketgroup
262 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
263 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
265 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
266 my $basketgroupid = $input->param('basketgroupid');
269 my $freedeliveryplace;
270 if ( $basketgroupid ) {
271 # Get the selected baskets in the basketgroup to display them
272 my $selecteds = GetBasketsByBasketgroup($basketgroupid);
273 foreach my $basket(@{$selecteds}){
274 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
276 $template->param(basketgroupid => $basketgroupid,
277 selectedbaskets => $selecteds);
279 # Get general informations about the basket group to prefill the form
280 my $basketgroup = GetBasketgroup($basketgroupid);
282 name => $basketgroup->{name},
283 deliverycomment => $basketgroup->{deliverycomment},
284 freedeliveryplace => $basketgroup->{freedeliveryplace},
286 $billingplace = $basketgroup->{billingplace};
287 $deliveryplace = $basketgroup->{deliveryplace};
288 $freedeliveryplace = $basketgroup->{freedeliveryplace};
289 $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
291 $template->param( closedbg => 0);
293 # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
294 my $patron = Koha::Patrons->find( $loggedinuser ); # FIXME Not needed if billingplace and deliveryplace are set
295 $billingplace = $billingplace || $patron->branchcode;
296 $deliveryplace = $deliveryplace || $patron->branchcode;
298 $template->param( billingplace => $billingplace );
299 $template->param( deliveryplace => $deliveryplace );
300 $template->param( booksellerid => $booksellerid );
302 # the template will display a unique basketgroup
303 $template->param(grouping => 1);
304 my $basketgroups = &GetBasketgroups($booksellerid);
305 my $baskets = &GetBasketsByBookseller($booksellerid);
306 displaybasketgroups($basketgroups, $bookseller, $baskets);
307 } elsif ($op eq 'mod_basket') {
309 # edit an individual basket contained in this basketgroup
311 my $basketno=$input->param('basketno');
312 my $basketgroupid=$input->param('basketgroupid');
313 ModBasket( { basketno => $basketno,
314 basketgroupid => $basketgroupid } );
315 print $input->redirect("basket.pl?basketno=" . $basketno);
316 } elsif ( $op eq 'closeandprint') {
318 # close an open basketgroup and generates a pdf
320 my $basketgroupid = $input->param('basketgroupid');
321 CloseBasketgroup($basketgroupid);
322 printbasketgrouppdf($basketgroupid);
324 }elsif ($op eq 'print'){
326 # print a closed basketgroup
328 my $basketgroupid = $input->param('basketgroupid');
329 printbasketgrouppdf($basketgroupid);
331 }elsif ( $op eq "export" ) {
333 # export a closed basketgroup in csv
335 my $basketgroupid = $input->param('basketgroupid');
336 print $input->header(
338 -attachment => 'basketgroup' . $basketgroupid . '.csv',
340 print GetBasketGroupAsCSV( $basketgroupid, $input );
342 }elsif( $op eq "delete"){
344 # delete an closed basketgroup
346 my $basketgroupid = $input->param('basketgroupid');
347 DelBasketgroup($basketgroupid);
348 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&listclosed=1');
349 }elsif ( $op eq 'reopen'){
351 # reopen a closed basketgroup
353 my $basketgroupid = $input->param('basketgroupid');
354 my $booksellerid = $input->param('booksellerid');
355 ReOpenBasketgroup($basketgroupid);
356 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';
357 print $input->redirect($redirectpath);
358 } elsif ( $op eq 'attachbasket') {
360 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
363 my $basketgroup = {};
364 my @baskets = $input->multi_param('basket');
365 my $basketgroupid = $input->param('basketgroupid');
366 my $basketgroupname = $input->param('basketgroupname');
367 my $booksellerid = $input->param('booksellerid');
368 my $billingplace = $input->param('billingplace');
369 my $deliveryplace = $input->param('deliveryplace');
370 my $freedeliveryplace = $input->param('freedeliveryplace');
371 my $deliverycomment = $input->param('deliverycomment');
372 my $closedbg = $input->param('closedbg') ? 1 : 0;
373 if ($basketgroupid) {
374 # If we have a basketgroupid we edit the basketgroup
376 name => $basketgroupname,
377 id => $basketgroupid,
378 basketlist => \@baskets,
379 billingplace => $billingplace,
380 deliveryplace => $deliveryplace,
381 freedeliveryplace => $freedeliveryplace,
382 deliverycomment => $deliverycomment,
385 ModBasketgroup($basketgroup);
390 # we create a new basketgroup (with a closed basket)
392 name => $basketgroupname,
393 booksellerid => $booksellerid,
394 basketlist => \@baskets,
395 billingplace => $billingplace,
396 deliveryplace => $deliveryplace,
397 freedeliveryplace => $freedeliveryplace,
398 deliverycomment => $deliverycomment,
401 $basketgroupid = NewBasketgroup($basketgroup);
403 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;
404 $redirectpath .= "&listclosed=1" if $closedbg ;
405 print $input->redirect($redirectpath );
407 } elsif ( $op eq 'ediprint') {
408 my $basketgroupid = $input->param('basketgroupid');
409 if ($template->param( 'ediaccount' )) {
410 generate_edifact_orders( $basketgroupid );
413 $template->param('NoEDIMessage' => 1);
414 my $basketgroups = &GetBasketgroups($booksellerid);
415 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
416 my $baskets = &GetBasketsByBookseller($booksellerid);
418 displaybasketgroups($basketgroups, $bookseller, $baskets);
421 # no param : display the list of all basketgroups for a given vendor
422 my $basketgroups = &GetBasketgroups($booksellerid);
423 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
424 my $baskets = &GetBasketsByBookseller($booksellerid);
426 displaybasketgroups($basketgroups, $bookseller, $baskets);
428 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
429 #prolly won't use all these, maybe just use print, the rest can be done inside validate
430 output_html_with_http_headers $input, $cookie, $template->output;