BUG 8652: Add a default value for the lateorders
[koha.git] / acqui / lateorders.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2005 Biblibre
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 lateorders.pl
23
24 =head1 DESCRIPTION
25
26 this script shows late orders for a specific supplier, branch and delay
27 given on input arg.
28
29 =head1 CGI PARAMETERS
30
31 =over 4
32
33 =item booksellerid
34 To know on which supplier this script have to display late order.
35
36 =item delay
37 To know the time boundary. Default value is 30 days.
38
39 =item branch
40 To know on which branch this script have to display late order.
41
42 =back
43
44 =cut
45
46 use Modern::Perl;
47 use CGI;
48 use C4::Bookseller qw( GetBooksellersWithLateOrders );
49 use C4::Auth;
50 use C4::Koha;
51 use C4::Output;
52 use C4::Context;
53 use C4::Acquisition;
54 use C4::Letters;
55 use C4::Branch; # GetBranches
56 use Koha::DateUtils;
57
58 my $input = new CGI;
59 my ($template, $loggedinuser, $cookie) = get_template_and_user({
60         template_name => "acqui/lateorders.tmpl",
61         query => $input,
62          type => "intranet",
63         authnotrequired => 0,
64         flagsrequired => {acquisition => 'order_receive'},
65         debug => 1,
66 });
67
68 my $booksellerid = $input->param('booksellerid') || undef; # we don't want "" or 0
69 my $delay        = $input->param('delay');
70
71 # Get the "date from" param if !defined is today
72 my $estimateddeliverydatefrom = $input->param('estimateddeliverydatefrom');
73 my $estimateddeliverydateto   = $input->param('estimateddeliverydateto');
74
75 my $estimateddeliverydatefrom_dt =
76   $estimateddeliverydatefrom
77   ? dt_from_string($estimateddeliverydatefrom)
78   : undef;
79
80 # Get the "date to" param. If it is not defined and $delay is not defined too, it is the today's date.
81 my $estimateddeliverydateto_dt = dt_from_string($estimateddeliverydateto);
82
83 # Format the output of "date from" and "date to"
84 if ($estimateddeliverydatefrom) {
85     $estimateddeliverydatefrom = output_pref($estimateddeliverydatefrom_dt);
86     $estimateddeliverydatefrom =~ s/ \d\d:\d\d$//;
87 }
88 if ($estimateddeliverydateto) {
89     $estimateddeliverydateto = output_pref($estimateddeliverydateto_dt);
90     $estimateddeliverydateto =~ s/ \d\d:\d\d$//;
91 }
92
93 my $branch     = $input->param('branch');
94 my $op         = $input->param('op');
95
96 my @errors = ();
97 if ( $delay and not $delay =~ /^\d{1,3}$/ ) {
98     push @errors, {delay_digits => 1, bad_delay => $delay};
99 }
100
101 if ($op and $op eq "send_alert"){
102     my @ordernums = $input->param("claim_for");# FIXME: Fallback values?
103     my $err;
104     eval {
105         $err = SendAlerts( 'claimacquisition', \@ordernums, $input->param("letter_code") );    # FIXME: Fallback value?
106         if ( not ref $err or not exists $err->{error} ) {
107             AddClaim ( $_ ) for @ordernums;
108         }
109     };
110     if ( $@ ) {
111         $template->param(error_claim => $@);
112     } elsif ( ref $err and exists $err->{error} and $err->{error} eq "no_email" ) {
113         $template->{VARS}->{'error_claim'} = "no_email";
114     } else {
115         $template->{VARS}->{'info_claim'} = 1;
116     }
117 }
118
119 my @parameters = ( $delay, $branch );
120 if ($estimateddeliverydatefrom_dt) {
121     push @parameters, $estimateddeliverydatefrom_dt->ymd();
122 }
123 else {
124     push @parameters, undef;
125 }
126 if ($estimateddeliverydateto_dt) {
127     push @parameters, $estimateddeliverydateto_dt->ymd();
128 }
129 my %supplierlist = GetBooksellersWithLateOrders(@parameters);
130
131 my (@sloopy);   # supplier loop
132 foreach (keys %supplierlist){
133         push @sloopy, (($booksellerid and $booksellerid eq $_ )            ?
134                                         {id=>$_, name=>$supplierlist{$_}, selected=>1} :
135                                         {id=>$_, name=>$supplierlist{$_}} )            ;
136 }
137 $template->param(SUPPLIER_LOOP => \@sloopy);
138
139 $template->param(Supplier=>$supplierlist{$booksellerid}) if ($booksellerid);
140 $template->param(booksellerid=>$booksellerid) if ($booksellerid);
141
142 @parameters =
143   ( $delay, $booksellerid, $branch );
144 if ($estimateddeliverydatefrom_dt) {
145     push @parameters, $estimateddeliverydatefrom_dt->ymd();
146 }
147 else {
148     push @parameters, undef;
149 }
150 if ($estimateddeliverydateto_dt) {
151     push @parameters, $estimateddeliverydateto_dt->ymd();
152 }
153 my @lateorders = GetLateOrders( @parameters );
154
155 my $total;
156 foreach (@lateorders){
157         $total += $_->{subtotal};
158 }
159
160 my @letters;
161 my $letters=GetLetters("claimacquisition");
162 foreach (keys %$letters){
163         push @letters, {code=>$_,name=>$letters->{$_}};
164 }
165 $template->param(letters=>\@letters) if (@letters);
166
167 $template->param(ERROR_LOOP => \@errors) if (@errors);
168 $template->param(
169         lateorders => \@lateorders,
170         delay => $delay,
171     estimateddeliverydatefrom => $estimateddeliverydatefrom,
172     estimateddeliverydateto   => $estimateddeliverydateto,
173         total => $total,
174         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
175     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
176 );
177 output_html_with_http_headers $input, $cookie, $template->output;