5f071e9659177912dbe0aa3f1cfad2a2b8224d10
[koha.git] / acqui / basket.pl
1 #!/usr/bin/perl
2
3 #script to show display basket of orders
4 #written by chris@katipo.co.nz 24/2/2000
5
6 # Copyright 2000-2002 Katipo Communications
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
24 use strict;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Output;
28 use CGI;
29 use C4::Acquisition;
30 use C4::Bookfund;
31 use C4::Bookseller;
32 use C4::Dates qw/format_date/;
33
34 use vars qw($debug);
35
36 BEGIN {
37         $debug = $ENV{DEBUG} || 1;
38 }
39
40 =head1 NAME
41
42 basket.pl
43
44 =head1 DESCRIPTION
45
46  This script display all informations about basket for the supplier given
47  on input arg. Moreover, it allow to add a new order for this supplier from
48  an existing record, a suggestion or from a new record.
49
50 =head1 CGI PARAMETERS
51
52 =over 4
53
54 =item $basketno
55
56 this parameter seems to be unused.
57
58 =item supplierid
59
60 the supplier this script have to display the basket.
61
62 =item order
63
64 =back
65
66 =cut
67
68 my $query        = new CGI;
69 my $basketno     = $query->param('basketno');
70 my $booksellerid = $query->param('supplierid');
71 my $order        = $query->param('order');
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73     {
74         template_name   => "acqui/basket.tmpl",
75         query           => $query,
76         type            => "intranet",
77         authnotrequired => 0,
78         flagsrequired   => { acquisition => 1 },
79         debug           => 1,
80     }
81 );
82
83 my $basket = GetBasket($basketno);
84
85 # FIXME : the query->param('supplierid') below is probably useless. The bookseller is always known from the basket
86 # if no booksellerid in parameter, get it from basket
87 # warn "=>".$basket->{booksellerid};
88 $booksellerid = $basket->{booksellerid} unless $booksellerid;
89 my ($bookseller) = GetBookSellerFromId($booksellerid);
90
91 if (! $bookseller){
92         $template->param( NO_BOOKSELLER => 1 ); 
93 }
94 else {
95 # get librarian branch...
96 if ( C4::Context->preference("IndependantBranches") ) {
97     my $userenv = C4::Context->userenv;
98     unless ( $userenv->{flags} == 1 ) {
99         my $validtest = ( $basket->{creationdate} eq '' )
100           || ( $userenv->{branch} eq $basket->{branch} )
101           || ( $userenv->{branch} eq '' )
102           || ( $basket->{branch}  eq '' );
103         unless ($validtest) {
104             print $query->redirect("../mainpage.pl");
105             exit 1;
106         }
107     }
108 }
109
110 # if new basket, pre-fill infos
111 $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
112 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
113 $debug and warn 
114         sprintf "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
115                 $basket->{creationdate}, $basket->{authorisedby} ;
116
117 my @results = GetOrders( $basketno, $order );
118 my $count = scalar @results;
119
120 my $line_total;     # total of each line
121 my $sub_total;      # total of line totals
122 my $gist;           # GST
123 my $grand_total;    # $subttotal + $gist
124 my $toggle = 0;
125
126
127 # my $line_total_est; # total of each line
128 my $sub_total_est;      # total of line totals
129 my $sub_total_rrp;      # total of line totals
130 my $gist_est;           # GST
131 my $grand_total_est;    # $subttotal + $gist
132
133 my $qty_total;
134 my @books_loop;
135 for ( my $i = 0 ; $i < $count ; $i++ ) {
136     my $rrp = $results[$i]->{'listprice'};
137     $rrp = ConvertCurrency( $results[$i]->{'currency'}, $rrp );
138     $sub_total_rrp += $results[$i]->{'quantity'} * $results[$i]->{'rrp'};
139     $line_total = $results[$i]->{'quantity'} * $results[$i]->{'ecost'};
140     $sub_total += $line_total;
141     $qty_total += $results[$i]->{'quantity'};
142     my %line;
143     %line=%{$results[$i]};
144    if ( $toggle == 0 ) {
145         $line{color} = '#EEEEEE';
146         $toggle = 1;
147     }
148     else {
149         $line{color} = 'white';
150         $toggle = 0;
151     }
152         $line{order_received} = ($results[$i]->{'quantity'} eq $results[$i]->{'quantityreceived'});
153         $line{publishercode}    = $results[$i]->{'publishercode'};
154         $line{basketno}         = $basketno;
155     $line{i}                = $i;
156     $line{rrp}              = sprintf( "%.2f", $line{'rrp'} );
157     $line{ecost}            = sprintf( "%.2f", $line{'ecost'} );
158     $line{line_total}       = sprintf( "%.2f", $line_total );
159     $line{odd}              = $i % 2;
160     push @books_loop, \%line;
161 }
162 my $prefgist = C4::Context->preference("gist");
163 $gist            = sprintf( "%.2f", $sub_total * $prefgist );
164 $grand_total     = $sub_total;
165 $grand_total_est = $sub_total_est;
166 unless ($bookseller->{'listincgst'}) {
167         $grand_total     += $gist;
168         $grand_total_est += sprintf( "%.2f", $sub_total_est * $prefgist );
169 }
170 my $grand_total_rrp =  sprintf( "%.2f", $sub_total_rrp );
171 $gist_est = sprintf( "%.2f", $sub_total_est * $prefgist );
172 $template->param(
173     basketno         => $basketno,
174     creationdate     => format_date( $basket->{creationdate} ),
175     authorisedby     => $basket->{authorisedby},
176     authorisedbyname => $basket->{authorisedbyname},
177     closedate        => format_date( $basket->{closedate} ),
178     active           => $bookseller->{'active'},
179     booksellerid     => $bookseller->{'id'},
180     name             => $bookseller->{'name'},
181     address1         => $bookseller->{'address1'},
182     address2         => $bookseller->{'address2'},
183     address3         => $bookseller->{'address3'},
184     address4         => $bookseller->{'address4'},
185     entrydate        => format_date( $results[0]->{'entrydate'} ),
186     books_loop       => \@books_loop,
187     count            => $count,
188     sub_total        => sprintf( "%.2f", $sub_total),
189     gist             => $gist,
190     grand_total      => sprintf( "%.2f", $grand_total),
191     sub_total_est    => $sub_total_est,
192     gist_est         => $gist_est,
193     grand_total_est  => $grand_total_est,
194     grand_total_rrp  => $grand_total_rrp,
195     currency         => $bookseller->{'listprice'},
196     qty_total        => $qty_total,
197     GST => $prefgist,
198 );
199         }
200 output_html_with_http_headers $query, $cookie, $template->output;