Bug 11369 - Updating the jquery.cookie.js-plugin
[koha.git] / acqui / lateorders.pl
blob1a64a4cb72193b2ce64606536795ee4f598a5e4c
1 #!/usr/bin/perl
4 # Copyright 2005 Biblibre
5 # This file is part of Koha.
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.
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.
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.
20 =head1 NAME
22 lateorders.pl
24 =head1 DESCRIPTION
26 this script shows late orders for a specific supplier, branch and delay
27 given on input arg.
29 =head1 CGI PARAMETERS
31 =over 4
33 =item booksellerid
34 To know on which supplier this script have to display late order.
36 =item delay
37 To know the time boundary. Default value is 30 days.
39 =item branch
40 To know on which branch this script have to display late order.
42 =back
44 =cut
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;
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 });
68 my $booksellerid = $input->param('booksellerid') || undef; # we don't want "" or 0
69 my $delay = $input->param('delay') // 0;
71 # Get the "date from" param if !defined is today
72 my $estimateddeliverydatefrom = $input->param('estimateddeliverydatefrom');
73 my $estimateddeliverydateto = $input->param('estimateddeliverydateto');
75 my $estimateddeliverydatefrom_dt =
76 $estimateddeliverydatefrom
77 ? dt_from_string($estimateddeliverydatefrom)
78 : undef;
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;
87 # Format the output of "date from" and "date to"
88 if ($estimateddeliverydatefrom_dt) {
89 $estimateddeliverydatefrom = output_pref({dt => $estimateddeliverydatefrom_dt, dateonly => 1});
91 if ($estimateddeliverydateto_dt) {
92 $estimateddeliverydateto = output_pref({dt => $estimateddeliverydateto_dt, dateonly => 1});
95 my $branch = $input->param('branch');
96 my $op = $input->param('op');
98 my @errors = ();
99 if ( $delay and not $delay =~ /^\d{1,3}$/ ) {
100 push @errors, {delay_digits => 1, bad_delay => $delay};
103 if ($op and $op eq "send_alert"){
104 my @ordernums = $input->param("ordernumber");# FIXME: Fallback values?
105 my $err;
106 eval {
107 $err = SendAlerts( 'claimacquisition', \@ordernums, $input->param("letter_code") ); # FIXME: Fallback value?
108 if ( not ref $err or not exists $err->{error} ) {
109 AddClaim ( $_ ) for @ordernums;
113 if ( $@ ) {
114 $template->param(error_claim => $@);
115 } elsif ( ref $err and exists $err->{error} and $err->{error} eq "no_email" ) {
116 $template->{VARS}->{'error_claim'} = "no_email";
117 } else {
118 $template->{VARS}->{'info_claim'} = 1;
122 my @parameters = ( $delay );
123 push @parameters, $estimateddeliverydatefrom_dt
124 ? $estimateddeliverydatefrom_dt->ymd()
125 : undef;
127 push @parameters, $estimateddeliverydateto_dt
128 ? $estimateddeliverydateto_dt->ymd()
129 : undef;
131 my %supplierlist = GetBooksellersWithLateOrders(@parameters);
133 my (@sloopy); # supplier loop
134 foreach (keys %supplierlist){
135 push @sloopy, (($booksellerid and $booksellerid eq $_ ) ?
136 {id=>$_, name=>$supplierlist{$_}, selected=>1} :
137 {id=>$_, name=>$supplierlist{$_}} ) ;
139 $template->param(SUPPLIER_LOOP => \@sloopy);
141 $template->param(Supplier=>$supplierlist{$booksellerid}) if ($booksellerid);
142 $template->param(booksellerid=>$booksellerid) if ($booksellerid);
144 @parameters =
145 ( $delay, $booksellerid, $branch );
146 if ($estimateddeliverydatefrom_dt) {
147 push @parameters, $estimateddeliverydatefrom_dt->ymd();
149 else {
150 push @parameters, undef;
152 if ($estimateddeliverydateto_dt) {
153 push @parameters, $estimateddeliverydateto_dt->ymd();
155 my @lateorders = GetLateOrders( @parameters );
157 my $total;
158 foreach (@lateorders){
159 $total += $_->{subtotal};
162 my @letters;
163 my $letters=GetLetters("claimacquisition");
164 foreach (keys %$letters){
165 push @letters, {code=>$_,name=>$letters->{$_}};
167 $template->param(letters=>\@letters) if (@letters);
169 $template->param(ERROR_LOOP => \@errors) if (@errors);
170 $template->param(
171 lateorders => \@lateorders,
172 delay => $delay,
173 estimateddeliverydatefrom => $estimateddeliverydatefrom,
174 estimateddeliverydateto => $estimateddeliverydateto,
175 total => $total,
176 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
178 output_html_with_http_headers $input, $cookie, $template->output;