Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / acqui / invoices.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011 BibLibre SARL
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 =head1 NAME
20
21 invoices.pl
22
23 =head1 DESCRIPTION
24
25 Search for invoices
26
27 =cut
28
29 use Modern::Perl;
30
31 use CGI qw ( -utf8 );
32 use C4::Auth;
33 use C4::Output;
34
35 use C4::Acquisition qw/GetInvoices/;
36 use C4::Budgets;
37 use Koha::DateUtils;
38 use Koha::Acquisition::Booksellers;
39
40 my $input = CGI->new;
41 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
42     {
43         template_name   => 'acqui/invoices.tt',
44         query           => $input,
45         type            => 'intranet',
46         flagsrequired   => { 'acquisition' => '*' },
47     }
48 );
49
50 my $invoicenumber    = $input->param('invoicenumber');
51 my $supplierid       = $input->param('supplierid');
52 my $shipmentdatefrom = $input->param('shipmentdatefrom');
53 my $shipmentdateto   = $input->param('shipmentdateto');
54 my $billingdatefrom  = $input->param('billingdatefrom');
55 my $billingdateto    = $input->param('billingdateto');
56 my $isbneanissn      = $input->param('isbneanissn');
57 my $title            = $input->param('title');
58 my $author           = $input->param('author');
59 my $publisher        = $input->param('publisher');
60 my $publicationyear  = $input->param('publicationyear');
61 my $branch           = $input->param('branch');
62 my $message_id       = $input->param('message_id');
63 my $op               = $input->param('op');
64
65 $shipmentdatefrom and $shipmentdatefrom = eval { dt_from_string( $shipmentdatefrom ) };
66 $shipmentdateto   and $shipmentdateto   = eval { dt_from_string( $shipmentdateto ) };
67 $billingdatefrom  and $billingdatefrom  = eval { dt_from_string( $billingdatefrom ) };
68 $billingdateto    and $billingdateto    = eval { dt_from_string( $billingdateto ) };
69
70 my $invoices = [];
71 if ( $op and $op eq 'do_search' ) {
72     @{$invoices} = GetInvoices(
73         invoicenumber    => $invoicenumber,
74         supplierid       => $supplierid,
75         shipmentdatefrom => $shipmentdatefrom ? output_pref( { str => $shipmentdatefrom, dateformat => 'iso' } ) : undef,
76         shipmentdateto   => $shipmentdateto   ? output_pref( { str => $shipmentdateto,   dateformat => 'iso' } ) : undef,
77         billingdatefrom  => $billingdatefrom  ? output_pref( { str => $billingdatefrom,  dateformat => 'iso' } ) : undef,
78         billingdateto    => $billingdateto    ? output_pref( { str => $billingdateto,    dateformat => 'iso' } ) : undef,
79         isbneanissn      => $isbneanissn,
80         title            => $title,
81         author           => $author,
82         publisher        => $publisher,
83         publicationyear  => $publicationyear,
84         branchcode       => $branch,
85         message_id       => $message_id,
86     );
87 }
88
89 # Build suppliers list
90 my @suppliers      = Koha::Acquisition::Booksellers->search( undef, { order_by => { -asc => 'name' } } );
91 my $suppliers_loop = [];
92 my $suppliername;
93 foreach (@suppliers) {
94     my $selected = 0;
95     if ($supplierid && $supplierid == $_->id ) {
96         $selected = 1;
97         $suppliername = $_->name;
98     }
99     push @{$suppliers_loop},
100       {
101         suppliername => $_->name,
102         booksellerid   => $_->id,
103         selected     => $selected,
104       };
105 }
106
107 my $budgets = GetBudgets();
108 my @budgets_loop;
109 foreach my $budget (@$budgets) {
110     push @budgets_loop, $budget if CanUserUseBudget( $loggedinuser, $budget, $flags );
111 }
112
113 my (@openedinvoices, @closedinvoices);
114 for my $sub ( @{$invoices} ) {
115     unless ( $sub->{closedate} ) {
116         push @openedinvoices, $sub
117     } else {
118         push @closedinvoices, $sub
119     }
120 }
121
122 $template->{'VARS'}->{'budgets_loop'} = \@budgets_loop;
123
124 $template->param(
125     openedinvoices => \@openedinvoices,
126     closedinvoices => \@closedinvoices,
127     do_search => ( $op and $op eq 'do_search' ) ? 1 : 0,
128     invoices => $invoices,
129     invoicenumber   => $invoicenumber,
130     booksellerid    => $supplierid,
131     suppliername    => $suppliername,
132     shipmentdatefrom => $shipmentdatefrom,
133     shipmentdateto   => $shipmentdateto,
134     billingdatefrom => $billingdatefrom,
135     billingdateto   => $billingdateto,
136     isbneanissn     => $isbneanissn,
137     title           => $title,
138     author          => $author,
139     publisher       => $publisher,
140     publicationyear => $publicationyear,
141     branch          => $branch,
142     suppliers_loop  => $suppliers_loop,
143 );
144
145 output_html_with_http_headers $input, $cookie, $template->output;