Merge branch 'bug_9925' into 3.14-master
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 strict;
53 #use warnings; FIXME - Bug 2505
54 use CGI;
55 use C4::Auth;    # get_template_and_user
56 use C4::Output;
57 use C4::Acquisition;
58 use C4::Dates;
59 use C4::Debug;
60
61 my $input = new CGI;
62 my $title                   = $input->param( 'title');
63 utf8::decode($title);
64 my $author                  = $input->param('author');
65 utf8::decode($author);
66 my $isbn                    = $input->param('isbn');
67 utf8::decode($isbn);
68 my $name                    = $input->param( 'name' );
69 utf8::decode($name);
70 my $ean                     = $input->param('ean');
71 utf8::decode($ean);
72 my $basket                  = $input->param( 'basket' );
73 utf8::decode($basket);
74 my $basketgroupname             = $input->param('basketgroupname');
75 utf8::decode($basketgroupname);
76 my $booksellerinvoicenumber = $input->param( 'booksellerinvoicenumber' );
77 utf8::decode($booksellerinvoicenumber);
78
79 my $do_search               = $input->param('do_search') || 0;
80 my $from_placed_on          = C4::Dates->new($input->param('from'));
81 my $to_placed_on            = C4::Dates->new($input->param('to'));
82 if ( not $input->param('from') ) {
83     # FIXME Dirty but we can't sent a Date::Calc to C4::Dates ?
84     # We would use a function like Add_Delta_YM(-1, 0, 0);
85     $$from_placed_on{dmy_arrayref}[5] -= 1;
86 }
87
88 my $dbh = C4::Context->dbh;
89 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
90     {
91         template_name   => "acqui/histsearch.tmpl",
92         query           => $input,
93         type            => "intranet",
94         authnotrequired => 0,
95         flagsrequired   => { acquisition => '*' },
96         debug           => 1,
97     }
98 );
99
100 my ( $from_iso, $to_iso, $d );
101 if ( $d = $input->param('from') ) {
102     $from_iso = C4::Dates->new($d)->output('iso');
103 }
104 if ( $d = $input->param('iso') ) {
105     $to_iso = C4::Dates->new($d)->output('iso');
106 }
107
108 my ( $order_loop, $total_qty, $total_price, $total_qtyreceived );
109 # If we're supplied any value then we do a search. Otherwise we don't.
110 if ($do_search) {
111     ( $order_loop, $total_qty, $total_price, $total_qtyreceived ) = GetHistory(
112         title => $title,
113         author => $author,
114         isbn   => $isbn,
115         ean   => $ean,
116         name => $name,
117         from_placed_on => $from_iso,
118         to_placed_on => $to_iso,
119         basket => $basket,
120         booksellerinvoicenumber => $booksellerinvoicenumber,
121         basketgroupname => $basketgroupname,
122     );
123 }
124
125 my $from_date = $from_placed_on ? $from_placed_on->output('syspref') : undef;
126 my $to_date = $to_placed_on ? $to_placed_on->output('syspref') : undef;
127
128 $template->param(
129     suggestions_loop        => $order_loop,
130     total_qty               => $total_qty,
131     total_qtyreceived       => $total_qtyreceived,
132     total_price             => sprintf( "%.2f", $total_price ),
133     numresults              => $order_loop ? scalar(@$order_loop) : undef,
134     title                   => $title,
135     author                  => $author,
136     isbn                    => $isbn,
137     ean                     => $ean,
138     name                    => $name,
139     basket                  => $basket,
140     booksellerinvoicenumber => $booksellerinvoicenumber,
141     basketgroupname         => $basketgroupname,
142     from_placed_on          => $from_date,
143     to_placed_on            => $to_date,
144     debug                   => $debug || $input->param('debug') || 0,
145     uc(C4::Context->preference("marcflavour")) => 1
146 );
147
148 output_html_with_http_headers $input, $cookie, $template->output;