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) {
foreach ($input['credits'] as $key => $value) {
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']);
$credits_total += $value['amount'];

View File

@ -11,6 +11,7 @@
namespace App\Http\ValidationRules;
use App\Models\Invoice;
use App\Models\Payment;
use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\Validation\Rule;
@ -22,6 +23,8 @@ class PaymentAppliedValidAmount implements Rule
{
use MakesHash;
private $message;
/**
* @param string $attribute
* @param mixed $value
@ -29,6 +32,8 @@ class PaymentAppliedValidAmount implements Rule
*/
public function passes($attribute, $value)
{
$this->message = ctrans('texts.insufficient_applied_amount_remaining');
return $this->calculateAmounts();
}
@ -37,12 +42,13 @@ class PaymentAppliedValidAmount implements Rule
*/
public function message()
{
return ctrans('texts.insufficient_applied_amount_remaining');
return $this->message;
}
private function calculateAmounts() :bool
{
$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) {
return false;
@ -71,10 +77,24 @@ class PaymentAppliedValidAmount implements Rule
if (request()->input('invoices') && is_array(request()->input('invoices'))) {
foreach (request()->input('invoices') as $invoice) {
$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)}");
return round($payment_amounts, 3) >= round($invoice_amounts, 3);
if(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()) {
$this->payment->invoices()->each(function ($paymentable_invoice) {
$net_deletable = $paymentable_invoice->pivot->amount - $paymentable_invoice->pivot->refunded;
$this->_paid_to_date_deleted += $net_deletable;
$paymentable_invoice = $paymentable_invoice->fresh();
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}")
->save();
$client = $this->payment
->client
->service()
->updateBalanceAndPaidToDate($net_deletable, $net_deletable*-1)
->save();
$this->payment
->client
->service()
->updateBalanceAndPaidToDate($net_deletable, $net_deletable*-1)
->save();
if ($paymentable_invoice->balance == $paymentable_invoice->amount) {
$paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save();
} else {
$paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save();
}
} else {
$paymentable_invoice->restore();
$paymentable_invoice->service()