Fix small errors in bulk and mass controller

This commit is contained in:
James Cole 2018-03-04 09:12:33 +01:00
parent 8b52006959
commit 2f17521c06
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 69 additions and 48 deletions

View File

@ -168,7 +168,7 @@ class BulkController extends Controller
}
if ($ignoreTags === false) {
Log::debug(sprintf('Set tags to %s', $request->string('budget_id')));
$repository->updateTags($journal, explode(',', $request->string('tags')));
$repository->updateTags($journal,['tags' => explode(',', $request->string('tags'))]);
}
// update tags if not told to ignore (and is withdrawal)
}

View File

@ -115,6 +115,8 @@ class MassController extends Controller
}
/**
* TODO this code is a mess.
*
* @param Collection $journals
*
* @return View
@ -222,10 +224,11 @@ class MassController extends Controller
if (!is_null($journal)) {
// get optional fields:
$what = strtolower($this->repository->getTransactionType($journal));
$sourceAccountId = $request->get('source_account_id')[$journal->id] ?? 0;
$sourceAccountName = $request->get('source_account_name')[$journal->id] ?? '';
$destAccountId = $request->get('destination_account_id')[$journal->id] ?? 0;
$destAccountName = $request->get('destination_account_name')[$journal->id] ?? '';
$sourceAccountId = $request->get('source_account_id')[$journal->id] ?? null;
$currencyId = $request->get('transaction_currency_id')[$journal->id] ?? 1;
$sourceAccountName = $request->get('source_account_name')[$journal->id] ?? null;
$destAccountId = $request->get('destination_account_id')[$journal->id] ?? null;
$destAccountName = $request->get('destination_account_name')[$journal->id] ?? null;
$budgetId = $request->get('budget_id')[$journal->id] ?? 0;
$category = $request->get('category')[$journal->id];
$tags = $journal->tags->pluck('tag')->toArray();
@ -233,29 +236,47 @@ class MassController extends Controller
$foreignAmount = isset($request->get('foreign_amount')[$journal->id]) ? round($request->get('foreign_amount')[$journal->id], 12) : null;
$foreignCurrencyId = isset($request->get('foreign_currency_id')[$journal->id]) ?
intval($request->get('foreign_currency_id')[$journal->id]) : null;
$notes = $repository->getNoteText($journal);
// build data array
$data = [
'id' => $journal->id,
'what' => $what,
'description' => $request->get('description')[$journal->id],
'source_account_id' => intval($sourceAccountId),
'source_account_name' => $sourceAccountName,
'destination_account_id' => intval($destAccountId),
'destination_account_name' => $destAccountName,
'amount' => $foreignAmount,
'native_amount' => $amount,
'source_amount' => $amount,
'date' => new Carbon($request->get('date')[$journal->id]),
'interest_date' => $journal->interest_date,
'book_date' => $journal->book_date,
'process_date' => $journal->process_date,
'budget_id' => intval($budgetId),
'currency_id' => $foreignCurrencyId,
'foreign_amount' => $foreignAmount,
'destination_amount' => $foreignAmount,
'category' => $category,
'tags' => $tags,
'id' => $journal->id,
'what' => $what,
'description' => $request->get('description')[$journal->id],
'date' => new Carbon($request->get('date')[$journal->id]),
'bill_id' => null,
'bill_name' => null,
'notes' => $notes,
'transactions' => [[
'category_id' => null,
'category_name' => $category,
'budget_id' => intval($budgetId),
'budget_name' => null,
'source_id' => intval($sourceAccountId),
'source_name' => $sourceAccountName,
'destination_id' => intval($destAccountId),
'destination_name' => $destAccountName,
'amount' => $amount,
'identifier' => 0,
'reconciled' => false,
'currency_id' => intval($currencyId),
'currency_code' => null,
'description' => null,
'foreign_amount' => null,
'foreign_currency_id' => $foreignCurrencyId,
'foreign_currency_code' => null,
//'native_amount' => $amount,
//'source_amount' => $amount,
//'foreign_amount' => $foreignAmount,
//'destination_amount' => $foreignAmount,
//'amount' => $foreignAmount,
]],
'currency_id' => $foreignCurrencyId,
'tags' => $tags,
'interest_date' => $journal->interest_date,
'book_date' => $journal->book_date,
'process_date' => $journal->process_date,
];
// call repository update function.
$repository->update($journal, $data);

View File

@ -37,6 +37,28 @@ use FireflyIII\Models\TransactionJournal;
trait JournalServiceTrait
{
/**
* @param TransactionJournal $journal
* @param array $data
*/
public function connectTags(TransactionJournal $journal, array $data): void
{
/** @var TagFactory $factory */
$factory = app(TagFactory::class);
$factory->setUser($journal->user);
$set = [];
if (!is_array($data['tags'])) {
return; // @codeCoverageIgnore
}
foreach ($data['tags'] as $string) {
if (strlen($string) > 0) {
$tag = $factory->findOrCreate($string);
$set[] = $tag->id;
}
}
$journal->tags()->sync($set);
}
/**
* Connect bill if present.
*
@ -62,28 +84,6 @@ trait JournalServiceTrait
return;
}
/**
* @param TransactionJournal $journal
* @param array $data
*/
protected function connectTags(TransactionJournal $journal, array $data): void
{
/** @var TagFactory $factory */
$factory = app(TagFactory::class);
$factory->setUser($journal->user);
$set = [];
if (!is_array($data['tags'])) {
return; // @codeCoverageIgnore
}
foreach ($data['tags'] as $string) {
if (strlen($string) > 0) {
$tag = $factory->findOrCreate($string);
$set[] = $tag->id;
}
}
$journal->tags()->sync($set);
}
/**
* @param TransactionJournal $journal
* @param array $data