all those fixes are related to translation improvement.
[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 $query="select max(substring(borrowers.cardnumber,2,7)) from borrowers";
79         my $sth=$dbh->prepare($query);
80         $sth->execute;
81
82         my $data=$sth->fetchrow_hashref;
83         $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
84         $sth->finish;
85
86         # purpose: generate checksum'd member numbers.
87         # We'll assume we just got the max value of digits 2-8 of member #'s
88         # from the database and our job is to increment that by one,
89         # determine the 1st and 9th digits and return the full string.
90
91         if (! $cardnumber) {                    # If DB has no values,
92             $cardnumber = 1000000;              # start at 1000000
93         } else {
94             $cardnumber += 1;
95         }
96
97         my $sum = 0;
98         for (my $i = 0; $i < 8; $i += 1) {
99             # read weightings, left to right, 1 char at a time
100             my $temp1 = $weightings[$i];
101
102             # sequence left to right, 1 char at a time
103             my $temp2 = substr($cardnumber,$i,1);
104
105             # mult each char 1-7 by its corresponding weighting
106             $sum += $temp1 * $temp2;
107         }
108
109         my $rem = ($sum%11);
110         $rem = 'X' if $rem == 10;
111
112         $cardnumber="V$cardnumber$rem";
113     }
114     return $cardnumber;
115 }
116
117 1;