Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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 = !$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 $pkgpart_svcpart = ''; #first_pkg
202 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
203 my %svc_phone = ();
204 my %svc_dsl = ();
205 my $prospectnum = '';
206 my $locationnum = '';
207 my $same = '';
208
209
210 if ( $cgi->param('error') ) {
211
212   $same = ($cgi->param('same') || '') eq 'Y';
213   # false laziness w/ edit/process/cust_main.cgi
214   my %locations;
215   for my $pre (qw(bill ship)) {
216     my %hash;
217     foreach ( FS::cust_main->location_fields ) {
218       $hash{$_} = scalar($cgi->param($pre.'_'.$_));
219     }
220     $hash{'custnum'} = $cgi->param('custnum');
221     $locations{$pre} = qsearchs('cust_location', \%hash)
222                        || FS::cust_location->new( \%hash );
223   }
224   if ( $same ) {
225     $locations{ship} = $locations{bill};
226   }
227
228   $cust_main = new FS::cust_main ( {
229     map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
230     map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
231   } );
232
233   for my $pre (qw(bill ship)) {
234     $cust_main->set($pre.'_location', $locations{$pre});
235     $cust_main->set($pre.'_locationnum', $locations{$pre}->locationnum);
236   }
237
238   $custnum = $cust_main->custnum;
239
240   die "access denied"
241     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
242
243   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
244   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
245   $ss = $cust_main->ss;           # don't mask an entered value on errors
246   $stateid = $cust_main->stateid; # don't mask an entered value on errors
247   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
248
249   $prospectnum = $cgi->param('prospectnum') || '';
250
251   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
252
253   $locationnum = $cgi->param('locationnum') || '';
254
255   #svc_acct
256   $username = $cgi->param('username');
257   $password = $cgi->param('_password');
258   $popnum = $cgi->param('popnum');
259   $saved_domsvc = $cgi->param('domsvc') || '';
260   if ( $saved_domsvc =~ /^(\d+)$/ ) {
261     $saved_domsvc = $1;
262   } else {
263     $saved_domsvc = '';
264   }
265
266   #svc_phone
267   $svc_phone{$_} = $cgi->param($_)
268     foreach qw( countrycode phonenum sip_password pin phone_name );
269
270   #svc_dsl (phonenum came in with svc_phone)
271   $svc_phone{$_} = $cgi->param($_)
272     foreach qw( password isp_chg isp_prev vendor_qual_id );
273
274 } elsif ( $cgi->keywords ) { #editing
275
276   die "access denied"
277     unless $curuser->access_right('Edit customer');
278
279   my( $query ) = $cgi->keywords;
280   $query =~ /^(\d+)$/;
281   $custnum=$1;
282   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
283   if ( $cust_main->dbdef_table->column('paycvv')
284        && length($cust_main->paycvv)             ) {
285     my $paycvv = $cust_main->paycvv;
286     $paycvv =~ s/./*/g;
287     $cust_main->paycvv($paycvv);
288   }
289   @invoicing_list = $cust_main->invoicing_list;
290   $ss = $conf->exists('unmask_ss') ? $cust_main->ss : $cust_main->masked('ss');
291   $stateid = $cust_main->masked('stateid');
292   $payinfo = $cust_main->paymask;
293
294 } else { #new customer
295
296   die "access denied"
297     unless $curuser->access_right('New customer');
298
299   $custnum='';
300   $cust_main = new FS::cust_main ( {} );
301   $cust_main->agentnum( $conf->config('default_agentnum') )
302     if $conf->exists('default_agentnum');
303   $cust_main->otaker( &getotaker );
304   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
305   @invoicing_list = ();
306   push @invoicing_list, 'POST'
307     unless $conf->exists('disablepostalinvoicedefault');
308   $ss = '';
309   $stateid = '';
310   $payinfo = '';
311
312   if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
313     my $qualnum = $1;
314     my $qual = qsearchs('qual', { 'qualnum' => $qualnum } )
315       or die "unknown qualnum $qualnum";
316
317     my $prospect_main = $qual->cust_or_prospect;
318     $prospectnum = $prospect_main->prospectnum
319       or die "qualification not on a prospect";
320
321     $cust_main->agentnum( $prospect_main->agentnum );
322     $cust_main->company(  $prospect_main->company  );
323
324     #first contact? -> name
325     my @contacts = $prospect_main->contact;
326     my $contact = $contacts[0];
327     $cust_main->first( $contact->first );
328     $cust_main->set( 'last', $contact->get('last') );
329     #contact phone numbers?
330
331     #location -> address  (all prospect quals have location, right?)
332     my $cust_location = $qual->cust_location;
333     $cust_location->dealternize;
334     $cust_main->$_( $cust_location->$_ )
335       foreach qw( address1 address2 city county state zip country latitude longitude coord_auto geocode );
336
337     #locationnum -> package order
338     $locationnum = $qual->locationnum;
339
340     #pkgpart handled by lock_pkgpart below
341
342     #service telephone & vendor_qual_id -> svc_dsl
343     $svc_dsl{$_} = $qual->$_
344       foreach qw( phonenum vendor_qual_id );
345   }
346   else {
347     my $countrydefault = $conf->config('countrydefault') || 'US';
348     my $statedefault = $conf->config('statedefault') || 'CA';
349     $cust_main->set('bill_location', 
350       FS::cust_location->new(
351         { country => $countrydefault, state => $statedefault }
352       )
353     );
354     $cust_main->set('ship_location',
355       FS::cust_location->new(
356         { country => $countrydefault, state => $statedefault }
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 %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
372 $cgi->delete( grep { !$keep{$_} && $_ !~ /^tax_/ } $cgi->param );
373
374 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
375 $title = mt($title);
376 $title .= ": ". $cust_main->name if $custnum;
377
378 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
379
380 </%init>