Update views and routes for #1078

This commit is contained in:
James Cole
2017-12-30 14:25:11 +01:00
parent 73051d7d42
commit 3fba741f1b
11 changed files with 153 additions and 108 deletions

View File

@@ -22,14 +22,15 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Journal;
use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
use FireflyIII\Models\Note;
use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
@@ -435,44 +436,40 @@ class JournalRepository implements JournalRepositoryInterface
}
/**
* @param array $journals
* @param var $category
* @param var $tags
* @param TransactionJournal $journal
* @param int $budgetId
*
* @return int
* @return TransactionJournal
*/
public function updateBulk(array $journals, $category, $tags): int
public function updateBudget(TransactionJournal $journal, int $budgetId): TransactionJournal
{
$count = 0;
foreach ($journals as $journalId) {
$journal = $this->find(intval($journalId));
if ($journal) {
// update category:
if (isset($category)) {
$categoryToReplace = Category::firstOrCreateEncrypted(['name' => strval($category), 'user_id' => $journal->user->id]);
$journal->categories()->sync([$categoryToReplace->id]);
/** @var Transaction $transaction */
foreach ($journal->transactions()->getResults() as $transaction) {
$transaction->categories()->sync([$categoryToReplace->id]);
$transaction->touch();
}
}
if ($budgetId === 0) {
$journal->budgets()->detach();
$journal->save();
// update tags:
if (isset($tags)) {
$tagsToReplace = [];
foreach (explode(',', strval($tags)) as $tag) {
array_push($tagsToReplace, Tag::firstOrCreateEncrypted(['tag' => $tag, 'user_id' => $journal->user->id])->id);
}
$journal->tags()->sync($tagsToReplace);
}
$journal->touch();
++$count;
}
return $journal;
}
$this->storeBudgetWithJournal($journal, $budgetId);
return $count;
return $journal;
}
/**
* @param TransactionJournal $journal
* @param string $category
*
* @return TransactionJournal
*/
public function updateCategory(TransactionJournal $journal, string $category): TransactionJournal
{
Log::debug(sprintf('In updateCategory("%s")', $category));
$journal->categories()->detach();
if (strlen($category) === 0) {
return $journal;
}
$this->storeCategoryWithJournal($journal, $category);
return $journal;
}
/**
@@ -535,4 +532,48 @@ class JournalRepository implements JournalRepositoryInterface
return $journal;
}
/**
* Update tags.
*
* @param TransactionJournal $journal
* @param array $array
*
* @return bool
*/
public function updateTags(TransactionJournal $journal, array $array): bool
{
// create tag repository
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = app(TagRepositoryInterface::class);
// find or create all tags:
$tags = [];
$ids = [];
foreach ($array as $name) {
if (strlen(trim($name)) > 0) {
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
$tags[] = $tag;
$ids[] = $tag->id;
}
}
// delete all tags connected to journal not in this array:
if (count($ids) > 0) {
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->whereNotIn('tag_id', $ids)->delete();
}
// if count is zero, delete them all:
if (0 === count($ids)) {
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->delete();
}
// connect each tag to journal (if not yet connected):
/** @var Tag $tag */
foreach ($tags as $tag) {
Log::debug(sprintf('Will try to connect tag #%d to journal #%d.', $tag->id, $journal->id));
$tagRepository->connect($journal, $tag);
}
return true;
}
}

View File

@@ -161,13 +161,20 @@ interface JournalRepositoryInterface
public function update(TransactionJournal $journal, array $data): TransactionJournal;
/**
* @param array $journals
* @param var $category
* @param var $tags
* @param TransactionJournal $journal
* @param int $budgetId
*
* @return int
* @return TransactionJournal
*/
public function updateBulk(array $journals, $category, $tags): int;
public function updateBudget(TransactionJournal $journal, int $budgetId): TransactionJournal;
/**
* @param TransactionJournal $journal
* @param string $category
*
* @return TransactionJournal
*/
public function updateCategory(TransactionJournal $journal, string $category): TransactionJournal;
/**
* @param TransactionJournal $journal
@@ -176,4 +183,12 @@ interface JournalRepositoryInterface
* @return TransactionJournal
*/
public function updateSplitJournal(TransactionJournal $journal, array $data): TransactionJournal;
/**
* @param TransactionJournal $journal
* @param array $tags
*
* @return bool
*/
public function updateTags(TransactionJournal $journal, array $tags): bool;
}

View File

@@ -100,6 +100,7 @@ trait SupportJournalsTrait
$budget = Budget::find($budgetId);
$journal->budgets()->save($budget);
}
$journal->touch();
}
/**
@@ -112,6 +113,7 @@ trait SupportJournalsTrait
$category = Category::firstOrCreateEncrypted(['name' => $category, 'user_id' => $journal->user_id]);
$journal->categories()->save($category);
}
$journal->touch();
}
/**

View File

@@ -37,6 +37,7 @@ use Log;
*/
trait UpdateJournalsTrait
{
/**
* When the user edits a split journal, each line is missing crucial data:.
*
@@ -121,47 +122,5 @@ trait UpdateJournalsTrait
$transaction->save();
}
/**
* Update tags.
*
* @param TransactionJournal $journal
* @param array $array
*
* @return bool
*/
protected function updateTags(TransactionJournal $journal, array $array): bool
{
// create tag repository
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = app(TagRepositoryInterface::class);
// find or create all tags:
$tags = [];
$ids = [];
foreach ($array as $name) {
if (strlen(trim($name)) > 0) {
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
$tags[] = $tag;
$ids[] = $tag->id;
}
}
// delete all tags connected to journal not in this array:
if (count($ids) > 0) {
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->whereNotIn('tag_id', $ids)->delete();
}
// if count is zero, delete them all:
if (0 === count($ids)) {
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->delete();
}
// connect each tag to journal (if not yet connected):
/** @var Tag $tag */
foreach ($tags as $tag) {
Log::debug(sprintf('Will try to connect tag #%d to journal #%d.', $tag->id, $journal->id));
$tagRepository->connect($journal, $tag);
}
return true;
}
}