970037b2d767f146d29732b041dddf4f169728da
[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->config('national_id-country')
30 %      || $conf->exists('cust_main-enable_birthdate')
31 %      || $conf->exists('cust_main-enable_spouse_birthdate')
32 %      || $conf->exists('cust_main-enable_anniversary_date')
33 %    )
34 % {
35   <BR>
36   <& cust_main/birthdate.html, $cust_main &>
37 % }
38
39 %# contact info
40
41 %  my $same_checked = '';
42 %  my $ship_disabled = '';
43 %  my @ship_style = ();
44 %  unless ( $cust_main->ship_last && $same ne 'Y' ) {
45 %    $same_checked = 'CHECKED';
46 %    $ship_disabled = 'DISABLED';
47 %    push @ship_style, 'background-color:#dddddd';
48 %    foreach (
49 %      qw( last first company address1 address2 city county state zip country
50 %          latitude longitude coord_auto
51 %          daytime night fax mobile )
52 %    ) {
53 %      $cust_main->set("ship_$_", $cust_main->get($_) );
54 %    }
55 %  }
56
57 <BR>
58 <FONT SIZE="+1"><B><% mt('Billing address') |h %></B></FONT>
59
60 <& cust_main/contact.html,
61              'cust_main'    => $cust_main,
62              'pre'          => '',
63              'onchange'     => 'bill_changed(this)',
64              'disabled'     => '',
65              'ss'           => $ss,
66              'stateid'      => $stateid,
67              'same_checked' => $same_checked, #for address2 "Unit #" labeling
68 &>
69
70 <SCRIPT>
71 function bill_changed(what) {
72   if ( what.form.same.checked ) {
73 % for (qw( last first company address1 address2 city zip latitude longitude coord_auto daytime night fax mobile )) { 
74     what.form.ship_<%$_%>.value = what.form.<%$_%>.value;
75 % } 
76
77     what.form.ship_country.selectedIndex = what.form.country.selectedIndex;
78
79     function fix_ship_city() {
80       what.form.ship_city_select.selectedIndex = what.form.city_select.selectedIndex;
81       what.form.ship_city.style.display = what.form.city.style.display;
82       if ( what.form.ship_city_select ) {
83         what.form.ship_city_select.style.display = what.form.city_select.style.display;
84       }
85     }
86
87     function fix_ship_county() {
88       what.form.ship_county.selectedIndex = what.form.county.selectedIndex;
89       ship_county_changed(what.form.ship_county, fix_ship_city );
90     }
91
92     function fix_ship_state() {
93       what.form.ship_state.selectedIndex = what.form.state.selectedIndex;
94       ship_state_changed(what.form.ship_state, fix_ship_county );
95     }
96
97     ship_country_changed(what.form.ship_country, fix_ship_state );
98
99   }
100 }
101 function samechanged(what) {
102   if ( what.checked ) {
103     bill_changed(what);
104
105 %   my @fields = qw( last first company address1 address2 city county state zip country latitude longitude daytime night fax mobile );
106 %   for (@fields) { 
107       what.form.ship_<%$_%>.disabled = true;
108       what.form.ship_<%$_%>.style.backgroundColor = '#dddddd';
109 %   }
110     if ( what.form.ship_city_select ) {
111       what.form.ship_city_select.disabled = true;
112       what.form.ship_city_select.style.backgroundColor = '#dddddd';
113     }
114
115 %   if ( $conf->exists('cust_main-require_address2') ) {
116       document.getElementById('address2_required').style.visibility = '';
117       document.getElementById('address2_label').style.visibility = '';
118       document.getElementById('ship_address2_required').style.visibility = 'hidden';
119       document.getElementById('ship_address2_label').style.visibility = 'hidden';
120 %   }
121
122   } else {
123
124 %   for (@fields) { 
125       what.form.ship_<%$_%>.disabled = false;
126       what.form.ship_<%$_%>.style.backgroundColor = '#ffffff';
127 %   } 
128
129 %   if ( $conf->exists('cust_main-require_address2') ) {
130       document.getElementById('address2_required').style.visibility = 'hidden';
131       document.getElementById('address2_label').style.visibility = 'hidden';
132       document.getElementById('ship_address2_required').style.visibility = '';
133       document.getElementById('ship_address2_label').style.visibility = '';
134 %   }
135
136   }
137 }
138 </SCRIPT>
139
140 <BR>
141 <FONT SIZE="+1"><B><% mt('Service address') |h %></B></FONT>
142
143 (<INPUT TYPE="checkbox" ID="same" NAME="same" VALUE="Y" onClick="samechanged(this)" <%$same_checked%>><% mt('same as billing address') |h %>)
144 <& cust_main/contact.html,
145              'cust_main' => $cust_main,
146              'pre'       => 'ship_',
147              'onchange'  => '',
148              'disabled'  => $ship_disabled,
149              'style'     => \@ship_style
150 &>
151
152 %# billing info
153 <& cust_main/billing.html, $cust_main,
154                'payinfo'        => $payinfo,
155                'invoicing_list' => \@invoicing_list,
156 &>
157
158 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
159 % if (!$ro_comments || $cust_main->comments) {
160
161     <BR><% mt('Comments') |h %> 
162     <% &ntable("#cccccc") %>
163       <TR>
164         <TD>
165           <TEXTAREA NAME = "comments"
166                     COLS = 80
167                     ROWS = 5
168                     WRAP = "HARD"
169                     <% $ro_comments %>
170           ><% $cust_main->comments %></TEXTAREA>
171         </TD>
172       </TR>
173     </TABLE>
174
175 % }
176
177 % unless ( $custnum ) {
178
179     <& cust_main/first_pkg.html, $cust_main,
180                  'pkgpart_svcpart' => $pkgpart_svcpart,
181                  'disable_empty'   =>
182                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
183                  'username'        => $username,
184                  'password'        => $password,
185                  'popnum'          => $popnum,
186                  'saved_domsvc'    => $saved_domsvc,
187                  %svc_phone,
188                  %svc_dsl,
189     &>
190
191 % }
192
193 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
194
195 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
196
197 %# cust_main/bottomfixup.js
198 % foreach my $hidden (
199 %    'payauto', 'billday',
200 %    'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
201 %    'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
202 %    'paystart_month', 'paystart_year', 'payissue',
203 %    'payip',
204 %    'paid',
205 % ) {
206     <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
207 % } 
208
209 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
210
211 <BR>
212 <INPUT TYPE    = "button"
213        NAME    = "submitButton"
214        ID      = "submitButton"
215        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
216        onClick = "this.disabled=true; bottomfixup(this.form);"
217 >
218 </FORM>
219
220 <& /elements/footer.html &>
221
222 <%init>
223
224 my $curuser = $FS::CurrentUser::CurrentUser;
225
226 #probably redundant given the checks below...
227 die "access denied"
228   unless $curuser->access_right('New customer')
229      ||  $curuser->access_right('Edit customer');
230
231 my $conf = new FS::Conf;
232
233 #get record
234
235 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
236 my $same = '';
237 my $pkgpart_svcpart = ''; #first_pkg
238 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
239 my %svc_phone = ();
240 my %svc_dsl = ();
241 my $prospectnum = '';
242 my $locationnum = '';
243
244 if ( $cgi->param('error') ) {
245
246   $cust_main = new FS::cust_main ( {
247     map { $_, scalar($cgi->param($_)) } fields('cust_main')
248   } );
249
250   $custnum = $cust_main->custnum;
251
252   die "access denied"
253     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
254
255   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
256   $same = $cgi->param('same');
257   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
258   $ss = $cust_main->ss;           # don't mask an entered value on errors
259   $stateid = $cust_main->stateid; # don't mask an entered value on errors
260   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
261
262   $cust_main->national_id( $cgi->param('national_id1') || $cgi->param('national_id2') );
263
264   $prospectnum = $cgi->param('prospectnum') || '';
265
266   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
267
268   $locationnum = $cgi->param('locationnum') || '';
269
270   #svc_acct
271   $username = $cgi->param('username');
272   $password = $cgi->param('_password');
273   $popnum = $cgi->param('popnum');
274   $saved_domsvc = $cgi->param('domsvc') || '';
275   if ( $saved_domsvc =~ /^(\d+)$/ ) {
276     $saved_domsvc = $1;
277   } else {
278     $saved_domsvc = '';
279   }
280
281   #svc_phone
282   $svc_phone{$_} = $cgi->param($_)
283     foreach qw( countrycode phonenum sip_password pin phone_name );
284
285   #svc_dsl (phonenum came in with svc_phone)
286   $svc_phone{$_} = $cgi->param($_)
287     foreach qw( password isp_chg isp_prev vendor_qual_id );
288
289 } elsif ( $cgi->keywords ) { #editing
290
291   die "access denied"
292     unless $curuser->access_right('Edit customer');
293
294   my( $query ) = $cgi->keywords;
295   $query =~ /^(\d+)$/;
296   $custnum=$1;
297   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
298     or die "custnum $custnum not found";
299   if ( $cust_main->dbdef_table->column('paycvv')
300        && length($cust_main->paycvv)             ) {
301     my $paycvv = $cust_main->paycvv;
302     $paycvv =~ s/./*/g;
303     $cust_main->paycvv($paycvv);
304   }
305   @invoicing_list = $cust_main->invoicing_list;
306   $ss = $conf->exists('unmask_ss') ? $cust_main->ss : $cust_main->masked('ss');
307   $stateid = $cust_main->masked('stateid');
308   $payinfo = $cust_main->paymask;
309
310 } else { #new customer
311
312   die "access denied"
313     unless $curuser->access_right('New customer');
314
315   $custnum='';
316   $cust_main = new FS::cust_main ( {} );
317   $cust_main->agentnum( $conf->config('default_agentnum') )
318     if $conf->exists('default_agentnum');
319   $cust_main->otaker( &getotaker );
320   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
321   @invoicing_list = ();
322   push @invoicing_list, 'POST'
323     unless $conf->exists('disablepostalinvoicedefault');
324   $ss = '';
325   $stateid = '';
326   $payinfo = '';
327
328   $cgi->param('tagnum', FS::part_tag->default_tags);
329
330   if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
331     my $qualnum = $1;
332     my $qual = qsearchs('qual', { 'qualnum' => $qualnum } )
333       or die "unknown qualnum $qualnum";
334
335     my $prospect_main = $qual->cust_or_prospect;
336     $prospectnum = $prospect_main->prospectnum
337       or die "qualification not on a prospect";
338
339     $cust_main->agentnum( $prospect_main->agentnum );
340     $cust_main->company(  $prospect_main->company  );
341
342     #first contact? -> name
343     my @contacts = $prospect_main->contact;
344     my $contact = $contacts[0];
345     $cust_main->first( $contact->first );
346     $cust_main->set( 'last', $contact->get('last') );
347     #contact phone numbers?
348
349     #location -> address  (all prospect quals have location, right?)
350     my $cust_location = $qual->cust_location;
351     $cust_location->dealternize;
352     $cust_main->$_( $cust_location->$_ )
353       foreach qw( address1 address2 city county state zip country latitude longitude coord_auto geocode );
354
355     #locationnum -> package order
356     $locationnum = $qual->locationnum;
357
358     #pkgpart handled by lock_pkgpart below
359
360     #service telephone & vendor_qual_id -> svc_dsl
361     $svc_dsl{$_} = $qual->$_
362       foreach qw( phonenum vendor_qual_id );
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>