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