mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 13:54:34 -04:00
Fixes for payments affecting the client ledger balance
This commit is contained in:
parent
b6c0300775
commit
8149251180
@ -251,12 +251,12 @@ class Client extends BaseModel implements HasLocalePreference
|
|||||||
* @param float $amount Adjustment amount
|
* @param float $amount Adjustment amount
|
||||||
* @return Client
|
* @return Client
|
||||||
*/
|
*/
|
||||||
public function processUnappliedPayment($amount) :Client
|
// public function processUnappliedPayment($amount) :Client
|
||||||
{
|
// {
|
||||||
return $this->service()->updatePaidToDate($amount)
|
// return $this->service()->updatePaidToDate($amount)
|
||||||
->adjustCreditBalance($amount)
|
// ->adjustCreditBalance($amount)
|
||||||
->save();
|
// ->save();
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -46,7 +46,6 @@ class PaymentRepository extends BaseRepository
|
|||||||
/**
|
/**
|
||||||
* Saves and updates a payment. //todo refactor to handle refunds and payments.
|
* Saves and updates a payment. //todo refactor to handle refunds and payments.
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @param array $data the request object
|
* @param array $data the request object
|
||||||
* @param Payment $payment The Payment object
|
* @param Payment $payment The Payment object
|
||||||
* @return Payment|null Payment $payment
|
* @return Payment|null Payment $payment
|
||||||
@ -62,8 +61,8 @@ class PaymentRepository extends BaseRepository
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles a positive payment request
|
* Handles a positive payment request
|
||||||
* @param array $data The data object
|
* @param array $data The data object
|
||||||
* @param Payment $payment The $payment entity
|
* @param Payment $payment The $payment entity
|
||||||
* @return Payment The updated/created payment object
|
* @return Payment The updated/created payment object
|
||||||
*/
|
*/
|
||||||
private function applyPayment(array $data, Payment $payment): ?Payment
|
private function applyPayment(array $data, Payment $payment): ?Payment
|
||||||
@ -73,21 +72,20 @@ class PaymentRepository extends BaseRepository
|
|||||||
$this->processExchangeRates($data, $payment);
|
$this->processExchangeRates($data, $payment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Fill the payment*/
|
||||||
$payment->fill($data);
|
$payment->fill($data);
|
||||||
|
|
||||||
$payment->status_id = Payment::STATUS_COMPLETED;
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
||||||
|
|
||||||
$payment->save();
|
$payment->save();
|
||||||
|
|
||||||
|
/*Ensure payment number generated*/
|
||||||
if (!$payment->number || strlen($payment->number) == 0) {
|
if (!$payment->number || strlen($payment->number) == 0) {
|
||||||
$payment->number = $payment->client->getNextPaymentNumber($payment->client);
|
$payment->number = $payment->client->getNextPaymentNumber($payment->client);
|
||||||
}
|
}
|
||||||
|
|
||||||
$payment->client->service()->updatePaidToDate($payment->amount)->save();
|
|
||||||
|
|
||||||
$invoice_totals = 0;
|
$invoice_totals = 0;
|
||||||
$credit_totals = 0;
|
$credit_totals = 0;
|
||||||
|
|
||||||
|
/*Iterate through invoices and apply payments*/
|
||||||
if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
|
if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
|
||||||
$invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
|
$invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
|
||||||
|
|
||||||
@ -103,15 +101,14 @@ class PaymentRepository extends BaseRepository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//payment is made, but not to any invoice, therefore we are applying the payment to the clients credit
|
//payment is made, but not to any invoice, therefore we are applying the payment to the clients paid_to_date only
|
||||||
$payment->client->processUnappliedPayment($payment->amount);
|
$payment->client->service()->updatePaidToDate($payment->amount)->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array_key_exists('credits', $data) && is_array($data['credits'])) {
|
if (array_key_exists('credits', $data) && is_array($data['credits'])) {
|
||||||
$credit_totals = array_sum(array_column($data['credits'], 'amount'));
|
$credit_totals = array_sum(array_column($data['credits'], 'amount'));
|
||||||
|
|
||||||
$credits = Credit::whereIn('id', $this->transformKeys(array_column($data['credits'], 'credit_id')))->get();
|
$credits = Credit::whereIn('id', $this->transformKeys(array_column($data['credits'], 'credit_id')))->get();
|
||||||
|
|
||||||
$payment->credits()->saveMany($credits);
|
$payment->credits()->saveMany($credits);
|
||||||
|
|
||||||
foreach ($data['credits'] as $paid_credit) {
|
foreach ($data['credits'] as $paid_credit) {
|
||||||
@ -136,7 +133,6 @@ class PaymentRepository extends BaseRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
$payment->save();
|
$payment->save();
|
||||||
|
|
||||||
return $payment->fresh();
|
return $payment->fresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user