0a7a8c031b6c6597acafb8e696697533159d84c4
[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', $cust_main->agentnum,
126               'label'       => "<B>${r}Agent</B>",
127               'empty_label' => 'Select agent',
128            )
129 %>
130
131 <!-- referral (advertising source) -->
132 %
133 %my $refnum = $cust_main->refnum || $conf->config('referraldefault') || 0;
134 %if ( $custnum && ! $conf->exists('editreferrals') ) {
135 %
136
137
138   <INPUT TYPE="hidden" NAME="refnum" VALUE="<% $refnum %>">
139 % } else { 
140
141
142    <% include('/elements/tr-select-part_referral.html', $refnum ) %>
143 % } 
144
145
146 <!-- referring customer -->
147 %
148 %my $referring_cust_main = '';
149 %if ( $cust_main->referral_custnum
150 %     and $referring_cust_main =
151 %           qsearchs('cust_main', { custnum => $cust_main->referral_custnum } )
152 %) {
153 %
154
155
156   <TR>
157     <TD ALIGN="right">Referring customer</TD>
158     <TD>
159       <A HREF="<% popurl(1) %>/cust_main.cgi?<% $cust_main->referral_custnum %>"><% $cust_main->referral_custnum %>: <% $referring_cust_main->name %></A>
160     </TD>
161   </TR>
162   <INPUT TYPE="hidden" NAME="referral_custnum" VALUE="<% $cust_main->referral_custnum %>">
163 % } elsif ( ! $conf->exists('disable_customer_referrals') ) { 
164
165
166   <TR>
167     <TD ALIGN="right">Referring customer</TD>
168     <TD>
169       <!-- <INPUT TYPE="text" NAME="referral_custnum" VALUE=""> -->
170       <% include('/elements/search-cust_main.html',
171                     'field_name' => 'referral_custnum',
172                  )
173       %>
174     </TD>
175   </TR>
176 % } else { 
177
178
179   <INPUT TYPE="hidden" NAME="referral_custnum" VALUE="">
180 % } 
181
182
183 </TABLE>
184
185 <!-- birthdate -->
186
187 % if ( $conf->exists('cust_main-enable_birthdate') ) {
188
189   <BR>
190   <% ntable("#cccccc", 2) %>
191   <% include ('/elements/tr-input-date-field.html',
192               'birthdate',
193               $cust_main->birthdate,
194               'Date of Birth',
195               $conf->config('date_format') || "%m/%d/%Y",
196               1)
197   %>
198
199   </TABLE>
200
201 % }
202
203 <!-- contact info -->
204
205 <BR><BR>
206 Billing address
207 <% include('cust_main/contact.html', $cust_main, '', 'bill_changed(this)', '', 'ss' => $ss, 'stateid' => $stateid ) %>
208
209 <!-- service address -->
210 % if ( defined $cust_main->dbdef_table->column('ship_last') ) { 
211
212
213 <SCRIPT>
214 function bill_changed(what) {
215   if ( what.form.same.checked ) {
216 % for (qw( last first company address1 address2 city zip daytime night fax )) { 
217
218     what.form.ship_<%$_%>.value = what.form.<%$_%>.value;
219 % } 
220
221     what.form.ship_country.selectedIndex = what.form.country.selectedIndex;
222
223     function fix_ship_county() {
224       what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
225     }
226
227     function fix_ship_state() {
228       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
229       ship_state_changed(what.form.ship_state, fix_ship_county );
230     }
231
232     ship_country_changed(what.form.ship_country, fix_ship_state );
233
234   }
235 }
236 function samechanged(what) {
237   if ( what.checked ) {
238     bill_changed(what);
239 % for (qw( last first company address1 address2 city county state zip country daytime night fax )) { 
240
241     what.form.ship_<%$_%>.disabled = true;
242     what.form.ship_<%$_%>.style.backgroundColor = '#dddddd';
243 % } 
244
245   } else {
246 % for (qw( last first company address1 address2 city county state zip country daytime night fax )) { 
247
248     what.form.ship_<%$_%>.disabled = false;
249     what.form.ship_<%$_%>.style.backgroundColor = '#ffffff';
250 % } 
251
252   }
253 }
254 </SCRIPT>
255 %
256 %  my $checked = '';
257 %  my $disabled = '';
258 %  my $disabledselect = '';
259 %  unless ( $cust_main->ship_last && $same ne 'Y' ) {
260 %    $checked = 'CHECKED';
261 %    $disabled = 'DISABLED STYLE="background-color: #dddddd"';
262 %    foreach (
263 %      qw( last first company address1 address2 city county state zip country
264 %          daytime night fax )
265 %    ) {
266 %      $cust_main->set("ship_$_", $cust_main->get($_) );
267 %    }
268 %  }
269 %
270
271
272 <BR>
273 Service address 
274 (<INPUT TYPE="checkbox" NAME="same" VALUE="Y" onClick="samechanged(this)" <%$checked%>>same as billing address)
275 <% include('cust_main/contact.html', $cust_main, 'ship_', '', $disabled ) %>
276 % } 
277
278
279 <!-- billing info -->
280
281 <% include( 'cust_main/billing.html', $cust_main,
282                'payinfo'        => $payinfo,
283                'invoicing_list' => \@invoicing_list,
284            )
285 %>
286
287 <SCRIPT>
288 function bottomfixup(what) {
289
290   var topvars = new Array(
291     'birthdate',
292
293     'custnum', 'agentnum', 'refnum', 'referral_custnum',
294
295     'last', 'first', 'ss', 'company',
296     'address1', 'address2', 'city',
297     'county', 'state', 'zip', 'country',
298     'daytime', 'night', 'fax',
299     'stateid', 'stateid_state',
300
301     'same',
302
303     'ship_last', 'ship_first', 'ship_company',
304     'ship_address1', 'ship_address2', 'ship_city',
305     'ship_county', 'ship_state', 'ship_zip', 'ship_country',
306     'ship_daytime','ship_night', 'ship_fax',
307
308     'select' // XXX key
309   );
310
311   var layervars = new Array(
312     'payauto',
313     'payinfo', 'payinfo1', 'payinfo2', 'paytype',
314     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
315     'paystart_month', 'paystart_year', 'payissue',
316     'payip',
317     'paid'
318   );
319
320   var billing_bottomvars = new Array(
321     'tax',
322     'invoicing_list', 'invoicing_list_POST', 'invoicing_list_FAX',
323     'invoice_terms',
324     'spool_cdr'
325   );
326
327   for ( f=0; f < topvars.length; f++ ) {
328     var field = topvars[f];
329     copyelement( document.topform.elements[field],
330                  document.bottomform.elements[field]
331                );
332   }
333
334   var layerform = document.topform.select.options[document.topform.select.selectedIndex].value;
335   for ( f=0; f < layervars.length; f++ ) {
336     var field = layervars[f];
337     copyelement( document.forms[layerform].elements[field],
338                  document.bottomform.elements[field]
339                );
340   }
341
342   for ( f=0; f < billing_bottomvars.length; f++ ) {
343     var field = billing_bottomvars[f];
344     copyelement( document.billing_bottomform.elements[field],
345                  document.bottomform.elements[field]
346                );
347   }
348
349 }
350
351 function copyelement(from, to) {
352   if ( from == undefined ) {
353     to.value = '';
354   } else if ( from.type == 'select-one' ) {
355     to.value = from.options[from.selectedIndex].value;
356     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
357   } else if ( from.type == 'checkbox' ) {
358     if ( from.checked ) {
359       to.value = from.value;
360     } else {
361       to.value = '';
362     }
363   } else {
364     if ( from.value == undefined ) {
365       to.value = '';
366     } else {
367       to.value = from.value;
368     }
369   }
370   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
371 }
372
373 </SCRIPT>
374
375 <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">
376 % foreach my $hidden (
377 %     'birthdate',
378 %
379 %     'custnum', 'agentnum', 'refnum', 'referral_custnum',
380 %     'last', 'first', 'ss', 'company',
381 %     'address1', 'address2', 'city',
382 %     'county', 'state', 'zip', 'country',
383 %     'daytime', 'night', 'fax',
384 %     'stateid', 'stateid_state',
385 %     
386 %     'same',
387 %     
388 %     'ship_last', 'ship_first', 'ship_company',
389 %     'ship_address1', 'ship_address2', 'ship_city',
390 %     'ship_county', 'ship_state', 'ship_zip', 'ship_country',
391 %     'ship_daytime','ship_night', 'ship_fax',
392 %     
393 %     'select', #XXX key
394 %
395 %     'payauto',
396 %     'payinfo', 'payinfo1', 'payinfo2', 'paytype',
397 %     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
398 %     'paystart_month', 'paystart_year', 'payissue',
399 %     'payip',
400 %     'paid',
401 %     
402 %     'tax',
403 %     'invoicing_list', 'invoicing_list_POST', 'invoicing_list_FAX',
404 %     'invoice_terms',
405 %     'spool_cdr'
406 %   ) {
407 %
408
409   <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
410 % } 
411 %
412 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
413 % if (!$ro_comments || $cust_main->comments) {
414
415 <BR>Comments
416 <% &ntable("#cccccc") %>
417   <TR>
418     <TD>
419       <TEXTAREA COLS=80 ROWS=5 WRAP="HARD" NAME="comments" <%$ro_comments%>><% $cust_main->comments %></TEXTAREA>
420     </TD>
421   </TR>
422 </TABLE>
423 %
424 % }
425 %
426 %unless ( $custnum ) {
427 %  # pry the wrong place for this logic.  also pretty expensive
428 %  #use FS::part_pkg;
429 %
430 %  #false laziness, copied from FS::cust_pkg::order
431 %  my $pkgpart;
432 %  my @agents = $FS::CurrentUser::CurrentUser->agents;
433 %  if ( scalar(@agents) == 1 ) {
434 %    # $pkgpart->{PKGPART} is true iff $custnum may purchase PKGPART
435 %    $pkgpart = $agents[0]->pkgpart_hashref;
436 %  } else {
437 %    #can't know (agent not chosen), so, allow all
438 %    my %typenum;
439 %    foreach my $agent ( @agents ) {
440 %      next if $typenum{$agent->typenum}++;
441 %      #fixed in 5.004_05 #$pkgpart->{$_}++ foreach keys %{ $agent->pkgpart_hashref }
442 %      foreach ( keys %{ $agent->pkgpart_hashref } ) { $pkgpart->{$_}++; } #5.004_04 workaround
443 %    }
444 %  }
445 %  #eslaf
446 %
447 %  my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
448 %    qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
449 %
450 %  if ( @part_pkg ) {
451 %
452 %    #    print "<BR><BR>First package", &itable("#cccccc", "0 ALIGN=LEFT"),
453 %    #apiabuse & undesirable wrapping
454 %
455 %    
456
457     <BR>First package
458     <% ntable("#cccccc") %>
459     
460       <TR>
461         <TD COLSPAN=2>
462           <% include('cust_main/select-domain.html',
463                        'pkgparts'      => \@part_pkg,
464                        'saved_pkgpart' => $saved_pkgpart,
465                        'saved_domsvc' => $saved_domsvc,
466                     )
467           %>
468         </TD>
469       </TR>
470
471 %        #false laziness: (mostly) copied from edit/svc_acct.cgi
472 %        #$ulen = $svc_acct->dbdef_table->column('username')->length;
473 %        my $ulen = dbdef->table('svc_acct')->column('username')->length;
474 %        my $ulen2 = $ulen+2;
475 %        my $passwordmax = $conf->config('passwordmax') || 8;
476 %        my $pmax2 = $passwordmax + 2;
477 %      
478
479     
480       <TR>
481         <TD ALIGN="right">Username</TD>
482         <TD>
483           <INPUT TYPE="text" NAME="username" VALUE="<% $username %>" SIZE=<% $ulen2 %> MAXLENGTH=<% $ulen %>>
484         </TD>
485       </TR>
486     
487       <TR>
488         <TD ALIGN="right">Domain</TD>
489         <TD>
490           <SELECT NAME="domsvc">
491             <OPTION>(none)</OPTION>
492           </SELECT>
493         </TD>
494       </TR>
495     
496       <TR>
497         <TD ALIGN="right">Password</TD>
498         <TD>
499           <INPUT TYPE="text" NAME="_password" VALUE="<% $password %>" SIZE=<% $pmax2 %> MAXLENGTH=<% $passwordmax %>>
500           (blank to generate)
501         </TD>
502       </TR>
503     
504       <TR>
505         <TD ALIGN="right">Access number</TD>
506         <TD><% FS::svc_acct_pop::popselector($popnum) %></TD>
507       </TR>
508     </TABLE>
509 % } 
510 % } 
511
512
513 <INPUT TYPE="hidden" NAME="otaker" VALUE="<% $cust_main->otaker %>">
514 <BR>
515 <INPUT TYPE="submit" NAME="submit" VALUE="<% $custnum ?  "Apply Changes" : "Add Customer" %>">
516 <BR>
517 </FORM>
518
519 <% include('/elements/footer.html') %>
520