2 % $cgi->param('error', $error);
4 <% $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string ) %>
8 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum) %>
13 my $me = '[edit/process/cust_main.cgi]';
20 unless $FS::CurrentUser::CurrentUser->access_right('Edit customer');
26 $cgi->param('tax','') unless defined $cgi->param('tax');
28 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
30 #my $payby = $cgi->param('payby');
31 my $payby = $cgi->param('select'); # XXX key
37 $payby = $noauto{$payby}
38 if ! $cgi->param('payauto') && exists $noauto{$payby};
40 $cgi->param('payby', $payby);
43 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
44 $cgi->param('payinfo',
45 $cgi->param('payinfo1'). '@'. $cgi->param('payinfo2') );
47 $cgi->param('paydate',
48 $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
51 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
52 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
53 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
54 $cgi->param('invoicing_list', join(',', @invoicing_list) );
57 #create new record object
59 my $new = new FS::cust_main ( {
61 $_, scalar($cgi->param($_))
62 # } qw(custnum agentnum last first ss company address1 address2 city county
63 # state zip daytime night fax payby payinfo paydate payname tax
68 delete( $new->hashref->{'agent_custid'} )
69 unless $new->hashref->{'agent_custid'};
71 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
72 $new->setfield("ship_$_", '') foreach qw(
73 last first company address1 address2 city county state zip
74 country daytime night fax
78 if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10})$/) {
79 my $conf = new FS::Conf;
80 my $format = $conf->config('date_format') || "%m/%d/%Y";
81 my $parser = DateTime::Format::Strptime->new(pattern => $format,
82 time_zone => 'floating',
84 my $dt = $parser->parse_datetime($1);
86 $new->setfield('birthdate', $dt->epoch);
87 $cgi->param('birthdate', $dt->epoch);
89 # $error ||= $cgi->param('birthdate') . " is an invalid birthdate:" . $parser->errmsg;
90 $error ||= "Invalid birthdate: " . $cgi->param('birthdate') . ".";
91 $cgi->param('birthdate', '');
95 $new->setfield('paid', $cgi->param('paid') )
96 if $cgi->param('paid');
98 #perhaps this stuff should go to cust_main.pm
101 if ( $new->custnum eq '' ) {
103 if ( $cgi->param('pkgpart_svcpart') ) {
104 my $x = $cgi->param('pkgpart_svcpart');
105 $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
106 my($pkgpart, $svcpart) = ($1, $2);
107 #false laziness: copied from FS::cust_pkg::order (which should become a
108 #FS::cust_main method)
111 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
112 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
114 #foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
115 # my($pkgpart)=$type_pkgs->pkgpart;
116 # $part_pkg{$pkgpart}++;
118 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
119 my $pkgpart_href = $agent->pkgpart_hashref;
122 # this should wind up in FS::cust_pkg!
123 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't ".
124 "purchase pkgpart ". $pkgpart
125 #unless $part_pkg{ $pkgpart };
126 unless $pkgpart_href->{ $pkgpart };
128 $cust_pkg = new FS::cust_pkg ( {
129 #later 'custnum' => $custnum,
130 'pkgpart' => $pkgpart,
132 #$error ||= $cust_pkg->check;
134 #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
136 #$error ||= $cust_svc->check;
139 'svcpart' => $svcpart,
140 'username' => $cgi->param('username'),
141 '_password' => $cgi->param('_password'),
142 'popnum' => $cgi->param('popnum'),
144 $svc_acct{'domsvc'} = $cgi->param('domsvc')
145 if $cgi->param('domsvc');
147 $svc_acct = new FS::svc_acct \%svc_acct;
149 #and just in case you were silly
150 $svc_acct->svcpart($svcpart);
151 $svc_acct->username($cgi->param('username'));
152 $svc_acct->_password($cgi->param('_password'));
153 $svc_acct->popnum($cgi->param('popnum'));
155 #$error ||= $svc_acct->check;
157 } elsif ( $cgi->param('username') ) { #good thing to catch
158 $error = "Can't assign username without a package!";
162 tie my %hash, 'Tie::RefHash';
163 %hash = ( $cust_pkg => [ $svc_acct ] ) if $cust_pkg;
164 $error ||= $new->insert( \%hash, \@invoicing_list );
166 my $conf = new FS::Conf;
167 if ( $conf->exists('backend-realtime') && ! $error ) {
169 my $berror = $new->bill
170 || $new->apply_payments_and_credits
171 || $new->collect( 'realtime' => 1 );
172 warn "Warning, error billing during backend-realtime: $berror" if $berror;
176 } else { #create old record object
178 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
179 $error ||= "Old record not found!" unless $old;
180 if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
181 $new->paycvv($old->paycvv);
183 if ($new->ss =~ /xx/) {
186 if ($new->stateid =~ /^xxx/) {
187 $new->stateid($old->stateid);
189 if ($new->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ && $new->payinfo =~ /xx/) {
190 $new->payinfo($old->payinfo);
193 warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
194 local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
195 local($FS::Record::DEBUG) = $DEBUG if $DEBUG;
197 $error ||= $new->replace($old, \@invoicing_list);
199 warn "$me returned from replace" if $DEBUG;