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