Bug 30359: GetBudgetHierarchy is slow on order receive page
[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::Auth qw( get_template_and_user );
49 use C4::Output qw( output_html_with_http_headers );
50 use C4::Context;
51 use C4::Letters qw( SendAlerts GetLetters );
52 use Koha::DateUtils qw( dt_from_string );
53 use Koha::Acquisition::Orders qw( filter_by_lates );
54 use Koha::CsvProfiles;
55
56 my $input = CGI->new;
57 my ($template, $loggedinuser, $cookie) = get_template_and_user(
58     {
59         template_name   => "acqui/lateorders.tt",
60         query           => $input,
61         type            => "intranet",
62         flagsrequired   => { acquisition => 'order_receive' },
63     }
64 );
65
66 my $booksellerid = $input->param('booksellerid');
67 my $delay        = $input->param('delay') // 0;
68
69 # Get the "date from" param if !defined is today
70 my $estimateddeliverydatefrom = $input->param('estimateddeliverydatefrom');
71 my $estimateddeliverydateto   = $input->param('estimateddeliverydateto');
72
73 # Get the "date to" param. If it is not defined and $delay is not defined too, it is the today's date.
74 $estimateddeliverydateto ||=
75   ( not defined $delay and not defined $estimateddeliverydatefrom )
76   ? dt_from_string()
77   : undef;
78
79 my $branch     = $input->param('branch');
80 my $op         = $input->param('op');
81
82 my @errors = ();
83 if ( $delay and not $delay =~ /^\d{1,3}$/ ) {
84     push @errors, {delay_digits => 1, bad_delay => $delay};
85 }
86
87 if ($op and $op eq "send_alert"){
88     my @ordernums = $input->multi_param("ordernumber");
89     my $err;
90     eval {
91         $err = SendAlerts( 'claimacquisition', \@ordernums, $input->param("letter_code") );
92         if ( not ref $err or not exists $err->{error} ) {
93             Koha::Acquisition::Orders->find($_)->claim() for @ordernums;
94         }
95     };
96
97     if ( ref $err and exists $err->{error} and $err->{error} eq "no_email" ) {
98         $template->{VARS}->{'error_claim'} = "no_email";
99     } elsif ( ref $err and exists $err->{error} and $err->{error} eq "no_order_selected"){
100         $template->{VARS}->{'error_claim'} = "no_order_selected";
101     } elsif ( $@ or ref $err and exists $err->{error} ) {
102         $template->param(error_claim => $@ || $err->{error});
103     } else {
104         $template->{VARS}->{'info_claim'} = 1;
105     }
106 }
107
108 my @lateorders = Koha::Acquisition::Orders->filter_by_lates(
109     {
110         delay        => $delay,
111         (
112             $estimateddeliverydatefrom
113             ? ( estimated_from => dt_from_string($estimateddeliverydatefrom, 'iso') )
114             : ()
115         ),
116         (
117             $estimateddeliverydateto
118             ? ( estimated_to => dt_from_string($estimateddeliverydateto, 'iso') )
119             : ()
120         )
121     },
122 )->as_list;
123
124 my $booksellers = Koha::Acquisition::Booksellers->search(
125     {
126         id => {
127             -in => [ map { $_->basket->booksellerid } @lateorders ]
128         },
129     }
130 );
131
132 @lateorders = grep { $_->basket->booksellerid eq $booksellerid } @lateorders if $booksellerid;
133
134 my $letters = GetLetters({ module => "claimacquisition" });
135
136 $template->param(ERROR_LOOP => \@errors) if (@errors);
137 $template->param(
138     lateorders => \@lateorders,
139     booksellers => $booksellers,
140     bookseller_filter => ( $booksellerid ? $booksellers->find($booksellerid) : undef),
141         delay => $delay,
142     letters => $letters,
143     estimateddeliverydatefrom => $estimateddeliverydatefrom,
144     estimateddeliverydateto   => $estimateddeliverydateto,
145         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
146     csv_profiles         => Koha::CsvProfiles->search({ type => 'sql', used_for => 'late_orders' }),
147 );
148 output_html_with_http_headers $input, $cookie, $template->output;