3 #script to recieve orders
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2008-2009 BibLibre SARL
9 # This file is part of Koha.
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
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.
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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.
37 To know the supplier this script has to show orders.
40 is the bookseller invoice number.
49 To filter the results list on this given date.
56 #use warnings; FIXME - Bug 2505
65 use C4
::Dates qw
/format_date format_date_in_iso/;
69 my $supplierid=$input->param('supplierid');
70 my $bookseller=GetBookSellerFromId
($supplierid);
72 my $invoice=$input->param('invoice') || '';
73 my $freight=$input->param('freight');
74 my $gst= $input->param('gst') || $bookseller->{gstrate
} || C4
::Context
->preference("gist") || 0;
75 my $datereceived = ($input->param('op') eq 'new') ? C4
::Dates
->new($input->param('datereceived'))
76 : C4
::Dates
->new($input->param('datereceived'), 'iso') ;
77 $datereceived = C4
::Dates
->new() unless $datereceived;
78 my $code = $input->param('code');
79 my @rcv_err = $input->param('error');
80 my @rcv_err_barcode = $input->param('error_bc');
82 my $startfrom=$input->param('startfrom');
83 my $resultsperpage = $input->param('resultsperpage');
84 $resultsperpage = 20 unless ($resultsperpage);
85 $startfrom=0 unless ($startfrom);
87 if($input->param('format') eq "json"){
88 my ($template, $loggedinuser, $cookie)
89 = get_template_and_user
({template_name
=> "acqui/ajax.tmpl",
93 flagsrequired
=> {acquisition
=> 'order_receive'},
98 my $search = $input->param('search') || '';
99 my $supplier = $input->param('supplierid') || '';
100 my $basketno = $input->param('basketno') || '';
101 my $orderno = $input->param('orderno') || '';
103 my $orders = SearchOrder
($orderno, $search, $supplier, $basketno);
104 foreach my $order (@
$orders){
105 if($order->{quantityreceived
} < $order->{quantity
}){
108 $data->{basketno
} = $order->{basketno
};
109 $data->{ordernumber
} = $order->{ordernumber
};
110 $data->{title
} = $order->{title
};
111 $data->{author
} = $order->{author
};
112 $data->{isbn
} = $order->{isbn
};
113 $data->{booksellerid
} = $order->{booksellerid
};
114 $data->{biblionumber
} = $order->{biblionumber
};
115 $data->{freight
} = $order->{freight
};
116 $data->{quantity
} = $order->{quantity
};
117 $data->{ecost
} = $order->{ecost
};
118 $data->{ordertotal
} = sprintf("%.2f",$order->{ecost
}*$order->{quantity
});
123 my $json_text = to_json
(\
@datas);
124 $template->param(return => $json_text);
125 output_html_with_http_headers
$input, $cookie, $template->output;
129 my ($template, $loggedinuser, $cookie)
130 = get_template_and_user
({template_name
=> "acqui/parcel.tmpl",
133 authnotrequired
=> 0,
134 flagsrequired
=> {acquisition
=> 'order_receive'},
138 my $action = $input->param('action');
139 my $ordernumber = $input->param('ordernumber');
140 my $biblionumber = $input->param('biblionumber');
142 # If canceling an order
143 if ($action eq "cancelorder") {
148 # We delete the order
149 DelOrder
($biblionumber, $ordernumber);
151 # We delete all the items related to this order
152 my @itemnumbers = GetItemnumbersFromOrder
($ordernumber);
153 foreach (@itemnumbers) {
154 my $delcheck = DelItemCheck
(C4
::Context
->dbh, $biblionumber, $_);
155 # (should always success, as no issue should exist on item on order)
156 if ($delcheck != 1) { $error_delitem = 1; }
159 # We get the number of remaining items
160 my $itemcount = GetItemsCount
($biblionumber);
162 # If there are no items left,
163 if ($itemcount eq 0) {
164 # We delete the record
165 $error_delbiblio = DelBiblio
($biblionumber);
168 if ($error_delitem || $error_delbiblio) {
170 warn $error_delbiblio;
171 if ($error_delitem) { $template->param(error_delitem
=> 1); }
172 if ($error_delbiblio) { $template->param(error_delbiblio
=> 1); }
174 $template->param(success_delorder
=> 1);
178 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
179 if( scalar(@rcv_err) ) {
182 for my $err (@rcv_err) {
183 push @
$error_loop, { "error_$err" => 1 , barcode
=> $rcv_err_barcode[$cnt] };
186 $template->param( receive_error
=> 1 ,
187 error_loop
=> $error_loop,
191 my $cfstr = "%.2f"; # currency format string -- could get this from currency table.
192 my @parcelitems = GetParcel
($supplierid, $invoice, $datereceived->output('iso'));
193 my $countlines = scalar @parcelitems;
195 my $totalfreight = 0;
196 my $totalquantity = 0;
199 my @loop_received = ();
201 for (my $i = 0 ; $i < $countlines ; $i++) {
203 #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'}; #weird, are the freight fees counted by book? (pierre)
204 $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'}; #weird, are the freight fees counted by book? (pierre)
205 $parcelitems[$i]->{'unitprice'} += 0;
207 %line = %{ $parcelitems[$i] };
208 $line{invoice
} = $invoice;
210 $line{total
} = sprintf($cfstr, $total);
211 $line{supplierid
} = $supplierid;
212 push @loop_received, \
%line;
213 $totalprice += $parcelitems[$i]->{'unitprice'};
214 $line{unitprice
} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
216 #double FIXME - totalfreight is redefined later.
218 # 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..
219 if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
220 warn "FREIGHT CHARGE MISMATCH!!";
222 $totalfreight = $parcelitems[$i]->{'freight'};
223 $totalquantity += $parcelitems[$i]->{'quantityreceived'};
227 my $pendingorders = GetPendingOrders
($supplierid);
228 my $countpendings = scalar @
$pendingorders;
230 # pending orders totals
231 my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
233 my @loop_orders = ();
234 for (my $i = 0 ; $i < $countpendings ; $i++) {
236 %line = %{$pendingorders->[$i]};
238 $line{quantityreceived
}+=0;
240 $totalPunitprice += $line{unitprice
};
241 $totalPquantity +=$line{quantity
};
242 $totalPqtyrcvd +=$line{quantityreceived
};
243 $totalPecost += $line{ecost
};
244 $line{ecost
} = sprintf("%.2f",$line{ecost
});
245 $line{ordertotal
} = sprintf("%.2f",$line{ecost
}*$line{quantity
});
246 $line{unitprice
} = sprintf("%.2f",$line{unitprice
});
247 $line{invoice
} = $invoice;
249 $line{total
} = $total;
250 $line{supplierid
} = $supplierid;
251 $ordergrandtotal += $line{ecost
} * $line{quantity
};
252 push @loop_orders, \
%line if ($i >= $startfrom and $i < $startfrom + $resultsperpage);
254 $freight = $totalfreight unless $freight;
256 my $count = $countpendings;
258 if ($count>$resultsperpage){
260 my $displayprev=$startfrom;
261 if(($count - ($startfrom+$resultsperpage)) > 0 ) {
266 for (my $i=1; $i<$count/$resultsperpage+1; $i++) {
268 ($startfrom/$resultsperpage==($i-1)) && ($highlight=1);
269 push @numbers, { number
=> $i,
270 highlight
=> $highlight ,
271 startfrom
=> ($i-1)*$resultsperpage};
274 my $from = $startfrom*$resultsperpage+1;
276 if($count < (($startfrom+1)*$resultsperpage)){
279 $to = (($startfrom+1)*$resultsperpage);
281 $template->param(numbers
=>\
@numbers,
282 displaynext
=>$displaynext,
283 displayprev
=>$displayprev,
284 nextstartfrom
=>(($startfrom+$resultsperpage<$count)?
$startfrom+$resultsperpage:$count),
285 prevstartfrom
=>(($startfrom-$resultsperpage>0)?
$startfrom-$resultsperpage:0)
289 #$totalfreight=$freight;
290 $tototal = $tototal + $freight;
294 datereceived
=> $datereceived->output('iso'),
295 invoicedatereceived
=> $datereceived->output('iso'),
296 formatteddatereceived
=> $datereceived->output(),
297 name
=> $bookseller->{'name'},
298 supplierid
=> $supplierid,
302 countreceived
=> $countlines,
303 loop_received
=> \
@loop_received,
304 countpending
=> $countpendings,
305 loop_orders
=> \
@loop_orders,
306 totalprice
=> sprintf($cfstr, $totalprice),
307 totalfreight
=> $totalfreight,
308 totalquantity
=> $totalquantity,
309 tototal
=> sprintf($cfstr, $tototal),
310 ordergrandtotal
=> sprintf($cfstr, $ordergrandtotal),
312 grandtot
=> sprintf($cfstr, $tototal + $gst),
313 totalPunitprice
=> sprintf("%.2f", $totalPunitprice),
314 totalPquantity
=> $totalPquantity,
315 totalPqtyrcvd
=> $totalPqtyrcvd,
316 totalPecost
=> sprintf("%.2f", $totalPecost),
317 resultsperpage
=> $resultsperpage,
319 output_html_with_http_headers
$input, $cookie, $template->output;