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