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