Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Sort by alphabet.
James Colec9e4a09da657
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.phpindex e0b3dc3dc8..ba3b4200e3 100644--- a/app/Repositories/Journal/JournalRepository.php+++ b/app/Repositories/Journal/JournalRepository.php@@ -369,18 +369,18 @@ class JournalRepository implements JournalRepositoryInterface** @return array*/- protected function storeWithdrawalAccounts(array $data)+ protected function storeDepositAccounts(array $data){- $fromAccount = Account::find($data['account_id']);+ $toAccount = Account::find($data['account_id']);- if (strlen($data['expense_account']) > 0) {- $toType = AccountType::where('type', 'Expense account')->first();- $toAccount = Account::firstOrCreateEncrypted(- ['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => $data['expense_account'], 'active' => 1]+ 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();- $toAccount = Account::firstOrCreateEncrypted(+ $toType = AccountType::where('type', 'Cash account')->first();+ $fromAccount = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);}@@ -393,18 +393,18 @@ class JournalRepository implements JournalRepositoryInterface** @return array*/- protected function storeDepositAccounts(array $data)+ protected function storeWithdrawalAccounts(array $data){- $toAccount = Account::find($data['account_id']);+ $fromAccount = 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]+ if (strlen($data['expense_account']) > 0) {+ $toType = AccountType::where('type', 'Expense account')->first();+ $toAccount = Account::firstOrCreateEncrypted(+ ['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => $data['expense_account'], 'active' => 1]);} else {- $toType = AccountType::where('type', 'Cash account')->first();- $fromAccount = Account::firstOrCreateEncrypted(+ $toType = AccountType::where('type', 'Cash account')->first();+ $toAccount = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);}diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.phpindex 5f4be310b5..0af4ad1ff6 100644--- a/app/Repositories/Rule/RuleRepository.php+++ b/app/Repositories/Rule/RuleRepository.php@@ -24,23 +24,19 @@ class RuleRepository implements RuleRepositoryInterface{/**- * @param Rule $rule- * @param array $ids+ * @param Rule $rule** @return bool*/- public function reorderRuleTriggers(Rule $rule, array $ids)+ public function destroy(Rule $rule){- $order = 1;- foreach ($ids as $triggerId) {- /** @var RuleTrigger $trigger */- $trigger = $rule->ruleTriggers()->find($triggerId);- if (!is_null($trigger)) {- $trigger->order = $order;- $trigger->save();- $order++;- }+ foreach ($rule->ruleTriggers as $trigger) {+ $trigger->delete();+ }+ foreach ($rule->ruleActions as $action) {+ $action->delete();}+ $rule->delete();return true;}@@ -48,26 +44,54 @@ class RuleRepository implements RuleRepositoryInterface/*** @param RuleGroup $ruleGroup*+ * @return int+ */+ public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup)+ {+ return intval($ruleGroup->rules()->max('order'));+ }++ /**+ * @param Rule $rule+ ** @return bool*/- public function resetRulesInGroupOrder(RuleGroup $ruleGroup)+ public function moveDown(Rule $rule){- $ruleGroup->rules()->whereNotNull('deleted_at')->update(['order' => 0]);+ $order = $rule->order;- $set = $ruleGroup->rules()- ->orderBy('order', 'ASC')- ->orderBy('updated_at', 'DESC')- ->get();- $count = 1;- /** @var Rule $entry */- foreach ($set as $entry) {- $entry->order = $count;- $entry->save();- $count++;+ // find the rule with order+1 and give it order-1+ $other = $rule->ruleGroup->rules()->where('order', ($order + 1))->first();+ if ($other) {+ $other->order = $other->order - 1;+ $other->save();}- return true;+ $rule->order = ($rule->order + 1);+ $rule->save();+ $this->resetRulesInGroupOrder($rule->ruleGroup);+ }++ /**+ * @param Rule $rule+ *+ * @return bool+ */+ public function moveUp(Rule $rule)+ {+ $order = $rule->order;++ // find the rule with order-1 and give it order+1+ $other = $rule->ruleGroup->rules()->where('order', ($order - 1))->first();+ if ($other) {+ $other->order = ($other->order + 1);+ $other->save();+ }++ $rule->order = ($rule->order - 1);+ $rule->save();+ $this->resetRulesInGroupOrder($rule->ruleGroup);}/**@@ -92,51 +116,53 @@ class RuleRepository implements RuleRepositoryInterfacereturn true;}-/**- * @param Rule $rule+ * @param Rule $rule+ * @param array $ids** @return bool*/- public function moveUp(Rule $rule)+ public function reorderRuleTriggers(Rule $rule, array $ids){- $order = $rule->order;-- // find the rule with order-1 and give it order+1- $other = $rule->ruleGroup->rules()->where('order', ($order - 1))->first();- if ($other) {- $other->order = ($other->order + 1);- $other->save();+ $order = 1;+ foreach ($ids as $triggerId) {+ /** @var RuleTrigger $trigger */+ $trigger = $rule->ruleTriggers()->find($triggerId);+ if (!is_null($trigger)) {+ $trigger->order = $order;+ $trigger->save();+ $order++;+ }}- $rule->order = ($rule->order - 1);- $rule->save();- $this->resetRulesInGroupOrder($rule->ruleGroup);+ return true;}/**- * @param Rule $rule+ * @param RuleGroup $ruleGroup** @return bool*/- public function moveDown(Rule $rule)+ public function resetRulesInGroupOrder(RuleGroup $ruleGroup){- $order = $rule->order;+ $ruleGroup->rules()->whereNotNull('deleted_at')->update(['order' => 0]);- // find the rule with order+1 and give it order-1- $other = $rule->ruleGroup->rules()->where('order', ($order + 1))->first();- if ($other) {- $other->order = $other->order - 1;- $other->save();+ $set = $ruleGroup->rules()+ ->orderBy('order', 'ASC')+ ->orderBy('updated_at', 'DESC')+ ->get();+ $count = 1;+ /** @var Rule $entry */+ foreach ($set as $entry) {+ $entry->order = $count;+ $entry->save();+ $count++;}+ return true;- $rule->order = ($rule->order + 1);- $rule->save();- $this->resetRulesInGroupOrder($rule->ruleGroup);}-/*** @param array $data*@@ -192,51 +218,23 @@ class RuleRepository implements RuleRepositoryInterface* @param bool $stopProcessing* @param int $order*- * @return RuleTrigger- */- public function storeTrigger(Rule $rule, $action, $value, $stopProcessing, $order)- {- $ruleTrigger = new RuleTrigger;- $ruleTrigger->rule()->associate($rule);- $ruleTrigger->order = $order;- $ruleTrigger->active = 1;- $ruleTrigger->stop_processing = $stopProcessing;- $ruleTrigger->trigger_type = $action;- $ruleTrigger->trigger_value = $value;- $ruleTrigger->save();-- return $ruleTrigger;- }-- /**- * @param Rule $rule- *- * @return bool+ * @return RuleAction*/- public function destroy(Rule $rule)+ public function storeAction(Rule $rule, $action, $value, $stopProcessing, $order){- foreach ($rule->ruleTriggers as $trigger) {- $trigger->delete();- }- foreach ($rule->ruleActions as $action) {- $action->delete();- }- $rule->delete();+ $ruleAction = new RuleAction;+ $ruleAction->rule()->associate($rule);+ $ruleAction->order = $order;+ $ruleAction->active = 1;+ $ruleAction->stop_processing = $stopProcessing;+ $ruleAction->action_type = $action;+ $ruleAction->action_value = $value;+ $ruleAction->save();- return true;- }- /**- * @param RuleGroup $ruleGroup- *- * @return int- */- public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup)- {- return intval($ruleGroup->rules()->max('order'));+ return $ruleAction;}-/*** @param Rule $rule* @param string $action@@ -244,21 +242,20 @@ class RuleRepository implements RuleRepositoryInterface* @param bool $stopProcessing* @param int $order*- * @return RuleAction+ * @return RuleTrigger*/- public function storeAction(Rule $rule, $action, $value, $stopProcessing, $order)+ public function storeTrigger(Rule $rule, $action, $value, $stopProcessing, $order){- $ruleAction = new RuleAction;- $ruleAction->rule()->associate($rule);- $ruleAction->order = $order;- $ruleAction->active = 1;- $ruleAction->stop_processing = $stopProcessing;- $ruleAction->action_type = $action;- $ruleAction->action_value = $value;- $ruleAction->save();-+ $ruleTrigger = new RuleTrigger;+ $ruleTrigger->rule()->associate($rule);+ $ruleTrigger->order = $order;+ $ruleTrigger->active = 1;+ $ruleTrigger->stop_processing = $stopProcessing;+ $ruleTrigger->trigger_type = $action;+ $ruleTrigger->trigger_value = $value;+ $ruleTrigger->save();- return $ruleAction;+ return $ruleTrigger;}/**diff --git a/app/Repositories/Rule/RuleRepositoryInterface.php b/app/Repositories/Rule/RuleRepositoryInterface.phpindex 7843c8c72a..42829751ff 100644--- a/app/Repositories/Rule/RuleRepositoryInterface.php+++ b/app/Repositories/Rule/RuleRepositoryInterface.php@@ -36,49 +36,41 @@ interface RuleRepositoryInterfacepublic function getHighestOrderInRuleGroup(RuleGroup $ruleGroup);/**- * @param Rule $rule- * @param array $ids- *- * @return bool- */- public function reorderRuleTriggers(Rule $rule, array $ids);-- /**- * @param Rule $rule- * @param array $ids+ * @param Rule $rule** @return bool*/- public function reorderRuleActions(Rule $rule, array $ids);+ public function moveDown(Rule $rule);/**- * @param RuleGroup $ruleGroup+ * @param Rule $rule** @return bool*/- public function resetRulesInGroupOrder(RuleGroup $ruleGroup);+ public function moveUp(Rule $rule);/**- * @param Rule $rule+ * @param Rule $rule+ * @param array $ids** @return bool*/- public function moveUp(Rule $rule);+ public function reorderRuleActions(Rule $rule, array $ids);/*** @param Rule $rule- * @param array $data+ * @param array $ids*- * @return Rule+ * @return bool*/- public function update(Rule $rule, array $data);+ public function reorderRuleTriggers(Rule $rule, array $ids);/**- * @param Rule $rule+ * @param RuleGroup $ruleGroup** @return bool*/- public function moveDown(Rule $rule);+ public function resetRulesInGroupOrder(RuleGroup $ruleGroup);/*** @param array $data@@ -94,9 +86,9 @@ interface RuleRepositoryInterface* @param bool $stopProcessing* @param int $order*- * @return RuleTrigger+ * @return RuleAction*/- public function storeTrigger(Rule $rule, $action, $value, $stopProcessing, $order);+ public function storeAction(Rule $rule, $action, $value, $stopProcessing, $order);/*** @param Rule $rule@@ -105,8 +97,16 @@ interface RuleRepositoryInterface* @param bool $stopProcessing* @param int $order*- * @return RuleAction+ * @return RuleTrigger*/- public function storeAction(Rule $rule, $action, $value, $stopProcessing, $order);+ public function storeTrigger(Rule $rule, $action, $value, $stopProcessing, $order);++ /**+ * @param Rule $rule+ * @param array $data+ *+ * @return Rule+ */+ public function update(Rule $rule, array $data);}\ No newline at end of filediff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.phpindex d0368d05f1..952090e750 100644--- a/app/Repositories/Tag/TagRepository.php+++ b/app/Repositories/Tag/TagRepository.php@@ -22,6 +22,48 @@ class TagRepository implements TagRepositoryInterface{+ /**+ * @param Collection $accounts+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return Collection+ */+ public function allCoveredByBalancingActs(Collection $accounts, Carbon $start, Carbon $end)+ {+ $ids = $accounts->pluck('id')->toArray();+ $set = Auth::user()->tags()+ ->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id')+ ->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')+ ->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id')+ ->leftJoin(+ 'transactions AS t_from', function (JoinClause $join) {+ $join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0);+ }+ )+ ->leftJoin(+ 'transactions AS t_to', function (JoinClause $join) {+ $join->on('transaction_journals.id', '=', 't_to.transaction_journal_id')->where('t_to.amount', '>', 0);+ }+ )+ ->where('tags.tagMode', 'balancingAct')+ ->where('transaction_types.type', TransactionType::TRANSFER)+ ->where('transaction_journals.date', '>=', $start->format('Y-m-d'))+ ->where('transaction_journals.date', '<=', $end->format('Y-m-d'))+ ->whereNull('transaction_journals.deleted_at')+ ->whereIn('t_from.account_id', $ids)+ ->whereIn('t_to.account_id', $ids)+ ->groupBy('t_to.account_id')+ ->get(+ [+ 't_to.account_id',+ DB::Raw('SUM(`t_to`.`amount`) as `sum`'),+ ]+ );++ return $set;+ }+/**** @param TransactionJournal $journal@@ -95,49 +137,6 @@ class TagRepository implements TagRepositoryInterfacereturn $amount;}-- /**- * @param Collection $accounts- * @param Carbon $start- * @param Carbon $end- *- * @return Collection- */- public function allCoveredByBalancingActs(Collection $accounts, Carbon $start, Carbon $end)- {- $ids = $accounts->pluck('id')->toArray();- $set = Auth::user()->tags()- ->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id')- ->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')- ->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id')- ->leftJoin(- 'transactions AS t_from', function (JoinClause $join) {- $join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0);- }- )- ->leftJoin(- 'transactions AS t_to', function (JoinClause $join) {- $join->on('transaction_journals.id', '=', 't_to.transaction_journal_id')->where('t_to.amount', '>', 0);- }- )- ->where('tags.tagMode', 'balancingAct')- ->where('transaction_types.type', TransactionType::TRANSFER)- ->where('transaction_journals.date', '>=', $start->format('Y-m-d'))- ->where('transaction_journals.date', '<=', $end->format('Y-m-d'))- ->whereNull('transaction_journals.deleted_at')- ->whereIn('t_from.account_id', $ids)- ->whereIn('t_to.account_id', $ids)- ->groupBy('t_to.account_id')- ->get(- [- 't_to.account_id',- DB::Raw('SUM(`t_to`.`amount`) as `sum`'),- ]- );-- return $set;- }-/*** @param Tag $tag*@@ -278,42 +277,6 @@ class TagRepository implements TagRepositoryInterfacereturn $tag;}- /**- * @param TransactionJournal $journal- * @param Tag $tag- *- * @return boolean- */- protected function connectBalancingAct(TransactionJournal $journal, Tag $tag)- {- /** @var TransactionType $withdrawal */- $withdrawal = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();- $withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();- /** @var TransactionType $transfer */- $transfer = TransactionType::whereType(TransactionType::TRANSFER)->first();- $transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();--- // only if this is the only withdrawal.- if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {- $journal->tags()->save($tag);- $journal->save();-- return true;- }- // and only if this is the only transfer- if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {- $journal->tags()->save($tag);- $journal->save();-- return true;- }-- // ignore expense- return false;-- }-/*** @param TransactionJournal $journal* @param Tag $tag@@ -361,6 +324,42 @@ class TagRepository implements TagRepositoryInterface}+ /**+ * @param TransactionJournal $journal+ * @param Tag $tag+ *+ * @return boolean+ */+ protected function connectBalancingAct(TransactionJournal $journal, Tag $tag)+ {+ /** @var TransactionType $withdrawal */+ $withdrawal = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();+ $withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();+ /** @var TransactionType $transfer */+ $transfer = TransactionType::whereType(TransactionType::TRANSFER)->first();+ $transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();+++ // only if this is the only withdrawal.+ if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {+ $journal->tags()->save($tag);+ $journal->save();++ return true;+ }+ // and only if this is the only transfer+ if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {+ $journal->tags()->save($tag);+ $journal->save();++ return true;+ }++ // ignore expense+ return false;++ }+/*** @param TransactionJournal $journal* @param Tag $tagdiff --git a/app/Rules/Processor.php b/app/Rules/Processor.phpindex eb932ca2ec..54125bfcac 100644--- a/app/Rules/Processor.php+++ b/app/Rules/Processor.php@@ -25,17 +25,14 @@ use Log;*/class Processor{- /** @var Rule */- protected $rule;-/** @var TransactionJournal */protected $journal;-- /** @var array */- private $triggerTypes = [];-+ /** @var Rule */+ protected $rule;/** @var array */private $actionTypes = [];+ /** @var array */+ private $triggerTypes = [];/*** Processor constructor.@@ -51,6 +48,38 @@ class Processor$this->actionTypes = Domain::getRuleActions();}+ /**+ * @return TransactionJournal+ */+ public function getJournal()+ {+ return $this->journal;+ }++ /**+ * @param TransactionJournal $journal+ */+ public function setJournal($journal)+ {+ $this->journal = $journal;+ }++ /**+ * @return Rule+ */+ public function getRule()+ {+ return $this->rule;+ }++ /**+ * @param Rule $rule+ */+ public function setRule($rule)+ {+ $this->rule = $rule;+ }+public function handle(){// get all triggers:@@ -62,6 +91,34 @@ class Processor}+ /**+ * @return bool+ */+ protected function actions()+ {+ /**+ * @var int $index+ * @var RuleAction $action+ */+ foreach ($this->rule->ruleActions()->orderBy('order', 'ASC')->get() as $action) {+ $type = $action->action_type;+ $class = $this->actionTypes[$type];+ Log::debug('Action #' . $action->id . ' for rule #' . $action->rule_id . ' (' . $type . ')');+ if (!class_exists($class)) {+ abort(500, 'Could not instantiate class for rule action type "' . $type . '" (' . $class . ').');+ }+ /** @var ActionInterface $actionClass */+ $actionClass = new $class($action, $this->journal);+ $actionClass->act();+ if ($action->stop_processing) {+ break;+ }++ }++ return true;+ }+/*** TODO stop when stop_processing is present.*@@ -101,65 +158,5 @@ class Processor}- /**- * @return bool- */- protected function actions()- {- /**- * @var int $index- * @var RuleAction $action- */- foreach ($this->rule->ruleActions()->orderBy('order', 'ASC')->get() as $action) {- $type = $action->action_type;- $class = $this->actionTypes[$type];- Log::debug('Action #' . $action->id . ' for rule #' . $action->rule_id . ' (' . $type . ')');- if (!class_exists($class)) {- abort(500, 'Could not instantiate class for rule action type "' . $type . '" (' . $class . ').');- }- /** @var ActionInterface $actionClass */- $actionClass = new $class($action, $this->journal);- $actionClass->act();- if ($action->stop_processing) {- break;- }-- }-- return true;- }-- /**- * @return Rule- */- public function getRule()- {- return $this->rule;- }-- /**- * @param Rule $rule- */- public function setRule($rule)- {- $this->rule = $rule;- }-- /**- * @return TransactionJournal- */- public function getJournal()- {- return $this->journal;- }-- /**- * @param TransactionJournal $journal- */- public function setJournal($journal)- {- $this->journal = $journal;- }-}\ No newline at end of filediff --git a/app/Support/Amount.php b/app/Support/Amount.phpindex 72bb329ad7..be465b5872 100644--- a/app/Support/Amount.php+++ b/app/Support/Amount.php@@ -17,6 +17,17 @@ use Preferences as Prefs;class Amount{+ /**+ * @param $amount+ * @param bool $coloured+ *+ * @return string+ */+ public function format($amount, $coloured = true)+ {+ return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);+ }+/*** This method will properly format the given number, in color or "black and white",* as a currency, given two things: the currency required and the current locale.@@ -48,48 +59,6 @@ class Amountreturn $result;}- /**- * @param $amount- * @param bool $coloured- *- * @return string- */- public function format($amount, $coloured = true)- {- return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);- }-- /**- * @return string- */- public function getCurrencySymbol()- {- $cache = new CacheProperties;- $cache->addProperty('getCurrencySymbol');- if ($cache->has()) {- return $cache->get();- } else {- $currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR'));- $currency = TransactionCurrency::whereCode($currencyPreference->data)->first();-- $cache->store($currency->symbol);-- return $currency->symbol;- }- }-- /**- * @param string $symbol- * @param float $amount- * @param bool $coloured- *- * @return string- */- public function formatWithSymbol($symbol, $amount, $coloured = true)- {- return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);- }-/**** @param TransactionJournal $journal@@ -139,6 +108,18 @@ class Amountreturn $this->formatAnything($currency, $transaction->amount, $coloured);}+ /**+ * @param string $symbol+ * @param float $amount+ * @param bool $coloured+ *+ * @return string+ */+ public function formatWithSymbol($symbol, $amount, $coloured = true)+ {+ return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);+ }+/*** @return Collection*/@@ -173,6 +154,25 @@ class Amount}}+ /**+ * @return string+ */+ public function getCurrencySymbol()+ {+ $cache = new CacheProperties;+ $cache->addProperty('getCurrencySymbol');+ if ($cache->has()) {+ return $cache->get();+ } else {+ $currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR'));+ $currency = TransactionCurrency::whereCode($currencyPreference->data)->first();++ $cache->store($currency->symbol);++ return $currency->symbol;+ }+ }+/*** @return TransactionCurrency*/diff --git a/app/Support/CacheProperties.php b/app/Support/CacheProperties.phpindex ba2d654b3d..ed0c1f3302 100644--- a/app/Support/CacheProperties.php+++ b/app/Support/CacheProperties.php@@ -71,6 +71,14 @@ class CachePropertiesreturn Cache::has($this->md5);}+ /**+ * @param $data+ */+ public function store($data)+ {+ Cache::forever($this->md5, $data);+ }+/*** @return void*/@@ -95,12 +103,4 @@ class CacheProperties$this->md5 = md5($this->md5);}-- /**- * @param $data- */- public function store($data)- {- Cache::forever($this->md5, $data);- }}diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.phpindex 37e4cc6c53..2fe9edcd3e 100644--- a/app/Support/ExpandedForm.php+++ b/app/Support/ExpandedForm.php@@ -19,25 +19,6 @@ use Session;class ExpandedForm{- /**- * @param $name- * @param null $value- * @param array $options- *- * @return string- */- public function staticText($name, $value, array $options = [])- {- $label = $this->label($name, $options);- $options = $this->expandOptionArray($name, $label, $options);- $classes = $this->getHolderClasses($name);- $value = $this->fillFieldValue($name, $value);- $html = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render();-- return $html;-- }-/*** @param $name* @param null $value@@ -63,86 +44,6 @@ class ExpandedForm}- /**- * @param $name- * @param $options- *- * @return mixed- */- protected function label($name, $options)- {- if (isset($options['label'])) {- return $options['label'];- }-- return trans('form.' . $name);-- }-- /**- * @param $name- * @param $label- * @param array $options- *- * @return array- */- protected function expandOptionArray($name, $label, array $options)- {- $options['class'] = 'form-control';- $options['id'] = 'ffInput_' . $name;- $options['autocomplete'] = 'off';- $options['placeholder'] = ucfirst($label);-- return $options;- }-- /**- * @param $name- *- * @return string- */- protected function getHolderClasses($name)- {- /*- * Get errors from session:- */- /** @var MessageBag $errors */- $errors = Session::get('errors');- $classes = 'form-group';-- if (!is_null($errors) && $errors->has($name)) {- $classes = 'form-group has-error has-feedback';- }-- return $classes;- }-- /**- * @param $name- * @param $value- *- * @return mixed- */- protected function fillFieldValue($name, $value)- {- if (Session::has('preFilled')) {- $preFilled = Session::get('preFilled');- $value = isset($preFilled[$name]) && is_null($value) ? $preFilled[$name] : $value;- }- // @codeCoverageIgnoreStart- try {- if (!is_null(Input::old($name))) {- $value = Input::old($name);- }- } catch (RuntimeException $e) {- // don't care about session errors.- }-- // @codeCoverageIgnoreEnd-- return $value;- }-/*** @param $name* @param null $value@@ -208,6 +109,23 @@ class ExpandedFormreturn $html;}+ /**+ * @param $name+ * @param array $options+ *+ * @return string+ */+ public function file($name, array $options = [])+ {+ $label = $this->label($name, $options);+ $options = $this->expandOptionArray($name, $label, $options);+ $classes = $this->getHolderClasses($name);+ $html = view('form.file', compact('classes', 'name', 'label', 'options'))->render();++ return $html;++ }+/*** @param $name* @param null $value@@ -373,33 +291,35 @@ class ExpandedForm** @return string*/- public function tags($name, $value = null, array $options = [])+ public function staticText($name, $value, array $options = []){- $label = $this->label($name, $options);- $options = $this->expandOptionArray($name, $label, $options);- $classes = $this->getHolderClasses($name);- $value = $this->fillFieldValue($name, $value);- $options['data-role'] = 'tagsinput';- $html = view('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render();+ $label = $this->label($name, $options);+ $options = $this->expandOptionArray($name, $label, $options);+ $classes = $this->getHolderClasses($name);+ $value = $this->fillFieldValue($name, $value);+ $html = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render();return $html;+}/*** @param $name+ * @param null $value* @param array $options** @return string*/- public function file($name, array $options = [])+ public function tags($name, $value = null, array $options = []){- $label = $this->label($name, $options);- $options = $this->expandOptionArray($name, $label, $options);- $classes = $this->getHolderClasses($name);- $html = view('form.file', compact('classes', 'name', 'label', 'options'))->render();+ $label = $this->label($name, $options);+ $options = $this->expandOptionArray($name, $label, $options);+ $classes = $this->getHolderClasses($name);+ $value = $this->fillFieldValue($name, $value);+ $options['data-role'] = 'tagsinput';+ $html = view('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render();return $html;-}/**@@ -440,4 +360,84 @@ class ExpandedFormreturn $html;}++ /**+ * @param $name+ * @param $label+ * @param array $options+ *+ * @return array+ */+ protected function expandOptionArray($name, $label, array $options)+ {+ $options['class'] = 'form-control';+ $options['id'] = 'ffInput_' . $name;+ $options['autocomplete'] = 'off';+ $options['placeholder'] = ucfirst($label);++ return $options;+ }++ /**+ * @param $name+ * @param $value+ *+ * @return mixed+ */+ protected function fillFieldValue($name, $value)+ {+ if (Session::has('preFilled')) {+ $preFilled = Session::get('preFilled');+ $value = isset($preFilled[$name]) && is_null($value) ? $preFilled[$name] : $value;+ }+ // @codeCoverageIgnoreStart+ try {+ if (!is_null(Input::old($name))) {+ $value = Input::old($name);+ }+ } catch (RuntimeException $e) {+ // don't care about session errors.+ }++ // @codeCoverageIgnoreEnd++ return $value;+ }++ /**+ * @param $name+ *+ * @return string+ */+ protected function getHolderClasses($name)+ {+ /*+ * Get errors from session:+ */+ /** @var MessageBag $errors */+ $errors = Session::get('errors');+ $classes = 'form-group';++ if (!is_null($errors) && $errors->has($name)) {+ $classes = 'form-group has-error has-feedback';+ }++ return $classes;+ }++ /**+ * @param $name+ * @param $options+ *+ * @return mixed+ */+ protected function label($name, $options)+ {+ if (isset($options['label'])) {+ return $options['label'];+ }++ return trans('form.' . $name);++ }}diff --git a/app/Support/Preferences.php b/app/Support/Preferences.phpindex e705f9cb55..531eb3e0ad 100644--- a/app/Support/Preferences.php+++ b/app/Support/Preferences.php@@ -13,16 +13,6 @@ use FireflyIII\Models\Preference;*/class Preferences{- /**- * @return string- */- public function lastActivity()- {- $preference = $this->get('lastActivity', microtime())->data;-- return md5($preference);- }-/*** @param string $name* @param string $default@@ -53,6 +43,26 @@ class Preferences}+ /**+ * @return string+ */+ public function lastActivity()+ {+ $preference = $this->get('lastActivity', microtime())->data;++ return md5($preference);+ }++ /**+ * @return bool+ */+ public function mark()+ {+ $this->set('lastActivity', microtime());++ return true;+ }+/*** @param $name* @param string $value@@ -80,14 +90,4 @@ class Preferencesreturn $pref;}-- /**- * @return bool- */- public function mark()- {- $this->set('lastActivity', microtime());-- return true;- }}diff --git a/app/Support/Steam.php b/app/Support/Steam.phpindex 647b75f138..0358e07abd 100644--- a/app/Support/Steam.php+++ b/app/Support/Steam.php@@ -16,27 +16,6 @@ use FireflyIII\Models\Transaction;class Steam{- /**- * @param array $accounts- *- * @return array- */- public function getLastActivities(array $accounts)- {- $list = [];-- $set = Auth::user()->transactions()- ->whereIn('account_id', $accounts)- ->groupBy('account_id')- ->get(['transactions.account_id', DB::raw('MAX(`transaction_journals`.`date`) as `max_date`')]);-- foreach ($set as $entry) {- $list[intval($entry->account_id)] = new Carbon($entry->max_date);- }-- return $list;- }-/**** @param \FireflyIII\Models\Account $account@@ -163,7 +142,29 @@ class Steamreturn $result;}+ /**+ * @param array $accounts+ *+ * @return array+ */+ public function getLastActivities(array $accounts)+ {+ $list = [];++ $set = Auth::user()->transactions()+ ->whereIn('account_id', $accounts)+ ->groupBy('account_id')+ ->get(['transactions.account_id', DB::raw('MAX(`transaction_journals`.`date`) as `max_date`')]);++ foreach ($set as $entry) {+ $list[intval($entry->account_id)] = new Carbon($entry->max_date);+ }++ return $list;+ }+// parse PHP size:+/*** @param $string*diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.phpindex d386645292..8fd6befc1f 100644--- a/app/Validation/FireflyValidator.php+++ b/app/Validation/FireflyValidator.php@@ -42,36 +42,51 @@ class FireflyValidator extends Validator/*** @param $attribute+ * @param $value+ * @param $parameters** @return bool*/- public function validateRuleTriggerValue($attribute)+ public function validateBelongsToUser($attribute, $value, $parameters){- // get the index from a string like "rule-trigger-value.2".- $parts = explode('.', $attribute);- $index = $parts[count($parts) - 1];- // loop all rule-triggers.- // check if rule-value matches the thing.- if (is_array($this->data['rule-trigger'])) {- $name = $this->getRuleTriggerName($index);- $value = $this->getRuleTriggerValue($index);- switch ($name) {- default:- return true;- case 'amount_less':- return is_numeric($value);- break;- case 'transaction_type':- $count = TransactionType::where('type', $value)->count();+ $count = DB::table($parameters[0])->where('user_id', Auth::user()->id)->where('id', $value)->count();+ if ($count == 1) {+ return true;+ }- return $count === 1;- break;- case 'invalid':- return false;- break;- }+ return false;++ }++ /**+ * @param $attribute+ * @param $value+ *+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)+ *+ * @return bool+ */+ public function validateIban($attribute, $value)+ {+ if (!is_string($value) || is_null($value) || strlen($value) < 6) {+ return false;}++ $value = strtoupper($value);++ $search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];+ $replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31',+ '32', '33', '34', '35'];++ // take+ $first = substr($value, 0, 4);+ $last = substr($value, 4);+ $iban = $last . $first;+ $iban = str_replace($search, $replace, $iban);+ $checksum = bcmod($iban, '97');++ return (intval($checksum) === 1);}/**@@ -116,54 +131,38 @@ class FireflyValidator extends Validatorreturn false;}-/*** @param $attribute- * @param $value- * @param $parameters** @return bool*/- public function validateBelongsToUser($attribute, $value, $parameters)+ public function validateRuleTriggerValue($attribute){+ // get the index from a string like "rule-trigger-value.2".+ $parts = explode('.', $attribute);+ $index = $parts[count($parts) - 1];- $count = DB::table($parameters[0])->where('user_id', Auth::user()->id)->where('id', $value)->count();- if ($count == 1) {- return true;- }-- return false;-- }+ // loop all rule-triggers.+ // check if rule-value matches the thing.+ if (is_array($this->data['rule-trigger'])) {+ $name = $this->getRuleTriggerName($index);+ $value = $this->getRuleTriggerValue($index);+ switch ($name) {+ default:+ return true;+ case 'amount_less':+ return is_numeric($value);+ break;+ case 'transaction_type':+ $count = TransactionType::where('type', $value)->count();- /**- * @param $attribute- * @param $value- *- * @SuppressWarnings(PHPMD.UnusedFormalParameter)- *- * @return bool- */- public function validateIban($attribute, $value)- {- if (!is_string($value) || is_null($value) || strlen($value) < 6) {- return false;+ return $count === 1;+ break;+ case 'invalid':+ return false;+ break;+ }}-- $value = strtoupper($value);-- $search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];- $replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31',- '32', '33', '34', '35'];-- // take- $first = substr($value, 0, 4);- $last = substr($value, 4);- $iban = $last . $first;- $iban = str_replace($search, $replace, $iban);- $checksum = bcmod($iban, '97');-- return (intval($checksum) === 1);}/**@@ -198,62 +197,92 @@ class FireflyValidator extends Validator}/**+ * @param $attribute+ * @param $value+ * @param $parameters+ *+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)+ ** @return bool*/- protected function validateAccountAnonymously()+ public function validateUniqueForUser($attribute, $value, $parameters){- if (!isset($this->data['user_id'])) {- return false;+ $query = DB::table($parameters[0])->where($parameters[1], $value);+ $query->where('user_id', Auth::user()->id);+ if (isset($parameters[2])) {+ $query->where('id', '!=', $parameters[2]);}-- $user = User::find($this->data['user_id']);- $type = AccountType::find($this->data['account_type_id'])->first();- $value = $this->tryDecrypt($this->data['name']);--- $set = $user->accounts()->where('account_type_id', $type->id)->get();- /** @var Account $entry */- foreach ($set as $entry) {- if ($entry->name == $value) {- return false;- }+ $count = $query->count();+ if ($count == 0) {+ return true;}- return true;+ return false;+}/**+ * Validate an object and its unicity. Checks for encryption / encrypted values as well.+ *+ * parameter 0: the table+ * parameter 1: the field+ * parameter 2: an id to ignore (when editing)+ *+ * @param $attribute* @param $value+ * @param $parameters*- * @return mixed+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)+ *+ * @return bool*/- protected function tryDecrypt($value)+ public function validateUniqueObjectForUser($attribute, $value, $parameters){- try {- $value = Crypt::decrypt($value);- } catch (DecryptException $e) {- // do not care.+ $value = $this->tryDecrypt($value);+ // exclude?+ $table = $parameters[0];+ $field = $parameters[1];+ $exclude = isset($parameters[2]) ? intval($parameters[2]) : 0;++ // get entries from table+ $set = DB::table($table)->where('user_id', Auth::user()->id)+ ->where('id', '!=', $exclude)->get([$field]);++ foreach ($set as $entry) {+ $fieldValue = $this->tryDecrypt($entry->$field);++ if ($fieldValue === $value) {+ return false;+ }}- return $value;+ return true;}/**+ * @param $attribute* @param $value* @param $parameters*+ * @SuppressWarnings(PHPMD.UnusedFormalParameter) // cant remove it+ * @SuppressWarnings(PHPMD.CyclomaticComplexity) // its as simple as I can get it.+ ** @return bool*/- protected function validateByAccountTypeString($value, $parameters)+ public function validateUniquePiggyBankForUser($attribute, $value, $parameters){- $search = Config::get('firefly.accountTypeByIdentifier.' . $this->data['what']);- $type = AccountType::whereType($search)->first();- $ignore = isset($parameters[0]) ? intval($parameters[0]) : 0;+ $exclude = isset($parameters[0]) ? $parameters[0] : null;+ $query = DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')+ ->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', Auth::user()->id);+ if (!is_null($exclude)) {+ $query->where('piggy_banks.id', '!=', $exclude);+ }+ $set = $query->get(['piggy_banks.*']);- $set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();- /** @var Account $entry */+ /** @var PiggyBank $entry */foreach ($set as $entry) {- if ($entry->name == $value) {+ $fieldValue = $this->tryDecrypt($entry->name);+ if ($fieldValue == $value) {return false;}}@@ -263,17 +292,35 @@ class FireflyValidator extends Validator/*** @param $value- * @param $parameters*+ * @return mixed+ */+ protected function tryDecrypt($value)+ {+ try {+ $value = Crypt::decrypt($value);+ } catch (DecryptException $e) {+ // do not care.+ }++ return $value;+ }++ /*** @return bool*/- protected function validateByAccountTypeId($value, $parameters)+ protected function validateAccountAnonymously(){- $type = AccountType::find($this->data['account_type_id'])->first();- $ignore = isset($parameters[0]) ? intval($parameters[0]) : 0;- $value = $this->tryDecrypt($value);+ if (!isset($this->data['user_id'])) {+ return false;+ }- $set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();+ $user = User::find($this->data['user_id']);+ $type = AccountType::find($this->data['account_type_id'])->first();+ $value = $this->tryDecrypt($this->data['name']);+++ $set = $user->accounts()->where('account_type_id', $type->id)->get();/** @var Account $entry */foreach ($set as $entry) {if ($entry->name == $value) {@@ -282,7 +329,6 @@ class FireflyValidator extends Validator}return true;-}/**@@ -314,92 +360,45 @@ class FireflyValidator extends Validator}/**- * @param $attribute- * @param $value- * @param $parameters- *- * @SuppressWarnings(PHPMD.UnusedFormalParameter)- *- * @return bool- */- public function validateUniqueForUser($attribute, $value, $parameters)- {- $query = DB::table($parameters[0])->where($parameters[1], $value);- $query->where('user_id', Auth::user()->id);- if (isset($parameters[2])) {- $query->where('id', '!=', $parameters[2]);- }- $count = $query->count();- if ($count == 0) {- return true;- }-- return false;-- }-- /**- * Validate an object and its unicity. Checks for encryption / encrypted values as well.- *- * parameter 0: the table- * parameter 1: the field- * parameter 2: an id to ignore (when editing)- *- * @param $attribute* @param $value* @param $parameters*- * @SuppressWarnings(PHPMD.UnusedFormalParameter)- ** @return bool*/- public function validateUniqueObjectForUser($attribute, $value, $parameters)+ protected function validateByAccountTypeId($value, $parameters){- $value = $this->tryDecrypt($value);- // exclude?- $table = $parameters[0];- $field = $parameters[1];- $exclude = isset($parameters[2]) ? intval($parameters[2]) : 0;-- // get entries from table- $set = DB::table($table)->where('user_id', Auth::user()->id)- ->where('id', '!=', $exclude)->get([$field]);+ $type = AccountType::find($this->data['account_type_id'])->first();+ $ignore = isset($parameters[0]) ? intval($parameters[0]) : 0;+ $value = $this->tryDecrypt($value);+ $set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();+ /** @var Account $entry */foreach ($set as $entry) {- $fieldValue = $this->tryDecrypt($entry->$field);-- if ($fieldValue === $value) {+ if ($entry->name == $value) {return false;}}return true;+}/**- * @param $attribute* @param $value* @param $parameters*- * @SuppressWarnings(PHPMD.UnusedFormalParameter) // cant remove it- * @SuppressWarnings(PHPMD.CyclomaticComplexity) // its as simple as I can get it.- ** @return bool*/- public function validateUniquePiggyBankForUser($attribute, $value, $parameters)+ protected function validateByAccountTypeString($value, $parameters){- $exclude = isset($parameters[0]) ? $parameters[0] : null;- $query = DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')- ->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', Auth::user()->id);- if (!is_null($exclude)) {- $query->where('piggy_banks.id', '!=', $exclude);- }- $set = $query->get(['piggy_banks.*']);+ $search = Config::get('firefly.accountTypeByIdentifier.' . $this->data['what']);+ $type = AccountType::whereType($search)->first();+ $ignore = isset($parameters[0]) ? intval($parameters[0]) : 0;- /** @var PiggyBank $entry */+ $set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();+ /** @var Account $entry */foreach ($set as $entry) {- $fieldValue = $this->tryDecrypt($entry->name);- if ($fieldValue == $value) {+ if ($entry->name == $value) {return false;}}