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