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