Bug 8350: warning in logs when searching for nonexistent ISBN
[koha.git] / acqui / parcels.pl
blob4f2849d43f8862ef8e8873dd459584686775a513
1 #!/usr/bin/perl
4 #script to show display basket of orders
7 # Copyright 2000-2002 Katipo Communications
8 # Copyright 2008-2009 BibLibre SARL
10 # This file is part of Koha.
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.
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.
21 # You should have received a copy of the GNU General Public License along
22 # with Koha; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 =head1 NAME
27 parcels.pl
29 =head1 DESCRIPTION
31 This script shows all orders/parcels receipt or pending for a given supplier.
32 It allows to write an order/parcels as 'received' when he arrives.
34 =head1 CGI PARAMETERS
36 =over 4
38 =item booksellerid
40 To know the supplier this script has to show orders.
42 =item orderby
44 sort list of order by 'orderby'.
45 Orderby can be equals to
46 * datereceived desc (default value)
47 * invoicenumber
48 * datereceived
49 * invoicenumber desc
51 =item filter
53 =item datefrom
55 To filter on date
57 =item dateto
59 To filter on date
61 =item resultsperpage
63 To know how many results have to be display / page.
65 =back
67 =cut
69 use strict;
70 use warnings;
71 use CGI;
72 use C4::Auth;
73 use C4::Output;
75 use C4::Dates qw/format_date/;
76 use C4::Acquisition;
77 use C4::Bookseller qw/ GetBookSellerFromId /;
78 use C4::Budgets;
80 my $input = CGI->new;
81 my $booksellerid = $input->param('booksellerid');
82 my $order = $input->param('orderby') || 'datereceived desc';
83 my $startfrom = $input->param('startfrom');
84 my $code = $input->param('filter');
85 my $datefrom = $input->param('datefrom');
86 my $dateto = $input->param('dateto');
87 my $resultsperpage = $input->param('resultsperpage');
88 my $op = $input->param('op');
89 $resultsperpage ||= 20;
91 our ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
92 { template_name => 'acqui/parcels.tmpl',
93 query => $input,
94 type => 'intranet',
95 authnotrequired => 0,
96 flagsrequired => { acquisition => 'order_receive' },
97 debug => 1,
101 if($op and $op eq 'new') {
102 my $invoicenumber = $input->param('invoice');
103 my $shipmentdate = $input->param('shipmentdate');
104 my $shipmentcost = $input->param('shipmentcost');
105 my $shipmentcost_budgetid = $input->param('shipmentcost_budgetid');
106 if($shipmentdate) {
107 $shipmentdate = C4::Dates->new($shipmentdate)->output('iso');
109 my $invoiceid = AddInvoice(
110 invoicenumber => $invoicenumber,
111 booksellerid => $booksellerid,
112 shipmentdate => $shipmentdate,
113 shipmentcost => $shipmentcost,
114 shipmentcost_budgetid => $shipmentcost_budgetid,
116 if(defined $invoiceid) {
117 # Successful 'Add'
118 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");
119 exit 0;
120 } else {
121 $template->param(error_failed_to_create_invoice => 1);
125 my $bookseller = GetBookSellerFromId($booksellerid);
126 my @parcels = GetInvoices(
127 supplierid => $booksellerid,
128 invoicenumber => $code,
129 shipmentdatefrom => $datefrom,
130 shipmentdateto => $dateto,
131 order_by => $order
133 my $count_parcels = @parcels;
135 # multi page display gestion
136 $startfrom ||= 0;
137 if ( $count_parcels > $resultsperpage ) {
138 set_page_navigation( $count_parcels, $startfrom, $resultsperpage );
140 my $loopres = [];
142 my $next_page_start = $startfrom + $resultsperpage;
143 my $last_row = ( $next_page_start < $count_parcels ) ? $next_page_start - 1 : $count_parcels - 1;
144 for my $i ( $startfrom .. $last_row) {
145 my $p = $parcels[$i];
147 push @{$loopres},
148 { number => $i + 1,
149 invoiceid => $p->{invoiceid},
150 code => $p->{invoicenumber},
151 nullcode => $p->{invoicenumber} eq 'NULL',
152 emptycode => $p->{invoicenumber} eq q{},
153 raw_datereceived => $p->{shipmentdate},
154 datereceived => format_date( $p->{shipmentdate} ),
155 bibcount => $p->{receivedbiblios} || 0,
156 reccount => $p->{receiveditems} || 0,
157 itemcount => $p->{itemsexpected} || 0,
160 if ($count_parcels) {
161 $template->param( searchresults => $loopres, count => $count_parcels );
164 my $budgets = GetBudgets();
165 my @budgets_loop;
166 foreach my $budget (@$budgets) {
167 next unless CanUserUseBudget($loggedinuser, $budget, $flags);
168 push @budgets_loop, $budget;
171 $template->param(
172 orderby => $order,
173 filter => $code,
174 datefrom => $datefrom,
175 dateto => $dateto,
176 resultsperpage => $resultsperpage,
177 name => $bookseller->{'name'},
178 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
179 shipmentdate_today => C4::Dates->new()->output(),
180 booksellerid => $booksellerid,
181 GST => C4::Context->preference('gist'),
182 budgets => \@budgets_loop,
185 output_html_with_http_headers $input, $cookie, $template->output;
187 sub set_page_navigation {
188 my ( $total_rows, $startfrom, $resultsperpage ) = @_;
189 my $displaynext = 0;
190 my $displayprev = $startfrom;
191 my $next_row = $startfrom + $resultsperpage;
192 my $prev_row = $startfrom - $resultsperpage;
194 if ( $total_rows - $next_row > 0 ) {
195 $displaynext = 1;
198 # set up index numbers for paging
199 my $numbers = [];
200 if ( $total_rows > $resultsperpage ) {
201 my $pages = $total_rows / $resultsperpage;
202 if ( $total_rows % $resultsperpage ) {
203 ++$pages;
206 # set up page indexes for at max 15 pages
207 my $max_idx = ( $pages < 15 ) ? $pages : 15;
208 my $current_page = ( $startfrom / $resultsperpage ) - 1;
209 for my $idx ( 1 .. $max_idx ) {
210 push @{$numbers},
211 { number => $idx,
212 startfrom => ( $idx - 1 ) * $resultsperpage,
213 highlight => ( $idx == $current_page ),
218 $template->param(
219 numbers => $numbers,
220 displaynext => $displaynext,
221 displayprev => $displayprev,
222 nextstartfrom => ( ( $next_row < $total_rows ) ? $next_row : $total_rows ),
223 prevstartfrom => ( ( $prev_row > 0 ) ? $prev_row : 0 )
225 return;