Bug 15184: Prepare the ground - Move order search filters
[koha.git] / acqui / histsearch.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2004 Biblibre
6 # Parts copyright 2011 Catalyst IT Ltd.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 =head1 NAME
22
23 histsearch.pl
24
25 =head1 DESCRIPTION
26
27 this script offer a interface to search among order.
28
29 =head1 CGI PARAMETERS
30
31 =over 4
32
33 =item title
34 if the script has to filter the results on title.
35
36 =item author
37 if the script has to filter the results on author.
38
39 =item name
40 if the script has to filter the results on supplier.
41
42 =item fromplacedon
43 to filter on started date.
44
45 =item toplacedon
46 to filter on ended date.
47
48 =back
49
50 =cut
51
52 use Modern::Perl;
53 use CGI qw ( -utf8 );
54 use C4::Auth;    # get_template_and_user
55 use C4::Output;
56 use C4::Acquisition;
57 use C4::Debug;
58 use C4::Koha;
59 use Koha::DateUtils;
60
61 my $input = new CGI;
62 my $do_search               = $input->param('do_search') || 0;
63
64 my $dbh = C4::Context->dbh;
65 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
66     {
67         template_name   => "acqui/histsearch.tt",
68         query           => $input,
69         type            => "intranet",
70         authnotrequired => 0,
71         flagsrequired   => { acquisition => '*' },
72         debug           => 1,
73     }
74 );
75
76 my $filters = {
77     basket                  => scalar $input->param('basket'),
78     title                   => scalar $input->param('title'),
79     author                  => scalar $input->param('author'),
80     isbn                    => scalar $input->param('isbn'),
81     name                    => scalar $input->param('name'),
82     ean                     => scalar $input->param('ean'),
83     basketgroupname         => scalar $input->param('basketgroupname'),
84     budget                  => scalar $input->param('budget'),
85     booksellerinvoicenumber => scalar $input->param('booksellerinvoicenumber'),
86     budget                  => scalar $input->param('budget'),
87     orderstatus             => scalar $input->param('orderstatus'),
88     ordernumber             => scalar $input->param('ordernumber'),
89     search_children_too     => scalar $input->param('search_children_too'),
90     created_by              => scalar $input->multi_param('created_by'),
91 };
92 my $from_placed_on = eval { dt_from_string( scalar $input->param('from') ) } || dt_from_string;
93 my $to_placed_on   = eval { dt_from_string( scalar $input->param('to')   ) } || dt_from_string;
94 unless ( $input->param('from') ) {
95     # Fill the form with year-1
96     $from_placed_on->subtract( years => 1 );
97 }
98 $filters->{from_placed_on} = output_pref( { dt => $from_placed_on, dateformat => 'iso', dateonly => 1 } ),
99 $filters->{to_placed_on} = output_pref( { dt => $to_placed_on, dateformat => 'iso', dateonly => 1 } ),
100
101 my $order_loop;
102 # If we're supplied any value then we do a search. Otherwise we don't.
103 if ($do_search) {
104     $order_loop = GetHistory(%$filters);
105 }
106
107 my $budgetperiods = C4::Budgets::GetBudgetPeriods;
108 my $bp_loop = $budgetperiods;
109 for my $bp ( @{$budgetperiods} ) {
110     my $hierarchy = C4::Budgets::GetBudgetHierarchy( $$bp{budget_period_id} );
111     for my $budget ( @{$hierarchy} ) {
112         $$budget{budget_display_name} = sprintf("%s", ">" x $$budget{depth} . $$budget{budget_name});
113     }
114     $$bp{hierarchy} = $hierarchy;
115 }
116
117 $template->param(
118     order_loop  => $order_loop,
119     filters     => $filters,
120     bp_loop     => $bp_loop,
121     search_done => $do_search,
122 );
123
124 output_html_with_http_headers $input, $cookie, $template->output;