company_ledger_id); MultiDB::setDb($this->db); $cl = CompanyLedger::find($this->company_ledger_id); $ledger_item = CompanyLedger::query() ->where('company_id', $cl->company_id) ->where('client_id', $cl->client_id) ->where('company_ledgerable_id', $cl->company_ledgerable_id) ->where('company_ledgerable_type', $cl->company_ledgerable_type) ->exists(); if(!$cl) return; $entity = $cl->company_ledgerable; $balance = $entity->calc()->getBalance(); $cl->adjustment = $ledger_item ? $balance - $this->start_amount : $balance; $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(); $cl->balance = ($parent_ledger ? $parent_ledger->balance : 0) + $cl->adjustment; $cl->save(); } public function middleware() { return [new WithoutOverlapping($this->company_key)]; } }