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->config('national_id-country')
32 %      || $conf->exists('cust_main-enable_birthdate')
33 %      || $conf->exists('cust_main-enable_spouse_birthdate')
34 %      || $conf->exists('cust_main-enable_anniversary_date')
35 %    )
36 % {
37   <BR>
38   <& cust_main/birthdate.html, $cust_main &>
39 % }
40 % my $has_ship_address = '';
41 % if ( $cgi->param('error') ) {
42 %   $has_ship_address = !$same;
43 % } elsif ( $cust_main->custnum ) {
44 %   $has_ship_address = $cust_main->has_ship_address;
45 % }
46 <BR>
47 <TABLE> <TR>
48   <TD STYLE="width:650px">
49 %#; padding-right:2px; vertical-align:top">
50     <FONT CLASS="fsinnerbox-title"><% mt('Billing address') |h %></FONT>
51     <TABLE CLASS="fsinnerbox" WIDTH="100%">
52     <& cust_main/before_bill_location.html, $cust_main &>
53     <& /elements/location.html,
54         object => $cust_main->bill_location,
55         prefix => 'bill_',
56         enable_coords => 1,
57     &>
58     <& cust_main/after_bill_location.html, $cust_main &>
59     </TABLE>
60   </TD>
61 </TR>
62 <TR><TD STYLE="height:40px"></TD></TR>
63 <TR>
64   <TD STYLE="width:650px">
65     <FONT CLASS="fsinnerbox-title"><% mt('Service address') |h %></FONT>
66     <INPUT TYPE="checkbox" 
67            NAME="same"
68            ID="same"
69            onclick="samechanged(this)"
70            onkeyup="samechanged(this)"
71            VALUE="Y"
72            <% $has_ship_address ? '' : 'CHECKED' %>
73     ><% mt('same as billing address') |h %>
74     <DIV CLASS="fsinnerbox">
75       <TABLE ID="table_ship_location" WIDTH="100%">
76       <& cust_main/before_ship_location.html, $cust_main &>
77       <& /elements/location.html,
78           object => $cust_main->ship_location,
79           prefix => 'ship_',
80           enable_censustract => 1,
81           enable_district => 1,
82           enable_coords => 1,
83       &>
84       </TABLE>
85     </DIV>
86   </TD>
87 </TR></TABLE>
88
89 <SCRIPT>
90 function samechanged(what) {
91 %# not display = 'none', because we still want it to take up space
92 %#  document.getElementById('table_ship_location').style.visibility = 
93 %#    what.checked ? 'hidden' : 'visible';
94   var t1 = document.getElementById('table_ship_location');
95   if ( what.checked ) {
96     t1.style.visibility = 'hidden';
97   }
98   else {
99     t1.style.visibility = 'visible'
100   }
101 }
102 //samechanged(document.getElementById('same'));
103 </SCRIPT>
104
105 <BR>
106
107 <& cust_main/contacts_new.html,
108              'cust_main' => $cust_main,
109 &>
110
111 %# billing info
112 <& cust_main/billing.html, $cust_main,
113                'payinfo'        => $payinfo,
114                'invoicing_list' => \@invoicing_list,
115 &>
116 <BR>
117
118 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
119 % if (!$ro_comments || $cust_main->comments) {
120
121     <BR>
122     <FONT CLASS="fsinnerbox-title"><% mt('Comments') |h %></FONT>
123     <TABLE CLASS="fsinnerbox">
124       <TR>
125         <TD>
126           <TEXTAREA NAME = "comments"
127                     COLS = 80
128                     ROWS = 5
129                     WRAP = "HARD"
130                     <% $ro_comments %>
131           ><% $cust_main->comments %></TEXTAREA>
132         </TD>
133       </TR>
134     </TABLE>
135
136 % }
137
138 % unless ( $custnum ) {
139
140     <& cust_main/first_pkg.html, $cust_main,
141                  'pkgpart_svcpart' => $pkgpart_svcpart,
142                  'disable_empty'   =>
143                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
144                  'username'        => $username,
145                  'password'        => $password,
146                  'popnum'          => $popnum,
147                  'saved_domsvc'    => $saved_domsvc,
148                  %svc_phone,
149                  %svc_dsl,
150     &>
151
152 % }
153
154 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
155
156 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
157
158 %# cust_main/bottomfixup.js
159 % foreach my $hidden (
160 %    'payauto', 'billday',
161 %    'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
162 %    'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
163 %    'paystart_month', 'paystart_year', 'payissue',
164 %    'payip',
165 %    'paid',
166 % ) {
167     <INPUT TYPE="hidden" NAME="<% $hidden %>" VALUE="">
168 % } 
169
170 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
171
172 <BR>
173 <INPUT TYPE    = "button"
174        NAME    = "submitButton"
175        ID      = "submitButton"
176        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
177        onClick = "this.disabled=true; bottomfixup(this.form);"
178 >
179 <BR><BR>
180 </FORM>
181
182 <& /elements/footer.html &>
183
184 <%init>
185
186 my $curuser = $FS::CurrentUser::CurrentUser;
187
188 #probably redundant given the checks below...
189 die "access denied"
190   unless $curuser->access_right('New customer')
191      ||  $curuser->access_right('Edit customer');
192
193 my $conf = new FS::Conf;
194
195 #get record
196
197 my($custnum, $cust_main, $ss, $stateid, $payinfo, @invoicing_list);
198 my $pkgpart_svcpart = ''; #first_pkg
199 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
200 my %svc_phone = ();
201 my %svc_dsl = ();
202 my $prospectnum = '';
203 my $locationnum = '';
204 my $same = '';
205
206
207 if ( $cgi->param('error') ) {
208
209   $same = ($cgi->param('same') || '') eq 'Y';
210   # false laziness w/ edit/process/cust_main.cgi
211   my %locations;
212   for my $pre (qw(bill ship)) {
213     my %hash;
214     foreach ( FS::cust_main->location_fields ) {
215       $hash{$_} = scalar($cgi->param($pre.'_'.$_));
216     }
217     $hash{'custnum'} = $cgi->param('custnum');
218     $locations{$pre} = qsearchs('cust_location', \%hash)
219                        || FS::cust_location->new( \%hash );
220   }
221   if ( $same ) {
222     $locations{ship} = $locations{bill};
223   }
224
225   $cust_main = new FS::cust_main ( {
226     map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
227     map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
228   } );
229
230   for my $pre (qw(bill ship)) {
231     $cust_main->set($pre.'_location', $locations{$pre});
232     $cust_main->set($pre.'_locationnum', $locations{$pre}->locationnum);
233   }
234
235   $custnum = $cust_main->custnum;
236
237   die "access denied"
238     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
239
240   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
241   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
242   $ss = $cust_main->ss;           # don't mask an entered value on errors
243   $stateid = $cust_main->stateid; # don't mask an entered value on errors
244   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
245
246   $cust_main->national_id( $cgi->param('national_id1') || $cgi->param('national_id2') );
247
248   $prospectnum = $cgi->param('prospectnum') || '';
249
250   $pkgpart_svcpart = $cgi->param('pkgpart_svcpart') || '';
251
252   $locationnum = $cgi->param('locationnum') || '';
253
254   #svc_acct
255   $username = $cgi->param('username');
256   $password = $cgi->param('_password');
257   $popnum = $cgi->param('popnum');
258   $saved_domsvc = $cgi->param('domsvc') || '';
259   if ( $saved_domsvc =~ /^(\d+)$/ ) {
260     $saved_domsvc = $1;
261   } else {
262     $saved_domsvc = '';
263   }
264
265   #svc_phone
266   $svc_phone{$_} = $cgi->param($_)
267     foreach qw( countrycode phonenum sip_password pin phone_name );
268
269   #svc_dsl (phonenum came in with svc_phone)
270   $svc_phone{$_} = $cgi->param($_)
271     foreach qw( password isp_chg isp_prev vendor_qual_id );
272
273 } elsif ( $cgi->keywords ) { #editing
274
275   die "access denied"
276     unless $curuser->access_right('Edit customer');
277
278   my( $query ) = $cgi->keywords;
279   $query =~ /^(\d+)$/;
280   $custnum=$1;
281   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
282     or die "custnum $custnum not found";
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->referral_custnum( $cgi->param('referral_custnum') );
304   @invoicing_list = ();
305   push @invoicing_list, 'POST'
306     unless $conf->exists('disablepostalinvoicedefault');
307   $ss = '';
308   $stateid = '';
309   $payinfo = '';
310
311   $cgi->param('tagnum', FS::part_tag->default_tags);
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,
353           state => $statedefault,
354           coord_auto => 'Y',
355       } )
356     );
357     $cust_main->set('ship_location',
358       FS::cust_location->new( {
359           country => $countrydefault,
360           state => $statedefault,
361           coord_auto => 'Y',
362       } )
363     );
364   }
365
366   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
367     my $pkgpart = $1;
368     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
369       or die "unknown pkgpart $pkgpart";
370     my $svcpart = $part_pkg->svcpart;
371     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
372   }
373
374 }
375
376 my %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
377 $cgi->delete( grep { !$keep{$_} && $_ !~ /^tax_/ } $cgi->param );
378
379 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
380 $title = mt($title);
381 $title .= ": ". $cust_main->name if $custnum;
382
383 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
384
385 </%init>