Esempio n. 1
0
 /**
  * @covers FireflyIII\Models\Account::firstOrCreateEncrypted
  */
 public function testFirstOrCreateEncryptedNew()
 {
     // create account:
     $account = FactoryMuffin::create('FireflyIII\\Models\\Account');
     FactoryMuffin::create('FireflyIII\\User');
     // search for account with the same properties:
     $search = ['name' => 'Some new account', 'account_type_id' => $account->account_type_id, 'user_id' => $account->user_id, 'active' => 1];
     $result = Account::firstOrCreateEncrypted($search);
     // should not be the same account:
     $this->assertNotEquals($account->id, $result->id);
 }
 /**
  * @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;
 }
 /**
  * @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];
 }
Esempio n. 5
0
 /**
  * @return Account|null
  */
 protected function parseNameString()
 {
     $accountType = $this->getAccountType();
     $accounts = Auth::user()->accounts()->where('account_type_id', $accountType->id)->get();
     foreach ($accounts as $entry) {
         if ($entry->name == $this->data['asset-account-name']) {
             Log::debug('Found an asset account with this name (#' . $entry->id . ': ******)');
             return $entry;
         }
     }
     // create if not exists:
     $account = Account::firstOrCreateEncrypted(['user_id' => Auth::user()->id, 'account_type_id' => $accountType->id, 'name' => $this->data['asset-account-name'], 'iban' => '', 'active' => true]);
     return $account;
 }
Esempio n. 6
0
 /**
  * @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];
 }