Bug 32482: (follow-up) Add markup comments
[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 @suppliers      = Koha::Acquisition::Booksellers->search( undef, { order_by => { -asc => 'name' } } )->as_list;
109 my $suppliers_loop = [];
110 my $suppliername;
111 foreach (@suppliers) {
112     my $selected = 0;
113     if ($supplierid && $supplierid == $_->id ) {
114         $selected = 1;
115         $suppliername = $_->name;
116     }
117     push @{$suppliers_loop},
118       {
119         suppliername => $_->name,
120         booksellerid   => $_->id,
121         selected     => $selected,
122       };
123 }
124
125 my $budgets = GetBudgets();
126 my @budgets_loop;
127 foreach my $budget (@$budgets) {
128     push @budgets_loop, $budget if CanUserUseBudget( $loggedinuser, $budget, $flags );
129 }
130
131 my (@openedinvoices, @closedinvoices);
132 for my $sub ( @{$invoices} ) {
133     unless ( $sub->{closedate} ) {
134         push @openedinvoices, $sub
135     } else {
136         push @closedinvoices, $sub
137     }
138 }
139
140 $template->{'VARS'}->{'budgets_loop'} = \@budgets_loop;
141
142 $template->param(
143     openedinvoices => \@openedinvoices,
144     closedinvoices => \@closedinvoices,
145     do_search => ( $op and $op eq 'do_search' ) ? 1 : 0,
146     invoices => $invoices,
147     invoicenumber   => $invoicenumber,
148     booksellerid    => $supplierid,
149     suppliername    => $suppliername,
150     shipmentdatefrom => $shipmentdatefrom,
151     shipmentdateto   => $shipmentdateto,
152     billingdatefrom => $billingdatefrom,
153     billingdateto   => $billingdateto,
154     isbneanissn     => $isbneanissn,
155     title           => $title,
156     author          => $author,
157     publisher       => $publisher,
158     publicationyear => $publicationyear,
159     branch          => $branch,
160     suppliers_loop  => $suppliers_loop,
161 );
162
163 output_html_with_http_headers $input, $cookie, $template->output;