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