credit = $credit; $this->payment = $payment; $this->amount = $amount; $this->company = $company; } /** * Execute the job. * * * @return void */ public function handle() { MultiDB::setDB($this->company->db); /* Update Pivot Record amount */ $this->payment->credits->each(function ($cred) { if ($cred->id == $this->credit->id) { $cred->pivot->amount = $this->amount; $cred->pivot->save(); } }); $credit_balance = $this->credit->balance; if ($this->amount == $credit_balance) { //total credit applied. $this->credit->setStatus(Credit::STATUS_APPLIED); $this->credit->updateBalance($this->amount*-1); } elseif($this->amount < $credit_balance) { //compare number appropriately $this->credit->setStatus(Credit::PARTIAL); $this->credit->updateBalance($this->amount*-1); } /* Update Payment Applied Amount*/ $this->payment->save(); } }