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