3 # Copyright 2011 BibLibre
5 # This file is part of Koha.
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
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.
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, Suite 500, Boston, MA 02110-1335, USA
21 # Re-create statistics from issues and old_issues tables
31 my $dbh = C4
::Context
->dbh;
38 GetOptions
('issues' => \
$issues, 'returns' => \
$returns, 'help|?|h' => \
$help);
42 print ("Usage : perl $0 [--issues] [--returns]\n\n");
43 print (" issues: process only issues\n");
44 print (" returns: process only returns\n");
48 if ($issues == 0 && $returns == 0) {
55 my $count_renewals = 0;
56 my $count_returns = 0;
58 # Issues and renewals can be found in both issues and old_issues tables
60 foreach my $table ('issues', 'old_issues') {
62 print "looking for missing issues from $table\n";
63 my $query = "SELECT borrowernumber, branchcode, itemnumber, issuedate, renewals, lastreneweddate from $table where itemnumber is not null";
64 my $sth = $dbh->prepare($query);
66 # Looking for missing issues
67 while (my $hashref = $sth->fetchrow_hashref) {
68 my $ctnquery = "SELECT count(*) as cnt FROM statistics WHERE borrowernumber = ? AND itemnumber = ? AND DATE(datetime) = ? AND type = 'issue'";
69 my $substh = $dbh->prepare($ctnquery);
70 $substh->execute($hashref->{'borrowernumber'}, $hashref->{'itemnumber'}, $hashref->{'issuedate'});
71 my $count = $substh->fetchrow_hashref->{'cnt'};
73 # Inserting missing issue
74 my $insert = "INSERT INTO statistics (datetime, branch, value, type, other, itemnumber, itemtype, borrowernumber)
75 VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
76 $substh = $dbh->prepare($insert);
77 my $item = Koha
::Items
->find($hashref->{'itemnumber'});
78 my $itemtype = $item->effective_itemtype;
81 $hashref->{'issuedate'},
82 $hashref->{'branchcode'},
86 $hashref->{'itemnumber'},
88 $hashref->{'borrowernumber'}
90 print "date: $hashref->{'issuedate'} branchcode: $hashref->{'branchcode'} type: issue itemnumber: $hashref->{'itemnumber'} itype: $itemtype borrowernumber: $hashref->{'borrowernumber'}\n";
94 # Looking for missing renewals
95 if ($hashref->{'renewals'} && $hashref->{'renewals'} > 0 ) {
96 # This is the not-so accurate part :
97 # We assume that there are missing renewals, based on the last renewal date
98 # Maybe should this be deactivated by default ?
99 my $ctnquery = "SELECT count(*) as cnt FROM statistics WHERE borrowernumber = ? AND itemnumber = ? AND DATE(datetime) = ? AND type = 'renew'";
100 my $substh = $dbh->prepare($ctnquery);
101 $substh->execute($hashref->{'borrowernumber'}, $hashref->{'itemnumber'}, $hashref->{'lastreneweddate'});
103 my $missingrenewalscount = $hashref->{'renewals'} - $substh->fetchrow_hashref->{'cnt'};
104 print "We assume $missingrenewalscount renewals are missing. Creating them\n" if ($missingrenewalscount > 0);
105 for (my $i = 0; $i < $missingrenewalscount; $i++) {
107 # Inserting missing renewals
108 my $insert = "INSERT INTO statistics (datetime, branch, value, type, other, itemnumber, itemtype, borrowernumber)
109 VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
110 $substh = $dbh->prepare($insert);
111 my $item = Koha
::Items
->find($hashref->{'itemnumber'});
112 my $itemtype = $item->effective_itemtype;
115 $hashref->{'lastreneweddate'},
116 $hashref->{'branchcode'},
120 $hashref->{'itemnumber'},
122 $hashref->{'borrowernumber'}
124 print "date: $hashref->{'lastreneweddate'} branchcode: $hashref->{'branchcode'} type: renew itemnumber: $hashref->{'itemnumber'} itype: $itemtype borrowernumber: $hashref->{'borrowernumber'}\n";
136 print "looking for missing returns from old_issues\n";
137 my $query = "SELECT * from old_issues where itemnumber is not null";
138 my $sth = $dbh->prepare($query);
140 # Looking for missing returns
141 while (my $hashref = $sth->fetchrow_hashref) {
142 my $ctnquery = "SELECT count(*) as cnt FROM statistics WHERE borrowernumber = ? AND itemnumber = ? AND DATE(datetime) = ? AND type = 'return'";
143 my $substh = $dbh->prepare($ctnquery);
144 $substh->execute($hashref->{'borrowernumber'}, $hashref->{'itemnumber'}, $hashref->{'returndate'});
145 my $count = $substh->fetchrow_hashref->{'cnt'};
147 # Inserting missing issue
148 my $insert = "INSERT INTO statistics (datetime, branch, value, type, other, itemnumber, itemtype, borrowernumber)
149 VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
150 $substh = $dbh->prepare($insert);
151 my $item = Koha
::Items
->find($hashref->{'itemnumber'});
152 my $itemtype = $item->effective_itemtype;
155 $hashref->{'returndate'},
156 $hashref->{'branchcode'},
160 $hashref->{'itemnumber'},
162 $hashref->{'borrowernumber'}
164 print "date: $hashref->{'returndate'} branchcode: $hashref->{'branchcode'} type: return itemnumber: $hashref->{'itemnumber'} itype: $itemtype borrowernumber: $hashref->{'borrowernumber'}\n";
171 print "Missing issues added: $count_issues\n" if $issues;
172 print "Missing renewals added: $count_renewals\n" if $issues;
173 print "Missing returns added: $count_returns\n" if $returns;