contacts can be shared among customers / "duplicate contact emails", RT#27943
[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     <% $cust_main->status_label %>
21   </FONT></B>
22   <BR><BR>
23 % } 
24
25 %# agent, agent_custid, refnum (advertising source), referral_custnum
26 %# better section title for this?
27 <FONT CLASS="fsinnerbox-title"><% mt('Basics') |h %></FONT>
28 <& cust_main/basics.html, $cust_main, 'custnum' => $custnum  &>
29
30 %# birthdate
31 % if (    $conf->config('national_id-country')
32 %      || $conf->exists('cust_main-enable_birthdate')
33 %      || $conf->exists('cust_main-enable_anniversary_date')
34 %    )
35 % {
36   <BR>
37   <& cust_main/birthdate.html, $cust_main &>
38 % }
39 % my $has_ship_address = '';
40 % if ( $cgi->param('error') ) {
41 %   $has_ship_address = !$same;
42 % } elsif ( $cust_main->custnum ) {
43 %   $has_ship_address = $cust_main->has_ship_address;
44 % }
45 <BR>
46 <TABLE> <TR>
47   <TD STYLE="width:650px">
48 %#; padding-right:2px; vertical-align:top">
49     <FONT CLASS="fsinnerbox-title"><% mt('Billing address') |h %></FONT>
50     <TABLE CLASS="fsinnerbox" WIDTH="100%">
51     <& cust_main/before_bill_location.html, $cust_main &>
52     <& /elements/location.html,
53         object => $cust_main->bill_location,
54         prefix => 'bill_',
55         enable_censustract => 1,
56         enable_district => 1,
57         enable_coords => 1,
58     &>
59     <& cust_main/after_bill_location.html, $cust_main &>
60     </TABLE>
61   </TD>
62 </TR>
63 <TR><TD STYLE="height:40px"></TD></TR>
64 <TR>
65   <TD STYLE="width:650px">
66     <FONT CLASS="fsinnerbox-title"><% mt('Service address') |h %></FONT>
67     <INPUT TYPE="checkbox" 
68            NAME="same"
69            ID="same"
70            onclick="samechanged(this)"
71            onkeyup="samechanged(this)"
72            VALUE="Y"
73            <% $has_ship_address ? '' : 'CHECKED' %>
74     ><% mt('same as billing address') |h %>
75     <DIV CLASS="fsinnerbox">
76       <TABLE ID="table_ship_location" WIDTH="100%">
77       <& cust_main/before_ship_location.html, $cust_main &>
78       <& /elements/location.html,
79           object => $cust_main->ship_location,
80           prefix => 'ship_',
81           enable_censustract => 1,
82           enable_district => 1,
83           enable_coords => 1,
84       &>
85       </TABLE>
86     </DIV>
87   </TD>
88 </TR></TABLE>
89
90 <SCRIPT>
91 function samechanged(what) {
92 %# not display = 'none', because we still want it to take up space
93 %#  document.getElementById('table_ship_location').style.visibility = 
94 %#    what.checked ? 'hidden' : 'visible';
95   var t1 = document.getElementById('table_ship_location');
96   if ( what.checked ) {
97     t1.style.visibility = 'hidden';
98   }
99   else {
100     t1.style.visibility = 'visible'
101   }
102 }
103 //samechanged(document.getElementById('same'));
104 </SCRIPT>
105
106 <BR>
107
108 <& cust_main/contacts_new.html,
109              'cust_main' => $cust_main,
110 &>
111
112 %# billing info
113 <& cust_main/billing.html, $cust_main,
114                'payinfo'        => $payinfo,
115                'invoicing_list' => \@invoicing_list,
116 &>
117 <BR>
118
119 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
120 % if (!$ro_comments || $cust_main->comments) {
121
122     <BR>
123     <FONT CLASS="fsinnerbox-title"><% mt('Comments') |h %></FONT>
124     <TABLE CLASS="fsinnerbox">
125       <TR>
126         <TD>
127           <TEXTAREA NAME = "comments"
128                     COLS = 80
129                     ROWS = 5
130                     WRAP = "HARD"
131                     <% $ro_comments %>
132           ><% $cust_main->comments %></TEXTAREA>
133         </TD>
134       </TR>
135     </TABLE>
136
137 % }
138
139 % unless ( $custnum ) {
140
141     <& cust_main/first_pkg.html, $cust_main,
142                  'pkgpart_svcpart' => $pkgpart_svcpart,
143                  'disable_empty'   =>
144                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
145                  'username'        => $username,
146                  'password'        => $password,
147                  'popnum'          => $popnum,
148                  'saved_domsvc'    => $saved_domsvc,
149                  %svc_phone,
150                  %svc_dsl,
151     &>
152
153 % }
154
155 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
156
157 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
158
159 %# cust_main/bottomfixup.js
160 % foreach my $hidden (
161 %    'payauto', 'billday',
162 %    'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
163 %    'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
164 %    'paystart_month', 'paystart_year', 'payissue',
165 %    'payip',
166 %    'paid',
167 % ) {
168     <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
169 % } 
170
171 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
172
173 <BR>
174 <INPUT TYPE    = "button"
175        NAME    = "submitButton"
176        ID      = "submitButton"
177        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
178        onClick = "this.disabled=true; bottomfixup(this.form);"
179 >
180 <BR><BR>
181 </FORM>
182
183 <& /elements/footer.html &>
184
185 <%init>
186
187 my $curuser = $FS::CurrentUser::CurrentUser;
188
189 #probably redundant given the checks below...
190 die "access denied"
191   unless $curuser->access_right('New customer')
192      ||  $curuser->access_right('Edit customer');
193
194 my $conf = new FS::Conf;
195
196 #get record
197
198 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
199 my $pkgpart_svcpart = ''; #first_pkg
200 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
201 my %svc_phone = ();
202 my %svc_dsl = ();
203 my $prospectnum = '';
204 my $locationnum = '';
205 my $same = '';
206
207 $m->comp('/elements/handle_uri_query', 'secure'=>1);
208
209 if ( $cgi->param('error') ) {
210
211   $same = ($cgi->param('same') || '') eq 'Y';
212   # false laziness w/ edit/process/cust_main.cgi
213   my %locations;
214   for my $pre (qw(bill ship)) {
215     my %hash;
216     foreach ( FS::cust_main->location_fields ) {
217       $hash{$_} = scalar($cgi->param($pre.'_'.$_));
218     }
219     $hash{'custnum'} = $cgi->param('custnum');
220     $locations{$pre} = qsearchs('cust_location', \%hash)
221                        || FS::cust_location->new( \%hash );
222   }
223   if ( $same ) {
224     $locations{ship} = $locations{bill};
225   }
226
227   $cust_main = new FS::cust_main ( {
228     map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
229     map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
230   } );
231
232   for my $pre (qw(bill ship)) {
233     $cust_main->set($pre.'_location', $locations{$pre});
234     $cust_main->set($pre.'_locationnum', $locations{$pre}->locationnum);
235   }
236
237   $custnum = $cust_main->custnum;
238
239   die "access denied"
240     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
241
242   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
243   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
244   $ss = $cust_main->ss;           # don't mask an entered value on errors
245   $stateid = $cust_main->stateid; # don't mask an entered value on errors
246   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
247
248   $cust_main->national_id( $cgi->param('national_id1') || $cgi->param('national_id2') );
249
250   $prospectnum = $cgi->param('prospectnum') || '';
251
252   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
253
254   $locationnum = $cgi->param('locationnum') || '';
255
256   #svc_acct
257   $username = $cgi->param('username');
258   $password = $cgi->param('_password');
259   $popnum = $cgi->param('popnum');
260   $saved_domsvc = $cgi->param('domsvc') || '';
261   if ( $saved_domsvc =~ /^(\d+)$/ ) {
262     $saved_domsvc = $1;
263   } else {
264     $saved_domsvc = '';
265   }
266
267   #svc_phone
268   $svc_phone{$_} = $cgi->param($_)
269     foreach qw( countrycode phonenum sip_password pin phone_name );
270
271   #svc_dsl (phonenum came in with svc_phone)
272   $svc_phone{$_} = $cgi->param($_)
273     foreach qw( password isp_chg isp_prev vendor_qual_id );
274
275 } elsif ( $cgi->keywords ) { #editing
276
277   die "access denied"
278     unless $curuser->access_right('Edit customer');
279
280   my( $query ) = $cgi->keywords;
281   $query =~ /^(\d+)$/;
282   $custnum=$1;
283   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
284     or die "custnum $custnum not found";
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 = $conf->exists('unmask_ss') ? $cust_main->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->referral_custnum( $cgi->param('referral_custnum') );
306   @invoicing_list = ();
307   push @invoicing_list, 'POST'
308     unless $conf->exists('disablepostalinvoicedefault');
309   $ss = '';
310   $stateid = '';
311   $payinfo = '';
312
313   $cgi->param('tagnum', FS::part_tag->default_tags);
314
315   if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
316     my $qualnum = $1;
317     my $qual = qsearchs('qual', { 'qualnum' => $qualnum } )
318       or die "unknown qualnum $qualnum";
319
320     my $prospect_main = $qual->cust_or_prospect;
321     $prospectnum = $prospect_main->prospectnum
322       or die "qualification not on a prospect";
323
324     $cust_main->agentnum( $prospect_main->agentnum );
325     $cust_main->company(  $prospect_main->company  );
326
327     #first contact? -> name
328     my @prospect_contacts = $prospect_main->prospect_contact;
329     my $contact = $prospect_contacts[0]->contact;
330     $cust_main->first( $contact->first );
331     $cust_main->set( 'last', $contact->get('last') );
332     #contact phone numbers?
333
334     #location -> address  (all prospect quals have location, right?)
335     my $cust_location = $qual->cust_location;
336     $cust_location->dealternize;
337     $cust_main->$_( $cust_location->$_ )
338       foreach qw( address1 address2 city county state zip country latitude longitude coord_auto geocode );
339
340     #locationnum -> package order
341     $locationnum = $qual->locationnum;
342
343     #pkgpart handled by lock_pkgpart below
344
345     #service telephone & vendor_qual_id -> svc_dsl
346     $svc_dsl{$_} = $qual->$_
347       foreach qw( phonenum vendor_qual_id );
348   }
349   else {
350     my $countrydefault = $conf->config('countrydefault') || 'US';
351     my $statedefault = $conf->config('statedefault') || 'CA';
352     $cust_main->set('bill_location', 
353       FS::cust_location->new( {
354           country => $countrydefault,
355           state => $statedefault,
356           coord_auto => 'Y',
357       } )
358     );
359     $cust_main->set('ship_location',
360       FS::cust_location->new( {
361           country => $countrydefault,
362           state => $statedefault,
363           coord_auto => 'Y',
364       } )
365     );
366   }
367
368   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
369     my $pkgpart = $1;
370     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
371       or die "unknown pkgpart $pkgpart";
372     my $svcpart = $part_pkg->svcpart;
373     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
374   }
375
376 }
377
378 my %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
379 $cgi->delete( grep { !$keep{$_} && $_ !~ /^tax_/ } $cgi->param );
380
381 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
382 $title = mt($title);
383 $title .= ": ". $cust_main->name if $custnum;
384
385 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
386
387 </%init>