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