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