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