From f0013653c75b5b8fd4e3b2db9e82e0757d01693f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 8 Nov 2023 19:03:56 +1100 Subject: [PATCH] Cleanup --- app/Jobs/Ledger/ClientLedgerBalanceUpdate.php | 52 ++++++------------- app/Repositories/BaseRepository.php | 12 +++-- app/Services/Client/ClientService.php | 17 +++++- app/Services/Ledger/LedgerService.php | 50 +++++++++++------- 4 files changed, 71 insertions(+), 60 deletions(-) diff --git a/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php b/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php index 639fd2be3370..58f41ff61221 100644 --- a/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php +++ b/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php @@ -46,27 +46,27 @@ class ClientLedgerBalanceUpdate implements ShouldQueue { $uuid = \Illuminate\Support\Str::uuid(); - nlog("Updating company ledger for client {$this->client->id} {$uuid}"); + // nlog("Updating company ledger for client {$this->client->id} {$uuid}"); MultiDB::setDb($this->company->db); - $dupes = CompanyLedger::query() - ->where('client_id', $this->client->id) - ->where('balance', 0) - ->where('hash', '<>', '') - ->groupBy(['adjustment','hash']) - ->havingRaw('COUNT(*) > 1') - ->pluck('id'); + // $dupes = CompanyLedger::query() + // ->where('client_id', $this->client->id) + // ->where('balance', 0) + // ->where('hash', '<>', '') + // ->groupBy(['adjustment','hash']) + // ->havingRaw('COUNT(*) > 1') + // ->pluck('id'); // CompanyLedger::query()->whereIn('id', $dupes)->delete(); - $dupes = CompanyLedger::query() - ->where('client_id', $this->client->id) - ->where('balance', 0) - ->where('hash', '<>', '') - ->groupBy(['adjustment','hash']) - ->havingRaw('COUNT(*) > 1') - ->pluck('id'); + // $dupes = CompanyLedger::query() + // ->where('client_id', $this->client->id) + // ->where('balance', 0) + // ->where('hash', '<>', '') + // ->groupBy(['adjustment','hash']) + // ->havingRaw('COUNT(*) > 1') + // ->pluck('id'); // CompanyLedger::query()->whereIn('id', $dupes)->delete(); @@ -85,37 +85,17 @@ class ClientLedgerBalanceUpdate implements ShouldQueue ->orderBy('id', 'DESC') ->first(); - // if ($company_ledger->balance == 0) { - - // $last_record = CompanyLedger::query() - // ->where('client_id', $company_ledger->client_id) - // ->where('company_id', $company_ledger->company_id) - // ->where('balance', '!=', 0) - // ->orderBy('id', 'DESC') - // ->first(); - - // if (! $last_record) { - // $last_record = CompanyLedger::query() - // ->where('client_id', $company_ledger->client_id) - // ->where('company_id', $company_ledger->company_id) - // ->orderBy('id', 'DESC') - // ->first(); - // } - - // } - // $company_ledger->balance = $last_record->balance + $company_ledger->adjustment; $company_ledger->balance = ($parent_ledger ? $parent_ledger->balance : 0) + $company_ledger->adjustment; $company_ledger->save(); }); - nlog("finished job {$uuid}"); + // nlog("finished job {$uuid}"); } public function middleware() { - nlog("middle ware {$this->client->id}"); return [(new WithoutOverlapping($this->client->id))->dontRelease()]; } } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 40519498019d..ee20a4fef641 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -161,7 +161,7 @@ class BaseRepository $lcfirst_resource_id = $this->resolveEntityKey($model); //ie invoice_id - $state['starting_amount'] = $model->amount; + $state['starting_amount'] = $model->balance; if (! $model->id) { $company_defaults = $client->setCompanyDefaults($data, lcfirst($resource)); @@ -273,7 +273,7 @@ class BaseRepository $model = $model->calc()->getInvoice(); /* We use this to compare to our starting amount */ - $state['finished_amount'] = $model->amount; + $state['finished_amount'] = $model->balance; /* Apply entity number */ $model = $model->service()->applyNumber()->save(); @@ -292,8 +292,12 @@ class BaseRepository if ($model instanceof Invoice) { if ($model->status_id != Invoice::STATUS_DRAFT) { $model->service()->updateStatus()->save(); - $model->client->service()->calculateBalance(); - $model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}"); + $model->client->service()->calculateBalance($model); + + // $diff = $state['finished_amount'] - $state['starting_amount']; + // nlog("{$diff} - {$state['finished_amount']} - {$state['starting_amount']}"); + // if(floatval($state['finished_amount']) != floatval($state['starting_amount'])) + // $model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}"); } if (! $model->design_id) { diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index 5ac1f6e8f43b..410b388841fb 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -35,13 +35,15 @@ class ClientService { } - public function calculateBalance() + public function calculateBalance(?Invoice $invoice = null) { $balance = Invoice::where('client_id', $this->client->id) ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) ->where('is_deleted', false) ->sum('balance'); + $pre_client_balance = $this->client->balance; + try { DB::connection(config('database.default'))->transaction(function () use ($balance) { $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); @@ -52,9 +54,20 @@ class ClientService nlog("DB ERROR " . $throwable->getMessage()); } + if($invoice && floatval($this->client->balance) != floatval($pre_client_balance)) { + $diff = $this->client->balance - $pre_client_balance; + $invoice->ledger()->insertInvoiceBalance($diff, $this->client->balance, "Update Adjustment Invoice # {$invoice->number} => {$diff}"); + } + return $this; } - + + /** + * Seeing too many race conditions under heavy load here. + * @deprecated + * @param float $amount + * @return void + */ public function updateBalance(float $amount) { try { diff --git a/app/Services/Ledger/LedgerService.php b/app/Services/Ledger/LedgerService.php index 9d2b3fa6795d..2a589191d8b2 100644 --- a/app/Services/Ledger/LedgerService.php +++ b/app/Services/Ledger/LedgerService.php @@ -26,6 +26,22 @@ class LedgerService $this->entity = $entity; } + public function insertInvoiceBalance($adjustment, $balance, $notes) + { + $company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id); + $company_ledger->client_id = $this->entity->client_id; + $company_ledger->adjustment = $adjustment; + $company_ledger->notes = $notes; + $company_ledger->balance = $balance; + $company_ledger->activity_id = Activity::UPDATE_INVOICE; + $company_ledger->save(); + + $this->entity->company_ledger()->save($company_ledger); + + return $this; + + } + public function updateInvoiceBalance($adjustment, $notes = '') { @@ -36,36 +52,34 @@ class LedgerService // $hash = sha1($adjustment.$notes.$this->entity->status_id.$this->entity->client_id.$this->entity->amount.$this->entity->balance.$this->entity->company_id.Activity::UPDATE_INVOICE); // $hash = sha1($hash); // $hash = sha1("{$this->entity->amount}.{$this->entity->balance}"); - $hash = "{$adjustment}.{$this->entity->amount}.{$this->entity->balance}"; + // $hash = "{$adjustment}.{$this->entity->amount}.{$this->entity->balance}"; - usleep(10000000); + // $exists = CompanyLedger::query() + // ->where('client_id', $this->entity->client_id) + // ->where('company_id', $this->entity->company_id) + // ->where('activity_id', Activity::UPDATE_INVOICE) + // ->where('adjustment', $adjustment) + // ->where('hash', $hash) + // ->where('notes', $notes) + // ->where('created_at', '>=', now()->subSeconds(1)) + // ->exists(); - $exists = CompanyLedger::query() - ->where('client_id', $this->entity->client_id) - ->where('company_id', $this->entity->company_id) - ->where('activity_id', Activity::UPDATE_INVOICE) - ->where('adjustment', $adjustment) - ->where('hash', $hash) - ->where('notes', $notes) - ->where('created_at', '>=', now()->subSeconds(1)) - ->exists(); - - if($exists) { - nlog("Collision {$adjustment} {$notes}"); - return $this; - } + // if($exists) { + // nlog("Collision {$adjustment} {$notes}"); + // return $this; + // } $company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id); $company_ledger->client_id = $this->entity->client_id; $company_ledger->adjustment = $adjustment; $company_ledger->notes = $notes; - $company_ledger->hash = $hash; + // $company_ledger->hash = $hash; $company_ledger->activity_id = Activity::UPDATE_INVOICE; $company_ledger->save(); $this->entity->company_ledger()->save($company_ledger); - ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(rand(4, 7)); + ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(rand(3, 7)); return $this; }