Albanian po files
[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::Dates qw/format_date/;
29 use Date::Calc qw/Today/;
30
31 my $input = new CGI;
32 my $order   = $input->param( 'order' ) || '';
33 my $showall = $input->param('showall');
34
35 my  $bornamefilter = $input->param( 'borname');
36 my   $borcatfilter = $input->param( 'borcat' );
37 my $itemtypefilter = $input->param('itemtype');
38 my $borflagsfilter = $input->param('borflags') || "";
39 my   $branchfilter = $input->param( 'branch' );
40 my $op             = $input->param(   'op'   ) || '';
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name   => "circ/overdue.tmpl",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { reports => 1, circulate => "circulate_remaining_permissions" },
49         debug           => 1,
50     }
51 );
52
53 my $dbh = C4::Context->dbh;
54
55 # download the complete CSV
56 if ($op eq 'csv') {
57 warn "BRANCH : $branchfilter";
58     my $lib = $branchfilter ? "-library $branchfilter" :'';
59     my $csv = `../misc/cronjobs/overdue_notices.pl -csv -n $lib`;
60     print $input->header(-type => 'application/vnd.sun.xml.calc',
61                         -encoding    => 'utf-8',
62                         -attachment=>"overdues.csv",
63                         -filename=>"overdues.csv" );
64     print $csv;
65     exit;
66 }
67 my $req;
68 $req = $dbh->prepare( "select categorycode, description from categories order by description");
69 $req->execute;
70 my @borcatloop;
71 while (my ($catcode, $description) =$req->fetchrow) {
72     push @borcatloop, {
73         value    => $catcode,
74         selected => $catcode eq $borcatfilter ? 1 : 0,
75         catname  => $description,
76     };
77 }
78
79 $req = $dbh->prepare( "select itemtype, description from itemtypes order by description");
80 $req->execute;
81 my @itemtypeloop;
82 while (my ($itemtype, $description) =$req->fetchrow) {
83     push @itemtypeloop, {
84         value        => $itemtype,
85         selected     => $itemtype eq $itemtypefilter ? 1 : 0,
86         itemtypename => $description,
87     };
88 }
89 my $onlymine=C4::Context->preference('IndependantBranches') && 
90              C4::Context->userenv && 
91              C4::Context->userenv->{flags}!=1 && 
92              C4::Context->userenv->{branch};
93
94 $branchfilter = C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
95
96 $template->param(
97     branchloop   => GetBranchesLoop($branchfilter, $onlymine),
98     branchfilter => $branchfilter,
99     borcatloop   => \@borcatloop,
100     itemtypeloop => \@itemtypeloop,
101     borname      => $bornamefilter,
102     order        => $order,
103     showall      => $showall,
104 );
105
106 my @sort_roots = qw(borrower title barcode date_due);
107 push @sort_roots, map {$_ . " desc"} @sort_roots;
108 my @order_loop = ({selected => $order ? 0 : 1});   # initial blank row
109 foreach (@sort_roots) {
110     my $tmpl_name = $_;
111     $tmpl_name =~ s/\s/_/g;
112     push @order_loop, {
113         selected => $order eq $_ ? 1 : 0,
114         ordervalue => $_,
115         foo => $tmpl_name,
116         'order_' . $tmpl_name => 1,
117     };
118 }
119 $template->param(ORDER_LOOP => \@order_loop);
120
121 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", Today());
122
123 $bornamefilter =~s/\*/\%/g;
124 $bornamefilter =~s/\?/\_/g;
125
126 my $strsth="SELECT date_due,
127   concat(surname,' ', firstname) as borrower, 
128   borrowers.phone,
129   borrowers.email,
130   issues.itemnumber,
131   items.barcode,
132   biblio.title,
133   biblio.author,
134   borrowers.borrowernumber,
135   biblio.biblionumber,
136   borrowers.branchcode 
137   FROM issues
138 LEFT JOIN borrowers   ON (issues.borrowernumber=borrowers.borrowernumber )
139 LEFT JOIN items       ON (issues.itemnumber=items.itemnumber)
140 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
141 LEFT JOIN biblio      ON (biblio.biblionumber=items.biblionumber )
142 WHERE 1=1 "; # placeholder, since it is possible that none of the additional
143              # conditions will be selected by user
144 $strsth.=" AND date_due               < '" . $todaysdate     . "' " unless ($showall);
145 $strsth.=" AND (borrowers.firstname like '".$bornamefilter."%' or borrowers.surname like '".$bornamefilter."%' or borrowers.cardnumber like '".$bornamefilter."%')" if($bornamefilter) ;
146 $strsth.=" AND borrowers.categorycode = '" . $borcatfilter   . "' " if $borcatfilter;
147 $strsth.=" AND biblioitems.itemtype   = '" . $itemtypefilter . "' " if $itemtypefilter;
148 $strsth.=" AND borrowers.flags        = '" . $borflagsfilter . "' " if $borflagsfilter;
149 $strsth.=" AND borrowers.branchcode   = '" . $branchfilter   . "' " if $branchfilter;
150 $strsth.=" ORDER BY " . (
151     ($order eq "borrower" or $order eq "borrower desc") ? "$order, date_due"                 : 
152     ($order eq "title"    or $order eq    "title desc") ? "$order, date_due, borrower"       :
153     ($order eq "barcode"  or $order eq  "barcode desc") ? "items.$order, date_due, borrower" :
154                             ($order eq "date_due desc") ? "date_due DESC, borrower"          :
155                                                           "date_due, borrower"  # default sort order
156 );
157 $template->param(sql=>$strsth);
158 my $sth=$dbh->prepare($strsth);
159 #warn "overdue.pl : query string ".$strsth;
160 $sth->execute();
161
162 my @overduedata;
163 while (my $data=$sth->fetchrow_hashref) {
164     push @overduedata, {
165         duedate        => format_date($data->{date_due}),
166         borrowernumber => $data->{borrowernumber},
167         barcode        => $data->{barcode},
168         itemnum        => $data->{itemnumber},
169         name           => $data->{borrower},
170         phone          => $data->{phone},
171         email          => $data->{email},
172         biblionumber   => $data->{biblionumber},
173         title          => $data->{title},
174         author         => $data->{author},
175         branchcode     => $data->{branchcode},
176     };
177 }
178
179 $template->param(
180     todaysdate  => format_date($todaysdate),
181     overdueloop => \@overduedata
182 );
183
184 output_html_with_http_headers $input, $cookie, $template->output;