Bug 23354: (RM follow-up) Italian sample notice
[koha.git] / acqui / basketgroup.pl
blobffa4ffb2aa533b233952f7de44c20d92a3dd7984
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 Modern::Perl;
47 use Carp;
49 use C4::Auth;
50 use C4::Output;
51 use CGI qw ( -utf8 );
52 use File::Spec;
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;
59 use Koha::ItemTypes;
60 use Koha::Patrons;
62 our $input=new CGI;
64 our ($template, $loggedinuser, $cookie)
65 = get_template_and_user({template_name => "acqui/basketgroup.tt",
66 query => $input,
67 type => "intranet",
68 authnotrequired => 0,
69 flagsrequired => {acquisition => 'group_manage'},
70 debug => 1,
71 });
73 sub BasketTotal {
74 my $basketno = shift;
75 my $bookseller = shift;
76 my $total = 0;
77 my @orders = GetOrders($basketno);
78 for my $order (@orders){
79 # FIXME The following is wrong
80 if ( $bookseller->listincgst ) {
81 $total = $total + ( get_rounded_price($order->{ecost_tax_included}) * $order->{quantity} );
82 } else {
83 $total = $total + ( get_rounded_price($order->{ecost_tax_excluded}) * $order->{quantity} );
86 return $total;
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;
107 ++$i;
109 $basketgroup -> {'basketsqty'} = $basketsqty;
111 $template->param(basketgroups => $basketgroups);
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);
121 $template->param(baskets => $baskets);
122 $template->param( booksellername => $bookseller->name);
125 sub printbasketgrouppdf{
126 my ($basketgroupid) = @_;
128 my $pdfformat = C4::Context->preference("OrderPdfFormat");
129 my @valid_pdfformats = qw(pdfformat::layout3pages pdfformat::layout2pages pdfformat::layout3pagesfr pdfformat::layout2pagesde);
130 if (grep {$_ eq $pdfformat} @valid_pdfformats) {
131 $pdfformat = "Koha::$pdfformat";
132 my $pdfformat_filepath = File::Spec->catfile(split /::/, $pdfformat) . '.pm';
133 require $pdfformat_filepath;
134 import $pdfformat qw(printpdf);
136 else {
137 print $input->header;
138 print $input->start_html; # FIXME Should do a nicer page
139 print "<h1>Invalid PDF Format set</h1>";
140 print "Please go to the systempreferences and set a valid pdfformat";
141 exit;
144 my $basketgroup = GetBasketgroup($basketgroupid);
145 my $bookseller = Koha::Acquisition::Booksellers->find( $basketgroup->{booksellerid} );
146 my $baskets = GetBasketsByBasketgroup($basketgroupid);
148 my %orders;
149 for my $basket (@$baskets) {
150 my @ba_orders;
151 my @ords = &GetOrders($basket->{basketno});
152 for my $ord (@ords) {
154 next unless ( $ord->{biblionumber} or $ord->{quantity}> 0 );
155 eval {
156 require C4::Biblio;
157 import C4::Biblio;
159 if ($@){
160 croak $@;
162 eval {
163 require C4::Koha;
164 import C4::Koha;
166 if ($@){
167 croak $@;
170 $ord->{tax_value} = $ord->{tax_value_on_ordering};
171 $ord->{tax_rate} = $ord->{tax_rate_on_ordering};
172 $ord->{total_tax_included} = get_rounded_price($ord->{ecost_tax_included}) * $ord->{quantity};
173 $ord->{total_tax_excluded} = get_rounded_price($ord->{ecost_tax_excluded}) * $ord->{quantity};
175 my $biblioitem = Koha::Biblioitems->search({ biblionumber => $ord->{biblionumber} })->next;
177 #FIXME DELETE ME
178 # 0 1 2 3 4 5 6 7 8 9
179 #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount tax_rate
181 # Editor Number
182 my $en;
183 my $edition;
184 $ord->{marcxml} = C4::Biblio::GetXmlBiblio( $ord->{biblionumber} );
185 my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
186 if ($marcrecord){
187 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
188 $en = $marcrecord->subfield( '345', "b" );
189 $edition = $marcrecord->subfield( '205', 'a' );
190 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
191 $en = $marcrecord->subfield( '037', "a" );
192 $edition = $marcrecord->subfield( '250', 'a' );
196 my $itemtype = ( $ord->{itemtype} and $biblioitem->itemtype )
197 ? Koha::ItemTypes->find( $biblioitem->itemtype )
198 : undef;
199 $ord->{itemtype} = $itemtype ? $itemtype->description : undef;
201 $ord->{en} = $en ? $en : undef;
202 $ord->{edition} = $edition ? $edition : undef;
204 push(@ba_orders, $ord);
206 $orders{$basket->{basketno}} = \@ba_orders;
208 print $input->header(
209 -type => 'application/pdf',
210 -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
212 my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->tax_rate // C4::Context->preference("gist")) || die "pdf generation failed";
213 print $pdf;
217 sub generate_edifact_orders {
218 my $basketgroupid = shift;
219 my $baskets = GetBasketsByBasketgroup($basketgroupid);
220 my $ean = get_edifact_ean();
222 if($ean) {
223 for my $basket ( @{$baskets} ) {
224 create_edi_order( { ean => $ean, basketno => $basket->{basketno}, } );
226 } else {
227 my $booksellerid = $input->param('booksellerid') || 0;
228 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' .
229 $booksellerid .
230 '&message=No%20EDIFACT%20Setup');
232 return;
235 my $op = $input->param('op') || 'display';
236 # possible values of $op :
237 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
238 # - mod_basket : modify an individual basket of the basketgroup
239 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
240 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
241 # - ediprint : generate edi order messages for the baskets in the group
242 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
243 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
244 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
245 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
246 # - display : display the list of all basketgroups for a vendor
247 my $booksellerid = $input->param('booksellerid');
248 $template->param(booksellerid => $booksellerid);
249 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
251 my $schema = Koha::Database->new()->schema();
252 my $rs = $schema->resultset('VendorEdiAccount')->search(
253 { vendor_id => $booksellerid, } );
254 $template->param( ediaccount => ($rs->count > 0));
256 if ( $op eq "add" ) {
258 # if no param('basketgroupid') is not defined, adds a new basketgroup
259 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
260 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
262 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
263 my $basketgroupid = $input->param('basketgroupid');
264 my $billingplace;
265 my $deliveryplace;
266 my $freedeliveryplace;
267 if ( $basketgroupid ) {
268 # Get the selected baskets in the basketgroup to display them
269 my $selecteds = GetBasketsByBasketgroup($basketgroupid);
270 foreach my $basket(@{$selecteds}){
271 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
273 $template->param(basketgroupid => $basketgroupid,
274 selectedbaskets => $selecteds);
276 # Get general informations about the basket group to prefill the form
277 my $basketgroup = GetBasketgroup($basketgroupid);
278 $template->param(
279 name => $basketgroup->{name},
280 deliverycomment => $basketgroup->{deliverycomment},
281 freedeliveryplace => $basketgroup->{freedeliveryplace},
283 $billingplace = $basketgroup->{billingplace};
284 $deliveryplace = $basketgroup->{deliveryplace};
285 $freedeliveryplace = $basketgroup->{freedeliveryplace};
286 $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
287 } else {
288 $template->param( closedbg => 0);
290 # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
291 my $patron = Koha::Patrons->find( $loggedinuser ); # FIXME Not needed if billingplace and deliveryplace are set
292 $billingplace = $billingplace || $patron->branchcode;
293 $deliveryplace = $deliveryplace || $patron->branchcode;
295 $template->param( billingplace => $billingplace );
296 $template->param( deliveryplace => $deliveryplace );
297 $template->param( booksellerid => $booksellerid );
299 # the template will display a unique basketgroup
300 $template->param(grouping => 1);
301 my $basketgroups = &GetBasketgroups($booksellerid);
302 my $baskets = &GetBasketsByBookseller($booksellerid);
303 displaybasketgroups($basketgroups, $bookseller, $baskets);
304 } elsif ($op eq 'mod_basket') {
306 # edit an individual basket contained in this basketgroup
308 my $basketno=$input->param('basketno');
309 my $basketgroupid=$input->param('basketgroupid');
310 ModBasket( { basketno => $basketno,
311 basketgroupid => $basketgroupid } );
312 print $input->redirect("basket.pl?basketno=" . $basketno);
313 } elsif ( $op eq 'closeandprint') {
315 # close an open basketgroup and generates a pdf
317 my $basketgroupid = $input->param('basketgroupid');
318 CloseBasketgroup($basketgroupid);
319 printbasketgrouppdf($basketgroupid);
320 exit;
321 }elsif ($op eq 'print'){
323 # print a closed basketgroup
325 my $basketgroupid = $input->param('basketgroupid');
326 printbasketgrouppdf($basketgroupid);
327 exit;
328 }elsif ( $op eq "export" ) {
330 # export a closed basketgroup in csv
332 my $basketgroupid = $input->param('basketgroupid');
333 my $basketgroup = GetBasketgroup($basketgroupid);
334 print $input->header(
335 -type => 'text/csv',
336 -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.csv'
338 print GetBasketGroupAsCSV( $basketgroupid, $input );
339 exit;
340 }elsif( $op eq "delete"){
342 # delete an closed basketgroup
344 my $basketgroupid = $input->param('basketgroupid');
345 DelBasketgroup($basketgroupid);
346 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&amp;listclosed=1');
347 }elsif ( $op eq 'reopen'){
349 # reopen a closed basketgroup
351 my $basketgroupid = $input->param('basketgroupid');
352 my $booksellerid = $input->param('booksellerid');
353 ReOpenBasketgroup($basketgroupid);
354 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';
355 print $input->redirect($redirectpath);
356 } elsif ( $op eq 'attachbasket') {
358 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
360 # Getting parameters
361 my $basketgroup = {};
362 my @baskets = $input->multi_param('basket');
363 my $basketgroupid = $input->param('basketgroupid');
364 my $basketgroupname = $input->param('basketgroupname');
365 my $booksellerid = $input->param('booksellerid');
366 my $billingplace = $input->param('billingplace');
367 my $deliveryplace = $input->param('deliveryplace');
368 my $freedeliveryplace = $input->param('freedeliveryplace');
369 my $deliverycomment = $input->param('deliverycomment');
370 my $closedbg = $input->param('closedbg') ? 1 : 0;
371 if ($basketgroupid) {
372 # If we have a basketgroupid we edit the basketgroup
373 $basketgroup = {
374 name => $basketgroupname,
375 id => $basketgroupid,
376 basketlist => \@baskets,
377 billingplace => $billingplace,
378 deliveryplace => $deliveryplace,
379 freedeliveryplace => $freedeliveryplace,
380 deliverycomment => $deliverycomment,
381 closed => $closedbg,
383 ModBasketgroup($basketgroup);
384 if($closedbg){
385 # FIXME
387 }else{
388 # we create a new basketgroup (with a closed basket)
389 $basketgroup = {
390 name => $basketgroupname,
391 booksellerid => $booksellerid,
392 basketlist => \@baskets,
393 billingplace => $billingplace,
394 deliveryplace => $deliveryplace,
395 freedeliveryplace => $freedeliveryplace,
396 deliverycomment => $deliverycomment,
397 closed => $closedbg,
399 $basketgroupid = NewBasketgroup($basketgroup);
401 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;
402 $redirectpath .= "&amp;listclosed=1" if $closedbg ;
403 print $input->redirect($redirectpath );
405 } elsif ( $op eq 'ediprint') {
406 my $basketgroupid = $input->param('basketgroupid');
407 if ($template->param( 'ediaccount' )) {
408 generate_edifact_orders( $basketgroupid );
409 exit;
410 } else {
411 $template->param('NoEDIMessage' => 1);
412 my $basketgroups = &GetBasketgroups($booksellerid);
413 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
414 my $baskets = &GetBasketsByBookseller($booksellerid);
416 displaybasketgroups($basketgroups, $bookseller, $baskets);
418 }else{
419 # no param : display the list of all basketgroups for a given vendor
420 my $basketgroups = &GetBasketgroups($booksellerid);
421 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
422 my $baskets = &GetBasketsByBookseller($booksellerid);
424 displaybasketgroups($basketgroups, $bookseller, $baskets);
426 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
427 #prolly won't use all these, maybe just use print, the rest can be done inside validate
428 output_html_with_http_headers $input, $cookie, $template->output;