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