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