Merge remote-tracking branch 'kc/new/bug_5616' into kcmaster
[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 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.
16 #
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.
20 #
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.
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 supplierid
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     * aqorders.booksellerinvoicenumber
48     * datereceived
49     * aqorders.booksellerinvoicenumber 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;
72 use C4::Auth;
73 use C4::Output;
74
75 use C4::Dates qw/format_date/;
76 use C4::Acquisition;
77 use C4::Bookseller qw/ GetBookSellerFromId /;
78
79 my $input          = CGI->new;
80 my $supplierid     = $input->param('supplierid');
81 my $order          = $input->param('orderby') || 'datereceived desc';
82 my $startfrom      = $input->param('startfrom');
83 my $code           = $input->param('filter');
84 my $datefrom       = $input->param('datefrom');
85 my $dateto         = $input->param('dateto');
86 my $resultsperpage = $input->param('resultsperpage');
87 $resultsperpage ||= 20;
88
89 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
90     {   template_name   => 'acqui/parcels.tmpl',
91         query           => $input,
92         type            => 'intranet',
93         authnotrequired => 0,
94         flagsrequired   => { acquisition => 'order_receive' },
95         debug           => 1,
96     }
97 );
98
99 my $bookseller = GetBookSellerFromId($supplierid);
100 my @parcels = GetParcels( $supplierid, $order, $code, $datefrom, $dateto );
101 my $count_parcels = @parcels;
102
103 # multi page display gestion
104 $startfrom ||= 0;
105 if ( $count_parcels > $resultsperpage ) {
106     set_page_navigation( $count_parcels, $startfrom, $resultsperpage );
107 }
108 my $loopres = [];
109
110 my $next_page_start = $startfrom + $resultsperpage;
111 my $last_row = ( $next_page_start < $count_parcels  ) ? $next_page_start - 1 : $count_parcels - 1;
112 for my $i ( $startfrom .. $last_row) {
113     my $p = $parcels[$i];
114
115     push @{$loopres},
116       { number           => $i + 1,
117         code             => $p->{booksellerinvoicenumber},
118         nullcode         => $p->{booksellerinvoicenumber} eq 'NULL',
119         emptycode        => $p->{booksellerinvoicenumber} eq q{},
120         raw_datereceived => $p->{datereceived},
121         datereceived     => format_date( $p->{datereceived} ),
122         bibcount         => $p->{biblio},
123         reccount         => $p->{itemsreceived},
124         itemcount        => $p->{itemsexpected},
125       };
126 }
127 if ($count_parcels) {
128     $template->param( searchresults => $loopres, count => $count_parcels );
129 }
130 $template->param(
131     orderby                  => $order,
132     filter                   => $code,
133     datefrom                 => $datefrom,
134     dateto                   => $dateto,
135     resultsperpage           => $resultsperpage,
136     name                     => $bookseller->{'name'},
137     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
138     datereceived_today       => C4::Dates->new()->output(),
139     supplierid               => $supplierid,
140     GST                      => C4::Context->preference('gist'),
141 );
142
143 output_html_with_http_headers $input, $cookie, $template->output;
144
145 sub set_page_navigation {
146     my ( $total_rows, $startfrom, $resultsperpage ) = @_;
147     my $displaynext = 0;
148     my $displayprev = $startfrom;
149     my $next_row    = $startfrom + $resultsperpage;
150     my $prev_row    = $startfrom - $resultsperpage;
151
152     if ( $total_rows - $next_row > 0 ) {
153         $displaynext = 1;
154     }
155
156     # set up index numbers for paging
157     my $numbers = [];
158     if ( $total_rows > $resultsperpage ) {
159         my $pages = $total_rows / $resultsperpage;
160         if ( $total_rows % $resultsperpage ) {
161             ++$pages;
162         }
163
164         # set up page indexes for at max 15 pages
165         my $max_idx = ( $pages < 15 ) ? $pages : 15;
166         my $current_page = ( $startfrom / $resultsperpage ) - 1;
167         for my $idx ( 1 .. $max_idx ) {
168             push @{$numbers},
169               { number    => $idx,
170                 startfrom => ( $idx - 1 ) * $resultsperpage,
171                 highlight => ( $idx == $current_page ),
172               };
173         }
174     }
175
176     $template->param(
177         numbers     => $numbers,
178         displaynext => $displaynext,
179         displayprev => $displayprev,
180         nextstartfrom => ( ( $next_row < $total_rows ) ? $next_row : $total_rows ),
181         prevstartfrom => ( ( $prev_row > 0 ) ? $prev_row : 0 )
182     );
183     return;
184 }