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