address standardization, part 2
[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>
44 %#; padding-right:2px; vertical-align:top">
45     <FONT CLASS="fsinnerbox-title"><% mt('Billing address') |h %></FONT>
46     <FIELDSET ID="bill_location" CLASS="location">
47     <TABLE CLASS="fsinnerbox" WIDTH="100%">
48     <& cust_main/before_bill_location.html, $cust_main &>
49     <& /elements/location.html,
50         object => $cust_main->bill_location,
51         prefix => 'bill_',
52         enable_censustract => 1,
53         enable_district => 1,
54         enable_coords => 1,
55     &>
56     <& cust_main/after_bill_location.html, $cust_main &>
57     </TABLE>
58     </FIELDSET>
59   </TD>
60 </TR>
61 <TR><TD STYLE="height:14px"></TD></TR>
62 <TR>
63   <TD>
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     <FIELDSET ID="ship_location" CLASS="location">
74       <TABLE WIDTH="100%" CLASS="fsinnerbox">
75       <& cust_main/before_ship_location.html, $cust_main &>
76       <& /elements/location.html,
77           object => $cust_main->ship_location,
78           prefix => 'ship_',
79           enable_censustract => 1,
80           enable_district => 1,
81           enable_coords => 1,
82       &>
83 % unless ($conf->exists('invoice-ship_address')) { #it's always on, so hide per-cust config
84         <TR>
85           <TD COLSPAN="8">
86             <% include('/elements/checkbox.html',
87                  'field'      => 'invoice_ship_address',
88                  'value'      => 'Y',
89                  'curr_value' => $cust_main->invoice_ship_address,
90                  'postfix'    => emt('Include service address on invoices'),
91             ) %>
92           </TD>
93         </TR>
94 % }
95       </TABLE>
96     </FIELDSET>
97   </TD>
98 </TR></TABLE>
99
100 <SCRIPT TYPE="text/javascript">
101
102 function samechanged(what) {
103   if ( what.checked ) {
104     $('#ship_location').slideUp();
105   } else {
106     $('#ship_location').slideDown();
107   }
108 }
109
110 $().ready( function() {
111   window.bill_location = new Location($('fieldset#bill_location'), 'bill_');
112   window.ship_location = new Location($('fieldset#ship_location'), 'ship_');
113
114   if ($('#same').prop('checked')) {
115     $('#ship_location').hide();
116   }
117
118 });
119
120 </SCRIPT>
121
122 <& cust_main/contacts_new.html, 'cust_main'=>$cust_main, &>
123
124 %# billing info
125 <& cust_main/billing.html, $cust_main,
126                'payinfo'        => $payinfo,
127                'invoicing_list' => \@invoicing_list,
128 &>
129
130 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
131 % if (!$ro_comments || $cust_main->comments) {
132
133     <BR>
134     <FONT CLASS="fsinnerbox-title"><% mt('Comments') |h %></FONT>
135     <TABLE CLASS="fsinnerbox">
136       <TR>
137         <TD>
138           <TEXTAREA NAME = "comments"
139                     COLS = 80
140                     ROWS = 5
141                     WRAP = "HARD"
142                     <% $ro_comments %>
143           ><% $cust_main->comments %></TEXTAREA>
144         </TD>
145       </TR>
146     </TABLE>
147
148 % }
149
150 % unless ( $custnum ) {
151
152     <& cust_main/first_pkg.html, $cust_main,
153                  'pkgpart_svcpart' => $pkgpart_svcpart,
154                  'disable_empty'   =>
155                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
156                  'username'        => $username,
157                  'password'        => $password,
158                  'popnum'          => $popnum,
159                  'saved_domsvc'    => $saved_domsvc,
160                  %svc_phone,
161                  %svc_dsl,
162     &>
163
164 % }
165
166 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
167
168 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
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 @prospect_contacts = $prospect_main->prospect_contact;
328     my $contact = $prospect_contacts[0]->contact;
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 $title = $custnum ? 'Edit Customer' : 'Add Customer';
378 $title = mt($title);
379 $title .= ": ". $cust_main->name if $custnum;
380
381 my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
382
383 </%init>