better UI/label for flag to include service address on invoices, RT#29406
[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 %# not display = 'none', because we still want it to take up space
101 %#  document.getElementById('table_ship_location').style.visibility = 
102 %#    what.checked ? 'hidden' : 'visible';
103   var t1 = document.getElementById('table_ship_location');
104   if ( what.checked ) {
105     t1.style.visibility = 'hidden';
106   }
107   else {
108     t1.style.visibility = 'visible'
109   }
110 }
111 //samechanged(document.getElementById('same'));
112 </SCRIPT>
113
114 <BR>
115
116 <& cust_main/contacts_new.html, 'cust_main'=>$cust_main, &>
117
118 %# billing info
119 <& cust_main/billing.html, $cust_main,
120                'payinfo'        => $payinfo,
121                'invoicing_list' => \@invoicing_list,
122 &>
123
124 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
125 % if (!$ro_comments || $cust_main->comments) {
126
127     <BR>
128     <FONT CLASS="fsinnerbox-title"><% mt('Comments') |h %></FONT>
129     <TABLE CLASS="fsinnerbox">
130       <TR>
131         <TD>
132           <TEXTAREA NAME = "comments"
133                     COLS = 80
134                     ROWS = 5
135                     WRAP = "HARD"
136                     <% $ro_comments %>
137           ><% $cust_main->comments %></TEXTAREA>
138         </TD>
139       </TR>
140     </TABLE>
141
142 % }
143
144 % unless ( $custnum ) {
145
146     <& cust_main/first_pkg.html, $cust_main,
147                  'pkgpart_svcpart' => $pkgpart_svcpart,
148                  'disable_empty'   =>
149                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
150                  'username'        => $username,
151                  'password'        => $password,
152                  'popnum'          => $popnum,
153                  'saved_domsvc'    => $saved_domsvc,
154                  %svc_phone,
155                  %svc_dsl,
156     &>
157
158 % }
159
160 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
161
162 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
163
164 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
165
166 <BR>
167 <INPUT TYPE    = "button"
168        NAME    = "submitButton"
169        ID      = "submitButton"
170        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
171        onClick = "this.disabled=true; bottomfixup(this.form);"
172 >
173 <BR><BR>
174 </FORM>
175
176 <& /elements/footer.html &>
177
178 <%init>
179
180 my $curuser = $FS::CurrentUser::CurrentUser;
181
182 #probably redundant given the checks below...
183 die "access denied"
184   unless $curuser->access_right('New customer')
185      ||  $curuser->access_right('Edit customer');
186
187 my $conf = new FS::Conf;
188
189 #get record
190
191 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
192 my $pkgpart_svcpart = ''; #first_pkg
193 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
194 my %svc_phone = ();
195 my %svc_dsl = ();
196 my $prospectnum = '';
197 my $locationnum = '';
198 my $same = '';
199
200 $m->comp('/elements/handle_uri_query', 'secure'=>1);
201
202 if ( $cgi->param('error') ) {
203
204   $same = ($cgi->param('same') || '') eq 'Y';
205   # false laziness w/ edit/process/cust_main.cgi
206   my %locations;
207   for my $pre (qw(bill ship)) {
208     my %hash;
209     foreach ( FS::cust_main->location_fields ) {
210       $hash{$_} = scalar($cgi->param($pre.'_'.$_));
211     }
212     $hash{'custnum'} = $cgi->param('custnum');
213     $locations{$pre} = qsearchs('cust_location', \%hash)
214                        || FS::cust_location->new( \%hash );
215   }
216   if ( $same ) {
217     $locations{ship} = $locations{bill};
218   }
219
220   $cust_main = new FS::cust_main ( {
221     map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
222     map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
223   } );
224
225   for my $pre (qw(bill ship)) {
226     $cust_main->set($pre.'_location', $locations{$pre});
227     $cust_main->set($pre.'_locationnum', $locations{$pre}->locationnum);
228   }
229
230   $custnum = $cust_main->custnum;
231
232   die "access denied"
233     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
234
235   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
236   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
237   $ss = $cust_main->ss;           # don't mask an entered value on errors
238   $stateid = $cust_main->stateid; # don't mask an entered value on errors
239   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
240
241   $cust_main->national_id( $cgi->param('national_id1') || $cgi->param('national_id2') );
242
243   $prospectnum = $cgi->param('prospectnum') || '';
244
245   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
246
247   $locationnum = $cgi->param('locationnum') || '';
248
249   #svc_acct
250   $username = $cgi->param('username');
251   $password = $cgi->param('_password');
252   $popnum = $cgi->param('popnum');
253   $saved_domsvc = $cgi->param('domsvc') || '';
254   if ( $saved_domsvc =~ /^(\d+)$/ ) {
255     $saved_domsvc = $1;
256   } else {
257     $saved_domsvc = '';
258   }
259
260   #svc_phone
261   $svc_phone{$_} = $cgi->param($_)
262     foreach qw( countrycode phonenum sip_password pin phone_name );
263
264   #svc_dsl (phonenum came in with svc_phone)
265   $svc_phone{$_} = $cgi->param($_)
266     foreach qw( password isp_chg isp_prev vendor_qual_id );
267
268 } elsif ( $cgi->keywords ) { #editing
269
270   die "access denied"
271     unless $curuser->access_right('Edit customer');
272
273   my( $query ) = $cgi->keywords;
274   $query =~ /^(\d+)$/;
275   $custnum=$1;
276   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
277     or die "custnum $custnum not found";
278   if ( $cust_main->dbdef_table->column('paycvv')
279        && length($cust_main->paycvv)             ) {
280     my $paycvv = $cust_main->paycvv;
281     $paycvv =~ s/./*/g;
282     $cust_main->paycvv($paycvv);
283   }
284   @invoicing_list = $cust_main->invoicing_list;
285   $ss = $conf->exists('unmask_ss') ? $cust_main->ss : $cust_main->masked('ss');
286   $stateid = $cust_main->masked('stateid');
287   $payinfo = $cust_main->paymask;
288
289 } else { #new customer
290
291   die "access denied"
292     unless $curuser->access_right('New customer');
293
294   $custnum='';
295   $cust_main = new FS::cust_main ( {} );
296   $cust_main->agentnum( $conf->config('default_agentnum') )
297     if $conf->exists('default_agentnum');
298   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
299   @invoicing_list = ();
300   push @invoicing_list, 'POST'
301     unless $conf->exists('disablepostalinvoicedefault');
302   $ss = '';
303   $stateid = '';
304   $payinfo = '';
305
306   $cgi->param('tagnum', FS::part_tag->default_tags);
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 @prospect_contacts = $prospect_main->prospect_contact;
322     my $contact = $prospect_contacts[0]->contact;
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,
348           state => $statedefault,
349           coord_auto => 'Y',
350       } )
351     );
352     $cust_main->set('ship_location',
353       FS::cust_location->new( {
354           country => $countrydefault,
355           state => $statedefault,
356           coord_auto => 'Y',
357       } )
358     );
359   }
360
361   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
362     my $pkgpart = $1;
363     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
364       or die "unknown pkgpart $pkgpart";
365     my $svcpart = $part_pkg->svcpart;
366     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
367   }
368
369 }
370
371 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
372 $title = mt($title);
373 $title .= ": ". $cust_main->name if $custnum;
374
375 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
376
377 </%init>