another place to alphabetize
[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     what.form.ship_country.selectedIndex = what.form.country.selectedIndex;
208
209     function fix_ship_county() {
210       what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
211     }
212
213     function fix_ship_state() {
214       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
215       ship_state_changed(what.form.ship_state, fix_ship_county );
216     }
217
218     ship_country_changed(what.form.ship_country, fix_ship_state );
219
220   }
221 }
222 function samechanged(what) {
223   if ( what.checked ) {
224     bill_changed(what);
225 % for (qw( last first company address1 address2 city county state zip country daytime night fax )) { 
226
227     what.form.ship_<%$_%>.disabled = true;
228     what.form.ship_<%$_%>.style.backgroundColor = '#dddddd';
229 % } 
230
231   } else {
232 % for (qw( last first company address1 address2 city county state zip country daytime night fax )) { 
233
234     what.form.ship_<%$_%>.disabled = false;
235     what.form.ship_<%$_%>.style.backgroundColor = '#ffffff';
236 % } 
237
238   }
239 }
240 </SCRIPT>
241 %
242 %  my $checked = '';
243 %  my $disabled = '';
244 %  my $disabledselect = '';
245 %  unless ( $cust_main->ship_last && $same ne 'Y' ) {
246 %    $checked = 'CHECKED';
247 %    $disabled = 'DISABLED STYLE="background-color: #dddddd"';
248 %    foreach (
249 %      qw( last first company address1 address2 city county state zip country
250 %          daytime night fax )
251 %    ) {
252 %      $cust_main->set("ship_$_", $cust_main->get($_) );
253 %    }
254 %  }
255 %
256
257
258 <BR>
259 Service address 
260 (<INPUT TYPE="checkbox" NAME="same" VALUE="Y" onClick="samechanged(this)" <%$checked%>>same as billing address)
261 <% include('cust_main/contact.html', $cust_main, 'ship_', '', $disabled ) %>
262 % } 
263
264
265 <!-- billing info -->
266
267 <% include( 'cust_main/billing.html', $cust_main,
268                'payinfo'        => $payinfo,
269                'invoicing_list' => \@invoicing_list,
270            )
271 %>
272
273 <SCRIPT>
274 function bottomfixup(what) {
275
276   var topvars = new Array(
277     'birthdate',
278
279     'custnum', 'agentnum', 'refnum', 'referral_custnum',
280
281     'last', 'first', 'ss', 'company',
282     'address1', 'address2', 'city',
283     'county', 'state', 'zip', 'country',
284     'daytime', 'night', 'fax',
285
286     'same',
287
288     'ship_last', 'ship_first', 'ship_company',
289     'ship_address1', 'ship_address2', 'ship_city',
290     'ship_county', 'ship_state', 'ship_zip', 'ship_country',
291     'ship_daytime','ship_night', 'ship_fax',
292
293     'select' // XXX key
294   );
295
296   var layervars = new Array(
297     'payauto',
298     'payinfo', 'payinfo1', 'payinfo2',
299     'payname', 'exp_month', 'exp_year', 'paycvv',
300     'paystart_month', 'paystart_year', 'payissue',
301     'payip',
302     'paid'
303   );
304
305   var billing_bottomvars = new Array(
306     'tax',
307     'invoicing_list', 'invoicing_list_POST', 'invoicing_list_FAX',
308     'spool_cdr'
309   );
310
311   for ( f=0; f < topvars.length; f++ ) {
312     var field = topvars[f];
313     copyelement( document.topform.elements[field],
314                  document.bottomform.elements[field]
315                );
316   }
317
318   var layerform = document.topform.select.options[document.topform.select.selectedIndex].value;
319   for ( f=0; f < layervars.length; f++ ) {
320     var field = layervars[f];
321     copyelement( document.forms[layerform].elements[field],
322                  document.bottomform.elements[field]
323                );
324   }
325
326   for ( f=0; f < billing_bottomvars.length; f++ ) {
327     var field = billing_bottomvars[f];
328     copyelement( document.billing_bottomform.elements[field],
329                  document.bottomform.elements[field]
330                );
331   }
332
333 }
334
335 function copyelement(from, to) {
336   if ( from == undefined ) {
337     to.value = '';
338   } else if ( from.type == 'select-one' ) {
339     to.value = from.options[from.selectedIndex].value;
340     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
341   } else if ( from.type == 'checkbox' ) {
342     if ( from.checked ) {
343       to.value = from.value;
344     } else {
345       to.value = '';
346     }
347   } else {
348     if ( from.value == undefined ) {
349       to.value = '';
350     } else {
351       to.value = from.value;
352     }
353   }
354   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
355 }
356
357 </SCRIPT>
358
359 <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">
360 % foreach my $hidden (
361 %     'birthdate',
362 %
363 %     'custnum', 'agentnum', 'refnum', 'referral_custnum',
364 %     'last', 'first', 'ss', 'company',
365 %     'address1', 'address2', 'city',
366 %     'county', 'state', 'zip', 'country',
367 %     'daytime', 'night', 'fax',
368 %     
369 %     'same',
370 %     
371 %     'ship_last', 'ship_first', 'ship_company',
372 %     'ship_address1', 'ship_address2', 'ship_city',
373 %     'ship_county', 'ship_state', 'ship_zip', 'ship_country',
374 %     'ship_daytime','ship_night', 'ship_fax',
375 %     
376 %     'select', #XXX key
377 %
378 %     'payauto',
379 %     'payinfo', 'payinfo1', 'payinfo2',
380 %     'payname', 'exp_month', 'exp_year', 'paycvv',
381 %     'paystart_month', 'paystart_year', 'payissue',
382 %     'payip',
383 %     'paid',
384 %     
385 %     'tax',
386 %     'invoicing_list', 'invoicing_list_POST', 'invoicing_list_FAX',
387 %     'spool_cdr'
388 %   ) {
389 %
390
391   <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
392 % } 
393 %
394 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
395 % if (!$ro_comments || $cust_main->comments) {
396
397 <BR>Comments
398 <% &ntable("#cccccc") %>
399   <TR>
400     <TD>
401       <TEXTAREA COLS=80 ROWS=5 WRAP="HARD" NAME="comments" <%$ro_comments%>><% $cust_main->comments %></TEXTAREA>
402     </TD>
403   </TR>
404 </TABLE>
405 %
406 % }
407 %
408 %unless ( $custnum ) {
409 %  # pry the wrong place for this logic.  also pretty expensive
410 %  #use FS::part_pkg;
411 %
412 %  #false laziness, copied from FS::cust_pkg::order
413 %  my $pkgpart;
414 %  my @agents = $FS::CurrentUser::CurrentUser->agents;
415 %  if ( scalar(@agents) == 1 ) {
416 %    # $pkgpart->{PKGPART} is true iff $custnum may purchase PKGPART
417 %    $pkgpart = $agents[0]->pkgpart_hashref;
418 %  } else {
419 %    #can't know (agent not chosen), so, allow all
420 %    my %typenum;
421 %    foreach my $agent ( @agents ) {
422 %      next if $typenum{$agent->typenum}++;
423 %      #fixed in 5.004_05 #$pkgpart->{$_}++ foreach keys %{ $agent->pkgpart_hashref }
424 %      foreach ( keys %{ $agent->pkgpart_hashref } ) { $pkgpart->{$_}++; } #5.004_04 workaround
425 %    }
426 %  }
427 %  #eslaf
428 %
429 %  my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
430 %    qsearch( 'part_pkg', { 'disabled' => '' },'','ORDER BY pkg' ); # case?
431 %
432 %  if ( @part_pkg ) {
433 %
434 %    #    print "<BR><BR>First package", &itable("#cccccc", "0 ALIGN=LEFT"),
435 %    #apiabuse & undesirable wrapping
436 %
437 %    
438
439     <BR>First package
440     <% ntable("#cccccc") %>
441     
442       <TR>
443         <TD COLSPAN=2>
444           <SELECT NAME="pkgpart_svcpart">
445             <OPTION VALUE="">(none)
446 % foreach my $part_pkg ( @part_pkg ) { 
447
448     
449               <OPTION VALUE="<% $part_pkg->pkgpart. "_". $part_pkg->svcpart('svc_acct') %>"<% ( $saved_pkgpart && $part_pkg->pkgpart == $saved_pkgpart ) ? ' SELECTED' : '' %>><% $part_pkg->pkg. " - ". $part_pkg->comment %>
450 % } 
451
452           </SELECT>
453         </TD>
454       </TR>
455
456 %        #false laziness: (mostly) copied from edit/svc_acct.cgi
457 %        #$ulen = $svc_acct->dbdef_table->column('username')->length;
458 %        my $ulen = dbdef->table('svc_acct')->column('username')->length;
459 %        my $ulen2 = $ulen+2;
460 %        my $passwordmax = $conf->config('passwordmax') || 8;
461 %        my $pmax2 = $passwordmax + 2;
462 %      
463
464     
465       <TR>
466         <TD ALIGN="right">Username</TD>
467         <TD>
468           <INPUT TYPE="text" NAME="username" VALUE="<% $username %>" SIZE=<% $ulen2 %> MAXLENGTH=<% $ulen %>>
469         </TD>
470       </TR>
471     
472       <TR>
473         <TD ALIGN="right">Password</TD>
474         <TD>
475           <INPUT TYPE="text" NAME="_password" VALUE="<% $password %>" SIZE=<% $pmax2 %> MAXLENGTH=<% $passwordmax %>>
476           (blank to generate)
477         </TD>
478       </TR>
479     
480       <TR>
481         <TD ALIGN="right">Access number</TD>
482         <TD><% FS::svc_acct_pop::popselector($popnum) %></TD>
483       </TR>
484     </TABLE>
485 % } 
486 % } 
487
488
489 <INPUT TYPE="hidden" NAME="otaker" VALUE="<% $cust_main->otaker %>">
490 <BR>
491 <INPUT TYPE="submit" NAME="submit" VALUE="<% $custnum ?  "Apply Changes" : "Add Customer" %>">
492 <BR>
493 </FORM>
494
495 <% include('/elements/footer.html') %>
496