Migrate to increment() for client balances
This commit is contained in:
parent
eb1475a451
commit
3441e8fa04
|
|
@ -75,34 +75,26 @@ class PaymentRepository extends BaseRepository
|
|||
|
||||
$is_existing_payment = false;
|
||||
|
||||
\DB::connection(config('database.default'))->transaction(function () use ($data) {
|
||||
$client = Client::query()->where('id', $data['client_id'])->withTrashed()->lockForUpdate()->first();
|
||||
$client = Client::query()->where('id', $data['client_id'])->withTrashed()->first();
|
||||
|
||||
/*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 ($data['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();
|
||||
/*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 ($data['amount'] == '') {
|
||||
$data['amount'] = array_sum(array_column($data['invoices'], 'amount'));
|
||||
}
|
||||
|
||||
if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) {
|
||||
$_credit_totals = array_sum(array_column($data['credits'], 'amount'));
|
||||
$client->service()->updatePaidToDate($data['amount'])->save();
|
||||
} else {
|
||||
//this fixes an edge case with unapplied payments
|
||||
$client->service()->updatePaidToDate($data['amount'])->save();
|
||||
}
|
||||
|
||||
$client->service()->updatePaidToDate($_credit_totals)->save();
|
||||
// $client->paid_to_date += $_credit_totals;
|
||||
$client->saveQuietly();
|
||||
}
|
||||
}, 1);
|
||||
if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) {
|
||||
$_credit_totals = array_sum(array_column($data['credits'], 'amount'));
|
||||
$client->service()->updatePaidToDate($_credit_totals)->save();
|
||||
}
|
||||
|
||||
$client = Client::query()->where('id', $data['client_id'])->withTrashed()->first();
|
||||
$client->refresh();
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -153,7 +145,6 @@ class PaymentRepository extends BaseRepository
|
|||
|
||||
if ($invoice) {
|
||||
|
||||
//25-06-2023
|
||||
$paymentable = new Paymentable();
|
||||
$paymentable->payment_id = $payment->id;
|
||||
$paymentable->paymentable_id = $invoice->id;
|
||||
|
|
|
|||
|
|
@ -77,19 +77,21 @@ class ClientService
|
|||
*/
|
||||
public function updateBalance(float $amount)
|
||||
{
|
||||
try {
|
||||
DB::connection(config('database.default'))->transaction(function () use ($amount) {
|
||||
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
|
||||
$this->client->balance += $amount;
|
||||
$this->client->saveQuietly();
|
||||
}, 2);
|
||||
} catch (\Throwable $throwable) {
|
||||
// try {
|
||||
// DB::connection(config('database.default'))->transaction(function () use ($amount) {
|
||||
// $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
|
||||
// $this->client->balance += $amount;
|
||||
// $this->client->saveQuietly();
|
||||
// }, 2);
|
||||
// } catch (\Throwable $throwable) {
|
||||
|
||||
if (DB::connection(config('database.default'))->transactionLevel() > 0) {
|
||||
DB::connection(config('database.default'))->rollBack();
|
||||
}
|
||||
// if (DB::connection(config('database.default'))->transactionLevel() > 0) {
|
||||
// DB::connection(config('database.default'))->rollBack();
|
||||
// }
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
$this->client->increment('balance', $amount);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
@ -97,60 +99,10 @@ class ClientService
|
|||
public function updateBalanceAndPaidToDate(float $balance, float $paid_to_date)
|
||||
{
|
||||
|
||||
// refactor to calculated balances.
|
||||
// $ib = Invoice::withTrashed()
|
||||
// ->where('client_id', $this->client->id)
|
||||
// ->whereIn('status_id', [2,3])
|
||||
// ->where('is_deleted',0)
|
||||
// ->where('is_proforma',0)
|
||||
// ->sum('balance');
|
||||
$this->client->increment('balance', $balance);
|
||||
$this->client->increment('paid_to_date', $paid_to_date);
|
||||
|
||||
// $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 {
|
||||
DB::connection(config('database.default'))->transaction(function () use ($balance, $paid_to_date) {
|
||||
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
|
||||
|
|
@ -172,46 +124,22 @@ try {
|
|||
|
||||
public function updatePaidToDate(float $amount)
|
||||
{
|
||||
try {
|
||||
DB::connection(config('database.default'))->transaction(function () use ($amount) {
|
||||
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
|
||||
$this->client->paid_to_date += $amount;
|
||||
$this->client->saveQuietly();
|
||||
}, 2);
|
||||
} catch (\Throwable $throwable) {
|
||||
nlog("DB ERROR " . $throwable->getMessage());
|
||||
// try {
|
||||
// DB::connection(config('database.default'))->transaction(function () use ($amount) {
|
||||
// $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
|
||||
// $this->client->paid_to_date += $amount;
|
||||
// $this->client->saveQuietly();
|
||||
// }, 2);
|
||||
// } catch (\Throwable $throwable) {
|
||||
// nlog("DB ERROR " . $throwable->getMessage());
|
||||
|
||||
if (DB::connection(config('database.default'))->transactionLevel() > 0) {
|
||||
DB::connection(config('database.default'))->rollBack();
|
||||
}
|
||||
// if (DB::connection(config('database.default'))->transactionLevel() > 0) {
|
||||
// DB::connection(config('database.default'))->rollBack();
|
||||
// }
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
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);
|
||||
$this->client->increment('paid_to_date', $amount);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
@ -237,7 +165,36 @@ try {
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue