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>
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>
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 ID="div_ship_location">
72       <TABLE WIDTH="100%" CLASS="fsinnerbox">
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 COLSPAN="8">
84             <% include('/elements/checkbox.html',
85                  'field'      => 'invoice_ship_address',
86                  'value'      => 'Y',
87                  'curr_value' => $cust_main->invoice_ship_address,
88                  'postfix'    => emt('Include service address on invoices'),
89             ) %>
90           </TD>
91         </TR>
92 % }
93       </TABLE>
94     </DIV>
95   </TD>
96 </TR></TABLE>
97
98 <SCRIPT TYPE="text/javascript">
99
100 function samechanged(what) {
101   if ( what.checked ) {
102     $('#div_ship_location').slideUp();
103   } else {
104     $('#div_ship_location').slideDown();
105   }
106 }
107
108 % if ( ! $has_ship_address ) {
109   $('#div_ship_location').hide();
110 % }
111
112 </SCRIPT>
113
114 <& cust_main/contacts_new.html, 'cust_main'=>$cust_main, &>
115
116 %# billing info
117 <& cust_main/billing.html, $cust_main,
118                'invoicing_list' => \@invoicing_list,
119 &>
120
121 % my $ro_comments = $conf->exists('cust_main-use_comments')?'':'readonly';
122 % if (!$ro_comments || $cust_main->comments) {
123
124     <BR>
125     <FONT CLASS="fsinnerbox-title"><% mt('Comments') |h %></FONT>
126     <TABLE CLASS="fsinnerbox">
127       <TR>
128         <TD>
129           <TEXTAREA NAME = "comments"
130                     COLS = 80
131                     ROWS = 5
132                     WRAP = "HARD"
133                     <% $ro_comments %>
134           ><% $cust_main->comments %></TEXTAREA>
135         </TD>
136       </TR>
137     </TABLE>
138
139 % }
140
141 % unless ( $custnum ) {
142
143     <& cust_main/first_pkg.html, $cust_main,
144                  'pkgpart_svcpart' => $pkgpart_svcpart,
145                  'disable_empty'   =>
146                    scalar( $cgi->param('lock_pkgpart') =~ /^(\d+)$/ ),
147                  'username'        => $username,
148                  'password'        => $password,
149                  'popnum'          => $popnum,
150                  'saved_domsvc'    => $saved_domsvc,
151                  %svc_phone,
152                  %svc_dsl,
153     &>
154
155 % }
156
157 <INPUT TYPE="hidden" NAME="locationnum" VALUE="<% $locationnum %>">
158
159 <INPUT TYPE="hidden" NAME="usernum" VALUE="<% $cust_main->usernum %>">
160
161 <& cust_main/bottomfixup.html, 'custnum' => $custnum &>
162
163 <BR>
164 <INPUT TYPE    = "button"
165        NAME    = "submitButton"
166        ID      = "submitButton"
167        VALUE   = "<% $custnum ?  emt("Apply changes") : emt("Add Customer") %>"
168        onClick = "this.disabled=true; bottomfixup(this.form);"
169 >
170 <BR><BR>
171 </FORM>
172
173 <& /elements/footer.html &>
174
175 <%init>
176
177 my $curuser = $FS::CurrentUser::CurrentUser;
178
179 #probably redundant given the checks below...
180 die "access denied"
181   unless $curuser->access_right('New customer')
182      ||  $curuser->access_right('Edit customer');
183
184 my $conf = new FS::Conf;
185
186 #get record
187
188 my($custnum, $cust_main, $ss, $stateid, @invoicing_list);
189 my $pkgpart_svcpart = ''; #first_pkg
190 my($username, $password, $popnum, $saved_domsvc) = ( '', '', 0, 0 ); #svc_acct
191 my %svc_phone = ();
192 my %svc_dsl = ();
193 my $prospectnum = '';
194 my $locationnum = '';
195 my $same = '';
196
197 $m->comp('/elements/handle_uri_query', 'secure'=>1);
198
199 if ( $cgi->param('error') ) {
200
201   $same = ($cgi->param('same') || '') eq 'Y';
202   # false laziness w/ edit/process/cust_main.cgi
203   my %locations;
204   for my $pre (qw(bill ship)) {
205     my %hash;
206     foreach my $locfield ( FS::cust_main->location_fields ) {
207       # don't search on lat/long, string values can cause qsearchs to die
208       next if grep {$_ eq $locfield} qw(latitude longitude);
209       $hash{$locfield} = scalar($cgi->param($pre.'_'.$locfield));
210     }
211     $hash{'custnum'} = $cgi->param('custnum');
212     $locations{$pre} = qsearchs('cust_location', \%hash)
213                        || FS::cust_location->new( \%hash );
214     # now set lat/long, for redisplay of entered values
215     foreach my $locfield ( qw(latitude longitude) ) {
216       my $locvalue = scalar($cgi->param($pre.'_'.$locfield));
217       $locations{$pre}->set($locfield,$locvalue);
218     }
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' => scalar($cgi->param('paid')) )
241     if $cgi->param('paid');
242   $ss = $cust_main->ss;           # don't mask an entered value on errors
243   $stateid = $cust_main->stateid; # 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
292 } else { #new customer
293
294   die "access denied"
295     unless $curuser->access_right('New customer');
296
297   $custnum='';
298   $cust_main = new FS::cust_main ( {} );
299
300   my @agentnums = $curuser->agentnums;
301   $cust_main->agentnum( $agentnums[0] )
302     if scalar(@agentnums) == 1;
303   $cust_main->agentnum( $conf->config('default_agentnum') )
304     if $conf->exists('default_agentnum');
305
306   $cust_main->referral_custnum( $cgi->param('referral_custnum') );
307   $cust_main->set('postal_invoice', 'Y')
308     unless $conf->exists('disablepostalinvoicedefault');
309   $ss = '';
310   $stateid = '';
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>