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 %      || $conf->exists('cust_main-enable_anniversary_date')
34 %    )
35 % {
36   <BR>
37   <& cust_main/birthdate.html, $cust_main &>
38 % }
39 % my $has_ship_address = '';
40 % if ( $cgi->param('error') ) {
41 %   $has_ship_address = !$same;
42 % } elsif ( $cust_main->custnum ) {
43 %   $has_ship_address = $cust_main->has_ship_address;
44 % }
45 <BR>
46 <TABLE> <TR>
47   <TD STYLE="width:650px">
48 %#; padding-right:2px; vertical-align:top">
49     <FONT CLASS="fsinnerbox-title"><% mt('Billing address') |h %></FONT>
50     <TABLE CLASS="fsinnerbox">
51     <& cust_main/before_bill_location.html, $cust_main &>
52     <& /elements/location.html,
53         object => $cust_main->bill_location,
54         prefix => 'bill_',
55     &>
56     <& cust_main/after_bill_location.html, $cust_main &>
57     </TABLE>
58   </TD>
59 </TR>
60 <TR><TD STYLE="height:40px"></TD></TR>
61 <TR>
62   <TD STYLE="width:650px">
63 %#; padding-left:2px; vertical-align:top">
64     <FONT CLASS="fsinnerbox-title"><% mt('Service address') |h %></FONT>
65     <INPUT TYPE="checkbox" 
66            NAME="same"
67            ID="same"
68            onclick="samechanged(this)"
69            onkeyup="samechanged(this)"
70            VALUE="Y"
71            <% $has_ship_address ? '' : 'CHECKED' %>
72     ><% mt('same as billing address') |h %>
73     <TABLE CLASS="fsinnerbox" ID="table_ship_location">
74     <& /elements/location.html,
75         object => $cust_main->ship_location,
76         prefix => 'ship_',
77         enable_censustract => 1,
78         enable_district => 1,
79     &>
80     </TABLE>
81     <TABLE CLASS="fsinnerbox" ID="table_ship_location_blank"
82     STYLE="display:none">
83     <TR><TD></TD></TR>
84     </TABLE>
85   </TD>
86 </TR></TABLE>
87
88 <SCRIPT>
89 function samechanged(what) {
90 %# not display = 'none', because we still want it to take up space
91 %#  document.getElementById('table_ship_location').style.visibility = 
92 %#    what.checked ? 'hidden' : 'visible';
93   var t1 = document.getElementById('table_ship_location');
94   var t2 = document.getElementById('table_ship_location_blank');
95   if ( what.checked ) {
96     t2.style.width  = t1.clientWidth  + 'px';
97     t2.style.height = t1.clientHeight + 'px';
98     t1.style.display = 'none';
99     t2.style.display = '';
100   }
101   else {
102     t2.style.display = 'none';
103     t1.style.display = '';
104   }
105 }
106 samechanged(document.getElementById('same'));
107 </SCRIPT>
108
109 <BR>
110
111 <& cust_main/contacts_new.html,
112              'cust_main' => $cust_main,
113 &>
114
115 %# billing info
116 <& cust_main/billing.html, $cust_main,
117                'payinfo'        => $payinfo,
118                'invoicing_list' => \@invoicing_list,
119 &>
120 <BR>
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.js
163 % foreach my $hidden (
164 %    'payauto', 'billday',
165 %    'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
166 %    'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
167 %    'paystart_month', 'paystart_year', 'payissue',
168 %    'payip',
169 %    'paid',
170 % ) {
171     <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
172 % } 
173
174 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
175
176 <BR>
177 <INPUT TYPE    = "button"
178        NAME    = "submitButton"
179        ID      = "submitButton"
180        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
181        onClick = "this.disabled=true; bottomfixup(this.form);"
182 >
183 <BR><BR>
184 </FORM>
185
186 <& /elements/footer.html &>
187
188 <%init>
189
190 my $curuser = $FS::CurrentUser::CurrentUser;
191
192 #probably redundant given the checks below...
193 die "access denied"
194   unless $curuser->access_right('New customer')
195      ||  $curuser->access_right('Edit customer');
196
197 my $conf = new FS::Conf;
198
199 #get record
200
201 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
202 my $pkgpart_svcpart = ''; #first_pkg
203 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
204 my %svc_phone = ();
205 my %svc_dsl = ();
206 my $prospectnum = '';
207 my $locationnum = '';
208 my $same = '';
209
210
211 if ( $cgi->param('error') ) {
212
213   $same = ($cgi->param('same') || '') eq 'Y';
214   # false laziness w/ edit/process/cust_main.cgi
215   my %locations;
216   for my $pre (qw(bill ship)) {
217     my %hash;
218     foreach ( FS::cust_main->location_fields ) {
219       $hash{$_} = scalar($cgi->param($pre.'_'.$_));
220     }
221     $hash{'custnum'} = $cgi->param('custnum');
222     $locations{$pre} = qsearchs('cust_location', \%hash)
223                        || FS::cust_location->new( \%hash );
224   }
225   if ( $same ) {
226     $locations{ship} = $locations{bill};
227   }
228
229   $cust_main = new FS::cust_main ( {
230     map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
231     map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
232   } );
233
234   for my $pre (qw(bill ship)) {
235     $cust_main->set($pre.'_location', $locations{$pre});
236     $cust_main->set($pre.'_locationnum', $locations{$pre}->locationnum);
237   }
238
239   $custnum = $cust_main->custnum;
240
241   die "access denied"
242     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
243
244   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
245   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
246   $ss = $cust_main->ss;           # don't mask an entered value on errors
247   $stateid = $cust_main->stateid; # don't mask an entered value on errors
248   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
249
250   $prospectnum = $cgi->param('prospectnum') || '';
251
252   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
253
254   $locationnum = $cgi->param('locationnum') || '';
255
256   #svc_acct
257   $username = $cgi->param('username');
258   $password = $cgi->param('_password');
259   $popnum = $cgi->param('popnum');
260   $saved_domsvc = $cgi->param('domsvc') || '';
261   if ( $saved_domsvc =~ /^(\d+)$/ ) {
262     $saved_domsvc = $1;
263   } else {
264     $saved_domsvc = '';
265   }
266
267   #svc_phone
268   $svc_phone{$_} = $cgi->param($_)
269     foreach qw( countrycode phonenum sip_password pin phone_name );
270
271   #svc_dsl (phonenum came in with svc_phone)
272   $svc_phone{$_} = $cgi->param($_)
273     foreach qw( password isp_chg isp_prev vendor_qual_id );
274
275 } elsif ( $cgi->keywords ) { #editing
276
277   die "access denied"
278     unless $curuser->access_right('Edit customer');
279
280   my( $query ) = $cgi->keywords;
281   $query =~ /^(\d+)$/;
282   $custnum=$1;
283   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
284   if ( $cust_main->dbdef_table->column('paycvv')
285        && length($cust_main->paycvv)             ) {
286     my $paycvv = $cust_main->paycvv;
287     $paycvv =~ s/./*/g;
288     $cust_main->paycvv($paycvv);
289   }
290   @invoicing_list = $cust_main->invoicing_list;
291   $ss = $conf->exists('unmask_ss') ? $cust_main->ss : $cust_main->masked('ss');
292   $stateid = $cust_main->masked('stateid');
293   $payinfo = $cust_main->paymask;
294
295 } else { #new customer
296
297   die "access denied"
298     unless $curuser->access_right('New customer');
299
300   $custnum='';
301   $cust_main = new FS::cust_main ( {} );
302   $cust_main->agentnum( $conf->config('default_agentnum') )
303     if $conf->exists('default_agentnum');
304   $cust_main->otaker( &getotaker );
305   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
306   @invoicing_list = ();
307   push @invoicing_list, 'POST'
308     unless $conf->exists('disablepostalinvoicedefault');
309   $ss = '';
310   $stateid = '';
311   $payinfo = '';
312
313   if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
314     my $qualnum = $1;
315     my $qual = qsearchs('qual', { 'qualnum' => $qualnum } )
316       or die "unknown qualnum $qualnum";
317
318     my $prospect_main = $qual->cust_or_prospect;
319     $prospectnum = $prospect_main->prospectnum
320       or die "qualification not on a prospect";
321
322     $cust_main->agentnum( $prospect_main->agentnum );
323     $cust_main->company(  $prospect_main->company  );
324
325     #first contact? -> name
326     my @contacts = $prospect_main->contact;
327     my $contact = $contacts[0];
328     $cust_main->first( $contact->first );
329     $cust_main->set( 'last', $contact->get('last') );
330     #contact phone numbers?
331
332     #location -> address  (all prospect quals have location, right?)
333     my $cust_location = $qual->cust_location;
334     $cust_location->dealternize;
335     $cust_main->$_( $cust_location->$_ )
336       foreach qw( address1 address2 city county state zip country latitude longitude coord_auto geocode );
337
338     #locationnum -> package order
339     $locationnum = $qual->locationnum;
340
341     #pkgpart handled by lock_pkgpart below
342
343     #service telephone & vendor_qual_id -> svc_dsl
344     $svc_dsl{$_} = $qual->$_
345       foreach qw( phonenum vendor_qual_id );
346   }
347   else {
348     my $countrydefault = $conf->config('countrydefault') || 'US';
349     my $statedefault = $conf->config('statedefault') || 'CA';
350     $cust_main->set('bill_location', 
351       FS::cust_location->new(
352         { country => $countrydefault, state => $statedefault }
353       )
354     );
355     $cust_main->set('ship_location',
356       FS::cust_location->new(
357         { country => $countrydefault, state => $statedefault }
358       )
359     );
360   }
361
362   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
363     my $pkgpart = $1;
364     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
365       or die "unknown pkgpart $pkgpart";
366     my $svcpart = $part_pkg->svcpart;
367     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
368   }
369
370 }
371
372 my %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
373 $cgi->delete( grep { !$keep{$_} && $_ !~ /^tax_/ } $cgi->param );
374
375 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
376 $title = mt($title);
377 $title .= ": ". $cust_main->name if $custnum;
378
379 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
380
381 </%init>