Fixes for deleting partial payment payments on a single invoice

This commit is contained in:
David Bomba 2023-03-11 12:26:56 +11:00
parent a23c44c0b8
commit eeb87dc5cb
3 changed files with 31 additions and 9 deletions

View File

@ -70,7 +70,6 @@ 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) {
if (array_key_exists('credit_id', $input['credits'][$key])) { if (array_key_exists('credit_id', $input['credits'][$key])) {
// $input['credits'][$key]['credit_id'] = $value['credit_id'];
$input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']); $input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']);
$credits_total += $value['amount']; $credits_total += $value['amount'];

View File

@ -11,6 +11,7 @@
namespace App\Http\ValidationRules; namespace App\Http\ValidationRules;
use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\Validation\Rule; use Illuminate\Contracts\Validation\Rule;
@ -22,6 +23,8 @@ class PaymentAppliedValidAmount implements Rule
{ {
use MakesHash; use MakesHash;
private $message;
/** /**
* @param string $attribute * @param string $attribute
* @param mixed $value * @param mixed $value
@ -29,6 +32,8 @@ class PaymentAppliedValidAmount implements Rule
*/ */
public function passes($attribute, $value) public function passes($attribute, $value)
{ {
$this->message = ctrans('texts.insufficient_applied_amount_remaining');
return $this->calculateAmounts(); return $this->calculateAmounts();
} }
@ -37,12 +42,13 @@ class PaymentAppliedValidAmount implements Rule
*/ */
public function message() public function message()
{ {
return ctrans('texts.insufficient_applied_amount_remaining'); return $this->message;
} }
private function calculateAmounts() :bool private function calculateAmounts() :bool
{ {
$payment = Payment::withTrashed()->whereId($this->decodePrimaryKey(request()->segment(4)))->company()->first(); $payment = Payment::withTrashed()->whereId($this->decodePrimaryKey(request()->segment(4)))->company()->first();
$inv_collection = Invoice::withTrashed()->whereIn('id', array_column(request()->input('invoices'), 'invoice_id'))->get();
if (! $payment) { if (! $payment) {
return false; return false;
@ -71,10 +77,24 @@ class PaymentAppliedValidAmount implements Rule
if (request()->input('invoices') && is_array(request()->input('invoices'))) { if (request()->input('invoices') && is_array(request()->input('invoices'))) {
foreach (request()->input('invoices') as $invoice) { foreach (request()->input('invoices') as $invoice) {
$invoice_amounts += $invoice['amount']; $invoice_amounts += $invoice['amount'];
$inv = $inv_collection->firstWhere('id', $invoice['invoice_id']);
if($inv->balance < $invoice['amount']) {
// $this->message = ctrans('texts.insufficient_applied_amount_remaining');
$this->message = 'Amount cannot be greater than invoice balance';
return false;
}
} }
} }
// nlog("{round($payment_amounts,3)} >= {round($invoice_amounts,3)}"); if(round($payment_amounts, 3) >= round($invoice_amounts, 3)) {
return round($payment_amounts, 3) >= round($invoice_amounts, 3); return true;
}
return false;
} }
} }

View File

@ -74,9 +74,11 @@ class DeletePayment
if ($this->payment->invoices()->exists()) { if ($this->payment->invoices()->exists()) {
$this->payment->invoices()->each(function ($paymentable_invoice) { $this->payment->invoices()->each(function ($paymentable_invoice) {
$net_deletable = $paymentable_invoice->pivot->amount - $paymentable_invoice->pivot->refunded; $net_deletable = $paymentable_invoice->pivot->amount - $paymentable_invoice->pivot->refunded;
$this->_paid_to_date_deleted += $net_deletable; $this->_paid_to_date_deleted += $net_deletable;
$paymentable_invoice = $paymentable_invoice->fresh();
nlog("net deletable amount - refunded = {$net_deletable}"); nlog("net deletable amount - refunded = {$net_deletable}");
@ -92,17 +94,18 @@ class DeletePayment
->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}") ->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}")
->save(); ->save();
$client = $this->payment $this->payment
->client ->client
->service() ->service()
->updateBalanceAndPaidToDate($net_deletable, $net_deletable*-1) ->updateBalanceAndPaidToDate($net_deletable, $net_deletable*-1)
->save(); ->save();
if ($paymentable_invoice->balance == $paymentable_invoice->amount) { if ($paymentable_invoice->balance == $paymentable_invoice->amount) {
$paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save(); $paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save();
} else { } else {
$paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save(); $paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save();
} }
} else { } else {
$paymentable_invoice->restore(); $paymentable_invoice->restore();
$paymentable_invoice->service() $paymentable_invoice->service()