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