1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Ledger refactor

This commit is contained in:
David Bomba 2022-05-15 17:51:06 +10:00
parent 431b2b84d4
commit 7c3613455d
7 changed files with 113 additions and 11 deletions

View File

@ -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)) {

View File

@ -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();

View File

@ -38,6 +38,8 @@ class SubscriptionCron
public function handle() : void
{
nlog("Subscription Cron");
if (! config('ninja.db.multi_db_enabled')) {
$this->loopSubscriptions();

View File

@ -0,0 +1,85 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Ledger;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\Company;
use App\Models\CompanyLedger;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ClientLedgerBalanceUpdate implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
public $company;
public $client;
public function __construct(Company $company, Client $client)
{
$this->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()
{
}
}

View File

@ -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();

View File

@ -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/');

View File

@ -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;
}