diff --git a/app/Jobs/Ledger/UpdateLedger.php b/app/Jobs/Ledger/UpdateLedger.php new file mode 100644 index 000000000000..bf4430990a8a --- /dev/null +++ b/app/Jobs/Ledger/UpdateLedger.php @@ -0,0 +1,100 @@ +db); + + $cl = CompanyLedger::find($this->company_ledger_id); + $entity = $cl->company_ledgerable; + $balance = $entity->calc()->getBalance(); + $cl->adjustment = $balance - $this->start_amount; + + $parent_ledger = CompanyLedger::query() + ->where('id', '<', $cl->id) + ->where('company_id', $cl->company_id) + ->where('client_id', $cl->client_id) + ->orderBy('id', 'DESC') + ->first(); + + $cl->balance = $parent_ledger ? $parent_ledger->balance + $cl->adjustment : 0; + $cl->save(); + + + // CompanyLedger::query() + // ->where('company_id', $cl->company_id) + // ->where('client_id', $cl->client_id) + // ->where('balance', 0) + // ->orderBy('updated_at', 'ASC') + // ->cursor() + // ->each(function ($company_ledger) { + + // $last_record = null; + + // if ($company_ledger->balance == 0) { + // $last_record = CompanyLedger::query() + // ->where('company_id', $company_ledger->company_id) + // ->where('client_id', $company_ledger->client_id) + // ->where('balance', '!=', 0) + // ->orderBy('id', 'DESC') + // ->first(); + + // if (! $last_record) { + // $last_record = CompanyLedger::query() + // ->where('company_id', $company_ledger->company_id) + // ->where('client_id', $company_ledger->client_id) + // ->orderBy('id', 'DESC') + // ->first(); + // } + // } + + // if($last_record) { + // $company_ledger->balance = $last_record->balance + $company_ledger->adjustment; + // $company_ledger->save(); + // } + // }); + + + } + + public function middleware() + { + return [(new WithoutOverlapping($this->company_key))->dontRelease()]; + } +} diff --git a/app/Models/CompanyLedger.php b/app/Models/CompanyLedger.php index 5b624e4a7903..caea1b0b9946 100644 --- a/app/Models/CompanyLedger.php +++ b/app/Models/CompanyLedger.php @@ -21,8 +21,8 @@ use Illuminate\Database\Eloquent\Model; * @property int|null $client_id * @property int|null $user_id * @property int|null $activity_id - * @property string|null $adjustment - * @property string|null $balance + * @property float|null $adjustment + * @property float|null $balance * @property string|null $notes * @property string|null $hash * @property int $company_ledgerable_id diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 2fc13b4ff3db..8442134a1702 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -290,10 +290,13 @@ nlog($model->toArray()); /* Perform model specific tasks */ if ($model instanceof Invoice) { - if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) { + if ($model->status_id != Invoice::STATUS_DRAFT) { + // if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) { $model->service()->updateStatus()->save(); - $model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save(); - $model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}"); + // $model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save(); + $model->client->service()->calculateBalance(); + $model->ledger()->mutateInvoiceBalance($state['starting_amount'], "Update adjustment for invoice {$model->number} by {$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 e8e39fb4cae1..5ac1f6e8f43b 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -11,6 +11,7 @@ namespace App\Services\Client; +use Carbon\Carbon; use App\Utils\Number; use App\Models\Client; use App\Models\Credit; @@ -34,6 +35,26 @@ class ClientService { } + public function calculateBalance() + { + $balance = Invoice::where('client_id', $this->client->id) + ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) + ->where('is_deleted', false) + ->sum('balance'); + + try { + DB::connection(config('database.default'))->transaction(function () use ($balance) { + $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); + $this->client->balance = $balance; + $this->client->saveQuietly(); + }, 2); + } catch (\Throwable $throwable) { + nlog("DB ERROR " . $throwable->getMessage()); + } + + return $this; + } + public function updateBalance(float $amount) { try { diff --git a/app/Services/Ledger/LedgerService.php b/app/Services/Ledger/LedgerService.php index d03945ad21db..376b8823d667 100644 --- a/app/Services/Ledger/LedgerService.php +++ b/app/Services/Ledger/LedgerService.php @@ -13,6 +13,7 @@ namespace App\Services\Ledger; use App\Factory\CompanyLedgerFactory; use App\Jobs\Ledger\ClientLedgerBalanceUpdate; +use App\Jobs\Ledger\UpdateLedger; use App\Models\Activity; class LedgerService @@ -24,6 +25,20 @@ class LedgerService $this->entity = $entity; } + public function mutateInvoiceBalance(float $start_amount, string $notes ='') + { + $company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id); + $company_ledger->client_id = $this->entity->client_id; + $company_ledger->adjustment = 0; + $company_ledger->notes = $notes; + $company_ledger->activity_id = Activity::UPDATE_INVOICE; + $company_ledger->save(); + + $this->entity->company_ledger()->save($company_ledger); + + UpdateLedger::dispatch($company_ledger->id, $start_amount, $this->entity->company->company_key, $this->entity->company->db); + } + public function updateInvoiceBalance($adjustment, $notes = '') { $company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id);