1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 17:31:35 +02:00
invoiceninja/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php

81 lines
2.4 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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
{
2024-01-14 05:05:00 +01:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
2022-05-15 09:51:06 +02:00
public $tries = 1;
2024-01-14 05:05:00 +01:00
2022-08-02 09:04:33 +02:00
public $deleteWhenMissingModels = true;
2023-11-08 05:19:30 +01:00
private ?CompanyLedger $next_balance_record;
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
*/
2024-01-14 05:05:00 +01:00
public function handle(): void
2022-05-15 09:51:06 +02:00
{
2024-04-10 14:07:03 +02:00
2022-05-15 09:51:06 +02:00
MultiDB::setDb($this->company->db);
2023-11-08 05:19:30 +01:00
CompanyLedger::query()
2024-04-10 14:07:03 +02:00
->whereNull('balance')
2023-11-08 05:19:30 +01:00
->where('client_id', $this->client->id)
->orderBy('id', 'ASC')
->get()
->each(function ($company_ledger) {
$parent_ledger = CompanyLedger::query()
->where('id', '<', $company_ledger->id)
->where('client_id', $company_ledger->client_id)
->where('company_id', $company_ledger->company_id)
2024-04-14 23:57:24 +02:00
->whereNotNull('balance')
// ->where('balance', '!=', 0)
2023-11-08 05:19:30 +01:00
->orderBy('id', 'DESC')
->first();
$company_ledger->balance = ($parent_ledger ? $parent_ledger->balance : 0) + $company_ledger->adjustment;
$company_ledger->save();
2023-11-26 08:41:42 +01:00
});
2023-11-08 05:19:30 +01:00
}
public function middleware()
{
return [(new WithoutOverlapping($this->client->id))->dontRelease()];
}
2022-05-15 09:51:06 +02:00
}