1 #this stuff is SG-specific (i.e. multi-customer company username hack)
3 package FS::ClientAPI::SGNG;
6 use vars qw( $cache $DEBUG );
7 use Time::Local qw(timelocal timelocal_nocheck);
8 use Business::CreditCard;
9 use FS::Record qw( qsearch qsearchs );
12 use FS::ClientAPI::MyAccount; #qw( payment_info process_payment )
17 $cache ||= new FS::ClientAPI_SessionCache( {
18 'namespace' => 'FS::ClientAPI::MyAccount', #yes, share session_ids
22 #this might almost be general-purpose
26 my $session = _cache->get($p->{'session_id'})
27 or return { 'error' => "Can't resume session" }; #better error message
29 my $custnum = $session->{'custnum'};
31 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
32 or return { 'error' => "unknown custnum $custnum" };
34 return { 'error' => 'Not a complimentary customer' }
35 unless $cust_main->payby eq 'COMP';
38 $cust_main->paydate =~ /^\S+$/ ? $cust_main->paydate : '2037-12-31';
40 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
42 my $date = timelocal(0,0,0,$payday,--$paymonth,$payyear);
44 foreach my $cust_pkg (
45 qsearch({ 'table' => 'cust_pkg',
46 'hashref' => { 'custnum' => $custnum,
49 'extra_sql' => ' AND '. FS::cust_pkg->active_sql,
52 $cust_pkg->set('bill', $date);
53 my $error = $cust_pkg->replace;
54 return { 'error' => $error } if $error;
57 return { 'error' => '' };
61 #find old payment info
62 # (should work just like MyAccount::payment_info, except returns previous info
64 # definitly sg-specific, no one else stores past customer records like this
65 sub previous_payment_info {
68 my $session = _cache->get($p->{'session_id'})
69 or return { 'error' => "Can't resume session" }; #better error message
71 my $payment_info = FS::ClientAPI::MyAccount::payment_info($p);
73 my $custnum = $session->{'custnum'};
75 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
76 or return { 'error' => "unknown custnum $custnum" };
79 return $payment_info if $cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/;
81 foreach my $prev_cust_main (
82 reverse _previous_cust_main( 'custnum' => $custnum,
83 'username' => $cust_main->company,
88 next unless $prev_cust_main->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/;
90 if ( $prev_cust_main->payby =~ /^(CARD|DCRD)$/ ) {
93 my ($payyear,$paymonth,$payday) = split (/-/, $cust_main->paydate);
95 my $expdate = timelocal_nocheck(0,0,0,1,$paymonth,$payyear);
97 next if $expdate < time;
99 } elsif ( $prev_cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
101 #any check? or just skip these in favor of cards?
105 return { %$payment_info,
106 #$prev_cust_main->payment_info
107 _cust_main_payment_info( $prev_cust_main ),
108 'previous_custnum' => $prev_cust_main->custnum,
113 #still nothing? return an error?
114 return $payment_info;
118 #this is really FS::cust_main::payment_info, but here for now
119 sub _cust_main_payment_info {
124 $return{balance} = $self->balance;
126 $return{payname} = $self->payname
127 || ( $self->first. ' '. $self->get('last') );
129 $return{$_} = $self->get($_) for qw(address1 address2 city state zip);
131 $return{payby} = $self->payby;
132 $return{stateid_state} = $self->stateid_state;
134 if ( $self->payby =~ /^(CARD|DCRD)$/ ) {
135 $return{card_type} = cardtype($self->payinfo);
136 $return{payinfo} = $self->paymask;
138 @return{'month', 'year'} = $self->paydate_monthyear;
142 if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
143 my ($payinfo1, $payinfo2) = split '@', $self->paymask;
144 $return{payinfo1} = $payinfo1;
145 $return{payinfo2} = $payinfo2;
146 $return{paytype} = $self->paytype;
147 $return{paystate} = $self->paystate;
151 #doubleclick protection
153 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
159 #find old cust_main records (with payments)
160 sub _previous_cust_main {
162 my $custnum = $opt{'custnum'};
163 my $username = $opt{'username'};
166 if ( $opt{'with_payments'} ) {
167 $search{'extra_sql'} =
168 ' AND 0 < ( SELECT COUNT(*) FROM cust_pay
169 WHERE cust_pay.custnum = cust_main.custnum
175 'table' => 'cust_main',
176 'hashref' => { 'company' => { op => 'ILIKE', value => $opt{'username'} },
177 'custnum' => { op => '!=', value => $opt{'custnum'} },
179 'order_by' => 'ORDER BY custnum',
185 #since we could be passing masked old CC data, need to look that up and
186 #replace it (like regular process_payment does) w/info from old customer record
187 sub previous_process_payment {
190 return FS::ClientAPI::MyAccount::process_payment($p)
191 unless $p->{'previous_custnum'}
192 && ( ( $p->{'payby'} =~ /^(CARD|DCRD)$/ && $p->{'payinfo'} =~ /x/i )
193 || ( $p->{'payby'} =~ /^(CHEK|DCHK)$/ && $p->{'payinfo1'} =~ /x/i )
196 my $session = _cache->get($p->{'session_id'})
197 or return { 'error' => "Can't resume session" }; #better error message
199 my $custnum = $session->{'custnum'};
201 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
202 or return { 'error' => "unknown custnum $custnum" };
204 #make sure this is really a previous custnum of this customer
205 my @previous_cust_main =
206 grep { $_->custnum == $p->{'previous_custnum'} }
207 _previous_cust_main( 'custnum' => $custnum,
208 'username' => $cust_main->company,
209 'with_payments' => 1,
212 my $previous_cust_main = $previous_cust_main[0];
214 #causes problems with old data w/old masking method
215 #if $previous_cust_main->paymask eq $payinfo;
217 if ( $p->{'payby'} =~ /^(CHEK|DCHK)$/ && $p->{'payinfo1'} =~ /x/i ) {
218 ( $p->{'payinfo1'}, $p->{'payinfo2'} ) =
219 split('@', $previous_cust_main->payinfo);
220 } elsif ( $p->{'payby'} =~ /^(CARD|DCRD)$/ && $p->{'payinfo'} =~ /x/i ) {
221 $p->{'payinfo'} = $previous_cust_main->payinfo;
224 FS::ClientAPI::MyAccount::process_payment($p);
228 sub previous_process_payment_order_pkg {
231 my $hr = previous_process_payment($p);
232 return $hr if $hr->{'error'};
237 sub previous_process_payment_change_pkg {
240 my $hr = previous_process_payment($p);
241 return $hr if $hr->{'error'};
246 sub previous_process_payment_order_renew {
249 my $hr = previous_process_payment($p);
250 return $hr if $hr->{'error'};