mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 05:54:35 -04:00
Ledger refactor
This commit is contained in:
parent
431b2b84d4
commit
7c3613455d
@ -607,7 +607,7 @@ class CheckData extends Command
|
|||||||
invoices ON
|
invoices ON
|
||||||
clients.id=invoices.client_id
|
clients.id=invoices.client_id
|
||||||
WHERE invoices.is_deleted = false
|
WHERE invoices.is_deleted = false
|
||||||
AND invoices.status_id IN (2,3,4)
|
AND invoices.status_id IN (2,3)
|
||||||
GROUP BY clients.id
|
GROUP BY clients.id
|
||||||
HAVING invoice_balance != clients.balance
|
HAVING invoice_balance != clients.balance
|
||||||
ORDER BY clients.id;
|
ORDER BY clients.id;
|
||||||
@ -663,7 +663,7 @@ class CheckData extends Command
|
|||||||
ON invoices.client_id = clients.id
|
ON invoices.client_id = clients.id
|
||||||
WHERE invoices.is_deleted = 0
|
WHERE invoices.is_deleted = 0
|
||||||
AND clients.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
|
GROUP BY clients.id
|
||||||
HAVING(invoices_balance != clients.balance)
|
HAVING(invoices_balance != clients.balance)
|
||||||
ORDER BY clients.id;
|
ORDER BY clients.id;
|
||||||
@ -683,7 +683,7 @@ class CheckData extends Command
|
|||||||
{
|
{
|
||||||
$client = Client::withTrashed()->find($_client->id);
|
$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();
|
$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;
|
$this->wrong_paid_to_dates = 0;
|
||||||
|
|
||||||
foreach (Client::where('is_deleted', 0)->where('clients.updated_at', '>', now()->subDays(2))->cursor() as $client) {
|
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();
|
$ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
|
||||||
|
|
||||||
if ($ledger && number_format($ledger->balance, 4) != number_format($client->balance, 4)) {
|
if ($ledger && number_format($ledger->balance, 4) != number_format($client->balance, 4)) {
|
||||||
|
@ -87,9 +87,9 @@ class Kernel extends ConsoleKernel
|
|||||||
|
|
||||||
$schedule->job(new SendFailedEmails)->daily()->withoutOverlapping();
|
$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();
|
$schedule->command('ninja:s3-cleanup')->dailyAt('23:15')->withoutOverlapping();
|
||||||
|
|
||||||
|
@ -38,6 +38,8 @@ class SubscriptionCron
|
|||||||
public function handle() : void
|
public function handle() : void
|
||||||
{
|
{
|
||||||
|
|
||||||
|
nlog("Subscription Cron");
|
||||||
|
|
||||||
if (! config('ninja.db.multi_db_enabled')) {
|
if (! config('ninja.db.multi_db_enabled')) {
|
||||||
|
|
||||||
$this->loopSubscriptions();
|
$this->loopSubscriptions();
|
||||||
|
85
app/Jobs/Ledger/ClientLedgerBalanceUpdate.php
Normal file
85
app/Jobs/Ledger/ClientLedgerBalanceUpdate.php
Normal 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()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -24,6 +24,8 @@ class LedgerBalanceUpdate implements ShouldQueue
|
|||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
public $tries = 1;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -34,25 +36,30 @@ class LedgerBalanceUpdate implements ShouldQueue
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle() :void
|
||||||
{
|
{
|
||||||
|
nlog("Updating company ledgers");
|
||||||
|
|
||||||
if (! config('ninja.db.multi_db_enabled')) {
|
if (! config('ninja.db.multi_db_enabled')) {
|
||||||
$this->check();
|
$this->checkLedger();
|
||||||
} else {
|
} else {
|
||||||
//multiDB environment, need to
|
//multiDB environment, need to
|
||||||
foreach (MultiDB::$dbs as $db) {
|
foreach (MultiDB::$dbs as $db) {
|
||||||
MultiDB::setDB($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){
|
CompanyLedger::where('balance', 0)->cursor()->each(function ($company_ledger){
|
||||||
|
|
||||||
if($company_ledger->balance > 0)
|
if($company_ledger->balance > 0)
|
||||||
@ -67,6 +74,8 @@ class LedgerBalanceUpdate implements ShouldQueue
|
|||||||
if(!$last_record)
|
if(!$last_record)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
nlog("Updating Balance NOW");
|
||||||
|
|
||||||
$company_ledger->balance = $last_record->balance + $company_ledger->adjustment;
|
$company_ledger->balance = $last_record->balance + $company_ledger->adjustment;
|
||||||
$company_ledger->save();
|
$company_ledger->save();
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ class DiskCleanup implements ShouldQueue
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
|
nlog("Cleaning Storage");
|
||||||
|
|
||||||
// Get all files in a directory
|
// Get all files in a directory
|
||||||
$files = Storage::allFiles(config('filesystems.default'), 'backups/');
|
$files = Storage::allFiles(config('filesystems.default'), 'backups/');
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace App\Services\Ledger;
|
namespace App\Services\Ledger;
|
||||||
|
|
||||||
use App\Factory\CompanyLedgerFactory;
|
use App\Factory\CompanyLedgerFactory;
|
||||||
|
use App\Jobs\Ledger\ClientLedgerBalanceUpdate;
|
||||||
use App\Models\Activity;
|
use App\Models\Activity;
|
||||||
use App\Models\CompanyLedger;
|
use App\Models\CompanyLedger;
|
||||||
|
|
||||||
@ -36,6 +37,7 @@ class LedgerService
|
|||||||
|
|
||||||
$this->entity->company_ledger()->save($company_ledger);
|
$this->entity->company_ledger()->save($company_ledger);
|
||||||
|
|
||||||
|
ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(300);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -52,6 +54,8 @@ class LedgerService
|
|||||||
|
|
||||||
$this->entity->company_ledger()->save($company_ledger);
|
$this->entity->company_ledger()->save($company_ledger);
|
||||||
|
|
||||||
|
ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(300);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,6 +71,8 @@ class LedgerService
|
|||||||
|
|
||||||
$this->entity->company_ledger()->save($company_ledger);
|
$this->entity->company_ledger()->save($company_ledger);
|
||||||
|
|
||||||
|
ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(300);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user