fix: cust_main-enable_birthdate needs to be enabled for MY national_id to work, RT...
[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_spouse_birthdate')
34 %      || $conf->exists('cust_main-enable_anniversary_date')
35 %    )
36 % {
37   <BR>
38   <& cust_main/birthdate.html, $cust_main &>
39 % }
40 % my $has_ship_address = '';
41 % if ( $cgi->param('error') ) {
42 %   $has_ship_address = !$same;
43 % } elsif ( $cust_main->custnum ) {
44 %   $has_ship_address = $cust_main->has_ship_address;
45 % }
46 <BR>
47 <TABLE> <TR>
48   <TD STYLE="width:650px">
49 %#; padding-right:2px; vertical-align:top">
50     <FONT CLASS="fsinnerbox-title"><% mt('Billing address') |h %></FONT>
51     <TABLE CLASS="fsinnerbox">
52     <& cust_main/before_bill_location.html, $cust_main &>
53     <& /elements/location.html,
54         object => $cust_main->bill_location,
55         prefix => 'bill_',
56     &>
57     <& cust_main/after_bill_location.html, $cust_main &>
58     </TABLE>
59   </TD>
60 </TR>
61 <TR><TD STYLE="height:40px"></TD></TR>
62 <TR>
63   <TD STYLE="width:650px">
64 %#; padding-left:2px; vertical-align:top">
65     <FONT CLASS="fsinnerbox-title"><% mt('Service address') |h %></FONT>
66     <INPUT TYPE="checkbox" 
67            NAME="same"
68            ID="same"
69            onclick="samechanged(this)"
70            onkeyup="samechanged(this)"
71            VALUE="Y"
72            <% $has_ship_address ? '' : 'CHECKED' %>
73     ><% mt('same as billing address') |h %>
74     <TABLE CLASS="fsinnerbox" ID="table_ship_location">
75     <& /elements/location.html,
76         object => $cust_main->ship_location,
77         prefix => 'ship_',
78         enable_censustract => 1,
79         enable_district => 1,
80     &>
81     </TABLE>
82     <TABLE CLASS="fsinnerbox" ID="table_ship_location_blank"
83     STYLE="display:none">
84     <TR><TD></TD></TR>
85     </TABLE>
86   </TD>
87 </TR></TABLE>
88
89 <SCRIPT>
90 function samechanged(what) {
91 %# not display = 'none', because we still want it to take up space
92 %#  document.getElementById('table_ship_location').style.visibility = 
93 %#    what.checked ? 'hidden' : 'visible';
94   var t1 = document.getElementById('table_ship_location');
95   var t2 = document.getElementById('table_ship_location_blank');
96   if ( what.checked ) {
97     t2.style.width  = t1.clientWidth  + 'px';
98     t2.style.height = t1.clientHeight + 'px';
99     t1.style.display = 'none';
100     t2.style.display = '';
101   }
102   else {
103     t2.style.display = 'none';
104     t1.style.display = '';
105   }
106 }
107 samechanged(document.getElementById('same'));
108 </SCRIPT>
109
110 <BR>
111
112 <& cust_main/contacts_new.html,
113              'cust_main' => $cust_main,
114 &>
115
116 %# billing info
117 <& cust_main/billing.html, $cust_main,
118                'payinfo'        => $payinfo,
119                'invoicing_list' => \@invoicing_list,
120 &>
121 <BR>
122
123 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
124 % if (!$ro_comments || $cust_main->comments) {
125
126     <BR>
127     <FONT CLASS="fsinnerbox-title"><% mt('Comments') |h %></FONT>
128     <TABLE CLASS="fsinnerbox">
129       <TR>
130         <TD>
131           <TEXTAREA NAME = "comments"
132                     COLS = 80
133                     ROWS = 5
134                     WRAP = "HARD"
135                     <% $ro_comments %>
136           ><% $cust_main->comments %></TEXTAREA>
137         </TD>
138       </TR>
139     </TABLE>
140
141 % }
142
143 % unless ( $custnum ) {
144
145     <& cust_main/first_pkg.html, $cust_main,
146                  'pkgpart_svcpart' => $pkgpart_svcpart,
147                  'disable_empty'   =>
148                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
149                  'username'        => $username,
150                  'password'        => $password,
151                  'popnum'          => $popnum,
152                  'saved_domsvc'    => $saved_domsvc,
153                  %svc_phone,
154                  %svc_dsl,
155     &>
156
157 % }
158
159 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
160
161 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
162
163 %# cust_main/bottomfixup.js
164 % foreach my $hidden (
165 %    'payauto', 'billday',
166 %    'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
167 %    'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
168 %    'paystart_month', 'paystart_year', 'payissue',
169 %    'payip',
170 %    'paid',
171 % ) {
172     <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
173 % } 
174
175 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
176
177 <BR>
178 <INPUT TYPE    = "button"
179        NAME    = "submitButton"
180        ID      = "submitButton"
181        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
182        onClick = "this.disabled=true; bottomfixup(this.form);"
183 >
184 <BR><BR>
185 </FORM>
186
187 <& /elements/footer.html &>
188
189 <%init>
190
191 my $curuser = $FS::CurrentUser::CurrentUser;
192
193 #probably redundant given the checks below...
194 die "access denied"
195   unless $curuser->access_right('New customer')
196      ||  $curuser->access_right('Edit customer');
197
198 my $conf = new FS::Conf;
199
200 #get record
201
202 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
203 my $pkgpart_svcpart = ''; #first_pkg
204 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
205 my %svc_phone = ();
206 my %svc_dsl = ();
207 my $prospectnum = '';
208 my $locationnum = '';
209 my $same = '';
210
211
212 if ( $cgi->param('error') ) {
213
214   $same = ($cgi->param('same') || '') eq 'Y';
215   # false laziness w/ edit/process/cust_main.cgi
216   my %locations;
217   for my $pre (qw(bill ship)) {
218     my %hash;
219     foreach ( FS::cust_main->location_fields ) {
220       $hash{$_} = scalar($cgi->param($pre.'_'.$_));
221     }
222     $hash{'custnum'} = $cgi->param('custnum');
223     $locations{$pre} = qsearchs('cust_location', \%hash)
224                        || FS::cust_location->new( \%hash );
225   }
226   if ( $same ) {
227     $locations{ship} = $locations{bill};
228   }
229
230   $cust_main = new FS::cust_main ( {
231     map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
232     map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
233   } );
234
235   for my $pre (qw(bill ship)) {
236     $cust_main->set($pre.'_location', $locations{$pre});
237     $cust_main->set($pre.'_locationnum', $locations{$pre}->locationnum);
238   }
239
240   $custnum = $cust_main->custnum;
241
242   die "access denied"
243     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
244
245   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
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 = $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->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   else {
349     my $countrydefault = $conf->config('countrydefault') || 'US';
350     my $statedefault = $conf->config('statedefault') || 'CA';
351     $cust_main->set('bill_location', 
352       FS::cust_location->new(
353         { country => $countrydefault, state => $statedefault }
354       )
355     );
356     $cust_main->set('ship_location',
357       FS::cust_location->new(
358         { country => $countrydefault, state => $statedefault }
359       )
360     );
361   }
362
363   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
364     my $pkgpart = $1;
365     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
366       or die "unknown pkgpart $pkgpart";
367     my $svcpart = $part_pkg->svcpart;
368     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
369   }
370
371 }
372
373 my %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
374 $cgi->delete( grep { !$keep{$_} && $_ !~ /^tax_/ } $cgi->param );
375
376 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
377 $title = mt($title);
378 $title .= ": ". $cust_main->name if $custnum;
379
380 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
381
382 </%init>