1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php

73 lines
2.3 KiB
PHP
Raw Normal View History

2022-05-15 09:51:06 +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-15 09:51:06 +02:00
*
* @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\Middleware\WithoutOverlapping;
2022-05-15 09:51:06 +02:00
use Illuminate\Queue\SerializesModels;
class ClientLedgerBalanceUpdate implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
2022-08-02 09:04:33 +02:00
public $deleteWhenMissingModels = true;
public function __construct(public Company $company, public Client $client)
2023-02-17 22:36:51 +01:00
{
}
2022-05-15 09:51:06 +02:00
/**
* Execute the job.
*
*
* @return void
*/
public function handle() :void
{
2022-06-10 04:18:02 +02:00
// nlog("Updating company ledger for client ". $this->client->id);
2022-05-15 09:51:06 +02:00
MultiDB::setDb($this->company->db);
2023-08-08 10:56:31 +02:00
CompanyLedger::query()->where('balance', 0)->where('client_id', $this->client->id)->orderBy('updated_at', 'ASC')->cursor()->each(function ($company_ledger) {
2023-02-16 02:36:09 +01:00
if ($company_ledger->balance == 0) {
2023-08-08 10:56:31 +02:00
$last_record = CompanyLedger::query()->where('client_id', $company_ledger->client_id)
2023-01-15 05:10:41 +01:00
->where('company_id', $company_ledger->company_id)
->where('balance', '!=', 0)
->orderBy('id', 'DESC')
->first();
if (! $last_record) {
2023-08-08 10:56:31 +02:00
$last_record = CompanyLedger::query()->where('client_id', $company_ledger->client_id)
2023-01-15 05:10:41 +01:00
->where('company_id', $company_ledger->company_id)
->orderBy('id', 'DESC')
->first();
}
2022-05-20 01:21:47 +02:00
}
2023-01-15 05:10:41 +01:00
2023-02-16 02:36:09 +01:00
$company_ledger->balance = $last_record->balance + $company_ledger->adjustment;
$company_ledger->save();
2022-05-15 09:51:06 +02:00
});
}
public function middleware()
{
return [(new WithoutOverlapping($this->client->id))->dontRelease()];
}
2022-05-15 09:51:06 +02:00
}