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.
30 This script shows all orders receipt or pending for a given supplier.
31 It allows to write an order as 'received' when he arrives.
39 To know the supplier this script has to show orders.
43 is the bookseller invoice number.
53 To filter the results list on this given date.
60 #use warnings; FIXME - Bug 2505
64 use C4
::Bookseller qw
/ GetBookSellerFromId /;
69 use C4
::Dates qw
/format_date format_date_in_iso/;
74 my $booksellerid=$input->param('booksellerid');
75 my $bookseller=GetBookSellerFromId
($booksellerid);
77 my $invoice=$input->param('invoice') || '';
78 my $freight=$input->param('freight');
79 my $input_gst = ($input->param('gst') eq '' ?
undef : $input->param('gst'));
80 my $gst= $input_gst // $bookseller->{gstrate
} // C4
::Context
->preference("gist") // 0;
81 my $datereceived = ($input->param('op') eq 'new') ? C4
::Dates
->new($input->param('datereceived'))
82 : C4
::Dates
->new($input->param('datereceived'), 'iso') ;
83 $datereceived = C4
::Dates
->new() unless $datereceived;
84 my $code = $input->param('code');
85 my @rcv_err = $input->param('error');
86 my @rcv_err_barcode = $input->param('error_bc');
88 my $startfrom=$input->param('startfrom');
89 my $resultsperpage = $input->param('resultsperpage');
90 $resultsperpage = 20 unless ($resultsperpage);
91 $startfrom=0 unless ($startfrom);
93 if($input->param('format') eq "json"){
94 my ($template, $loggedinuser, $cookie)
95 = get_template_and_user
({template_name
=> "acqui/ajax.tmpl",
99 flagsrequired
=> {acquisition
=> 'order_receive'},
104 my $search = $input->param('search') || '';
105 my $supplier = $input->param('booksellerid') || '';
106 my $basketno = $input->param('basketno') || '';
107 my $orderno = $input->param('orderno') || '';
109 my $orders = SearchOrder
($orderno, $search, $supplier, $basketno);
110 foreach my $order (@
$orders){
111 if($order->{quantityreceived
} < $order->{quantity
}){
114 $data->{basketno
} = $order->{basketno
};
115 $data->{ordernumber
} = $order->{ordernumber
};
116 $data->{title
} = $order->{title
};
117 $data->{author
} = $order->{author
};
118 $data->{isbn
} = $order->{isbn
};
119 $data->{booksellerid
} = $order->{booksellerid
};
120 $data->{biblionumber
} = $order->{biblionumber
};
121 $data->{freight
} = $order->{freight
};
122 $data->{quantity
} = $order->{quantity
};
123 $data->{ecost
} = $order->{ecost
};
124 $data->{ordertotal
} = sprintf("%.2f",$order->{ecost
}*$order->{quantity
});
129 my $json_text = to_json
(\
@datas);
130 $template->param(return => $json_text);
131 output_html_with_http_headers
$input, $cookie, $template->output;
135 my ($template, $loggedinuser, $cookie)
136 = get_template_and_user
({template_name
=> "acqui/parcel.tmpl",
139 authnotrequired
=> 0,
140 flagsrequired
=> {acquisition
=> 'order_receive'},
144 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
145 if( scalar(@rcv_err) ) {
148 for my $err (@rcv_err) {
149 push @
$error_loop, { "error_$err" => 1 , barcode
=> $rcv_err_barcode[$cnt] };
152 $template->param( receive_error
=> 1 ,
153 error_loop
=> $error_loop,
157 my $cfstr = "%.2f"; # currency format string -- could get this from currency table.
158 my @parcelitems = GetParcel
($booksellerid, $invoice, $datereceived->output('iso'));
159 my $countlines = scalar @parcelitems;
161 my $totalfreight = 0;
162 my $totalquantity = 0;
165 my @loop_received = ();
167 for (my $i = 0 ; $i < $countlines ; $i++) {
169 #$total=($parcelitems[$i]->{'unitprice'} + $parcelitems[$i]->{'freight'}) * $parcelitems[$i]->{'quantityreceived'}; #weird, are the freight fees counted by book? (pierre)
170 $total = ($parcelitems[$i]->{'unitprice'}) * $parcelitems[$i]->{'quantityreceived'}; #weird, are the freight fees counted by book? (pierre)
171 $parcelitems[$i]->{'unitprice'} += 0;
173 %line = %{ $parcelitems[$i] };
174 $line{invoice
} = $invoice;
176 $line{total
} = sprintf($cfstr, $total);
177 $line{booksellerid
} = $booksellerid;
178 push @loop_received, \
%line;
179 $totalprice += $parcelitems[$i]->{'unitprice'};
180 $line{unitprice
} = sprintf($cfstr, $parcelitems[$i]->{'unitprice'});
182 my $suggestion = GetSuggestionInfoFromBiblionumber
($line{biblionumber
});
183 $line{suggestionid
} = $suggestion->{suggestionid
};
184 $line{surnamesuggestedby
} = $suggestion->{surnamesuggestedby
};
185 $line{firstnamesuggestedby
} = $suggestion->{firstnamesuggestedby
};
187 #double FIXME - totalfreight is redefined later.
189 # 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..
190 if ($i > 0 && $totalfreight != $parcelitems[$i]->{'freight'}) {
191 warn "FREIGHT CHARGE MISMATCH!!";
193 $totalfreight = $parcelitems[$i]->{'freight'};
194 $totalquantity += $parcelitems[$i]->{'quantityreceived'};
198 my $pendingorders = GetPendingOrders
($booksellerid);
199 my $countpendings = scalar @
$pendingorders;
201 # pending orders totals
202 my ($totalPunitprice, $totalPquantity, $totalPecost, $totalPqtyrcvd);
204 my @loop_orders = ();
205 for (my $i = 0 ; $i < $countpendings ; $i++) {
207 %line = %{$pendingorders->[$i]};
210 $line{quantityreceived
}+=0;
212 $totalPunitprice += $line{unitprice
};
213 $totalPquantity +=$line{quantity
};
214 $totalPqtyrcvd +=$line{quantityreceived
};
215 $totalPecost += $line{ecost
};
216 $line{ecost
} = sprintf("%.2f",$line{ecost
});
217 $line{ordertotal
} = sprintf("%.2f",$line{ecost
}*$line{quantity
});
218 $line{unitprice
} = sprintf("%.2f",$line{unitprice
});
219 $line{invoice
} = $invoice;
221 $line{total
} = $total;
222 $line{booksellerid
} = $booksellerid;
223 $ordergrandtotal += $line{ecost
} * $line{quantity
};
225 my $biblionumber = $line{'biblionumber'};
226 my $countbiblio = CountBiblioInOrders
($biblionumber);
227 my $ordernumber = $line{'ordernumber'};
228 my @subscriptions = GetSubscriptionsId
($biblionumber);
229 my $itemcount = GetItemsCount
($biblionumber);
230 my $holds = GetHolds
($biblionumber);
231 my @items = GetItemnumbersFromOrder
( $ordernumber );
233 foreach my $item (@items){
234 my $nb = GetItemHolds
($biblionumber, $item);
240 my $suggestion = GetSuggestionInfoFromBiblionumber
($line{biblionumber
});
241 $line{suggestionid
} = $suggestion->{suggestionid
};
242 $line{surnamesuggestedby
} = $suggestion->{surnamesuggestedby
};
243 $line{firstnamesuggestedby
} = $suggestion->{firstnamesuggestedby
};
245 # if the biblio is not in other orders and if there is no items elsewhere and no subscriptions and no holds we can then show the link "Delete order and Biblio" see bug 5680
246 $line{can_del_bib
} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
247 $line{items
} = ($itemcount) - (scalar @items);
248 $line{left_item
} = 1 if $line{items
} >= 1;
249 $line{left_biblio
} = 1 if $countbiblio > 1;
250 $line{biblios
} = $countbiblio - 1;
251 $line{left_subscription
} = 1 if scalar @subscriptions >= 1;
252 $line{subscriptions
} = scalar @subscriptions;
253 $line{left_holds
} = 1 if $holds >= 1;
254 $line{left_holds_on_order
} = 1 if $line{left_holds
}==1 && ($line{items
} == 0 || $itemholds );
255 $line{holds
} = $holds;
256 $line{holds_on_order
} = $itemholds?
$itemholds:$holds if $line{left_holds_on_order
};
259 push @loop_orders, \
%line if ($i >= $startfrom and $i < $startfrom + $resultsperpage);
261 $freight = $totalfreight unless $freight;
263 my $count = $countpendings;
265 if ($count>$resultsperpage){
267 my $displayprev=$startfrom;
268 if(($count - ($startfrom+$resultsperpage)) > 0 ) {
273 for (my $i=1; $i<$count/$resultsperpage+1; $i++) {
275 ($startfrom/$resultsperpage==($i-1)) && ($highlight=1);
276 push @numbers, { number
=> $i,
277 highlight
=> $highlight ,
278 startfrom
=> ($i-1)*$resultsperpage};
281 my $from = $startfrom*$resultsperpage+1;
283 if($count < (($startfrom+1)*$resultsperpage)){
286 $to = (($startfrom+1)*$resultsperpage);
288 $template->param(numbers
=>\
@numbers,
289 displaynext
=>$displaynext,
290 displayprev
=>$displayprev,
291 nextstartfrom
=>(($startfrom+$resultsperpage<$count)?
$startfrom+$resultsperpage:$count),
292 prevstartfrom
=>(($startfrom-$resultsperpage>0)?
$startfrom-$resultsperpage:0)
296 #$totalfreight=$freight;
297 $tototal = $tototal + $freight;
301 datereceived
=> $datereceived->output('iso'),
302 invoicedatereceived
=> $datereceived->output('iso'),
303 formatteddatereceived
=> $datereceived->output(),
304 name
=> $bookseller->{'name'},
305 booksellerid
=> $booksellerid,
309 countreceived
=> $countlines,
310 loop_received
=> \
@loop_received,
311 countpending
=> $countpendings,
312 loop_orders
=> \
@loop_orders,
313 totalprice
=> sprintf($cfstr, $totalprice),
314 totalfreight
=> $totalfreight,
315 totalquantity
=> $totalquantity,
316 tototal
=> sprintf($cfstr, $tototal),
317 ordergrandtotal
=> sprintf($cfstr, $ordergrandtotal),
319 grandtot
=> sprintf($cfstr, $tototal + $gst),
320 totalPunitprice
=> sprintf("%.2f", $totalPunitprice),
321 totalPquantity
=> $totalPquantity,
322 totalPqtyrcvd
=> $totalPqtyrcvd,
323 totalPecost
=> sprintf("%.2f", $totalPecost),
324 resultsperpage
=> $resultsperpage,
326 output_html_with_http_headers
$input, $cookie, $template->output;