(bug #4207) receive shipment problem
[koha.git] / circ / overdue.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 # use warnings; # FIXME
23 use C4::Context;
24 use C4::Output;
25 use CGI;
26 use C4::Auth;
27 use C4::Branch;
28 use C4::Overdues qw/GetOverduesByBorrowers/;
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use Date::Calc qw/Today/;
31 use Text::CSV_XS;
32
33 my $input = new CGI;
34 my $order   = $input->param( 'order' ) || '';
35 my $showall = $input->param('showall');
36
37 my  $bornamefilter = $input->param( 'borname');
38 my   $borcatfilter = $input->param( 'borcat' );
39 my $itemtypefilter = $input->param('itemtype');
40 my $borflagsfilter = $input->param('borflags') || "";
41 my   $branchfilter = $input->param( 'branch' );
42 my $op             = $input->param(   'op'   ) || '';
43
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45     {
46         template_name   => "circ/overdue.tmpl",
47         query           => $input,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { reports => 1, circulate => "circulate_remaining_permissions" },
51         debug           => 1,
52     }
53 );
54
55 my $dbh = C4::Context->dbh;
56
57 my $req;
58 $req = $dbh->prepare( "select categorycode, description from categories order by description");
59 $req->execute;
60 my @borcatloop;
61 while (my ($catcode, $description) =$req->fetchrow) {
62     push @borcatloop, {
63         value    => $catcode,
64         selected => $catcode eq $borcatfilter ? 1 : 0,
65         catname  => $description,
66     };
67 }
68
69 $req = $dbh->prepare( "select itemtype, description from itemtypes order by description");
70 $req->execute;
71 my @itemtypeloop;
72 while (my ($itemtype, $description) =$req->fetchrow) {
73     push @itemtypeloop, {
74         value        => $itemtype,
75         selected     => $itemtype eq $itemtypefilter ? 1 : 0,
76         itemtypename => $description,
77     };
78 }
79 my $onlymine=C4::Context->preference('IndependantBranches') && 
80              C4::Context->userenv && 
81              C4::Context->userenv->{flags}!=1 && 
82              C4::Context->userenv->{branch};
83
84 $branchfilter = C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
85
86 $template->param(
87     branchloop   => GetBranchesLoop($branchfilter, $onlymine),
88     branchfilter => $branchfilter,
89     borcatloop   => \@borcatloop,
90     itemtypeloop => \@itemtypeloop,
91     borname      => $bornamefilter,
92     order        => $order,
93     showall      => $showall,
94     csv_param_string => $input->query_string(),
95     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
96     dateduefrom => $input->param( 'dateduefrom' ) || '',
97     datedueto   => $input->param( 'datedueto' ) || '',
98 );
99
100 my @sort_roots = qw(borrower title barcode date_due);
101 push @sort_roots, map {$_ . " desc"} @sort_roots;
102 my @order_loop = ({selected => $order ? 0 : 1});   # initial blank row
103 foreach (@sort_roots) {
104     my $tmpl_name = $_;
105     $tmpl_name =~ s/\s/_/g;
106     push @order_loop, {
107         selected => $order eq $_ ? 1 : 0,
108         ordervalue => $_,
109         foo => $tmpl_name,
110         'order_' . $tmpl_name => 1,
111     };
112 }
113 $template->param(ORDER_LOOP => \@order_loop);
114
115 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Today());
116
117 $bornamefilter =~s/\*/\%/g;
118 $bornamefilter =~s/\?/\_/g;
119
120 my $dateduefrom = format_date_in_iso($input->param( 'dateduefrom' ));
121 my $datedueto   = format_date_in_iso($input->param( 'datedueto' ));
122
123 my @overduedata = @{GetOverduesByBorrowers($branchfilter, $borcatfilter, $itemtypefilter, $borflagsfilter, $bornamefilter, $order, $dateduefrom, $datedueto)};
124
125 $template->param(
126     todaysdate  => format_date($todaysdate),
127     overdueloop => \@overduedata 
128 );
129
130 # download the complete CSV
131 if ($op eq 'csv') {
132         binmode(STDOUT, ":utf8");
133         my $csv = build_csv(\@overduedata);
134         print $input->header(-type => 'application/vnd.sun.xml.calc',
135                              -encoding    => 'utf-8',
136                              -attachment=>"overdues.csv",
137                              -filename=>"overdues.csv" );
138         print $csv;
139         exit;
140 }
141
142 output_html_with_http_headers $input, $cookie, $template->output;
143
144
145 sub build_csv {
146     my $overdues = shift;
147
148     return "" if scalar(@$overdues) == 0;
149
150     my @lines = ();
151
152     # build header ...
153     my @keys = (
154         'borrowernumber',
155         'title',
156         'firstname',
157         'surname',
158         'address',
159         'city',
160         'zipcode',
161         'phone',
162         'email',
163         'branchcode',
164         'overdues'
165     );
166
167     my $csv = Text::CSV_XS->new({
168         binary   => 1,
169         sep_char => C4::Context->preference("delimiter") ? 
170                     C4::Context->preference("delimiter") : ';' ,
171     });
172     $csv->combine(@keys);
173     push @lines, $csv->string();
174
175     # ... and rest of report
176     foreach my $overdue ( @{ $overdues } ) {
177         my $issues;
178         foreach my $issue ( @{$overdue->{overdues} }){
179             $issues .= "$issue->{title} / $issue->{author} / $issue->{itemcallnumber} / $issue->{barcode} / ".$issue->{issuedate}. " - " . $issue->{date_due} . " \r\n";
180         }
181         push @lines, $csv->string() if $csv->combine(
182             $overdue->{borrowernumber},
183             $overdue->{title},
184             $overdue->{firstname},
185             $overdue->{surname},
186             $overdue->{address},
187             $overdue->{city},
188             $overdue->{zipcode},
189             $overdue->{phone},
190             $overdue->{email},
191             $overdue->{branchcode},
192             $issues,
193         );
194     }
195
196     return join("\n", @lines) . "\n";
197 }