Merging Katipo changes.
[koha.git] / acqui / basket.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 #script to show display basket of orders
6 #written by chris@katipo.co.nz 24/2/2000
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use C4::Auth;
27 use C4::Koha;
28 use C4::Output;
29 use CGI;
30 use C4::Interface::CGI::Output;
31 use C4::Database;
32 use HTML::Template;
33 use C4::Acquisition;
34 use C4::Date;
35
36 my $query        = new CGI;
37 my $basketno     = $query->param('basket');
38 my $booksellerid = $query->param('supplierid');
39 my $order        = $query->param('order');
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => "acqui/basket.tmpl",
43         query           => $query,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { acquisition => 1 },
47         debug           => 1,
48     }
49 );
50 my ( $count, @results );
51
52 my $basket = getbasket($basketno);
53
54 # FIXME : the query->param('supplierid') below is probably useless. The bookseller is always known from the basket
55 # if no booksellerid in parameter, get it from basket
56 # warn "=>".$basket->{booksellerid};
57 $booksellerid = $basket->{booksellerid} unless $booksellerid;
58 my ( $count2, @booksellers ) = bookseller($booksellerid);
59
60 # get librarian branch...
61 if ( C4::Context->preference("IndependantBranches") ) {
62     my $userenv = C4::Context->userenv;
63     unless ( $userenv->{flags} == 1 ) {
64         my $validtest = ( $basket->{creationdate} eq '' )
65           || ( $basket->{branch}  eq '' )
66           || ( $userenv->{branch} eq $basket->{branch} )
67           || ( $userenv->{branch} eq '' )
68           || ( $basket->{branch}  eq '' );
69         unless ($validtest) {
70             print $query->redirect("../mainpage.pl");
71             exit 1;
72         }
73     }
74 }
75
76 # if new basket, pre-fill infos
77 $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
78 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
79 ( $count, @results ) = getbasketcontent( $basketno, '', $order );
80
81 my $line_total;     # total of each line
82 my $sub_total;      # total of line totals
83 my $gist;           # GST
84 my $grand_total;    # $subttotal + $gist
85 my $toggle = 0;
86
87
88 # my $line_total_est; # total of each line
89 my $sub_total_est;      # total of line totals
90 my $gist_est;           # GST
91 my $grand_total_est;    # $subttotal + $gist
92
93 my $qty_total;
94
95 my @books_loop;
96 for ( my $i = 0 ; $i < $count ; $i++ ) {
97     my $rrp = $results[$i]->{'listprice'};
98     $rrp = curconvert( $results[$i]->{'currency'}, $rrp );
99
100     $sub_total_est += $results[$i]->{'quantity'} * $results[$i]->{'rrp'};
101     $line_total = $results[$i]->{'quantity'} * $results[$i]->{'ecost'};
102     $sub_total += $line_total;
103     $qty_total += $results[$i]->{'quantity'};
104     my %line;
105    if ( $toggle == 0 ) {
106         $line{color} = '#EEEEEE';
107         $toggle = 1;
108     }
109     else {
110         $line{color} = 'white';
111         $toggle = 0;
112     }
113     $line{ordernumber}      = $results[$i]->{'ordernumber'};
114     $line{publishercode}    = $results[$i]->{'publishercode'};
115     $line{isbn}             = $results[$i]->{'isbn'};
116     $line{booksellerid}     = $results[$i]->{'booksellerid'};
117     $line{basketno}         = $basketno;
118     $line{title}            = $results[$i]->{'title'};
119     $line{notes}            = $results[$i]->{'notes'};
120     $line{author}           = $results[$i]->{'author'};
121     $line{i}                = $i;
122     $line{rrp}              = sprintf( "%.2f", $results[$i]->{'rrp'} );
123     $line{ecost}            = sprintf( "%.2f", $results[$i]->{'ecost'} );
124     $line{quantity}         = $results[$i]->{'quantity'};
125     $line{quantityrecieved} = $results[$i]->{'quantityreceived'};
126     $line{line_total}       = sprintf( "%.2f", $line_total );
127     $line{biblionumber}     = $results[$i]->{'biblionumber'};
128     $line{bookfundid}       = $results[$i]->{'bookfundid'};
129     $line{odd}              = $i % 2;
130     push @books_loop, \%line;
131 }
132 my $prefgist = C4::Context->preference("gist");
133 $gist            = sprintf( "%.2f", $sub_total * $prefgist );
134 $grand_total     = $sub_total + $gist;
135 $grand_total_est =
136   $sub_total_est + sprintf( "%.2f", $sub_total_est * $prefgist );
137 $gist_est = sprintf( "%.2f", $sub_total_est * $prefgist );
138 $template->param(
139     basketno         => $basketno,
140     creationdate     => format_date( $basket->{creationdate} ),
141     authorisedby     => $basket->{authorisedby},
142     authorisedbyname => $basket->{authorisedbyname},
143     closedate        => format_date( $basket->{closedate} ),
144     active           => $booksellers[0]->{'active'},
145     booksellerid     => $booksellers[0]->{'id'},
146     name             => $booksellers[0]->{'name'},
147     address1         => $booksellers[0]->{'address1'},
148     address2         => $booksellers[0]->{'address2'},
149     address3         => $booksellers[0]->{'address3'},
150     address4         => $booksellers[0]->{'address4'},
151     entrydate        => format_date( $results[0]->{'entrydate'} ),
152     books_loop       => \@books_loop,
153     count            => $count,
154     sub_total        => $sub_total,
155     gist             => $gist,
156     grand_total      => $grand_total,
157     sub_total_est    => $sub_total_est,
158     gist_est         => $gist_est,
159     grand_total_est  => $grand_total_est,
160     currency         => $booksellers[0]->{'listprice'},
161     qty_total        => $qty_total,
162 );
163 output_html_with_http_headers $query, $cookie, $template->output;