View | Details | Raw Unified | Return to bug 6868
Collapse All | Expand All

(-)a/misc/maintenance/cleanup_accountno.pl (-1 / +89 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2011 Ian Walls and ByWater Solutions
4
#
5
# This script is intended to help Koha libraries affected by Bug 6868 repair
6
# their accountlines table; unnecessarily large accountno numbers were entered
7
# due to a bug with the writeoff subroutine; this is fixed by enhancement 3498.
8
# The accountno has a maximum value of 32767, and once that's reached, fines payments
9
# will be applied to multiple fines (resulting in data loss).
10
#
11
# This 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
# This script 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                                      
21
# with Koha; if not, write to the Free Software Foundation, Inc.,                                              
22
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24
25
26
# modules to use
27
use Getopt::Long;
28
use C4::Context;
29
30
# Database handle
31
my $dbh = C4::Context->dbh;
32
my $sth_input = $dbh->prepare("SELECT borrowernumber FROM accountlines GROUP BY borrowernumber HAVING count(*) < max(accountno)"); 
33
my $sth2 = $dbh->prepare("SELECT * FROM accountlines WHERE borrowernumber = ? ORDER BY accountno ASC");
34
my $sth_update = $dbh->prepare("UPDATE accountlines SET accountno = ? WHERE borrowernumber = ? AND accountno = ? LIMIT 1");
35
36
# Benchmarking variables
37
my $startime = time();
38
my $totalcount = 0;
39
40
# Realtime Feedback needs this
41
$|=1;
42
43
# Options
44
my @input;
45
my $testmode;
46
my $verbose;
47
48
GetOptions(
49
  't'	=> \$testmode,
50
  'v' => \$verbose
51
);
52
53
$sth_input->execute();
54
while ($row = $sth_input->fetchrow_hashref) {
55
  main_job($row);
56
  $totalcount++;
57
}
58
59
60
# Benchmarking
61
my $endtime = time();
62
my $time = $endtime-$startime;
63
64
print "\n$totalcount accountlines rows updated in $time seconds\n";
65
66
#
67
# THE MAIN EVENT
68
#
69
70
sub main_job {
71
  my $input = shift;
72
73
  my $borrowernumber = $input->{'borrowernumber'};
74
  my $newaccountno = 1;
75
76
  $sth2->execute($borrowernumber);
77
  while (my $accounts = $sth2->fetchrow_hashref) {
78
    my $oldaccountno = $accounts->{'accountno'};
79
    if (defined $verbose) {
80
      print "Updating Borrower $borrowernumber, Accountno $oldaccountno to $newaccountno\n";
81
    } else {
82
      print ".";
83
      print "\r$totalcount" unless ($totalcount % 100);
84
    }
85
    $sth_update->execute($newaccountno, $borrowernumber, $oldaccountno) unless (defined $testmode);  
86
    $newaccountno++;
87
  }
88
89
}

Return to bug 6868