duplicate address checking for new customers, #16582
[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 SIZE="+1"><B><% mt('Billing address') |h %></B></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 SIZE="+1"><B><% mt('Service address') |h %></B></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 %# billing info
142 <& cust_main/billing.html, $cust_main,
143                'payinfo'        => $payinfo,
144                'invoicing_list' => \@invoicing_list,
145 &>
146
147 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
148 % if (!$ro_comments || $cust_main->comments) {
149
150     <BR><% mt('Comments') |h %> 
151     <% &ntable("#cccccc") %>
152       <TR>
153         <TD>
154           <TEXTAREA NAME = "comments"
155                     COLS = 80
156                     ROWS = 5
157                     WRAP = "HARD"
158                     <% $ro_comments %>
159           ><% $cust_main->comments %></TEXTAREA>
160         </TD>
161       </TR>
162     </TABLE>
163
164 % }
165
166 % unless ( $custnum ) {
167
168     <& cust_main/first_pkg.html, $cust_main,
169                  'pkgpart_svcpart' => $pkgpart_svcpart,
170                  'disable_empty'   =>
171                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
172                  'username'        => $username,
173                  'password'        => $password,
174                  'popnum'          => $popnum,
175                  'saved_domsvc'    => $saved_domsvc,
176                  %svc_phone,
177                  %svc_dsl,
178     &>
179
180 % }
181
182 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
183
184 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
185
186 %# cust_main/bottomfixup.js
187 % foreach my $hidden (
188 %    'payauto', 'billday',
189 %    'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
190 %    'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
191 %    'paystart_month', 'paystart_year', 'payissue',
192 %    'payip',
193 %    'paid',
194 % ) {
195     <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
196 % } 
197
198 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
199
200 <BR>
201 <INPUT TYPE    = "button"
202        NAME    = "submitButton"
203        ID      = "submitButton"
204        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
205        onClick = "this.disabled=true; bottomfixup(this.form);"
206 >
207 </FORM>
208
209 <& /elements/footer.html &>
210
211 <%init>
212
213 my $curuser = $FS::CurrentUser::CurrentUser;
214
215 #probably redundant given the checks below...
216 die "access denied"
217   unless $curuser->access_right('New customer')
218      ||  $curuser->access_right('Edit customer');
219
220 my $conf = new FS::Conf;
221
222 #get record
223
224 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
225 my $same = '';
226 my $pkgpart_svcpart = ''; #first_pkg
227 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
228 my %svc_phone = ();
229 my %svc_dsl = ();
230 my $prospectnum = '';
231 my $locationnum = '';
232
233 if ( $cgi->param('error') ) {
234
235   $cust_main = new FS::cust_main ( {
236     map { $_, scalar($cgi->param($_)) } fields('cust_main')
237   } );
238
239   $custnum = $cust_main->custnum;
240
241   die "access denied"
242     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
243
244   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
245   $same = $cgi->param('same');
246   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
247   $ss = $cust_main->ss;           # don't mask an entered value on errors
248   $stateid = $cust_main->stateid; # don't mask an entered value on errors
249   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
250
251   $prospectnum = $cgi->param('prospectnum') || '';
252
253   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
254
255   $locationnum = $cgi->param('locationnum') || '';
256
257   #svc_acct
258   $username = $cgi->param('username');
259   $password = $cgi->param('_password');
260   $popnum = $cgi->param('popnum');
261   $saved_domsvc = $cgi->param('domsvc') || '';
262   if ( $saved_domsvc =~ /^(\d+)$/ ) {
263     $saved_domsvc = $1;
264   } else {
265     $saved_domsvc = '';
266   }
267
268   #svc_phone
269   $svc_phone{$_} = $cgi->param($_)
270     foreach qw( countrycode phonenum sip_password pin phone_name );
271
272   #svc_dsl (phonenum came in with svc_phone)
273   $svc_phone{$_} = $cgi->param($_)
274     foreach qw( password isp_chg isp_prev vendor_qual_id );
275
276 } elsif ( $cgi->keywords ) { #editing
277
278   die "access denied"
279     unless $curuser->access_right('Edit customer');
280
281   my( $query ) = $cgi->keywords;
282   $query =~ /^(\d+)$/;
283   $custnum=$1;
284   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
285   if ( $cust_main->dbdef_table->column('paycvv')
286        && length($cust_main->paycvv)             ) {
287     my $paycvv = $cust_main->paycvv;
288     $paycvv =~ s/./*/g;
289     $cust_main->paycvv($paycvv);
290   }
291   @invoicing_list = $cust_main->invoicing_list;
292   $ss = $cust_main->masked('ss');
293   $stateid = $cust_main->masked('stateid');
294   $payinfo = $cust_main->paymask;
295
296 } else { #new customer
297
298   die "access denied"
299     unless $curuser->access_right('New customer');
300
301   $custnum='';
302   $cust_main = new FS::cust_main ( {} );
303   $cust_main->agentnum( $conf->config('default_agentnum') )
304     if $conf->exists('default_agentnum');
305   $cust_main->otaker( &getotaker );
306   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
307   @invoicing_list = ();
308   push @invoicing_list, 'POST'
309     unless $conf->exists('disablepostalinvoicedefault');
310   $ss = '';
311   $stateid = '';
312   $payinfo = '';
313
314   if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
315     my $qualnum = $1;
316     my $qual = qsearchs('qual', { 'qualnum' => $qualnum } )
317       or die "unknown qualnum $qualnum";
318
319     my $prospect_main = $qual->cust_or_prospect;
320     $prospectnum = $prospect_main->prospectnum
321       or die "qualification not on a prospect";
322
323     $cust_main->agentnum( $prospect_main->agentnum );
324     $cust_main->company(  $prospect_main->company  );
325
326     #first contact? -> name
327     my @contacts = $prospect_main->contact;
328     my $contact = $contacts[0];
329     $cust_main->first( $contact->first );
330     $cust_main->set( 'last', $contact->get('last') );
331     #contact phone numbers?
332
333     #location -> address  (all prospect quals have location, right?)
334     my $cust_location = $qual->cust_location;
335     $cust_location->dealternize;
336     $cust_main->$_( $cust_location->$_ )
337       foreach qw( address1 address2 city county state zip country latitude longitude coord_auto geocode );
338
339     #locationnum -> package order
340     $locationnum = $qual->locationnum;
341
342     #pkgpart handled by lock_pkgpart below
343
344     #service telephone & vendor_qual_id -> svc_dsl
345     $svc_dsl{$_} = $qual->$_
346       foreach qw( phonenum vendor_qual_id );
347   }
348
349   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
350     my $pkgpart = $1;
351     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
352       or die "unknown pkgpart $pkgpart";
353     my $svcpart = $part_pkg->svcpart;
354     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
355   }
356
357 }
358
359 my %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
360 $cgi->delete( grep !$keep{$_}, $cgi->param );
361
362 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
363 $title = mt($title);
364 $title .= ": ". $cust_main->name if $custnum;
365
366 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
367
368 </%init>