add svc_phone on new customer first package, RT#4315
[freeside.git] / httemplate / edit / process / cust_main.cgi
1 % if ( $error ) {
2 %   $cgi->param('error', $error);
3 %
4 <% $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string ) %>
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 die "access denied"
20   unless $FS::CurrentUser::CurrentUser->access_right('Edit customer');
21
22 my $error = '';
23
24 #unmunge stuff
25
26 $cgi->param('tax','') unless defined $cgi->param('tax');
27
28 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
29
30 my $payby = $cgi->param('payby');
31
32 my %noauto = (
33   'CARD' => 'DCRD',
34   'CHEK' => 'DCHK',
35 );
36 $payby = $noauto{$payby}
37   if ! $cgi->param('payauto') && exists $noauto{$payby};
38
39 if ( $payby ) {
40   if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
41     $cgi->param('payinfo',
42       $cgi->param('payinfo1'). '@'. $cgi->param('payinfo2') );
43   }
44   $cgi->param('paydate',
45     $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
46 }
47
48 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
49 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
50 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
51 $cgi->param('invoicing_list', join(',', @invoicing_list) );
52
53
54 #create new record object
55
56 my $new = new FS::cust_main ( {
57   map {
58     $_, scalar($cgi->param($_))
59 #  } qw(custnum agentnum last first ss company address1 address2 city county
60 #       state zip daytime night fax payby payinfo paydate payname tax
61 #       otaker refnum)
62   } fields('cust_main')
63 } );
64
65 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
66   $new->setfield("ship_$_", '') foreach qw(
67     last first company address1 address2 city county state zip
68     country daytime night fax
69   );
70 }
71
72 if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10})$/) {
73   my $conf = new FS::Conf;
74   my $format = $conf->config('date_format') || "%m/%d/%Y";
75   my $parser = DateTime::Format::Strptime->new(pattern => $format,
76                                                time_zone => 'floating',
77                                               );
78   my $dt =  $parser->parse_datetime($1);
79   if ($dt) {
80     $new->setfield('birthdate', $dt->epoch);
81     $cgi->param('birthdate', $dt->epoch);
82   } else {
83 #    $error ||= $cgi->param('birthdate') . " is an invalid birthdate:" . $parser->errmsg;
84     $error ||= "Invalid birthdate: " . $cgi->param('birthdate') . ".";
85     $cgi->param('birthdate', '');
86   }
87 }
88
89 $new->setfield('paid', $cgi->param('paid') )
90   if $cgi->param('paid');
91
92 #perhaps this stuff should go to cust_main.pm
93 if ( $new->custnum eq '' ) {
94
95   my $cust_pkg = '';
96   my $svc;
97
98   if ( $cgi->param('pkgpart_svcpart') ) {
99
100     my $x = $cgi->param('pkgpart_svcpart');
101     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
102     my($pkgpart, $svcpart) = ($1, $2);
103     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
104     #false laziness: copied from FS::cust_pkg::order (which should become a
105     #FS::cust_main method)
106     my(%part_pkg);
107     # generate %part_pkg
108     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
109     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
110
111     if ( $agent ) {
112       # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
113       my $pkgpart_href = $agent->pkgpart_hashref
114         if $agent;
115       #eslaf
116
117       # this should wind up in FS::cust_pkg!
118       $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
119                  ") can't purchase pkgpart ". $pkgpart
120         #unless $part_pkg{ $pkgpart };
121         unless $pkgpart_href->{ $pkgpart }
122             || $agent->agentnum == $part_pkg->agentnum;
123     } else {
124       $error = 'Select agent';
125     }
126
127     $cust_pkg = new FS::cust_pkg ( {
128       #later         'custnum' => $custnum,
129       'pkgpart' => $pkgpart,
130     } );
131     #$error ||= $cust_pkg->check;
132
133     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
134
135     #$error ||= $cust_svc->check;
136
137     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
138     my $svcdb = $part_svc->svcdb;
139
140     if ( $svcdb eq 'svc_acct' ) {
141
142       my %svc_acct = (
143                        'svcpart'   => $svcpart,
144                        'username'  => scalar($cgi->param('username')),
145                        '_password' => scalar($cgi->param('_password')),
146                        'popnum'    => scalar($cgi->param('popnum')),
147                      );
148       $svc_acct{'domsvc'} = $cgi->param('domsvc')
149         if $cgi->param('domsvc');
150
151       $svc = new FS::svc_acct \%svc_acct;
152
153       #and just in case you were silly
154       $svc->svcpart($svcpart);
155       $svc->username($cgi->param('username'));
156       $svc->_password($cgi->param('_password'));
157       $svc->popnum($cgi->param('popnum'));
158
159     } elsif ( $svcdb eq 'svc_phone' ) {
160
161       my %svc_phone = (
162                         'svcpart' => $svcpart,
163                         map { $_ => scalar($cgi->param($_)) }
164                           qw( countrycode phonenum sip_password pin phone_name )
165                       );
166
167       $svc = new FS::svc_phone \%svc_phone;
168
169     } else {
170       die "$svcdb not handled on new customer yet";
171     }
172
173     #$error ||= $svc_acct->check;
174
175   }
176
177   use Tie::RefHash;
178   tie my %hash, 'Tie::RefHash';
179   %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
180   $error ||= $new->insert( \%hash, \@invoicing_list );
181
182   my $conf = new FS::Conf;
183   if ( $conf->exists('backend-realtime') && ! $error ) {
184
185     my $berror =    $new->bill
186                  || $new->apply_payments_and_credits
187                  || $new->collect( 'realtime' => 1 );
188     warn "Warning, error billing during backend-realtime: $berror" if $berror;
189
190   }
191   
192 } else { #create old record object
193
194   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
195   $error ||= "Old record not found!" unless $old;
196   if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
197     $new->paycvv($old->paycvv);
198   }
199   if ($new->ss =~ /xx/) {
200     $new->ss($old->ss);
201   }
202   if ($new->stateid =~ /^xxx/) {
203     $new->stateid($old->stateid);
204   }
205   if ($new->payby =~ /^(CARD|DCRD)$/ && $new->payinfo =~ /xx/) {
206     $new->payinfo($old->payinfo);
207   } elsif ($new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/) {
208     #fix for #3085 "edit of customer's routing code only surprisingly causes
209     #nothing to happen...
210     # this probably won't do the right thing when we don't have the
211     # public key (can't actually get the real $old->payinfo)
212     my($new_account, $new_aba) = split('@', $new->payinfo);
213     my($old_account, $old_aba) = split('@', $old->payinfo);
214     $new_account = $old_account if $new_account =~ /xx/;
215     $new_aba     = $old_aba     if $new_aba     =~ /xx/;
216     $new->payinfo($new_account.'@'.$new_aba);
217   }
218
219   warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
220   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
221   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
222
223   $error ||= $new->replace($old, \@invoicing_list);
224
225   warn "$me returned from replace" if $DEBUG;
226   
227 }
228
229 </%init>