bugfix => escaping correctly values in SQL
[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     # Find out whether member numbers should be generated
70     # automatically. Should be either "1" or something else.
71     # Defaults to "0", which is interpreted as "no".
72
73     if ($cardnumber !~ /\S/ && $autonumber_members) {
74         my $dbh = C4::Context->dbh;
75         my $sth=$dbh->prepare("select max(substring(borrowers.cardnumber,2,7)) from borrowers");
76         $sth->execute;
77
78         my $data=$sth->fetchrow_hashref;
79         $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
80         $sth->finish;
81
82         # purpose: generate checksum'd member numbers.
83         # We'll assume we just got the max value of digits 2-8 of member #'s
84         # from the database and our job is to increment that by one,
85         # determine the 1st and 9th digits and return the full string.
86
87         if (! $cardnumber) {                    # If DB has no values,
88             $cardnumber = 1000000;              # start at 1000000
89         } else {
90             $cardnumber += 1;
91         }
92
93         my $sum = 0;
94         for (my $i = 0; $i < 8; $i += 1) {
95             # read weightings, left to right, 1 char at a time
96             my $temp1 = $weightings[$i];
97
98             # sequence left to right, 1 char at a time
99             my $temp2 = substr($cardnumber,$i,1);
100
101             # mult each char 1-7 by its corresponding weighting
102             $sum += $temp1 * $temp2;
103         }
104
105         my $rem = ($sum%11);
106         $rem = 'X' if $rem == 10;
107
108         $cardnumber="V$cardnumber$rem";
109     }
110     return $cardnumber;
111 }
112
113 1;