multiple payment options, RT#23741
[freeside.git] / httemplate / edit / process / cust_main.cgi
1 % if ( $error ) {
2 %   $cgi->param('error', $error);
3 %   my $query = $m->scomp('/elements/create_uri_query', 'secure'=>1);
4 <% $cgi->redirect(popurl(2). "cust_main.cgi?$query" ) %>
5 %
6 % } else { 
7 %
8 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum) %>
9 %
10 % }
11 <%once>
12
13 my $me = '[edit/process/cust_main.cgi]';
14 my $DEBUG = 0;
15
16 </%once>
17 <%init>
18
19 my $curuser = $FS::CurrentUser::CurrentUser;
20 die "access denied" unless $curuser->access_right('Edit customer');
21
22 my $conf = new FS::Conf;
23
24 my $error = '';
25
26 #unmunge stuff
27
28 $cgi->param('tax','') unless defined $cgi->param('tax');
29
30 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
31
32 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
33 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
34 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
35 $cgi->param('invoicing_list', join(',', @invoicing_list) );
36
37 # is this actually used?  if so, we need to clone locations...
38 # but I can't find anything that sets this parameter to a non-empty value
39 # yes, fec48523d3cf056da08813f9b2b7d633b27aaf8d for #16582 is where it came in,
40 # for "duplicate address checking for new customers".  afaict still in
41 # edit/cust_main/bottomfixup.html (and working?)
42 $cgi->param('duplicate_of_custnum') =~ /^(\d+)$/;
43 my $duplicate_of = $1;
44
45 # if this is enabled, enforce it
46 if ( $conf->exists('agent-ship_address', $cgi->param('agentnum')) ) {
47   my $agent = FS::agent->by_key($cgi->param('agentnum'));
48   my $agent_cust_main = $agent->agent_cust_main;
49   if ( $agent_cust_main ) {
50     my $agent_location = $agent_cust_main->ship_location;
51     foreach (qw(address1 city state zip country latitude longitude district)) {
52       $cgi->param("ship_$_", $agent_location->get($_));
53     }
54   }
55 }
56
57 my %locations;
58 for my $pre (qw(bill ship)) {
59
60   my %hash;
61   foreach ( FS::cust_main->location_fields ) {
62     $hash{$_} = scalar($cgi->param($pre.'_'.$_));
63   }
64   $hash{'custnum'} = $cgi->param('custnum');
65   warn Dumper \%hash if $DEBUG;
66   $locations{$pre} = FS::cust_location->new(\%hash);
67 }
68
69 if ( ($cgi->param('same') || '') eq 'Y' ) {
70   $locations{ship} = $locations{bill};
71 }
72
73 #create new record object
74 # but explicitly avoid setting ship_ fields
75
76 my $new = new FS::cust_main ( {
77   map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
78   map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
79 } );
80
81 $new->invoice_noemail( ($cgi->param('invoice_email') eq 'Y') ? '' : 'Y' );
82
83 if ( $duplicate_of ) {
84   # then negate all changes to the customer; the only change we should
85   # make is to order a package, if requested
86   $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
87   # this should never happen
88     or die "nonexistent existing customer (custnum $duplicate_of)";
89 }
90
91 for my $pre (qw(bill ship)) {
92   $new->set($pre.'_location', $locations{$pre});
93   $new->set($pre.'_locationnum', $locations{$pre}->locationnum);
94 }
95
96 if ( $cgi->param('no_credit_limit') ) {
97   $new->setfield('credit_limit', '');
98 }
99
100 $new->tagnum( [ $cgi->param('tagnum') ] );
101
102 $error ||= $new->set_national_id_from_cgi( $cgi );
103
104 my %usedatetime = ( 'birthdate'        => 1,
105                     'spouse_birthdate' => 1,
106                     'anniversary_date' => 1,
107                   );
108
109 foreach my $dfield (qw(
110   signupdate birthdate spouse_birthdate anniversary_date
111 )) {
112
113   if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
114
115     my $value = $1;
116     my $parsed = '';
117
118     if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
119
120       my $format = $conf->config('date_format') || "%m/%d/%Y";
121       my $parser = DateTime::Format::Strptime->new( pattern   => $format,
122                                                     time_zone => 'floating',
123                                                   );
124       my $dt = $parser->parse_datetime($value);
125       if ( $dt ) {
126         $parsed = $dt->epoch;
127       } else {
128         $error ||= "Invalid $dfield: $value";
129       }
130
131     } else {
132
133       $parsed = parse_datetime($value)
134         or $error ||= "Invalid $dfield: $value";
135
136     }
137
138     $new->setfield( $dfield, $parsed );
139     $cgi->param(    $dfield, $parsed );
140
141   }
142
143 }
144
145 $new->setfield('paid', $cgi->param('paid') )
146   if $cgi->param('paid');
147
148 my %options = ();
149 if ( $curuser->access_right('Edit customer tax exemptions') ) { 
150   my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
151   my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
152   $options{'tax_exemption'} = {
153     map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt
154   };
155 }
156
157 $options{'contact_params'} = scalar($cgi->Vars);
158 $options{'cust_payby_params'} = scalar($cgi->Vars);
159
160 #perhaps this stuff should go to cust_main.pm
161 if ( $new->custnum eq '' or $duplicate_of ) {
162
163   my $cust_pkg = '';
164   my $svc;
165
166   if ( $cgi->param('pkgpart_svcpart') ) {
167
168     my $x = $cgi->param('pkgpart_svcpart');
169     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
170     my($pkgpart, $svcpart) = ($1, $2);
171     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
172     #false laziness: copied from FS::cust_pkg::order (which should become a
173     #FS::cust_main method)
174     my(%part_pkg);
175     # generate %part_pkg
176     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
177     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
178
179     if ( $agent ) {
180       # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
181       my $pkgpart_href = $agent->pkgpart_hashref
182         if $agent;
183       #eslaf
184
185       # this should wind up in FS::cust_pkg!
186       $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
187                  ") can't purchase pkgpart ". $pkgpart
188         #unless $part_pkg{ $pkgpart };
189         unless $pkgpart_href->{ $pkgpart }
190             || $agent->agentnum == $part_pkg->agentnum;
191     } else {
192       $error = 'Select agent';
193     }
194
195     $cust_pkg = new FS::cust_pkg ( {
196       #later         'custnum' => $custnum,
197       'pkgpart'     => $pkgpart,
198       'locationnum' => scalar($cgi->param('locationnum')),
199       'salesnum'    => scalar($cgi->param('salesnum')),
200     } );
201
202
203     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
204     my $svcdb = $part_svc->svcdb;
205
206     if ( $svcdb eq 'svc_acct' ) {
207
208       my %svc_acct = (
209                        'svcpart'   => $svcpart,
210                        'username'  => scalar($cgi->param('username')),
211                        '_password' => scalar($cgi->param('_password')),
212                        'popnum'    => scalar($cgi->param('popnum')),
213                      );
214       $svc_acct{'domsvc'} = $cgi->param('domsvc')
215         if $cgi->param('domsvc');
216
217       $svc = new FS::svc_acct \%svc_acct;
218
219       #and just in case you were silly
220       $svc->svcpart($svcpart);
221       $svc->username($cgi->param('username'));
222       $svc->_password($cgi->param('_password'));
223       $svc->popnum($cgi->param('popnum'));
224
225     } elsif ( $svcdb eq 'svc_phone' ) {
226
227       my %svc_phone = (
228         'svcpart' => $svcpart,
229         map { $_ => scalar($cgi->param($_)) }
230             qw( countrycode phonenum sip_password pin phone_name )
231       );
232
233       $svc = new FS::svc_phone \%svc_phone;
234
235     } elsif ( $svcdb eq 'svc_dsl' ) {
236
237       my %svc_dsl = (
238         'svcpart' => $svcpart,
239         ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
240               qw( first last company )
241         ),
242         ( map { $_ => scalar($cgi->param($_)) }
243               qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
244         ),
245         'desired_due_date'  => time, #XXX enter?
246         'vendor_order_type' => 'NEW',
247       );
248       $svc = new FS::svc_dsl \%svc_dsl;
249
250     } else {
251       die "$svcdb not handled on new customer yet";
252     }
253
254   }
255
256
257   use Tie::RefHash;
258   tie my %hash, 'Tie::RefHash';
259   %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
260   if ( $duplicate_of ) {
261     # order the package and service normally
262     $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
263   }
264   else {
265     # create the customer
266     $error ||= $new->insert( \%hash, \@invoicing_list,
267                              %options,
268                              prospectnum => scalar($cgi->param('prospectnum')),
269                            );
270
271     my $conf = new FS::Conf;
272     if ( $conf->exists('backend-realtime') && ! $error ) {
273
274       my $berror =    $new->bill
275                    || $new->apply_payments_and_credits
276                    || $new->collect( 'realtime' => 1 );
277       warn "Warning, error billing during backend-realtime: $berror" if $berror;
278
279     }
280   } #if $duplicate_of
281   
282 } else { #create old record object
283
284   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
285   $error ||= "Old record not found!" unless $old;
286
287   if ($new->ss =~ /xx/) {
288     $new->ss($old->ss);
289   }
290   if ($new->stateid =~ /^xxx/) {
291     $new->stateid($old->stateid);
292   }
293
294   if ( ! $conf->exists('cust_main-edit_signupdate') or
295        ! $new->signupdate ) {
296     $new->signupdate($old->signupdate);
297   }
298
299   warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
300   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
301   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
302
303   local($Data::Dumper::Sortkeys) = 1;
304   warn Dumper({ new => $new, old => $old }) if $DEBUG;
305
306   $error ||= $new->replace( $old, \@invoicing_list,
307                             %options,
308                           );
309
310   warn "$me returned from replace" if $DEBUG;
311   
312 }
313
314 </%init>