basket management
[koha.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 with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 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;
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 my $sort         = $query->param('order') || "aqorders.ordernumber";
69
70 my @sort_loop;
71 if (defined $sort) {
72         foreach (split /\,/, $sort) {
73                 my %sorthash = (
74                         string => $_,
75                 );
76                 # other possibly valid tables for later: aqbookfund biblio biblioitems
77                 if (
78                         (/^\s*(biblioitems)\.(\w+)\s*$/      and $2 eq 'publishercode') or
79                         (/^\s*(aqorders)\.(\w+)\s*$/ and $2 eq 'ordernumber' )
80                 ) {
81                         $sorthash{table} = $1;
82                         $sorthash{field} = $2;
83                 } else {
84                         $sorthash{error} = 1;
85                 }
86                 push @sort_loop, \%sorthash;
87         }
88 }
89
90 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
91     {
92         template_name   => "acqui/basket.tmpl",
93         query           => $query,
94         type            => "intranet",
95         authnotrequired => 0,
96         flagsrequired   => { acquisition => 'order_manage' },
97         debug           => 1,
98     }
99 );
100
101 my $basket = GetBasket($basketno);
102
103 # FIXME : what about the "discount" percentage?
104 # FIXME : the query->param('supplierid') below is probably useless. The bookseller is always known from the basket
105 # if no booksellerid in parameter, get it from basket
106 # warn "=>".$basket->{booksellerid};
107 $booksellerid = $basket->{booksellerid} unless $booksellerid;
108 my ($bookseller) = GetBookSellerFromId($booksellerid);
109 my $op = $query->param('op');
110
111 if ( $op eq 'delete_confirm' ) {
112     my $basketno = $query->param('basketno');
113     DelBasket($basketno);
114     $template->param( delete_confirmed => 1 );
115 } elsif ( !$bookseller ) {
116     $template->param( NO_BOOKSELLER => 1 );
117 } elsif ( $op eq 'del_basket') {
118     $template->param( delete_confirm => 1 );
119     if ( C4::Context->preference("IndependantBranches") ) {
120         my $userenv = C4::Context->userenv;
121         unless ( $userenv->{flags} == 1 ) {
122             my $validtest = ( $basket->{creationdate} eq '' )
123               || ( $userenv->{branch} eq $basket->{branch} )
124               || ( $userenv->{branch} eq '' )
125               || ( $basket->{branch}  eq '' );
126             unless ($validtest) {
127                 print $query->redirect("../mainpage.pl");
128                 exit 1;
129             }
130         }
131     }
132     $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
133     $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
134     my $contract = &GetContract($basket->{contractnumber});
135     my $count = scalar GetOrders( $basketno);
136     $template->param(
137         basketno             => $basketno,
138         basketname           => $basket->{'basketname'},
139         basketnote           => $basket->{note},
140         basketbooksellernote => $basket->{booksellernote},
141         basketcontractno     => $basket->{contractnumber},
142         basketcontractname   => $contract->{contractname},
143         creationdate         => format_date( $basket->{creationdate} ),
144         authorisedby         => $basket->{authorisedby},
145         authorisedbyname     => $basket->{authorisedbyname},
146         closedate            => format_date( $basket->{closedate} ),
147         active               => $bookseller->{'active'},
148         booksellerid         => $bookseller->{'id'},
149         name                 => $bookseller->{'name'},
150         address1             => $bookseller->{'address1'},
151         address2             => $bookseller->{'address2'},
152         address3             => $bookseller->{'address3'},
153         address4             => $bookseller->{'address4'},
154         count               =>     $count,
155       );
156 } else {
157     # get librarian branch...
158     if ( C4::Context->preference("IndependantBranches") ) {
159         my $userenv = C4::Context->userenv;
160         unless ( $userenv->{flags} == 1 ) {
161             my $validtest = ( $basket->{creationdate} eq '' )
162               || ( $userenv->{branch} eq $basket->{branch} )
163               || ( $userenv->{branch} eq '' )
164               || ( $basket->{branch}  eq '' );
165             unless ($validtest) {
166                 print $query->redirect("../mainpage.pl");
167                 exit 1;
168             }
169         }
170     }
171 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
172     my $basketgroups;
173     my $member = GetMember($loggedinuser, "borrowernumber");
174     if ($basket->{closedate} && haspermission(C4::Context->dbh, $member->{userid}, { flagsrequired   => { acquisition => 'group_manage'} })) {
175         $basketgroups = GetBasketgroups($basket->{booksellerid});
176         for (my $i=0; $i < scalar(@$basketgroups); $i++) {
177             if (@$basketgroups[$i]->{closed}) {
178                 splice(@$basketgroups, $i, 1);
179                 $i--;
180             } elsif ($basket->{basketgroupid} == @$basketgroups[$i]->{id}){
181                 @$basketgroups[$i]->{default} = 1;
182             }
183         }
184         my %emptygroup = ( id   =>   undef,
185                            name =>   "No group");
186         if ( ! $basket->{basketgroupid} ) {
187             $emptygroup{default} = 1;
188         }
189         unshift( @$basketgroups, \%emptygroup );
190     }
191     # if new basket, pre-fill infos
192     $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
193     $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
194     $debug
195       and warn sprintf
196       "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
197       $basket->{creationdate}, $basket->{authorisedby};
198
199     my @results = GetOrders( $basketno, $sort );
200     my $count = scalar @results;
201
202     my $sub_total;      # total of line totals
203     my $grand_total;    # $subttotal + $gist
204
205     # my $line_total_est; # total of each line
206     my $sub_total_est;      # total of line totals
207     my $sub_total_rrp;      # total of line totals
208     my $grand_total_est;    # $subttotal + $gist
209
210     my $qty_total;
211     my @books_loop;
212
213     for ( my $i = 0 ; $i < $count ; $i++ ) {
214         my $rrp = $results[$i]->{'listprice'};
215                 my $qty = $results[$i]->{'quantity'};
216
217         my $budget = GetBudget(  $results[$i]->{'budget_id'} );
218         $rrp = ConvertCurrency( $results[$i]->{'currency'}, $rrp );
219
220         $sub_total_rrp += $qty * $results[$i]->{'rrp'};
221         my $line_total = $qty * $results[$i]->{'ecost'};
222                 # FIXME: what about the "actual cost" field?
223         $sub_total += $line_total;
224         $qty_total += $qty;
225         my %line = %{ $results[$i] };
226                 ($i%2) and $line{toggle} = 1;
227
228         $line{order_received} = ( $qty eq $results[$i]->{'quantityreceived'} );
229         $line{basketno}       = $basketno;
230         $line{i}              = $i;
231         $line{budget_name}    = $budget->{budget_name};
232         $line{rrp}            = sprintf( "%.2f", $line{'rrp'} );
233         $line{ecost}          = sprintf( "%.2f", $line{'ecost'} );
234         $line{line_total}     = sprintf( "%.2f", $line_total );
235         $line{odd}            = $i % 2;
236         if ($line{uncertainprice}) {
237             $template->param( unclosable => 1 );
238             for my $key (qw/ecost line_total rrp/) {
239                 $line{$key} .= '??';
240             }
241         }
242         push @books_loop, \%line;
243     }
244
245
246
247
248     my $prefgist = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
249     my $gist     = $sub_total     * $prefgist;
250     my $gist_rrp = $sub_total_rrp * $prefgist;
251     $grand_total     = $sub_total_est = $sub_total;
252     $grand_total_est = $sub_total_est;          # FIXME: Too many things that are ALL the SAME
253         my $temp;
254     if ($temp = $bookseller->{'listincgst'}) {
255                 $template->param(listincgst => $temp);
256                 $gist = 0;
257         } else {
258         $grand_total += $gist;
259         $grand_total_est += $sub_total_est * $prefgist;         # same thing as += gist
260     }
261     if ($temp = $bookseller->{'discount'}) {
262                 $template->param(discount => sprintf( "%.2f", $temp ));
263         }
264     my $contract = &GetContract($basket->{contractnumber});
265     $template->param(
266         basketno             => $basketno,
267         basketname           => $basket->{'basketname'},
268         basketnote           => $basket->{note},
269         basketbooksellernote => $basket->{booksellernote},
270         basketcontractno     => $basket->{contractnumber},
271         basketcontractname   => $contract->{contractname},
272         creationdate         => format_date( $basket->{creationdate} ),
273         authorisedby         => $basket->{authorisedby},
274         authorisedbyname     => $basket->{authorisedbyname},
275         closedate            => format_date( $basket->{closedate} ),
276         active               => $bookseller->{'active'},
277         booksellerid         => $bookseller->{'id'},
278         name                 => $bookseller->{'name'},
279         address1             => $bookseller->{'address1'},
280         address2             => $bookseller->{'address2'},
281         address3             => $bookseller->{'address3'},
282         address4             => $bookseller->{'address4'},
283         entrydate            => format_date( $results[0]->{'entrydate'} ),
284         books_loop           => \@books_loop,
285         sort_loop            => \@sort_loop,
286         count                => $count,
287         gist                 => $gist ? sprintf( "%.2f", $gist ) : 0,
288         gist_rate       => sprintf( "%.2f", $prefgist * 100 ) . '%',
289         gist_est        => sprintf( "%.2f", $sub_total_est * $prefgist ),
290         gist_rrp        => sprintf( "%.2f", $gist_rrp ),
291         sub_total       => sprintf( "%.2f", $sub_total ),
292         grand_total     => sprintf( "%.2f", $grand_total ),
293         sub_total_est   => sprintf( "%.2f", $sub_total_est ),
294         grand_total_est => sprintf( "%.2f", $grand_total_est ),
295         sub_total_rrp   => sprintf( "%.2f", $sub_total_rrp ),
296         grand_total_rrp => sprintf( "%.2f", $sub_total_rrp + $gist_rrp ),
297         currency        => $bookseller->{'listprice'},
298         qty_total       => $qty_total,
299         GST             => $prefgist,
300         basketgroups  =>  $basketgroups,
301         grouped    => $basket->{basketgroupid},
302     );
303 }
304 output_html_with_http_headers $query, $cookie, $template->output;