Bug 12976: Use the centralize VAT and prices calculation - invoice.pl
[koha.git] / acqui / basketgroup.pl
1 #!/usr/bin/perl
2
3 #script to group (closed) baskets into basket groups for easier order management
4 #written by john.soros@biblibre.com 01/10/2008
5
6 # Copyright 2008 - 2009 BibLibre SARL
7 # Parts Copyright Catalyst 2010
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24
25 =head1 NAME
26
27 basketgroup.pl
28
29 =head1 DESCRIPTION
30
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.
33
34 =head1 CGI PARAMETERS
35
36 =over 4
37
38 =item $booksellerid
39
40 The bookseller who we want to display the baskets (and basketgroups) of.
41
42 =back
43
44 =cut
45
46 use strict;
47 use warnings;
48 use Carp;
49
50 use C4::Input;
51 use C4::Auth;
52 use C4::Output;
53 use CGI qw ( -utf8 );
54
55 use C4::Budgets qw/ConvertCurrency/;
56 use C4::Acquisition qw/CloseBasketgroup ReOpenBasketgroup GetOrders GetBasketsByBasketgroup GetBasketsByBookseller ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroups ModBasket GetBasketgroup GetBasket GetBasketGroupAsCSV/;
57 use C4::Branch qw/GetBranches/;
58 use C4::Members qw/GetMember/;
59
60 use Koha::Acquisition::Bookseller;
61
62 our $input=new CGI;
63
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                 });
72
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         $total = $total + ( $order->{ecost} * $order->{quantity} );
80         if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} // C4::Context->preference("gist") )) {
81             my $gst = $bookseller->{gstrate} // C4::Context->preference("gist");
82             $total = $total * ( $gst / 100 +1);
83         }
84     }
85     $total .= " " . ($bookseller->{invoiceprice} // 0);
86     return $total;
87 }
88
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;
106                 }
107                 ++$i;
108             }
109             $basketgroup -> {'basketsqty'} = $basketsqty;
110         }
111         $template->param(basketgroups => $basketgroups);
112     }
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);
119         }
120     }
121     $template->param(baskets => $baskets);
122     $template->param( booksellername => $bookseller ->{'name'});
123 }
124
125 sub printbasketgrouppdf{
126     my ($basketgroupid) = @_;
127     
128     my $pdfformat = C4::Context->preference("OrderPdfFormat");
129     if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages' || $pdfformat eq 'pdfformat::layout3pagesfr'
130         || $pdfformat eq 'pdfformat::layout2pagesde'){
131         eval {
132         eval "require $pdfformat";
133             import $pdfformat;
134         };
135         if ($@){
136         }
137     }
138     else {
139         print $input->header;  
140         print $input->start_html;  # FIXME Should do a nicer page
141         print "<h1>Invalid PDF Format set</h1>";
142         print "Please go to the systempreferences and set a valid pdfformat";
143         exit;
144     }
145     
146     my $basketgroup = GetBasketgroup($basketgroupid);
147     my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basketgroup->{booksellerid} });
148     my $baskets = GetBasketsByBasketgroup($basketgroupid);
149     
150     my %orders;
151     for my $basket (@$baskets) {
152         my @ba_orders;
153         my @ords = &GetOrders($basket->{basketno});
154         for my $ord (@ords) {
155
156             next unless ( $ord->{biblionumber} or $ord->{quantity}> 0 );
157             eval {
158                 require C4::Biblio;
159                 import C4::Biblio;
160             };
161             if ($@){
162                 croak $@;
163             }
164             eval {
165                 require C4::Koha;
166                 import C4::Koha;
167             };
168             if ($@){
169                 croak $@;
170             }
171
172             $ord = C4::Acquisition::populate_order_with_prices({ order => $ord, booksellerid => $bookseller->{id}, ordering => 1 });
173             my $bib = GetBiblioData($ord->{biblionumber});
174             my $itemtypes = GetItemTypes();
175
176             #FIXME DELETE ME
177             # 0      1        2        3         4            5         6       7      8        9
178             #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount gstrate
179
180             # Editor Number
181             my $en;
182             my $edition;
183             my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
184             if ($marcrecord){
185                 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
186                     $en = $marcrecord->subfield( '345', "b" );
187                     $edition = $marcrecord->subfield( '205', 'a' );
188                 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
189                     $en = $marcrecord->subfield( '037', "a" );
190                     $edition = $marcrecord->subfield( '250', 'a' );
191                 }
192             }
193
194             $ord->{itemtype} = ( $ord->{itemtype} and $bib->{itemtype} ) ? $itemtypes->{$bib->{itemtype}}->{description} : undef;
195             $ord->{en} = $en ? $en : undef;
196             $ord->{edition} = $edition ? $edition : undef;
197
198             push(@ba_orders, $ord);
199         }
200         $orders{$basket->{basketno}} = \@ba_orders;
201     }
202     print $input->header(
203         -type       => 'application/pdf',
204         -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
205     );
206     my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} // C4::Context->preference("gist")) || die "pdf generation failed";
207     print $pdf;
208
209 }
210
211 my $op = $input->param('op') || 'display';
212 # possible values of $op :
213 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
214 # - mod_basket : modify an individual basket of the basketgroup
215 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
216 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
217 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
218 # - delete : delete an open basketgroup. called by clicking on "Delete" button in open basketgroups list
219 # - reopen : reopen a closed basketgroup. called by clicking on "Reopen" button in closed basketgroup list
220 # - attachbasket : save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
221 # - display : display the list of all basketgroups for a vendor
222 my $booksellerid = $input->param('booksellerid');
223 $template->param(booksellerid => $booksellerid);
224
225 if ( $op eq "add" ) {
226 #
227 # if no param('basketgroupid') is not defined, adds a new basketgroup
228 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
229 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
230 #
231     my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
232     my $basketgroupid = $input->param('basketgroupid');
233     my $billingplace;
234     my $deliveryplace;
235     my $freedeliveryplace;
236     if ( $basketgroupid ) {
237         # Get the selected baskets in the basketgroup to display them
238         my $selecteds = GetBasketsByBasketgroup($basketgroupid);
239         foreach my $basket(@{$selecteds}){
240             $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
241         }
242         $template->param(basketgroupid => $basketgroupid,
243                          selectedbaskets => $selecteds);
244
245         # Get general informations about the basket group to prefill the form
246         my $basketgroup = GetBasketgroup($basketgroupid);
247         $template->param(
248             name            => $basketgroup->{name},
249             deliverycomment => $basketgroup->{deliverycomment},
250             freedeliveryplace => $basketgroup->{freedeliveryplace},
251         );
252         $billingplace  = $basketgroup->{billingplace};
253         $deliveryplace = $basketgroup->{deliveryplace};
254         $freedeliveryplace = $basketgroup->{freedeliveryplace};
255         $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
256     } else {
257         $template->param( closedbg => 0);
258     }
259     # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
260     my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) );
261     $billingplace  = $billingplace  || $borrower->{'branchcode'};
262     $deliveryplace = $deliveryplace || $borrower->{'branchcode'};
263
264     my $branches = C4::Branch::GetBranchesLoop( $billingplace );
265     $template->param( billingplaceloop => $branches );
266     $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
267     $template->param( deliveryplaceloop => $branches );
268     $template->param( booksellerid => $booksellerid );
269
270     # the template will display a unique basketgroup
271     $template->param(grouping => 1);
272     my $basketgroups = &GetBasketgroups($booksellerid);
273     my $baskets = &GetBasketsByBookseller($booksellerid);
274     displaybasketgroups($basketgroups, $bookseller, $baskets);
275 } elsif ($op eq 'mod_basket') {
276 #
277 # edit an individual basket contained in this basketgroup
278 #
279   my $basketno=$input->param('basketno');
280   my $basketgroupid=$input->param('basketgroupid');
281   ModBasket( { basketno => $basketno,
282                          basketgroupid => $basketgroupid } );
283   print $input->redirect("basket.pl?basketno=" . $basketno);
284 } elsif ( $op eq 'closeandprint') {
285 #
286 # close an open basketgroup and generates a pdf
287 #
288     my $basketgroupid = $input->param('basketgroupid');
289     CloseBasketgroup($basketgroupid);
290     printbasketgrouppdf($basketgroupid);
291     exit;
292 }elsif ($op eq 'print'){
293 #
294 # print a closed basketgroup
295 #
296     my $basketgroupid = $input->param('basketgroupid');
297     printbasketgrouppdf($basketgroupid);
298     exit;
299 }elsif ( $op eq "export" ) {
300 #
301 # export a closed basketgroup in csv
302 #
303     my $basketgroupid = $input->param('basketgroupid');
304     print $input->header(
305         -type       => 'text/csv',
306         -attachment => 'basketgroup' . $basketgroupid . '.csv',
307     );
308     print GetBasketGroupAsCSV( $basketgroupid, $input );
309     exit;
310 }elsif( $op eq "delete"){
311 #
312 # delete an closed basketgroup
313 #
314     my $basketgroupid = $input->param('basketgroupid');
315     DelBasketgroup($basketgroupid);
316     print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid.'&amp;listclosed=1');
317 }elsif ( $op eq 'reopen'){
318 #
319 # reopen a closed basketgroup
320 #
321     my $basketgroupid   = $input->param('basketgroupid');
322     my $booksellerid    = $input->param('booksellerid');
323     ReOpenBasketgroup($basketgroupid);
324     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';
325     print $input->redirect($redirectpath);
326 } elsif ( $op eq 'attachbasket') {
327 #
328 # save a modified basketgroup, or creates a new basketgroup when a basket is closed. called from basket page
329 #
330     # Getting parameters
331     my $basketgroup       = {};
332     my @baskets           = $input->param('basket');
333     my $basketgroupid     = $input->param('basketgroupid');
334     my $basketgroupname   = $input->param('basketgroupname');
335     my $booksellerid      = $input->param('booksellerid');
336     my $billingplace      = $input->param('billingplace');
337     my $deliveryplace     = $input->param('deliveryplace');
338     my $freedeliveryplace = $input->param('freedeliveryplace');
339     my $deliverycomment   = $input->param('deliverycomment');
340     my $closedbg          = $input->param('closedbg') ? 1 : 0;
341     if ($basketgroupid) {
342     # If we have a basketgroupid we edit the basketgroup
343         $basketgroup = {
344               name              => $basketgroupname,
345               id                => $basketgroupid,
346               basketlist        => \@baskets,
347               billingplace      => $billingplace,
348               deliveryplace     => $deliveryplace,
349               freedeliveryplace => $freedeliveryplace,
350               deliverycomment   => $deliverycomment,
351               closed            => $closedbg,
352         };
353         ModBasketgroup($basketgroup);
354         if($closedbg){
355 # FIXME
356         }
357     }else{
358     # we create a new basketgroup (whith a closed basket)
359         $basketgroup = {
360             name              => $basketgroupname,
361             booksellerid      => $booksellerid,
362             basketlist        => \@baskets,
363             billingplace      => $billingplace,
364             deliveryplace     => $deliveryplace,
365             freedeliveryplace => $freedeliveryplace,
366             deliverycomment   => $deliverycomment,
367             closed            => $closedbg,
368         };
369         $basketgroupid = NewBasketgroup($basketgroup);
370     }
371     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;
372     $redirectpath .=  "&amp;listclosed=1" if $closedbg ;
373     print $input->redirect($redirectpath );
374     
375 }else{
376 # no param : display the list of all basketgroups for a given vendor
377     my $basketgroups = &GetBasketgroups($booksellerid);
378     my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
379     my $baskets = &GetBasketsByBookseller($booksellerid);
380
381     displaybasketgroups($basketgroups, $bookseller, $baskets);
382 }
383 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
384 #prolly won't use all these, maybe just use print, the rest can be done inside validate
385 output_html_with_http_headers $input, $cookie, $template->output;