DBI call fix for bug 662
[koha.git] / C4 / Members.pm
1 # -*- tab-width: 8 -*-
2
3 package C4::Members;
4
5 # Copyright 2000-2003 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 require Exporter;
24 use C4::Context;
25
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
27
28 $VERSION = 0.01;
29
30 =head1 NAME
31
32 C4::Members - Perl Module containing convenience functions for member handling
33
34 =head1 SYNOPSIS
35
36
37 =head1 DESCRIPTION
38
39
40 =head1 FUNCTIONS
41
42 =over 2
43
44 =cut
45
46 @ISA = qw(Exporter);
47 @EXPORT = qw();
48
49 @EXPORT_OK = qw(
50         &fixup_cardnumber
51     );
52
53 ################################################################################
54
55 =item fixup_cardnumber
56
57 Warning: The caller is responsible for locking the members table in write
58 mode, to avoid database corruption.
59
60 =cut
61
62 use vars qw( @weightings );
63 my @weightings = (8,4,6,3,5,2,1);
64
65 sub fixup_cardnumber ($) {
66     my($cardnumber) = @_;
67     my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
68     $autonumber_members = 0 unless defined $autonumber_members;
69
70     warn "autoMemberNum is $autonumber_members\n";
71
72     # Find out whether member numbers should be generated
73     # automatically. Should be either "1" or something else.
74     # Defaults to "0", which is interpreted as "no".
75
76     if ($cardnumber !~ /\S/ && $autonumber_members) {
77         my $dbh = C4::Context->dbh;
78         my $sth=$dbh->prepare("select max(substring(borrowers.cardnumber,2,7)) from borrowers");
79         $sth->execute;
80
81         my $data=$sth->fetchrow_hashref;
82         $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
83         $sth->finish;
84
85         # purpose: generate checksum'd member numbers.
86         # We'll assume we just got the max value of digits 2-8 of member #'s
87         # from the database and our job is to increment that by one,
88         # determine the 1st and 9th digits and return the full string.
89
90         if (! $cardnumber) {                    # If DB has no values,
91             $cardnumber = 1000000;              # start at 1000000
92         } else {
93             $cardnumber += 1;
94         }
95
96         my $sum = 0;
97         for (my $i = 0; $i < 8; $i += 1) {
98             # read weightings, left to right, 1 char at a time
99             my $temp1 = $weightings[$i];
100
101             # sequence left to right, 1 char at a time
102             my $temp2 = substr($cardnumber,$i,1);
103
104             # mult each char 1-7 by its corresponding weighting
105             $sum += $temp1 * $temp2;
106         }
107
108         my $rem = ($sum%11);
109         $rem = 'X' if $rem == 10;
110
111         $cardnumber="V$cardnumber$rem";
112     }
113     return $cardnumber;
114 }
115
116 1;