diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index b681a12bce..4374fb0d91 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -333,7 +333,7 @@ class CheckData extends Command $credit_total_applied += $payment->paymentables->where('paymentable_type', App\Models\Credit::class)->sum(\DB::raw('amount')); } - $total_invoice_payments += $credit_total_applied; + $total_invoice_payments += $credit_total_applied; //todo this is contentious info("total invoice payments = {$total_invoice_payments} with client paid to date of of {$client->paid_to_date}"); diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index bfff80355b..67c50a24e6 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -77,7 +77,7 @@ class StorePaymentRequest extends Request if (! isset($input['amount']) || $input['amount'] == 0) { //$input['amount'] = $invoices_total - $credits_total; - $input['amount'] = $invoices_total; + $input['amount'] = $invoices_total - $credits_total; //todo the payment amount is always less the credit amount applied } $input['is_manual'] = true; @@ -94,17 +94,15 @@ class StorePaymentRequest extends Request $rules = [ 'amount' => 'numeric|required', 'amount' => [new PaymentAmountsBalanceRule(), new ValidCreditsPresentRule()], - //'date' => 'required', 'client_id' => 'bail|required|exists:clients,id', - 'invoices.*.invoice_id' => 'required|distinct|exists:invoices,id', + 'invoices.*.invoice_id' => 'bail|required|distinct|exists:invoices,id', 'invoices.*.invoice_id' => new ValidInvoicesRules($this->all()), 'invoices.*.amount' => 'required', - 'credits.*.credit_id' => 'required|exists:credits,id', + 'credits.*.credit_id' => 'bail|required|exists:credits,id', 'credits.*.credit_id' => new ValidCreditsRules($this->all()), 'credits.*.amount' => 'required', 'invoices' => new ValidPayableInvoicesRule(), - 'number' => 'nullable|unique:payments,number,'.$this->id.',id,company_id,'.$this->company_id, - //'number' => 'nullable', + 'number' => 'bail|nullable|unique:payments,number,'.$this->id.',id,company_id,'.$this->company_id, ]; if ($this->input('documents') && is_array($this->input('documents'))) { diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index b3b0efb61b..c9cca86dfe 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -88,6 +88,14 @@ class PaymentRepository extends BaseRepository $client->service()->updatePaidToDate($data['amount'])->save(); } +//todo + if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) { + if ($data['amount'] == '') { + $data['amount'] += array_sum(array_column($data['credits'], 'amount')); + } + + } + } /*Fill the payment*/ @@ -166,7 +174,7 @@ class PaymentRepository extends BaseRepository // $payment->applied += $invoice_totals; // } - $payment->applied += $invoice_totals; //wont work because - check tests + $payment->applied += ($invoice_totals - $credit_totals); //wont work because - check tests $payment->save();