selectable domain on first package in edit/cust_main
[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     'spool_cdr'
317   );
318
319   for ( f=0; f < topvars.length; f++ ) {
320     var field = topvars[f];
321     copyelement( document.topform.elements[field],
322                  document.bottomform.elements[field]
323                );
324   }
325
326   var layerform = document.topform.select.options[document.topform.select.selectedIndex].value;
327   for ( f=0; f < layervars.length; f++ ) {
328     var field = layervars[f];
329     copyelement( document.forms[layerform].elements[field],
330                  document.bottomform.elements[field]
331                );
332   }
333
334   for ( f=0; f < billing_bottomvars.length; f++ ) {
335     var field = billing_bottomvars[f];
336     copyelement( document.billing_bottomform.elements[field],
337                  document.bottomform.elements[field]
338                );
339   }
340
341 }
342
343 function copyelement(from, to) {
344   if ( from == undefined ) {
345     to.value = '';
346   } else if ( from.type == 'select-one' ) {
347     to.value = from.options[from.selectedIndex].value;
348     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
349   } else if ( from.type == 'checkbox' ) {
350     if ( from.checked ) {
351       to.value = from.value;
352     } else {
353       to.value = '';
354     }
355   } else {
356     if ( from.value == undefined ) {
357       to.value = '';
358     } else {
359       to.value = from.value;
360     }
361   }
362   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
363 }
364
365 </SCRIPT>
366
367 <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">
368 % foreach my $hidden (
369 %     'birthdate',
370 %
371 %     'custnum', 'agentnum', 'refnum', 'referral_custnum',
372 %     'last', 'first', 'ss', 'company',
373 %     'address1', 'address2', 'city',
374 %     'county', 'state', 'zip', 'country',
375 %     'daytime', 'night', 'fax',
376 %     
377 %     'same',
378 %     
379 %     'ship_last', 'ship_first', 'ship_company',
380 %     'ship_address1', 'ship_address2', 'ship_city',
381 %     'ship_county', 'ship_state', 'ship_zip', 'ship_country',
382 %     'ship_daytime','ship_night', 'ship_fax',
383 %     
384 %     'select', #XXX key
385 %
386 %     'payauto',
387 %     'payinfo', 'payinfo1', 'payinfo2',
388 %     'payname', 'exp_month', 'exp_year', 'paycvv',
389 %     'paystart_month', 'paystart_year', 'payissue',
390 %     'payip',
391 %     'paid',
392 %     
393 %     'tax',
394 %     'invoicing_list', 'invoicing_list_POST', 'invoicing_list_FAX',
395 %     'spool_cdr'
396 %   ) {
397 %
398
399   <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
400 % } 
401 %
402 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
403 % if (!$ro_comments || $cust_main->comments) {
404
405 <BR>Comments
406 <% &ntable("#cccccc") %>
407   <TR>
408     <TD>
409       <TEXTAREA COLS=80 ROWS=5 WRAP="HARD" NAME="comments" <%$ro_comments%>><% $cust_main->comments %></TEXTAREA>
410     </TD>
411   </TR>
412 </TABLE>
413 %
414 % }
415 %
416 %unless ( $custnum ) {
417 %  # pry the wrong place for this logic.  also pretty expensive
418 %  #use FS::part_pkg;
419 %
420 %  #false laziness, copied from FS::cust_pkg::order
421 %  my $pkgpart;
422 %  my @agents = $FS::CurrentUser::CurrentUser->agents;
423 %  if ( scalar(@agents) == 1 ) {
424 %    # $pkgpart->{PKGPART} is true iff $custnum may purchase PKGPART
425 %    $pkgpart = $agents[0]->pkgpart_hashref;
426 %  } else {
427 %    #can't know (agent not chosen), so, allow all
428 %    my %typenum;
429 %    foreach my $agent ( @agents ) {
430 %      next if $typenum{$agent->typenum}++;
431 %      #fixed in 5.004_05 #$pkgpart->{$_}++ foreach keys %{ $agent->pkgpart_hashref }
432 %      foreach ( keys %{ $agent->pkgpart_hashref } ) { $pkgpart->{$_}++; } #5.004_04 workaround
433 %    }
434 %  }
435 %  #eslaf
436 %
437 %  my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
438 %    qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
439 %
440 %  if ( @part_pkg ) {
441 %
442 %    #    print "<BR><BR>First package", &itable("#cccccc", "0 ALIGN=LEFT"),
443 %    #apiabuse & undesirable wrapping
444 %
445 %    
446
447     <BR>First package
448     <% ntable("#cccccc") %>
449     
450       <TR>
451         <TD COLSPAN=2>
452           <% include('cust_main/select-domain.html',
453                        'pkgparts'      => \@part_pkg,
454                        'saved_pkgpart' => $saved_pkgpart,
455                        'saved_domsvc' => $saved_domsvc,
456                     )
457           %>
458         </TD>
459       </TR>
460
461 %        #false laziness: (mostly) copied from edit/svc_acct.cgi
462 %        #$ulen = $svc_acct->dbdef_table->column('username')->length;
463 %        my $ulen = dbdef->table('svc_acct')->column('username')->length;
464 %        my $ulen2 = $ulen+2;
465 %        my $passwordmax = $conf->config('passwordmax') || 8;
466 %        my $pmax2 = $passwordmax + 2;
467 %      
468
469     
470       <TR>
471         <TD ALIGN="right">Username</TD>
472         <TD>
473           <INPUT TYPE="text" NAME="username" VALUE="<% $username %>" SIZE=<% $ulen2 %> MAXLENGTH=<% $ulen %>>
474         </TD>
475       </TR>
476     
477       <TR>
478         <TD ALIGN="right">Domain</TD>
479         <TD>
480           <SELECT NAME="domsvc">
481             <OPTION>(none)</OPTION>
482           </SELECT>
483         </TD>
484       </TR>
485     
486       <TR>
487         <TD ALIGN="right">Password</TD>
488         <TD>
489           <INPUT TYPE="text" NAME="_password" VALUE="<% $password %>" SIZE=<% $pmax2 %> MAXLENGTH=<% $passwordmax %>>
490           (blank to generate)
491         </TD>
492       </TR>
493     
494       <TR>
495         <TD ALIGN="right">Access number</TD>
496         <TD><% FS::svc_acct_pop::popselector($popnum) %></TD>
497       </TR>
498     </TABLE>
499 % } 
500 % } 
501
502
503 <INPUT TYPE="hidden" NAME="otaker" VALUE="<% $cust_main->otaker %>">
504 <BR>
505 <INPUT TYPE="submit" NAME="submit" VALUE="<% $custnum ?  "Apply Changes" : "Add Customer" %>">
506 <BR>
507 </FORM>
508
509 <% include('/elements/footer.html') %>
510