add spouse_birthdate, RT#17577
[freeside.git] / httemplate / edit / cust_main.cgi
1 <& /elements/header.html,
2       $title,
3       '',
4       ' onUnload="myclose()"' #hmm, in billing.html
5 &>
6
7 <& /elements/error.html &>
8
9 <FORM NAME   = "CustomerForm"
10       METHOD = "POST"
11       ACTION = "<% popurl(1) %>process/cust_main.cgi"
12 >
13
14 <INPUT TYPE="hidden" NAME="custnum"     VALUE="<% $custnum %>">
15 <INPUT TYPE="hidden" NAME="prospectnum" VALUE="<% $prospectnum %>">
16
17 % if ( $custnum ) { 
18   <% mt('Customer #') |h %><B><% $cust_main->display_custnum %></B> - 
19   <B><FONT COLOR="#<% $cust_main->statuscolor %>">
20     <% ucfirst($cust_main->status) %>
21   </FONT></B>
22   <BR><BR>
23 % } 
24
25 %# agent, agent_custid, refnum (advertising source), referral_custnum
26 <& cust_main/top_misc.html, $cust_main, 'custnum' => $custnum  &>
27
28 %# birthdate
29 % if (    $conf->exists('cust_main-enable_birthdate')
30 %      || $conf->exists('cust_main-enable_spouse_birthdate')
31 %    )
32 % {
33   <BR>
34   <& cust_main/birthdate.html, $cust_main &>
35 % }
36
37 %# contact info
38
39 %  my $same_checked = '';
40 %  my $ship_disabled = '';
41 %  my @ship_style = ();
42 %  unless ( $cust_main->ship_last && $same ne 'Y' ) {
43 %    $same_checked = 'CHECKED';
44 %    $ship_disabled = 'DISABLED';
45 %    push @ship_style, 'background-color:#dddddd';
46 %    foreach (
47 %      qw( last first company address1 address2 city county state zip country
48 %          latitude longitude coord_auto
49 %          daytime night fax mobile )
50 %    ) {
51 %      $cust_main->set("ship_$_", $cust_main->get($_) );
52 %    }
53 %  }
54
55 <BR>
56 <FONT CLASS="fsinnerbox-title"><% mt('Billing address') |h %></FONT>
57
58 <& cust_main/contact.html,
59              'cust_main'    => $cust_main,
60              'pre'          => '',
61              'onchange'     => 'bill_changed(this)',
62              'disabled'     => '',
63              'ss'           => $ss,
64              'stateid'      => $stateid,
65              'same_checked' => $same_checked, #for address2 "Unit #" labeling
66 &>
67
68 <SCRIPT>
69 function bill_changed(what) {
70   if ( what.form.same.checked ) {
71 % for (qw( last first company address1 address2 city zip latitude longitude coord_auto daytime night fax mobile )) { 
72     what.form.ship_<%$_%>.value = what.form.<%$_%>.value;
73 % } 
74
75     what.form.ship_country.selectedIndex = what.form.country.selectedIndex;
76
77     function fix_ship_city() {
78       what.form.ship_city_select.selectedIndex = what.form.city_select.selectedIndex;
79       what.form.ship_city.style.display = what.form.city.style.display;
80       what.form.ship_city_select.style.display = what.form.city_select.style.display;
81     }
82
83     function fix_ship_county() {
84       what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
85       ship_county_changed(what.form.ship_county, fix_ship_city );
86     }
87
88     function fix_ship_state() {
89       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
90       ship_state_changed(what.form.ship_state, fix_ship_county );
91     }
92
93     ship_country_changed(what.form.ship_country, fix_ship_state );
94
95   }
96 }
97 function samechanged(what) {
98   if ( what.checked ) {
99     bill_changed(what);
100
101 %   my @fields = qw( last first company address1 address2 city city_select county state zip country latitude longitude daytime night fax mobile );
102 %   for (@fields) { 
103       what.form.ship_<%$_%>.disabled = true;
104       what.form.ship_<%$_%>.style.backgroundColor = '#dddddd';
105 %   } 
106
107 %   if ( $conf->exists('cust_main-require_address2') ) {
108       document.getElementById('address2_required').style.visibility = '';
109       document.getElementById('address2_label').style.visibility = '';
110       document.getElementById('ship_address2_required').style.visibility = 'hidden';
111       document.getElementById('ship_address2_label').style.visibility = 'hidden';
112 %   }
113
114   } else {
115
116 %   for (@fields) { 
117       what.form.ship_<%$_%>.disabled = false;
118       what.form.ship_<%$_%>.style.backgroundColor = '#ffffff';
119 %   } 
120
121 %   if ( $conf->exists('cust_main-require_address2') ) {
122       document.getElementById('address2_required').style.visibility = 'hidden';
123       document.getElementById('address2_label').style.visibility = 'hidden';
124       document.getElementById('ship_address2_required').style.visibility = '';
125       document.getElementById('ship_address2_label').style.visibility = '';
126 %   }
127
128   }
129 }
130 </SCRIPT>
131
132 <BR>
133 <FONT CLASS="fsinnerbox-title"><% mt('Service address') |h %></FONT>
134
135 <INPUT TYPE="checkbox" NAME="same" VALUE="Y" onClick="samechanged(this)" <%$same_checked%>><% mt('same as billing address') |h %>
136 <& cust_main/contact.html,
137              'cust_main' => $cust_main,
138              'pre'       => 'ship_',
139              'onchange'  => '',
140              'disabled'  => $ship_disabled,
141              'style'     => \@ship_style
142 &>
143
144 <& cust_main/contacts_new.html,
145              'cust_main' => $cust_main,
146 &>
147
148 %# billing info
149 <& cust_main/billing.html, $cust_main,
150                'payinfo'        => $payinfo,
151                'invoicing_list' => \@invoicing_list,
152 &>
153 <BR>
154
155 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
156 % if (!$ro_comments || $cust_main->comments) {
157
158     <BR>
159     <FONT CLASS="fsinnerbox-title"><% mt('Comments') |h %></FONT>
160     <TABLE CLASS="fsinnerbox">
161       <TR>
162         <TD>
163           <TEXTAREA NAME = "comments"
164                     COLS = 80
165                     ROWS = 5
166                     WRAP = "HARD"
167                     <% $ro_comments %>
168           ><% $cust_main->comments %></TEXTAREA>
169         </TD>
170       </TR>
171     </TABLE>
172
173 % }
174
175 % unless ( $custnum ) {
176
177     <& cust_main/first_pkg.html, $cust_main,
178                  'pkgpart_svcpart' => $pkgpart_svcpart,
179                  'disable_empty'   =>
180                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
181                  'username'        => $username,
182                  'password'        => $password,
183                  'popnum'          => $popnum,
184                  'saved_domsvc'    => $saved_domsvc,
185                  %svc_phone,
186                  %svc_dsl,
187     &>
188
189 % }
190
191 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
192
193 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
194
195 %# cust_main/bottomfixup.js
196 % foreach my $hidden (
197 %    'payauto', 'billday',
198 %    'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
199 %    'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
200 %    'paystart_month', 'paystart_year', 'payissue',
201 %    'payip',
202 %    'paid',
203 % ) {
204     <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
205 % } 
206
207 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
208
209 <BR>
210 <INPUT TYPE    = "button"
211        NAME    = "submitButton"
212        ID      = "submitButton"
213        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
214        onClick = "this.disabled=true; bottomfixup(this.form);"
215 >
216 <BR><BR>
217 </FORM>
218
219 <& /elements/footer.html &>
220
221 <%init>
222
223 my $curuser = $FS::CurrentUser::CurrentUser;
224
225 #probably redundant given the checks below...
226 die "access denied"
227   unless $curuser->access_right('New customer')
228      ||  $curuser->access_right('Edit customer');
229
230 my $conf = new FS::Conf;
231
232 #get record
233
234 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
235 my $same = '';
236 my $pkgpart_svcpart = ''; #first_pkg
237 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
238 my %svc_phone = ();
239 my %svc_dsl = ();
240 my $prospectnum = '';
241 my $locationnum = '';
242
243 if ( $cgi->param('error') ) {
244
245   $cust_main = new FS::cust_main ( {
246     map { $_, scalar($cgi->param($_)) } fields('cust_main')
247   } );
248
249   $custnum = $cust_main->custnum;
250
251   die "access denied"
252     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
253
254   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
255   $same = $cgi->param('same');
256   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
257   $ss = $cust_main->ss;           # don't mask an entered value on errors
258   $stateid = $cust_main->stateid; # don't mask an entered value on errors
259   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
260
261   $prospectnum = $cgi->param('prospectnum') || '';
262
263   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
264
265   $locationnum = $cgi->param('locationnum') || '';
266
267   #svc_acct
268   $username = $cgi->param('username');
269   $password = $cgi->param('_password');
270   $popnum = $cgi->param('popnum');
271   $saved_domsvc = $cgi->param('domsvc') || '';
272   if ( $saved_domsvc =~ /^(\d+)$/ ) {
273     $saved_domsvc = $1;
274   } else {
275     $saved_domsvc = '';
276   }
277
278   #svc_phone
279   $svc_phone{$_} = $cgi->param($_)
280     foreach qw( countrycode phonenum sip_password pin phone_name );
281
282   #svc_dsl (phonenum came in with svc_phone)
283   $svc_phone{$_} = $cgi->param($_)
284     foreach qw( password isp_chg isp_prev vendor_qual_id );
285
286 } elsif ( $cgi->keywords ) { #editing
287
288   die "access denied"
289     unless $curuser->access_right('Edit customer');
290
291   my( $query ) = $cgi->keywords;
292   $query =~ /^(\d+)$/;
293   $custnum=$1;
294   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
295   if ( $cust_main->dbdef_table->column('paycvv')
296        && length($cust_main->paycvv)             ) {
297     my $paycvv = $cust_main->paycvv;
298     $paycvv =~ s/./*/g;
299     $cust_main->paycvv($paycvv);
300   }
301   @invoicing_list = $cust_main->invoicing_list;
302   $ss = $cust_main->masked('ss');
303   $stateid = $cust_main->masked('stateid');
304   $payinfo = $cust_main->paymask;
305
306 } else { #new customer
307
308   die "access denied"
309     unless $curuser->access_right('New customer');
310
311   $custnum='';
312   $cust_main = new FS::cust_main ( {} );
313   $cust_main->agentnum( $conf->config('default_agentnum') )
314     if $conf->exists('default_agentnum');
315   $cust_main->otaker( &getotaker );
316   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
317   @invoicing_list = ();
318   push @invoicing_list, 'POST'
319     unless $conf->exists('disablepostalinvoicedefault');
320   $ss = '';
321   $stateid = '';
322   $payinfo = '';
323
324   if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
325     my $qualnum = $1;
326     my $qual = qsearchs('qual', { 'qualnum' => $qualnum } )
327       or die "unknown qualnum $qualnum";
328
329     my $prospect_main = $qual->cust_or_prospect;
330     $prospectnum = $prospect_main->prospectnum
331       or die "qualification not on a prospect";
332
333     $cust_main->agentnum( $prospect_main->agentnum );
334     $cust_main->company(  $prospect_main->company  );
335
336     #first contact? -> name
337     my @contacts = $prospect_main->contact;
338     my $contact = $contacts[0];
339     $cust_main->first( $contact->first );
340     $cust_main->set( 'last', $contact->get('last') );
341     #contact phone numbers?
342
343     #location -> address  (all prospect quals have location, right?)
344     my $cust_location = $qual->cust_location;
345     $cust_location->dealternize;
346     $cust_main->$_( $cust_location->$_ )
347       foreach qw( address1 address2 city county state zip country latitude longitude coord_auto geocode );
348
349     #locationnum -> package order
350     $locationnum = $qual->locationnum;
351
352     #pkgpart handled by lock_pkgpart below
353
354     #service telephone & vendor_qual_id -> svc_dsl
355     $svc_dsl{$_} = $qual->$_
356       foreach qw( phonenum vendor_qual_id );
357   }
358
359   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
360     my $pkgpart = $1;
361     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
362       or die "unknown pkgpart $pkgpart";
363     my $svcpart = $part_pkg->svcpart;
364     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
365   }
366
367 }
368
369 my %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
370 $cgi->delete( grep !$keep{$_}, $cgi->param );
371
372 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
373 $title = mt($title);
374 $title .= ": ". $cust_main->name if $custnum;
375
376 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
377
378 </%init>