Bug 11081: (followup) Add license information
[koha.git] / acqui / basketgroup.pl
blobb2ed7db5543d5f6c8c876ae0f7273650bc5851bb
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::Budgets qw/ConvertCurrency/;
55 use C4::Acquisition qw/CloseBasketgroup ReOpenBasketgroup GetOrders GetBasketsByBasketgroup GetBasketsByBookseller ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroups ModBasket GetBasketgroup GetBasket GetBasketGroupAsCSV/;
56 use C4::Branch qw/GetBranches/;
57 use C4::Members qw/GetMember/;
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 my $op = $input->param('op') || 'display';
211 # possible values of $op :
212 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
213 # - mod_basket : modify an individual basket of the basketgroup
214 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
215 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
216 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
217 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
218 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
219 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
220 # - display : display the list of all basketgroups for a vendor
221 my $booksellerid = $input->param('booksellerid');
222 $template->param(booksellerid => $booksellerid);
224 if ( $op eq "add" ) {
226 # if no param('basketgroupid') is not defined, adds a new basketgroup
227 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
228 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
230 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
231 my $basketgroupid = $input->param('basketgroupid');
232 my $billingplace;
233 my $deliveryplace;
234 my $freedeliveryplace;
235 if ( $basketgroupid ) {
236 # Get the selected baskets in the basketgroup to display them
237 my $selecteds = GetBasketsByBasketgroup($basketgroupid);
238 foreach my $basket(@{$selecteds}){
239 $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
241 $template->param(basketgroupid => $basketgroupid,
242 selectedbaskets => $selecteds);
244 # Get general informations about the basket group to prefill the form
245 my $basketgroup = GetBasketgroup($basketgroupid);
246 $template->param(
247 name => $basketgroup->{name},
248 deliverycomment => $basketgroup->{deliverycomment},
249 freedeliveryplace => $basketgroup->{freedeliveryplace},
251 $billingplace = $basketgroup->{billingplace};
252 $deliveryplace = $basketgroup->{deliveryplace};
253 $freedeliveryplace = $basketgroup->{freedeliveryplace};
254 $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
255 } else {
256 $template->param( closedbg => 0);
258 # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
259 my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) );
260 $billingplace = $billingplace || $borrower->{'branchcode'};
261 $deliveryplace = $deliveryplace || $borrower->{'branchcode'};
263 my $branches = C4::Branch::GetBranchesLoop( $billingplace );
264 $template->param( billingplaceloop => $branches );
265 $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
266 $template->param( deliveryplaceloop => $branches );
267 $template->param( booksellerid => $booksellerid );
269 # the template will display a unique basketgroup
270 $template->param(grouping => 1);
271 my $basketgroups = &GetBasketgroups($booksellerid);
272 my $baskets = &GetBasketsByBookseller($booksellerid);
273 displaybasketgroups($basketgroups, $bookseller, $baskets);
274 } elsif ($op eq 'mod_basket') {
276 # edit an individual basket contained in this basketgroup
278 my $basketno=$input->param('basketno');
279 my $basketgroupid=$input->param('basketgroupid');
280 ModBasket( { basketno => $basketno,
281 basketgroupid => $basketgroupid } );
282 print $input->redirect("basket.pl?basketno=" . $basketno);
283 } elsif ( $op eq 'closeandprint') {
285 # close an open basketgroup and generates a pdf
287 my $basketgroupid = $input->param('basketgroupid');
288 CloseBasketgroup($basketgroupid);
289 printbasketgrouppdf($basketgroupid);
290 exit;
291 }elsif ($op eq 'print'){
293 # print a closed basketgroup
295 my $basketgroupid = $input->param('basketgroupid');
296 printbasketgrouppdf($basketgroupid);
297 exit;
298 }elsif ( $op eq "export" ) {
300 # export a closed basketgroup in csv
302 my $basketgroupid = $input->param('basketgroupid');
303 print $input->header(
304 -type => 'text/csv',
305 -attachment => 'basketgroup' . $basketgroupid . '.csv',
307 print GetBasketGroupAsCSV( $basketgroupid, $input );
308 exit;
309 }elsif( $op eq "delete"){
311 # delete an closed basketgroup
313 my $basketgroupid = $input->param('basketgroupid');
314 DelBasketgroup($basketgroupid);
315 print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&amp;listclosed=1');
316 }elsif ( $op eq 'reopen'){
318 # reopen a closed basketgroup
320 my $basketgroupid = $input->param('basketgroupid');
321 my $booksellerid = $input->param('booksellerid');
322 ReOpenBasketgroup($basketgroupid);
323 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';
324 print $input->redirect($redirectpath);
325 } elsif ( $op eq 'attachbasket') {
327 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
329 # Getting parameters
330 my $basketgroup = {};
331 my @baskets = $input->param('basket');
332 my $basketgroupid = $input->param('basketgroupid');
333 my $basketgroupname = $input->param('basketgroupname');
334 my $booksellerid = $input->param('booksellerid');
335 my $billingplace = $input->param('billingplace');
336 my $deliveryplace = $input->param('deliveryplace');
337 my $freedeliveryplace = $input->param('freedeliveryplace');
338 my $deliverycomment = $input->param('deliverycomment');
339 my $closedbg = $input->param('closedbg') ? 1 : 0;
340 if ($basketgroupid) {
341 # If we have a basketgroupid we edit the basketgroup
342 $basketgroup = {
343 name => $basketgroupname,
344 id => $basketgroupid,
345 basketlist => \@baskets,
346 billingplace => $billingplace,
347 deliveryplace => $deliveryplace,
348 freedeliveryplace => $freedeliveryplace,
349 deliverycomment => $deliverycomment,
350 closed => $closedbg,
352 ModBasketgroup($basketgroup);
353 if($closedbg){
354 # FIXME
356 }else{
357 # we create a new basketgroup (with a closed basket)
358 $basketgroup = {
359 name => $basketgroupname,
360 booksellerid => $booksellerid,
361 basketlist => \@baskets,
362 billingplace => $billingplace,
363 deliveryplace => $deliveryplace,
364 freedeliveryplace => $freedeliveryplace,
365 deliverycomment => $deliverycomment,
366 closed => $closedbg,
368 $basketgroupid = NewBasketgroup($basketgroup);
370 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;
371 $redirectpath .= "&amp;listclosed=1" if $closedbg ;
372 print $input->redirect($redirectpath );
374 }else{
375 # no param : display the list of all basketgroups for a given vendor
376 my $basketgroups = &GetBasketgroups($booksellerid);
377 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
378 my $baskets = &GetBasketsByBookseller($booksellerid);
380 displaybasketgroups($basketgroups, $bookseller, $baskets);
382 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
383 #prolly won't use all these, maybe just use print, the rest can be done inside validate
384 output_html_with_http_headers $input, $cookie, $template->output;