lengthen cardnumber to 16 and make it unique
[koha.git] / insertdata.pl
1 #!/usr/bin/perl
2
3 #script to enter borrower data into the data base
4 #needs to be moved into a perl module
5 # written 9/11/99 by chris@katipo.co.nz
6
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use CGI;
26 use C4::Context;
27 use C4::Input;
28 use C4::Search;
29 use Date::Manip;
30 use C4::Date;
31 use strict;
32
33 my $input= new CGI;
34
35 #get all the data into a hash
36 my @names=$input->param;
37 my %data;
38 my $keyfld;
39 my $keyval;
40 my $problems;
41 my $env;
42 foreach my $key (@names){
43   $data{$key}=$input->param($key);
44   $data{$key}=~ s/\'/\\\'/g;
45   $data{$key}=~ s/\"/\\\"/g;
46 }
47 my $dbh = C4::Context->dbh;
48 my $query="Select * from borrowers where borrowernumber=?";
49 my $sth=$dbh->prepare($query);
50 $sth->execute($data{'borrowernumber'});
51 if (my $data2=$sth->fetchrow_hashref){
52   $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
53   $data{'joining'}=format_date_in_iso($data{'joining'});
54   $data{'expiry'}=format_date_in_iso($data{'expiry'});
55   $query="update borrowers set title='$data{'title'}',expiry='$data{'expiry'}',
56   cardnumber='$data{'cardnumber'}',sex='$data{'sex'}',ethnotes='$data{'ethnicnotes'}',
57   streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',firstname='$data{'firstname'}',
58   altnotes='$data{'altnotes'}',dateofbirth='$data{'dateofbirth'}',contactname='$data{'contactname'}',
59   emailaddress='$data{'emailaddress'}',dateenrolled='$data{'joining'}',streetcity='$data{'streetcity'}',
60   altrelationship='$data{'altrelationship'}',othernames='$data{'othernames'}',phoneday='$data{'phoneday'}',
61   categorycode='$data{'categorycode'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}',
62   borrowernotes='$data{'borrowernotes'}',altphone='$data{'altphone'}',surname='$data{'surname'}',
63   initials='$data{'initials'}',physstreet='$data{'streetaddress'}',ethnicity='$data{'ethnicity'}',
64   gonenoaddress='$data{'gna'}',lost='$data{'lost'}',debarred='$data{'debarred'}',
65   textmessaging='$data{'textmessaging'}', branchcode = '$data{'branchcode'}',
66   zipcode = '$data{'zipcode'}',homezipcode='$data{'homezipcode'}'
67   where borrowernumber=$data{'borrowernumber'}";
68
69 }else{
70   $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
71   $data{'joining'}=format_date_in_iso($data{'joining'});
72   $data{'expiry'}=format_date_in_iso($data{'expiry'});
73     $data{'borrowernumber'}=NewBorrowerNumber();
74   $query="insert into borrowers (title,expiry,cardnumber,sex,ethnotes,streetaddress,faxnumber,
75   firstname,altnotes,dateofbirth,contactname,emailaddress,textmessaging,dateenrolled,streetcity,
76   altrelationship,othernames,phoneday,categorycode,city,area,phone,borrowernotes,altphone,surname,
77   initials,ethnicity,physstreet,branchcode,zipcode,homezipcode) values ('$data{'title'}','$data{'expiry'}','$data{'cardnumber'}',
78   '$data{'sex'}','$data{'ethnotes'}','$data{'address'}','$data{'faxnumber'}',
79   '$data{'firstname'}','$data{'altnotes'}','$data{'dateofbirth'}','$data{'contactname'}','$data{'emailaddress'}','$data{'textmessaging'}',
80   '$data{'joining'}','$data{'streetcity'}','$data{'altrelationship'}','$data{'othernames'}',
81   '$data{'phoneday'}','$data{'categorycode'}','$data{'city'}','$data{'area'}','$data{'phone'}',
82   '$data{'borrowernotes'}','$data{'altphone'}','$data{'surname'}','$data{'initials'}',
83   '$data{'ethnicity'}','$data{'streetaddress'}','$data{'branchcode'}','$data{'zipcode'}','$data{'homezipcode'}')";
84 }
85 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
86 # so when we update information for an adult we should check for guarantees and update the relevant part
87 # of their records, ie addresses and phone numbers
88
89 if ($data{'categorycode'} eq 'A' || $data{'categorycode'} eq 'W'){
90     # is adult check guarantees;
91     my ($count,$guarantees)=findguarantees($data{'borrowernumber'});
92     for (my $i=0;$i<$count;$i++){
93         # FIXME
94         # It looks like the $i is only being returned to handle walking through
95         # the array, which is probably better done as a foreach loop.
96         #
97         my $guaquery="update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
98         streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
99         ,streetaddress='$data{'address'}'
100         where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
101         my $sth3=$dbh->prepare($guaquery);
102         $sth3->execute;
103         $sth3->finish;
104      }
105 }
106
107   my $sth2=$dbh->prepare($query);
108   $sth2->execute;
109   $sth2->finish;
110 $sth->finish;
111 print $input->redirect("/cgi-bin/koha/moremember.pl?bornum=$data{'borrowernumber'}");