Merge branch 'master' of git://git.koha-community.org/koha into MSWin32
[wip/koha-chris_n.git] / acqui / basket.pl
1 #!/usr/bin/perl
2
3 #script to show display basket of orders
4
5 # Copyright 2000 - 2004 Katipo
6 # Copyright 2008 - 2009 BibLibre SARL
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 use warnings;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Output;
28 use CGI;
29 use C4::Acquisition;
30 use C4::Budgets;
31
32 use C4::Bookseller qw( GetBookSellerFromId);
33 use C4::Dates qw/format_date/;
34 use C4::Debug;
35
36 use C4::Members qw/GetMember/;  #needed for permissions checking for changing basketgroup of a basket
37 =head1 NAME
38
39 basket.pl
40
41 =head1 DESCRIPTION
42
43  This script display all informations about basket for the supplier given
44  on input arg.  Moreover, it allows us to add a new order for this supplier from
45  an existing record, a suggestion or a new record.
46
47 =head1 CGI PARAMETERS
48
49 =over 4
50
51 =item $basketno
52
53 The basket number.
54
55 =item supplierid
56
57 the supplier this script have to display the basket.
58
59 =item order
60
61 =back
62
63 =cut
64
65 my $query        = new CGI;
66 my $basketno     = $query->param('basketno');
67 my $booksellerid = $query->param('supplierid');
68
69 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
70     {
71         template_name   => "acqui/basket.tmpl",
72         query           => $query,
73         type            => "intranet",
74         authnotrequired => 0,
75         flagsrequired   => { acquisition => 'order_manage' },
76         debug           => 1,
77     }
78 );
79
80 my $basket = GetBasket($basketno);
81
82 # FIXME : what about the "discount" percentage?
83 # FIXME : the query->param('supplierid') below is probably useless. The bookseller is always known from the basket
84 # if no booksellerid in parameter, get it from basket
85 # warn "=>".$basket->{booksellerid};
86 $booksellerid = $basket->{booksellerid} unless $booksellerid;
87 my ($bookseller) = GetBookSellerFromId($booksellerid);
88 my $op = $query->param('op');
89 if (!defined $op) {
90     $op = q{};
91 }
92
93 my $confirm_pref= C4::Context->preference("BasketConfirmations") || '1';
94 $template->param( skip_confirm_reopen => 1) if $confirm_pref eq '2';
95
96 if ( $op eq 'delete_confirm' ) {
97     my $basketno = $query->param('basketno');
98     DelBasket($basketno);
99     $template->param( delete_confirmed => 1 );
100 } elsif ( !$bookseller ) {
101     $template->param( NO_BOOKSELLER => 1 );
102 } elsif ( $op eq 'del_basket') {
103     $template->param( delete_confirm => 1 );
104     if ( C4::Context->preference("IndependantBranches") ) {
105         my $userenv = C4::Context->userenv;
106         unless ( $userenv->{flags} == 1 ) {
107             my $validtest = ( $basket->{creationdate} eq '' )
108               || ( $userenv->{branch} eq $basket->{branch} )
109               || ( $userenv->{branch} eq '' )
110               || ( $basket->{branch}  eq '' );
111             unless ($validtest) {
112                 print $query->redirect("../mainpage.pl");
113                 exit 1;
114             }
115         }
116     }
117     $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
118     $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
119     my $contract = &GetContract($basket->{contractnumber});
120     $template->param(
121         basketno             => $basketno,
122         basketname           => $basket->{'basketname'},
123         basketnote           => $basket->{note},
124         basketbooksellernote => $basket->{booksellernote},
125         basketcontractno     => $basket->{contractnumber},
126         basketcontractname   => $contract->{contractname},
127         creationdate         => format_date( $basket->{creationdate} ),
128         authorisedby         => $basket->{authorisedby},
129         authorisedbyname     => $basket->{authorisedbyname},
130         closedate            => format_date( $basket->{closedate} ),
131         active               => $bookseller->{'active'},
132         booksellerid         => $bookseller->{'id'},
133         name                 => $bookseller->{'name'},
134         address1             => $bookseller->{'address1'},
135         address2             => $bookseller->{'address2'},
136         address3             => $bookseller->{'address3'},
137         address4             => $bookseller->{'address4'},
138       );
139 } elsif ($op eq 'attachbasket' && $template->{'VARS'}->{'CAN_user_acquisition_group_manage'} == 1) {
140       print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid);
141     # check if we have to "close" a basket before building page
142 } elsif ($op eq 'export') {
143     print $query->header(
144         -type       => 'text/csv',
145         -attachment => 'basket' . $basket->{'basketno'} . '.csv',
146     );
147     print GetBasketAsCSV($query->param('basketno'));
148     exit;
149 } elsif ($op eq 'close') {
150     my $confirm = $query->param('confirm') || $confirm_pref eq '2';
151     if ($confirm) {
152         my $basketno = $query->param('basketno');
153         my $booksellerid = $query->param('booksellerid');
154         $basketno =~ /^\d+$/ and CloseBasket($basketno);
155         # if requested, create basket group, close it and attach the basket
156         if ($query->param('createbasketgroup')) {
157             my $basketgroupid = NewBasketgroup( { name => $basket->{basketname},
158                             booksellerid => $booksellerid,
159                             closed => 1,
160                             });
161             ModBasket( { basketno => $basketno,
162                          basketgroupid => $basketgroupid } );
163             print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1');
164         } else {
165             print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?supplierid=' . $booksellerid);
166         }
167         exit;
168     } else {
169     $template->param(confirm_close => "1",
170             booksellerid    => $booksellerid,
171             basketno        => $basket->{'basketno'},
172                 basketname      => $basket->{'basketname'},
173             basketgroupname => $basket->{'basketname'});
174         
175     }
176 } elsif ($op eq 'reopen') {
177     my $basket;
178     $basket->{basketno} = $query->param('basketno');
179     $basket->{closedate} = undef;
180     ModBasket($basket);
181     print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
182 } else {
183     # get librarian branch...
184     if ( C4::Context->preference("IndependantBranches") ) {
185         my $userenv = C4::Context->userenv;
186         unless ( $userenv->{flags} == 1 ) {
187             my $validtest = ( $basket->{creationdate} eq '' )
188               || ( $userenv->{branch} eq $basket->{branch} )
189               || ( $userenv->{branch} eq '' )
190               || ( $basket->{branch}  eq '' );
191             unless ($validtest) {
192                 print $query->redirect("../mainpage.pl");
193                 exit 1;
194             }
195         }
196     }
197 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
198     my $basketgroups;
199     my $member = GetMember(borrowernumber => $loggedinuser);
200     if ($basket->{closedate} && haspermission({ acquisition => 'group_manage'} )) {
201         $basketgroups = GetBasketgroups($basket->{booksellerid});
202         for my $bg ( @{$basketgroups} ) {
203             if ($basket->{basketgroupid} && $basket->{basketgroupid} == $bg->{id}){
204                 $bg->{default} = 1;
205             }
206         }
207         my %emptygroup = ( id   =>   undef,
208                            name =>   "No group");
209         if ( ! $basket->{basketgroupid} ) {
210             $emptygroup{default} = 1;
211             $emptygroup{nogroup} = 1;
212         }
213         unshift( @$basketgroups, \%emptygroup );
214     }
215     # if new basket, pre-fill infos
216     $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
217     $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
218     $debug
219       and warn sprintf
220       "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
221       $basket->{creationdate}, $basket->{authorisedby};
222
223         #to get active currency
224         my $cur = GetCurrency();
225
226
227     my @results = GetOrders( $basketno );
228     
229         my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
230         $gist = 0 if $gist == 0.0000;
231         my $discount = $bookseller->{'discount'} / 100;
232     my $total_rrp;      # RRP Total, its value will be assigned to $total_rrp_gsti or $total_rrp_gste depending of $bookseller->{'listincgst'}
233         my $total_rrp_gsti; # RRP Total, GST included
234         my $total_rrp_gste; # RRP Total, GST excluded
235         my $gist_rrp;
236         my $total_rrp_est;
237         
238     my $qty_total;
239     my @books_loop;
240
241     for my $order ( @results ) {
242         my $rrp = $order->{'listprice'} || 0;
243                 my $qty = $order->{'quantity'} || 0;
244         if (!defined $order->{quantityreceived}) {
245             $order->{quantityreceived} = 0;
246         }
247         for ( qw(rrp ecost quantityreceived)) {
248             if (!defined $order->{$_}) {
249                 $order->{$_} = 0;
250             }
251         }
252
253         my $budget = GetBudget(  $order->{'budget_id'} );
254         $rrp = ConvertCurrency( $order->{'currency'}, $rrp );
255
256         $total_rrp += $qty * $order->{'rrp'};
257         my $line_total = $qty * $order->{'ecost'};
258         $total_rrp_est += $qty * $order->{'ecost'};
259                 # FIXME: what about the "actual cost" field?
260         $qty_total += $qty;
261         my %line = %{ $order };
262
263         $line{order_received} = ( $qty == $order->{'quantityreceived'} );
264         $line{basketno}       = $basketno;
265         $line{budget_name}    = $budget->{budget_name};
266         $line{rrp}            = sprintf( "%.2f", $line{'rrp'} );
267         $line{ecost}          = sprintf( "%.2f", $line{'ecost'} );
268         $line{line_total}     = sprintf( "%.2f", $line_total );
269         if ($line{uncertainprice}) {
270             $template->param( uncertainprices => 1 );
271             $line{rrp} .= ' (Uncertain)';
272         }
273         if ($line{'title'}){
274             my $volume = $order->{'volume'};
275             my $seriestitle = $order->{'seriestitle'};
276             $line{'title'} .= " / $seriestitle" if $seriestitle;
277             $line{'title'} .= " / $volume" if $volume;
278         } else {
279             $line{'title'} = "Deleted bibliographic notice, can't find title.";
280         }
281         push @books_loop, \%line;
282     }
283
284 my $total_est_gste;
285     my $total_est_gsti;
286     my $gist_est;
287     if ($gist){                                                    # if we have GST
288        if ( $bookseller->{'listincgst'} ) {                        # if prices already includes GST
289            $total_rrp_gsti = $total_rrp;                           # we know $total_rrp_gsti
290            $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 );      # and can reverse compute other values
291            $gist_rrp       = $total_rrp_gsti - $total_rrp_gste;    #
292            $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
293            $total_est_gsti = $total_rrp_est;
294         } else {                                                    # if prices does not include GST
295            $total_rrp_gste = $total_rrp;                           # then we use the common way to compute other values
296            $gist_rrp       = $total_rrp_gste * $gist;              #
297            $total_rrp_gsti = $total_rrp_gste + $gist_rrp;          #
298            $total_est_gste = $total_rrp_est;
299            $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
300        }
301        $gist_est = $gist_rrp - ( $gist_rrp * $discount );
302     } else {
303     $total_rrp_gsti = $total_rrp;
304     $total_est_gsti = $total_rrp_est;
305 }
306
307     my $contract = &GetContract($basket->{contractnumber});
308     my @orders = GetOrders($basketno);
309
310     my $borrower= GetMember('borrowernumber' => $loggedinuser);
311     my $budgets = GetBudgetHierarchy(q{},$borrower->{branchcode},$borrower->{borrowernumber});
312     my $has_budgets = 0;
313     foreach my $r (@{$budgets}) {
314         if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
315             next;
316         }
317         $has_budgets = 1;
318         last;
319     }
320
321     $template->param(
322         basketno             => $basketno,
323         basketname           => $basket->{'basketname'},
324         basketnote           => $basket->{note},
325         basketbooksellernote => $basket->{booksellernote},
326         basketcontractno     => $basket->{contractnumber},
327         basketcontractname   => $contract->{contractname},
328         creationdate         => C4::Dates->new($basket->{creationdate},'iso')->output,
329         authorisedby         => $basket->{authorisedby},
330         authorisedbyname     => $basket->{authorisedbyname},
331         closedate            => C4::Dates->new($basket->{closedate},'iso')->output,
332         active               => $bookseller->{'active'},
333         booksellerid         => $bookseller->{'id'},
334         name                 => $bookseller->{'name'},
335         entrydate            => C4::Dates->new($results[0]->{'entrydate'},'iso')->output,
336         books_loop           => \@books_loop,
337         gist_rate            => sprintf( "%.2f", $gist * 100 ) . '%',
338         total_rrp_gste       => sprintf( "%.2f", $total_rrp_gste ),
339         total_est_gste       => sprintf( "%.2f", $total_est_gste ),
340         gist_est             => sprintf( "%.2f", $gist_est ),
341         gist_rrp             => sprintf( "%.2f", $gist_rrp ),        
342         total_rrp_gsti       => sprintf( "%.2f", $total_rrp_gsti ),
343         total_est_gsti       => sprintf( "%.2f", $total_est_gsti ),
344 #        currency             => $bookseller->{'listprice'},
345         currency                => $cur->{'currency'},
346         qty_total            => $qty_total,
347         GST                  => $gist,
348         basketgroups         => $basketgroups,
349         grouped              => $basket->{basketgroupid},
350         unclosable           => @orders ? 0 : 1, 
351         has_budgets          => $has_budgets,
352     );
353 }
354
355 output_html_with_http_headers $query, $cookie, $template->output;