public function createNinjaCredit($client, $amount) { $account = $this->getNinjaAccount(); $lastCredit = Credit::withTrashed()->whereAccountId($account->id)->orderBy('public_id', 'DESC')->first(); $publicId = $lastCredit ? $lastCredit->public_id + 1 : 1; $credit = new Credit(); $credit->public_id = $publicId; $credit->account_id = $account->id; $credit->user_id = $account->users()->first()->id; $credit->client_id = $client->id; $credit->amount = $amount; $credit->save(); return $credit; }
/** * Record Credits * * @return bool * @throws \Exception */ private function recordCredits() { $user = User::find($this->requestor_id); $credit = $user->credit; if ($credit) { $credit->amount += $this->amount; } else { $credit = new Credit(); $credit->user_id = $user->id; $credit->amount = $this->amount; } if ($credit->save()) { $creditLog = new CreditLog(); $creditLog->user_id = $user->id; $creditLog->credit_id = $credit->id; $creditLog->amount = $this->amount; $creditLog->type = CreditLog::TYPE_EARNED; if (!$creditLog->save()) { throw new \Exception('Error saving credit log!'); } } else { throw new \Exception('Error saving credit!'); } return true; }