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::Context;
26 use C4::Output;
27 use CGI;
28 use C4::Search;
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 $autonumber_members = C4::Context->preference("autoMemberNum") || 0;
65                 # Find out whether member numbers should be generated
66                 # automatically. Should be either "1" or something else.
67                 # Defaults to "0", which is interpreted as "no".
68   # FIXME
69   # This logic should probably be moved out of the presentation code.
70   # Not tonight though.
71   #
72   if ($cardnumber eq '' && $autonumber_members eq '1') {
73     my $dbh = C4::Context->dbh;
74     my $query="select max(substring(borrowers.cardnumber,2,7)) from borrowers";
75     my $sth=$dbh->prepare($query);
76     $sth->execute;
77     my $data=$sth->fetchrow_hashref;
78     $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
79     $sth->finish;
80     # purpose: generate checksum'd member numbers.
81     # We'll assume we just got the max value of digits 2-8 of member #'s from the database and our job is to
82     # increment that by one, determine the 1st and 9th digits and return the full string.
83     my @weightings = (8,4,6,3,5,2,1);
84     my $sum;
85     my $i = 0;
86     if (! $cardnumber) {                        # If DB has no values, start at 1000000
87       $cardnumber = 1000000;
88     } else {
89       $cardnumber = $cardnumber + 1;            # FIXME - $cardnumber++;
90     }
91   
92     while ($i <8) {                     # step from char 1 to 7.
93       my $temp1 = $weightings[$i];      # read weightings, left to right, 1 char at a time
94       my $temp2 = substr($cardnumber,$i,1);     # sequence left to right, 1 char at a time
95   #print "$temp2<br>";
96       $sum = $sum + ($temp1*$temp2);    # mult each char 1-7 by its corresponding weighting
97                                         # FIXME - +=
98       $i++;                             # increment counter
99     }
100     my $rem = ($sum%11);                        # remainder of sum/11 (eg. 9999999/11, remainder=2)
101     if ($rem == 10) {                   # if remainder is 10, use X instead
102       $rem = "X";
103     }  
104     $cardnumber="V$cardnumber$rem";
105   } else {
106     $cardnumber=$data->{'cardnumber'};
107   }
108   
109   if ($data->{'sex'} eq 'F'){
110     $template->param(female => 1);
111   } 
112   
113   my @titles = ('Miss', 'Mrs', 'Ms', 'Mr', 'Dr', 'Sir');
114         # FIXME - Assumes English. This ought to be made part of i18n.
115   my @titledata;
116   while (@titles) {
117     my %row;
118     my $title = shift @titles;
119     $row{'title'} = $title;
120     if ($data->{'title'} eq $title) {
121       $row{'selected'}=' selected';
122     } else {
123       $row{'selected'}='';
124     }
125     push(@titledata, \%row);
126   }
127   
128   my ($categories,$labels)=ethnicitycategories();
129   my $ethnicitycategoriescount=$#{$categories};
130   my $ethcatpopup;
131   if ($ethnicitycategoriescount>=0) {
132         $ethcatpopup = popup_menu(-name=>'ethnicity',
133                                 -values=>$categories,
134                                 -default=>$data->{'ethnicity'},
135                                 -labels=>$labels);
136         $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
137   }
138   
139   ($categories,$labels)=borrowercategories();
140   my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
141                                 -values=>$categories,
142                                 -default=>$data->{'categorycode'},
143                                 -labels=>$labels);
144   
145   my @areas = ('L','F','S','H','K','O','X','Z','V');
146   my %arealabels = ('L' => 'Levin',
147                   'F' => 'Foxton',
148                   'S' => 'Shannon',
149                   'H' => 'Horowhenua',
150                   'K' => 'Kapiti',
151                   'O' => 'Out of District',
152                   'X' => 'Temporary Visitor',
153                   'Z' => 'Interloan Libraries',
154                   'V' => 'Village');
155                   
156   my @areadata;
157   while (@areas) {
158     my %row;
159     my $shortcut = shift @areas;
160     $row{'shortcut'} = $shortcut;
161     if ($data->{'area'} eq $shortcut) {
162       $row{'selected'}=' selected';
163     } else {
164       $row{'selected'}='';
165     }
166     $row{'area'}=$arealabels{$shortcut};
167     push(@areadata, \%row);
168   }
169   
170   
171   my @relationships = ('workplace', 'relative','friend', 'neighbour');
172   my @relshipdata;
173   while (@relationships) {
174     my $relship = shift @relationships;
175     my %row = ('relationship' => $relship);
176     if ($data->{'altrelationship'} eq $relship) {
177       $row{'selected'}=' selected';
178     } else {
179       $row{'selected'}='';
180     }
181     push(@relshipdata, \%row);
182   }
183   
184   # %flags: keys=$data-keys, datas=[formname, HTML-explanation]
185   my %flags = ('gonenoaddress' => ['gna', 'Gone no address'],
186                'lost'          => ['lost', 'Lost'],
187                'debarred'      => ['debarred', 'Debarred']);
188   
189   my @flagdata;
190   foreach (keys(%flags)) {
191     my $key = $_;
192     my %row =  ('key'   => $key,
193                 'name'  => $flags{$key}[0],
194                 'html'  => $flags{$key}[1]);
195     if ($data->{$key}) {
196       $row{'yes'}=' checked';
197       $row{'no'}='';
198     } else {
199       $row{'yes'}='';
200       $row{'no'}=' checked';
201     }
202     push(@flagdata, \%row);
203   }
204
205   if ($modify){
206     $template->param( modify => 1 );
207   }
208   
209   $template->param( startmenumember => join ('', startmenu('member')),
210                         endmenumember   => join ('', endmenu('member')),
211                         member          => $member,
212                         firstname       => $data->{'firstname'},
213                         surname         => $data->{'surname'},
214                         othernames      => $data->{'othernames'},
215                         initials        => $data->{'initials'},
216                         ethcatpopup     => $ethcatpopup,
217                         catcodepopup    => $catcodepopup,
218                         streetaddress   => $data->{'physstreet'},
219                         streetcity      => $data->{'streetcity'},
220                         city            => $data->{'city'},
221                         phone           => $data->{'phone'},
222                         phoneday        => $data->{'phoneday'},
223                         faxnumber       => $data->{'faxnumber'},
224                         emailaddress    => $data->{'emailaddress'},
225                         contactname     => $data->{'contactname'},
226                         altphone        => $data->{'altphone'},
227                         altnotes        => $data->{'altnotes'},
228                         borrowernotes   => $data->{'borrowernotes'},
229                         flagloop        => \@flagdata,
230                         relshiploop     => \@relshipdata,
231                         titleloop       => \@titledata,
232                         arealoop        => \@areadata,
233                         dateenrolled    => $data->{'dateenrolled'},
234                         expiry          => $data->{'expiry'},
235                         cardnumber      => $cardnumber,
236                         dateofbirth     => $data->{'dateofbirth'});
237                         
238   print "Content-Type: text/html\n\n", $template->output;
239   
240
241 }