adding 'gettext' as this is requred by the translation utility
[koha.git] / circ / bookcount.pl
1 #!/usr/bin/perl
2
3
4 #written 7/3/2002 by Finlay
5 #script to display reports
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Context;
27 use C4::Circulation;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Auth;
31 use C4::Branch; # GetBranches
32 use C4::Biblio; # GetBiblioItemData
33 use C4::Dates qw/format_date/;
34
35 my $input        = new CGI;
36 my $itm          = $input->param('itm');
37 my $bi           = $input->param('bi');
38 my $biblionumber = $input->param('biblionumber');
39 my $branches     = GetBranches;
40
41 my $idata = itemdatanum($itm);
42 my $data  = GetBiblioItemData($bi);
43
44 my $homebranch    = $branches->{ $idata->{'homebranch'} }->{'branchname'};
45 my $holdingbranch = $branches->{ $idata->{'holdingbranch'} }->{'branchname'};
46
47 my ( $lastmove, $message ) = lastmove($itm);
48
49 my $lastdate;
50 my $count;
51 if ( not $lastmove ) {
52     $lastdate = $message;
53     $count = issuessince( $itm, 0 );
54 }
55 else {
56     $lastdate = $lastmove->{'datearrived'};
57     $count = issuessince( $itm, $lastdate );
58 }
59
60 # make the page ...
61
62 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63     {
64         template_name   => "circ/bookcount.tmpl",
65         query           => $input,
66         type            => "intranet",
67         authnotrequired => 0,
68         flagsrequired   => { circulate => 1 },
69         debug           => 1,
70     }
71 );
72
73 my @branchloop;
74
75 foreach my $branchcode ( keys %$branches ) {
76     my %linebranch;
77     $linebranch{issues} = issuesat( $itm, $branchcode );
78     my $date = lastseenat( $itm, $branchcode );
79     $linebranch{seen}       = slashdate($date);
80     $linebranch{branchname} = $branches->{$branchcode}->{'branchname'};
81     push( @branchloop, \%linebranch );
82 }
83
84 $template->param(
85     biblionumber            => $biblionumber,
86     title                   => $data->{'title'},
87     author                  => $data->{'author'},
88     barcode                 => $idata->{'barcode'},
89     biblioitemnumber        => $bi,
90     homebranch              => $homebranch,
91     holdingbranch           => $holdingbranch,
92     lastdate                => format_date($lastdate),
93     count                   => $count,
94     branchloop              => \@branchloop,
95 );
96
97 output_html_with_http_headers $input, $cookie, $template->output;
98
99
100 sub itemdatanum {
101     my ($itemnumber) = @_;
102     my $dbh          = C4::Context->dbh;
103     my $sth          = $dbh->prepare("select * from items where itemnumber=?");
104     $sth->execute($itemnumber);
105     my $data = $sth->fetchrow_hashref;
106     $sth->finish;
107     return ($data);
108 }
109
110 sub lastmove {
111     my ($itemnumber) = @_;
112     my $dbh          = C4::Context->dbh;
113     my $sth          =
114       $dbh->prepare(
115 "select max(branchtransfers.datearrived) from branchtransfers where branchtransfers.itemnumber=?"
116       );
117     $sth->execute($itemnumber);
118     my ($date) = $sth->fetchrow_array;
119     return ( 0, "Item has no branch transfers record" ) if not $date;
120     $sth =
121       $dbh->prepare(
122 "Select * from branchtransfers where branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
123       );
124     $sth->execute( $itemnumber, $date );
125     my ($data) = $sth->fetchrow_hashref;
126     return ( 0, "Item has no branch transfers record" ) if not $data;
127     $sth->finish;
128     return ( $data, "" );
129 }
130
131 sub issuessince {
132     my ( $itemnumber, $date ) = @_;
133     my $dbh = C4::Context->dbh;
134     my $sth =
135       $dbh->prepare("SELECT SUM(count) FROM (
136                         SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
137                         UNION ALL
138                         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
139                      ) tmp");
140     $sth->execute( $itemnumber, $date, $itemnumber, $date );
141     my $count = $sth->fetchrow_arrayref->[0];
142     $sth->finish;
143     return ( $count );
144 }
145
146 sub issuesat {
147     my ( $itemnumber, $brcd ) = @_;
148     my $dbh = C4::Context->dbh;
149     my $sth =
150       $dbh->prepare("SELECT SUM(count) FROM (
151                         SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and branchcode = ?
152                         UNION ALL
153                         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and branchcode = ?
154                      ) tmp");
155     $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
156     my ($count) = $sth->fetchrow_array;
157     $sth->finish;
158     return ($count);
159 }
160
161 sub lastseenat {
162     my ( $itm, $brc ) = @_;
163     my $dbh = C4::Context->dbh;
164     my $sth =
165       $dbh->prepare("SELECT MAX(tstamp) FROM (
166                         SELECT MAX(timestamp) AS tstamp FROM issues WHERE itemnumber = ? and branchcode = ?
167                         UNION ALL
168                         SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? and branchcode = ?
169                      ) tmp");
170     $sth->execute( $itm, $brc, $itm, $brc );
171     my ($date1) = $sth->fetchrow_array;
172     $sth->finish;
173     $sth =
174       $dbh->prepare(
175 "Select max(datearrived) from branchtransfers where itemnumber=? and tobranch = ?"
176       );
177     $sth->execute( $itm, $brc );
178     my ($date2) = $sth->fetchrow_array;
179     $sth->finish;
180
181     #FIXME: MJR thinks unsafe
182     $date1 =~ s/-//g;
183     $date1 =~ s/://g;
184     $date1 =~ s/ //g;
185     $date2 =~ s/-//g;
186     $date2 =~ s/://g;
187     $date2 =~ s/ //g;
188     my $date;
189     if ( $date1 < $date2 ) {
190         $date = $date2;
191     }
192     else {
193         $date = $date1;
194     }
195     return ($date);
196 }
197
198 #####################################################
199 # write date....
200 sub slashdate {
201     my ($date) = @_;
202     if ( not $date ) {
203         return "never";
204     }
205     my ( $yr, $mo, $da, $hr, $mi ) = (
206         substr( $date, 0,  4 ),
207         substr( $date, 4,  2 ),
208         substr( $date, 6,  2 ),
209         substr( $date, 8,  2 ),
210         substr( $date, 10, 2 )
211     );
212     return "$hr:$mi  " . format_date("$yr-$mo-$da");
213 }