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