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