Improve mass controller and test controllers.

This commit is contained in:
James Cole
2019-07-20 16:02:50 +02:00
parent 6d34cfb940
commit 889b7e9a18
18 changed files with 525 additions and 542 deletions

View File

@@ -63,6 +63,8 @@ class BulkController extends Controller
/**
* Edit a set of journals in bulk.
*
* TODO user wont be able to tell if journal is part of split.
*
* @param Collection $journals
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
@@ -71,6 +73,8 @@ class BulkController extends Controller
{
$subTitle = (string)trans('firefly.mass_bulk_journals');
// make amounts positive.
// get list of budgets:
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);

View File

@@ -25,23 +25,19 @@ namespace FireflyIII\Http\Controllers\Transaction;
use Carbon\Carbon;
use FireflyIII\Events\UpdatedTransactionGroup;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Filter\TransactionViewFilter;
use FireflyIII\Helpers\Filter\TransferFilter;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\MassDeleteJournalRequest;
use FireflyIII\Http\Requests\MassEditJournalRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Transformers\TransactionTransformer;
use FireflyIII\User;
use Illuminate\Support\Collection;
use FireflyIII\Services\Internal\Update\JournalUpdateService;
use Illuminate\View\View as IlluminateView;
use Symfony\Component\HttpFoundation\ParameterBag;
use InvalidArgumentException;
use Log;
/**
* Class MassController.
@@ -55,6 +51,7 @@ class MassController extends Controller
/**
* MassController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
@@ -65,7 +62,6 @@ class MassController extends Controller
app('view')->share('title', (string)trans('firefly.transactions'));
app('view')->share('mainTitleIcon', 'fa-repeat');
$this->repository = app(JournalRepositoryInterface::class);
return $next($request);
}
);
@@ -74,11 +70,11 @@ class MassController extends Controller
/**
* Mass delete transactions.
*
* @param Collection $journals
* @param array $journals
*
* @return IlluminateView
*/
public function delete(Collection $journals): IlluminateView
public function delete(array $journals): IlluminateView
{
$subTitle = (string)trans('firefly.mass_delete_journals');
@@ -103,10 +99,11 @@ class MassController extends Controller
if (is_array($ids)) {
/** @var string $journalId */
foreach ($ids as $journalId) {
/** @var TransactionJournal $journal */
$journal = $this->repository->findNull((int)$journalId);
if (null !== $journal && (int)$journalId === $journal->id) {
$this->repository->destroy($journal);
$this->repository->destroyJournal($journal);
++$count;
}
}
@@ -123,137 +120,69 @@ class MassController extends Controller
/**
* Mass edit of journals.
*
* @param Collection $journals
* @param array $journals
*
* @return IlluminateView
*
* TODO rebuild this feature.
* @throws FireflyException
*/
public function edit(Collection $journals): IlluminateView
public function edit(array $journals): IlluminateView
{
throw new FireflyException(sprintf('The mass-editor is not available in v%s of Firefly III. Sorry about that. It will be back soon.', config('firefly.version')));
/** @var User $user */
$user = auth()->user();
$subTitle = (string)trans('firefly.mass_edit_journals');
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
// valid withdrawal sources:
$array = array_keys(config(sprintf('firefly.source_dests.%s', TransactionType::WITHDRAWAL)));
$withdrawalSources = $repository->getAccountsByType($array);
// valid deposit destinations:
$array = config(sprintf('firefly.source_dests.%s.%s', TransactionType::DEPOSIT, AccountType::REVENUE));
$depositDestinations = $repository->getAccountsByType($array);
/** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app(BudgetRepositoryInterface::class);
$budgets = $budgetRepository->getBudgets();
// reverse amounts
foreach ($journals as $index => $journal) {
$journals[$index]['amount'] = app('steam')->positive($journal['amount']);
$journals[$index]['foreign_amount'] = null === $journal['foreign_amount'] ?
null : app('steam')->positive($journal['foreign_amount']);
}
$this->rememberPreviousUri('transactions.mass-edit.uri');
/** @var TransactionTransformer $transformer */
$transformer = app(TransactionTransformer::class);
$transformer->setParameters(new ParameterBag);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($user);
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
$collector->setJournals($journals);
$collector->addFilter(TransactionViewFilter::class);
$collector->addFilter(TransferFilter::class);
$collection = $collector->getTransactions();
$transactions = $collection->map(
function (Transaction $transaction) use ($transformer) {
$transformed = $transformer->transform($transaction);
// make sure amount is positive:
$transformed['amount'] = app('steam')->positive((string)$transformed['amount']);
$transformed['foreign_amount'] = app('steam')->positive((string)$transformed['foreign_amount']);
return $transformed;
}
);
return view('transactions.mass.edit', compact('transactions', 'subTitle', 'accounts', 'budgets'));
return view('transactions.mass.edit', compact('journals', 'subTitle', 'withdrawalSources', 'depositDestinations', 'budgets'));
}
/**
* Mass update of journals.
*
* @param MassEditJournalRequest $request
* @param JournalRepositoryInterface $repository
*
* @return mixed
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws FireflyException
*/
public function update(MassEditJournalRequest $request, JournalRepositoryInterface $repository)
public function update(MassEditJournalRequest $request)
{
throw new FireflyException('Needs refactor');
$journalIds = $request->get('journals');
$count = 0;
if (is_array($journalIds)) {
foreach ($journalIds as $journalId) {
$journal = $repository->findNull((int)$journalId);
if (null !== $journal) {
// get optional fields:
$what = strtolower($this->repository->getTransactionType($journal));
$sourceAccountId = $request->get('source_id')[$journal->id] ?? null;
$currencyId = $request->get('transaction_currency_id')[$journal->id] ?? 1;
$sourceAccountName = $request->get('source_name')[$journal->id] ?? null;
$destAccountId = $request->get('destination_id')[$journal->id] ?? null;
$destAccountName = $request->get('destination_name')[$journal->id] ?? null;
$budgetId = (int)($request->get('budget_id')[$journal->id] ?? 0.0);
$category = $request->get('category')[$journal->id];
$tags = $journal->tags->pluck('tag')->toArray();
$amount = round($request->get('amount')[$journal->id], 12);
$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]) ?
(int)$request->get('foreign_currency_id')[$journal->id] : null;
// build data array
$data = [
'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' => $repository->getNoteText($journal),
'transactions' => [[
'category_id' => null,
'category_name' => $category,
'budget_id' => $budgetId,
'budget_name' => null,
'source_id' => (int)$sourceAccountId,
'source_name' => $sourceAccountName,
'destination_id' => (int)$destAccountId,
'destination_name' => $destAccountName,
'amount' => $amount,
'identifier' => 0,
'reconciled' => false,
'currency_id' => (int)$currencyId,
'currency_code' => null,
'description' => null,
'foreign_amount' => $foreignAmount,
'foreign_currency_id' => $foreignCurrencyId,
'foreign_currency_code' => null,
]],
'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);
// trigger rules
event(new UpdatedTransactionGroup($group));
++$count;
}
if (!is_array($journalIds)) {
// TODO something error.
throw new FireflyException('This is not an array.'); // @codeCoverageIgnore
}
$count = 0;
/** @var string $journalId */
foreach ($journalIds as $journalId) {
$integer = (int)$journalId;
try {
$this->updateJournal($integer, $request);
$count++;
} catch (FireflyException $e) { // @codeCoverageIgnore
// do something with error.
//echo $e->getMessage();
//exit;
}
}
app('preferences')->mark();
session()->flash('success', (string)trans('firefly.mass_edited_transactions_success', ['amount' => $count]));
@@ -261,4 +190,106 @@ class MassController extends Controller
return redirect($this->getPreviousUri('transactions.mass-edit.uri'));
}
/**
* @param int $journalId
* @param MassEditJournalRequest $request
* @throws FireflyException
*/
private function updateJournal(int $journalId, MassEditJournalRequest $request): void
{
$journal = $this->repository->findNull($journalId);
if (null === $journal) {
throw new FireflyException(sprintf('Trying to edit non-existent or deleted journal #%d', $journalId)); // @codeCoverageIgnore
}
$service = app(JournalUpdateService::class);
// for each field, call the update service.
$service->setTransactionJournal($journal);
$data = [
'date' => $this->getDateFromRequest($request, $journal->id, 'date'),
'description' => $this->getStringFromRequest($request, $journal->id, 'description'),
'source_id' => $this->getIntFromRequest($request, $journal->id, 'source_id'),
'source_name' => $this->getStringFromRequest($request, $journal->id, 'source_name'),
'destination_id' => $this->getIntFromRequest($request, $journal->id, 'destination_id'),
'destination_name' => $this->getStringFromRequest($request, $journal->id, 'destination_name'),
'budget_id' => $this->getIntFromRequest($request, $journal->id, 'budget_id'),
'category_name' => $this->getStringFromRequest($request, $journal->id, 'category'),
'amount' => $this->getStringFromRequest($request, $journal->id, 'amount'),
'foreign_amount' => $this->getStringFromRequest($request, $journal->id, 'foreign_amount'),
];
Log::debug(sprintf('Will update journal #%d with data.', $journal->id), $data);
// call service to update.
$service->setData($data);
$service->update();
// trigger rules
event(new UpdatedTransactionGroup($journal->transactionGroup));
}
/**
* @param MassEditJournalRequest $request
* @param int $journalId
* @param string $string
* @return int|null
* @codeCoverageIgnore
*/
private function getIntFromRequest(MassEditJournalRequest $request, int $journalId, string $string): ?int
{
$value = $request->get($string);
if (!is_array($value)) {
return null;
}
if (!isset($value[$journalId])) {
return null;
}
return (int)$value[$journalId];
}
/**
* @param MassEditJournalRequest $request
* @param int $journalId
* @param string $string
* @return string|null
* @codeCoverageIgnore
*/
private function getStringFromRequest(MassEditJournalRequest $request, int $journalId, string $string): ?string
{
$value = $request->get($string);
if (!is_array($value)) {
return null;
}
if (!isset($value[$journalId])) {
return null;
}
return (string)$value[$journalId];
}
/**
* @param MassEditJournalRequest $request
* @param int $journalId
* @param string $string
* @return Carbon|null
* @codeCoverageIgnore
*/
private function getDateFromRequest(MassEditJournalRequest $request, int $journalId, string $string): ?Carbon
{
$value = $request->get($string);
if (!is_array($value)) {
return null;
}
if (!isset($value[$journalId])) {
return null;
}
try {
$carbon = Carbon::parse($value[$journalId]);
} catch (InvalidArgumentException $e) {
$e->getMessage();
return null;
}
return $carbon;
}
}

View File

@@ -41,7 +41,7 @@ class ShowController extends Controller
private $repository;
/**
* ConvertController constructor.
* ShowController constructor.
*/
public function __construct()
{
@@ -81,8 +81,29 @@ class ShowController extends Controller
$groupArray = $transformer->transformObject($transactionGroup);
// do some amount calculations:
$amounts = $this->getAmounts($groupArray);
$events = $this->repository->getPiggyEvents($transactionGroup);
$attachments = $this->repository->getAttachments($transactionGroup);
$links = $this->repository->getLinks($transactionGroup);
return view(
'transactions.show', compact(
'transactionGroup', 'amounts', 'first', 'type', 'subTitle', 'splits', 'groupArray',
'events', 'attachments', 'links', 'message'
)
);
}
/**
* @param array $group
* @return array
*/
private function getAmounts(array $group): array
{
$amounts = [];
foreach ($groupArray['transactions'] as $transaction) {
foreach ($group['transactions'] as $transaction) {
$symbol = $transaction['currency_symbol'];
if (!isset($amounts[$symbol])) {
$amounts[$symbol] = [
@@ -106,15 +127,6 @@ class ShowController extends Controller
}
}
$events = $this->repository->getPiggyEvents($transactionGroup);
$attachments = $this->repository->getAttachments($transactionGroup);
$links = $this->repository->getLinks($transactionGroup);
return view(
'transactions.show', compact(
'transactionGroup', 'amounts', 'first', 'type', 'subTitle', 'splits', 'groupArray',
'events', 'attachments', 'links', 'message'
)
);
return $amounts;
}
}