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