Merge branch 'master' of https://github.com/rvandam/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 $m->comp('/elements/handle_uri_query', 'secure'=>1);
207
208 if ( $cgi->param('error') ) {
209
210   $same = ($cgi->param('same') || '') eq 'Y';
211   # false laziness w/ edit/process/cust_main.cgi
212   my %locations;
213   for my $pre (qw(bill ship)) {
214     my %hash;
215     foreach ( FS::cust_main->location_fields ) {
216       $hash{$_} = scalar($cgi->param($pre.'_'.$_));
217     }
218     $hash{'custnum'} = $cgi->param('custnum');
219     $locations{$pre} = qsearchs('cust_location', \%hash)
220                        || FS::cust_location->new( \%hash );
221   }
222   if ( $same ) {
223     $locations{ship} = $locations{bill};
224   }
225
226   $cust_main = new FS::cust_main ( {
227     map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
228     map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
229   } );
230
231   for my $pre (qw(bill ship)) {
232     $cust_main->set($pre.'_location', $locations{$pre});
233     $cust_main->set($pre.'_locationnum', $locations{$pre}->locationnum);
234   }
235
236   $custnum = $cust_main->custnum;
237
238   die "access denied"
239     unless $curuser->access_right($custnum ? 'Edit customer' : 'New customer');
240
241   @invoicing_list = split( /\s*,\s*/, $cgi->param('invoicing_list') );
242   $cust_main->setfield('paid' => $cgi->param('paid')) if $cgi->param('paid');
243   $ss = $cust_main->ss;           # don't mask an entered value on errors
244   $stateid = $cust_main->stateid; # don't mask an entered value on errors
245   $payinfo = $cust_main->payinfo; # don't mask an entered value on errors
246
247   $cust_main->national_id( $cgi->param('national_id1') || $cgi->param('national_id2') );
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     or die "custnum $custnum not found";
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->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   $cgi->param('tagnum', FS::part_tag->default_tags);
313
314   if ( $cgi->param('qualnum') =~ /^(\d+)$/ ) {
315     my $qualnum = $1;
316     my $qual = qsearchs('qual', { 'qualnum' => $qualnum } )
317       or die "unknown qualnum $qualnum";
318
319     my $prospect_main = $qual->cust_or_prospect;
320     $prospectnum = $prospect_main->prospectnum
321       or die "qualification not on a prospect";
322
323     $cust_main->agentnum( $prospect_main->agentnum );
324     $cust_main->company(  $prospect_main->company  );
325
326     #first contact? -> name
327     my @contacts = $prospect_main->contact;
328     my $contact = $contacts[0];
329     $cust_main->first( $contact->first );
330     $cust_main->set( 'last', $contact->get('last') );
331     #contact phone numbers?
332
333     #location -> address  (all prospect quals have location, right?)
334     my $cust_location = $qual->cust_location;
335     $cust_location->dealternize;
336     $cust_main->$_( $cust_location->$_ )
337       foreach qw( address1 address2 city county state zip country latitude longitude coord_auto geocode );
338
339     #locationnum -> package order
340     $locationnum = $qual->locationnum;
341
342     #pkgpart handled by lock_pkgpart below
343
344     #service telephone & vendor_qual_id -> svc_dsl
345     $svc_dsl{$_} = $qual->$_
346       foreach qw( phonenum vendor_qual_id );
347   }
348   else {
349     my $countrydefault = $conf->config('countrydefault') || 'US';
350     my $statedefault = $conf->config('statedefault') || 'CA';
351     $cust_main->set('bill_location', 
352       FS::cust_location->new( {
353           country => $countrydefault,
354           state => $statedefault,
355           coord_auto => 'Y',
356       } )
357     );
358     $cust_main->set('ship_location',
359       FS::cust_location->new( {
360           country => $countrydefault,
361           state => $statedefault,
362           coord_auto => 'Y',
363       } )
364     );
365   }
366
367   if ( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ) {
368     my $pkgpart = $1;
369     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
370       or die "unknown pkgpart $pkgpart";
371     my $svcpart = $part_pkg->svcpart;
372     $pkgpart_svcpart = $pkgpart.'_'.$svcpart;
373   }
374
375 }
376
377 my %keep = map { $_=>1 } qw( error tagnum lock_agentnum lock_pkgpart );
378 $cgi->delete( grep { !$keep{$_} && $_ !~ /^tax_/ } $cgi->param );
379
380 my $title = $custnum ? 'Edit Customer' : 'Add Customer';
381 $title = mt($title);
382 $title .= ": ". $cust_main->name if $custnum;
383
384 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
385
386 </%init>