This commit was generated by cvs2svn to compensate for changes in r12472,
[freeside.git] / httemplate / edit / cust_main.cgi
1 <& /elements/header.html,
2       $title,
3       '',
4       ' onUnload="myclose()"' #hmm, in billing.html
5 &>
6
7 <& /elements/error.html &>
8
9 <FORM NAME   = "CustomerForm"
10       METHOD = "POST"
11       ACTION = "<% popurl(1) %>process/cust_main.cgi"
12 >
13
14 <INPUT TYPE="hidden" NAME="custnum"     VALUE="<% $custnum %>">
15 <INPUT TYPE="hidden" NAME="prospectnum" VALUE="<% $prospectnum %>">
16
17 % if ( $custnum ) { 
18   <% mt('Customer #') |h %><B><% $cust_main->display_custnum %></B> - 
19   <B><FONT COLOR="#<% $cust_main->statuscolor %>">
20     <% ucfirst($cust_main->status) %>
21   </FONT></B>
22   <BR><BR>
23 % } 
24
25 %# agent, agent_custid, refnum (advertising source), referral_custnum
26 <& cust_main/top_misc.html, $cust_main, 'custnum' => $custnum  &>
27
28 %# birthdate
29 % if ( $conf->exists('cust_main-enable_birthdate') ) {
30   <BR>
31   <& cust_main/birthdate.html, $cust_main &>
32 % }
33
34 %# latitude and longitude
35 % if ( $conf->exists('cust_main-require_censustract') ) {
36 %   my ($latitude, $longitude) = $cust_main->service_coordinates;
37 %   $latitude ||= $conf->config('company_latitude') || '';
38 %   $longitude ||= $conf->config('company_longitude') || '';
39   <INPUT NAME="latitude" TYPE="hidden" VALUE="<% $latitude |h %>">
40   <INPUT NAME="longitude" TYPE="hidden" VALUE="<% $longitude |h %>">
41 % }
42
43 %# contact info
44
45 %  my $same_checked = '';
46 %  my $ship_disabled = '';
47 %  my @ship_style = ();
48 %  unless ( $cust_main->ship_last && $same ne 'Y' ) {
49 %    $same_checked = 'CHECKED';
50 %    $ship_disabled = 'DISABLED';
51 %    push @ship_style, 'background-color:#dddddd';
52 %    foreach (
53 %      qw( last first company address1 address2 city county state zip country
54 %          latitude longitude coord_auto
55 %          daytime night fax mobile )
56 %    ) {
57 %      $cust_main->set("ship_$_", $cust_main->get($_) );
58 %    }
59 %  }
60
61 <BR>
62 <FONT SIZE="+1"><B><% mt('Billing address') |h %></B></FONT>
63
64 <& cust_main/contact.html,
65              'cust_main'    => $cust_main,
66              'pre'          => '',
67              'onchange'     => 'bill_changed(this)',
68              'disabled'     => '',
69              'ss'           => $ss,
70              'stateid'      => $stateid,
71              'same_checked' => $same_checked, #for address2 "Unit #" labeling
72 &>
73
74 <SCRIPT>
75 function bill_changed(what) {
76   if ( what.form.same.checked ) {
77 % for (qw( last first company address1 address2 city zip latitude longitude coord_auto daytime night fax mobile )) { 
78     what.form.ship_<%$_%>.value = what.form.<%$_%>.value;
79 % } 
80
81     what.form.ship_country.selectedIndex = what.form.country.selectedIndex;
82
83     function fix_ship_city() {
84       what.form.ship_city_select.selectedIndex = what.form.city_select.selectedIndex;
85       what.form.ship_city.style.display = what.form.city.style.display;
86       what.form.ship_city_select.style.display = what.form.city_select.style.display;
87     }
88
89     function fix_ship_county() {
90       what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
91       ship_county_changed(what.form.ship_county, fix_ship_city );
92     }
93
94     function fix_ship_state() {
95       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
96       ship_state_changed(what.form.ship_state, fix_ship_county );
97     }
98
99     ship_country_changed(what.form.ship_country, fix_ship_state );
100
101   }
102 }
103 function samechanged(what) {
104   if ( what.checked ) {
105     bill_changed(what);
106
107 %   my @fields = qw( last first company address1 address2 city city_select county state zip country latitude longitude daytime night fax mobile );
108 %   for (@fields) { 
109       what.form.ship_<%$_%>.disabled = true;
110       what.form.ship_<%$_%>.style.backgroundColor = '#dddddd';
111 %   } 
112
113 %   if ( $conf->exists('cust_main-require_address2') ) {
114       document.getElementById('address2_required').style.visibility = '';
115       document.getElementById('address2_label').style.visibility = '';
116       document.getElementById('ship_address2_required').style.visibility = 'hidden';
117       document.getElementById('ship_address2_label').style.visibility = 'hidden';
118 %   }
119
120   } else {
121
122 %   for (@fields) { 
123       what.form.ship_<%$_%>.disabled = false;
124       what.form.ship_<%$_%>.style.backgroundColor = '#ffffff';
125 %   } 
126
127 %   if ( $conf->exists('cust_main-require_address2') ) {
128       document.getElementById('address2_required').style.visibility = 'hidden';
129       document.getElementById('address2_label').style.visibility = 'hidden';
130       document.getElementById('ship_address2_required').style.visibility = '';
131       document.getElementById('ship_address2_label').style.visibility = '';
132 %   }
133
134   }
135 }
136 </SCRIPT>
137
138 <BR>
139 <FONT SIZE="+1"><B><% mt('Service address') |h %></B></FONT>
140
141 (<INPUT TYPE="checkbox" NAME="same" VALUE="Y" onClick="samechanged(this)" <%$same_checked%>><% mt('same as billing address') |h %>)
142 <& cust_main/contact.html,
143              'cust_main' => $cust_main,
144              'pre'       => 'ship_',
145              'onchange'  => '',
146              'disabled'  => $ship_disabled,
147              'style'     => \@ship_style
148 &>
149
150 %# billing info
151 <& cust_main/billing.html, $cust_main,
152                'payinfo'        => $payinfo,
153                'invoicing_list' => \@invoicing_list,
154 &>
155
156 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
157 % if (!$ro_comments || $cust_main->comments) {
158
159     <BR><% mt('Comments') |h %> 
160     <% &ntable("#cccccc") %>
161       <TR>
162         <TD>
163           <TEXTAREA NAME = "comments"
164                     COLS = 80
165                     ROWS = 5
166                     WRAP = "HARD"
167                     <% $ro_comments %>
168           ><% $cust_main->comments %></TEXTAREA>
169         </TD>
170       </TR>
171     </TABLE>
172
173 % }
174
175 % unless ( $custnum ) {
176
177     <& cust_main/first_pkg.html, $cust_main,
178                  'pkgpart_svcpart' => $pkgpart_svcpart,
179                  'disable_empty'   =>
180                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
181                  'username'        => $username,
182                  'password'        => $password,
183                  'popnum'          => $popnum,
184                  'saved_domsvc'    => $saved_domsvc,
185                  %svc_phone,
186                  %svc_dsl,
187     &>
188
189 % }
190
191 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
192
193 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
194
195 %# cust_main/bottomfixup.js
196 % foreach my $hidden (
197 %    'payauto', 'billday',
198 %    'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
199 %    'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
200 %    'paystart_month', 'paystart_year', 'payissue',
201 %    'payip',
202 %    'paid',
203 % ) {
204     <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
205 % } 
206
207 <& cust_main/bottomfixup.html &>
208
209 <BR>
210 <INPUT TYPE    = "button"
211        NAME    = "submitButton"
212        ID      = "submitButton"
213        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
214        onClick = "this.disabled=true; bottomfixup(this.form);"
215 >
216 </FORM>
217
218 <& /elements/footer.html &>
219
220 <%init>
221
222 my $curuser = $FS::CurrentUser::CurrentUser;
223
224 #probably redundant given the checks below...
225 die "access denied"
226   unless $curuser->access_right('New customer')
227      ||  $curuser->access_right('Edit customer');
228
229 my $conf = new FS::Conf;
230
231 #get record
232
233 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
234 my $same = '';
235 my $pkgpart_svcpart = ''; #first_pkg
236 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
237 my %svc_phone = ();
238 my %svc_dsl = ();
239 my $prospectnum = '';
240 my $locationnum = '';
241
242 if ( $cgi->param('error') ) {
243
244   $cust_main = new FS::cust_main ( {
245     map { $_, scalar($cgi->param($_)) } fields('cust_main')
246   } );
247
248   $custnum = $cust_main->custnum;
249
250   die "access denied"
251     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
252
253   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
254   $same = $cgi->param('same');
255   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
256   $ss = $cust_main->ss;           # don't mask an entered value on errors
257   $stateid = $cust_main->stateid; # don't mask an entered value on errors
258   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
259
260   $prospectnum = $cgi->param('prospectnum') || '';
261
262   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
263
264   $locationnum = $cgi->param('locationnum') || '';
265
266   #svc_acct
267   $username = $cgi->param('username');
268   $password = $cgi->param('_password');
269   $popnum = $cgi->param('popnum');
270   $saved_domsvc = $cgi->param('domsvc') || '';
271   if ( $saved_domsvc =~ /^(\d+)$/ ) {
272     $saved_domsvc = $1;
273   } else {
274     $saved_domsvc = '';
275   }
276
277   #svc_phone
278   $svc_phone{$_} = $cgi->param($_)
279     foreach qw( countrycode phonenum sip_password pin phone_name );
280
281   #svc_dsl (phonenum came in with svc_phone)
282   $svc_phone{$_} = $cgi->param($_)
283     foreach qw( password isp_chg isp_prev vendor_qual_id );
284
285 } elsif ( $cgi->keywords ) { #editing
286
287   die "access denied"
288     unless $curuser->access_right('Edit customer');
289
290   my( $query ) = $cgi->keywords;
291   $query =~ /^(\d+)$/;
292   $custnum=$1;
293   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
294   if ( $cust_main->dbdef_table->column('paycvv')
295        && length($cust_main->paycvv)             ) {
296     my $paycvv = $cust_main->paycvv;
297     $paycvv =~ s/./*/g;
298     $cust_main->paycvv($paycvv);
299   }
300   @invoicing_list = $cust_main->invoicing_list;
301   $ss = $cust_main->masked('ss');
302   $stateid = $cust_main->masked('stateid');
303   $payinfo = $cust_main->paymask;
304
305 } else { #new customer
306
307   die "access denied"
308     unless $curuser->access_right('New customer');
309
310   $custnum='';
311   $cust_main = new FS::cust_main ( {} );
312   $cust_main->agentnum( $conf->config('default_agentnum') )
313     if $conf->exists('default_agentnum');
314   $cust_main->otaker( &getotaker );
315   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
316   @invoicing_list = ();
317   push @invoicing_list, 'POST'
318     unless $conf->exists('disablepostalinvoicedefault');
319   $ss = '';
320   $stateid = '';
321   $payinfo = '';
322
323   if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
324     my $qualnum = $1;
325     my $qual = qsearchs('qual', { 'qualnum' => $qualnum } )
326       or die "unknown qualnum $qualnum";
327
328     my $prospect_main = $qual->cust_or_prospect;
329     $prospectnum = $prospect_main->prospectnum
330       or die "qualification not on a prospect";
331
332     $cust_main->agentnum( $prospect_main->agentnum );
333     $cust_main->company(  $prospect_main->company  );
334
335     #first contact? -> name
336     my @contacts = $prospect_main->contact;
337     my $contact = $contacts[0];
338     $cust_main->first( $contact->first );
339     $cust_main->set( 'last', $contact->get('last') );
340     #contact phone numbers?
341
342     #location -> address  (all prospect quals have location, right?)
343     my $cust_location = $qual->cust_location;
344     $cust_location->dealternize;
345     $cust_main->$_( $cust_location->$_ )
346       foreach qw( address1 address2 city county state zip country latitude longitude coord_auto geocode );
347
348     #locationnum -> package order
349     $locationnum = $qual->locationnum;
350
351     #pkgpart handled by lock_pkgpart below
352
353     #service telephone & vendor_qual_id -> svc_dsl
354     $svc_dsl{$_} = $qual->$_
355       foreach qw( phonenum vendor_qual_id );
356   }
357
358   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
359     my $pkgpart = $1;
360     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
361       or die "unknown pkgpart $pkgpart";
362     my $svcpart = $part_pkg->svcpart;
363     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
364   }
365
366 }
367
368 my %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
369 $cgi->delete( grep !$keep{$_}, $cgi->param );
370
371 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
372 $title = mt($title);
373 $title .= ": ". $cust_main->name if $custnum;
374
375 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
376
377 </%init>