Bug 9416: add new order vendor note field
[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 use C4::Branch;
61 use C4::Koha;
62
63 my $input = new CGI;
64 my $title                   = $input->param( 'title');
65 my $author                  = $input->param('author');
66 my $isbn                    = $input->param('isbn');
67 my $name                    = $input->param( 'name' );
68 my $ean                     = $input->param('ean');
69 my $basket                  = $input->param( 'basket' );
70 my $basketgroupname             = $input->param('basketgroupname');
71 my $booksellerinvoicenumber = $input->param( 'booksellerinvoicenumber' );
72 my $do_search               = $input->param('do_search') || 0;
73 my $from_placed_on          = C4::Dates->new($input->param('from'));
74 my $to_placed_on            = C4::Dates->new($input->param('to'));
75 my $budget                  = $input->param( 'budget' );
76 my $orderstatus             = $input->param( 'orderstatus' );
77
78 if ( not $input->param('from') ) {
79     # FIXME Dirty but we can't sent a Date::Calc to C4::Dates ?
80     # We would use a function like Add_Delta_YM(-1, 0, 0);
81     $$from_placed_on{dmy_arrayref}[5] -= 1;
82 }
83
84 my $dbh = C4::Context->dbh;
85 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
86     {
87         template_name   => "acqui/histsearch.tmpl",
88         query           => $input,
89         type            => "intranet",
90         authnotrequired => 0,
91         flagsrequired   => { acquisition => '*' },
92         debug           => 1,
93     }
94 );
95
96 my ( $from_iso, $to_iso, $d );
97 if ( $d = $input->param('from') ) {
98     $from_iso = C4::Dates->new($d)->output('iso');
99 }
100 if ( $d = $input->param('iso') ) {
101     $to_iso = C4::Dates->new($d)->output('iso');
102 }
103
104 my ( $order_loop, $total_qty, $total_price, $total_qtyreceived );
105 # If we're supplied any value then we do a search. Otherwise we don't.
106 if ($do_search) {
107     ( $order_loop, $total_qty, $total_price, $total_qtyreceived ) = GetHistory(
108         title => $title,
109         author => $author,
110         isbn   => $isbn,
111         ean   => $ean,
112         name => $name,
113         from_placed_on => $from_iso,
114         to_placed_on => $to_iso,
115         basket => $basket,
116         booksellerinvoicenumber => $booksellerinvoicenumber,
117         basketgroupname => $basketgroupname,
118         budget => $budget,
119         orderstatus => $orderstatus,
120     );
121 }
122
123 my $from_date = $from_placed_on ? $from_placed_on->output('syspref') : undef;
124 my $to_date = $to_placed_on ? $to_placed_on->output('syspref') : undef;
125
126 my $budgetperiods = C4::Budgets::GetBudgetPeriods;
127 my $bp_loop = $budgetperiods;
128 for my $bp ( @{$budgetperiods} ) {
129     my $hierarchy = C4::Budgets::GetBudgetHierarchy( $$bp{budget_period_id} );
130     for my $budget ( @{$hierarchy} ) {
131         $$budget{budget_display_name} = sprintf("%s", ">" x $$budget{depth} . $$budget{budget_name});
132     }
133     $$bp{hierarchy} = $hierarchy;
134 }
135
136 $template->param(
137     order_loop              => $order_loop,
138     total_qty               => $total_qty,
139     total_qtyreceived       => $total_qtyreceived,
140     total_price             => sprintf( "%.2f", $total_price ),
141     numresults              => $order_loop ? scalar(@$order_loop) : undef,
142     title                   => $title,
143     author                  => $author,
144     isbn                    => $isbn,
145     ean                     => $ean,
146     name                    => $name,
147     basket                  => $basket,
148     booksellerinvoicenumber => $booksellerinvoicenumber,
149     basketgroupname         => $basketgroupname,
150     from_placed_on          => $from_date,
151     to_placed_on            => $to_date,
152     orderstatus             => $orderstatus,
153     budget_id               => $budget,
154     bp_loop                 => $bp_loop,
155     search_done             => $do_search,
156     debug                   => $debug || $input->param('debug') || 0,
157     uc(C4::Context->preference("marcflavour")) => 1
158 );
159
160 output_html_with_http_headers $input, $cookie, $template->output;