2023-11-07 02:10:16 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023. 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\CompanyLedger;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2023-11-26 08:41:42 +01:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
2023-11-07 02:10:16 +01:00
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2023-11-26 08:41:42 +01:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-11-07 02:10:16 +01:00
|
|
|
|
2023-11-08 05:19:30 +01:00
|
|
|
//@deprecated
|
2023-11-07 02:10:16 +01:00
|
|
|
class UpdateLedger implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $tries = 1;
|
2023-11-07 04:54:44 +01:00
|
|
|
|
2023-11-07 02:10:16 +01:00
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
|
|
|
public function __construct(private int $company_ledger_id, private float $start_amount, private string $company_key, private string $db)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle() :void
|
|
|
|
{
|
2023-11-07 10:04:56 +01:00
|
|
|
nlog("Updating company ledger for client ". $this->company_ledger_id);
|
2023-11-07 04:54:44 +01:00
|
|
|
|
2023-11-07 02:10:16 +01:00
|
|
|
MultiDB::setDb($this->db);
|
|
|
|
|
|
|
|
$cl = CompanyLedger::find($this->company_ledger_id);
|
2023-11-07 07:39:56 +01:00
|
|
|
|
2023-11-07 11:16:33 +01:00
|
|
|
$ledger_item = $cl->company_ledgerable->company_ledger()->count() == 1;
|
2023-11-07 10:04:56 +01:00
|
|
|
|
2023-11-07 11:16:33 +01:00
|
|
|
nlog($cl->company_ledgerable->company_ledger()->count());
|
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
if(!$cl) {
|
2023-11-07 07:39:56 +01:00
|
|
|
return;
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
2023-11-07 07:39:56 +01:00
|
|
|
|
2023-11-07 02:10:16 +01:00
|
|
|
$entity = $cl->company_ledgerable;
|
|
|
|
$balance = $entity->calc()->getBalance();
|
2023-11-07 11:16:33 +01:00
|
|
|
$cl->adjustment = $ledger_item ? $balance : ($balance - $this->start_amount);
|
2023-11-07 02:10:16 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
$parent_ledger = CompanyLedger::query()
|
|
|
|
->where('id', '<', $cl->id)
|
|
|
|
->where('company_id', $cl->company_id)
|
|
|
|
->where('client_id', $cl->client_id)
|
|
|
|
->where('balance', '!=', 0)
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->first();
|
2023-11-07 02:10:16 +01:00
|
|
|
|
2023-11-07 04:54:44 +01:00
|
|
|
$cl->balance = ($parent_ledger ? $parent_ledger->balance : 0) + $cl->adjustment;
|
2023-11-07 02:10:16 +01:00
|
|
|
$cl->save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function middleware()
|
|
|
|
{
|
2023-11-07 04:54:44 +01:00
|
|
|
return [new WithoutOverlapping($this->company_key)];
|
2023-11-07 02:10:16 +01:00
|
|
|
}
|
|
|
|
}
|