mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 15:54:37 -04:00
Cleanup
This commit is contained in:
parent
7325a66c47
commit
f0013653c7
@ -46,27 +46,27 @@ class ClientLedgerBalanceUpdate implements ShouldQueue
|
|||||||
{
|
{
|
||||||
$uuid = \Illuminate\Support\Str::uuid();
|
$uuid = \Illuminate\Support\Str::uuid();
|
||||||
|
|
||||||
nlog("Updating company ledger for client {$this->client->id} {$uuid}");
|
// nlog("Updating company ledger for client {$this->client->id} {$uuid}");
|
||||||
|
|
||||||
MultiDB::setDb($this->company->db);
|
MultiDB::setDb($this->company->db);
|
||||||
|
|
||||||
$dupes = CompanyLedger::query()
|
// $dupes = CompanyLedger::query()
|
||||||
->where('client_id', $this->client->id)
|
// ->where('client_id', $this->client->id)
|
||||||
->where('balance', 0)
|
// ->where('balance', 0)
|
||||||
->where('hash', '<>', '')
|
// ->where('hash', '<>', '')
|
||||||
->groupBy(['adjustment','hash'])
|
// ->groupBy(['adjustment','hash'])
|
||||||
->havingRaw('COUNT(*) > 1')
|
// ->havingRaw('COUNT(*) > 1')
|
||||||
->pluck('id');
|
// ->pluck('id');
|
||||||
|
|
||||||
// CompanyLedger::query()->whereIn('id', $dupes)->delete();
|
// CompanyLedger::query()->whereIn('id', $dupes)->delete();
|
||||||
|
|
||||||
$dupes = CompanyLedger::query()
|
// $dupes = CompanyLedger::query()
|
||||||
->where('client_id', $this->client->id)
|
// ->where('client_id', $this->client->id)
|
||||||
->where('balance', 0)
|
// ->where('balance', 0)
|
||||||
->where('hash', '<>', '')
|
// ->where('hash', '<>', '')
|
||||||
->groupBy(['adjustment','hash'])
|
// ->groupBy(['adjustment','hash'])
|
||||||
->havingRaw('COUNT(*) > 1')
|
// ->havingRaw('COUNT(*) > 1')
|
||||||
->pluck('id');
|
// ->pluck('id');
|
||||||
|
|
||||||
// CompanyLedger::query()->whereIn('id', $dupes)->delete();
|
// CompanyLedger::query()->whereIn('id', $dupes)->delete();
|
||||||
|
|
||||||
@ -85,37 +85,17 @@ class ClientLedgerBalanceUpdate implements ShouldQueue
|
|||||||
->orderBy('id', 'DESC')
|
->orderBy('id', 'DESC')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
// if ($company_ledger->balance == 0) {
|
|
||||||
|
|
||||||
// $last_record = CompanyLedger::query()
|
|
||||||
// ->where('client_id', $company_ledger->client_id)
|
|
||||||
// ->where('company_id', $company_ledger->company_id)
|
|
||||||
// ->where('balance', '!=', 0)
|
|
||||||
// ->orderBy('id', 'DESC')
|
|
||||||
// ->first();
|
|
||||||
|
|
||||||
// if (! $last_record) {
|
|
||||||
// $last_record = CompanyLedger::query()
|
|
||||||
// ->where('client_id', $company_ledger->client_id)
|
|
||||||
// ->where('company_id', $company_ledger->company_id)
|
|
||||||
// ->orderBy('id', 'DESC')
|
|
||||||
// ->first();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $company_ledger->balance = $last_record->balance + $company_ledger->adjustment;
|
// $company_ledger->balance = $last_record->balance + $company_ledger->adjustment;
|
||||||
$company_ledger->balance = ($parent_ledger ? $parent_ledger->balance : 0) + $company_ledger->adjustment;
|
$company_ledger->balance = ($parent_ledger ? $parent_ledger->balance : 0) + $company_ledger->adjustment;
|
||||||
$company_ledger->save();
|
$company_ledger->save();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
nlog("finished job {$uuid}");
|
// nlog("finished job {$uuid}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function middleware()
|
public function middleware()
|
||||||
{
|
{
|
||||||
nlog("middle ware {$this->client->id}");
|
|
||||||
return [(new WithoutOverlapping($this->client->id))->dontRelease()];
|
return [(new WithoutOverlapping($this->client->id))->dontRelease()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,7 +161,7 @@ class BaseRepository
|
|||||||
|
|
||||||
$lcfirst_resource_id = $this->resolveEntityKey($model); //ie invoice_id
|
$lcfirst_resource_id = $this->resolveEntityKey($model); //ie invoice_id
|
||||||
|
|
||||||
$state['starting_amount'] = $model->amount;
|
$state['starting_amount'] = $model->balance;
|
||||||
|
|
||||||
if (! $model->id) {
|
if (! $model->id) {
|
||||||
$company_defaults = $client->setCompanyDefaults($data, lcfirst($resource));
|
$company_defaults = $client->setCompanyDefaults($data, lcfirst($resource));
|
||||||
@ -273,7 +273,7 @@ class BaseRepository
|
|||||||
$model = $model->calc()->getInvoice();
|
$model = $model->calc()->getInvoice();
|
||||||
|
|
||||||
/* We use this to compare to our starting amount */
|
/* We use this to compare to our starting amount */
|
||||||
$state['finished_amount'] = $model->amount;
|
$state['finished_amount'] = $model->balance;
|
||||||
|
|
||||||
/* Apply entity number */
|
/* Apply entity number */
|
||||||
$model = $model->service()->applyNumber()->save();
|
$model = $model->service()->applyNumber()->save();
|
||||||
@ -292,8 +292,12 @@ class BaseRepository
|
|||||||
if ($model instanceof Invoice) {
|
if ($model instanceof Invoice) {
|
||||||
if ($model->status_id != Invoice::STATUS_DRAFT) {
|
if ($model->status_id != Invoice::STATUS_DRAFT) {
|
||||||
$model->service()->updateStatus()->save();
|
$model->service()->updateStatus()->save();
|
||||||
$model->client->service()->calculateBalance();
|
$model->client->service()->calculateBalance($model);
|
||||||
$model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
|
|
||||||
|
// $diff = $state['finished_amount'] - $state['starting_amount'];
|
||||||
|
// nlog("{$diff} - {$state['finished_amount']} - {$state['starting_amount']}");
|
||||||
|
// if(floatval($state['finished_amount']) != floatval($state['starting_amount']))
|
||||||
|
// $model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $model->design_id) {
|
if (! $model->design_id) {
|
||||||
|
@ -35,13 +35,15 @@ class ClientService
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function calculateBalance()
|
public function calculateBalance(?Invoice $invoice = null)
|
||||||
{
|
{
|
||||||
$balance = Invoice::where('client_id', $this->client->id)
|
$balance = Invoice::where('client_id', $this->client->id)
|
||||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||||
->where('is_deleted', false)
|
->where('is_deleted', false)
|
||||||
->sum('balance');
|
->sum('balance');
|
||||||
|
|
||||||
|
$pre_client_balance = $this->client->balance;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DB::connection(config('database.default'))->transaction(function () use ($balance) {
|
DB::connection(config('database.default'))->transaction(function () use ($balance) {
|
||||||
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
|
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
|
||||||
@ -52,9 +54,20 @@ class ClientService
|
|||||||
nlog("DB ERROR " . $throwable->getMessage());
|
nlog("DB ERROR " . $throwable->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($invoice && floatval($this->client->balance) != floatval($pre_client_balance)) {
|
||||||
|
$diff = $this->client->balance - $pre_client_balance;
|
||||||
|
$invoice->ledger()->insertInvoiceBalance($diff, $this->client->balance, "Update Adjustment Invoice # {$invoice->number} => {$diff}");
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seeing too many race conditions under heavy load here.
|
||||||
|
* @deprecated
|
||||||
|
* @param float $amount
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
public function updateBalance(float $amount)
|
public function updateBalance(float $amount)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
@ -26,6 +26,22 @@ class LedgerService
|
|||||||
$this->entity = $entity;
|
$this->entity = $entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function insertInvoiceBalance($adjustment, $balance, $notes)
|
||||||
|
{
|
||||||
|
$company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id);
|
||||||
|
$company_ledger->client_id = $this->entity->client_id;
|
||||||
|
$company_ledger->adjustment = $adjustment;
|
||||||
|
$company_ledger->notes = $notes;
|
||||||
|
$company_ledger->balance = $balance;
|
||||||
|
$company_ledger->activity_id = Activity::UPDATE_INVOICE;
|
||||||
|
$company_ledger->save();
|
||||||
|
|
||||||
|
$this->entity->company_ledger()->save($company_ledger);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function updateInvoiceBalance($adjustment, $notes = '')
|
public function updateInvoiceBalance($adjustment, $notes = '')
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -36,36 +52,34 @@ class LedgerService
|
|||||||
// $hash = sha1($adjustment.$notes.$this->entity->status_id.$this->entity->client_id.$this->entity->amount.$this->entity->balance.$this->entity->company_id.Activity::UPDATE_INVOICE);
|
// $hash = sha1($adjustment.$notes.$this->entity->status_id.$this->entity->client_id.$this->entity->amount.$this->entity->balance.$this->entity->company_id.Activity::UPDATE_INVOICE);
|
||||||
// $hash = sha1($hash);
|
// $hash = sha1($hash);
|
||||||
// $hash = sha1("{$this->entity->amount}.{$this->entity->balance}");
|
// $hash = sha1("{$this->entity->amount}.{$this->entity->balance}");
|
||||||
$hash = "{$adjustment}.{$this->entity->amount}.{$this->entity->balance}";
|
// $hash = "{$adjustment}.{$this->entity->amount}.{$this->entity->balance}";
|
||||||
|
|
||||||
usleep(10000000);
|
// $exists = CompanyLedger::query()
|
||||||
|
// ->where('client_id', $this->entity->client_id)
|
||||||
|
// ->where('company_id', $this->entity->company_id)
|
||||||
|
// ->where('activity_id', Activity::UPDATE_INVOICE)
|
||||||
|
// ->where('adjustment', $adjustment)
|
||||||
|
// ->where('hash', $hash)
|
||||||
|
// ->where('notes', $notes)
|
||||||
|
// ->where('created_at', '>=', now()->subSeconds(1))
|
||||||
|
// ->exists();
|
||||||
|
|
||||||
$exists = CompanyLedger::query()
|
// if($exists) {
|
||||||
->where('client_id', $this->entity->client_id)
|
// nlog("Collision {$adjustment} {$notes}");
|
||||||
->where('company_id', $this->entity->company_id)
|
// return $this;
|
||||||
->where('activity_id', Activity::UPDATE_INVOICE)
|
// }
|
||||||
->where('adjustment', $adjustment)
|
|
||||||
->where('hash', $hash)
|
|
||||||
->where('notes', $notes)
|
|
||||||
->where('created_at', '>=', now()->subSeconds(1))
|
|
||||||
->exists();
|
|
||||||
|
|
||||||
if($exists) {
|
|
||||||
nlog("Collision {$adjustment} {$notes}");
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
$company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id);
|
$company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id);
|
||||||
$company_ledger->client_id = $this->entity->client_id;
|
$company_ledger->client_id = $this->entity->client_id;
|
||||||
$company_ledger->adjustment = $adjustment;
|
$company_ledger->adjustment = $adjustment;
|
||||||
$company_ledger->notes = $notes;
|
$company_ledger->notes = $notes;
|
||||||
$company_ledger->hash = $hash;
|
// $company_ledger->hash = $hash;
|
||||||
$company_ledger->activity_id = Activity::UPDATE_INVOICE;
|
$company_ledger->activity_id = Activity::UPDATE_INVOICE;
|
||||||
$company_ledger->save();
|
$company_ledger->save();
|
||||||
|
|
||||||
$this->entity->company_ledger()->save($company_ledger);
|
$this->entity->company_ledger()->save($company_ledger);
|
||||||
|
|
||||||
ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(rand(4, 7));
|
ClientLedgerBalanceUpdate::dispatch($this->entity->company, $this->entity->client)->delay(rand(3, 7));
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user