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

@@ -44,17 +44,15 @@ class UpdateRequest extends FormRequest
*/ */
public function getAll(): array public function getAll(): array
{ {
$active = true; // This is the way.
$fields = [
if (null !== $this->get('active')) { 'title' => ['title', 'string'],
$active = $this->boolean('active'); 'description' => ['description', 'nlString'],
} 'active' => ['active', 'boolean'],
'order' => ['order', 'integer'],
return [
'title' => $this->string('title'),
'description' => $this->string('description'),
'active' => $active,
]; ];
return $this->getAllData($fields);
} }
/** /**
@@ -65,8 +63,9 @@ class UpdateRequest extends FormRequest
public function rules(): array public function rules(): array
{ {
$ruleGroup = $this->route()->parameter('ruleGroup'); $ruleGroup = $this->route()->parameter('ruleGroup');
return [ return [
'title' => 'required|between:1,100|uniqueObjectForUser:rule_groups,title,' . $ruleGroup->id, 'title' => 'between:1,100|uniqueObjectForUser:rule_groups,title,' . $ruleGroup->id,
'description' => 'between:1,5000|nullable', 'description' => 'between:1,5000|nullable',
'active' => [new IsBoolean], 'active' => [new IsBoolean],
]; ];

View File

@@ -47,13 +47,14 @@ class UpdateRequest extends FormRequest
*/ */
public function getAll(): array public function getAll(): array
{ {
$data = [ // This is the way.
'tag' => $this->string('tag'), $fields = [
'date' => $this->date('date'), 'tag' => ['tag', 'string'],
'description' => $this->string('description'), 'date' => ['date', 'date'],
'has_location' => true, // pretend location is present. 'description' => ['description', 'string'],
]; ];
$data = $this->appendLocationData($data, null); $data = $this->getAllData($fields);
$data = $this->appendLocationData($data, null);
return $data; return $data;
} }
@@ -66,9 +67,9 @@ class UpdateRequest extends FormRequest
public function rules(): array public function rules(): array
{ {
$tag = $this->route()->parameter('tagOrId'); $tag = $this->route()->parameter('tagOrId');
// TODO is uniqueObjectForUser not obsolete?
$rules = [ $rules = [
'tag' => 'required|min:1|uniqueObjectForUser:tags,tag,' . $tag->id, 'tag' => 'min:1|uniqueObjectForUser:tags,tag,' . $tag->id,
'description' => 'min:1|nullable', 'description' => 'min:1|nullable',
'date' => 'date|nullable', 'date' => 'date|nullable',
]; ];

View File

@@ -28,6 +28,7 @@ use FireflyIII\User;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
use DB;
/** /**
* Class RuleGroupRepository. * Class RuleGroupRepository.
@@ -357,11 +358,25 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
public function update(RuleGroup $ruleGroup, array $data): RuleGroup public function update(RuleGroup $ruleGroup, array $data): RuleGroup
{ {
// update the account: // update the account:
$ruleGroup->title = $data['title']; if (array_key_exists('title', $data)) {
$ruleGroup->description = $data['description']; $ruleGroup->title = $data['title'];
$ruleGroup->active = $data['active']; }
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(); $ruleGroup->save();
$this->resetRuleGroupOrder();
return $ruleGroup; return $ruleGroup;
} }
@@ -401,6 +416,59 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
$search->orderBy('rule_groups.order', 'ASC') $search->orderBy('rule_groups.order', 'ASC')
->orderBy('rule_groups.title', '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 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. * Delete everything.
*/ */

View File

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

View File

@@ -139,12 +139,14 @@ class JournalUpdateService
$this->updateField('date'); $this->updateField('date');
$this->updateField('order'); $this->updateField('order');
$this->transactionJournal->save(); $this->transactionJournal->save();
$this->transactionJournal->refresh(); $this->transactionJournal->refresh();
$this->updateCategory(); $this->updateCategory();
$this->updateBudget(); $this->updateBudget();
$this->updateTags(); $this->updateTags();
$this->updateReconciled();
$this->updateNotes(); $this->updateNotes();
$this->updateMeta(); $this->updateMeta();
$this->updateCurrency(); $this->updateCurrency();
@@ -750,4 +752,14 @@ class JournalUpdateService
} }
Log::debug('No type field present.'); Log::debug('No type field present.');
} }
/**
*
*/
private function updateReconciled(): void
{
if (array_key_exists('reconciled', $this->data) && is_bool($this->data['reconciled'])) {
$this->transactionJournal->transactions()->update(['reconciled' => $this->data['reconciled']]);
}
}
} }

View File

@@ -87,8 +87,6 @@ trait AppendsLocationData
$longitudeKey = $this->getLocationKey($prefix, 'longitude'); $longitudeKey = $this->getLocationKey($prefix, 'longitude');
$latitudeKey = $this->getLocationKey($prefix, 'latitude'); $latitudeKey = $this->getLocationKey($prefix, 'latitude');
$zoomLevelKey = $this->getLocationKey($prefix, 'zoom_level'); $zoomLevelKey = $this->getLocationKey($prefix, 'zoom_level');
$hasLocationKey = $this->getLocationKey($prefix, 'has_location');
$hasLocation = $this->boolean($hasLocationKey) || true === ($data['has_location'] ?? false);
$isValidPOST = $this->isValidPost($prefix); $isValidPOST = $this->isValidPost($prefix);
$isValidPUT = $this->isValidPUT($prefix); $isValidPUT = $this->isValidPUT($prefix);
$isValidEmptyPUT = $this->isValidEmptyPUT($prefix); $isValidEmptyPUT = $this->isValidEmptyPUT($prefix);
@@ -202,10 +200,11 @@ trait AppendsLocationData
return ( return (
null === $this->get($longitudeKey) null === $this->get($longitudeKey)
&& null === $this->get($latitudeKey) && null === $this->get($latitudeKey)
&& null === $this->has($zoomLevelKey)) && null === $this->get($zoomLevelKey))
&& (('PUT' === $this->method() && $this->routeIs('*.update')) && ('PUT' === $this->method()
|| ('POST' === $this->method() && $this->routeIs('*.update')) || ('POST' === $this->method() && $this->routeIs('*.update'))
); );
} }
} }

View File

@@ -265,6 +265,9 @@ trait ConvertsDataTypes
if (null === $value) { if (null === $value) {
return false; return false;
} }
if ('' === $value) {
return false;
}
if ('true' === $value) { if ('true' === $value) {
return true; return true;
} }

View File

@@ -538,7 +538,7 @@ class TransactionGroupTransformer extends AbstractTransformer
'sepa_cc' => $metaFieldData['sepa_cc'], 'sepa_cc' => $metaFieldData['sepa_cc'],
'sepa_ct_op' => $metaFieldData['sepa_ct_op'], 'sepa_ct_op' => $metaFieldData['sepa_ct_op'],
'sepa_ct_id' => $metaFieldData['sepa_ct_id'], 'sepa_ct_id' => $metaFieldData['sepa_ct_id'],
'sepa_db' => $metaFieldData['sepa_ddb'], 'sepa_db' => $metaFieldData['sepa_db'],
'sepa_country' => $metaFieldData['sepa_country'], 'sepa_country' => $metaFieldData['sepa_country'],
'sepa_ep' => $metaFieldData['sepa_ep'], 'sepa_ep' => $metaFieldData['sepa_ep'],
'sepa_ci' => $metaFieldData['sepa_ci'], 'sepa_ci' => $metaFieldData['sepa_ci'],