Bug 7113: Standardize vendor id name in templates and scripts
[koha.git] / acqui / lateorders.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18
19 =head1 NAME
20
21 lateorders.pl
22
23 =head1 DESCRIPTION
24
25 this script shows late orders for a specific supplier, branch and delay
26 given on input arg.
27
28 =head1 CGI PARAMETERS
29
30 =over 4
31
32 =item booksellerid
33 To know on which supplier this script have to display late order.
34
35 =item delay
36 To know the time boundary. Default value is 30 days.
37
38 =item branch
39 To know on which branch this script have to display late order.
40
41 =back
42
43 =cut
44
45 use strict;
46 use warnings;
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
57 my $input = new CGI;
58 my ($template, $loggedinuser, $cookie) = get_template_and_user({
59         template_name => "acqui/lateorders.tmpl",
60         query => $input,
61          type => "intranet",
62         authnotrequired => 0,
63         flagsrequired => {acquisition => 'order_receive'},
64         debug => 1,
65 });
66
67 my $booksellerid = $input->param('booksellerid') || undef; # we don't want "" or 0
68 my $delay      = $input->param('delay');
69 my $branch     = $input->param('branch');
70 my $op         = $input->param('op');
71
72 my @errors = ();
73 $delay = 30 unless defined $delay;
74 unless ($delay =~ /^\d{1,3}$/) {
75         push @errors, {delay_digits => 1, bad_delay => $delay};
76         $delay = 30;    #default value for delay
77 }
78
79 if ($op and $op eq "send_alert"){
80     my @ordernums = $input->param("claim_for");# FIXME: Fallback values?
81     my $err;
82     eval {
83         $err = SendAlerts( 'claimacquisition', \@ordernums, $input->param("letter_code") );    # FIXME: Fallback value?
84         AddClaim ( $_ ) for @ordernums;
85     };
86     if ( $@ ) {
87         $template->param(error_claim => $@);
88     } elsif ( defined $err->{error} and $err->{error} eq "no_email" ) {
89         $template->{VARS}->{'error_claim'} = "no_email";
90     } else {
91         $template->{VARS}->{'info_claim'} = 1;
92     }
93 }
94
95 my %supplierlist = GetBooksellersWithLateOrders($delay);
96 my (@sloopy);   # supplier loop
97 foreach (keys %supplierlist){
98         push @sloopy, (($booksellerid and $booksellerid eq $_ )            ?
99                                         {id=>$_, name=>$supplierlist{$_}, selected=>1} :
100                                         {id=>$_, name=>$supplierlist{$_}} )            ;
101 }
102 $template->param(SUPPLIER_LOOP => \@sloopy);
103
104 $template->param(Supplier=>$supplierlist{$booksellerid}) if ($booksellerid);
105 $template->param(booksellerid=>$booksellerid) if ($booksellerid);
106
107 my @lateorders = GetLateOrders($delay,$booksellerid,$branch);
108
109 my $total;
110 foreach (@lateorders){
111         $total += $_->{subtotal};
112 }
113
114 my @letters;
115 my $letters=GetLetters("claimacquisition");
116 foreach (keys %$letters){
117         push @letters, {code=>$_,name=>$letters->{$_}};
118 }
119 $template->param(letters=>\@letters) if (@letters);
120
121 $template->param(ERROR_LOOP => \@errors) if (@errors);
122 $template->param(
123         lateorders => \@lateorders,
124         delay => $delay,
125         total => $total,
126         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
127 );
128 output_html_with_http_headers $input, $cookie, $template->output;