Migrate to increment() for client balances

This commit is contained in:
David Bomba 2024-11-25 20:17:43 +11:00
parent eb1475a451
commit 3441e8fa04
2 changed files with 75 additions and 127 deletions

View File

@ -75,34 +75,26 @@ class PaymentRepository extends BaseRepository
$is_existing_payment = false; $is_existing_payment = false;
\DB::connection(config('database.default'))->transaction(function () use ($data) { $client = Client::query()->where('id', $data['client_id'])->withTrashed()->first();
$client = Client::query()->where('id', $data['client_id'])->withTrashed()->lockForUpdate()->first();
/*We only update the paid to date ONCE per payment*/ /*We only update the paid to date ONCE per payment*/
if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) { if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {
if ($data['amount'] == '') { if ($data['amount'] == '') {
$data['amount'] = array_sum(array_column($data['invoices'], 'amount')); $data['amount'] = array_sum(array_column($data['invoices'], 'amount'));
}
$client->service()->updatePaidToDate($data['amount'])->save();
$client->saveQuietly();
} else {
//this fixes an edge case with unapplied payments
$client->service()->updatePaidToDate($data['amount'])->save();
// $client->paid_to_date += $data['amount'];
$client->saveQuietly();
} }
if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) { $client->service()->updatePaidToDate($data['amount'])->save();
$_credit_totals = array_sum(array_column($data['credits'], 'amount')); } else {
//this fixes an edge case with unapplied payments
$client->service()->updatePaidToDate($data['amount'])->save();
}
$client->service()->updatePaidToDate($_credit_totals)->save(); if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) {
// $client->paid_to_date += $_credit_totals; $_credit_totals = array_sum(array_column($data['credits'], 'amount'));
$client->saveQuietly(); $client->service()->updatePaidToDate($_credit_totals)->save();
} }
}, 1);
$client->refresh();
$client = Client::query()->where('id', $data['client_id'])->withTrashed()->first();
} }
@ -153,7 +145,6 @@ class PaymentRepository extends BaseRepository
if ($invoice) { if ($invoice) {
//25-06-2023
$paymentable = new Paymentable(); $paymentable = new Paymentable();
$paymentable->payment_id = $payment->id; $paymentable->payment_id = $payment->id;
$paymentable->paymentable_id = $invoice->id; $paymentable->paymentable_id = $invoice->id;

View File

@ -77,19 +77,21 @@ class ClientService
*/ */
public function updateBalance(float $amount) public function updateBalance(float $amount)
{ {
try { // try {
DB::connection(config('database.default'))->transaction(function () use ($amount) { // DB::connection(config('database.default'))->transaction(function () use ($amount) {
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); // $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
$this->client->balance += $amount; // $this->client->balance += $amount;
$this->client->saveQuietly(); // $this->client->saveQuietly();
}, 2); // }, 2);
} catch (\Throwable $throwable) { // } catch (\Throwable $throwable) {
if (DB::connection(config('database.default'))->transactionLevel() > 0) { // if (DB::connection(config('database.default'))->transactionLevel() > 0) {
DB::connection(config('database.default'))->rollBack(); // DB::connection(config('database.default'))->rollBack();
} // }
} // }
$this->client->increment('balance', $amount);
return $this; return $this;
} }
@ -97,60 +99,10 @@ class ClientService
public function updateBalanceAndPaidToDate(float $balance, float $paid_to_date) public function updateBalanceAndPaidToDate(float $balance, float $paid_to_date)
{ {
// refactor to calculated balances. $this->client->increment('balance', $balance);
// $ib = Invoice::withTrashed() $this->client->increment('paid_to_date', $paid_to_date);
// ->where('client_id', $this->client->id)
// ->whereIn('status_id', [2,3])
// ->where('is_deleted',0)
// ->where('is_proforma',0)
// ->sum('balance');
// $payments = Payment::withTrashed() /*
// ->where('client_id', $this->client->id)
// ->whereIn('status_id', [1,4,5,6])
// ->where('is_deleted',0)
// ->sum(\DB::raw('amount - refunded'));
// $credit_payments = Payment::where('client_id', $this->client->id)
// ->where('is_deleted', 0)
// ->join('paymentables', 'payments.id', '=', 'paymentables.payment_id')
// ->where('paymentables.paymentable_type', \App\Models\Credit::class)
// ->whereNull('paymentables.deleted_at')
// ->where('paymentables.amount', '>', 0)
// ->sum(DB::raw('paymentables.amount - paymentables.refunded'));
// $credits_from_reversal = \App\Models\Credit::withTrashed()
// ->where('client_id', $this->client->id)
// ->where('is_deleted', 0)
// ->whereNotNull('invoice_id')
// ->sum('amount');
// $paid_to_date = $payments+$credit_payments-$credits_from_reversal;
try {
DB::connection(config('database.default'))->transaction(function () use ($balance, $paid_to_date) {
Client::withTrashed()
->where('id', $this->client->id)
->lockForUpdate()
->update([
'balance' => DB::raw("balance + {$balance}"),
'paid_to_date' => DB::raw("paid_to_date + {$paid_to_date}")
]);
}, 2);
} catch (\Throwable $throwable) {
nlog("DB ERROR " . $throwable->getMessage());
if (DB::connection(config('database.default'))->transactionLevel() > 0) {
DB::connection(config('database.default'))->rollBack();
}
}
/* switch to increment/decrement
try { try {
DB::connection(config('database.default'))->transaction(function () use ($balance, $paid_to_date) { DB::connection(config('database.default'))->transaction(function () use ($balance, $paid_to_date) {
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
@ -172,23 +124,54 @@ try {
public function updatePaidToDate(float $amount) public function updatePaidToDate(float $amount)
{ {
try { // try {
DB::connection(config('database.default'))->transaction(function () use ($amount) { // DB::connection(config('database.default'))->transaction(function () use ($amount) {
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); // $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
$this->client->paid_to_date += $amount; // $this->client->paid_to_date += $amount;
$this->client->saveQuietly(); // $this->client->saveQuietly();
}, 2); // }, 2);
} catch (\Throwable $throwable) { // } catch (\Throwable $throwable) {
nlog("DB ERROR " . $throwable->getMessage()); // nlog("DB ERROR " . $throwable->getMessage());
if (DB::connection(config('database.default'))->transactionLevel() > 0) { // if (DB::connection(config('database.default'))->transactionLevel() > 0) {
DB::connection(config('database.default'))->rollBack(); // DB::connection(config('database.default'))->rollBack();
} // }
} // }
$this->client->increment('paid_to_date', $amount);
return $this; return $this;
} }
public function updatePaymentBalance()
{
$amount = Payment::query()
->withTrashed()
->where('client_id', $this->client->id)
->where('is_deleted', 0)
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
->selectRaw('SUM(payments.amount - payments.applied) as amount')->first()->amount ?? 0;
DB::connection(config('database.default'))->transaction(function () use ($amount) {
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
$this->client->payment_balance = $amount;
$this->client->saveQuietly();
}, 2);
return $this;
}
public function adjustCreditBalance(float $amount)
{
// $this->client->credit_balance += $amount;
$this->client->increment('credit_balance', $amount);
return $this;
}
public function applyNumber(): self public function applyNumber(): self
{ {
@ -216,32 +199,6 @@ try {
return $this; return $this;
} }
public function updatePaymentBalance()
{
$amount = Payment::query()
->withTrashed()
->where('client_id', $this->client->id)
->where('is_deleted', 0)
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
->selectRaw('SUM(payments.amount - payments.applied) as amount')->first()->amount ?? 0;
DB::connection(config('database.default'))->transaction(function () use ($amount) {
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
$this->client->payment_balance = $amount;
$this->client->saveQuietly();
}, 2);
return $this;
}
public function adjustCreditBalance(float $amount)
{
$this->client->credit_balance += $amount;
return $this;
}
public function getCreditBalance(): float public function getCreditBalance(): float
{ {
$credits = Credit::withTrashed()->where('client_id', $this->client->id) $credits = Credit::withTrashed()->where('client_id', $this->client->id)