Sort by alphabet.

This commit is contained in:
James Cole
2016-01-20 15:23:36 +01:00
parent c84f4e2bc0
commit c9e4a09da6
11 changed files with 713 additions and 720 deletions

View File

@@ -364,30 +364,6 @@ class JournalRepository implements JournalRepositoryInterface
return [$fromAccount, $toAccount];
}
/**
* @param array $data
*
* @return array
*/
protected function storeWithdrawalAccounts(array $data)
{
$fromAccount = 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]
);
} else {
$toType = AccountType::where('type', 'Cash account')->first();
$toAccount = Account::firstOrCreateEncrypted(
['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]
);
}
return [$fromAccount, $toAccount];
}
/**
* @param array $data
*
@@ -411,4 +387,28 @@ class JournalRepository implements JournalRepositoryInterface
return [$fromAccount, $toAccount];
}
/**
* @param array $data
*
* @return array
*/
protected function storeWithdrawalAccounts(array $data)
{
$fromAccount = 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]
);
} else {
$toType = AccountType::where('type', 'Cash account')->first();
$toAccount = Account::firstOrCreateEncrypted(
['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]
);
}
return [$fromAccount, $toAccount];
}
}

View File

@@ -23,6 +23,99 @@ use FireflyIII\Models\RuleTrigger;
class RuleRepository implements RuleRepositoryInterface
{
/**
* @param Rule $rule
*
* @return bool
*/
public function destroy(Rule $rule)
{
foreach ($rule->ruleTriggers as $trigger) {
$trigger->delete();
}
foreach ($rule->ruleActions as $action) {
$action->delete();
}
$rule->delete();
return true;
}
/**
* @param RuleGroup $ruleGroup
*
* @return int
*/
public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup)
{
return intval($ruleGroup->rules()->max('order'));
}
/**
* @param Rule $rule
*
* @return bool
*/
public function moveDown(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);
}
/**
* @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);
}
/**
* @param Rule $rule
* @param array $ids
*
* @return bool
*/
public function reorderRuleActions(Rule $rule, array $ids)
{
$order = 1;
foreach ($ids as $actionId) {
/** @var RuleTrigger $trigger */
$action = $rule->ruleActions()->find($actionId);
if (!is_null($action)) {
$action->order = $order;
$action->save();
$order++;
}
}
return true;
}
/**
* @param Rule $rule
* @param array $ids
@@ -70,73 +163,6 @@ class RuleRepository implements RuleRepositoryInterface
}
/**
* @param Rule $rule
* @param array $ids
*
* @return bool
*/
public function reorderRuleActions(Rule $rule, array $ids)
{
$order = 1;
foreach ($ids as $actionId) {
/** @var RuleTrigger $trigger */
$action = $rule->ruleActions()->find($actionId);
if (!is_null($action)) {
$action->order = $order;
$action->save();
$order++;
}
}
return true;
}
/**
* @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);
}
/**
* @param Rule $rule
*
* @return bool
*/
public function moveDown(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);
}
/**
* @param array $data
*
@@ -185,58 +211,6 @@ class RuleRepository implements RuleRepositoryInterface
return $rule;
}
/**
* @param Rule $rule
* @param string $action
* @param string $value
* @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
*/
public function destroy(Rule $rule)
{
foreach ($rule->ruleTriggers as $trigger) {
$trigger->delete();
}
foreach ($rule->ruleActions as $action) {
$action->delete();
}
$rule->delete();
return true;
}
/**
* @param RuleGroup $ruleGroup
*
* @return int
*/
public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup)
{
return intval($ruleGroup->rules()->max('order'));
}
/**
* @param Rule $rule
* @param string $action
@@ -261,6 +235,29 @@ class RuleRepository implements RuleRepositoryInterface
return $ruleAction;
}
/**
* @param Rule $rule
* @param string $action
* @param string $value
* @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
* @param array $data

View File

@@ -36,27 +36,11 @@ interface RuleRepositoryInterface
public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup);
/**
* @param Rule $rule
* @param array $ids
* @param Rule $rule
*
* @return bool
*/
public function reorderRuleTriggers(Rule $rule, array $ids);
/**
* @param Rule $rule
* @param array $ids
*
* @return bool
*/
public function reorderRuleActions(Rule $rule, array $ids);
/**
* @param RuleGroup $ruleGroup
*
* @return bool
*/
public function resetRulesInGroupOrder(RuleGroup $ruleGroup);
public function moveDown(Rule $rule);
/**
* @param Rule $rule
@@ -67,18 +51,26 @@ interface RuleRepositoryInterface
/**
* @param Rule $rule
* @param array $data
*
* @return Rule
*/
public function update(Rule $rule, array $data);
/**
* @param Rule $rule
* @param array $ids
*
* @return bool
*/
public function moveDown(Rule $rule);
public function reorderRuleActions(Rule $rule, array $ids);
/**
* @param Rule $rule
* @param array $ids
*
* @return bool
*/
public function reorderRuleTriggers(Rule $rule, array $ids);
/**
* @param RuleGroup $ruleGroup
*
* @return bool
*/
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);
}

View File

@@ -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 TagRepositoryInterface
return $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 TagRepositoryInterface
return $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 $tag