Bug 6874: Add unit tests for C4::UploadedFiles
[koha.git] / acqui / parcels.pl
1 #!/usr/bin/perl
2
3
4 #script to show display basket of orders
5
6
7 # Copyright 2000-2002 Katipo Communications
8 # Copyright 2008-2009 BibLibre SARL
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24
25 =head1 NAME
26
27 parcels.pl
28
29 =head1 DESCRIPTION
30
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.
33
34 =head1 CGI PARAMETERS
35
36 =over 4
37
38 =item booksellerid
39
40 To know the supplier this script has to show orders.
41
42 =item orderby
43
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
50
51 =item filter
52
53 =item datefrom
54
55 To filter on date
56
57 =item dateto
58
59 To filter on date
60
61 =item resultsperpage
62
63 To know how many results have to be display / page.
64
65 =back
66
67 =cut
68
69 use strict;
70 use warnings;
71 use CGI qw ( -utf8 );
72 use C4::Auth;
73 use C4::Output;
74
75 use C4::Acquisition;
76 use C4::Budgets;
77
78 use Koha::Acquisition::Bookseller;
79 use Koha::DateUtils qw( output_pref dt_from_string );
80
81 my $input          = CGI->new;
82 my $booksellerid     = $input->param('booksellerid');
83 my $order          = $input->param('orderby') || 'shipmentdate desc';
84 my $startfrom      = $input->param('startfrom');
85 my $code           = $input->param('filter');
86 my $datefrom       = $input->param('datefrom');
87 my $dateto         = $input->param('dateto');
88 my $resultsperpage = $input->param('resultsperpage');
89 my $op             = $input->param('op');
90 $resultsperpage ||= 20;
91
92 our ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
93     {   template_name   => 'acqui/parcels.tt',
94         query           => $input,
95         type            => 'intranet',
96         authnotrequired => 0,
97         flagsrequired   => { acquisition => 'order_receive' },
98         debug           => 1,
99     }
100 );
101
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');
108 }
109
110 if ( $op and $op eq 'new' ) {
111     if ( C4::Context->preference('AcqWarnOnDuplicateInvoice') ) {
112         my @invoices = GetInvoices(
113             supplierid    => $booksellerid,
114             invoicenumber => $invoicenumber,
115         );
116         if ( scalar @invoices > 0 ) {
117             $template->{'VARS'}->{'duplicate_invoices'} = \@invoices;
118             $template->{'VARS'}->{'invoicenumber'}      = $invoicenumber;
119             $template->{'VARS'}->{'shipmentdate'}       = $shipmentdate;
120             $template->{'VARS'}->{'shipmentcost'}       = $shipmentcost;
121             $template->{'VARS'}->{'shipmentcost_budgetid'} =
122               $shipmentcost_budgetid;
123         }
124     }
125     $op = 'confirm' unless $template->{'VARS'}->{'duplicate_invoices'};
126 }
127 if ($op and $op eq 'confirm') {
128     my $invoiceid = AddInvoice(
129         invoicenumber => $invoicenumber,
130         booksellerid => $booksellerid,
131         shipmentdate => $shipmentdate,
132         shipmentcost => $shipmentcost,
133         shipmentcost_budgetid => $shipmentcost_budgetid,
134     );
135     if(defined $invoiceid) {
136         # Successful 'Add'
137         print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");
138         exit 0;
139     } else {
140         $template->param(error_failed_to_create_invoice => 1);
141     }
142 }
143
144 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
145 my @parcels = GetInvoices(
146     supplierid => $booksellerid,
147     invoicenumber => $code,
148     ( $datefrom ? ( shipmentdatefrom => output_pref({ dt => dt_from_string($datefrom), dateformat => 'iso' }) ) : () ),
149     ( $dateto   ? ( shipmentdateto   => output_pref({ dt => dt_from_string($dateto),   dateformat => 'iso' }) )    : () ),
150     order_by => $order
151 );
152 my $count_parcels = @parcels;
153
154 # multi page display gestion
155 $startfrom ||= 0;
156 if ( $count_parcels > $resultsperpage ) {
157     set_page_navigation( $count_parcels, $startfrom, $resultsperpage );
158 }
159 my $loopres = [];
160
161 my $next_page_start = $startfrom + $resultsperpage;
162 my $last_row = ( $next_page_start < $count_parcels  ) ? $next_page_start - 1 : $count_parcels - 1;
163 for my $i ( $startfrom .. $last_row) {
164     my $p = $parcels[$i];
165
166     push @{$loopres},
167       { number           => $i + 1,
168         invoiceid        => $p->{invoiceid},
169         code             => $p->{invoicenumber},
170         nullcode         => $p->{invoicenumber} eq 'NULL',
171         emptycode        => $p->{invoicenumber} eq q{},
172         raw_datereceived => $p->{shipmentdate},
173         datereceived     => $p->{shipmentdate},
174         bibcount         => $p->{receivedbiblios} || 0,
175         reccount         => $p->{receiveditems} || 0,
176         itemcount        => $p->{itemsexpected} || 0,
177       };
178 }
179 if ($count_parcels) {
180     $template->param( searchresults => $loopres, count => $count_parcels );
181 }
182
183 # build budget list
184 my $budget_loop = [];
185 my $budgets = GetBudgetHierarchy;
186 foreach my $r (@{$budgets}) {
187     next unless (CanUserUseBudget($loggedinuser, $r, $flags));
188     if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
189         next;
190     }
191     push @{$budget_loop}, {
192         b_id  => $r->{budget_id},
193         b_txt => $r->{budget_name},
194         b_active => $r->{budget_period_active},
195     };
196 }
197
198 @{$budget_loop} =
199   sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
200
201
202 $template->param(
203     orderby                  => $order,
204     filter                   => $code,
205     datefrom                 => $datefrom,
206     dateto                   => $dateto,
207     resultsperpage           => $resultsperpage,
208     name                     => $bookseller->{'name'},
209     shipmentdate_today       => C4::Dates->new()->output(),
210     booksellerid             => $booksellerid,
211     GST                      => C4::Context->preference('gist'),
212     budgets                  => $budget_loop,
213 );
214
215 output_html_with_http_headers $input, $cookie, $template->output;
216
217 sub set_page_navigation {
218     my ( $total_rows, $startfrom, $resultsperpage ) = @_;
219     my $displaynext = 0;
220     my $displayprev = $startfrom;
221     my $next_row    = $startfrom + $resultsperpage;
222     my $prev_row    = $startfrom - $resultsperpage;
223
224     if ( $total_rows - $next_row > 0 ) {
225         $displaynext = 1;
226     }
227
228     # set up index numbers for paging
229     my $numbers = [];
230     if ( $total_rows > $resultsperpage ) {
231         my $pages = $total_rows / $resultsperpage;
232         if ( $total_rows % $resultsperpage ) {
233             ++$pages;
234         }
235
236         # set up page indexes for at max 15 pages
237         my $max_idx = ( $pages < 15 ) ? $pages : 15;
238         my $current_page = ( $startfrom / $resultsperpage ) - 1;
239         for my $idx ( 1 .. $max_idx ) {
240             push @{$numbers},
241               { number    => $idx,
242                 startfrom => ( $idx - 1 ) * $resultsperpage,
243                 highlight => ( $idx == $current_page ),
244               };
245         }
246     }
247
248     $template->param(
249         numbers     => $numbers,
250         displaynext => $displaynext,
251         displayprev => $displayprev,
252         nextstartfrom => ( ( $next_row < $total_rows ) ? $next_row : $total_rows ),
253         prevstartfrom => ( ( $prev_row > 0 ) ? $prev_row : 0 )
254     );
255     return;
256 }