payment = $payment; $this->adjustment = $adjustment; $this->company = $company; } /** * Execute the job. * * @return void */ public function handle() { MultiDB::setDB($this->company->db); $balance = 0; /* Get the last record for the client and set the current balance*/ $ledger = CompanyLedger::whereClientId($this->payment->client_id) ->whereCompanyId($this->payment->company_id) ->orderBy('id', 'DESC') ->first(); if($ledger) $balance = $ledger->balance; $company_ledger = CompanyLedgerFactory::create($this->payment->company_id, $this->payment->user_id); $company_ledger->client_id = $this->payment->client_id; $company_ledger->adjustment = $this->adjustment; $company_ledger->balance = $balance + $this->adjustment; $company_ledger->save(); $this->payment->company_ledger()->save($company_ledger); //todo add model directive here } }