Skip to content

Firefly III: a personal finances manager

Update repositories, remove references to admin id

James Cole75e5115aa339
diff --git a/app/Api/V2/Controllers/Autocomplete/AccountController.php b/app/Api/V2/Controllers/Autocomplete/AccountController.php
index cfab89a40b..7d06c0a5de 100644
--- a/app/Api/V2/Controllers/Autocomplete/AccountController.php
+++ b/app/Api/V2/Controllers/Autocomplete/AccountController.php
@@ -67,9 +67,9 @@ class AccountController extends Controller
* TODO list of checks
* 1. use dates from ParameterBag
* 2. Request validates dates
- * 3. Request includes user_group_id as administration_id
+ * 3. Request includes user_group_id
* 4. Endpoint is documented.
- * 5. Collector uses administration_id
+ * 5. Collector uses user_group_id
*
* @param AutocompleteRequest $request
*
diff --git a/app/Api/V2/Controllers/Chart/AccountController.php b/app/Api/V2/Controllers/Chart/AccountController.php
index aa5a5bef7a..b45fa09e9f 100644
--- a/app/Api/V2/Controllers/Chart/AccountController.php
+++ b/app/Api/V2/Controllers/Chart/AccountController.php
@@ -76,7 +76,7 @@ class AccountController extends Controller
* If a transaction has foreign currency = native currency, the foreign amount will be used, no conversion
* will take place.
*
- * TODO validate and set administration_id from request
+ * TODO validate and set user_group_id from request
*
* @param DateRequest $request
*
diff --git a/app/Api/V2/Controllers/Chart/BalanceController.php b/app/Api/V2/Controllers/Chart/BalanceController.php
index 6c56e97313..8ba070f627 100644
--- a/app/Api/V2/Controllers/Chart/BalanceController.php
+++ b/app/Api/V2/Controllers/Chart/BalanceController.php
@@ -71,7 +71,7 @@ class BalanceController extends Controller
* If the transaction being processed is already in native currency OR if the
* foreign amount is in the native currency, the amount will not be converted.
*
- * TODO validate and set administration_id
+ * TODO validate and set user_group_id
* TODO collector set group, not user
*
* @param BalanceChartRequest $request
diff --git a/app/Api/V2/Controllers/Model/Bill/ShowController.php b/app/Api/V2/Controllers/Model/Bill/ShowController.php
index 992524d430..33fece0c92 100644
--- a/app/Api/V2/Controllers/Model/Bill/ShowController.php
+++ b/app/Api/V2/Controllers/Model/Bill/ShowController.php
@@ -52,6 +52,7 @@ class ShowController extends Controller
function ($request, $next) {
$this->repository = app(BillRepositoryInterface::class);
+ // new way of user group validation
$userGroup = $this->validateUserGroup($request);
if (null !== $userGroup) {
$this->repository->setUserGroup($userGroup);
diff --git a/app/Api/V2/Controllers/Model/Transaction/StoreController.php b/app/Api/V2/Controllers/Model/Transaction/StoreController.php
index 01aebcb56a..dc14f8b22e 100644
--- a/app/Api/V2/Controllers/Model/Transaction/StoreController.php
+++ b/app/Api/V2/Controllers/Model/Transaction/StoreController.php
@@ -62,8 +62,6 @@ class StoreController extends Controller
/**
* TODO this method is practically the same as the V1 method and borrows as much code as possible.
- * TODO still it duplicates a lot.
- * TODO the v1 endpoints will never support separate administrations, this is an important distinction.
*
* @return JsonResponse
* @throws FireflyException
@@ -74,7 +72,6 @@ class StoreController extends Controller
app('log')->debug('Now in API v2 StoreController::store()');
$data = $request->getAll();
- $data['user'] = auth()->user()->id;
$userGroup = $request->getUserGroup();
$data['user_group'] = $userGroup;
diff --git a/app/Api/V2/Controllers/Summary/NetWorthController.php b/app/Api/V2/Controllers/Summary/NetWorthController.php
index 7700a135d7..553721d3f6 100644
--- a/app/Api/V2/Controllers/Summary/NetWorthController.php
+++ b/app/Api/V2/Controllers/Summary/NetWorthController.php
@@ -26,8 +26,9 @@ namespace FireflyIII\Api\V2\Controllers\Summary;
use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Generic\SingleDateRequest;
-use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Report\NetWorthInterface;
+use FireflyIII\Support\Http\Api\ConvertsExchangeRates;
+use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
use Illuminate\Http\JsonResponse;
/**
@@ -35,6 +36,9 @@ use Illuminate\Http\JsonResponse;
*/
class NetWorthController extends Controller
{
+ use ValidatesUserGroupTrait;
+ use ConvertsExchangeRates;
+
private NetWorthInterface $netWorth;
/**
@@ -46,7 +50,12 @@ class NetWorthController extends Controller
$this->middleware(
function ($request, $next) {
$this->netWorth = app(NetWorthInterface::class);
- $this->netWorth->setUser(auth()->user());
+
+ // new way of user group validation
+ $userGroup = $this->validateUserGroup($request);
+ if (null !== $userGroup) {
+ $this->netWorth->setUserGroup($userGroup);
+ }
return $next($request);
}
@@ -63,7 +72,6 @@ class NetWorthController extends Controller
*/
public function get(SingleDateRequest $request): JsonResponse
{
- throw new FireflyException('deprecated use of thing.');
$date = $request->getDate();
$result = $this->netWorth->sumNetWorthByCurrency($date);
$converted = $this->cerSum($result);
diff --git a/app/Api/V2/Request/Autocomplete/AutocompleteRequest.php b/app/Api/V2/Request/Autocomplete/AutocompleteRequest.php
index 8a73be5eac..35b63cd42c 100644
--- a/app/Api/V2/Request/Autocomplete/AutocompleteRequest.php
+++ b/app/Api/V2/Request/Autocomplete/AutocompleteRequest.php
@@ -24,13 +24,10 @@ declare(strict_types=1);
namespace FireflyIII\Api\V2\Request\Autocomplete;
use FireflyIII\Enums\UserRoleEnum;
-use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\AccountType;
-use FireflyIII\Models\UserRole;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\User;
-use FireflyIII\Validation\Administration\ValidatesAdministrationAccess;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
@@ -39,13 +36,12 @@ use Illuminate\Validation\Validator;
*/
class AutocompleteRequest extends FormRequest
{
+ protected array $acceptedRoles = [UserRoleEnum::MANAGE_TRANSACTIONS];
use ConvertsDataTypes;
use ChecksLogin;
- use ValidatesAdministrationAccess;
/**
* @return array
- * @throws FireflyException
*/
public function getData(): array
{
@@ -63,11 +59,10 @@ class AutocompleteRequest extends FormRequest
$user = auth()->user();
return [
- 'types' => $array,
- 'query' => $this->convertString('query'),
- 'date' => $this->getCarbonDate('date'),
- 'limit' => $limit,
- 'administration_id' => (int)($this->get('administration_id', null) ?? $user->getAdministrationId()),
+ 'types' => $array,
+ 'query' => $this->convertString('query'),
+ 'date' => $this->getCarbonDate('date'),
+ 'limit' => $limit,
];
}
diff --git a/app/Helpers/Report/NetWorth.php b/app/Helpers/Report/NetWorth.php
index 5b089c6159..f8505c7321 100644
--- a/app/Helpers/Report/NetWorth.php
+++ b/app/Helpers/Report/NetWorth.php
@@ -29,8 +29,8 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\UserGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
-use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface as AdminAccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
+use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface as AdminAccountRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\User;
@@ -39,6 +39,8 @@ use Illuminate\Support\Collection;
use JsonException;
/**
+ * This class can handle both request with and without a user group and will return the appropriate repository when
+ * necessary.
*
* Class NetWorth
*/
@@ -49,7 +51,7 @@ class NetWorth implements NetWorthInterface
private CurrencyRepositoryInterface $currencyRepos;
private User $user;
- private UserGroup $userGroup;
+ private null | UserGroup $userGroup;
/**
* @param Collection $accounts
@@ -67,7 +69,7 @@ class NetWorth implements NetWorthInterface
$cache->addProperty('net-worth-by-accounts');
$cache->addProperty($ids);
if ($cache->has()) {
- //return $cache->get();
+ return $cache->get();
}
app('log')->debug(sprintf('Now in byAccounts("%s", "%s")', $ids, $date->format('Y-m-d')));
@@ -96,7 +98,7 @@ class NetWorth implements NetWorthInterface
/** @var Account $account */
foreach ($accounts as $account) {
app('log')->debug(sprintf('Now at account #%d ("%s")', $account->id, $account->name));
- $currency = $this->adminAccountRepository->getAccountCurrency($account);
+ $currency = $this->getRepository()->getAccountCurrency($account);
$currencyId = (int)$currency->id;
$balance = '0';
$nativeBalance = '0';
@@ -137,6 +139,17 @@ class NetWorth implements NetWorthInterface
return $netWorth;
}
+ /**
+ * @return AdminAccountRepositoryInterface|AccountRepositoryInterface
+ */
+ private function getRepository(): AdminAccountRepositoryInterface | AccountRepositoryInterface
+ {
+ if (null === $this->userGroup) {
+ return $this->accountRepository;
+ }
+ return $this->adminAccountRepository;
+ }
+
/**
* Returns the user's net worth in an array with the following layout:
*
@@ -181,7 +194,7 @@ class NetWorth implements NetWorthInterface
/** @var Account $account */
foreach ($accounts as $account) {
// Log::debug(sprintf('Now at account #%d: "%s"', $account->id, $account->name));
- $currencyId = (int)$this->accountRepository->getMetaValue($account, 'currency_id');
+ $currencyId = (int)$this->getRepository()->getMetaValue($account, 'currency_id');
$currencyId = 0 === $currencyId ? $default->id : $currencyId;
// Log::debug(sprintf('Currency ID is #%d', $currencyId));
@@ -227,7 +240,8 @@ class NetWorth implements NetWorthInterface
if (null === $user) {
return;
}
- $this->user = $user;
+ $this->user = $user;
+ $this->userGroup = null;
// make repository:
$this->accountRepository = app(AccountRepositoryInterface::class);
@@ -239,7 +253,6 @@ class NetWorth implements NetWorthInterface
/**
* @inheritDoc
- * @throws FireflyException
*/
public function setUserGroup(UserGroup $userGroup): void
{
@@ -260,7 +273,7 @@ class NetWorth implements NetWorthInterface
$return = [];
$balances = app('steam')->balancesByAccounts($accounts, $date);
foreach ($accounts as $account) {
- $currency = $this->accountRepository->getAccountCurrency($account);
+ $currency = $this->getRepository()->getAccountCurrency($account);
$balance = $balances[$account->id] ?? '0';
// always subtract virtual balance.
@@ -288,13 +301,13 @@ class NetWorth implements NetWorthInterface
*/
private function getAccounts(): Collection
{
- $accounts = $this->accountRepository->getAccountsByType(
+ $accounts = $this->getRepository()->getAccountsByType(
[AccountType::ASSET, AccountType::DEFAULT, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]
);
$filtered = new Collection();
/** @var Account $account */
foreach ($accounts as $account) {
- if (1 === (int)$this->accountRepository->getMetaValue($account, 'include_net_worth')) {
+ if (1 === (int)$this->getRepository()->getMetaValue($account, 'include_net_worth')) {
$filtered->push($account);
}
}
diff --git a/app/Http/Requests/AccountFormRequest.php b/app/Http/Requests/AccountFormRequest.php
index 95896d7579..cff35136c8 100644
--- a/app/Http/Requests/AccountFormRequest.php
+++ b/app/Http/Requests/AccountFormRequest.php
@@ -26,24 +26,21 @@ namespace FireflyIII\Http\Requests;
use FireflyIII\Enums\UserRoleEnum;
use FireflyIII\Models\Account;
use FireflyIII\Models\Location;
-use FireflyIII\Models\UserRole;
use FireflyIII\Rules\UniqueIban;
use FireflyIII\Support\Request\AppendsLocationData;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
-use FireflyIII\Validation\Administration\ValidatesAdministrationAccess;
use Illuminate\Foundation\Http\FormRequest;
-use Illuminate\Validation\Validator;
/**
* Class AccountFormRequest.
*/
class AccountFormRequest extends FormRequest
{
+ protected array $acceptedRoles = [UserRoleEnum::MANAGE_TRANSACTIONS];
use ConvertsDataTypes;
use AppendsLocationData;
use ChecksLogin;
- use ValidatesAdministrationAccess;
/**
* Get all data.
@@ -53,7 +50,6 @@ class AccountFormRequest extends FormRequest
public function getAccountData(): array
{
$data = [
- 'administration_id' => $this->convertInteger('administration_id'),
'name' => $this->convertString('name'),
'active' => $this->boolean('active'),
'account_type_name' => $this->convertString('objectType'),
@@ -73,9 +69,6 @@ class AccountFormRequest extends FormRequest
'include_net_worth' => '1',
'liability_direction' => $this->convertString('liability_direction'),
];
- if (0 === $data['administration_id']) {
- $data['administration_id'] = auth()->user()->getAdministrationId();
- }
$data = $this->appendLocationData($data, 'location');
if (false === $this->boolean('include_net_worth')) {
@@ -110,7 +103,6 @@ class AccountFormRequest extends FormRequest
$types = implode(',', array_keys(config('firefly.subTitlesByIdentifier')));
$ccPaymentTypes = implode(',', array_keys(config('firefly.ccTypes')));
$rules = [
- 'administration_id' => 'min:1|max:16777216|numeric',
'name' => 'required|max:1024|min:1|uniqueAccountForUser',
'opening_balance' => 'numeric|nullable|max:1000000000',
'opening_balance_date' => 'date|required_with:opening_balance|nullable',
@@ -140,21 +132,4 @@ class AccountFormRequest extends FormRequest
return $rules;
}
-
- /**
- * Configure the validator instance with special rules for after the basic validation rules.
- *
- * @param Validator $validator
- *
- * @return void
- */
- public function withValidator(Validator $validator): void
- {
- $validator->after(
- function (Validator $validator) {
- // validate if the account can access this administration
- $this->validateAdministration($validator, [UserRoleEnum::MANAGE_TRANSACTIONS->value]);
- }
- );
- }
}
diff --git a/app/Repositories/UserGroups/Account/AccountRepository.php b/app/Repositories/UserGroups/Account/AccountRepository.php
index 9b7acaf693..061ad83872 100644
--- a/app/Repositories/UserGroups/Account/AccountRepository.php
+++ b/app/Repositories/UserGroups/Account/AccountRepository.php
@@ -32,6 +32,7 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\Log;
/**
* Class AccountRepository
@@ -40,6 +41,32 @@ class AccountRepository implements AccountRepositoryInterface
{
use UserGroupTrait;
+ /**
+ * @inheritDoc
+ */
+ public function findByName(string $name, array $types): ?Account
+ {
+ $query = $this->userGroup->accounts();
+
+ if (0 !== count($types)) {
+ $query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
+ $query->whereIn('account_types.type', $types);
+ }
+ Log::debug(sprintf('Searching for account named "%s" (of user #%d) of the following type(s)', $name, $this->user->id), ['types' => $types]);
+
+ $query->where('accounts.name', $name);
+ /** @var Account $account */
+ $account = $query->first(['accounts.*']);
+ if (null === $account) {
+ Log::debug(sprintf('There is no account with name "%s" of types', $name), $types);
+
+ return null;
+ }
+ Log::debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id));
+
+ return $account;
+ }
+
/**
* @param Account $account
*
diff --git a/app/Repositories/UserGroups/Account/AccountRepositoryInterface.php b/app/Repositories/UserGroups/Account/AccountRepositoryInterface.php
index 2e3bbc6c1c..347baff32f 100644
--- a/app/Repositories/UserGroups/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/UserGroups/Account/AccountRepositoryInterface.php
@@ -41,6 +41,14 @@ interface AccountRepositoryInterface
*/
public function find(int $accountId): ?Account;
+ /**
+ * @param string $name
+ * @param array $types
+ *
+ * @return Account|null
+ */
+ public function findByName(string $name, array $types): ?Account;
+
/**
* @param Account $account
*
diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php
index 2d10f04d24..790ec698eb 100644
--- a/app/Transformers/UserTransformer.php
+++ b/app/Transformers/UserTransformer.php
@@ -48,15 +48,14 @@ class UserTransformer extends AbstractTransformer
$this->repository = $this->repository ?? app(UserRepositoryInterface::class);
return [
- 'id' => (int)$user->id,
- 'administration_id' => (string)$user->getAdministrationId(),
- 'created_at' => $user->created_at->toAtomString(),
- 'updated_at' => $user->updated_at->toAtomString(),
- 'email' => $user->email,
- 'blocked' => 1 === (int)$user->blocked,
- 'blocked_code' => '' === $user->blocked_code ? null : $user->blocked_code,
- 'role' => $this->repository->getRoleByUser($user),
- 'links' => [
+ 'id' => (int)$user->id,
+ 'created_at' => $user->created_at->toAtomString(),
+ 'updated_at' => $user->updated_at->toAtomString(),
+ 'email' => $user->email,
+ 'blocked' => 1 === (int)$user->blocked,
+ 'blocked_code' => '' === $user->blocked_code ? null : $user->blocked_code,
+ 'role' => $this->repository->getRoleByUser($user),
+ 'links' => [
[
'rel' => 'self',
'uri' => '/users/' . $user->id,
diff --git a/app/Validation/Administration/ValidatesAdministrationAccess.php b/app/Validation/Administration/ValidatesAdministrationAccess.php
index 61a13ccf64..ecd88233ec 100644
--- a/app/Validation/Administration/ValidatesAdministrationAccess.php
+++ b/app/Validation/Administration/ValidatesAdministrationAccess.php
@@ -34,6 +34,7 @@ use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
/**
+ * @deprecated
* Trait ValidatesAdministrationAccess
*/
trait ValidatesAdministrationAccess
@@ -45,6 +46,7 @@ trait ValidatesAdministrationAccess
* @return void
* @throws AuthenticationException
* @throws FireflyException
+ * @deprecated
*/
protected function validateAdministration(Validator $validator, array $allowedRoles): void
{
diff --git a/package-lock.json b/package-lock.json
index e7e16191bb..5b325d1103 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,7 +9,7 @@
"@popperjs/core": "^2.11.8",
"alpinejs": "^3.13.0",
"bootstrap": "^5.3.0",
- "bootstrap5-autocomplete": "^1.1.20",
+ "bootstrap5-autocomplete": "^1.1.22",
"chart.js": "^4.4.0",
"chartjs-adapter-date-fns": "^3.0.0",
"chartjs-chart-sankey": "^0.12.0",
diff --git a/package.json b/package.json
index 393801fa53..c170c8dfae 100644
--- a/package.json
+++ b/package.json
@@ -17,7 +17,7 @@
"@popperjs/core": "^2.11.8",
"alpinejs": "^3.13.0",
"bootstrap": "^5.3.0",
- "bootstrap5-autocomplete": "^1.1.20",
+ "bootstrap5-autocomplete": "^1.1.22",
"chart.js": "^4.4.0",
"chartjs-adapter-date-fns": "^3.0.0",
"chartjs-chart-sankey": "^0.12.0",
diff --git a/resources/assets/js/components/transactions/CreateTransaction.vue b/resources/assets/js/components/transactions/CreateTransaction.vue
index d16c16c1b6..e233fe84fe 100644
--- a/resources/assets/js/components/transactions/CreateTransaction.vue
+++ b/resources/assets/js/components/transactions/CreateTransaction.vue
@@ -19,984 +19,995 @@
-->
<template>
- <form accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data">
- <input name="_token" type="hidden" value="xxx">
- <div v-if="error_message !== ''" class="row">
- <div class="col-lg-12">
- <div class="alert alert-danger alert-dismissible" role="alert">
- <button class="close" data-dismiss="alert" type="button" v-bind:aria-label="$t('firefly.close')"><span
- aria-hidden="true">&times;</span></button>
- <strong>{{ $t("firefly.flash_error") }}</strong> {{ error_message }}
+ <form accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data">
+ <input name="_token" type="hidden" value="xxx">
+ <div v-if="error_message !== ''" class="row">
+ <div class="col-lg-12">
+ <div class="alert alert-danger alert-dismissible" role="alert">
+ <button class="close" data-dismiss="alert" type="button"
+ v-bind:aria-label="$t('firefly.close')"><span
+ aria-hidden="true">&times;</span></button>
+ <strong>{{ $t("firefly.flash_error") }}</strong> {{ error_message }}
+ </div>
+ </div>
</div>
- </div>
- </div>
-
- <div v-if="success_message !== ''" class="row">
- <div class="col-lg-12">
- <div class="alert alert-success alert-dismissible" role="alert">
- <button class="close" data-dismiss="alert" type="button" v-bind:aria-label="$t('firefly.close')"><span
- aria-hidden="true">&times;</span></button>
- <strong>{{ $t("firefly.flash_success") }}</strong> <span v-html="success_message"></span>
+
+ <div v-if="success_message !== ''" class="row">
+ <div class="col-lg-12">
+ <div class="alert alert-success alert-dismissible" role="alert">
+ <button class="close" data-dismiss="alert" type="button"
+ v-bind:aria-label="$t('firefly.close')"><span
+ aria-hidden="true">&times;</span></button>
+ <strong>{{ $t("firefly.flash_success") }}</strong> <span v-html="success_message"></span>
+ </div>
+ </div>
</div>
- </div>
- </div>
- <div>
- <div v-for="(transaction, index) in transactions" class="row">
- <div class="col-lg-12">
- <div class="box">
- <div class="box-header with-border">
- <h3 class="box-title splitTitle">
+ <div>
+ <div v-for="(transaction, index) in transactions" class="row">
+ <div class="col-lg-12">
+ <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title splitTitle">
<span v-if="transactions.length > 1">{{ $t('firefly.single_split') }} {{ index + 1 }} / {{
- transactions.length
- }}</span>
- <span v-if="transactions.length === 1">{{ $t('firefly.transaction_journal_information') }}</span>
- </h3>
- <div v-if="transactions.length > 1" class="box-tools pull-right">
- <button class="btn btn-xs btn-danger" type="button" v-on:click="deleteTransaction(index, $event)"><i
- class="fa fa-trash"></i></button>
- </div>
- </div>
- <div class="box-body">
- <div class="row">
- <div id="transaction-info" class="col-lg-4">
- <transaction-description
- v-model="transaction.description"
- :error="transaction.errors.description"
- :index="index"
- >
- </transaction-description>
- <account-select
- :accountName="transaction.source_account.name"
- :accountTypeFilters="transaction.source_account.allowed_types"
- :defaultAccountTypeFilters="transaction.source_account.default_allowed_types"
- :error="transaction.errors.source_account"
- :index="index"
- :transactionType="transactionType"
- inputName="source[]"
- v-bind:inputDescription="$t('firefly.source_account')"
- v-on:clear:value="clearSource(index)"
- v-on:select:account="selectedSourceAccount(index, $event)"
- ></account-select>
- <account-select
- :accountName="transaction.destination_account.name"
- :accountTypeFilters="transaction.destination_account.allowed_types"
- :defaultAccountTypeFilters="transaction.destination_account.default_allowed_types"
- :error="transaction.errors.destination_account"
- :index="index"
- :transactionType="transactionType"
- inputName="destination[]"
- v-bind:inputDescription="$t('firefly.destination_account')"
- v-on:clear:value="clearDestination(index)"
- v-on:select:account="selectedDestinationAccount(index, $event)"
- ></account-select>
- <p v-if="0!== index && (null === transactionType || 'invalid' === transactionType || '' === transactionType)"
- class="text-warning">
- {{ $t('firefly.multi_account_warning_unknown') }}
- </p>
- <p v-if="0!== index && 'Withdrawal' === transactionType" class="text-warning">
- {{ $t('firefly.multi_account_warning_withdrawal') }}
- </p>
- <p v-if="0!== index && 'Deposit' === transactionType" class="text-warning">
- {{ $t('firefly.multi_account_warning_deposit') }}
- </p>
- <p v-if="0!== index && 'Transfer' === transactionType" class="text-warning">
- {{ $t('firefly.multi_account_warning_transfer') }}
- </p>
- <standard-date v-if="0===index"
- v-model="transaction.date"
- :error="transaction.errors.date"
- :index="index"
- >
- </standard-date>
- <div v-if="index===0">
- <transaction-type
- :destination="transaction.destination_account.type"
- :source="transaction.source_account.type"
- v-on:set:transactionType="setTransactionType($event)"
- v-on:act:limitSourceType="limitSourceType($event)"
- v-on:act:limitDestinationType="limitDestinationType($event)"
- ></transaction-type>
- </div>
- </div>
- <div id="amount-info" class="col-lg-4">
- <amount
- v-model="transaction.amount"
- :destination="transaction.destination_account"
- :error="transaction.errors.amount"
- :source="transaction.source_account"
- :transactionType="transactionType"
- ></amount>
- <foreign-amount
- v-model="transaction.foreign_amount"
- :destination="transaction.destination_account"
- :error="transaction.errors.foreign_amount"
- :source="transaction.source_account"
- :transactionType="transactionType"
- v-bind:title="$t('form.foreign_amount')"
- ></foreign-amount>
- </div>
- <div id="optional-info" class="col-lg-4">
- <budget
- v-model="transaction.budget"
- :error="transaction.errors.budget_id"
- :no_budget="$t('firefly.none_in_select_list')"
- :transactionType="transactionType"
- ></budget>
- <category
- v-model="transaction.category"
- :error="transaction.errors.category"
- :transactionType="transactionType"
- ></category>
- <piggy-bank
- v-model="transaction.piggy_bank"
- :error="transaction.errors.piggy_bank"
- :no_piggy_bank="$t('firefly.no_piggy_bank')"
- :transactionType="transactionType"
- ></piggy-bank>
- <tags
- v-model="transaction.tags"
- :error="transaction.errors.tags"
- ></tags>
- <bill
- v-model="transaction.bill"
- :error="transaction.errors.bill_id"
- :no_bill="$t('firefly.none_in_select_list')"
- :transactionType="transactionType"
- ></bill>
- <custom-transaction-fields
- v-model="transaction.custom_fields"
- :error="transaction.errors.custom_errors"
- ></custom-transaction-fields>
+ transactions.length
+ }}</span>
+ <span v-if="transactions.length === 1">{{
+ $t('firefly.transaction_journal_information')
+ }}</span>
+ </h3>
+ <div v-if="transactions.length > 1" class="box-tools pull-right">
+ <button class="btn btn-xs btn-danger" type="button"
+ v-on:click="deleteTransaction(index, $event)"><i
+ class="fa fa-trash"></i></button>
+ </div>
+ </div>
+ <div class="box-body">
+ <div class="row">
+ <div id="transaction-info" class="col-lg-4">
+ <transaction-description
+ v-model="transaction.description"
+ :error="transaction.errors.description"
+ :index="index"
+ >
+ </transaction-description>
+ <account-select
+ :accountName="transaction.source_account.name"
+ :accountTypeFilters="transaction.source_account.allowed_types"
+ :defaultAccountTypeFilters="transaction.source_account.default_allowed_types"
+ :error="transaction.errors.source_account"
+ :index="index"
+ :transactionType="transactionType"
+ inputName="source[]"
+ v-bind:inputDescription="$t('firefly.source_account')"
+ v-on:clear:value="clearSource(index)"
+ v-on:select:account="selectedSourceAccount(index, $event)"
+ ></account-select>
+ <account-select
+ :accountName="transaction.destination_account.name"
+ :accountTypeFilters="transaction.destination_account.allowed_types"
+ :defaultAccountTypeFilters="transaction.destination_account.default_allowed_types"
+ :error="transaction.errors.destination_account"
+ :index="index"
+ :transactionType="transactionType"
+ inputName="destination[]"
+ v-bind:inputDescription="$t('firefly.destination_account')"
+ v-on:clear:value="clearDestination(index)"
+ v-on:select:account="selectedDestinationAccount(index, $event)"
+ ></account-select>
+ <p v-if="0!== index && (null === transactionType || 'invalid' === transactionType || '' === transactionType)"
+ class="text-warning">
+ {{ $t('firefly.multi_account_warning_unknown') }}
+ </p>
+ <p v-if="0!== index && 'Withdrawal' === transactionType" class="text-warning">
+ {{ $t('firefly.multi_account_warning_withdrawal') }}
+ </p>
+ <p v-if="0!== index && 'Deposit' === transactionType" class="text-warning">
+ {{ $t('firefly.multi_account_warning_deposit') }}
+ </p>
+ <p v-if="0!== index && 'Transfer' === transactionType" class="text-warning">
+ {{ $t('firefly.multi_account_warning_transfer') }}
+ </p>
+ <standard-date v-if="0===index"
+ v-model="transaction.date"
+ :error="transaction.errors.date"
+ :index="index"
+ >
+ </standard-date>
+ <div v-if="index===0">
+ <transaction-type
+ :destination="transaction.destination_account.type"
+ :source="transaction.source_account.type"
+ v-on:set:transactionType="setTransactionType($event)"
+ v-on:act:limitSourceType="limitSourceType($event)"
+ v-on:act:limitDestinationType="limitDestinationType($event)"
+ ></transaction-type>
+ </div>
+ </div>
+ <div id="amount-info" class="col-lg-4">
+ <amount
+ v-model="transaction.amount"
+ :destination="transaction.destination_account"
+ :error="transaction.errors.amount"
+ :source="transaction.source_account"
+ :transactionType="transactionType"
+ ></amount>
+ <foreign-amount
+ v-model="transaction.foreign_amount"
+ :destination="transaction.destination_account"
+ :error="transaction.errors.foreign_amount"
+ :source="transaction.source_account"
+ :transactionType="transactionType"
+ v-bind:title="$t('form.foreign_amount')"
+ ></foreign-amount>
+ </div>
+ <div id="optional-info" class="col-lg-4">
+ <budget
+ v-model="transaction.budget"
+ :error="transaction.errors.budget_id"
+ :no_budget="$t('firefly.none_in_select_list')"
+ :transactionType="transactionType"
+ ></budget>
+ <category
+ v-model="transaction.category"
+ :error="transaction.errors.category"
+ :transactionType="transactionType"
+ ></category>
+ <piggy-bank
+ v-model="transaction.piggy_bank"
+ :error="transaction.errors.piggy_bank"
+ :no_piggy_bank="$t('firefly.no_piggy_bank')"
+ :transactionType="transactionType"
+ ></piggy-bank>
+ <tags
+ v-model="transaction.tags"
+ :error="transaction.errors.tags"
+ ></tags>
+ <bill
+ v-model="transaction.bill"
+ :error="transaction.errors.bill_id"
+ :no_bill="$t('firefly.none_in_select_list')"
+ :transactionType="transactionType"
+ ></bill>
+ <custom-transaction-fields
+ v-model="transaction.custom_fields"
+ :error="transaction.errors.custom_errors"
+ ></custom-transaction-fields>
+ </div>
+ </div>
+ </div>
+ <div v-if="transactions.length-1 === index" class="box-footer">
+ <button class="split_add_btn btn btn-default" type="button" @click="addTransactionToArray">
+ {{ $t('firefly.add_another_split') }}
+ </button>
+ </div>
+ </div>
</div>
- </div>
</div>
- <div v-if="transactions.length-1 === index" class="box-footer">
- <button class="split_add_btn btn btn-default" type="button" @click="addTransactionToArray">
- {{ $t('firefly.add_another_split') }}
- </button>
- </div>
- </div>
- </div>
- </div>
- </div>
- <div v-if="transactions.length > 1" class="row">
- <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
- <div class="box">
- <div class="box-header with-border">
- <h3 class="box-title">
- {{ $t('firefly.split_transaction_title') }}
- </h3>
- </div>
- <div class="box-body">
- <group-description
- v-model="group_title"
- :error="group_title_errors"
- ></group-description>
- </div>
</div>
- </div>
- </div>
- <div class="row">
- <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
- <div class="box">
- <div class="box-header with-border">
- <h3 class="box-title">
- {{ $t('firefly.submission') }}
- </h3>
- </div>
- <div class="box-body">
- <div class="checkbox">
- <label>
- <input v-model="createAnother" name="create_another" type="checkbox">
- {{ $t('firefly.create_another') }}
- </label>
- </div>
- <div class="checkbox">
- <label v-bind:class="{ 'text-muted': this.createAnother === false}">
- <input v-model="resetFormAfter" :disabled="this.createAnother === false"
- name="reset_form" type="checkbox">
- {{ $t('firefly.reset_after') }}
-
- </label>
- </div>
- </div>
- <div class="box-footer">
- <div class="btn-group">
- <button id="submitButton" class="btn btn-success" @click="submit">{{ $t('firefly.submit') }}</button>
+ <div v-if="transactions.length > 1" class="row">
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
+ <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title">
+ {{ $t('firefly.split_transaction_title') }}
+ </h3>
+ </div>
+ <div class="box-body">
+ <group-description
+ v-model="group_title"
+ :error="group_title_errors"
+ ></group-description>
+ </div>
+ </div>
</div>
- <p class="text-success" v-html="success_message"></p>
- <p class="text-danger" v-html="error_message"></p>
- </div>
</div>
- </div>
- <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
- <div class="box">
- <div class="box-header with-border">
- <h3 class="box-title">
- {{ $t('firefly.submission_options') }}
- </h3>
- </div>
- <div class="box-body">
- <div class="checkbox">
- <label>
- <input v-model="applyRules" name="apply_rules" type="checkbox">
- {{ $t('firefly.apply_rules_checkbox') }}
- </label>
+ <div class="row">
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
+ <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title">
+ {{ $t('firefly.submission') }}
+ </h3>
+ </div>
+ <div class="box-body">
+ <div class="checkbox">
+ <label>
+ <input v-model="createAnother" name="create_another" type="checkbox">
+ {{ $t('firefly.create_another') }}
+ </label>
+ </div>
+ <div class="checkbox">
+ <label v-bind:class="{ 'text-muted': this.createAnother === false}">
+ <input v-model="resetFormAfter" :disabled="this.createAnother === false"
+ name="reset_form" type="checkbox">
+ {{ $t('firefly.reset_after') }}
+
+ </label>
+ </div>
+ </div>
+ <div class="box-footer">
+ <div class="btn-group">
+ <button id="submitButton" class="btn btn-success" @click="submit">{{
+ $t('firefly.submit')
+ }}
+ </button>
+ </div>
+ <p class="text-success" v-html="success_message"></p>
+ <p class="text-danger" v-html="error_message"></p>
+ </div>
+ </div>
</div>
- <div class="checkbox">
- <label>
- <input v-model="fireWebhooks" name="fire_webhooks" type="checkbox">
- {{ $t('firefly.fire_webhooks_checkbox') }}
-
- </label>
+ <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
+ <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title">
+ {{ $t('firefly.submission_options') }}
+ </h3>
+ </div>
+ <div class="box-body">
+ <div class="checkbox">
+ <label>
+ <input v-model="applyRules" name="apply_rules" type="checkbox">
+ {{ $t('firefly.apply_rules_checkbox') }}
+ </label>
+ </div>
+ <div class="checkbox">
+ <label>
+ <input v-model="fireWebhooks" name="fire_webhooks" type="checkbox">
+ {{ $t('firefly.fire_webhooks_checkbox') }}
+
+ </label>
+ </div>
+ </div>
+ </div>
</div>
- </div>
</div>
- </div>
- </div>
- </form>
+ </form>
</template>
<script>
export default {
- name: "CreateTransaction",
- components: {},
- created() {
- this.addTransactionToArray();
- document.onreadystatechange = () => {
- if (document.readyState === "complete") {
- this.prefillSourceAccount();
- this.prefillDestinationAccount();
- }
- }
- },
- methods: {
- prefillSourceAccount() {
- if (0 === window.sourceId) {
- return;
- }
- this.getAccount(window.sourceId, 'source_account');
- },
- prefillDestinationAccount() {
- if (0 === destinationId) {
- return;
- }
- this.getAccount(window.destinationId, 'destination_account');
- },
- getAccount(accountId, slot) {
- const uri = './api/v1/accounts/' + accountId + '?_token=' + document.head.querySelector('meta[name="csrf-token"]').content;
- axios.get(uri).then(response => {
- let model = response.data.data.attributes;
- model.type = this.fullAccountType(model.type, model.liability_type);
- model.id = parseInt(response.data.data.id);
- if ('source_account' === slot) {
- this.selectedSourceAccount(0, model);
- }
- if ('destination_account' === slot) {
- this.selectedDestinationAccount(0, model);
+ name: "CreateTransaction",
+ components: {},
+ created() {
+ this.addTransactionToArray();
+ document.onreadystatechange = () => {
+ if (document.readyState === "complete") {
+ this.prefillSourceAccount();
+ this.prefillDestinationAccount();
+ }
}
- }).catch(error => {
- console.warn('Could not auto fill account');
- console.warn(error);
- });
-
- },
- fullAccountType: function (shortType, liabilityType) {
- let searchType = shortType;
- if ('liabilities' === shortType) {
- searchType = liabilityType;
- }
- let arr = {
- 'asset': 'Asset account',
- 'loan': 'Loan',
- 'debt': 'Debt',
- 'mortgage': 'Mortgage'
- };
- return arr[searchType] ?? searchType;
},
- convertData: function () {
- // console.log('Now in convertData()');
- let data = {
- 'apply_rules': this.applyRules,
- 'fire_webhooks': this.fireWebhooks,
- 'transactions': [],
- };
- let transactionType;
- let firstSource;
- let firstDestination;
-
- if (this.transactions.length > 1) {
- data.group_title = this.group_title;
- }
-
- // get transaction type from first transaction
- transactionType = this.transactionType ? this.transactionType.toLowerCase() : 'invalid';
-
- // if the transaction type is invalid, might just be that we can deduce it from
- // the presence of a source or destination account
- firstSource = this.transactions[0].source_account.type;
- firstDestination = this.transactions[0].destination_account.type;
- // console.log('Type of first source is ' + firstSource);
-
- if ('invalid' === transactionType && ['asset', 'Asset account', 'Loan', 'Debt', 'Mortgage'].includes(firstSource)) {
- transactionType = 'withdrawal';
- }
-
- if ('invalid' === transactionType && ['asset', 'Asset account', 'Loan', 'Debt', 'Mortgage'].includes(firstDestination)) {
- transactionType = 'deposit';
- }
-
- for (let key in this.transactions) {
- if (this.transactions.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
- data.transactions.push(this.convertDataRow(this.transactions[key], key, transactionType));
- }
- }
+ methods: {
+ prefillSourceAccount() {
+ if (0 === window.sourceId) {
+ return;
+ }
+ this.getAccount(window.sourceId, 'source_account');
+ },
+ prefillDestinationAccount() {
+ if (0 === destinationId) {
+ return;
+ }
+ this.getAccount(window.destinationId, 'destination_account');
+ },
+ getAccount(accountId, slot) {
+ const uri = './api/v1/accounts/' + accountId + '?_token=' + document.head.querySelector('meta[name="csrf-token"]').content;
+ axios.get(uri).then(response => {
+ let model = response.data.data.attributes;
+ model.type = this.fullAccountType(model.type, model.liability_type);
+ model.id = parseInt(response.data.data.id);
+ if ('source_account' === slot) {
+ this.selectedSourceAccount(0, model);
+ }
+ if ('destination_account' === slot) {
+ this.selectedDestinationAccount(0, model);
+ }
+ }).catch(error => {
+ console.warn('Could not auto fill account');
+ console.warn(error);
+ });
- // overrule group title in case its empty:
- if ('' === data.group_title && data.transactions.length > 1) {
- data.group_title = data.transactions[0].description;
- }
+ },
+ fullAccountType: function (shortType, liabilityType) {
+ let searchType = shortType;
+ if ('liabilities' === shortType) {
+ searchType = liabilityType;
+ }
+ let arr = {
+ 'asset': 'Asset account',
+ 'loan': 'Loan',
+ 'debt': 'Debt',
+ 'mortgage': 'Mortgage'
+ };
+ return arr[searchType] ?? searchType;
+ },
+ convertData: function () {
+ // console.log('Now in convertData()');
+ let data = {
+ 'apply_rules': this.applyRules,
+ 'fire_webhooks': this.fireWebhooks,
+ 'transactions': [],
+ };
+ let transactionType;
+ let firstSource;
+ let firstDestination;
- return data;
- },
- convertDataRow(row, index, transactionType) {
- // console.log('Now in convertDataRow()');
- let tagList = [];
- let foreignAmount = null;
- let foreignCurrency = null;
- let currentArray;
- let sourceId;
- let sourceName;
- let destId;
- let destName;
- let date;
-
- sourceId = row.source_account.id;
- sourceName = row.source_account.name;
- destId = row.destination_account.id;
- destName = row.destination_account.name;
-
- date = row.date;
- if (index > 0) {
- date = this.transactions[0].date;
- }
-
- // if type is 'withdrawal' and destination is empty, cash withdrawal.
- if (transactionType === 'withdrawal' && '' === destName) {
- destId = window.cashAccountId;
- }
-
- // if type is 'deposit' and source is empty, cash deposit.
- if (transactionType === 'deposit' && '' === sourceName) {
- sourceId = window.cashAccountId;
- }
-
- // if index is over 0 and type is withdrawal or transfer, take source from index 0.
- if (index > 0 && (transactionType.toLowerCase() === 'withdrawal' || transactionType.toLowerCase() === 'transfer')) {
- sourceId = this.transactions[0].source_account.id;
- sourceName = this.transactions[0].source_account.name;
- }
-
- // if index is over 0 and type is deposit or transfer, take destination from index 0.
- if (index > 0 && (transactionType.toLowerCase() === 'deposit' || transactionType.toLowerCase() === 'transfer')) {
- destId = this.transactions[0].destination_account.id;
- destName = this.transactions[0].destination_account.name;
- }
-
- tagList = [];
- // loop tags
- for (let tagKey in row.tags) {
- if (row.tags.hasOwnProperty(tagKey) && /^0$|^[1-9]\d*$/.test(tagKey) && tagKey <= 4294967294) {
- tagList.push(row.tags[tagKey].text);
- }
- }
-
- // set foreign currency info:
- if (row.foreign_amount.amount !== '' && parseFloat(row.foreign_amount.amount) !== .00) {
- foreignAmount = row.foreign_amount.amount;
- foreignCurrency = row.foreign_amount.currency_id;
- }
- if (foreignCurrency === row.currency_id) {
- foreignAmount = null;
- foreignCurrency = null;
- }
-
- // correct some id's
- if (0 === destId) {
- destId = null;
- }
- if (0 === sourceId) {
- sourceId = null;
- }
- // parse amount if has exactly one comma:
- // solves issues with some locales.
- if (1 === (row.amount.match(/\,/g) || []).length) {
- row.amount = row.amount.replace(',', '.');
- }
-
- currentArray =
- {
- type: transactionType,
- date: date,
-
- amount: row.amount,
- currency_id: row.currency_id,
-
- description: row.description,
-
- source_id: sourceId,
- source_name: sourceName,
-
- destination_id: destId,
- destination_name: destName,
-
-
- category_name: row.category,
-
- interest_date: row.custom_fields.interest_date,
- book_date: row.custom_fields.book_date,
- process_date: row.custom_fields.process_date,
- due_date: row.custom_fields.due_date,
- payment_date: row.custom_fields.payment_date,
- invoice_date: row.custom_fields.invoice_date,
- internal_reference: row.custom_fields.internal_reference,
- notes: row.custom_fields.notes,
- external_url: row.custom_fields.external_url
- };
-
- if (tagList.length > 0) {
- currentArray.tags = tagList;
- }
- if (null !== foreignAmount) {
- currentArray.foreign_amount = foreignAmount;
- currentArray.foreign_currency_id = foreignCurrency;
- }
- // set budget id and piggy ID.
- if (parseInt(row.budget) > 0) {
- currentArray.budget_id = parseInt(row.budget);
- }
- if (parseInt(row.bill) > 0) {
- currentArray.bill_id = parseInt(row.bill);
- }
- if (parseInt(row.piggy_bank) > 0) {
- currentArray.piggy_bank_id = parseInt(row.piggy_bank);
- }
- return currentArray;
- },
- // submit transaction
- submit(e) {
- // console.log('Now in submit()');
- const uri = './api/v1/transactions?_token=' + document.head.querySelector('meta[name="csrf-token"]').content;
- const data = this.convertData();
-
- let button = $('#submitButton');
- button.prop("disabled", true);
-
- axios.post(uri, data).then(response => {
- // console.log('Did a succesfull POST');
- // this method will ultimately send the user on (or not).
- if (0 === this.collectAttachmentData(response)) {
- // console.log('Will now go to redirectUser()');
- this.redirectUser(response.data.data.id, response.data.data);
- }
- }).catch(error => {
- // give user errors things back.
- // something something render errors.
-
- console.error('Error in transaction submission.');
- console.error(error);
- this.parseErrors(error.response.data);
-
- // something.
- // console.log('enable button again.')
- button.removeAttr('disabled');
- });
-
- if (e) {
- e.preventDefault();
- }
- },
- escapeHTML(unsafeText) {
- let div = document.createElement('div');
- div.innerText = unsafeText;
- return div.innerHTML;
- },
- redirectUser(groupId, transactionData) {
- // console.log('In redirectUser()');
- // console.log(transactionData);
- let title = null === transactionData.attributes.group_title ? transactionData.attributes.transactions[0].description : transactionData.attributes.group_title;
- // console.log('Title is "' + title + '"');
- // if count is 0, send user onwards.
- if (this.createAnother) {
- // do message:
- this.success_message = this.$t('firefly.transaction_stored_link', {ID: groupId, title: this.escapeHTML(title)});
- this.error_message = '';
- if (this.resetFormAfter) {
- // also clear form.
- this.resetTransactions();
- // do a short time out?
- setTimeout(() => this.addTransactionToArray(), 50);
- //this.addTransactionToArray();
- }
+ if (this.transactions.length > 1) {
+ data.group_title = this.group_title;
+ }
- // clear errors:
- this.setDefaultErrors();
+ // get transaction type from first transaction
+ transactionType = this.transactionType ? this.transactionType.toLowerCase() : 'invalid';
- // console.log('enable button again.')
- let button = $('#submitButton');
- button.removeAttr('disabled');
- } else {
- //console.log('Will redirect to previous URL. (' + previousUrl + ')');
- window.location.href = window.previousUrl + '?transaction_group_id=' + groupId + '&message=created';
- }
- },
+ // if the transaction type is invalid, might just be that we can deduce it from
+ // the presence of a source or destination account
+ firstSource = this.transactions[0].source_account.type;
+ firstDestination = this.transactions[0].destination_account.type;
+ // console.log('Type of first source is ' + firstSource);
- collectAttachmentData(response) {
- // console.log('Now incollectAttachmentData()');
- let groupId = response.data.data.id;
-
- // reverse list of transactions?
- response.data.data.attributes.transactions = response.data.data.attributes.transactions.reverse();
- // array of all files to be uploaded:
- let toBeUploaded = [];
-
- // array with all file data.
- let fileData = [];
-
- // all attachments
- let attachments = $('input[name="attachments[]"]');
-
- // loop over all attachments, and add references to this array:
- for (const key in attachments) {
- if (attachments.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
- for (const fileKey in attachments[key].files) {
- if (attachments[key].files.hasOwnProperty(fileKey) && /^0$|^[1-9]\d*$/.test(fileKey) && fileKey <= 4294967294) {
- // include journal thing.
- toBeUploaded.push(
- {
- journal: response.data.data.attributes.transactions[key].transaction_journal_id,
- file: attachments[key].files[fileKey]
- }
- );
- }
- }
- }
- }
- let count = toBeUploaded.length;
- // console.log('Found ' + toBeUploaded.length + ' attachments.');
-
- // loop all uploads.
- for (const key in toBeUploaded) {
- if (toBeUploaded.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
- // create file reader thing that will read all of these uploads
- (function (f, i, theParent) {
- let fileReader = new FileReader();
- fileReader.onloadend = function (evt) {
- if (evt.target.readyState === FileReader.DONE) { // DONE == 2
- fileData.push(
- {
- name: toBeUploaded[key].file.name,
- journal: toBeUploaded[key].journal,
- content: new Blob([evt.target.result])
- }
- );
- if (fileData.length === count) {
- theParent.uploadFiles(fileData, groupId, response.data.data);
+ if ('invalid' === transactionType && ['asset', 'Asset account', 'Loan', 'Debt', 'Mortgage'].includes(firstSource)) {
+ transactionType = 'withdrawal';
+ }
+
+ if ('invalid' === transactionType && ['asset', 'Asset account', 'Loan', 'Debt', 'Mortgage'].includes(firstDestination)) {
+ transactionType = 'deposit';
+ }
+
+ for (let key in this.transactions) {
+ if (this.transactions.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
+ data.transactions.push(this.convertDataRow(this.transactions[key], key, transactionType));
}
- }
- };
- fileReader.readAsArrayBuffer(f.file);
- })(toBeUploaded[key], key, this);
- }
- }
- return count;
- },
+ }
+
+ // overrule group title in case its empty:
+ if ('' === data.group_title && data.transactions.length > 1) {
+ data.group_title = data.transactions[0].description;
+ }
+
+ return data;
+ },
+ convertDataRow(row, index, transactionType) {
+ // console.log('Now in convertDataRow()');
+ let tagList = [];
+ let foreignAmount = null;
+ let foreignCurrency = null;
+ let currentArray;
+ let sourceId;
+ let sourceName;
+ let destId;
+ let destName;
+ let date;
+
+ sourceId = row.source_account.id;
+ sourceName = row.source_account.name;
+ destId = row.destination_account.id;
+ destName = row.destination_account.name;
+
+ date = row.date;
+ if (index > 0) {
+ date = this.transactions[0].date;
+ }
+
+ // if type is 'withdrawal' and destination is empty, cash withdrawal.
+ if (transactionType === 'withdrawal' && '' === destName) {
+ destId = window.cashAccountId;
+ }
+
+ // if type is 'deposit' and source is empty, cash deposit.
+ if (transactionType === 'deposit' && '' === sourceName) {
+ sourceId = window.cashAccountId;
+ }
+
+ // if index is over 0 and type is withdrawal or transfer, take source from index 0.
+ if (index > 0 && (transactionType.toLowerCase() === 'withdrawal' || transactionType.toLowerCase() === 'transfer')) {
+ sourceId = this.transactions[0].source_account.id;
+ sourceName = this.transactions[0].source_account.name;
+ }
+
+ // if index is over 0 and type is deposit or transfer, take destination from index 0.
+ if (index > 0 && (transactionType.toLowerCase() === 'deposit' || transactionType.toLowerCase() === 'transfer')) {
+ destId = this.transactions[0].destination_account.id;
+ destName = this.transactions[0].destination_account.name;
+ }
+
+ tagList = [];
+ // loop tags
+ for (let tagKey in row.tags) {
+ if (row.tags.hasOwnProperty(tagKey) && /^0$|^[1-9]\d*$/.test(tagKey) && tagKey <= 4294967294) {
+ tagList.push(row.tags[tagKey].text);
+ }
+ }
+
+ // set foreign currency info:
+ if (row.foreign_amount.amount !== '' && parseFloat(row.foreign_amount.amount) !== .00) {
+ foreignAmount = row.foreign_amount.amount;
+ foreignCurrency = row.foreign_amount.currency_id;
+ }
+ if (foreignCurrency === row.currency_id) {
+ foreignAmount = null;
+ foreignCurrency = null;
+ }
+
+ // correct some id's
+ if (0 === destId) {
+ destId = null;
+ }
+ if (0 === sourceId) {
+ sourceId = null;
+ }
+ // parse amount if has exactly one comma:
+ // solves issues with some locales.
+ if (1 === (row.amount.match(/\,/g) || []).length) {
+ row.amount = row.amount.replace(',', '.');
+ }
+
+ currentArray =
+ {
+ type: transactionType,
+ date: date,
+
+ amount: row.amount,
+ currency_id: row.currency_id,
+
+ description: row.description,
- uploadFiles(fileData, groupId, transactionData) {
- let count = fileData.length;
- let uploads = 0;
- for (const key in fileData) {
- if (fileData.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
- // console.log('Creating attachment #' + key);
- // axios thing, + then.
- const uri = './api/v1/attachments';
- const data = {
- filename: fileData[key].name,
- attachable_type: 'TransactionJournal',
- attachable_id: fileData[key].journal,
- };
- axios.post(uri, data)
- .then(response => {
- // console.log('Created attachment #' + key);
- // console.log('Uploading attachment #' + key);
- const uploadUri = './api/v1/attachments/' + response.data.data.id + '/upload';
- axios.post(uploadUri, fileData[key].content)
- .then(attachmentResponse => {
- // console.log('Uploaded attachment #' + key);
- uploads++;
- if (uploads === count) {
- // finally we can redirect the user onwards.
- // console.log('FINAL UPLOAD');
- this.redirectUser(groupId, transactionData);
- }
- // console.log('Upload complete!');
- return true;
- }).catch(error => {
- console.error('Could not upload');
- console.error(error);
- // console.log('Uploaded attachment #' + key);
- uploads++;
- if (uploads === count) {
- // finally we can redirect the user onwards.
- // console.log('FINAL UPLOAD');
- this.redirectUser(groupId, transactionData);
- }
- // console.log('Upload complete!');
- return false;
+ source_id: sourceId,
+ source_name: sourceName,
+
+ destination_id: destId,
+ destination_name: destName,
+
+
+ category_name: row.category,
+
+ interest_date: row.custom_fields.interest_date,
+ book_date: row.custom_fields.book_date,
+ process_date: row.custom_fields.process_date,
+ due_date: row.custom_fields.due_date,
+ payment_date: row.custom_fields.payment_date,
+ invoice_date: row.custom_fields.invoice_date,
+ internal_reference: row.custom_fields.internal_reference,
+ notes: row.custom_fields.notes,
+ external_url: row.custom_fields.external_url
+ };
+
+ if (tagList.length > 0) {
+ currentArray.tags = tagList;
+ }
+ if (null !== foreignAmount) {
+ currentArray.foreign_amount = foreignAmount;
+ currentArray.foreign_currency_id = foreignCurrency;
+ }
+ // set budget id and piggy ID.
+ if (parseInt(row.budget) > 0) {
+ currentArray.budget_id = parseInt(row.budget);
+ }
+ if (parseInt(row.bill) > 0) {
+ currentArray.bill_id = parseInt(row.bill);
+ }
+ if (parseInt(row.piggy_bank) > 0) {
+ currentArray.piggy_bank_id = parseInt(row.piggy_bank);
+ }
+ return currentArray;
+ },
+ // submit transaction
+ submit(e) {
+ // console.log('Now in submit()');
+ const uri = './api/v1/transactions?_token=' + document.head.querySelector('meta[name="csrf-token"]').content;
+ const data = this.convertData();
+
+ let button = $('#submitButton');
+ button.prop("disabled", true);
+
+ axios.post(uri, data).then(response => {
+ // console.log('Did a succesfull POST');
+ // this method will ultimately send the user on (or not).
+ if (0 === this.collectAttachmentData(response)) {
+ // console.log('Will now go to redirectUser()');
+ this.redirectUser(response.data.data.id, response.data.data);
+ }
+ }).catch(error => {
+ // give user errors things back.
+ // something something render errors.
+
+ console.error('Error in transaction submission.');
+ console.error(error);
+ this.parseErrors(error.response.data);
+
+ // something.
+ // console.log('enable button again.')
+ button.removeAttr('disabled');
+ });
+
+ if (e) {
+ e.preventDefault();
+ }
+ },
+ escapeHTML(unsafeText) {
+ let div = document.createElement('div');
+ div.innerText = unsafeText;
+ return div.innerHTML;
+ },
+ redirectUser(groupId, transactionData) {
+ // console.log('In redirectUser()');
+ // console.log(transactionData);
+ let title = null === transactionData.attributes.group_title ? transactionData.attributes.transactions[0].description : transactionData.attributes.group_title;
+ // console.log('Title is "' + title + '"');
+ // if count is 0, send user onwards.
+ if (this.createAnother) {
+ // do message:
+ this.success_message = this.$t('firefly.transaction_stored_link', {
+ ID: groupId,
+ title: this.escapeHTML(title)
});
- }).catch(error => {
- console.error('Could not create upload.');
- console.error(error);
- uploads++;
- if (uploads === count) {
- // finally we can redirect the user onwards.
- // console.log('FINAL UPLOAD');
- this.redirectUser(groupId, transactionData);
- }
- // console.log('Upload complete!');
- return false;
- });
- }
- }
+ this.error_message = '';
+ if (this.resetFormAfter) {
+ // also clear form.
+ this.resetTransactions();
+ // do a short time out?
+ setTimeout(() => this.addTransactionToArray(), 50);
+ //this.addTransactionToArray();
+ }
- },
+ // clear errors:
+ this.setDefaultErrors();
+ // console.log('enable button again.')
+ let button = $('#submitButton');
+ button.removeAttr('disabled');
+ } else {
+ //console.log('Will redirect to previous URL. (' + previousUrl + ')');
+ window.location.href = window.previousUrl + '?transaction_group_id=' + groupId + '&message=created';
+ }
+ },
- setDefaultErrors: function () {
- for (const key in this.transactions) {
- if (this.transactions.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
- // console.log('Set default errors for key ' + key);
- //this.transactions[key].description
- this.transactions[key].errors = {
- source_account: [],
- destination_account: [],
- description: [],
- amount: [],
- date: [],
- budget_id: [],
- bill_id: [],
- foreign_amount: [],
- category: [],
- piggy_bank: [],
- tags: [],
- // custom fields:
- custom_errors: {
- interest_date: [],
- book_date: [],
- process_date: [],
- due_date: [],
- payment_date: [],
- invoice_date: [],
- internal_reference: [],
- notes: [],
- attachments: [],
- external_url: [],
- },
- };
- }
- }
- },
- parseErrors: function (errors) {
- this.setDefaultErrors();
- this.error_message = "";
- if (typeof errors.errors === 'undefined') {
- this.success_message = '';
- this.error_message = errors.message;
- } else {
- this.success_message = '';
- this.error_message = this.$t('firefly.errors_submission');
- }
- let transactionIndex;
- let fieldName;
-
- for (const key in errors.errors) {
- if (errors.errors.hasOwnProperty(key)) {
- if (key === 'group_title') {
- this.group_title_errors = errors.errors[key];
- }
- if (key !== 'group_title') {
- // lol dumbest way to explode "transactions.0.something" ever.
- transactionIndex = parseInt(key.split('.')[1]);
- fieldName = key.split('.')[2];
- // set error in this object thing.
- switch (fieldName) {
- case 'amount':
- case 'date':
- case 'budget_id':
- case 'bill_id':
- case 'description':
- case 'tags':
- this.transactions[transactionIndex].errors[fieldName] = errors.errors[key];
- break;
- case 'source_name':
- case 'source_id':
- this.transactions[transactionIndex].errors.source_account =
- this.transactions[transactionIndex].errors.source_account.concat(errors.errors[key]);
- break;
- case 'destination_name':
- case 'destination_id':
- this.transactions[transactionIndex].errors.destination_account =
- this.transactions[transactionIndex].errors.destination_account.concat(errors.errors[key]);
- break;
- case 'foreign_amount':
- case 'foreign_currency_id':
- this.transactions[transactionIndex].errors.foreign_amount =
- this.transactions[transactionIndex].errors.foreign_amount.concat(errors.errors[key]);
- break;
- }
- }
- // unique some things
- if (typeof this.transactions[transactionIndex] !== 'undefined') {
- this.transactions[transactionIndex].errors.source_account =
- Array.from(new Set(this.transactions[transactionIndex].errors.source_account));
- this.transactions[transactionIndex].errors.destination_account =
- Array.from(new Set(this.transactions[transactionIndex].errors.destination_account));
- }
+ collectAttachmentData(response) {
+ // console.log('Now incollectAttachmentData()');
+ let groupId = response.data.data.id;
+
+ // reverse list of transactions?
+ response.data.data.attributes.transactions = response.data.data.attributes.transactions.reverse();
+ // array of all files to be uploaded:
+ let toBeUploaded = [];
+
+ // array with all file data.
+ let fileData = [];
+
+ // all attachments
+ let attachments = $('input[name="attachments[]"]');
+
+ // loop over all attachments, and add references to this array:
+ for (const key in attachments) {
+ if (attachments.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
+ for (const fileKey in attachments[key].files) {
+ if (attachments[key].files.hasOwnProperty(fileKey) && /^0$|^[1-9]\d*$/.test(fileKey) && fileKey <= 4294967294) {
+ // include journal thing.
+ toBeUploaded.push(
+ {
+ journal: response.data.data.attributes.transactions[key].transaction_journal_id,
+ file: attachments[key].files[fileKey]
+ }
+ );
+ }
+ }
+ }
+ }
+ let count = toBeUploaded.length;
+ // console.log('Found ' + toBeUploaded.length + ' attachments.');
+
+ // loop all uploads.
+ for (const key in toBeUploaded) {
+ if (toBeUploaded.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
+ // create file reader thing that will read all of these uploads
+ (function (f, i, theParent) {
+ let fileReader = new FileReader();
+ fileReader.onloadend = function (evt) {
+ if (evt.target.readyState === FileReader.DONE) { // DONE == 2
+ fileData.push(
+ {
+ name: toBeUploaded[key].file.name,
+ journal: toBeUploaded[key].journal,
+ content: new Blob([evt.target.result])
+ }
+ );
+ if (fileData.length === count) {
+ theParent.uploadFiles(fileData, groupId, response.data.data);
+ }
+ }
+ };
+ fileReader.readAsArrayBuffer(f.file);
+ })(toBeUploaded[key], key, this);
+ }
+ }
+ return count;
+ },
- }
- }
- },
- resetTransactions: function () {
- // console.log('Now in resetTransactions()');
- this.transactions = [];
- this.group_title = '';
+ uploadFiles(fileData, groupId, transactionData) {
+ let count = fileData.length;
+ let uploads = 0;
+ for (const key in fileData) {
+ if (fileData.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
+ // console.log('Creating attachment #' + key);
+ // axios thing, + then.
+ const uri = './api/v1/attachments';
+ const data = {
+ filename: fileData[key].name,
+ attachable_type: 'TransactionJournal',
+ attachable_id: fileData[key].journal,
+ };
+ axios.post(uri, data)
+ .then(response => {
+ // console.log('Created attachment #' + key);
+ // console.log('Uploading attachment #' + key);
+ const uploadUri = './api/v1/attachments/' + response.data.data.id + '/upload';
+ axios.post(uploadUri, fileData[key].content)
+ .then(attachmentResponse => {
+ // console.log('Uploaded attachment #' + key);
+ uploads++;
+ if (uploads === count) {
+ // finally we can redirect the user onwards.
+ // console.log('FINAL UPLOAD');
+ this.redirectUser(groupId, transactionData);
+ }
+ // console.log('Upload complete!');
+ return true;
+ }).catch(error => {
+ console.error('Could not upload');
+ console.error(error);
+ // console.log('Uploaded attachment #' + key);
+ uploads++;
+ if (uploads === count) {
+ // finally we can redirect the user onwards.
+ // console.log('FINAL UPLOAD');
+ this.redirectUser(groupId, transactionData);
+ }
+ // console.log('Upload complete!');
+ return false;
+ });
+ }).catch(error => {
+ console.error('Could not create upload.');
+ console.error(error);
+ uploads++;
+ if (uploads === count) {
+ // finally we can redirect the user onwards.
+ // console.log('FINAL UPLOAD');
+ this.redirectUser(groupId, transactionData);
+ }
+ // console.log('Upload complete!');
+ return false;
+ });
+ }
+ }
- },
- addTransactionToArray: function (e) {
- // console.log('Now in addTransactionToArray()');
- this.transactions.push({
- description: "",
- date: "",
- amount: "",
- category: "",
- piggy_bank: 0,
- errors: {
- source_account: [],
- destination_account: [],
- description: [],
- amount: [],
- date: [],
- budget_id: [],
- bill_id: [],
- foreign_amount: [],
- category: [],
- piggy_bank: [],
- tags: [],
- // custom fields:
- custom_errors: {
- interest_date: [],
- book_date: [],
- process_date: [],
- due_date: [],
- payment_date: [],
- invoice_date: [],
- internal_reference: [],
- notes: [],
- attachments: [],
- external_url: [],
- },
},
- budget: 0,
- bill: 0,
- tags: [],
- custom_fields: {
- "interest_date": "",
- "book_date": "",
- "process_date": "",
- "due_date": "",
- "payment_date": "",
- "invoice_date": "",
- "internal_reference": "",
- "notes": "",
- "attachments": [],
- "external_url": "",
+
+
+ setDefaultErrors: function () {
+ for (const key in this.transactions) {
+ if (this.transactions.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
+ // console.log('Set default errors for key ' + key);
+ //this.transactions[key].description
+ this.transactions[key].errors = {
+ source_account: [],
+ destination_account: [],
+ description: [],
+ amount: [],
+ date: [],
+ budget_id: [],
+ bill_id: [],
+ foreign_amount: [],
+ category: [],
+ piggy_bank: [],
+ tags: [],
+ // custom fields:
+ custom_errors: {
+ interest_date: [],
+ book_date: [],
+ process_date: [],
+ due_date: [],
+ payment_date: [],
+ invoice_date: [],
+ internal_reference: [],
+ notes: [],
+ attachments: [],
+ external_url: [],
+ },
+ };
+ }
+ }
+ },
+ parseErrors: function (errors) {
+ this.setDefaultErrors();
+ this.error_message = "";
+ if (typeof errors.errors === 'undefined') {
+ this.success_message = '';
+ this.error_message = errors.message;
+ } else {
+ this.success_message = '';
+ this.error_message = this.$t('firefly.errors_submission');
+ }
+ let transactionIndex;
+ let fieldName;
+
+ for (const key in errors.errors) {
+ if (errors.errors.hasOwnProperty(key)) {
+ if (key === 'group_title') {
+ this.group_title_errors = errors.errors[key];
+ }
+ if (key !== 'group_title') {
+ // lol dumbest way to explode "transactions.0.something" ever.
+ transactionIndex = parseInt(key.split('.')[1]);
+ fieldName = key.split('.')[2];
+ // set error in this object thing.
+ switch (fieldName) {
+ case 'amount':
+ case 'date':
+ case 'budget_id':
+ case 'bill_id':
+ case 'description':
+ case 'tags':
+ this.transactions[transactionIndex].errors[fieldName] = errors.errors[key];
+ break;
+ case 'source_name':
+ case 'source_id':
+ this.transactions[transactionIndex].errors.source_account =
+ this.transactions[transactionIndex].errors.source_account.concat(errors.errors[key]);
+ break;
+ case 'destination_name':
+ case 'destination_id':
+ this.transactions[transactionIndex].errors.destination_account =
+ this.transactions[transactionIndex].errors.destination_account.concat(errors.errors[key]);
+ break;
+ case 'foreign_amount':
+ case 'foreign_currency_id':
+ this.transactions[transactionIndex].errors.foreign_amount =
+ this.transactions[transactionIndex].errors.foreign_amount.concat(errors.errors[key]);
+ break;
+ }
+ }
+ // unique some things
+ if (typeof this.transactions[transactionIndex] !== 'undefined') {
+ this.transactions[transactionIndex].errors.source_account =
+ Array.from(new Set(this.transactions[transactionIndex].errors.source_account));
+ this.transactions[transactionIndex].errors.destination_account =
+ Array.from(new Set(this.transactions[transactionIndex].errors.destination_account));
+ }
+
+ }
+ }
+ },
+ resetTransactions: function () {
+ // console.log('Now in resetTransactions()');
+ this.transactions = [];
+ this.group_title = '';
+
},
- foreign_amount: {
- amount: "",
- currency_id: 0
+ addTransactionToArray: function (e) {
+ // console.log('Now in addTransactionToArray()');
+ this.transactions.push({
+ description: "",
+ date: "",
+ amount: "",
+ category: "",
+ piggy_bank: 0,
+ errors: {
+ source_account: [],
+ destination_account: [],
+ description: [],
+ amount: [],
+ date: [],
+ budget_id: [],
+ bill_id: [],
+ foreign_amount: [],
+ category: [],
+ piggy_bank: [],
+ tags: [],
+ // custom fields:
+ custom_errors: {
+ interest_date: [],
+ book_date: [],
+ process_date: [],
+ due_date: [],
+ payment_date: [],
+ invoice_date: [],
+ internal_reference: [],
+ notes: [],
+ attachments: [],
+ external_url: [],
+ },
+ },
+ budget: 0,
+ bill: 0,
+ tags: [],
+ custom_fields: {
+ "interest_date": "",
+ "book_date": "",
+ "process_date": "",
+ "due_date": "",
+ "payment_date": "",
+ "invoice_date": "",
+ "internal_reference": "",
+ "notes": "",
+ "attachments": [],
+ "external_url": "",
+ },
+ foreign_amount: {
+ amount: "",
+ currency_id: 0
+ },
+ source_account: {
+ id: 0,
+ name: "",
+ type: "",
+ currency_id: 0,
+ currency_name: '',
+ currency_code: '',
+ currency_decimal_places: 2,
+ allowed_types: ['Asset account', 'Revenue account', 'Loan', 'Debt', 'Mortgage'],
+ default_allowed_types: ['Asset account', 'Revenue account', 'Loan', 'Debt', 'Mortgage']
+ },
+ destination_account: {
+ id: 0,
+ name: "",
+ type: "",
+ currency_id: 0,
+ currency_name: '',
+ currency_code: '',
+ currency_decimal_places: 2,
+ allowed_types: ['Asset account', 'Expense account', 'Loan', 'Debt', 'Mortgage'],
+ default_allowed_types: ['Asset account', 'Expense account', 'Loan', 'Debt', 'Mortgage']
+ }
+ });
+ if (this.transactions.length === 1) {
+ // console.log('Length == 1, set date to today.');
+ // set first date.
+ let today = new Date();
+ this.transactions[0].date = today.getFullYear() + '-' + ("0" + (today.getMonth() + 1)).slice(-2) + '-' + ("0" + today.getDate()).slice(-2);
+ // call for extra clear thing:
+ // this.clearSource(0);
+ //this.clearDestination(0);
+ }
+ if (e) {
+ e.preventDefault();
+ }
},
- source_account: {
- id: 0,
- name: "",
- type: "",
- currency_id: 0,
- currency_name: '',
- currency_code: '',
- currency_decimal_places: 2,
- allowed_types: ['Asset account', 'Revenue account', 'Loan', 'Debt', 'Mortgage'],
- default_allowed_types: ['Asset account', 'Revenue account', 'Loan', 'Debt', 'Mortgage']
+ setTransactionType: function (type) {
+ this.transactionType = type;
},
- destination_account: {
- id: 0,
- name: "",
- type: "",
- currency_id: 0,
- currency_name: '',
- currency_code: '',
- currency_decimal_places: 2,
- allowed_types: ['Asset account', 'Expense account', 'Loan', 'Debt', 'Mortgage'],
- default_allowed_types: ['Asset account', 'Expense account', 'Loan', 'Debt', 'Mortgage']
- }
- });
- if (this.transactions.length === 1) {
- // console.log('Length == 1, set date to today.');
- // set first date.
- let today = new Date();
- this.transactions[0].date = today.getFullYear() + '-' + ("0" + (today.getMonth() + 1)).slice(-2) + '-' + ("0" + today.getDate()).slice(-2);
- // call for extra clear thing:
- // this.clearSource(0);
- //this.clearDestination(0);
- }
- if (e) {
- e.preventDefault();
- }
- },
- setTransactionType: function (type) {
- this.transactionType = type;
- },
- deleteTransaction: function (index, event) {
- event.preventDefault();
- this.transactions.splice(index, 1);
- },
- limitSourceType: function (type) {
- let i;
- for (i = 0; i < this.transactions.length; i++) {
- this.transactions[i].source_account.allowed_types = [type];
- }
- },
- limitDestinationType: function (type) {
- let i;
- for (i = 0; i < this.transactions.length; i++) {
- this.transactions[i].destination_account.allowed_types = [type];
- }
- },
+ deleteTransaction: function (index, event) {
+ event.preventDefault();
+ this.transactions.splice(index, 1);
+ },
+ limitSourceType: function (type) {
+ let i;
+ for (i = 0; i < this.transactions.length; i++) {
+ this.transactions[i].source_account.allowed_types = [type];
+ }
+ },
+ limitDestinationType: function (type) {
+ let i;
+ for (i = 0; i < this.transactions.length; i++) {
+ this.transactions[i].destination_account.allowed_types = [type];
+ }
+ },
- selectedSourceAccount: function (index, model) {
- // console.log('Now in selectedSourceAccount()');
- if (typeof model === 'string') {
- //console.log('model is string.')
- // cant change types, only name.
- this.transactions[index].source_account.name = model;
- } else {
- //console.log('model is NOT string.')
- this.transactions[index].source_account = {
- id: model.id,
- name: model.name,
- type: model.type,
- currency_id: model.currency_id,
- currency_name: model.currency_name,
- currency_code: model.currency_code,
- currency_decimal_places: model.currency_decimal_places,
- allowed_types: this.transactions[index].source_account.allowed_types,
- default_allowed_types: ['Asset account', 'Revenue account', 'Loan', 'Debt', 'Mortgage']
- };
+ selectedSourceAccount: function (index, model) {
+ // console.log('Now in selectedSourceAccount()');
+ if (typeof model === 'string') {
+ //console.log('model is string.')
+ // cant change types, only name.
+ this.transactions[index].source_account.name = model;
+ } else {
+ //console.log('model is NOT string.')
+ this.transactions[index].source_account = {
+ id: model.id,
+ name: model.name,
+ type: model.type,
+ currency_id: model.currency_id,
+ currency_name: model.currency_name,
+ currency_code: model.currency_code,
+ currency_decimal_places: model.currency_decimal_places,
+ allowed_types: this.transactions[index].source_account.allowed_types,
+ default_allowed_types: ['Asset account', 'Revenue account', 'Loan', 'Debt', 'Mortgage']
+ };
+
+ // force types on destination selector.
+ this.transactions[index].destination_account.allowed_types = window.allowedOpposingTypes.source[model.type];
+ }
+ //console.log('Transactions:');
+ //console.log(this.transactions);
+ },
+ selectedDestinationAccount: function (index, model) {
+ // console.log('Now in selectedDestinationAccount()');
+ if (typeof model === 'string') {
+ // cant change types, only name.
+ this.transactions[index].destination_account.name = model;
+ } else {
+ this.transactions[index].destination_account = {
+ id: model.id,
+ name: model.name,
+ type: model.type,
+ currency_id: model.currency_id,
+ currency_name: model.currency_name,
+ currency_code: model.currency_code,
+ currency_decimal_places: model.currency_decimal_places,
+ allowed_types: this.transactions[index].destination_account.allowed_types,
+ default_allowed_types: ['Asset account', 'Expense account', 'Loan', 'Debt', 'Mortgage']
+ };
+
+ // force types on destination selector.
+ this.transactions[index].source_account.allowed_types = window.allowedOpposingTypes.destination[model.type];
+ }
+ },
+ clearSource: function (index) {
+ // console.log('clearSource(' + index + ')');
+ // reset source account:
+ this.transactions[index].source_account = {
+ id: 0,
+ name: '',
+ type: '',
+ currency_id: 0,
+ currency_name: '',
+ currency_code: '',
+ currency_decimal_places: 2,
+ allowed_types: this.transactions[index].source_account.allowed_types,
+ default_allowed_types: ['Asset account', 'Revenue account', 'Loan', 'Debt', 'Mortgage']
+ };
+ // reset destination allowed account types.
+ this.transactions[index].destination_account.allowed_types = [];
- // force types on destination selector.
- this.transactions[index].destination_account.allowed_types = window.allowedOpposingTypes.source[model.type];
- }
- //console.log('Transactions:');
- //console.log(this.transactions);
- },
- selectedDestinationAccount: function (index, model) {
- // console.log('Now in selectedDestinationAccount()');
- if (typeof model === 'string') {
- // cant change types, only name.
- this.transactions[index].destination_account.name = model;
- } else {
- this.transactions[index].destination_account = {
- id: model.id,
- name: model.name,
- type: model.type,
- currency_id: model.currency_id,
- currency_name: model.currency_name,
- currency_code: model.currency_code,
- currency_decimal_places: model.currency_decimal_places,
- allowed_types: this.transactions[index].destination_account.allowed_types,
- default_allowed_types: ['Asset account', 'Expense account', 'Loan', 'Debt', 'Mortgage']
- };
+ // if there is a destination model, reset the types of the source
+ // by pretending we selected it again.
+ if (this.transactions[index].destination_account) {
+ this.selectedDestinationAccount(index, this.transactions[index].destination_account);
+ }
+ },
+ clearDestination: function (index) {
+ // console.log('clearDestination(' + index + ')');
+ // reset destination account:
+ this.transactions[index].destination_account = {
+ id: 0,
+ name: '',
+ type: '',
+ currency_id: 0,
+ currency_name: '',
+ currency_code: '',
+ currency_decimal_places: 2,
+ allowed_types: this.transactions[index].destination_account.allowed_types,
+ default_allowed_types: ['Asset account', 'Expense account', 'Loan', 'Debt', 'Mortgage']
+ };
+ // reset destination allowed account types.
+ this.transactions[index].source_account.allowed_types = [];
- // force types on destination selector.
- this.transactions[index].source_account.allowed_types = window.allowedOpposingTypes.destination[model.type];
- }
+ // if there is a source model, reset the types of the destination
+ // by pretending we selected it again.
+ if (this.transactions[index].source_account) {
+ this.selectedSourceAccount(index, this.transactions[index].source_account);
+ }
+ }
},
- clearSource: function (index) {
- // console.log('clearSource(' + index + ')');
- // reset source account:
- this.transactions[index].source_account = {
- id: 0,
- name: '',
- type: '',
- currency_id: 0,
- currency_name: '',
- currency_code: '',
- currency_decimal_places: 2,
- allowed_types: this.transactions[index].source_account.allowed_types,
- default_allowed_types: ['Asset account', 'Revenue account', 'Loan', 'Debt', 'Mortgage']
- };
- // reset destination allowed account types.
- this.transactions[index].destination_account.allowed_types = [];
-
- // if there is a destination model, reset the types of the source
- // by pretending we selected it again.
- if (this.transactions[index].destination_account) {
- this.selectedDestinationAccount(index, this.transactions[index].destination_account);
- }
+
+ /*
+ * The component's data.
+ */
+ data() {
+ return {
+ transactionType: null,
+ group_title: "",
+ transactions: [],
+ group_title_errors: [],
+ error_message: "",
+ success_message: "",
+ cash_account_id: 0,
+ createAnother: false,
+ resetFormAfter: false,
+ fireWebhooks: true,
+ applyRules: true,
+ resetButtonDisabled: true,
+ attachmentCount: 0,
+ };
},
- clearDestination: function (index) {
- // console.log('clearDestination(' + index + ')');
- // reset destination account:
- this.transactions[index].destination_account = {
- id: 0,
- name: '',
- type: '',
- currency_id: 0,
- currency_name: '',
- currency_code: '',
- currency_decimal_places: 2,
- allowed_types: this.transactions[index].destination_account.allowed_types,
- default_allowed_types: ['Asset account', 'Expense account', 'Loan', 'Debt', 'Mortgage']
- };
- // reset destination allowed account types.
- this.transactions[index].source_account.allowed_types = [];
-
- // if there is a source model, reset the types of the destination
- // by pretending we selected it again.
- if (this.transactions[index].source_account) {
- this.selectedSourceAccount(index, this.transactions[index].source_account);
- }
- }
- },
-
- /*
- * The component's data.
- */
- data() {
- return {
- transactionType: null,
- group_title: "",
- transactions: [],
- group_title_errors: [],
- error_message: "",
- success_message: "",
- cash_account_id: 0,
- createAnother: false,
- resetFormAfter: false,
- fireWebhooks: true,
- applyRules: true,
- resetButtonDisabled: true,
- attachmentCount: 0,
- };
- },
}
</script>