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