Bug 32779: Add tests
[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 it 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 Modern::Perl;
70 use CGI qw ( -utf8 );
71 use C4::Auth qw( get_template_and_user );
72 use C4::Output qw( output_html_with_http_headers );
73
74 use C4::Acquisition qw( GetInvoices GetInvoice AddInvoice );
75 use C4::Budgets qw( GetBudgetHierarchy GetBudget CanUserUseBudget );
76
77 use Koha::Acquisition::Booksellers;
78 use Koha::Acquisition::Invoices;
79 use Koha::AdditionalFields;
80 use Koha::DateUtils qw( dt_from_string );
81
82 my $input          = CGI->new;
83 my $booksellerid     = $input->param('booksellerid');
84 my $order          = $input->param('orderby') || 'shipmentdate desc';
85 my $startfrom      = $input->param('startfrom');
86 my $code           = $input->param('filter');
87 my $datefrom       = $input->param('datefrom');
88 my $dateto         = $input->param('dateto');
89 my $resultsperpage = $input->param('resultsperpage');
90 my $op             = $input->param('op');
91 $resultsperpage ||= 20;
92
93 our ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
94     {   template_name   => 'acqui/parcels.tt',
95         query           => $input,
96         type            => 'intranet',
97         flagsrequired   => { acquisition => 'order_receive' },
98     }
99 );
100
101 my $invoicenumber = $input->param('invoice');
102 my $shipmentcost = $input->param('shipmentcost');
103 my $shipmentcost_budgetid = $input->param('shipmentcost_budgetid');
104 my $shipmentdate = $input->param('shipmentdate');
105
106 if ( $op and $op eq 'new' ) {
107     if ( C4::Context->preference('AcqWarnOnDuplicateInvoice') ) {
108         my @invoices = GetInvoices(
109             supplierid    => $booksellerid,
110             invoicenumber => $invoicenumber,
111         );
112         if ( scalar @invoices > 0 ) {
113             $template->{'VARS'}->{'duplicate_invoices'} = \@invoices;
114             $template->{'VARS'}->{'invoicenumber'}      = $invoicenumber;
115             $template->{'VARS'}->{'shipmentdate'}       = $shipmentdate;
116             $template->{'VARS'}->{'shipmentcost'}       = $shipmentcost;
117             $template->{'VARS'}->{'shipmentcost_budgetid'} =
118               $shipmentcost_budgetid;
119         }
120     }
121     $op = 'confirm' unless $template->{'VARS'}->{'duplicate_invoices'};
122 }
123
124 if ($op and $op eq 'confirm') {
125     my $invoiceid = AddInvoice(
126         invoicenumber => $invoicenumber,
127         booksellerid => $booksellerid,
128         shipmentdate => $shipmentdate,
129         shipmentcost => $shipmentcost,
130         shipmentcost_budgetid => $shipmentcost_budgetid,
131     );
132     if (defined $invoiceid) {
133
134         my @additional_fields;
135         my $invoice_fields = Koha::AdditionalFields->search({ tablename => 'aqinvoices' });
136         while ( my $field = $invoice_fields->next ) {
137             my $value = $input->param('additional_field_' . $field->id);
138             if (defined $value) {
139                 push @additional_fields, {
140                     id    => $field->id,
141                     value => $value,
142                 };
143             }
144         }
145         if (@additional_fields) {
146             my $invoice = Koha::Acquisition::Invoices->find( $invoiceid );
147             $invoice->set_additional_fields(\@additional_fields);
148         }
149         # Successful 'Add'
150         print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");
151         exit 0;
152     } else {
153         $template->param(error_failed_to_create_invoice => 1);
154     }
155 }
156
157 $template->param(
158     available_additional_fields => [ Koha::AdditionalFields->search({ tablename => 'aqinvoices' })->as_list ]
159 );
160
161 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
162 my @parcels = GetInvoices(
163     supplierid => $booksellerid,
164     invoicenumber => $code,
165     ( $datefrom ? ( shipmentdatefrom => $datefrom ) : () ),
166     ( $dateto   ? ( shipmentdateto   => $dateto   ) : () ),
167     order_by => $order
168 );
169 my $count_parcels = @parcels;
170
171 # multi page display gestion
172 $startfrom ||= 0;
173 if ( $count_parcels > $resultsperpage ) {
174     set_page_navigation( $count_parcels, $startfrom, $resultsperpage );
175 }
176 my $loopres = [];
177
178 my $next_page_start = $startfrom + $resultsperpage;
179 my $last_row = ( $next_page_start < $count_parcels  ) ? $next_page_start - 1 : $count_parcels - 1;
180 for my $i ( $startfrom .. $last_row) {
181     my $p = $parcels[$i];
182
183     push @{$loopres},
184       { number           => $i + 1,
185         invoiceid        => $p->{invoiceid},
186         code             => $p->{invoicenumber},
187         nullcode         => $p->{invoicenumber} eq 'NULL',
188         emptycode        => $p->{invoicenumber} eq q{},
189         raw_datereceived => $p->{shipmentdate},
190         datereceived     => $p->{shipmentdate},
191         bibcount         => $p->{receivedbiblios} || 0,
192         reccount         => $p->{receiveditems} || 0,
193         itemcount        => $p->{itemsexpected} || 0,
194       };
195 }
196 if ($count_parcels) {
197     $template->param( searchresults => $loopres, count => $count_parcels );
198 }
199
200 # build budget list
201 my $budget_loop = [];
202 my $budgets = GetBudgetHierarchy;
203 foreach my $r (@{$budgets}) {
204     next unless (CanUserUseBudget($loggedinuser, $r, $flags));
205     push @{$budget_loop}, {
206         b_id  => $r->{budget_id},
207         b_txt => $r->{budget_name},
208         b_active => $r->{budget_period_active},
209     };
210 }
211
212 @{$budget_loop} =
213   sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
214
215
216 $template->param(
217     orderby                  => $order,
218     filter                   => $code,
219     datefrom                 => $datefrom,
220     dateto                   => $dateto,
221     resultsperpage           => $resultsperpage,
222     name                     => $bookseller->name,
223     shipmentdate_today       => dt_from_string,
224     booksellerid             => $booksellerid,
225     GST                      => C4::Context->preference('TaxRates'),
226     budgets                  => $budget_loop,
227 );
228
229 output_html_with_http_headers $input, $cookie, $template->output;
230
231 sub set_page_navigation {
232     my ( $total_rows, $startfrom, $resultsperpage ) = @_;
233     my $displaynext = 0;
234     my $displayprev = $startfrom;
235     my $next_row    = $startfrom + $resultsperpage;
236     my $prev_row    = $startfrom - $resultsperpage;
237
238     if ( $total_rows - $next_row > 0 ) {
239         $displaynext = 1;
240     }
241
242     # set up index numbers for paging
243     my $numbers = [];
244     if ( $total_rows > $resultsperpage ) {
245         my $pages = $total_rows / $resultsperpage;
246         if ( $total_rows % $resultsperpage ) {
247             ++$pages;
248         }
249
250         # set up page indexes for at max 15 pages
251         my $max_idx = ( $pages < 15 ) ? $pages : 15;
252         my $current_page = ( $startfrom / $resultsperpage ) - 1;
253         for my $idx ( 1 .. $max_idx ) {
254             push @{$numbers},
255               { number    => $idx,
256                 startfrom => ( $idx - 1 ) * $resultsperpage,
257                 highlight => ( $idx == $current_page ),
258               };
259         }
260     }
261
262     $template->param(
263         numbers     => $numbers,
264         displaynext => $displaynext,
265         displayprev => $displayprev,
266         nextstartfrom => ( ( $next_row < $total_rows ) ? $next_row : $total_rows ),
267         prevstartfrom => ( ( $prev_row > 0 ) ? $prev_row : 0 )
268     );
269     return;
270 }