public function run() { DB::table('account_types')->delete(); AccountType::create(['type' => 'Default account', 'editable' => true]); AccountType::create(['type' => 'Cash account', 'editable' => false]); AccountType::create(['type' => 'Asset account', 'editable' => true]); AccountType::create(['type' => 'Expense account', 'editable' => true]); AccountType::create(['type' => 'Revenue account', 'editable' => true]); AccountType::create(['type' => 'Initial balance account', 'editable' => false]); AccountType::create(['type' => 'Beneficiary account', 'editable' => true]); AccountType::create(['type' => 'Import account', 'editable' => false]); }
/** * @return Account|null */ public function convert() { // is mapped? Then it's easy! if (isset($this->mapped[$this->index][$this->value])) { $account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]); return $account; } if (strlen($this->value) > 0) { // find or create new account: $account = $this->findAccount(); $accountType = AccountType::where('type', 'Asset account')->first(); if (is_null($account)) { // create it if doesn't exist. $account = Account::firstOrCreateEncrypted(['name' => $this->value, 'iban' => $this->value, 'user_id' => Auth::user()->id, 'account_type_id' => $accountType->id, 'active' => 1]); } return $account; } return null; }
/** * @return Account|null */ public function convert() { // is mapped? Then it's easy! if (isset($this->mapped[$this->index][$this->value])) { $account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]); return $account; } // find or create new account: $accountType = AccountType::where('type', 'Asset account')->first(); $set = Auth::user()->accounts()->accountTypeIn(['Asset account', 'Default account'])->get(); /** @var Account $entry */ foreach ($set as $entry) { if ($entry->name == $this->value) { return $entry; } } // create it if doesnt exist. $account = Account::firstOrCreateEncrypted(['name' => $this->value, 'iban' => '', 'user_id' => Auth::user()->id, 'account_type_id' => $accountType->id, 'active' => 1]); return $account; }
/** * */ public function createRevenueAccounts() { $user = User::whereEmail('*****@*****.**')->first(); $revenueType = AccountType::whereType('Revenue account')->first(); Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'Employer', 'active' => 1]); Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'IRS', 'active' => 1]); Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'Second job employer', 'active' => 1]); }
/** * @param $value * @param $parameters * * @return bool */ protected function validateByAccountTypeId($value, $parameters) { $type = AccountType::find($this->data['account_type_id'])->first(); $ignore = isset($parameters[0]) ? intval($parameters[0]) : 0; $value = $this->tryDecrypt($value); $set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get(); /** @var Account $entry */ foreach ($set as $entry) { if ($entry->name == $value) { return false; } } return true; }
/** * @param $value * @param $parameters * * @return bool */ private function validateByAccountTypeString($value, $parameters) : bool { $search = Config::get('firefly.accountTypeByIdentifier.' . $this->data['what']); $type = AccountType::whereType($search)->first(); $ignore = $parameters[0] ?? 0; $set = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get(); /** @var Account $entry */ foreach ($set as $entry) { if ($entry->name == $value) { return false; } } return true; }
/** * @param array $data * * @return Account */ protected function storeAccount(array $data) { $type = Config::get('firefly.accountTypeByIdentifier.' . $data['accountType']); $accountType = AccountType::whereType($type)->first(); $newAccount = new Account(['user_id' => $data['user'], 'account_type_id' => $accountType->id, 'name' => $data['name'], 'virtual_balance' => $data['virtualBalance'], 'active' => $data['active'] === true ? true : false, 'iban' => $data['iban']]); if (!$newAccount->isValid()) { // does the account already exist? $searchData = ['user_id' => $data['user'], 'account_type_id' => $accountType->id, 'virtual_balance' => $data['virtualBalance'], 'name' => $data['name'], 'iban' => $data['iban']]; $existingAccount = Account::firstOrNullEncrypted($searchData); if (!$existingAccount) { Log::error('Account create error: ' . $newAccount->getErrors()->toJson()); abort(500); // @codeCoverageIgnoreStart } // @codeCoverageIgnoreEnd $newAccount = $existingAccount; } $newAccount->save(); return $newAccount; }
/** * @param array $data * * @return array */ private function storeWithdrawalAccounts(array $data) : array { $sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['source_account_id'])->first(['accounts.*']); if (strlen($data['destination_account_name']) > 0) { $destinationType = AccountType::where('type', 'Expense account')->first(); $destinationAccount = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $destinationType->id, 'name' => $data['destination_account_name'], 'active' => 1]); return [$sourceAccount, $destinationAccount]; } $destinationType = AccountType::where('type', 'Cash account')->first(); $destinationAccount = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $destinationType->id, 'name' => 'Cash account', 'active' => 1]); return [$sourceAccount, $destinationAccount]; }
/** * * @return AccountType */ protected function getAccountType() { return AccountType::where('type', 'Asset account')->first(); }
/** * @param array $data * * @return array */ protected function storeDepositAccounts(array $data) { $toAccount = Account::find($data['account_id']); if (strlen($data['revenue_account']) > 0) { $fromType = AccountType::where('type', 'Revenue account')->first(); $fromAccount = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $fromType->id, 'name' => $data['revenue_account'], 'active' => 1]); } else { $toType = AccountType::where('type', 'Cash account')->first(); $fromAccount = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]); } return [$fromAccount, $toAccount]; }
/** * * @return AccountType */ protected function getAccountType() { // opposing account type: if ($this->data['amount'] < 0) { // create expense account: return AccountType::where('type', 'Expense account')->first(); } else { // create revenue account: return AccountType::where('type', 'Revenue account')->first(); } }