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