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