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