Bug 13895: Remove create_item and create_biblio subs from test
[koha.git] / misc / recreateIssueStatistics.pl
blob6ba83e1e53fa4b267c3a2d4128e7e4c79a6a436a
1 #!/usr/bin/perl
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
10 # version.
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
23 use strict;
24 use warnings;
25 use C4::Context;
26 use C4::Items;
27 use Data::Dumper;
28 use Getopt::Long;
29 use Koha::Items;
31 my $dbh = C4::Context->dbh;
33 # Options
34 my $issues = 0;
35 my $returns = 0;
36 my $help = 0;
38 GetOptions ('issues' => \$issues, 'returns' => \$returns, 'help|?|h' => \$help);
40 # Show usage
41 if ($help == 1) {
42 print ("Usage : perl $0 [--issues] [--returns]\n\n");
43 print (" issues: process only issues\n");
44 print (" returns: process only returns\n");
45 exit 0;
48 if ($issues == 0 && $returns == 0) {
49 $issues = 1;
50 $returns = 1;
53 # Counters
54 my $count_issues = 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
59 if ($issues == 1) {
60 foreach my $table ('issues', 'old_issues') {
61 # Getting 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);
65 $sth->execute;
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'};
72 if ($count == 0) {
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;
80 $substh->execute(
81 $hashref->{'issuedate'},
82 $hashref->{'branchcode'},
84 'issue',
85 '',
86 $hashref->{'itemnumber'},
87 $itemtype,
88 $hashref->{'borrowernumber'}
90 print "date: $hashref->{'issuedate'} branchcode: $hashref->{'branchcode'} type: issue itemnumber: $hashref->{'itemnumber'} itype: $itemtype borrowernumber: $hashref->{'borrowernumber'}\n";
91 $count_issues++;
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;
114 $substh->execute(
115 $hashref->{'lastreneweddate'},
116 $hashref->{'branchcode'},
118 'renew',
120 $hashref->{'itemnumber'},
121 $itemtype,
122 $hashref->{'borrowernumber'}
124 print "date: $hashref->{'lastreneweddate'} branchcode: $hashref->{'branchcode'} type: renew itemnumber: $hashref->{'itemnumber'} itype: $itemtype borrowernumber: $hashref->{'borrowernumber'}\n";
125 $count_renewals++;
134 # Getting returns
135 if ($returns == 1) {
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);
139 $sth->execute;
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'};
146 if ($count == 0) {
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;
154 $substh->execute(
155 $hashref->{'returndate'},
156 $hashref->{'branchcode'},
158 'return',
160 $hashref->{'itemnumber'},
161 $itemtype,
162 $hashref->{'borrowernumber'}
164 print "date: $hashref->{'returndate'} branchcode: $hashref->{'branchcode'} type: return itemnumber: $hashref->{'itemnumber'} itype: $itemtype borrowernumber: $hashref->{'borrowernumber'}\n";
165 $count_returns++;
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;