2022-05-06 02:55:48 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-05-06 02:55:48 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Ledger;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
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 LedgerBalanceUpdate implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2022-05-15 09:51:06 +02:00
|
|
|
public $tries = 1;
|
|
|
|
|
2022-05-06 02:55:48 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-05-15 09:51:06 +02:00
|
|
|
public function handle() :void
|
2022-05-06 02:55:48 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('Updating company ledgers');
|
2022-05-06 02:55:48 +02:00
|
|
|
|
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2022-05-15 09:51:06 +02:00
|
|
|
$this->checkLedger();
|
2022-05-06 02:55:48 +02:00
|
|
|
} else {
|
|
|
|
//multiDB environment, need to
|
|
|
|
foreach (MultiDB::$dbs as $db) {
|
|
|
|
MultiDB::setDB($db);
|
|
|
|
|
2022-05-15 09:51:06 +02:00
|
|
|
$this->checkLedger();
|
2022-05-06 02:55:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('Finished checking company ledgers');
|
2022-05-06 02:55:48 +02:00
|
|
|
}
|
|
|
|
|
2022-05-15 09:51:06 +02:00
|
|
|
public function checkLedger()
|
2022-05-06 02:55:48 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('Checking ledgers....');
|
2022-05-06 02:55:48 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
CompanyLedger::where('balance', 0)->where('adjustment', '!=', 0)->cursor()->each(function ($company_ledger) {
|
|
|
|
if ($company_ledger->balance > 0) {
|
2022-05-06 02:55:48 +02:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-06 02:55:48 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$last_record = CompanyLedger::where('client_id', $company_ledger->client_id)
|
2022-05-06 02:55:48 +02:00
|
|
|
->where('company_id', $company_ledger->company_id)
|
|
|
|
->where('balance', '!=', 0)
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->first();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $last_record) {
|
2022-05-07 09:35:56 +02:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nlog('Updating Balance NOW');
|
2022-05-07 09:35:56 +02:00
|
|
|
|
2022-05-06 02:55:48 +02:00
|
|
|
$company_ledger->balance = $last_record->balance + $company_ledger->adjustment;
|
|
|
|
$company_ledger->save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|