RT# 75817 - fixed issue with contact edit on edit customer page
[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 %# better section title for this?
27 <FONT CLASS="fsinnerbox-title"><% mt('Basics') |h %></FONT>
28 <& cust_main/top_misc.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 % unless ($conf->exists('invoice-ship_address')) { #it's always on, so hide per-cust config
86         <TR>
87           <TD COLSPAN="8">
88             <% include('/elements/checkbox.html',
89                  'field'      => 'invoice_ship_address',
90                  'value'      => 'Y',
91                  'curr_value' => $cust_main->invoice_ship_address,
92                  'postfix'    => emt('Include service address on invoices'),
93             ) %>
94           </TD>
95         </TR>
96 % }
97       </TABLE>
98     </DIV>
99   </TD>
100 </TR></TABLE>
101
102 <SCRIPT>
103 function samechanged(what) {
104 %# not display = 'none', because we still want it to take up space
105 %#  document.getElementById('table_ship_location').style.visibility = 
106 %#    what.checked ? 'hidden' : 'visible';
107   var t1 = document.getElementById('table_ship_location');
108   if ( what.checked ) {
109     t1.style.visibility = 'hidden';
110   }
111   else {
112     t1.style.visibility = 'visible'
113   }
114 }
115 samechanged(document.getElementById('same'));
116 </SCRIPT>
117
118 <BR>
119
120 <& cust_main/contacts_new.html, 'cust_main'=>$cust_main, 'submit_id'=>'submitButton', &>
121
122 %# billing info
123 <& cust_main/billing.html, $cust_main,
124                'payinfo'        => $payinfo,
125                'invoicing_list' => \@invoicing_list,
126 &>
127 <BR>
128
129 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
130 % if (!$ro_comments || $cust_main->comments) {
131
132     <BR>
133     <FONT CLASS="fsinnerbox-title"><% mt('Comments') |h %></FONT>
134     <TABLE CLASS="fsinnerbox">
135       <TR>
136         <TD>
137           <TEXTAREA NAME = "comments"
138                     COLS = 80
139                     ROWS = 5
140                     WRAP = "HARD"
141                     <% $ro_comments %>
142           ><% $cust_main->comments %></TEXTAREA>
143         </TD>
144       </TR>
145     </TABLE>
146
147 % }
148
149 % unless ( $custnum ) {
150
151     <& cust_main/first_pkg.html, $cust_main,
152                  'pkgpart_svcpart' => $pkgpart_svcpart,
153                  'disable_empty'   =>
154                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
155                  'username'        => $username,
156                  'password'        => $password,
157                  'popnum'          => $popnum,
158                  'saved_domsvc'    => $saved_domsvc,
159                  %svc_phone,
160                  %svc_dsl,
161     &>
162
163 % }
164
165 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
166
167 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
168
169 %# cust_main/bottomfixup.js
170 % foreach my $hidden (
171 %    'payauto', 'billday',
172 %    'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
173 %    'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
174 %    'paystart_month', 'paystart_year', 'payissue',
175 %    'payip',
176 %    'paid',
177 % ) {
178     <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
179 % } 
180
181 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
182
183 <BR>
184 <INPUT TYPE    = "button"
185        NAME    = "submitButton"
186        ID      = "submitButton"
187        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
188        onClick = "this.disabled=true; bottomfixup(this.form);"
189 >
190 <BR><BR>
191 </FORM>
192
193 <& /elements/footer.html &>
194
195 <%init>
196
197 my $curuser = $FS::CurrentUser::CurrentUser;
198
199 #probably redundant given the checks below...
200 die "access denied"
201   unless $curuser->access_right('New customer')
202      ||  $curuser->access_right('Edit customer');
203
204 my $conf = new FS::Conf;
205
206 #get record
207
208 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
209 my $pkgpart_svcpart = ''; #first_pkg
210 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
211 my %svc_phone = ();
212 my %svc_dsl = ();
213 my $prospectnum = '';
214 my $locationnum = '';
215 my $same = '';
216
217 $m->comp('/elements/handle_uri_query', 'secure'=>1);
218
219 if ( $cgi->param('error') ) {
220
221   $same = ($cgi->param('same') || '') eq 'Y';
222   # false laziness w/ edit/process/cust_main.cgi
223   my %locations;
224   for my $pre (qw(bill ship)) {
225     my %hash;
226     foreach my $locfield ( FS::cust_main->location_fields ) {
227       # don't search on lat/long, string values can cause qsearchs to die
228       next if grep {$_ eq $locfield} qw(latitude longitude);
229       $hash{$locfield} = scalar($cgi->param($pre.'_'.$locfield));
230     }
231     $hash{'custnum'} = $cgi->param('custnum');
232     $locations{$pre} = qsearchs('cust_location', \%hash)
233                        || FS::cust_location->new( \%hash );
234     # now set lat/long, for redisplay of entered values
235     foreach my $locfield ( qw(latitude longitude) ) {
236       my $locvalue = scalar($cgi->param($pre.'_'.$locfield));
237       $locations{$pre}->set($locfield,$locvalue);
238     }
239   }
240   if ( $same ) {
241     $locations{ship} = $locations{bill};
242   }
243
244   $cust_main = new FS::cust_main ( {
245     map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
246     map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
247   } );
248
249   for my $pre (qw(bill ship)) {
250     $cust_main->set($pre.'_location', $locations{$pre});
251     $cust_main->set($pre.'_locationnum', $locations{$pre}->locationnum);
252   }
253
254   $custnum = $cust_main->custnum;
255
256   die "access denied"
257     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
258
259   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
260   $cust_main->setfield( 'paid' => scalar($cgi->param('paid')) )
261     if $cgi->param('paid');
262   $ss = $cust_main->ss;           # don't mask an entered value on errors
263   $stateid = $cust_main->stateid; # don't mask an entered value on errors
264   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
265
266   $cust_main->national_id( $cgi->param('national_id1') || $cgi->param('national_id2') );
267
268   $prospectnum = $cgi->param('prospectnum') || '';
269
270   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
271
272   $locationnum = $cgi->param('locationnum') || '';
273
274   #svc_acct
275   $username = $cgi->param('username');
276   $password = $cgi->param('_password');
277   $popnum = $cgi->param('popnum');
278   $saved_domsvc = $cgi->param('domsvc') || '';
279   if ( $saved_domsvc =~ /^(\d+)$/ ) {
280     $saved_domsvc = $1;
281   } else {
282     $saved_domsvc = '';
283   }
284
285   #svc_phone
286   $svc_phone{$_} = $cgi->param($_)
287     foreach qw( countrycode phonenum sip_password pin phone_name );
288
289   #svc_dsl (phonenum came in with svc_phone)
290   $svc_phone{$_} = $cgi->param($_)
291     foreach qw( password isp_chg isp_prev vendor_qual_id );
292
293 } elsif ( $cgi->keywords ) { #editing
294
295   die "access denied"
296     unless $curuser->access_right('Edit customer');
297
298   my( $query ) = $cgi->keywords;
299   $query =~ /^(\d+)$/;
300   $custnum=$1;
301   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
302     or die "custnum $custnum not found";
303   if ( $cust_main->dbdef_table->column('paycvv')
304        && length($cust_main->paycvv)             ) {
305     my $paycvv = $cust_main->paycvv;
306     $paycvv =~ s/./*/g;
307     $cust_main->paycvv($paycvv);
308   }
309   @invoicing_list = $cust_main->invoicing_list;
310   $ss = $conf->exists('unmask_ss') ? $cust_main->ss : $cust_main->masked('ss');
311   $stateid = $cust_main->masked('stateid');
312   $payinfo = $cust_main->paymask;
313
314 } else { #new customer
315
316   die "access denied"
317     unless $curuser->access_right('New customer');
318
319   $custnum='';
320   $cust_main = new FS::cust_main ( {} );
321
322   my @agentnums = $curuser->agentnums;
323   $cust_main->agentnum( $agentnums[0] )
324     if scalar(@agentnums) == 1;
325   $cust_main->agentnum( $conf->config('default_agentnum') )
326     if $conf->exists('default_agentnum');
327
328   $cust_main->otaker( &getotaker );
329   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
330   @invoicing_list = ();
331   push @invoicing_list, 'POST'
332     unless $conf->exists('disablepostalinvoicedefault');
333   $ss = '';
334   $stateid = '';
335   $payinfo = '';
336
337   $cgi->param('tagnum', FS::part_tag->default_tags);
338
339   if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
340     my $qualnum = $1;
341     my $qual = qsearchs('qual', { 'qualnum' => $qualnum } )
342       or die "unknown qualnum $qualnum";
343
344     my $prospect_main = $qual->cust_or_prospect;
345     $prospectnum = $prospect_main->prospectnum
346       or die "qualification not on a prospect";
347
348     $cust_main->agentnum( $prospect_main->agentnum );
349     $cust_main->company(  $prospect_main->company  );
350
351     #first contact? -> name
352     my @contacts = $prospect_main->contact;
353     my $contact = $contacts[0];
354     $cust_main->first( $contact->first );
355     $cust_main->set( 'last', $contact->get('last') );
356     #contact phone numbers?
357
358     #location -> address  (all prospect quals have location, right?)
359     my $cust_location = $qual->cust_location;
360     $cust_location->dealternize;
361     $cust_main->$_( $cust_location->$_ )
362       foreach qw( address1 address2 city county state zip country latitude longitude coord_auto geocode );
363
364     #locationnum -> package order
365     $locationnum = $qual->locationnum;
366
367     #pkgpart handled by lock_pkgpart below
368
369     #service telephone & vendor_qual_id -> svc_dsl
370     $svc_dsl{$_} = $qual->$_
371       foreach qw( phonenum vendor_qual_id );
372   }
373   else {
374     my $countrydefault = $conf->config('countrydefault') || 'US';
375     my $statedefault = $conf->config('statedefault') || 'CA';
376     $cust_main->set('bill_location', 
377       FS::cust_location->new( {
378           country => $countrydefault,
379           state => $statedefault,
380           coord_auto => 'Y',
381       } )
382     );
383     $cust_main->set('ship_location',
384       FS::cust_location->new( {
385           country => $countrydefault,
386           state => $statedefault,
387           coord_auto => 'Y',
388       } )
389     );
390   }
391
392   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
393     my $pkgpart = $1;
394     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
395       or die "unknown pkgpart $pkgpart";
396     my $svcpart = $part_pkg->svcpart;
397     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
398   }
399
400 }
401
402 my %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
403 #$cgi->delete( grep { !$keep{$_} && $_ !~ /^tax_/ } $cgi->param );
404 $cgi->delete( grep { !$keep{$_} && $_ !~ /^tax_/ && $_ !~ /^contactnum/ } $cgi->param );
405
406 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
407 $title = mt($title);
408 $title .= ": ". $cust_main->name if $custnum;
409
410 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
411
412 </%init>