Payment fixes (#3204)

* Fixes for unused variables and annotating the paymentrepository for future work

* Refactor Payment repository

* rename variables for payments
This commit is contained in:
David Bomba 2020-01-12 08:01:28 +10:00 committed by GitHub
parent 0878decf18
commit 2c99d3478b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 39 additions and 68 deletions

View File

@ -44,7 +44,7 @@ class StorePaymentRequest extends Request
if (isset($input['invoices']) && is_array($input['invoices']) !== false) { if (isset($input['invoices']) && is_array($input['invoices']) !== false) {
foreach ($input['invoices'] as $key => $value) { foreach ($input['invoices'] as $key => $value) {
$input['invoices'][$key]['id'] = $this->decodePrimaryKey($value['id']); $input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']);
} }
} }
@ -54,7 +54,7 @@ class StorePaymentRequest extends Request
if (isset($input['credits']) && is_array($input['credits']) !== false) { if (isset($input['credits']) && is_array($input['credits']) !== false) {
foreach ($input['credits'] as $key => $value) { foreach ($input['credits'] as $key => $value) {
$input['credits'][$key]['id'] = $this->decodePrimaryKey($value['id']); $input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']);
} }
} }

View File

@ -66,7 +66,7 @@ class UpdatePaymentRequest extends Request
if (isset($input['invoices']) && is_array($input['invoices']) !== false) { if (isset($input['invoices']) && is_array($input['invoices']) !== false) {
foreach ($input['invoices'] as $key => $value) { foreach ($input['invoices'] as $key => $value) {
$input['invoices'][$key]['id'] = $this->decodePrimaryKey($value['id']); $input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']);
} }
} }
$this->replace($input); $this->replace($input);

View File

@ -42,7 +42,7 @@ class PaymentAmountsBalanceRule implements Rule
private function calculateAmounts() :bool private function calculateAmounts() :bool
{ {
$data = [];
$payment_amounts = 0; $payment_amounts = 0;
$invoice_amounts = 0; $invoice_amounts = 0;

View File

@ -51,7 +51,6 @@ class PaymentAppliedValidAmount implements Rule
if(!$payment) if(!$payment)
return false; return false;
$data = [];
$payment_amounts = 0; $payment_amounts = 0;
$invoice_amounts = 0; $invoice_amounts = 0;

View File

@ -45,14 +45,13 @@ class ValidCreditsPresentRule implements Rule
private function validCreditsPresent() :bool private function validCreditsPresent() :bool
{ {
$data = [];
//todo need to ensure the clients credits are here not random ones! //todo need to ensure the clients credits are here not random ones!
if(request()->input('credits') && is_array(request()->input('credits'))) if(request()->input('credits') && is_array(request()->input('credits')))
{ {
foreach(request()->input('credits') as $credit) foreach(request()->input('credits') as $credit)
{ {
$cred = Credit::find($credit['id']); $cred = Credit::find($credit['invoice_id']);
if($cred->balance >= $credit['amount']) if($cred->balance >= $credit['amount'])
return false; return false;

View File

@ -37,7 +37,7 @@ class ValidPayableInvoicesRule implements Rule
$invoices = []; $invoices = [];
if (is_array($value)) { if (is_array($value)) {
$invoices = Invoice::whereIn('id', array_column($value, 'id'))->company()->get(); $invoices = Invoice::whereIn('id', array_column($value, 'invoice_id'))->company()->get();
} }
foreach ($invoices as $invoice) { foreach ($invoices as $invoice) {

View File

@ -32,12 +32,9 @@ class PaymentRepository extends BaseRepository
protected $credit_repo; protected $credit_repo;
public function __construct(CreditRepository $credit_repo) public function __construct(CreditRepository $credit_repo)
{ {
$this->credit_repo = $credit_repo; $this->credit_repo = $credit_repo;
} }
public function getClassName() public function getClassName()
@ -56,7 +53,22 @@ class PaymentRepository extends BaseRepository
public function save(Request $request, Payment $payment) : ?Payment public function save(Request $request, Payment $payment) : ?Payment
{ {
//todo this save() only works for new payments... will fail if a Payment is updated and saved through here. if($payment->amount >= 0)
return $this->applyPayment($request, $payment);
return $this->refundPayment($request, $payment);
}
/**
* Handles a positive payment request
* @param Request $request The request object
* @param Payment $payment The $payment entity
* @return Payment The updated/created payment object
*/
private function applyPayment(Request $request, Payment $payment) :?Payment
{
$payment->fill($request->all()); $payment->fill($request->all());
$payment->status_id = Payment::STATUS_COMPLETED; $payment->status_id = Payment::STATUS_COMPLETED;
@ -75,12 +87,12 @@ class PaymentRepository extends BaseRepository
$invoice_totals = array_sum(array_column($request->input('invoices'),'amount')); $invoice_totals = array_sum(array_column($request->input('invoices'),'amount'));
$invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'id'))->company()->get(); $invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'invoice_id'))->company()->get();
$payment->invoices()->saveMany($invoices); $payment->invoices()->saveMany($invoices);
foreach ($request->input('invoices') as $paid_invoice) { foreach ($request->input('invoices') as $paid_invoice) {
$invoice = Invoice::whereId($paid_invoice['id'])->company()->first(); $invoice = Invoice::whereId($paid_invoice['invoice_id'])->company()->first();
if ($invoice) { if ($invoice) {
ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company); ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company);
@ -96,13 +108,13 @@ class PaymentRepository extends BaseRepository
$credit_totals = array_sum(array_column($request->input('credits'),'amount')); $credit_totals = array_sum(array_column($request->input('credits'),'amount'));
$credits = Credit::whereIn('id', array_column($request->input('credits'), 'id'))->company()->get(); $credits = Credit::whereIn('id', array_column($request->input('credits'), 'credit_id'))->company()->get();
$payment->credits()->saveMany($credits); $payment->credits()->saveMany($credits);
foreach ($request->input('credits') as $paid_credit) foreach ($request->input('credits') as $paid_credit)
{ {
$credit = Credit::whereId($paid_credit['id'])->company()->first(); $credit = Credit::whereId($paid_credit['credit_id'])->company()->first();
if($credit) if($credit)
ApplyCreditPayment::dispatchNow($paid_credit, $payment, $paid_credit['amount'], $credit->company); ApplyCreditPayment::dispatchNow($paid_credit, $payment, $paid_credit['amount'], $credit->company);
@ -123,45 +135,6 @@ class PaymentRepository extends BaseRepository
$payment->save(); $payment->save();
return $payment->fresh(); return $payment->fresh();
}
/**
* Updates
*
* The update code path is different to the save path
* additional considerations come into play when 'updating'
* a payment.
*
* @param Request $request the request object
* @param Payment $payment The Payment object
* @return Object Payment $payment
*/
public function update(Request $request, Payment $payment) :?Payment
{
if($payment->amount >= 0)
return $this->applyPayment($request, $payment);
return $this->refundPayment($request, $payment);
}
private function applyPayment(Request $request, Payment $payment) :?Payment
{
$invoice_totals = array_sum(array_column($request->input('invoices'),'amount'));
$invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'id'))->company()->get();
$payment->invoices()->saveMany($invoices);
foreach ($request->input('invoices') as $paid_invoice) {
$invoice = Invoice::whereId($paid_invoice['id'])->company()->first();
if ($invoice) {
ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company);
}
}
} }
@ -174,7 +147,7 @@ class PaymentRepository extends BaseRepository
foreach($request->input('invoices') as $adjusted_invoice) { foreach($request->input('invoices') as $adjusted_invoice) {
$invoice = Invoice::whereId($adjusted_invoice['id'])->company()->first(); $invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->company()->first();
$invoice_total_adjustment += $adjusted_invoice['amount']; $invoice_total_adjustment += $adjusted_invoice['amount'];

View File

@ -132,7 +132,7 @@ class PaymentTest extends TestCase
'amount' => $this->invoice->amount, 'amount' => $this->invoice->amount,
'invoices' => [ 'invoices' => [
[ [
'id' => $this->invoice->hashed_id, 'invoice_id' => $this->invoice->hashed_id,
'amount' => $this->invoice->amount 'amount' => $this->invoice->amount
], ],
], ],
@ -182,7 +182,7 @@ class PaymentTest extends TestCase
'client_id' => $client->hashed_id, 'client_id' => $client->hashed_id,
'invoices' => [ 'invoices' => [
[ [
'id' => $this->invoice->hashed_id, 'invoice_id' => $this->invoice->hashed_id,
'amount' => $this->invoice->amount 'amount' => $this->invoice->amount
], ],
], ],
@ -299,7 +299,7 @@ class PaymentTest extends TestCase
'client_id' => $client->hashed_id, 'client_id' => $client->hashed_id,
'invoices' => [ 'invoices' => [
[ [
'id' => $this->invoice->hashed_id, 'invoice_id' => $this->invoice->hashed_id,
'amount' => 2.0 'amount' => 2.0
], ],
], ],
@ -377,7 +377,7 @@ class PaymentTest extends TestCase
'client_id' => $client->hashed_id, 'client_id' => $client->hashed_id,
'invoices' => [ 'invoices' => [
[ [
'id' => $this->invoice->hashed_id, 'invoice_id' => $this->invoice->hashed_id,
'amount' => 6.0 'amount' => 6.0
], ],
], ],
@ -438,7 +438,7 @@ class PaymentTest extends TestCase
'client_id' => $client->hashed_id, 'client_id' => $client->hashed_id,
'invoices' => [ 'invoices' => [
[ [
'id' => $this->invoice->hashed_id, 'invoice_id' => $this->invoice->hashed_id,
'amount' => 2.0 'amount' => 2.0
], ],
], ],
@ -499,7 +499,7 @@ class PaymentTest extends TestCase
'client_id' => $client->hashed_id, 'client_id' => $client->hashed_id,
'invoices' => [ 'invoices' => [
[ [
'id' => $this->invoice->hashed_id, 'invoice_id' => $this->invoice->hashed_id,
'amount' => 2.0 'amount' => 2.0
], ],
], ],
@ -556,7 +556,7 @@ class PaymentTest extends TestCase
'client_id' => $client->hashed_id, 'client_id' => $client->hashed_id,
'invoices' => [ 'invoices' => [
[ [
'id' => $this->invoice->hashed_id, 'invoice_id' => $this->invoice->hashed_id,
'amount' => 2.0 'amount' => 2.0
], ],
], ],
@ -690,7 +690,7 @@ class PaymentTest extends TestCase
'client_id' => $this->encodePrimaryKey($client->id), 'client_id' => $this->encodePrimaryKey($client->id),
'invoices' => [ 'invoices' => [
[ [
'id' => $this->encodePrimaryKey($this->invoice->id), 'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
'amount' => 10, 'amount' => 10,
] ]
], ],
@ -748,7 +748,7 @@ class PaymentTest extends TestCase
'client_id' => $this->encodePrimaryKey($client->id), 'client_id' => $this->encodePrimaryKey($client->id),
'invoices' => [ 'invoices' => [
[ [
'id' => $this->encodePrimaryKey($this->invoice->id), 'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
'amount' => 10, 'amount' => 10,
] ]
], ],
@ -802,7 +802,7 @@ class PaymentTest extends TestCase
'client_id' => $this->encodePrimaryKey($client->id), 'client_id' => $this->encodePrimaryKey($client->id),
'invoices' => [ 'invoices' => [
[ [
'id' => $this->encodePrimaryKey($this->invoice->id), 'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
'amount' => 10, 'amount' => 10,
] ]
], ],
@ -861,7 +861,7 @@ class PaymentTest extends TestCase
'client_id' => $this->encodePrimaryKey($client->id), 'client_id' => $this->encodePrimaryKey($client->id),
'invoices' => [ 'invoices' => [
[ [
'id' => $this->encodePrimaryKey($this->invoice->id), 'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
'amount' => 10, 'amount' => 10,
] ]
], ],
@ -906,7 +906,7 @@ class PaymentTest extends TestCase
'client_id' => $this->encodePrimaryKey($client->id), 'client_id' => $this->encodePrimaryKey($client->id),
'invoices' => [ 'invoices' => [
[ [
'id' => $this->encodePrimaryKey($this->invoice->id), 'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
'amount' => 10, 'amount' => 10,
] ]
], ],