summaryrefslogtreecommitdiff
path: root/FS
diff options
context:
space:
mode:
authorJonathan Prykop <jonathan@freeside.biz>2015-08-28 00:56:49 -0500
committerJonathan Prykop <jonathan@freeside.biz>2015-08-29 17:23:58 -0500
commit8508117a6127dc363736da08be6e71cb7aefc9d3 (patch)
tree1cf9cbad185a4a6485e9706a69dec841f68440f3 /FS
parent225f922a9b48c5179b3fd9b28d9eb442d7e8598d (diff)
RT#37064: Add action link to manually refund a payment
Diffstat (limited to 'FS')
-rw-r--r--FS/FS/access_user.pm37
-rw-r--r--FS/FS/cust_main/Billing_Realtime.pm1
-rw-r--r--FS/FS/cust_pay.pm96
3 files changed, 134 insertions, 0 deletions
diff --git a/FS/FS/access_user.pm b/FS/FS/access_user.pm
index a3f55bc..ecab32d 100644
--- a/FS/FS/access_user.pm
+++ b/FS/FS/access_user.pm
@@ -587,6 +587,43 @@ sub access_right {
}
+=item refund_rights PAYBY
+
+Accepts payment $payby (BILL,CASH,MCRD,MCHK,CARD,CHEK) and returns a
+list of the refund rights associated with that $payby.
+
+Returns empty list if $payby wasn't recognized.
+
+=cut
+
+sub refund_rights {
+ my $self = shift;
+ my $payby = shift;
+ my @rights = ();
+ push @rights, 'Post refund' if $payby =~ /^(BILL|CASH|MCRD|MCHK)$/;
+ push @rights, 'Post check refund' if $payby eq 'BILL';
+ push @rights, 'Post cash refund ' if $payby eq 'CASH';
+ push @rights, 'Refund payment' if $payby =~ /^(CARD|CHEK)$/;
+ push @rights, 'Refund credit card payment' if $payby eq 'CARD';
+ push @rights, 'Refund Echeck payment' if $payby eq 'CHEK';
+ return @rights;
+}
+
+=item refund_access_right PAYBY
+
+Returns true if user has L</access_right> for any L</refund_rights>
+for the specified payby.
+
+=cut
+
+sub refund_access_right {
+ my $self = shift;
+ my $payby = shift;
+ my @rights = $self->refund_rights($payby);
+ return '' unless @rights;
+ return $self->access_right(\@rights);
+}
+
=item default_customer_view
Returns the default customer view for this user, from the
diff --git a/FS/FS/cust_main/Billing_Realtime.pm b/FS/FS/cust_main/Billing_Realtime.pm
index d973896..fda3ae0 100644
--- a/FS/FS/cust_main/Billing_Realtime.pm
+++ b/FS/FS/cust_main/Billing_Realtime.pm
@@ -1649,6 +1649,7 @@ sub realtime_refund_bop {
$order_number = $refund->order_number if $refund->can('order_number');
+ # change this to just use $cust_pay->delete_cust_bill_pay?
while ( $cust_pay && $cust_pay->unapplied < $amount ) {
my @cust_bill_pay = $cust_pay->cust_bill_pay;
last unless @cust_bill_pay;
diff --git a/FS/FS/cust_pay.pm b/FS/FS/cust_pay.pm
index 5d4f67f..59d7774 100644
--- a/FS/FS/cust_pay.pm
+++ b/FS/FS/cust_pay.pm
@@ -821,6 +821,102 @@ sub amount {
$self->paid();
}
+=item delete_cust_bill_pay OPTIONS
+
+Deletes all associated cust_bill_pay records.
+
+If option 'unapplied' is a specified, only deletes until
+this object's 'unapplied' value is >= the specified amount.
+(Deletes in order returned by L</cust_bill_pay>.)
+
+=cut
+
+sub delete_cust_bill_pay {
+ my $self = shift;
+ my %opt = @_;
+
+ local $SIG{HUP} = 'IGNORE';
+ local $SIG{INT} = 'IGNORE';
+ local $SIG{QUIT} = 'IGNORE';
+ local $SIG{TERM} = 'IGNORE';
+ local $SIG{TSTP} = 'IGNORE';
+ local $SIG{PIPE} = 'IGNORE';
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $unapplied = $self->unapplied; #only need to look it up once
+
+ my $error = '';
+
+ # Maybe we should reverse the order these get deleted in?
+ # ie delete newest first?
+ # keeping consistent with how bop refunds work, for now...
+ foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
+ last if $opt{'unapplied'} && ($unapplied > $opt{'unapplied'});
+ $unapplied += $cust_bill_pay->amount;
+ $error = $cust_bill_pay->delete;
+ last if $error;
+ }
+
+ if ($error) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+
+ $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+ return '';
+}
+
+=item refund HASHREF
+
+Accepts input for creating a new FS::cust_refund object.
+Unapplies payment from invoices up to the amount of the refund,
+creates the refund and applies payment to refund. Allows entire
+process to be handled in one transaction.
+
+Causes a fatal error if called on CARD or CHEK payments.
+
+=cut
+
+sub refund {
+ my $self = shift;
+ my $hash = shift;
+ die "Cannot call cust_pay->refund on " . $self->payby
+ if grep { $_ eq $self->payby } qw(CARD CHEK);
+
+ local $SIG{HUP} = 'IGNORE';
+ local $SIG{INT} = 'IGNORE';
+ local $SIG{QUIT} = 'IGNORE';
+ local $SIG{TERM} = 'IGNORE';
+ local $SIG{TSTP} = 'IGNORE';
+ local $SIG{PIPE} = 'IGNORE';
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $error = $self->delete_cust_bill_pay('amount' => $hash->{'amount'});
+
+ if ($error) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+
+ $hash->{'paynum'} = $self->paynum;
+ my $new = new FS::cust_refund ( $hash );
+ $error = $new->insert;
+
+ if ($error) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+
+ $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+ return '';
+}
+
=back
=head1 CLASS METHODS