This commit is contained in:
James Cole 2018-04-24 19:48:42 +02:00
parent 846df21764
commit 48357d1cc9
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 58 additions and 56 deletions

View File

@ -88,39 +88,44 @@ class BulkController extends Controller
/**
* @param BulkEditJournalRequest $request
* @param JournalRepositoryInterface $repository
*
* @return mixed
*/
public function update(BulkEditJournalRequest $request, JournalRepositoryInterface $repository)
public function update(BulkEditJournalRequest $request)
{
$journalIds = $request->get('journals');
$journalIds = \is_array($journalIds) ? $journalIds : [];
$ignoreCategory = (int)$request->get('ignore_category') === 1;
$ignoreBudget = (int)$request->get('ignore_budget') === 1;
$ignoreTags = (int)$request->get('ignore_tags') === 1;
$count = 0;
if (\is_array($journalIds)) {
foreach ($journalIds as $journalId) {
$journal = $repository->find((int)$journalId);
$count++;
Log::debug(sprintf('Found journal #%d', $journal->id));
foreach ($journalIds as $journalId) {
$journal = $this->repository->findNull((int)$journalId);
if (null === $journal) {
continue;
}
// update category if not told to ignore
if ($ignoreCategory === false) {
Log::debug(sprintf('Set category to %s', $request->string('category')));
$count++;
Log::debug(sprintf('Found journal #%d', $journal->id));
$repository->updateCategory($journal, $request->string('category'));
}
// update budget if not told to ignore (and is withdrawal)
if ($ignoreBudget === false) {
Log::debug(sprintf('Set budget to %d', $request->integer('budget_id')));
$repository->updateBudget($journal, $request->integer('budget_id'));
}
if ($ignoreTags === false) {
Log::debug(sprintf('Set tags to %s', $request->string('budget_id')));
$repository->updateTags($journal, ['tags' => explode(',', $request->string('tags'))]);
}
// update category if not told to ignore
if ($ignoreCategory === false) {
Log::debug(sprintf('Set category to %s', $request->string('category')));
$this->repository->updateCategory($journal, $request->string('category'));
}
// update budget if not told to ignore (and is withdrawal)
if ($ignoreBudget === false) {
Log::debug(sprintf('Set budget to %d', $request->integer('budget_id')));
$this->repository->updateBudget($journal, $request->integer('budget_id'));
}
// update tags:
if ($ignoreTags === false) {
Log::debug(sprintf('Set tags to %s', $request->string('budget_id')));
$this->repository->updateTags($journal, ['tags' => explode(',', $request->string('tags'))]);
}
}
@ -130,5 +135,4 @@ class BulkController extends Controller
// redirect to previous URL:
return redirect($this->getPreviousUri('transactions.bulk-edit.uri'));
}
}

View File

@ -97,36 +97,23 @@ class ConvertController extends Controller
$destinationAccount = $this->repository->getJournalDestinationAccounts($journal)->first();
return view(
'transactions.convert',
compact(
'sourceType',
'destinationType',
'journal',
'positiveAmount',
'sourceAccount',
'destinationAccount',
'sourceType',
'subTitle',
'subTitleIcon'
)
'transactions.convert', compact(
'sourceType', 'destinationType', 'journal', 'positiveAmount', 'sourceAccount', 'destinationAccount', 'sourceType', 'subTitle', 'subTitleIcon'
)
);
// convert withdrawal to deposit requires a new source account ()
// or to transfer requires
}
/**
* @param Request $request
* @param JournalRepositoryInterface $repository
* @param TransactionType $destinationType
* @param TransactionJournal $journal
* @param Request $request
* @param TransactionType $destinationType
* @param TransactionJournal $journal
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*
* @throws FireflyException
* @throws FireflyException
*/
public function postIndex(Request $request, JournalRepositoryInterface $repository, TransactionType $destinationType, TransactionJournal $journal)
public function postIndex(Request $request, TransactionType $destinationType, TransactionJournal $journal)
{
// @codeCoverageIgnoreStart
if ($this->isOpeningBalance($journal)) {
@ -153,7 +140,7 @@ class ConvertController extends Controller
$destination = $this->getDestinationAccount($journal, $destinationType, $data);
// update the journal:
$errors = $repository->convert($journal, $destinationType, $source, $destination);
$errors = $this->repository->convert($journal, $destinationType, $source, $destination);
if ($errors->count() > 0) {
return redirect(route('transactions.convert.index', [strtolower($destinationType->type), $journal->id]))->withErrors($errors)->withInput();

View File

@ -27,7 +27,6 @@ use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\MassDeleteJournalRequest;
use FireflyIII\Http\Requests\MassEditBulkJournalRequest;
use FireflyIII\Http\Requests\MassEditJournalRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;

View File

@ -60,19 +60,30 @@ class JournalRepository implements JournalRepositoryInterface
*/
public function convert(TransactionJournal $journal, TransactionType $type, Account $source, Account $destination): MessageBag
{
// default message bag that shows errors for everything.
$messages = new MessageBag;
$messages->add('source_account_revenue', trans('firefly.invalid_convert_selection'));
$messages->add('destination_account_asset', trans('firefly.invalid_convert_selection'));
$messages->add('destination_account_expense', trans('firefly.invalid_convert_selection'));
$messages->add('source_account_asset', trans('firefly.invalid_convert_selection'));
if ($source->id === $destination->id || null === $source->id || null === $destination->id) {
// default message bag that shows errors for everything.
$messages = new MessageBag;
$messages->add('source_account_revenue', trans('firefly.invalid_convert_selection'));
$messages->add('destination_account_asset', trans('firefly.invalid_convert_selection'));
$messages->add('destination_account_expense', trans('firefly.invalid_convert_selection'));
$messages->add('source_account_asset', trans('firefly.invalid_convert_selection'));
return $messages;
}
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
$destinationTransaction = $journal->transactions()->where('amount', '>', 0)->first();
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
$destinationTransaction = $journal->transactions()->where('amount', '>', 0)->first();
if (null === $sourceTransaction || null === $destinationTransaction) {
// default message bag that shows errors for everything.
$messages = new MessageBag;
$messages->add('source_account_revenue', trans('firefly.source_or_dest_invalid'));
$messages->add('destination_account_asset', trans('firefly.source_or_dest_invalid'));
$messages->add('destination_account_expense', trans('firefly.source_or_dest_invalid'));
$messages->add('source_account_asset', trans('firefly.source_or_dest_invalid'));
return $messages;
}
$sourceTransaction->account_id = $source->id;
$sourceTransaction->save();
$destinationTransaction->account_id = $destination->id;
@ -83,6 +94,10 @@ class JournalRepository implements JournalRepositoryInterface
// if journal is a transfer now, remove budget:
if (TransactionType::TRANSFER === $type->type) {
$journal->budgets()->detach();
// also from transactions:
foreach ($journal->transactions as $transaction) {
$transaction->budgets()->detach();
}
}
Preferences::mark();
@ -137,8 +152,6 @@ class JournalRepository implements JournalRepositoryInterface
*
* @param int $journalId
*
* @deprecated
*
* @return TransactionJournal|null
*/
public function findNull(int $journalId): ?TransactionJournal

View File

@ -78,7 +78,6 @@ interface JournalRepositoryInterface
* Find a specific journal.
*
* @param int $journalId
* @deprecated
*
* @return TransactionJournal|null
*/