diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 11120817a88d..a57375b47612 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -607,7 +607,7 @@ class CheckData extends Command invoices ON clients.id=invoices.client_id WHERE invoices.is_deleted = false - AND invoices.status_id IN (2,3,4) + AND invoices.status_id IN (2,3) GROUP BY clients.id HAVING invoice_balance != clients.balance ORDER BY clients.id; @@ -663,7 +663,7 @@ class CheckData extends Command ON invoices.client_id = clients.id WHERE invoices.is_deleted = 0 AND clients.is_deleted = 0 - AND invoices.status_id IN (2,3,4) + AND invoices.status_id IN (2,3) GROUP BY clients.id HAVING(invoices_balance != clients.balance) ORDER BY clients.id; @@ -683,7 +683,7 @@ class CheckData extends Command { $client = Client::withTrashed()->find($_client->id); - $invoice_balance = $client->invoices()->where('is_deleted', false)->whereIn('status_id', [2,3,4])->sum('balance'); + $invoice_balance = $client->invoices()->where('is_deleted', false)->whereIn('status_id', [2,3])->sum('balance'); $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first(); @@ -724,7 +724,7 @@ class CheckData extends Command $this->wrong_paid_to_dates = 0; foreach (Client::where('is_deleted', 0)->where('clients.updated_at', '>', now()->subDays(2))->cursor() as $client) { - $invoice_balance = $client->invoices()->where('is_deleted', false)->whereIn('status_id', [2,3,4])->sum('balance'); + $invoice_balance = $client->invoices()->where('is_deleted', false)->whereIn('status_id', [2,3])->sum('balance'); $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first(); if ($ledger && number_format($ledger->balance, 4) != number_format($client->balance, 4)) { diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index c27713e47c94..b6e59ec00378 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -87,9 +87,9 @@ class Kernel extends ConsoleKernel $schedule->job(new SendFailedEmails)->daily()->withoutOverlapping(); - $schedule->command('ninja:check-data --database=db-ninja-01')->daily('01:00')->withoutOverlapping(); + $schedule->command('ninja:check-data --database=db-ninja-01')->daily('02:00')->withoutOverlapping(); - $schedule->command('ninja:check-data --database=db-ninja-02')->dailyAt('01:05')->withoutOverlapping(); + $schedule->command('ninja:check-data --database=db-ninja-02')->dailyAt('02:05')->withoutOverlapping(); $schedule->command('ninja:s3-cleanup')->dailyAt('23:15')->withoutOverlapping(); diff --git a/app/Jobs/Cron/SubscriptionCron.php b/app/Jobs/Cron/SubscriptionCron.php index 1cda190ec999..536c49063e84 100644 --- a/app/Jobs/Cron/SubscriptionCron.php +++ b/app/Jobs/Cron/SubscriptionCron.php @@ -38,6 +38,8 @@ class SubscriptionCron public function handle() : void { + nlog("Subscription Cron"); + if (! config('ninja.db.multi_db_enabled')) { $this->loopSubscriptions(); diff --git a/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php b/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php new file mode 100644 index 000000000000..36068bdb7fea --- /dev/null +++ b/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php @@ -0,0 +1,85 @@ +company = $company; + $this->client = $client; + } + + /** + * Execute the job. + * + * + * @return void + */ + public function handle() :void + { + nlog("Updating company ledgers"); + + MultiDB::setDb($this->company->db); + + CompanyLedger::where('balance', 0)->where('client_id', $this->client->id)->cursor()->each(function ($company_ledger){ + + if($company_ledger->balance > 0) + return; + + $last_record = CompanyLedger::where('client_id', $company_ledger->client_id) + ->where('company_id', $company_ledger->company_id) + ->where('balance', '!=', 0) + ->orderBy('id', 'DESC') + ->first(); + + if(!$last_record) + return; + + nlog("Updating Balance NOW"); + + $company_ledger->balance = $last_record->balance + $company_ledger->adjustment; + $company_ledger->save(); + + }); + + nlog("Finished checking company ledgers"); + + } + + public function checkLedger() + { + + + } + +} diff --git a/app/Jobs/Ledger/LedgerBalanceUpdate.php b/app/Jobs/Ledger/LedgerBalanceUpdate.php index e7fc1bde0157..536814e0d751 100644 --- a/app/Jobs/Ledger/LedgerBalanceUpdate.php +++ b/app/Jobs/Ledger/LedgerBalanceUpdate.php @@ -24,6 +24,8 @@ class LedgerBalanceUpdate implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + public $tries = 1; + public function __construct() { } @@ -34,25 +36,30 @@ class LedgerBalanceUpdate implements ShouldQueue * * @return void */ - public function handle() + public function handle() :void { + nlog("Updating company ledgers"); if (! config('ninja.db.multi_db_enabled')) { - $this->check(); + $this->checkLedger(); } else { //multiDB environment, need to foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); - $this->check(); + $this->checkLedger(); } } + nlog("Finished checking company ledgers"); + } - public function check() + public function checkLedger() { + nlog("Checking ledgers...."); + CompanyLedger::where('balance', 0)->cursor()->each(function ($company_ledger){ if($company_ledger->balance > 0) @@ -67,6 +74,8 @@ class LedgerBalanceUpdate implements ShouldQueue if(!$last_record) return; + nlog("Updating Balance NOW"); + $company_ledger->balance = $last_record->balance + $company_ledger->adjustment; $company_ledger->save(); diff --git a/app/Jobs/Util/DiskCleanup.php b/app/Jobs/Util/DiskCleanup.php index d18ac9737d10..698ae3975db7 100644 --- a/app/Jobs/Util/DiskCleanup.php +++ b/app/Jobs/Util/DiskCleanup.php @@ -36,7 +36,7 @@ class DiskCleanup implements ShouldQueue */ public function handle() { - + nlog("Cleaning Storage"); // Get all files in a directory $files = Storage::allFiles(config('filesystems.default'), 'backups/'); diff --git a/app/Services/Ledger/LedgerService.php b/app/Services/Ledger/LedgerService.php index df8d9568ead8..4925bcca997c 100644 --- a/app/Services/Ledger/LedgerService.php +++ b/app/Services/Ledger/LedgerService.php @@ -12,6 +12,7 @@ namespace App\Services\Ledger; use App\Factory\CompanyLedgerFactory; +use App\Jobs\Ledger\ClientLedgerBalanceUpdate; use App\Models\Activity; use App\Models\CompanyLedger; @@ -36,6 +37,7 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); + ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(300); return $this; } @@ -52,6 +54,8 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); + ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(300); + return $this; } @@ -67,6 +71,8 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); + ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(300); + return $this; }