diff --git a/app/Helpers/Functions/helpers.php b/app/Helpers/Functions/helpers.php index 072660f1a1..efb1c47825 100644 --- a/app/Helpers/Functions/helpers.php +++ b/app/Helpers/Functions/helpers.php @@ -24,9 +24,13 @@ declare(strict_types=1); use Carbon\Carbon; use Carbon\CarbonInterface; +use FireflyIII\Enums\AccountTypeEnum; +use FireflyIII\Enums\TransactionTypeEnum; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournalMeta; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Support\Facades\Amount; @@ -60,6 +64,155 @@ if (!function_exists('env_default_when_empty')) { } } +if (!function_exists('sign_amount')) { + + function sign_amount(string $amount, string $transactionType, string $sourceType): string + { + // withdrawals stay negative + if (TransactionTypeEnum::WITHDRAWAL->value !== $transactionType) { + $amount = bcmul($amount, '-1'); + } + + // opening balance and it comes from initial balance? its expense. + if (TransactionTypeEnum::OPENING_BALANCE->value === $transactionType && AccountTypeEnum::INITIAL_BALANCE->value !== $sourceType) { + $amount = bcmul($amount, '-1'); + } + + // reconciliation and it comes from reconciliation? + if (TransactionTypeEnum::RECONCILIATION->value === $transactionType && AccountTypeEnum::RECONCILIATION->value !== $sourceType) { + return bcmul($amount, '-1'); + } + + return $amount; + } +} +if (!function_exists('normal_journal_object_amount')) { + function normal_journal_object_amount(TransactionJournal $journal): string + { + $type = $journal->transactionType->type; + + /** @var Transaction $first */ + $first = $journal->transactions()->where('amount', '<', 0)->first(); + $currency = $journal->transactionCurrency; + $amount = $first->amount ?? '0'; + $colored = true; + $sourceType = $first->account->accountType()->first()->type; + + $amount = sign_amount($amount, $type, $sourceType); + + if (TransactionTypeEnum::TRANSFER->value === $type) { + $colored = false; + } + $result = Amount::formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored); + if (TransactionTypeEnum::TRANSFER->value === $type) { + return sprintf('%s', $result); + } + + return $result; + } +} + +if (!function_exists('journal_object_has_foreign')) { + function journal_object_has_foreign(TransactionJournal $journal): bool + { + /** @var Transaction $first */ + $first = $journal->transactions()->where('amount', '<', 0)->first(); + + return '' !== $first->foreign_amount; + } +} + +if (function_exists('foreign_journal_object_amount')) { + + function foreign_journal_object_amount(TransactionJournal $journal): string + { + $type = $journal->transactionType->type; + + /** @var Transaction $first */ + $first = $journal->transactions()->where('amount', '<', 0)->first(); + $currency = $first->foreignCurrency; + $amount = '' === $first->foreign_amount ? '0' : $first->foreign_amount; + $colored = true; + $sourceType = $first->account->accountType()->first()->type; + + $amount = sign_amount($amount, $type, $sourceType); + + if (TransactionTypeEnum::TRANSFER->value === $type) { + $colored = false; + } + $result = Amount::formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored); + if (TransactionTypeEnum::TRANSFER->value === $type) { + return sprintf('%s', $result); + } + + return $result; + } +} + +if (!function_exists('journal_object_amount')) { + function journal_object_amount(TransactionJournal $journal): string + { + $result = normal_journal_object_amount($journal); + + // now append foreign amount, if any. + if (journal_object_has_foreign($journal)) { + $foreign = foreign_journal_object_amount($journal); + $result = sprintf('%s (%s)', $result, $foreign); + } + + return $result; + } +} + +if (!function_exists('journal_link_translation')) { + function journal_link_translation(string $direction, string $original): string + { + $key = sprintf('firefly.%s_%s', $original, $direction); + $translation = (string)trans($key); + if ($key === $translation) { + return $original; + } + return $translation; + } +} + +if (!function_exists('all_journal_triggers')) { + function all_journal_triggers(): array + { + return [ + 'store-journal' => (string)trans('firefly.rule_trigger_store_journal'), + 'update-journal' => (string)trans('firefly.rule_trigger_update_journal'), + 'manual-activation' => (string)trans('firefly.rule_trigger_manual'), + ]; + } +} + +if (!function_exists('all_rule_actions')) { + function all_rule_actions(): array + { + // array of valid values for actions + $ruleActions = array_keys(config('firefly.rule-actions')); + $possibleActions = []; + foreach ($ruleActions as $key) { + $possibleActions[$key] = (string)trans('firefly.rule_action_' . $key . '_choice'); + } + unset($ruleActions); + asort($possibleActions); + return $possibleActions; + } +} + +if (!function_exists('all_journal_triggers')) { + function all_journal_triggers(): array + { + return [ + 'store-journal' => (string)trans('firefly.rule_trigger_store_journal'), + 'update-journal' => (string)trans('firefly.rule_trigger_update_journal'), + 'manual-activation' => (string)trans('firefly.rule_trigger_manual'), + ]; + } +} + if (!function_exists('mime_icon')) { function mime_icon(string $file): string { diff --git a/app/Http/Controllers/Json/FrontpageController.php b/app/Http/Controllers/Json/FrontpageController.php index 75b95fc278..e4a6f91ef1 100644 --- a/app/Http/Controllers/Json/FrontpageController.php +++ b/app/Http/Controllers/Json/FrontpageController.php @@ -92,7 +92,7 @@ final class FrontpageController extends Controller } catch (Throwable $e) { Log::error(sprintf('Cannot render json.piggy-banks: %s', $e->getMessage())); Log::error($e->getTraceAsString()); - $html = 'Could not render view.'; + $html = sprintf('Could not render view: %s', $e->getMessage()); throw new FireflyException($html, 0, $e); } diff --git a/app/Http/Controllers/Json/RuleController.php b/app/Http/Controllers/Json/RuleController.php index 71f9a5a0de..ac4c160633 100644 --- a/app/Http/Controllers/Json/RuleController.php +++ b/app/Http/Controllers/Json/RuleController.php @@ -43,7 +43,7 @@ final class RuleController extends Controller */ public function action(Request $request): JsonResponse { - $count = (int) $request->get('count') > 0 ? (int) $request->get('count') : 1; + $count = (int) $request->input('count') > 0 ? (int) $request->input('count') : 1; $keys = array_keys(config('firefly.rule-actions')); $actions = []; foreach ($keys as $key) { @@ -51,11 +51,11 @@ final class RuleController extends Controller } try { - $view = view('rules.partials.action', ['actions' => $actions, 'count' => $count])->render(); + $view = view('rules.partials.action', ['actions' => $actions, 'count' => $count,'oldAction' => null, 'oldValue' => null,'oldChecked' => null])->render(); } catch (Throwable $e) { Log::error(sprintf('Cannot render rules.partials.action: %s', $e->getMessage())); Log::error($e->getTraceAsString()); - $view = 'Could not render view.'; + $view = sprintf('Could not render view: %s', $e->getMessage()); throw new FireflyException($view, 0, $e); } @@ -70,7 +70,7 @@ final class RuleController extends Controller */ public function trigger(Request $request): JsonResponse { - $count = (int) $request->get('count') > 0 ? (int) $request->get('count') : 1; + $count = (int) $request->input('count') > 0 ? (int) $request->input('count') : 1; $operators = config('search.operators'); $triggers = []; foreach ($operators as $key => $operator) { @@ -81,11 +81,11 @@ final class RuleController extends Controller asort($triggers); try { - $view = view('rules.partials.trigger', ['triggers' => $triggers, 'count' => $count])->render(); + $view = view('rules.partials.trigger', ['triggers' => $triggers, 'count' => $count,'oldTrigger' => null, 'oldProhibited' => null,'oldValue' => null,'oldChecked' =>null])->render(); } catch (Throwable $e) { Log::error(sprintf('Cannot render rules.partials.trigger: %s', $e->getMessage())); Log::error($e->getTraceAsString()); - $view = 'Could not render view.'; + $view = sprintf('Could not render view: %s', $e->getMessage()); throw new FireflyException($view, 0, $e); } diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index 7d658abb3e..e1fc7a6ad5 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -61,13 +61,13 @@ final class SearchController extends Controller public function index(Request $request, SearchInterface $searcher): Factory|\Illuminate\Contracts\View\View { // search params: - $fullQuery = $request->get('search'); - if (is_array($request->get('search'))) { + $fullQuery = $request->input('search'); + if (is_array($request->input('search'))) { $fullQuery = ''; } $fullQuery = (string) $fullQuery; - $page = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page'); - $ruleId = (int) $request->get('rule'); + $page = 0 === (int) $request->input('page') ? 1 : (int) $request->get('page'); + $ruleId = (int) $request->input('rule'); $ruleChanged = false; // find rule, check if query is different, offer to update. @@ -132,7 +132,7 @@ final class SearchController extends Controller } catch (Throwable $e) { Log::error(sprintf('Cannot render search.search: %s', $e->getMessage())); Log::error($e->getTraceAsString()); - $html = 'Could not render view.'; + $html = sprintf('Could not render view: %s', $e->getMessage()); throw new FireflyException($html, 0, $e); } diff --git a/app/Support/Form/AccountForm.php b/app/Support/Form/AccountForm.php index d4e5d0e25e..427dca1c34 100644 --- a/app/Support/Form/AccountForm.php +++ b/app/Support/Form/AccountForm.php @@ -122,7 +122,7 @@ class AccountForm ])->render(); } catch (Throwable $e) { Log::debug(sprintf('Could not render assetAccountCheckList(): %s', $e->getMessage())); - $html = 'Could not render assetAccountCheckList.'; + $html = sprintf('Could not render assetAccountCheckList: %s', $e->getMessage()); throw new FireflyException($html, 0, $e); } diff --git a/app/Support/Http/Controllers/RenderPartialViews.php b/app/Support/Http/Controllers/RenderPartialViews.php index 4ac5322ad3..4f638000eb 100644 --- a/app/Support/Http/Controllers/RenderPartialViews.php +++ b/app/Support/Http/Controllers/RenderPartialViews.php @@ -96,7 +96,7 @@ trait RenderPartialViews $result = view('reports.options.budget', ['budgets' => $budgets])->render(); } catch (Throwable $e) { Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage())); - $result = 'Could not render view.'; + $result = sprintf('Could not render view: %s', $e->getMessage()); throw new FireflyException($result, 0, $e); } @@ -178,7 +178,7 @@ trait RenderPartialViews $result = view('reports.options.category', ['categories' => $categories])->render(); } catch (Throwable $e) { Log::error(sprintf('Cannot render reports.options.category: %s', $e->getMessage())); - $result = 'Could not render view.'; + $result = sprintf('Could not render view: %s', $e->getMessage()); throw new FireflyException($result, 0, $e); } @@ -232,7 +232,7 @@ trait RenderPartialViews $result = view('reports.options.double', ['set' => $set])->render(); } catch (Throwable $e) { Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage())); - $result = 'Could not render view.'; + $result = sprintf('Could not render view: %s', $e->getMessage()); throw new FireflyException($result, 0, $e); } @@ -404,7 +404,7 @@ trait RenderPartialViews $result = view('reports.options.no-options')->render(); } catch (Throwable $e) { Log::error(sprintf('Cannot render reports.options.no-options: %s', $e->getMessage())); - $result = 'Could not render view.'; + $result = sprintf('Could not render view: %s', $e->getMessage()); throw new FireflyException($result, 0, $e); } @@ -437,7 +437,7 @@ trait RenderPartialViews $result = view('reports.options.tag', ['tags' => $grouped])->render(); } catch (Throwable $e) { Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage())); - $result = 'Could not render view.'; + $result = sprintf('Could not render view: %s', $e->getMessage()); throw new FireflyException($result, 0, $e); } diff --git a/resources/views/components/layout/sidebar.blade.php b/resources/views/components/layout/sidebar.blade.php index 779268a60f..3b10f3be9f 100644 --- a/resources/views/components/layout/sidebar.blade.php +++ b/resources/views/components/layout/sidebar.blade.php @@ -90,7 +90,7 @@ -