Bug 16460: Update MARC21 frameworks to Update No. 22 (April 2016)
[koha.git] / acqui / basketgroup.pl
blob85422cbcdc683ce6e083138da412cb6dcc5b16e2
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::Branch qw/GetBranches/;
56 use C4::Members qw/GetMember/;
57 use Koha::EDI qw/create_edi_order get_edifact_ean/;
59 use Koha::Acquisition::Bookseller;
61 our $input=new CGI;
63 our ($template, $loggedinuser, $cookie)
64 = get_template_and_user({template_name => "acqui/basketgroup.tt",
65 query => $input,
66 type => "intranet",
67 authnotrequired => 0,
68 flagsrequired => {acquisition => 'group_manage'},
69 debug => 1,
70 });
72 sub BasketTotal {
73 my $basketno = shift;
74 my $bookseller = shift;
75 my $total = 0;
76 my @orders = GetOrders($basketno);
77 for my $order (@orders){
78 $total = $total + ( $order->{ecost} * $order->{quantity} );
79 if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} // C4::Context->preference("gist") )) {
80 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist");
81 $total = $total * ( $gst / 100 +1);
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 = C4::Acquisition::populate_order_with_prices({ order => $ord, booksellerid => $bookseller->{id}, ordering => 1 });
172 my $bib = GetBiblioData($ord->{biblionumber});
173 my $itemtypes = GetItemTypes();
175 #FIXME DELETE ME
176 # 0 1 2 3 4 5 6 7 8 9
177 #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount gstrate
179 # Editor Number
180 my $en;
181 my $edition;
182 my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
183 if ($marcrecord){
184 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
185 $en = $marcrecord->subfield( '345', "b" );
186 $edition = $marcrecord->subfield( '205', 'a' );
187 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
188 $en = $marcrecord->subfield( '037', "a" );
189 $edition = $marcrecord->subfield( '250', 'a' );
193 $ord->{itemtype} = ( $ord->{itemtype} and $bib->{itemtype} ) ? $itemtypes->{$bib->{itemtype}}->{description} : undef;
194 $ord->{en} = $en ? $en : undef;
195 $ord->{edition} = $edition ? $edition : undef;
197 push(@ba_orders, $ord);
199 $orders{$basket->{basketno}} = \@ba_orders;
201 print $input->header(
202 -type => 'application/pdf',
203 -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
205 my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} // C4::Context->preference("gist")) || die "pdf generation failed";
206 print $pdf;
210 sub generate_edifact_orders {
211 my $basketgroupid = shift;
212 my $baskets = GetBasketsByBasketgroup($basketgroupid);
213 my $ean = get_edifact_ean();
215 for my $basket ( @{$baskets} ) {
216 create_edi_order( { ean => $ean, basketno => $basket->{basketno}, } );
218 return;
221 my $op = $input->param('op') || 'display';
222 # possible values of $op :
223 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
224 # - mod_basket : modify an individual basket of the basketgroup
225 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
226 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
227 # - ediprint : generate edi order messages for the baskets in the group
228 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
229 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
230 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
231 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
232 # - display : display the list of all basketgroups for a vendor
233 my $booksellerid = $input->param('booksellerid');
234 $template->param(booksellerid => $booksellerid);
236 if ( $op eq "add" ) {
238 # if no param('basketgroupid') is not defined, adds a new basketgroup
239 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
240 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
242 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
243 my $basketgroupid = $input->param('basketgroupid');
244 my $billingplace;
245 my $deliveryplace;
246 my $freedeliveryplace;
247 if ( $basketgroupid ) {
248 # Get the selected baskets in the basketgroup to display them
249 my $selecteds = GetBasketsByBasketgroup($basketgroupid);
250 foreach my $basket(@{$selecteds}){
251 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
253 $template->param(basketgroupid => $basketgroupid,
254 selectedbaskets => $selecteds);
256 # Get general informations about the basket group to prefill the form
257 my $basketgroup = GetBasketgroup($basketgroupid);
258 $template->param(
259 name => $basketgroup->{name},
260 deliverycomment => $basketgroup->{deliverycomment},
261 freedeliveryplace => $basketgroup->{freedeliveryplace},
263 $billingplace = $basketgroup->{billingplace};
264 $deliveryplace = $basketgroup->{deliveryplace};
265 $freedeliveryplace = $basketgroup->{freedeliveryplace};
266 $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
267 } else {
268 $template->param( closedbg => 0);
270 # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
271 my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) );
272 $billingplace = $billingplace || $borrower->{'branchcode'};
273 $deliveryplace = $deliveryplace || $borrower->{'branchcode'};
275 my $branches = C4::Branch::GetBranchesLoop( $billingplace );
276 $template->param( billingplaceloop => $branches );
277 $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
278 $template->param( deliveryplaceloop => $branches );
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);
302 exit;
303 }elsif ($op eq 'print'){
305 # print a closed basketgroup
307 my $basketgroupid = $input->param('basketgroupid');
308 printbasketgrouppdf($basketgroupid);
309 exit;
310 }elsif ( $op eq "export" ) {
312 # export a closed basketgroup in csv
314 my $basketgroupid = $input->param('basketgroupid');
315 print $input->header(
316 -type => 'text/csv',
317 -attachment => 'basketgroup' . $basketgroupid . '.csv',
319 print GetBasketGroupAsCSV( $basketgroupid, $input );
320 exit;
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.'&amp;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&amp;basketgroupid='.$basketgroupid.'&amp;booksellerid='.$booksellerid : '/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' .$booksellerid.'&amp;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
341 # Getting parameters
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
354 $basketgroup = {
355 name => $basketgroupname,
356 id => $basketgroupid,
357 basketlist => \@baskets,
358 billingplace => $billingplace,
359 deliveryplace => $deliveryplace,
360 freedeliveryplace => $freedeliveryplace,
361 deliverycomment => $deliverycomment,
362 closed => $closedbg,
364 ModBasketgroup($basketgroup);
365 if($closedbg){
366 # FIXME
368 }else{
369 # we create a new basketgroup (with a closed basket)
370 $basketgroup = {
371 name => $basketgroupname,
372 booksellerid => $booksellerid,
373 basketlist => \@baskets,
374 billingplace => $billingplace,
375 deliveryplace => $deliveryplace,
376 freedeliveryplace => $freedeliveryplace,
377 deliverycomment => $deliverycomment,
378 closed => $closedbg,
380 $basketgroupid = NewBasketgroup($basketgroup);
382 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;
383 $redirectpath .= "&amp;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 );
389 exit;
390 }else{
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;