Bug 24663: [19.05.x] Remove authnotrequired if set to 0
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 qw ( -utf8 );
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 Koha::DateUtils;
56
57 my $input = new CGI;
58 my ($template, $loggedinuser, $cookie) = get_template_and_user(
59     {
60         template_name   => "acqui/lateorders.tt",
61         query           => $input,
62         type            => "intranet",
63         flagsrequired   => { acquisition => 'order_receive' },
64         debug           => 1,
65     }
66 );
67
68 my $booksellerid = $input->param('booksellerid') || undef; # we don't want "" or 0
69 my $delay        = $input->param('delay') // 0;
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 = $estimateddeliverydateto
82     ? dt_from_string($estimateddeliverydateto)
83     : ( not defined $delay and not defined $estimateddeliverydatefrom)
84         ? dt_from_string()
85         : undef;
86
87 # Format the output of "date from" and "date to"
88 if ($estimateddeliverydatefrom_dt) {
89     $estimateddeliverydatefrom = output_pref({dt => $estimateddeliverydatefrom_dt, dateonly => 1});
90 }
91 if ($estimateddeliverydateto_dt) {
92     $estimateddeliverydateto = output_pref({dt => $estimateddeliverydateto_dt, dateonly => 1});
93 }
94
95 my $branch     = $input->param('branch');
96 my $op         = $input->param('op');
97
98 my @errors = ();
99 if ( $delay and not $delay =~ /^\d{1,3}$/ ) {
100     push @errors, {delay_digits => 1, bad_delay => $delay};
101 }
102
103 if ($op and $op eq "send_alert"){
104     my @ordernums = $input->multi_param("ordernumber");
105     my $err;
106     eval {
107         $err = SendAlerts( 'claimacquisition', \@ordernums, $input->param("letter_code") );
108         if ( not ref $err or not exists $err->{error} ) {
109             AddClaim ( $_ ) for @ordernums;
110         }
111     };
112
113     if ( ref $err and exists $err->{error} and $err->{error} eq "no_email" ) {
114         $template->{VARS}->{'error_claim'} = "no_email";
115     } elsif ( ref $err and exists $err->{error} and $err->{error} eq "no_order_selected"){
116         $template->{VARS}->{'error_claim'} = "no_order_selected";
117     } elsif ( $@ or ref $err and exists $err->{error} ) {
118         $template->param(error_claim => $@ || $err->{error});
119     } else {
120         $template->{VARS}->{'info_claim'} = 1;
121     }
122 }
123
124 my @parameters = ( $delay );
125 push @parameters, $estimateddeliverydatefrom_dt
126     ? $estimateddeliverydatefrom_dt->ymd()
127     : undef;
128
129 push @parameters, $estimateddeliverydateto_dt
130     ? $estimateddeliverydateto_dt->ymd()
131     : undef;
132
133 my %supplierlist = GetBooksellersWithLateOrders(@parameters);
134
135 my (@sloopy);   # supplier loop
136 foreach( sort { $supplierlist{$a} cmp $supplierlist{$b} } keys %supplierlist ) {
137         push @sloopy, (($booksellerid and $booksellerid eq $_ )            ?
138                                         {id=>$_, name=>$supplierlist{$_}, selected=>1} :
139                                         {id=>$_, name=>$supplierlist{$_}} )            ;
140 }
141 $template->param(SUPPLIER_LOOP => \@sloopy);
142
143 $template->param(Supplier=>$supplierlist{$booksellerid}) if ($booksellerid);
144 $template->param(booksellerid=>$booksellerid) if ($booksellerid);
145
146 @parameters =
147   ( $delay, $booksellerid, $branch );
148 if ($estimateddeliverydatefrom_dt) {
149     push @parameters, $estimateddeliverydatefrom_dt->ymd();
150 }
151 else {
152     push @parameters, undef;
153 }
154 if ($estimateddeliverydateto_dt) {
155     push @parameters, $estimateddeliverydateto_dt->ymd();
156 }
157 my @lateorders = GetLateOrders( @parameters );
158
159 my $total;
160 foreach (@lateorders){
161         $total += $_->{subtotal};
162 }
163
164 my $letters = GetLetters({ module => "claimacquisition" });
165
166 $template->param(ERROR_LOOP => \@errors) if (@errors);
167 $template->param(
168         lateorders => \@lateorders,
169         delay => $delay,
170     letters => $letters,
171     estimateddeliverydatefrom => $estimateddeliverydatefrom,
172     estimateddeliverydateto   => $estimateddeliverydateto,
173         total => $total,
174         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
175 );
176 output_html_with_http_headers $input, $cookie, $template->output;