mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Improvements for client balance / ledger adjustments
This commit is contained in:
parent
14ae0900ae
commit
ae6d4680ad
100
app/Jobs/Ledger/UpdateLedger.php
Normal file
100
app/Jobs/Ledger/UpdateLedger.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?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\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
||||
|
||||
class UpdateLedger implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $tries = 1;
|
||||
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
|
||||
{
|
||||
MultiDB::setDb($this->db);
|
||||
|
||||
$cl = CompanyLedger::find($this->company_ledger_id);
|
||||
$entity = $cl->company_ledgerable;
|
||||
$balance = $entity->calc()->getBalance();
|
||||
$cl->adjustment = $balance - $this->start_amount;
|
||||
|
||||
$parent_ledger = CompanyLedger::query()
|
||||
->where('id', '<', $cl->id)
|
||||
->where('company_id', $cl->company_id)
|
||||
->where('client_id', $cl->client_id)
|
||||
->orderBy('id', 'DESC')
|
||||
->first();
|
||||
|
||||
$cl->balance = $parent_ledger ? $parent_ledger->balance + $cl->adjustment : 0;
|
||||
$cl->save();
|
||||
|
||||
|
||||
// CompanyLedger::query()
|
||||
// ->where('company_id', $cl->company_id)
|
||||
// ->where('client_id', $cl->client_id)
|
||||
// ->where('balance', 0)
|
||||
// ->orderBy('updated_at', 'ASC')
|
||||
// ->cursor()
|
||||
// ->each(function ($company_ledger) {
|
||||
|
||||
// $last_record = null;
|
||||
|
||||
// if ($company_ledger->balance == 0) {
|
||||
// $last_record = CompanyLedger::query()
|
||||
// ->where('company_id', $company_ledger->company_id)
|
||||
// ->where('client_id', $company_ledger->client_id)
|
||||
// ->where('balance', '!=', 0)
|
||||
// ->orderBy('id', 'DESC')
|
||||
// ->first();
|
||||
|
||||
// if (! $last_record) {
|
||||
// $last_record = CompanyLedger::query()
|
||||
// ->where('company_id', $company_ledger->company_id)
|
||||
// ->where('client_id', $company_ledger->client_id)
|
||||
// ->orderBy('id', 'DESC')
|
||||
// ->first();
|
||||
// }
|
||||
// }
|
||||
|
||||
// if($last_record) {
|
||||
// $company_ledger->balance = $last_record->balance + $company_ledger->adjustment;
|
||||
// $company_ledger->save();
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function middleware()
|
||||
{
|
||||
return [(new WithoutOverlapping($this->company_key))->dontRelease()];
|
||||
}
|
||||
}
|
@ -21,8 +21,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||
* @property int|null $client_id
|
||||
* @property int|null $user_id
|
||||
* @property int|null $activity_id
|
||||
* @property string|null $adjustment
|
||||
* @property string|null $balance
|
||||
* @property float|null $adjustment
|
||||
* @property float|null $balance
|
||||
* @property string|null $notes
|
||||
* @property string|null $hash
|
||||
* @property int $company_ledgerable_id
|
||||
|
@ -290,10 +290,13 @@ nlog($model->toArray());
|
||||
|
||||
/* Perform model specific tasks */
|
||||
if ($model instanceof Invoice) {
|
||||
if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
|
||||
if ($model->status_id != Invoice::STATUS_DRAFT) {
|
||||
// if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
|
||||
$model->service()->updateStatus()->save();
|
||||
$model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
|
||||
$model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
|
||||
// $model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
|
||||
$model->client->service()->calculateBalance();
|
||||
$model->ledger()->mutateInvoiceBalance($state['starting_amount'], "Update adjustment for invoice {$model->number} by {$state['starting_amount']}");
|
||||
// $model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
|
||||
}
|
||||
|
||||
if (! $model->design_id) {
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
namespace App\Services\Client;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Utils\Number;
|
||||
use App\Models\Client;
|
||||
use App\Models\Credit;
|
||||
@ -34,6 +35,26 @@ class ClientService
|
||||
{
|
||||
}
|
||||
|
||||
public function calculateBalance()
|
||||
{
|
||||
$balance = Invoice::where('client_id', $this->client->id)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||
->where('is_deleted', false)
|
||||
->sum('balance');
|
||||
|
||||
try {
|
||||
DB::connection(config('database.default'))->transaction(function () use ($balance) {
|
||||
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
|
||||
$this->client->balance = $balance;
|
||||
$this->client->saveQuietly();
|
||||
}, 2);
|
||||
} catch (\Throwable $throwable) {
|
||||
nlog("DB ERROR " . $throwable->getMessage());
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function updateBalance(float $amount)
|
||||
{
|
||||
try {
|
||||
|
@ -13,6 +13,7 @@ namespace App\Services\Ledger;
|
||||
|
||||
use App\Factory\CompanyLedgerFactory;
|
||||
use App\Jobs\Ledger\ClientLedgerBalanceUpdate;
|
||||
use App\Jobs\Ledger\UpdateLedger;
|
||||
use App\Models\Activity;
|
||||
|
||||
class LedgerService
|
||||
@ -24,6 +25,20 @@ class LedgerService
|
||||
$this->entity = $entity;
|
||||
}
|
||||
|
||||
public function mutateInvoiceBalance(float $start_amount, string $notes ='')
|
||||
{
|
||||
$company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id);
|
||||
$company_ledger->client_id = $this->entity->client_id;
|
||||
$company_ledger->adjustment = 0;
|
||||
$company_ledger->notes = $notes;
|
||||
$company_ledger->activity_id = Activity::UPDATE_INVOICE;
|
||||
$company_ledger->save();
|
||||
|
||||
$this->entity->company_ledger()->save($company_ledger);
|
||||
|
||||
UpdateLedger::dispatch($company_ledger->id, $start_amount, $this->entity->company->company_key, $this->entity->company->db);
|
||||
}
|
||||
|
||||
public function updateInvoiceBalance($adjustment, $notes = '')
|
||||
{
|
||||
$company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id);
|
||||
|
Loading…
x
Reference in New Issue
Block a user