Chenge database structure to not delete an order line when biblio gets deleted
[koha.git] / acqui / parcel.pl
1 #!/usr/bin/perl
2
3 #script to recieve orders
4
5
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2008-2009 BibLibre SARL
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 =head1 NAME
25
26 parcel.pl
27
28 =head1 DESCRIPTION
29 This script shows all orders receipt or pending for a given supplier.
30 It allows to write an order as 'received' when he arrives.
31
32 =head1 CGI PARAMETERS
33
34 =over 4
35
36 =item supplierid
37 To know the supplier this script has to show orders.
38
39 =item code
40 is the bookseller invoice number.
41
42 =item freight
43
44
45 =item gst
46
47
48 =item datereceived
49 To filter the results list on this given date.
50
51 =back
52
53 =cut
54
55 use C4::Auth;
56 use C4::Acquisition;
57 use C4::Bookseller;
58 use C4::Biblio;
59 use CGI;
60 use C4::Output;
61 use C4::Dates qw/format_date format_date_in_iso/;
62
63 use strict;
64
65 my $input=new CGI;
66 my $supplierid=$input->param('supplierid');
67 my $bookseller=GetBookSellerFromId($supplierid);
68
69 my $invoice=$input->param('invoice') || '';
70 my $freight=$input->param('freight');
71 my $gst= $input->param('gst') || $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
72 my $datereceived =  ($input->param('op') eq 'new') ? C4::Dates->new($input->param('datereceived')) 
73                                         :  C4::Dates->new($input->param('datereceived'), 'iso')   ;
74 $datereceived = C4::Dates->new() unless $datereceived;
75 my $code            = $input->param('code');
76 my @rcv_err         = $input->param('error');
77 my @rcv_err_barcode = $input->param('error_bc');
78
79 my $startfrom=$input->param('startfrom');
80 my $resultsperpage = $input->param('resultsperpage');
81 $resultsperpage = 20 unless ($resultsperpage);
82 $startfrom=0 unless ($startfrom);
83
84 my ($template, $loggedinuser, $cookie)
85     = get_template_and_user({template_name => "acqui/parcel.tmpl",
86                  query => $input,
87                                  type => "intranet",
88                  authnotrequired => 0,
89                  flagsrequired => {acquisition => 'order_receive'},
90                  debug => 1,
91 });
92
93 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
94 if( scalar(@rcv_err) ) {
95         my $cnt=0;
96         my $error_loop;
97         for my $err (@rcv_err) {
98                 push @$error_loop, { "error_$err" => 1 , barcode => $rcv_err_barcode[$cnt] };
99                 $cnt++;
100         }
101         $template->param( receive_error => 1 ,
102                                                 error_loop => $error_loop,
103                                         );
104 }
105
106 my $cfstr         = "%.2f";                                                           # currency format string -- could get this from currency table.
107 my @parcelitems   = GetParcel($supplierid, $invoice, $datereceived->output('iso'));
108 my $countlines    = scalar @parcelitems;
109 my $totalprice    = 0;
110 my $totalfreight  = 0;
111 my $totalquantity = 0;
112 my $total;
113 my $tototal;
114 my @loop_received = ();
115
116 for (my $i = 0 ; $i < $countlines ; $i++) {
117
118     #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'};   #weird, are the freight fees counted by book? (pierre)
119     $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'};    #weird, are the freight fees counted by book? (pierre)
120     $parcelitems[$i]->{'unitprice'} += 0;
121     my %line;
122     %line          = %{ $parcelitems[$i] };
123     $line{invoice} = $invoice;
124     $line{gst}     = $gst;
125     $line{total} = sprintf($cfstr, $total);
126     $line{supplierid} = $supplierid;
127     push @loop_received, \%line;
128     $totalprice += $parcelitems[$i]->{'unitprice'};
129     $line{unitprice} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
130
131     #double FIXME - totalfreight is redefined later.
132
133 # 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..
134     if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
135         warn "FREIGHT CHARGE MISMATCH!!";
136     }
137     $totalfreight = $parcelitems[$i]->{'freight'};
138     $totalquantity += $parcelitems[$i]->{'quantityreceived'};
139     $tototal       += $total;
140 }
141
142 my $pendingorders = GetPendingOrders($supplierid);
143 my $countpendings = scalar @$pendingorders;
144
145 # pending orders totals
146 my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
147 my $ordergrandtotal;
148 my @loop_orders = ();
149 for (my $i = 0 ; $i < $countpendings ; $i++) {
150     my %line;
151     %line = %{$pendingorders->[$i]};
152     $line{quantity}+=0;
153     $line{quantrem} = $line{quantity} - $line{quantityreceived};
154     $line{quantityreceived}+=0;
155     $line{unitprice}+=0;
156     $totalPunitprice += $line{unitprice};
157     $totalPquantity +=$line{quantity};
158     $totalPqtyrcvd +=$line{quantityreceived};
159     $totalPecost += $line{ecost};
160     $line{ecost} = sprintf("%.2f",$line{ecost});
161     $line{ordertotal} = sprintf("%.2f",$line{ecost}*$line{quantity});
162     $line{unitprice} = sprintf("%.2f",$line{unitprice});
163     $line{invoice} = $invoice;
164     $line{gst} = $gst;
165     $line{total} = $total;
166     $line{supplierid} = $supplierid;
167     $ordergrandtotal += $line{ecost} * $line{quantity};
168     push @loop_orders, \%line if ($i >= $startfrom and $i < $startfrom + $resultsperpage);
169 }
170 $freight = $totalfreight unless $freight;
171
172 my $count = $countpendings;
173
174 if ($count>$resultsperpage){
175     my $displaynext=0;
176     my $displayprev=$startfrom;
177     if(($count - ($startfrom+$resultsperpage)) > 0 ) {
178         $displaynext = 1;
179     }
180
181     my @numbers = ();
182     for (my $i=1; $i<$count/$resultsperpage+1; $i++) {
183             my $highlight=0;
184             ($startfrom/$resultsperpage==($i-1)) && ($highlight=1);
185             push @numbers, { number => $i,
186                 highlight => $highlight ,
187                 startfrom => ($i-1)*$resultsperpage};
188     }
189
190     my $from = $startfrom*$resultsperpage+1;
191     my $to;
192     if($count < (($startfrom+1)*$resultsperpage)){
193         $to = $count;
194     } else {
195         $to = (($startfrom+1)*$resultsperpage);
196     }
197     $template->param(numbers=>\@numbers,
198                      displaynext=>$displaynext,
199                      displayprev=>$displayprev,
200                      nextstartfrom=>(($startfrom+$resultsperpage<$count)?$startfrom+$resultsperpage:$count),
201                      prevstartfrom=>(($startfrom-$resultsperpage>0)?$startfrom-$resultsperpage:0)
202                     );
203 }
204
205 #$totalfreight=$freight;
206 $tototal = $tototal + $freight;
207
208 $template->param(
209     invoice               => $invoice,
210     datereceived          => $datereceived->output('iso'),
211     invoicedatereceived   => $datereceived->output('iso'),
212     formatteddatereceived => $datereceived->output(),
213     name                  => $bookseller->{'name'},
214     supplierid            => $supplierid,
215     gst                   => $gst,
216     freight               => $freight,
217     invoice               => $invoice,
218     countreceived         => $countlines,
219     loop_received         => \@loop_received,
220     countpending          => $countpendings,
221     loop_orders           => \@loop_orders,
222     totalprice            => sprintf($cfstr, $totalprice),
223     totalfreight          => $totalfreight,
224     totalquantity         => $totalquantity,
225     tototal               => sprintf($cfstr, $tototal),
226     ordergrandtotal       => sprintf($cfstr, $ordergrandtotal),
227     gst                   => $gst,
228     grandtot              => sprintf($cfstr, $tototal + $gst),
229     totalPunitprice       => sprintf("%.2f", $totalPunitprice),
230     totalPquantity        => $totalPquantity,
231     totalPqtyrcvd         => $totalPqtyrcvd,
232     totalPecost           => sprintf("%.2f", $totalPecost),
233     resultsperpage        => $resultsperpage,
234 );
235 output_html_with_http_headers $input, $cookie, $template->output;