63ce42910457d1a6fd7038e921fad9cfda79c764
[koha.git] / acqui / parcel.pl
1 #!/usr/bin/perl
2
3 #script to receive 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 =head1 NAME
24
25 parcel.pl
26
27 =head1 DESCRIPTION
28 This script shows all orders receipt or pending for a given supplier.
29 It allows to write an order as 'received' when he arrives.
30
31 =head1 CGI PARAMETERS
32
33 =over 4
34
35 =item supplierid
36 To know the supplier this script has to show orders.
37
38 =item code
39 is the bookseller invoice number.
40
41 =item freight
42
43
44 =item gst
45
46
47 =item datereceived
48 To filter the results list on this given date.
49
50 =back
51
52 =cut
53
54 use C4::Auth;
55 use C4::Acquisition;
56 use C4::Bookseller;
57 use C4::Biblio;
58 use CGI;
59 use C4::Output;
60 use C4::Dates qw/format_date format_date_in_iso/;
61
62 use strict;
63
64 my $input      = new CGI;
65 my $supplierid = $input->param('supplierid');
66 my $bookseller = GetBookSellerFromId($supplierid);
67
68 my $invoice = $input->param('invoice') || '';
69 my $freight = $input->param('freight');
70 my $gst     = $input->param('gst');
71 my $datereceived =
72   ($input->param('op') eq 'new')
73   ? C4::Dates->new($input->param('datereceived'))
74   : C4::Dates->new($input->param('datereceived'), 'iso');
75 $datereceived = C4::Dates->new() unless $datereceived;
76 my $code            = $input->param('code');
77 my @rcv_err         = $input->param('error');
78 my @rcv_err_barcode = $input->param('error_bc');
79
80 my ($template, $loggedinuser, $cookie) = get_template_and_user(
81     {   template_name   => "acqui/parcel.tmpl",
82         query           => $input,
83         type            => "intranet",
84         authnotrequired => 0,
85         flagsrequired   => { acquisition => 1 },
86         debug           => 1,
87     }
88 );
89
90 if($input->param('op') eq "delete" ){
91     if($input->param('biblionumber') and $input->param('ordernumber')){
92        DelOrder($input->param('biblionumber'), $input->param('ordernumber')); 
93        print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?supplierid=$supplierid&datereceived=" . $datereceived->output('iso'));
94        exit;
95     }
96 }
97
98
99 # If receiving error, report the error (coming from finishreceive.pl).
100 if (scalar(@rcv_err)) {
101     my $cnt = 0;
102     my $error_loop;
103     for my $err (@rcv_err) {
104         push @$error_loop, { "error_$err" => 1, barcode => $rcv_err_barcode[$cnt] };
105         $cnt++;
106     }
107     $template->param(
108         receive_error => 1,
109         error_loop    => $error_loop,
110     );
111 }
112
113 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
114 my @parcelitems   = GetParcel($supplierid, $invoice, $datereceived->output('iso'));
115 my $countlines    = scalar @parcelitems;
116 my $totalprice    = 0;
117 my $totalfreight  = 0;
118 my $totalquantity = 0;
119 my $total;
120 my $tototal;
121 my @loop_received = ();
122
123 for (my $i = 0 ; $i < $countlines ; $i++) {
124
125     #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'};   #weird, are the freight fees counted by book? (pierre)
126     $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'};    #weird, are the freight fees counted by book? (pierre)
127     $parcelitems[$i]->{'unitprice'} += 0;
128     my %line;
129     %line          = %{ $parcelitems[$i] };
130     $line{invoice} = $invoice;
131     $line{gst}     = $gst;
132     $line{total} = sprintf($cfstr, $total);
133     $line{supplierid} = $supplierid;
134     push @loop_received, \%line;
135     $totalprice += $parcelitems[$i]->{'unitprice'};
136     $line{unitprice} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
137
138     #double FIXME - totalfreight is redefined later.
139
140 # FIXME - each order in a  parcel holds the freight for the whole parcel. This means if you receive a parcel with items from multiple budgets, you'll see the freight charge in each budget..
141     if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
142         warn "FREIGHT CHARGE MISMATCH!!";
143     }
144     $totalfreight = $parcelitems[$i]->{'freight'};
145     $totalquantity += $parcelitems[$i]->{'quantityreceived'};
146     $tototal       += $total;
147 }
148
149 my $pendingorders = GetPendingOrders($supplierid);
150 my $countpendings = scalar @$pendingorders;
151
152 # pending orders totals
153 my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
154 my $ordergrandtotal;
155 my @loop_orders = ();
156 for (my $i = 0 ; $i < $countpendings ; $i++) {
157     my %line;
158     %line = %{ $pendingorders->[$i] };
159     $line{quantity}         += 0;
160     $line{quantityreceived} += 0;
161     $line{unitprice}        += 0;
162     $totalPunitprice        += $line{unitprice};
163     $totalPquantity         += $line{quantity};
164     $totalPqtyrcvd          += $line{quantityreceived};
165     $totalPecost            += $line{ecost};
166     $line{ecost}      = sprintf("%.2f", $line{ecost});
167     $line{ordertotal} = sprintf("%.2f", $line{ecost} * $line{quantity});
168     $line{unitprice}  = sprintf("%.2f", $line{unitprice});
169     $line{invoice}    = $invoice;
170     $line{gst}        = $gst;
171     $line{total}      = $total;
172     $line{supplierid} = $supplierid;
173     $ordergrandtotal += $line{ecost} * $line{quantity};
174     push @loop_orders, \%line;
175 }
176 $freight = $totalfreight unless $freight;
177
178 #$totalfreight=$freight;
179 $tototal = $tototal + $freight;
180
181 $template->param(
182     invoice               => $invoice,
183     datereceived          => $datereceived->output('iso'),
184     invoicedatereceived   => $datereceived->output('iso'),
185     formatteddatereceived => $datereceived->output(),
186     name                  => $bookseller->{'name'},
187     supplierid            => $supplierid,
188     gst                   => $gst,
189     freight               => $freight,
190     invoice               => $invoice,
191     countreceived         => $countlines,
192     loop_received         => \@loop_received,
193     countpending          => $countpendings,
194     loop_orders           => \@loop_orders,
195     totalprice            => sprintf($cfstr, $totalprice),
196     totalfreight          => $totalfreight,
197     totalquantity         => $totalquantity,
198     tototal               => sprintf($cfstr, $tototal),
199     ordergrandtotal       => sprintf($cfstr, $ordergrandtotal),
200     gst                   => $gst,
201     grandtot              => sprintf($cfstr, $tototal + $gst),
202     totalPunitprice       => sprintf("%.2f", $totalPunitprice),
203     totalPquantity        => $totalPquantity,
204     totalPqtyrcvd         => $totalPqtyrcvd,
205     totalPecost           => sprintf("%.2f", $totalPecost),
206 );
207 output_html_with_http_headers $input, $cookie, $template->output;