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