Bug 36233: Use select2 to load vendors on invoice search
[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 qw( get_template_and_user );
33 use C4::Output qw( output_html_with_http_headers );
34
35 use C4::Acquisition qw( GetInvoices GetInvoice );
36 use C4::Budgets qw( GetBudget GetBudgets CanUserUseBudget );
37 use Koha::DateUtils qw( dt_from_string );
38 use Koha::Acquisition::Booksellers;
39 use Koha::AdditionalFields;
40
41 my $input = CGI->new;
42 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
43     {
44         template_name   => 'acqui/invoices.tt',
45         query           => $input,
46         type            => 'intranet',
47         flagsrequired   => { 'acquisition' => '*' },
48     }
49 );
50
51 my $invoicenumber    = $input->param('invoicenumber');
52 my $supplierid       = $input->param('supplierid');
53 my $shipmentdatefrom = $input->param('shipmentdatefrom');
54 my $shipmentdateto   = $input->param('shipmentdateto');
55 my $billingdatefrom  = $input->param('billingdatefrom');
56 my $billingdateto    = $input->param('billingdateto');
57 my $isbneanissn      = $input->param('isbneanissn');
58 my $title            = $input->param('title');
59 my $author           = $input->param('author');
60 my $publisher        = $input->param('publisher');
61 my $publicationyear  = $input->param('publicationyear');
62 my $branch           = $input->param('branch');
63 my $message_id       = $input->param('message_id');
64 my $op               = $input->param('op');
65
66 my @additional_fields = Koha::AdditionalFields->search(
67     {   tablename  => 'aqinvoices',
68         searchable => 1
69     }
70 )->as_list;
71 my @additional_field_filters;
72 for my $field (@additional_fields) {
73     my $value = $input->param( 'additional_field_' . $field->id );
74     if ( defined $value and $value ne '' ) {
75         push @additional_field_filters,
76           { id    => $field->id,
77             value => $value,
78           };
79     }
80 }
81
82 my $invoices = [];
83 if ( $op and $op eq 'do_search' ) {
84     @{$invoices} = GetInvoices(
85         invoicenumber     => $invoicenumber,
86         supplierid        => $supplierid,
87         shipmentdatefrom  => $shipmentdatefrom,
88         shipmentdateto    => $shipmentdateto,
89         billingdatefrom   => $billingdatefrom,
90         billingdateto     => $billingdateto,
91         isbneanissn       => $isbneanissn,
92         title             => $title,
93         author            => $author,
94         publisher         => $publisher,
95         publicationyear   => $publicationyear,
96         branchcode        => $branch,
97         message_id        => $message_id,
98         additional_fields => \@additional_field_filters,
99     );
100 }
101
102 $template->param(
103     additional_field_filters      => { map { $_->{id} => $_->{value} } @additional_field_filters },
104     additional_fields_for_invoice => \@additional_fields,
105 );
106
107 # Build suppliers list
108 my $supplier;
109 $supplier = Koha::Acquisition::Booksellers->find($supplierid) if $supplierid;
110
111 my $budgets = GetBudgets();
112 my @budgets_loop;
113 foreach my $budget (@$budgets) {
114     push @budgets_loop, $budget if CanUserUseBudget( $loggedinuser, $budget, $flags );
115 }
116
117 my (@openedinvoices, @closedinvoices);
118 for my $sub ( @{$invoices} ) {
119     unless ( $sub->{closedate} ) {
120         push @openedinvoices, $sub
121     } else {
122         push @closedinvoices, $sub
123     }
124 }
125
126 $template->{'VARS'}->{'budgets_loop'} = \@budgets_loop;
127
128 $template->param(
129     openedinvoices   => \@openedinvoices,
130     closedinvoices   => \@closedinvoices,
131     do_search        => ( $op and $op eq 'do_search' ) ? 1 : 0,
132     invoices         => $invoices,
133     invoicenumber    => $invoicenumber,
134     booksellerid     => $supplierid,
135     supplier         => $supplier,
136     shipmentdatefrom => $shipmentdatefrom,
137     shipmentdateto   => $shipmentdateto,
138     billingdatefrom  => $billingdatefrom,
139     billingdateto    => $billingdateto,
140     isbneanissn      => $isbneanissn,
141     title            => $title,
142     author           => $author,
143     publisher        => $publisher,
144     publicationyear  => $publicationyear,
145     branch           => $branch,
146 );
147
148 output_html_with_http_headers $input, $cookie, $template->output;