diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index f31e1cf8d3ff..d796a082454d 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -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']; diff --git a/app/Http/ValidationRules/PaymentAppliedValidAmount.php b/app/Http/ValidationRules/PaymentAppliedValidAmount.php index 332bcc97e210..dcb0c6d11b26 100644 --- a/app/Http/ValidationRules/PaymentAppliedValidAmount.php +++ b/app/Http/ValidationRules/PaymentAppliedValidAmount.php @@ -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; } } diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index b327287fcb60..eec109408172 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -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()