Bug 28704: Library MARCOrgCode field needs maxlength attribute
[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;
72 use C4::Output;
73
74 use C4::Acquisition;
75 use C4::Budgets;
76
77 use Koha::Acquisition::Booksellers;
78 use Koha::DateUtils qw( output_pref dt_from_string );
79
80 my $input          = CGI->new;
81 my $booksellerid     = $input->param('booksellerid');
82 my $order          = $input->param('orderby') || 'shipmentdate 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;
90
91 our ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
92     {   template_name   => 'acqui/parcels.tt',
93         query           => $input,
94         type            => 'intranet',
95         flagsrequired   => { acquisition => 'order_receive' },
96         debug           => 1,
97     }
98 );
99
100 my $invoicenumber = $input->param('invoice');
101 my $shipmentcost = $input->param('shipmentcost');
102 my $shipmentcost_budgetid = $input->param('shipmentcost_budgetid');
103 my $shipmentdate = $input->param('shipmentdate');
104 $shipmentdate and $shipmentdate = output_pref({ str => $shipmentdate, dateformat => 'iso', dateonly => 1 });
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 if ($op and $op eq 'confirm') {
124     my $invoiceid = AddInvoice(
125         invoicenumber => $invoicenumber,
126         booksellerid => $booksellerid,
127         shipmentdate => $shipmentdate,
128         shipmentcost => $shipmentcost,
129         shipmentcost_budgetid => $shipmentcost_budgetid,
130     );
131     if(defined $invoiceid) {
132         # Successful 'Add'
133         print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");
134         exit 0;
135     } else {
136         $template->param(error_failed_to_create_invoice => 1);
137     }
138 }
139
140 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
141 my @parcels = GetInvoices(
142     supplierid => $booksellerid,
143     invoicenumber => $code,
144     ( $datefrom ? ( shipmentdatefrom => output_pref({ dt => dt_from_string($datefrom), dateformat => 'iso' }) ) : () ),
145     ( $dateto   ? ( shipmentdateto   => output_pref({ dt => dt_from_string($dateto),   dateformat => 'iso' }) )    : () ),
146     order_by => $order
147 );
148 my $count_parcels = @parcels;
149
150 # multi page display gestion
151 $startfrom ||= 0;
152 if ( $count_parcels > $resultsperpage ) {
153     set_page_navigation( $count_parcels, $startfrom, $resultsperpage );
154 }
155 my $loopres = [];
156
157 my $next_page_start = $startfrom + $resultsperpage;
158 my $last_row = ( $next_page_start < $count_parcels  ) ? $next_page_start - 1 : $count_parcels - 1;
159 for my $i ( $startfrom .. $last_row) {
160     my $p = $parcels[$i];
161
162     push @{$loopres},
163       { number           => $i + 1,
164         invoiceid        => $p->{invoiceid},
165         code             => $p->{invoicenumber},
166         nullcode         => $p->{invoicenumber} eq 'NULL',
167         emptycode        => $p->{invoicenumber} eq q{},
168         raw_datereceived => $p->{shipmentdate},
169         datereceived     => $p->{shipmentdate},
170         bibcount         => $p->{receivedbiblios} || 0,
171         reccount         => $p->{receiveditems} || 0,
172         itemcount        => $p->{itemsexpected} || 0,
173       };
174 }
175 if ($count_parcels) {
176     $template->param( searchresults => $loopres, count => $count_parcels );
177 }
178
179 # build budget list
180 my $budget_loop = [];
181 my $budgets = GetBudgetHierarchy;
182 foreach my $r (@{$budgets}) {
183     next unless (CanUserUseBudget($loggedinuser, $r, $flags));
184     push @{$budget_loop}, {
185         b_id  => $r->{budget_id},
186         b_txt => $r->{budget_name},
187         b_active => $r->{budget_period_active},
188     };
189 }
190
191 @{$budget_loop} =
192   sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
193
194
195 $template->param(
196     orderby                  => $order,
197     filter                   => $code,
198     datefrom                 => $datefrom,
199     dateto                   => $dateto,
200     resultsperpage           => $resultsperpage,
201     name                     => $bookseller->name,
202     shipmentdate_today       => dt_from_string,
203     booksellerid             => $booksellerid,
204     GST                      => C4::Context->preference('TaxRates'),
205     budgets                  => $budget_loop,
206 );
207
208 output_html_with_http_headers $input, $cookie, $template->output;
209
210 sub set_page_navigation {
211     my ( $total_rows, $startfrom, $resultsperpage ) = @_;
212     my $displaynext = 0;
213     my $displayprev = $startfrom;
214     my $next_row    = $startfrom + $resultsperpage;
215     my $prev_row    = $startfrom - $resultsperpage;
216
217     if ( $total_rows - $next_row > 0 ) {
218         $displaynext = 1;
219     }
220
221     # set up index numbers for paging
222     my $numbers = [];
223     if ( $total_rows > $resultsperpage ) {
224         my $pages = $total_rows / $resultsperpage;
225         if ( $total_rows % $resultsperpage ) {
226             ++$pages;
227         }
228
229         # set up page indexes for at max 15 pages
230         my $max_idx = ( $pages < 15 ) ? $pages : 15;
231         my $current_page = ( $startfrom / $resultsperpage ) - 1;
232         for my $idx ( 1 .. $max_idx ) {
233             push @{$numbers},
234               { number    => $idx,
235                 startfrom => ( $idx - 1 ) * $resultsperpage,
236                 highlight => ( $idx == $current_page ),
237               };
238         }
239     }
240
241     $template->param(
242         numbers     => $numbers,
243         displaynext => $displaynext,
244         displayprev => $displayprev,
245         nextstartfrom => ( ( $next_row < $total_rows ) ? $next_row : $total_rows ),
246         prevstartfrom => ( ( $prev_row > 0 ) ? $prev_row : 0 )
247     );
248     return;
249 }