Use DBI not mysql command-line to load the database tables
[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 # $Id$
24
25 use strict;
26 use C4::Auth;
27 use C4::Koha;
28 use C4::Output;
29 use CGI;
30
31
32 use C4::Acquisition;
33 use C4::Bookfund;
34 use C4::Bookseller;
35 use C4::Date;
36
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 allow to add a new order for this supplier from
45  an existing record, a suggestion or from a new record.
46
47 =head1 CGI PARAMETERS
48
49 =over 4
50
51 =item $basketno
52
53 this parameter seems to be unused.
54
55 =item supplierid
56
57 the supplier this script have to display the basket.
58
59 =item order
60
61
62
63 =back
64
65 =cut
66
67 my $query        = new CGI;
68 my $basketno     = $query->param('basketno');
69 my $booksellerid = $query->param('supplierid');
70 my $order        = $query->param('order');
71 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
72     {
73         template_name   => "acqui/basket.tmpl",
74         query           => $query,
75         type            => "intranet",
76         authnotrequired => 0,
77         flagsrequired   => { acquisition => 1 },
78         debug           => 1,
79     }
80 );
81
82 my $basket = GetBasket($basketno);
83
84 # FIXME : the query->param('supplierid') below is probably useless. The bookseller is always known from the basket
85 # if no booksellerid in parameter, get it from basket
86 # warn "=>".$basket->{booksellerid};
87 $booksellerid = $basket->{booksellerid} unless $booksellerid;
88 my @booksellers = GetBookSeller($booksellerid);
89 my $count2 = scalar @booksellers;
90
91 # get librarian branch...
92 if ( C4::Context->preference("IndependantBranches") ) {
93     my $userenv = C4::Context->userenv;
94     unless ( $userenv->{flags} == 1 ) {
95         my $validtest = ( $basket->{creationdate} eq '' )
96           || ( $basket->{branch}  eq '' )
97           || ( $userenv->{branch} eq $basket->{branch} )
98           || ( $userenv->{branch} eq '' )
99           || ( $basket->{branch}  eq '' );
100         unless ($validtest) {
101             print $query->redirect("../mainpage.pl");
102             exit 1;
103         }
104     }
105 }
106
107 # if new basket, pre-fill infos
108 $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
109 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
110
111 my ( $count, @results );
112 @results  = GetOrders( $basketno, $order );
113 $count = scalar @results;
114
115 my $line_total;     # total of each line
116 my $sub_total;      # total of line totals
117 my $gist;           # GST
118 my $grand_total;    # $subttotal + $gist
119 my $toggle = 0;
120
121
122 # my $line_total_est; # total of each line
123 my $sub_total_est;      # total of line totals
124 my $gist_est;           # GST
125 my $grand_total_est;    # $subttotal + $gist
126
127 my $qty_total;
128
129 my @books_loop;
130 for ( my $i = 0 ; $i < $count ; $i++ ) {
131     my $rrp = $results[$i]->{'listprice'};
132     $rrp = ConvertCurrency( $results[$i]->{'currency'}, $rrp );
133
134     $sub_total_est += $results[$i]->{'quantity'} * $results[$i]->{'rrp'};
135     $line_total = $results[$i]->{'quantity'} * $results[$i]->{'ecost'};
136     $sub_total += $line_total;
137     $qty_total += $results[$i]->{'quantity'};
138     my %line;
139     %line=%{$results[$i]};
140    if ( $toggle == 0 ) {
141         $line{color} = '#EEEEEE';
142         $toggle = 1;
143     }
144     else {
145         $line{color} = 'white';
146         $toggle = 0;
147     }
148     $line{basketno}         = $basketno;
149     $line{i}                = $i;
150     $line{rrp}              = sprintf( "%.2f", $line{'rrp'} );
151     $line{ecost}            = sprintf( "%.2f", $line{'ecost'} );
152     $line{line_total}       = sprintf( "%.2f", $line_total );
153     $line{odd}              = $i % 2;
154     push @books_loop, \%line;
155 }
156 my $prefgist = C4::Context->preference("gist");
157 $gist            = sprintf( "%.2f", $sub_total * $prefgist );
158 $grand_total     = $sub_total + $gist;
159 $grand_total_est =
160   $sub_total_est + sprintf( "%.2f", $sub_total_est * $prefgist );
161 $gist_est = sprintf( "%.2f", $sub_total_est * $prefgist );
162 $template->param(
163     basketno         => $basketno,
164     creationdate     => format_date( $basket->{creationdate} ),
165     authorisedby     => $basket->{authorisedby},
166     authorisedbyname => $basket->{authorisedbyname},
167     closedate        => format_date( $basket->{closedate} ),
168     active           => $booksellers[0]->{'active'},
169     booksellerid     => $booksellers[0]->{'id'},
170     name             => $booksellers[0]->{'name'},
171     address1         => $booksellers[0]->{'address1'},
172     address2         => $booksellers[0]->{'address2'},
173     address3         => $booksellers[0]->{'address3'},
174     address4         => $booksellers[0]->{'address4'},
175     entrydate        => format_date( $results[0]->{'entrydate'} ),
176     books_loop       => \@books_loop,
177     count            => $count,
178     sub_total        => $sub_total,
179     gist             => $gist,
180     grand_total      => $grand_total,
181     sub_total_est    => $sub_total_est,
182     gist_est         => $gist_est,
183     grand_total_est  => $grand_total_est,
184     currency         => $booksellers[0]->{'listprice'},
185     qty_total        => $qty_total,
186 );
187 output_html_with_http_headers $query, $cookie, $template->output;