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