address standardization part one, finally checked in from here
[freeside.git] / httemplate / edit / cust_main.cgi
1 <% include('/elements/header.html',
2       "Customer $action",
3       '',
4       ' onUnload="myclose()"'
5 ) %>
6
7 <% include('/elements/error.html') %>
8
9 <FORM NAME="topform" STYLE="margin-bottom: 0">
10 <INPUT TYPE="hidden" NAME="custnum" VALUE="<% $custnum %>">
11 % if ( $custnum ) { 
12
13   Customer #<B><% $custnum %></B> - 
14   <B><FONT COLOR="#<% $cust_main->statuscolor %>">
15     <% ucfirst($cust_main->status) %>
16   </FONT></B>
17   <BR><BR>
18 % } 
19
20
21 <% &ntable("#cccccc") %>
22
23 <!-- agent -->
24
25 <% include('/elements/tr-select-agent.html', 
26               'curr_value'    => $cust_main->agentnum,
27               'label'         => "<B>${r}Agent</B>",
28               'empty_label'   => 'Select agent',
29               'disable_empty' => ( $cust_main->agentnum ? 1 : 0 ),
30            )
31 %>
32
33 <!-- referral (advertising source) -->
34 %
35 %my $refnum = $cust_main->refnum || $conf->config('referraldefault') || 0;
36 %if ( $custnum && ! $conf->exists('editreferrals') ) {
37 %
38
39
40   <INPUT TYPE="hidden" NAME="refnum" VALUE="<% $refnum %>">
41 % } else { 
42
43
44    <% include('/elements/tr-select-part_referral.html',
45                 'curr_value' => $refnum
46              )
47    %>
48 % } 
49
50
51 <!-- referring customer -->
52 %
53 %my $referring_cust_main = '';
54 %if ( $cust_main->referral_custnum
55 %     and $referring_cust_main =
56 %           qsearchs('cust_main', { custnum => $cust_main->referral_custnum } )
57 %) {
58 %
59
60
61   <TR>
62     <TD ALIGN="right">Referring customer</TD>
63     <TD>
64       <A HREF="<% popurl(1) %>/cust_main.cgi?<% $cust_main->referral_custnum %>"><% $cust_main->referral_custnum %>: <% $referring_cust_main->name %></A>
65     </TD>
66   </TR>
67   <INPUT TYPE="hidden" NAME="referral_custnum" VALUE="<% $cust_main->referral_custnum %>">
68 % } elsif ( ! $conf->exists('disable_customer_referrals') ) { 
69
70
71   <TR>
72     <TD ALIGN="right">Referring customer</TD>
73     <TD>
74       <!-- <INPUT TYPE="text" NAME="referral_custnum" VALUE=""> -->
75       <% include('/elements/search-cust_main.html',
76                     'field_name' => 'referral_custnum',
77                  )
78       %>
79     </TD>
80   </TR>
81 % } else { 
82
83
84   <INPUT TYPE="hidden" NAME="referral_custnum" VALUE="">
85 % } 
86
87
88 </TABLE>
89
90 <!-- birthdate -->
91
92 % if ( $conf->exists('cust_main-enable_birthdate') ) {
93
94   <BR>
95   <% ntable("#cccccc", 2) %>
96   <% include ('/elements/tr-input-date-field.html',
97               'birthdate',
98               $cust_main->birthdate,
99               'Date of Birth',
100               $conf->config('date_format') || "%m/%d/%Y",
101               1)
102   %>
103
104   </TABLE>
105
106 % }
107
108 <!-- contact info -->
109
110 %  my $same_checked = '';
111 %  my $ship_disabled = '';
112 %  unless ( $cust_main->ship_last && $same ne 'Y' ) {
113 %    $same_checked = 'CHECKED';
114 %    $ship_disabled = 'DISABLED STYLE="background-color: #dddddd"';
115 %    foreach (
116 %      qw( last first company address1 address2 city county state zip country
117 %          daytime night fax )
118 %    ) {
119 %      $cust_main->set("ship_$_", $cust_main->get($_) );
120 %    }
121 %  }
122
123 <BR><BR>
124 Billing address
125 <% include('cust_main/contact.html',
126              'cust_main'    => $cust_main,
127              'pre'          => '',
128              'onchange'     => 'bill_changed(this)',
129              'disabled'     => '',
130              'ss'           => $ss,
131              'stateid'      => $stateid,
132              'same_checked' => $same_checked, #for address2 "Unit #" labeling
133           )
134 %>
135
136 <SCRIPT>
137 function bill_changed(what) {
138   if ( what.form.same.checked ) {
139 % for (qw( last first company address1 address2 city zip daytime night fax )) { 
140
141     what.form.ship_<%$_%>.value = what.form.<%$_%>.value;
142 % } 
143
144     what.form.ship_country.selectedIndex = what.form.country.selectedIndex;
145
146     function fix_ship_county() {
147       what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
148     }
149
150     function fix_ship_state() {
151       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
152       ship_state_changed(what.form.ship_state, fix_ship_county );
153     }
154
155     ship_country_changed(what.form.ship_country, fix_ship_state );
156
157   }
158 }
159 function samechanged(what) {
160   if ( what.checked ) {
161     bill_changed(what);
162
163 %   for (qw( last first company address1 address2 city county state zip country daytime night fax )) { 
164       what.form.ship_<%$_%>.disabled = true;
165       what.form.ship_<%$_%>.style.backgroundColor = '#dddddd';
166 %   } 
167
168 %   if ( $conf->exists('cust_main-require_address2') ) {
169       document.getElementById('address2_required').style.visibility = '';
170       document.getElementById('address2_label').style.visibility = '';
171       document.getElementById('ship_address2_required').style.visibility = 'hidden';
172       document.getElementById('ship_address2_label').style.visibility = 'hidden';
173 %   }
174
175   } else {
176
177 %   for (qw( last first company address1 address2 city county state zip country daytime night fax )) { 
178       what.form.ship_<%$_%>.disabled = false;
179       what.form.ship_<%$_%>.style.backgroundColor = '#ffffff';
180 %   } 
181
182 %   if ( $conf->exists('cust_main-require_address2') ) {
183       document.getElementById('address2_required').style.visibility = 'hidden';
184       document.getElementById('address2_label').style.visibility = 'hidden';
185       document.getElementById('ship_address2_required').style.visibility = '';
186       document.getElementById('ship_address2_label').style.visibility = '';
187 %   }
188
189   }
190 }
191 </SCRIPT>
192
193 <BR>
194 Service address 
195 (<INPUT TYPE="checkbox" NAME="same" VALUE="Y" onClick="samechanged(this)" <%$same_checked%>>same as billing address)
196 <% include('cust_main/contact.html',
197              'cust_main' => $cust_main,
198              'pre'       => 'ship_',
199              'onchange'  => '',
200              'disabled'  => $ship_disabled,
201           )
202 %>
203
204
205 <!-- billing info -->
206
207 <% include( 'cust_main/billing.html', $cust_main,
208                'payinfo'        => $payinfo,
209                'invoicing_list' => \@invoicing_list,
210            )
211 %>
212
213 <% include( '/elements/xmlhttp.html',
214               'url'  => $p.'misc/xmlhttp-cust_main-address_standardize.html',
215               'subs' => [ 'address_standardize' ],
216               #'method' => 'POST', #could get too long?
217           )
218 %>
219
220 <SCRIPT>
221 function bottomfixup(what) {
222
223   //i don't think we need to copy things between two forms anymore, modern
224   //browsers are fine with DIVs inside FORMs
225
226   var topvars = new Array(
227     'birthdate',
228
229     'custnum', 'agentnum', 'refnum', 'referral_custnum',
230
231     'last', 'first', 'ss', 'company',
232     'address1', 'address2', 'city',
233     'county', 'state', 'zip', 'country',
234     'daytime', 'night', 'fax',
235     'stateid', 'stateid_state',
236
237     'same',
238
239     'ship_last', 'ship_first', 'ship_company',
240     'ship_address1', 'ship_address2', 'ship_city',
241     'ship_county', 'ship_state', 'ship_zip', 'ship_country',
242     'ship_daytime','ship_night', 'ship_fax',
243
244     'select' // XXX key
245   );
246
247   var layervars = new Array(
248     'payauto',
249     'payinfo', 'payinfo1', 'payinfo2', 'paytype',
250     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
251     'paystart_month', 'paystart_year', 'payissue',
252     'payip',
253     'paid'
254   );
255
256   var billing_bottomvars = new Array(
257     'tax',
258     'invoicing_list', 'invoicing_list_POST', 'invoicing_list_FAX',
259     'invoice_terms',
260     'spool_cdr'
261   );
262
263   for ( f=0; f < topvars.length; f++ ) {
264     var field = topvars[f];
265     copyelement( document.topform.elements[field],
266                  document.bottomform.elements[field]
267                );
268   }
269
270   var layerform = document.topform.select.options[document.topform.select.selectedIndex].value;
271   for ( f=0; f < layervars.length; f++ ) {
272     var field = layervars[f];
273     copyelement( document.forms[layerform].elements[field],
274                  document.bottomform.elements[field]
275                );
276   }
277
278   for ( f=0; f < billing_bottomvars.length; f++ ) {
279     var field = billing_bottomvars[f];
280     copyelement( document.billing_bottomform.elements[field],
281                  document.bottomform.elements[field]
282                );
283   }
284
285   //this part does USPS address correction
286
287   // XXX should this be first and should we update the form fields that are
288   // displayed???
289
290   //var state_el = document.bottomform.elements['state'];
291
292   //address_standardize(
293   var cust_main = new Array(
294     'company',  document.bottomform.elements['company'].value,
295     'address1', document.bottomform.elements['address1'].value,
296     'address2', document.bottomform.elements['address2'].value,
297     'city',     document.bottomform.elements['city'].value,
298     'state',    document.bottomform.elements['state'].value,
299     //'state',    state_el.options[ state_el.selectedIndex ].value,
300     'zip',      document.bottomform.elements['zip'].value,
301
302     'ship_company',  document.bottomform.elements['company'].value,
303     'ship_address1', document.bottomform.elements['address1'].value,
304     'ship_address2', document.bottomform.elements['address2'].value,
305     'ship_city',     document.bottomform.elements['city'].value,
306     'ship_state',    document.bottomform.elements['state'].value,
307     //'ship_state',    state_el.options[ state_el.selectedIndex ].value,
308     'ship_zip',      document.bottomform.elements['zip'].value
309   );
310
311   address_standardize( cust_main, update_address );
312
313 }
314
315 function update_address(arg) {
316
317   var argsHash = eval('(' + arg + ')');
318
319   var address1 = argsHash['address1'];
320   var zip      = argsHash['zip'];
321   var changed  = argsHash['address_standardized'];
322   var ship_changed = argsHash['ship_address_standardized'];
323
324   alert(address1);
325   alert(zip);
326   alert(changed);
327   alert(ship_changed);
328
329 % if ( $conf->exists('cust_main-auto_standardize_address') ) {
330   // XXX this path not handled yet
331 % } else {
332   // XXX well, this path not handled yet either.  popup a confirmation popup
333 % }
334
335   document.bottomform.submit();
336
337 }
338
339 function copyelement(from, to) {
340   if ( from == undefined ) {
341     to.value = '';
342   } else if ( from.type == 'select-one' ) {
343     to.value = from.options[from.selectedIndex].value;
344     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
345   } else if ( from.type == 'checkbox' ) {
346     if ( from.checked ) {
347       to.value = from.value;
348     } else {
349       to.value = '';
350     }
351   } else {
352     if ( from.value == undefined ) {
353       to.value = '';
354     } else {
355       to.value = from.value;
356     }
357   }
358   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
359 }
360
361 </SCRIPT>
362
363 <FORM ACTION="<% popurl(1) %>process/cust_main.cgi" METHOD=POST NAME="bottomform" STYLE="margin-top: 0; margin-bottom: 0">
364 % foreach my $hidden (
365 %     'birthdate',
366 %
367 %     'custnum', 'agentnum', 'refnum', 'referral_custnum',
368 %     'last', 'first', 'ss', 'company',
369 %     'address1', 'address2', 'city',
370 %     'county', 'state', 'zip', 'country',
371 %     'daytime', 'night', 'fax',
372 %     'stateid', 'stateid_state',
373 %     
374 %     'same',
375 %     
376 %     'ship_last', 'ship_first', 'ship_company',
377 %     'ship_address1', 'ship_address2', 'ship_city',
378 %     'ship_county', 'ship_state', 'ship_zip', 'ship_country',
379 %     'ship_daytime','ship_night', 'ship_fax',
380 %     
381 %     'select', #XXX key
382 %
383 %     'payauto',
384 %     'payinfo', 'payinfo1', 'payinfo2', 'paytype',
385 %     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
386 %     'paystart_month', 'paystart_year', 'payissue',
387 %     'payip',
388 %     'paid',
389 %     
390 %     'tax',
391 %     'invoicing_list', 'invoicing_list_POST', 'invoicing_list_FAX',
392 %     'invoice_terms',
393 %     'spool_cdr'
394 %   ) {
395 %
396
397   <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
398 % } 
399 %
400 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
401 % if (!$ro_comments || $cust_main->comments) {
402
403 <BR>Comments
404 <% &ntable("#cccccc") %>
405   <TR>
406     <TD>
407       <TEXTAREA COLS=80 ROWS=5 WRAP="HARD" NAME="comments" <%$ro_comments%>><% $cust_main->comments %></TEXTAREA>
408     </TD>
409   </TR>
410 </TABLE>
411 %
412 % }
413 %
414 %unless ( $custnum ) {
415 %  # pry the wrong place for this logic.  also pretty expensive
416 %  #use FS::part_pkg;
417 %
418 %  #false laziness, copied from FS::cust_pkg::order
419 %  my $pkgpart;
420 %  my @agents = $FS::CurrentUser::CurrentUser->agents;
421 %  if ( scalar(@agents) == 1 ) {
422 %    # $pkgpart->{PKGPART} is true iff $custnum may purchase PKGPART
423 %    $pkgpart = $agents[0]->pkgpart_hashref;
424 %  } else {
425 %    #can't know (agent not chosen), so, allow all
426 %    my %typenum;
427 %    foreach my $agent ( @agents ) {
428 %      next if $typenum{$agent->typenum}++;
429 %      #fixed in 5.004_05 #$pkgpart->{$_}++ foreach keys %{ $agent->pkgpart_hashref }
430 %      foreach ( keys %{ $agent->pkgpart_hashref } ) { $pkgpart->{$_}++; } #5.004_04 workaround
431 %    }
432 %  }
433 %  #eslaf
434 %
435 %  my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
436 %    qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
437 %
438 %  if ( @part_pkg ) {
439 %
440 %    #    print "<BR><BR>First package", &itable("#cccccc", "0 ALIGN=LEFT"),
441 %    #apiabuse & undesirable wrapping
442 %
443 %    
444
445     <BR>First package
446     <% ntable("#cccccc") %>
447     
448       <TR>
449         <TD COLSPAN=2>
450           <% include('cust_main/select-domain.html',
451                        'pkgparts'      => \@part_pkg,
452                        'saved_pkgpart' => $saved_pkgpart,
453                        'saved_domsvc' => $saved_domsvc,
454                     )
455           %>
456         </TD>
457       </TR>
458
459 %        #false laziness: (mostly) copied from edit/svc_acct.cgi
460 %        #$ulen = $svc_acct->dbdef_table->column('username')->length;
461 %        my $ulen = dbdef->table('svc_acct')->column('username')->length;
462 %        my $ulen2 = $ulen+2;
463 %        my $passwordmax = $conf->config('passwordmax') || 8;
464 %        my $pmax2 = $passwordmax + 2;
465 %      
466
467     
468       <TR>
469         <TD ALIGN="right">Username</TD>
470         <TD>
471           <INPUT TYPE="text" NAME="username" VALUE="<% $username %>" SIZE=<% $ulen2 %> MAXLENGTH=<% $ulen %>>
472         </TD>
473       </TR>
474     
475       <TR>
476         <TD ALIGN="right">Domain</TD>
477         <TD>
478           <SELECT NAME="domsvc">
479             <OPTION>(none)</OPTION>
480           </SELECT>
481         </TD>
482       </TR>
483     
484       <TR>
485         <TD ALIGN="right">Password</TD>
486         <TD>
487           <INPUT TYPE="text" NAME="_password" VALUE="<% $password %>" SIZE=<% $pmax2 %> MAXLENGTH=<% $passwordmax %>>
488           (blank to generate)
489         </TD>
490       </TR>
491     
492       <TR>
493         <TD ALIGN="right">Access number</TD>
494         <TD><% FS::svc_acct_pop::popselector($popnum) %></TD>
495       </TR>
496     </TABLE>
497 % } 
498 % } 
499
500
501 <INPUT TYPE="hidden" NAME="otaker" VALUE="<% $cust_main->otaker %>">
502 <BR>
503 <INPUT TYPE="button" NAME="submitButton" ID="submitButton" VALUE="<% $custnum ?  "Apply Changes" : "Add Customer" %>" onClick="document.bottomform.submitButton.disabled=true; bottomfixup(this.form);">
504 <BR>
505 </FORM>
506
507 <% include('/elements/footer.html') %>
508
509 <%init>
510
511 die "access denied"
512   unless $FS::CurrentUser::CurrentUser->access_right('Edit customer');
513
514 #for misplaced logic below
515 #use FS::part_pkg;
516
517 #for false laziness below (now more properly lazy)
518 #use FS::svc_acct_pop;
519
520 #for (other) false laziness below
521 #use FS::agent;
522 #use FS::type_pkgs;
523
524 my $conf = new FS::Conf;
525
526 #get record
527
528 my($custnum, $username, $password, $popnum, $cust_main, $saved_pkgpart, $saved_domsvc);
529 my(@invoicing_list);
530 my ($ss,$stateid,$payinfo);
531 my $same = '';
532 if ( $cgi->param('error') ) {
533   $cust_main = new FS::cust_main ( {
534     map { $_, scalar($cgi->param($_)) } fields('cust_main')
535   } );
536   $custnum = $cust_main->custnum;
537   $saved_domsvc = $cgi->param('domsvc') || '';
538   if ( $saved_domsvc =~ /^(\d+)$/ ) {
539     $saved_domsvc = $1;
540   } else {
541     $saved_domsvc = '';
542   }
543   $saved_pkgpart = $cgi->param('pkgpart_svcpart') || '';
544   if ( $saved_pkgpart =~ /^(\d+)_/ ) {
545     $saved_pkgpart = $1;
546   } else {
547     $saved_pkgpart = '';
548   }
549   $username = $cgi->param('username');
550   $password = $cgi->param('_password');
551   $popnum = $cgi->param('popnum');
552   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
553   $same = $cgi->param('same');
554   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
555   $ss = $cust_main->ss;           # don't mask an entered value on errors
556   $stateid = $cust_main->stateid; # don't mask an entered value on errors
557   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
558 } elsif ( $cgi->keywords ) { #editing
559   my( $query ) = $cgi->keywords;
560   $query =~ /^(\d+)$/;
561   $custnum=$1;
562   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
563   if ( $cust_main->dbdef_table->column('paycvv')
564        && length($cust_main->paycvv)             ) {
565     my $paycvv = $cust_main->paycvv;
566     $paycvv =~ s/./*/g;
567     $cust_main->paycvv($paycvv);
568   }
569   $saved_pkgpart = 0;
570   $saved_domsvc = 0;
571   $username = '';
572   $password = '';
573   $popnum = 0;
574   @invoicing_list = $cust_main->invoicing_list;
575   $ss = $cust_main->masked('ss');
576   $stateid = $cust_main->masked('stateid');
577   $payinfo = $cust_main->paymask;
578 } else {
579   $custnum='';
580   $cust_main = new FS::cust_main ( {} );
581   $cust_main->otaker( &getotaker );
582   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
583   $saved_pkgpart = 0;
584   $saved_domsvc = 0;
585   $username = '';
586   $password = '';
587   $popnum = 0;
588   @invoicing_list = ();
589   push @invoicing_list, 'POST'
590     unless $conf->exists('disablepostalinvoicedefault');
591   $ss = '';
592   $stateid = '';
593   $payinfo = '';
594 }
595
596 my $error = $cgi->param('error');
597 $cgi->delete_all();
598 $cgi->param('error', $error);
599
600 my $action = $custnum ? 'Edit' : 'Add';
601 $action .= ": ". $cust_main->name if $custnum;
602
603 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
604
605 </%init>