Added some FIXME comments.
[koha.git] / memberentry.pl
1 #!/usr/bin/perl
2
3 #script to set up screen for modification of borrower details
4 #written 20/12/99 by chris@katipo.co.nz
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha 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 # Koha 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 with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use C4::Output;
26 use CGI;
27 use C4::Search;
28 use C4::Database;
29 use C4::Koha;
30 use HTML::Template;
31
32 my $input = new CGI;
33
34 my $theme = $input->param('theme'); # only used if allowthemeoverride is set
35 my %tmpldata = pathtotemplate ( template => 'memberentry.tmpl', theme => $theme );
36 my $template = HTML::Template->new(filename => $tmpldata{'path'}, die_on_bad_params => 0);
37
38 my $member=$input->param('bornum');
39 if ($member eq ''){
40   $member=NewBorrowerNumber();
41 }
42 my $type=$input->param('type') || '';
43 my $modify=$input->param('modify.x'); 
44 my $delete=$input->param('delete.x');
45 if ($delete){
46   print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$member");
47
48 } else {  # this else goes down the whole script
49   if ($type ne 'Add'){
50     $template->param( header => 'Update Member Details'); # bad templating style
51   } else {
52     $template->param( header => 'Add New Member');
53   }
54   
55   my $data=borrdata('',$member);
56   
57   if ($type eq 'Add'){
58     $template->param( updtype => 'I');
59   } else {
60     $template->param( updtype => 'M');
61   }
62   
63   my $cardnumber=$data->{'cardnumber'};
64   my %systemprefs=systemprefs();
65   # FIXME
66   # This logic should probably be moved out of the presentation code.
67   # Not tonight though.
68   #
69   if ($cardnumber eq '' && $systemprefs{'autoMemberNum'} eq '1') {
70     my $dbh=C4Connect;
71     my $query="select max(substring(borrowers.cardnumber,2,7)) from borrowers";
72     my $sth=$dbh->prepare($query);
73     $sth->execute;
74     my $data=$sth->fetchrow_hashref;
75     $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
76     $sth->finish;
77     $dbh->disconnect;
78     # purpose: generate checksum'd member numbers.
79     # We'll assume we just got the max value of digits 2-8 of member #'s from the database and our job is to
80     # increment that by one, determine the 1st and 9th digits and return the full string.
81     my @weightings = (8,4,6,3,5,2,1);
82     my $sum;
83     my $i = 0;
84     if (! $cardnumber) {                        # If DB has no values, start at 1000000
85       $cardnumber = 1000000;
86     } else {
87       $cardnumber = $cardnumber + 1;
88     }
89   
90     while ($i <8) {                     # step from char 1 to 7.
91       my $temp1 = $weightings[$i];      # read weightings, left to right, 1 char at a time
92       my $temp2 = substr($cardnumber,$i,1);     # sequence left to right, 1 char at a time
93   #print "$temp2<br>";
94       $sum = $sum + ($temp1*$temp2);    # mult each char 1-7 by its corresponding weighting
95       $i++;                             # increment counter
96     }
97     my $rem = ($sum%11);                        # remainder of sum/11 (eg. 9999999/11, remainder=2)
98     if ($rem == 10) {                   # if remainder is 10, use X instead
99       $rem = "X";
100     }  
101     $cardnumber="V$cardnumber$rem";
102   } else {
103     $cardnumber=$data->{'cardnumber'};
104   }
105   
106   if ($data->{'sex'} eq 'F'){
107     $template->param(female => 1);
108   } 
109   
110   my @titles = ('Miss', 'Mrs', 'Ms', 'Mr', 'Dr', 'Sir');
111         # FIXME - Assumes English. This ought to be made part of i18n.
112   my @titledata;
113   while (@titles) {
114     my %row;
115     my $title = shift @titles;
116     $row{'title'} = $title;
117     if ($data->{'title'} eq $title) {
118       $row{'selected'}=' selected';
119     } else {
120       $row{'selected'}='';
121     }
122     push(@titledata, \%row);
123   }
124   
125   my ($categories,$labels)=ethnicitycategories();
126   my $ethnicitycategoriescount=$#{$categories};
127   my $ethcatpopup;
128   if ($ethnicitycategoriescount>=0) {
129         $ethcatpopup = popup_menu(-name=>'ethnicity',
130                                 -values=>$categories,
131                                 -default=>$data->{'ethnicity'},
132                                 -labels=>$labels);
133         $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
134   }
135   
136   ($categories,$labels)=borrowercategories();
137   my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
138                                 -values=>$categories,
139                                 -default=>$data->{'categorycode'},
140                                 -labels=>$labels);
141   
142   my @areas = ('L','F','S','H','K','O','X','Z','V');
143   my %arealabels = ('L' => 'Levin',
144                   'F' => 'Foxton',
145                   'S' => 'Shannon',
146                   'H' => 'Horowhenua',
147                   'K' => 'Kapiti',
148                   'O' => 'Out of District',
149                   'X' => 'Temporary Visitor',
150                   'Z' => 'Interloan Libraries',
151                   'V' => 'Village');
152                   
153   my @areadata;
154   while (@areas) {
155     my %row;
156     my $shortcut = shift @areas;
157     $row{'shortcut'} = $shortcut;
158     if ($data->{'area'} eq $shortcut) {
159       $row{'selected'}=' selected';
160     } else {
161       $row{'selected'}='';
162     }
163     $row{'area'}=$arealabels{$shortcut};
164     push(@areadata, \%row);
165   }
166   
167   
168   my @relationships = ('workplace', 'relative','friend', 'neighbour');
169   my @relshipdata;
170   while (@relationships) {
171     my $relship = shift @relationships;
172     my %row = ('relationship' => $relship);
173     if ($data->{'altrelationship'} eq $relship) {
174       $row{'selected'}=' selected';
175     } else {
176       $row{'selected'}='';
177     }
178     push(@relshipdata, \%row);
179   }
180   
181   # %flags: keys=$data-keys, datas=[formname, HTML-explanation]
182   my %flags = ('gonenoaddress' => ['gna', 'Gone no address'],
183                'lost'          => ['lost', 'Lost'],
184                'debarred'      => ['debarred', 'Debarred']);
185   
186   my @flagdata;
187   foreach (keys(%flags)) {
188     my $key = $_;
189     my %row =  ('key'   => $key,
190                 'name'  => $flags{$key}[0],
191                 'html'  => $flags{$key}[1]);
192     if ($data->{$key}) {
193       $row{'yes'}=' checked';
194       $row{'no'}='';
195     } else {
196       $row{'yes'}='';
197       $row{'no'}=' checked';
198     }
199     push(@flagdata, \%row);
200   }
201
202   if ($modify){
203     $template->param( modify => 1 );
204   }
205   
206   $template->param( startmenumember => join ('', startmenu('member')),
207                         endmenumember   => join ('', endmenu('member')),
208                         member          => $member,
209                         firstname       => $data->{'firstname'},
210                         surname         => $data->{'surname'},
211                         othernames      => $data->{'othernames'},
212                         initials        => $data->{'initials'},
213                         ethcatpopup     => $ethcatpopup,
214                         catcodepopup    => $catcodepopup,
215                         streetaddress   => $data->{'physstreet'},
216                         streetcity      => $data->{'streetcity'},
217                         city            => $data->{'city'},
218                         phone           => $data->{'phone'},
219                         phoneday        => $data->{'phoneday'},
220                         faxnumber       => $data->{'faxnumber'},
221                         emailaddress    => $data->{'emailaddress'},
222                         contactname     => $data->{'contactname'},
223                         altphone        => $data->{'altphone'},
224                         altnotes        => $data->{'altnotes'},
225                         borrowernotes   => $data->{'borrowernotes'},
226                         flagloop        => \@flagdata,
227                         relshiploop     => \@relshipdata,
228                         titleloop       => \@titledata,
229                         arealoop        => \@areadata,
230                         dateenrolled    => $data->{'dateenrolled'},
231                         expiry          => $data->{'expiry'},
232                         cardnumber      => $cardnumber,
233                         dateofbirth     => $data->{'dateofbirth'});
234                         
235   print "Content-Type: text/html\n\n", $template->output;
236   
237
238 }