diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 835038859f..848e347a43 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -105,9 +105,9 @@ class CheckData extends Command config(['database.default' => $database]); } - $this->checkInvoiceBalances(); + $this->checkInvoiceBalances(); + $this->checkClientBalanceEdgeCases(); $this->checkPaidToDatesNew(); - $this->checkContacts(); $this->checkVendorContacts(); $this->checkEntityInvitations(); @@ -655,6 +655,39 @@ class CheckData extends Command $this->logMessage("{$this->wrong_paid_to_dates} clients with incorrect client balances"); } + private function checkClientBalanceEdgeCases() + { + Client::query() + ->where('is_deleted',false) + ->where('balance', '!=', 0) + ->cursor() + ->each(function ($client){ + + $count = Invoice::withTrashed() + ->where('client_id', $client->id) + ->where('is_deleted',false) + ->whereIn('status_id', [2,3]) + ->count(); + + if($count == 0){ + $this->logMessage("# {$client->id} # {$client->name} {$client->balance} is invalid should be 0"); + + if($this->option('client_balance')){ + + $this->logMessage("# {$client->id} " . $client->present()->name.' - '.$client->number." Fixing {$client->balance} to 0"); + + $client->balance = 0; + $client->save(); + + } + + + } + + }); + + } + private function invoiceBalanceQuery() { $results = \DB::select( \DB::raw(" diff --git a/app/Http/Requests/Invoice/UpdateInvoiceRequest.php b/app/Http/Requests/Invoice/UpdateInvoiceRequest.php index afbe0999a6..e555c19752 100644 --- a/app/Http/Requests/Invoice/UpdateInvoiceRequest.php +++ b/app/Http/Requests/Invoice/UpdateInvoiceRequest.php @@ -62,9 +62,6 @@ class UpdateInvoiceRequest extends Request $rules['discount'] = 'sometimes|numeric'; $rules['project_id'] = ['bail', 'sometimes', new ValidProjectForClient($this->all())]; - // if($this->input('status_id') != Invoice::STATUS_DRAFT) - // $rules['balance'] = new InvoiceBalanceSanity($this->invoice, $this->all()); - return $rules; } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index f0afe983f6..3d1f623e5d 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -309,7 +309,6 @@ class BaseRepository $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}"); - } if (! $model->design_id)