Bug 4330 : Fixing FSF address and copyright headers for acqui/
[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 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.
11 #
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.
15 #
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.
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 strict;
47 use warnings;
48 use CGI;
49 use C4::Bookseller qw( GetBooksellersWithLateOrders );
50 use C4::Auth;
51 use C4::Koha;
52 use C4::Output;
53 use C4::Context;
54 use C4::Acquisition;
55 use C4::Letters;
56 use C4::Branch; # GetBranches
57
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 });
67
68 my $booksellerid = $input->param('booksellerid') || undef; # we don't want "" or 0
69 my $delay      = $input->param('delay');
70 my $estimateddeliverydatefrom = $input->param('estimateddeliverydatefrom');
71 my $estimateddeliverydateto   = $input->param('estimateddeliverydateto');
72 my $branch     = $input->param('branch');
73 my $op         = $input->param('op');
74
75 my @errors = ();
76 if ( $delay and not $delay =~ /^\d{1,3}$/ ) {
77     push @errors, {delay_digits => 1, bad_delay => $delay};
78 }
79
80 if ($op and $op eq "send_alert"){
81     my @ordernums = $input->param("claim_for");# FIXME: Fallback values?
82     my $err;
83     eval {
84         $err = SendAlerts( 'claimacquisition', \@ordernums, $input->param("letter_code") );    # FIXME: Fallback value?
85         if ( not ref $err or not exists $err->{error} ) {
86             AddClaim ( $_ ) for @ordernums;
87         }
88     };
89     if ( $@ ) {
90         $template->param(error_claim => $@);
91     } elsif ( ref $err and exists $err->{error} and $err->{error} eq "no_email" ) {
92         $template->{VARS}->{'error_claim'} = "no_email";
93     } else {
94         $template->{VARS}->{'info_claim'} = 1;
95     }
96 }
97
98 my %supplierlist = GetBooksellersWithLateOrders(
99     $delay,
100     $branch,
101     C4::Dates->new($estimateddeliverydatefrom)->output("iso"),
102     C4::Dates->new($estimateddeliverydateto)->output("iso")
103 );
104
105 my (@sloopy);   # supplier loop
106 foreach (keys %supplierlist){
107         push @sloopy, (($booksellerid and $booksellerid eq $_ )            ?
108                                         {id=>$_, name=>$supplierlist{$_}, selected=>1} :
109                                         {id=>$_, name=>$supplierlist{$_}} )            ;
110 }
111 $template->param(SUPPLIER_LOOP => \@sloopy);
112
113 $template->param(Supplier=>$supplierlist{$booksellerid}) if ($booksellerid);
114 $template->param(booksellerid=>$booksellerid) if ($booksellerid);
115
116 my @lateorders = GetLateOrders(
117     $delay,
118     $booksellerid,
119     $branch,
120     C4::Dates->new($estimateddeliverydatefrom)->output("iso"),
121     C4::Dates->new($estimateddeliverydateto)->output("iso")
122 );
123
124 my $total;
125 foreach (@lateorders){
126         $total += $_->{subtotal};
127 }
128
129 my @letters;
130 my $letters=GetLetters("claimacquisition");
131 foreach (keys %$letters){
132         push @letters, {code=>$_,name=>$letters->{$_}};
133 }
134 $template->param(letters=>\@letters) if (@letters);
135
136 $template->param(ERROR_LOOP => \@errors) if (@errors);
137 $template->param(
138         lateorders => \@lateorders,
139         delay => $delay,
140     estimateddeliverydatefrom => $estimateddeliverydatefrom,
141     estimateddeliverydateto   => $estimateddeliverydateto,
142         total => $total,
143         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
144     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
145 );
146 output_html_with_http_headers $input, $cookie, $template->output;