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