Migrate to increment() for client balances
This commit is contained in:
parent
eb1475a451
commit
3441e8fa04
|
|
@ -75,8 +75,7 @@ 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) {
|
||||||
|
|
@ -85,24 +84,17 @@ class PaymentRepository extends BaseRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
$client->service()->updatePaidToDate($data['amount'])->save();
|
$client->service()->updatePaidToDate($data['amount'])->save();
|
||||||
$client->saveQuietly();
|
|
||||||
} else {
|
} else {
|
||||||
//this fixes an edge case with unapplied payments
|
//this fixes an edge case with unapplied payments
|
||||||
$client->service()->updatePaidToDate($data['amount'])->save();
|
$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) {
|
if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) {
|
||||||
$_credit_totals = array_sum(array_column($data['credits'], 'amount'));
|
$_credit_totals = array_sum(array_column($data['credits'], 'amount'));
|
||||||
|
|
||||||
$client->service()->updatePaidToDate($_credit_totals)->save();
|
$client->service()->updatePaidToDate($_credit_totals)->save();
|
||||||
// $client->paid_to_date += $_credit_totals;
|
|
||||||
$client->saveQuietly();
|
|
||||||
}
|
}
|
||||||
}, 1);
|
|
||||||
|
|
||||||
$client = Client::query()->where('id', $data['client_id'])->withTrashed()->first();
|
$client->refresh();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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,46 +124,22 @@ 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();
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
// }
|
||||||
|
|
||||||
return $this;
|
$this->client->increment('paid_to_date', $amount);
|
||||||
}
|
|
||||||
|
|
||||||
public function applyNumber(): self
|
|
||||||
{
|
|
||||||
$x = 1;
|
|
||||||
|
|
||||||
if (isset($this->client->number)) {
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
try {
|
|
||||||
$this->client->number = $this->getNextClientNumber($this->client);
|
|
||||||
$this->client->saveQuietly();
|
|
||||||
|
|
||||||
$this->completed = false;
|
|
||||||
} catch (QueryException $e) {
|
|
||||||
$x++;
|
|
||||||
|
|
||||||
if ($x > 50) {
|
|
||||||
$this->completed = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while ($this->completed);
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
@ -237,7 +165,36 @@ try {
|
||||||
|
|
||||||
public function adjustCreditBalance(float $amount)
|
public function adjustCreditBalance(float $amount)
|
||||||
{
|
{
|
||||||
$this->client->credit_balance += $amount;
|
// $this->client->credit_balance += $amount;
|
||||||
|
|
||||||
|
$this->client->increment('credit_balance', $amount);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function applyNumber(): self
|
||||||
|
{
|
||||||
|
$x = 1;
|
||||||
|
|
||||||
|
if (isset($this->client->number)) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
try {
|
||||||
|
$this->client->number = $this->getNextClientNumber($this->client);
|
||||||
|
$this->client->saveQuietly();
|
||||||
|
|
||||||
|
$this->completed = false;
|
||||||
|
} catch (QueryException $e) {
|
||||||
|
$x++;
|
||||||
|
|
||||||
|
if ($x > 50) {
|
||||||
|
$this->completed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while ($this->completed);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue