Bug 15722: Escape patron infos for JSON in patron searches
[koha.git] / acqui / basketgroup.pl
blobdacc74eee9396edaa90786ff919b0d3f306fdbcf
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/;
58 use Koha::Acquisition::Bookseller;
60 our $input=new CGI;
62 our ($template, $loggedinuser, $cookie)
63 = get_template_and_user({template_name => "acqui/basketgroup.tt",
64 query => $input,
65 type => "intranet",
66 authnotrequired => 0,
67 flagsrequired => {acquisition => 'group_manage'},
68 debug => 1,
69 });
71 sub BasketTotal {
72 my $basketno = shift;
73 my $bookseller = shift;
74 my $total = 0;
75 my @orders = GetOrders($basketno);
76 for my $order (@orders){
77 $total = $total + ( $order->{ecost} * $order->{quantity} );
78 if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} // C4::Context->preference("gist") )) {
79 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist");
80 $total = $total * ( $gst / 100 +1);
83 $total .= " " . ($bookseller->{invoiceprice} // 0);
84 return $total;
87 #displays all basketgroups and all closed baskets (in their respective groups)
88 sub displaybasketgroups {
89 my $basketgroups = shift;
90 my $bookseller = shift;
91 my $baskets = shift;
92 if (scalar @$basketgroups != 0) {
93 foreach my $basketgroup (@$basketgroups){
94 my $i = 0;
95 my $basketsqty = 0;
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);
102 ++$basketsqty;
103 --$i;
105 ++$i;
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);
114 --$i;
115 }else{
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 if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages' || $pdfformat eq 'pdfformat::layout3pagesfr'
128 || $pdfformat eq 'pdfformat::layout2pagesde'){
129 eval {
130 eval "require $pdfformat";
131 import $pdfformat;
133 if ($@){
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::Bookseller->fetch({ id => $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 = C4::Acquisition::populate_order_with_prices({ order => $ord, booksellerid => $bookseller->{id}, ordering => 1 });
171 my $bib = GetBiblioData($ord->{biblionumber});
172 my $itemtypes = GetItemTypes();
174 #FIXME DELETE ME
175 # 0 1 2 3 4 5 6 7 8 9
176 #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount gstrate
178 # Editor Number
179 my $en;
180 my $edition;
181 my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
182 if ($marcrecord){
183 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
184 $en = $marcrecord->subfield( '345', "b" );
185 $edition = $marcrecord->subfield( '205', 'a' );
186 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
187 $en = $marcrecord->subfield( '037', "a" );
188 $edition = $marcrecord->subfield( '250', 'a' );
192 $ord->{itemtype} = ( $ord->{itemtype} and $bib->{itemtype} ) ? $itemtypes->{$bib->{itemtype}}->{description} : undef;
193 $ord->{en} = $en ? $en : undef;
194 $ord->{edition} = $edition ? $edition : undef;
196 push(@ba_orders, $ord);
198 $orders{$basket->{basketno}} = \@ba_orders;
200 print $input->header(
201 -type => 'application/pdf',
202 -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
204 my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} // C4::Context->preference("gist")) || die "pdf generation failed";
205 print $pdf;
209 my $op = $input->param('op') || 'display';
210 # possible values of $op :
211 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
212 # - mod_basket : modify an individual basket of the basketgroup
213 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
214 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
215 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
216 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
217 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
218 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
219 # - display : display the list of all basketgroups for a vendor
220 my $booksellerid = $input->param('booksellerid');
221 $template->param(booksellerid => $booksellerid);
223 if ( $op eq "add" ) {
225 # if no param('basketgroupid') is not defined, adds a new basketgroup
226 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
227 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
229 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
230 my $basketgroupid = $input->param('basketgroupid');
231 my $billingplace;
232 my $deliveryplace;
233 my $freedeliveryplace;
234 if ( $basketgroupid ) {
235 # Get the selected baskets in the basketgroup to display them
236 my $selecteds = GetBasketsByBasketgroup($basketgroupid);
237 foreach my $basket(@{$selecteds}){
238 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
240 $template->param(basketgroupid => $basketgroupid,
241 selectedbaskets => $selecteds);
243 # Get general informations about the basket group to prefill the form
244 my $basketgroup = GetBasketgroup($basketgroupid);
245 $template->param(
246 name => $basketgroup->{name},
247 deliverycomment => $basketgroup->{deliverycomment},
248 freedeliveryplace => $basketgroup->{freedeliveryplace},
250 $billingplace = $basketgroup->{billingplace};
251 $deliveryplace = $basketgroup->{deliveryplace};
252 $freedeliveryplace = $basketgroup->{freedeliveryplace};
253 $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
254 } else {
255 $template->param( closedbg => 0);
257 # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
258 my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) );
259 $billingplace = $billingplace || $borrower->{'branchcode'};
260 $deliveryplace = $deliveryplace || $borrower->{'branchcode'};
262 my $branches = C4::Branch::GetBranchesLoop( $billingplace );
263 $template->param( billingplaceloop => $branches );
264 $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
265 $template->param( deliveryplaceloop => $branches );
266 $template->param( booksellerid => $booksellerid );
268 # the template will display a unique basketgroup
269 $template->param(grouping => 1);
270 my $basketgroups = &GetBasketgroups($booksellerid);
271 my $baskets = &GetBasketsByBookseller($booksellerid);
272 displaybasketgroups($basketgroups, $bookseller, $baskets);
273 } elsif ($op eq 'mod_basket') {
275 # edit an individual basket contained in this basketgroup
277 my $basketno=$input->param('basketno');
278 my $basketgroupid=$input->param('basketgroupid');
279 ModBasket( { basketno => $basketno,
280 basketgroupid => $basketgroupid } );
281 print $input->redirect("basket.pl?basketno=" . $basketno);
282 } elsif ( $op eq 'closeandprint') {
284 # close an open basketgroup and generates a pdf
286 my $basketgroupid = $input->param('basketgroupid');
287 CloseBasketgroup($basketgroupid);
288 printbasketgrouppdf($basketgroupid);
289 exit;
290 }elsif ($op eq 'print'){
292 # print a closed basketgroup
294 my $basketgroupid = $input->param('basketgroupid');
295 printbasketgrouppdf($basketgroupid);
296 exit;
297 }elsif ( $op eq "export" ) {
299 # export a closed basketgroup in csv
301 my $basketgroupid = $input->param('basketgroupid');
302 print $input->header(
303 -type => 'text/csv',
304 -attachment => 'basketgroup' . $basketgroupid . '.csv',
306 print GetBasketGroupAsCSV( $basketgroupid, $input );
307 exit;
308 }elsif( $op eq "delete"){
310 # delete an closed basketgroup
312 my $basketgroupid = $input->param('basketgroupid');
313 DelBasketgroup($basketgroupid);
314 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&amp;listclosed=1');
315 }elsif ( $op eq 'reopen'){
317 # reopen a closed basketgroup
319 my $basketgroupid = $input->param('basketgroupid');
320 my $booksellerid = $input->param('booksellerid');
321 ReOpenBasketgroup($basketgroupid);
322 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';
323 print $input->redirect($redirectpath);
324 } elsif ( $op eq 'attachbasket') {
326 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
328 # Getting parameters
329 my $basketgroup = {};
330 my @baskets = $input->param('basket');
331 my $basketgroupid = $input->param('basketgroupid');
332 my $basketgroupname = $input->param('basketgroupname');
333 my $booksellerid = $input->param('booksellerid');
334 my $billingplace = $input->param('billingplace');
335 my $deliveryplace = $input->param('deliveryplace');
336 my $freedeliveryplace = $input->param('freedeliveryplace');
337 my $deliverycomment = $input->param('deliverycomment');
338 my $closedbg = $input->param('closedbg') ? 1 : 0;
339 if ($basketgroupid) {
340 # If we have a basketgroupid we edit the basketgroup
341 $basketgroup = {
342 name => $basketgroupname,
343 id => $basketgroupid,
344 basketlist => \@baskets,
345 billingplace => $billingplace,
346 deliveryplace => $deliveryplace,
347 freedeliveryplace => $freedeliveryplace,
348 deliverycomment => $deliverycomment,
349 closed => $closedbg,
351 ModBasketgroup($basketgroup);
352 if($closedbg){
353 # FIXME
355 }else{
356 # we create a new basketgroup (with a closed basket)
357 $basketgroup = {
358 name => $basketgroupname,
359 booksellerid => $booksellerid,
360 basketlist => \@baskets,
361 billingplace => $billingplace,
362 deliveryplace => $deliveryplace,
363 freedeliveryplace => $freedeliveryplace,
364 deliverycomment => $deliverycomment,
365 closed => $closedbg,
367 $basketgroupid = NewBasketgroup($basketgroup);
369 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;
370 $redirectpath .= "&amp;listclosed=1" if $closedbg ;
371 print $input->redirect($redirectpath );
373 }else{
374 # no param : display the list of all basketgroups for a given vendor
375 my $basketgroups = &GetBasketgroups($booksellerid);
376 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
377 my $baskets = &GetBasketsByBookseller($booksellerid);
379 displaybasketgroups($basketgroups, $bookseller, $baskets);
381 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
382 #prolly won't use all these, maybe just use print, the rest can be done inside validate
383 output_html_with_http_headers $input, $cookie, $template->output;