Get the enrollmentperiod from the database when adding members
[koha.git] / newmember.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 #script to print confirmation screen, then if accepted calls itself to insert data
6 # FIXME - Yes, but what does it _do_?
7 # 2002/12/18 hdl@ifrance.com templating
8
9 # 2003/01/20 acli@ada.dhs.org XXX it seems to do the following:
10 # * "insert" seems to do nothing; in 1.2.2 the script just returns a blank
11 #   page (with the headers etc.) if "insert" has anything in it
12 # * $ok has the opposite meaning of what one expects; $ok == 1 means "not ok"
13 # * if ($ok == 0) considers the "ok" case; it displays a confirmation page
14 #   for the user to "click to confirm that everything is entered correctly"
15 # * The "else" case for ($ok == 0) handles the "not ok" case; $string is the
16 #   error message to display
17
18 # FIXME - What is the correct value of "flagsrequired"?
19 # FIXME - untranslatable strings here
20
21 # Copyright 2000-2002 Katipo Communications
22 #
23 # This file is part of Koha.
24 #
25 # Koha is free software; you can redistribute it and/or modify it under the
26 # terms of the GNU General Public License as published by the Free Software
27 # Foundation; either version 2 of the License, or (at your option) any later
28 # version.
29 #
30 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
31 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
32 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
33 #
34 # You should have received a copy of the GNU General Public License along with
35 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
36 # Suite 330, Boston, MA  02111-1307 USA
37
38 use strict;
39 use C4::Auth;
40 use C4::Input;
41 use C4::Interface::CGI::Output;
42 use CGI;
43 use Date::Manip;
44 use HTML::Template;
45 use C4::Date;
46 my %env;
47 my $input = new CGI;
48
49 #get rest of data
50 my %data;
51 my @names=$input->param;
52 foreach my $key (@names){
53   $data{$key}=$input->param($key);
54 }
55
56 my ($template, $borrowernumber, $cookie)
57     = get_template_and_user({template_name => "newmember.tmpl",
58                              query => $input,
59                              type => "intranet",
60                              authnotrequired => 0,
61                              flagsrequired => {parameters => 1},
62                          });
63
64 #Get the database handle
65 my $dbh = C4::Context->dbh;
66
67 # Check that all compulsary fields are entered
68 # If everything is ok, set $ok = 0
69 # Otherwise set $ok = 1 and $string to the error message to display.
70
71 my $ok=0;
72 my $string = "The following compulsary fields have been left blank. "
73         . "Please push the back button and try again<p>";
74
75 if ($data{'cardnumber'} eq ''){
76     $string.=" Cardnumber<br>";
77     $ok=1;
78 } else {
79     #check cardnumber is valid
80     my $nounique;
81     if ( $data{'type'} ne "Add" )
82     {
83         $nounique = 1;
84     }
85     else
86     {
87         $nounique = 0;
88     }
89     my $valid=checkdigit(\%env,$data{'cardnumber'}, $nounique);
90     if ($valid != 1){
91         $ok=1;
92         $string.=" Invalid Cardnumber<br>";
93     }
94 }
95 if ($data{'sex'} eq ''){
96     $string.=" Gender <br>";
97     $ok=1;
98 }
99 if ($data{'firstname'} eq ''){
100     $string.=" Given Names<br>";
101     $ok=1;
102 }
103 if ($data{'surname'} eq ''){
104     $string.=" Surname<br>";
105     $ok=1;
106 }
107 if ($data{'address'} eq ''){
108     $string.=" Postal Street Address<br>";
109     $ok=1;
110 }
111 if ($data{'city'} eq ''){
112     $string.=" Postal City<br>";
113     $ok=1;
114 }
115
116 # Pass the ok/not ok status and the error message to the template
117 $template->param(       OK=> ($ok==0),
118                         string=> $string);
119
120 # If things are ok, display the confirmation page
121 if ($ok == 0) {
122     my $name=$data{'title'}." ";
123     if ($data{'othernames'} ne ''){
124         $name.=$data{'othernames'}." ";
125     } else {
126         $name.=$data{'firstname'}." ";
127     }
128     $name.="$data{'surname'} ( $data{'firstname'}, $data{'initials'})";
129     my $sex;
130     if ($data{'sex'} eq 'M'){
131         $sex="Male";
132     } else {
133         $sex="Female";
134     }
135     if ($data{'joining'} eq ''){
136         $data{'joining'}=ParseDate('today');
137         $data{'joining'}=format_date($data{'joining'});
138     }
139     if ($data{'expiry'} eq ''){
140         my $get_enrolmentperiod = $dbh->prepare(q{SELECT enrolmentperiod FROM categories WHERE categorycode = ?});
141         $get_enrolmentperiod->execute($data{'categorycode'});
142         my ( $period ) = $get_enrolmentperiod->fetchrow;
143         if ( ($period)  && ($period != 1))
144         {
145                 $data{'expiry'}=ParseDate("in $period years");
146                 $data{'expiry'}=format_date($data{'expiry'});
147         }
148         else
149         {
150                 $data{'expiry'}=ParseDate('in 1 year');
151                 $data{'expiry'}=format_date($data{'expiry'});
152         }
153     }
154     my $ethnic=$data{'ethnicity'}." ".$data{'ethnicnotes'};
155     my $postal=$data{'address'}."<br>".$data{'city'};
156     my $home;
157     if ($data{'streetaddress'} ne ''){
158         $home=$data{'streetaddress'}."<br>".$data{'streetcity'};
159     } else {
160         $home=$postal;
161     }
162     my @inputsloop;
163     while (my ($key, $value) = each %data) {
164         $value=~ s/\"/%22/g;
165         my %line;
166         $line{'key'}=$key;
167         $line{'value'}=$value;
168         push(@inputsloop, \%line);
169     }
170
171     #Get the fee
172     my $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode = ?");
173     $sth->execute($data{'categorycode'});
174     my ($fee) = $sth->fetchrow;
175     $sth->finish;
176
177     $template->param(name => $name,
178                      bornum => $data{'borrowernumber'},
179                      cardnum => $data{'cardnumber'},
180                      memcat => $data{'categorycode'},
181                      fee => $fee,
182                      joindate => format_date($data{'joining'}),
183                      expdate => format_date($data{'expiry'}),
184                      branchcode => $data{'branchcode'},
185                      ethnic => $ethnic,
186                      dob => format_date($data{'dateofbirth'}),
187                      sex => $sex,
188                      postal => $postal,
189                      home => $home,
190                         zipcode => $data{'zipcode'},
191                         homezipcode => $data{'homezipcode'},
192                      phone => $data{'phone'},
193                      phoneday => $data{'phoneday'},
194                      faxnumber => $data{'faxnumber'},
195                      emailaddress => $data{'emailaddress'},
196                         textmessaging => $data{'textmessaging'},
197                      contactname => $data{'contactname'},
198                      altphone => $data{'altphone'},
199                      altrelationship => $data{'altrelationship'},
200                      altnotes => $data{'altnotes'},
201                      bornotes => $data{'borrowernotes'},
202                      inputsloop => \@inputsloop);
203
204 # If things are not ok, display the error message
205 } else {
206     # Nothing to do; the "OK" and "string" variables have already been set
207     ;
208 }
209
210 output_html_with_http_headers $input, $cookie, $template->output;
211
212