diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index 0be53a30c2f4..3cfeac9aee40 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -44,7 +44,7 @@ class StorePaymentRequest extends Request if (isset($input['invoices']) && is_array($input['invoices']) !== false) { 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) { foreach ($input['credits'] as $key => $value) { - $input['credits'][$key]['id'] = $this->decodePrimaryKey($value['id']); + $input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']); } } diff --git a/app/Http/Requests/Payment/UpdatePaymentRequest.php b/app/Http/Requests/Payment/UpdatePaymentRequest.php index 22b78e0f0f69..0c716b90a85c 100644 --- a/app/Http/Requests/Payment/UpdatePaymentRequest.php +++ b/app/Http/Requests/Payment/UpdatePaymentRequest.php @@ -66,7 +66,7 @@ class UpdatePaymentRequest extends Request if (isset($input['invoices']) && is_array($input['invoices']) !== false) { 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); diff --git a/app/Http/ValidationRules/PaymentAmountsBalanceRule.php b/app/Http/ValidationRules/PaymentAmountsBalanceRule.php index e437c9a044ea..13e6c57ecd1b 100644 --- a/app/Http/ValidationRules/PaymentAmountsBalanceRule.php +++ b/app/Http/ValidationRules/PaymentAmountsBalanceRule.php @@ -42,7 +42,7 @@ class PaymentAmountsBalanceRule implements Rule private function calculateAmounts() :bool { - $data = []; + $payment_amounts = 0; $invoice_amounts = 0; diff --git a/app/Http/ValidationRules/PaymentAppliedValidAmount.php b/app/Http/ValidationRules/PaymentAppliedValidAmount.php index 3711f0832274..7b43ed1cbe79 100644 --- a/app/Http/ValidationRules/PaymentAppliedValidAmount.php +++ b/app/Http/ValidationRules/PaymentAppliedValidAmount.php @@ -51,7 +51,6 @@ class PaymentAppliedValidAmount implements Rule if(!$payment) return false; - $data = []; $payment_amounts = 0; $invoice_amounts = 0; diff --git a/app/Http/ValidationRules/ValidCreditsPresentRule.php b/app/Http/ValidationRules/ValidCreditsPresentRule.php index 1471cb704338..31bfc5d5c72b 100644 --- a/app/Http/ValidationRules/ValidCreditsPresentRule.php +++ b/app/Http/ValidationRules/ValidCreditsPresentRule.php @@ -45,14 +45,13 @@ class ValidCreditsPresentRule implements Rule private function validCreditsPresent() :bool { - $data = []; //todo need to ensure the clients credits are here not random ones! if(request()->input('credits') && is_array(request()->input('credits'))) { foreach(request()->input('credits') as $credit) { - $cred = Credit::find($credit['id']); + $cred = Credit::find($credit['invoice_id']); if($cred->balance >= $credit['amount']) return false; diff --git a/app/Http/ValidationRules/ValidPayableInvoicesRule.php b/app/Http/ValidationRules/ValidPayableInvoicesRule.php index 52f0940fa71b..80fe1f7a9473 100644 --- a/app/Http/ValidationRules/ValidPayableInvoicesRule.php +++ b/app/Http/ValidationRules/ValidPayableInvoicesRule.php @@ -37,7 +37,7 @@ class ValidPayableInvoicesRule implements Rule $invoices = []; 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) { diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 00347159ed74..e37699eddcb5 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -32,12 +32,9 @@ class PaymentRepository extends BaseRepository protected $credit_repo; - public function __construct(CreditRepository $credit_repo) { - $this->credit_repo = $credit_repo; - } public function getClassName() @@ -56,7 +53,22 @@ class PaymentRepository extends BaseRepository 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->status_id = Payment::STATUS_COMPLETED; @@ -75,12 +87,12 @@ class PaymentRepository extends BaseRepository $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); 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) { 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')); - $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); 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) ApplyCreditPayment::dispatchNow($paid_credit, $payment, $paid_credit['amount'], $credit->company); @@ -123,45 +135,6 @@ class PaymentRepository extends BaseRepository $payment->save(); 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) { - $invoice = Invoice::whereId($adjusted_invoice['id'])->company()->first(); + $invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->company()->first(); $invoice_total_adjustment += $adjusted_invoice['amount']; diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index df35009d21ff..cd6bc667bfd3 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -132,7 +132,7 @@ class PaymentTest extends TestCase 'amount' => $this->invoice->amount, 'invoices' => [ [ - 'id' => $this->invoice->hashed_id, + 'invoice_id' => $this->invoice->hashed_id, 'amount' => $this->invoice->amount ], ], @@ -182,7 +182,7 @@ class PaymentTest extends TestCase 'client_id' => $client->hashed_id, 'invoices' => [ [ - 'id' => $this->invoice->hashed_id, + 'invoice_id' => $this->invoice->hashed_id, 'amount' => $this->invoice->amount ], ], @@ -299,7 +299,7 @@ class PaymentTest extends TestCase 'client_id' => $client->hashed_id, 'invoices' => [ [ - 'id' => $this->invoice->hashed_id, + 'invoice_id' => $this->invoice->hashed_id, 'amount' => 2.0 ], ], @@ -377,7 +377,7 @@ class PaymentTest extends TestCase 'client_id' => $client->hashed_id, 'invoices' => [ [ - 'id' => $this->invoice->hashed_id, + 'invoice_id' => $this->invoice->hashed_id, 'amount' => 6.0 ], ], @@ -438,7 +438,7 @@ class PaymentTest extends TestCase 'client_id' => $client->hashed_id, 'invoices' => [ [ - 'id' => $this->invoice->hashed_id, + 'invoice_id' => $this->invoice->hashed_id, 'amount' => 2.0 ], ], @@ -499,7 +499,7 @@ class PaymentTest extends TestCase 'client_id' => $client->hashed_id, 'invoices' => [ [ - 'id' => $this->invoice->hashed_id, + 'invoice_id' => $this->invoice->hashed_id, 'amount' => 2.0 ], ], @@ -556,7 +556,7 @@ class PaymentTest extends TestCase 'client_id' => $client->hashed_id, 'invoices' => [ [ - 'id' => $this->invoice->hashed_id, + 'invoice_id' => $this->invoice->hashed_id, 'amount' => 2.0 ], ], @@ -690,7 +690,7 @@ class PaymentTest extends TestCase 'client_id' => $this->encodePrimaryKey($client->id), 'invoices' => [ [ - 'id' => $this->encodePrimaryKey($this->invoice->id), + 'invoice_id' => $this->encodePrimaryKey($this->invoice->id), 'amount' => 10, ] ], @@ -748,7 +748,7 @@ class PaymentTest extends TestCase 'client_id' => $this->encodePrimaryKey($client->id), 'invoices' => [ [ - 'id' => $this->encodePrimaryKey($this->invoice->id), + 'invoice_id' => $this->encodePrimaryKey($this->invoice->id), 'amount' => 10, ] ], @@ -802,7 +802,7 @@ class PaymentTest extends TestCase 'client_id' => $this->encodePrimaryKey($client->id), 'invoices' => [ [ - 'id' => $this->encodePrimaryKey($this->invoice->id), + 'invoice_id' => $this->encodePrimaryKey($this->invoice->id), 'amount' => 10, ] ], @@ -861,7 +861,7 @@ class PaymentTest extends TestCase 'client_id' => $this->encodePrimaryKey($client->id), 'invoices' => [ [ - 'id' => $this->encodePrimaryKey($this->invoice->id), + 'invoice_id' => $this->encodePrimaryKey($this->invoice->id), 'amount' => 10, ] ], @@ -906,7 +906,7 @@ class PaymentTest extends TestCase 'client_id' => $this->encodePrimaryKey($client->id), 'invoices' => [ [ - 'id' => $this->encodePrimaryKey($this->invoice->id), + 'invoice_id' => $this->encodePrimaryKey($this->invoice->id), 'amount' => 10, ] ],