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