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