1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Refunds implementation v1 (#3270)

* Minor fixes for tests

* Refunds implementation v1
This commit is contained in:
David Bomba 2020-01-30 22:19:51 +11:00 committed by GitHub
parent 63f514f3bc
commit c3da9c80b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 37 deletions

View File

@ -40,7 +40,6 @@ class ValidRefundableRequest implements Rule
$payment = Payment::whereId($this->decodePrimaryKey(request()->input('id')))->first();
if(!$payment)
{
$this->error_msg = "Unable to retrieve specified payment";

View File

@ -13,19 +13,19 @@ use App\Repositories\ActivityRepository;
trait Refundable
{
/**
* Entry point for processing of refunds
*/
public function processRefund(array $data)
{
if(isset($data['invoices']))
return $this->refundPaymentWithInvoicesAndOrCredits($data);
return $this->refundPaymentWithInvoices($data);
if(!isset($data['invoices']) && isset($data['credits']))
return $this->refundPaymentWithCreditsOnly($data);
return $this->refundPaymentWithNoInvoicesOrCredits($data);
return $this->refundPaymentWithNoInvoices($data);
}
private function refundPaymentWithNoInvoicesOrCredits(array $data)
private function refundPaymentWithNoInvoices(array $data)
{
//adjust payment refunded column amount
$this->refunded = $data['amount'];
@ -66,13 +66,8 @@ trait Refundable
return $this;
}
private function refundPaymentWithCreditsOnly($data)
{
}
private function refundPaymentWithInvoicesAndOrCredits($data)
private function refundPaymentWithInvoices($data)
{
$total_refund = 0;
@ -82,11 +77,13 @@ trait Refundable
$data['amount'] = $total_refund;
/* Set Payment Status*/
if($total_refund == $this->amount)
$this->status_id = Payment::STATUS_REFUNDED;
else
$this->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
/* Build Credit Note*/
$credit_note = $this->buildCreditNote($data);
$line_items = [];
@ -121,34 +118,39 @@ trait Refundable
}
}
if(isset($data['credits']))
if($this->credits()->exists())
{
/* Update paymentable record */
//Adjust credits first!!!
foreach($this->credits as $paymentable_credit)
{
$available_credit = $paymentable_credit->pivot->amount - $paymentable_credit->pivot->refunded;
foreach($data['credits'] as $refunded_credit)
{
if($available_credit > $total_refund){
$paymentable_credit->pivot->refunded += $total_refund;
$paymentable_credit->pivot->save();
if($refunded_credit['credit_id'] == $refunded_credit->id)
{
$refunded_credit->pivot->refunded += $refunded_credit['amount'];
$refunded_credit->pivot->save();
$refunded_credit->balance += $refunded_credit['amount'];
$refunded_credit->save();
$paymentable_credit->balance += $total_refund;
$paymentable_credit->save();
}
$total_refund = 0;
}
else {
$paymentable_credit->pivot->refunded += $available_credit;
$paymentable_credit->pivot->save();
$paymentable_credit->balance += $available_credit;
$paymentable_credit->save();
$total_refund -= $available_credit;
}
if($total_refund == 0)
break;
}
}
$credit_note->line_items = $line_items;
@ -160,6 +162,10 @@ trait Refundable
//todo process gateway refund, on success, reduce the credit note balance to 0
}
if($total_refund > 0)
$this->refunded += $total_refund;
$this->save();
$this->adjustInvoices($data);
@ -171,12 +177,6 @@ trait Refundable
return $this;
}
private function refundPaymentWithInvoicesAndCredits($data)
{
return $this;
}
private function createActivity(array $data, int $credit_id)
{

View File

@ -41,7 +41,6 @@
"omnipay/stripe": "^3.0",
"predis/predis": "^1.1",
"sentry/sentry-laravel": "^1.0",
"simshaun/recurr": "^4.0",
"spatie/browsershot": "^3.29",
"staudenmeir/eloquent-has-many-deep": "^1.11",
"stripe/stripe-php": "^7.0",
@ -52,6 +51,7 @@
"yajra/laravel-datatables-oracle": "~9.0"
},
"require-dev": {
"ext-json": "*",
"barryvdh/laravel-debugbar": "^3.2",
"beyondcode/laravel-dump-server": "^1.0",
"darkaonline/l5-swagger": "^6.0",
@ -60,8 +60,7 @@
"laravel/dusk": "^5.0",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.0",
"ext-json": "*"
"phpunit/phpunit": "^7.0"
},
"autoload": {
"classmap": [

View File

@ -3,6 +3,7 @@
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Hash;
trait CreatesApplication
{
@ -17,6 +18,8 @@ trait CreatesApplication
$app->make(Kernel::class)->bootstrap();
Hash::setRounds(4);
return $app;
}
}