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