Fix tags and rule groups.

This commit is contained in:
James Cole
2021-03-11 06:29:07 +01:00
parent 75c8ec7c0a
commit 625e31d053
9 changed files with 158 additions and 43 deletions

View File

@@ -28,6 +28,7 @@ use FireflyIII\User;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Log;
use DB;
/**
* Class RuleGroupRepository.
@@ -357,11 +358,25 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
public function update(RuleGroup $ruleGroup, array $data): RuleGroup
{
// update the account:
$ruleGroup->title = $data['title'];
$ruleGroup->description = $data['description'];
$ruleGroup->active = $data['active'];
if (array_key_exists('title', $data)) {
$ruleGroup->title = $data['title'];
}
if (array_key_exists('description', $data)) {
$ruleGroup->description = $data['description'];
}
if (array_key_exists('active', $data)) {
$ruleGroup->active = $data['active'];
}
// order
if (array_key_exists('order', $data) && $ruleGroup->order !== $data['order']) {
$this->correctRuleGroupOrder();
$max = $this->maxOrder();
// TODO also for bills and accounts:
$data['order'] = $data['order'] > $max ? $max : $data['order'];
$ruleGroup = $this->updateOrder($ruleGroup, $ruleGroup->order, $data['order']);
}
$ruleGroup->save();
$this->resetRuleGroupOrder();
return $ruleGroup;
}
@@ -401,6 +416,59 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
$search->orderBy('rule_groups.order', 'ASC')
->orderBy('rule_groups.title', 'ASC');
return $search->take($limit)->get(['id','title','description']);
return $search->take($limit)->get(['id', 'title', 'description']);
}
/**
* @inheritDoc
*/
public function correctRuleGroupOrder(): void
{
$set = $this->user
->ruleGroups()
->orderBy('order', 'ASC')
->orderBy('active', 'DESC')
->orderBy('title', 'ASC')
->get(['rule_groups.id']);
$index = 1;
/** @var RuleGroup $ruleGroup */
foreach ($set as $ruleGroup) {
if ($ruleGroup->order !== $index) {
$ruleGroup->order = $index;
$ruleGroup->save();
}
$index++;
}
}
/**
* @inheritDoc
*/
public function updateOrder(RuleGroup $ruleGroup, int $oldOrder, int $newOrder): RuleGroup
{
if ($newOrder > $oldOrder) {
$this->user->ruleGroups()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
->where('rule_groups.id', '!=', $ruleGroup->id)
->update(['order' => DB::raw('rule_groups.order-1')]);
$ruleGroup->order = $newOrder;
$ruleGroup->save();
}
if ($newOrder < $oldOrder) {
$this->user->ruleGroups()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
->where('rule_groups.id', '!=', $ruleGroup->id)
->update(['order' => DB::raw('rule_groups.order+1')]);
$ruleGroup->order = $newOrder;
$ruleGroup->save();
}
return $ruleGroup;
}
/**
* @inheritDoc
*/
public function maxOrder(): int
{
return (int)$this->user->ruleGroups()->max('order');
}
}

View File

@@ -31,6 +31,29 @@ use Illuminate\Support\Collection;
*/
interface RuleGroupRepositoryInterface
{
/**
* Make sure rule group order is correct in DB.
*/
public function correctRuleGroupOrder(): void;
/**
*
* @param RuleGroup $ruleGroup
* @param int $oldOrder
* @param int $newOrder
*
* @return RuleGroup
*/
public function updateOrder(RuleGroup $ruleGroup, int $oldOrder, int $newOrder): RuleGroup;
/**
* Get highest possible order for a rule group.
*
* @return int
*/
public function maxOrder(): int;
/**
* Delete everything.
*/

View File

@@ -28,8 +28,6 @@ use FireflyIII\Factory\TagFactory;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\Location;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
@@ -317,7 +315,7 @@ class TagRepository implements TagRepositoryInterface
/** @var array $journal */
foreach ($journals as $journal) {
$currencyId = (int) $journal['currency_id'];
$currencyId = (int)$journal['currency_id'];
$sums[$currencyId] = $sums[$currencyId] ?? [
'currency_id' => $currencyId,
'currency_name' => $journal['currency_name'],
@@ -331,7 +329,7 @@ class TagRepository implements TagRepositoryInterface
];
// add amount to correct type:
$amount = app('steam')->positive((string) $journal['amount']);
$amount = app('steam')->positive((string)$journal['amount']);
$type = $journal['transaction_type_type'];
if (TransactionType::WITHDRAWAL === $type) {
$amount = bcmul($amount, '-1');
@@ -352,7 +350,7 @@ class TagRepository implements TagRepositoryInterface
TransactionType::OPENING_BALANCE => '0',
];
// add foreign amount to correct type:
$amount = app('steam')->positive((string) $journal['foreign_amount']);
$amount = app('steam')->positive((string)$journal['foreign_amount']);
$type = $journal['transaction_type_type'];
if (TransactionType::WITHDRAWAL === $type) {
$amount = bcmul($amount, '-1');
@@ -361,6 +359,7 @@ class TagRepository implements TagRepositoryInterface
}
}
return $sums;
}
@@ -395,7 +394,7 @@ class TagRepository implements TagRepositoryInterface
Log::debug(sprintf('Each coin in a tag earns it %s points', $pointsPerCoin));
/** @var Tag $tag */
foreach ($tags as $tag) {
$amount = (string) $tag->amount_sum;
$amount = (string)$tag->amount_sum;
$amount = '' === $amount ? '0' : $amount;
$amountMin = bcsub($amount, $min);
$pointsForTag = bcmul($amountMin, $pointsPerCoin);
@@ -440,16 +439,24 @@ class TagRepository implements TagRepositoryInterface
*/
public function update(Tag $tag, array $data): Tag
{
$tag->tag = $data['tag'];
$tag->date = $data['date'];
$tag->description = $data['description'];
$tag->latitude = null;
$tag->longitude = null;
$tag->zoomLevel = null;
if (array_key_exists('tag', $data)) {
$tag->tag = $data['tag'];
}
if (array_key_exists('date', $data)) {
$tag->date = $data['date'];
}
if (array_key_exists('description', $data)) {
$tag->description = $data['description'];
}
$tag->latitude = null;
$tag->longitude = null;
$tag->zoomLevel = null;
$tag->save();
// update, delete or create location:
$updateLocation = $data['update_location'] ?? false;
$deleteLocation = $data['remove_location'] ?? false;
// location must be updated?
if (true === $updateLocation) {
@@ -472,6 +479,9 @@ class TagRepository implements TagRepositoryInterface
$location->save();
}
}
if(true === $deleteLocation) {
$tag->locations()->delete();
}
return $tag;
}
@@ -486,7 +496,7 @@ class TagRepository implements TagRepositoryInterface
$max = '0';
/** @var Tag $tag */
foreach ($tags as $tag) {
$amount = (string) $tag->amount_sum;
$amount = (string)$tag->amount_sum;
$amount = '' === $amount ? '0' : $amount;
$max = 1 === bccomp($amount, $max) ? $amount : $max;
@@ -508,7 +518,7 @@ class TagRepository implements TagRepositoryInterface
/** @var Tag $tag */
foreach ($tags as $tag) {
$amount = (string) $tag->amount_sum;
$amount = (string)$tag->amount_sum;
$amount = '' === $amount ? '0' : $amount;
if (null === $min) {