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