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