mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fixed #330
This commit is contained in:
parent
c9f14da294
commit
d4995e342f
@ -14,11 +14,14 @@ declare(strict_types = 1);
|
||||
namespace FireflyIII\Http\Controllers\Transaction;
|
||||
|
||||
use ExpandedForm;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Session;
|
||||
use View;
|
||||
@ -102,16 +105,19 @@ class ConvertController extends Controller
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param JournalRepositoryInterface $repository
|
||||
* @param TransactionType $destinationType
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function submit(Request $request, TransactionType $destinationType, TransactionJournal $journal)
|
||||
public function submit(Request $request, JournalRepositoryInterface $repository, TransactionType $destinationType, TransactionJournal $journal)
|
||||
{
|
||||
$sourceType = $journal->transactionType;
|
||||
$data = $request->all();
|
||||
|
||||
// cannot convert to its own type.
|
||||
if ($sourceType->type === $destinationType->type) {
|
||||
Session::flash('info', trans('firefly.convert_is_already_type_' . $destinationType->type));
|
||||
if ($journal->transactionType->type === $destinationType->type) {
|
||||
Session::flash('error', trans('firefly.convert_is_already_type_' . $destinationType->type));
|
||||
|
||||
return redirect(route('transactions.show', [$journal->id]));
|
||||
}
|
||||
@ -123,18 +129,113 @@ class ConvertController extends Controller
|
||||
return redirect(route('transactions.show', [$journal->id]));
|
||||
}
|
||||
|
||||
// try the conversion with the given data:
|
||||
// get the new source and destination account:
|
||||
$source = $this->getSourceAccount($journal, $destinationType, $data);
|
||||
$destination = $this->getDestinationAccount($journal, $destinationType, $data);
|
||||
|
||||
// update the journal:
|
||||
$errors = $repository->convert($journal, $destinationType, $source, $destination);
|
||||
|
||||
if ($errors->count() > 0) {
|
||||
return redirect(route('transactions.convert', [strtolower($destinationType->type), $journal->id]))->withErrors($errors)->withInput();
|
||||
}
|
||||
|
||||
Session::flash('success', trans('firefly.converted_to_' . $destinationType->type));
|
||||
|
||||
return redirect(route('transactions.show', [$journal->id]));
|
||||
}
|
||||
|
||||
echo '<pre>';
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param TransactionType $destinationType
|
||||
* @param array $data
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getDestinationAccount(TransactionJournal $journal, TransactionType $destinationType, array $data): Account
|
||||
{
|
||||
/** @var AccountRepositoryInterface $accountRepository */
|
||||
$accountRepository = app(AccountRepositoryInterface::class);
|
||||
$sourceAccount = TransactionJournal::sourceAccountList($journal)->first();
|
||||
$destinationAccount = TransactionJournal::destinationAccountList($journal)->first();
|
||||
$sourceType = $journal->transactionType;
|
||||
$destination = null;
|
||||
$joined = $sourceType->type . '-' . $destinationType->type;
|
||||
switch ($joined) {
|
||||
default:
|
||||
throw new FireflyException('Cannot handle ' . $joined);
|
||||
case TransactionType::WITHDRAWAL . '-' . TransactionType::DEPOSIT: # one
|
||||
$destination = $sourceAccount;
|
||||
break;
|
||||
case TransactionType::WITHDRAWAL . '-' . TransactionType::TRANSFER: # two
|
||||
$destination = $accountRepository->find(intval($data['destination_account_asset']));
|
||||
break;
|
||||
case TransactionType::DEPOSIT . '-' . TransactionType::WITHDRAWAL: # three
|
||||
case TransactionType::TRANSFER . '-' . TransactionType::WITHDRAWAL: #five
|
||||
$data = [
|
||||
'name' => $data['destination_account_expense'],
|
||||
'accountType' => 'expense',
|
||||
'virtualBalance' => 0,
|
||||
'active' => true,
|
||||
'iban' => null,
|
||||
];
|
||||
$destination = $accountRepository->store($data);
|
||||
break;
|
||||
case TransactionType::DEPOSIT . '-' . TransactionType::TRANSFER: # four
|
||||
case TransactionType::TRANSFER . '-' . TransactionType::DEPOSIT: #six
|
||||
$destination = $destinationAccount;
|
||||
break;
|
||||
}
|
||||
|
||||
var_dump($request->all());
|
||||
return $destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param TransactionType $destinationType
|
||||
* @param array $data
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceAccount(TransactionJournal $journal, TransactionType $destinationType, array $data): Account
|
||||
{
|
||||
/** @var AccountRepositoryInterface $accountRepository */
|
||||
$accountRepository = app(AccountRepositoryInterface::class);
|
||||
$sourceAccount = TransactionJournal::sourceAccountList($journal)->first();
|
||||
$destinationAccount = TransactionJournal::destinationAccountList($journal)->first();
|
||||
$sourceType = $journal->transactionType;
|
||||
$source = new Account;
|
||||
$joined = $sourceType->type . '-' . $destinationType->type;
|
||||
switch ($joined) {
|
||||
default:
|
||||
throw new FireflyException('Cannot handle ' . $joined);
|
||||
case TransactionType::WITHDRAWAL . '-' . TransactionType::DEPOSIT: # one
|
||||
case TransactionType::TRANSFER . '-' . TransactionType::DEPOSIT: #six
|
||||
$data = [
|
||||
'name' => $data['source_account_revenue'],
|
||||
'accountType' => 'revenue',
|
||||
'virtualBalance' => 0,
|
||||
'active' => true,
|
||||
'iban' => null,
|
||||
];
|
||||
$source = $accountRepository->store($data);
|
||||
break;
|
||||
case TransactionType::WITHDRAWAL . '-' . TransactionType::TRANSFER: # two
|
||||
case TransactionType::TRANSFER . '-' . TransactionType::WITHDRAWAL: #five
|
||||
$source = $sourceAccount;
|
||||
break;
|
||||
case TransactionType::DEPOSIT . '-' . TransactionType::WITHDRAWAL: # three
|
||||
$source = $destinationAccount;
|
||||
break;
|
||||
case TransactionType::DEPOSIT . '-' . TransactionType::TRANSFER: # four
|
||||
$source = $accountRepository->find(intval($data['source_account_asset']));
|
||||
break;
|
||||
}
|
||||
|
||||
return $source;
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
}
|
@ -26,7 +26,9 @@ use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Log;
|
||||
use Preferences;
|
||||
|
||||
/**
|
||||
* Class JournalRepository
|
||||
@ -51,6 +53,41 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param TransactionType $type
|
||||
* @param Account $source
|
||||
* @param Account $destination
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
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 || is_null($source->id) || is_null($destination->id)) {
|
||||
return $messages;
|
||||
}
|
||||
|
||||
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$destinationTransaction = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
$sourceTransaction->account_id = $source->id;
|
||||
$sourceTransaction->save();
|
||||
$destinationTransaction->account_id = $destination->id;
|
||||
$destinationTransaction->save();
|
||||
$journal->transaction_type_id = $type->id;
|
||||
$journal->save();
|
||||
Preferences::mark();
|
||||
$messages = new MessageBag;
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
@ -95,7 +132,6 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
return $entry;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
|
@ -13,7 +13,10 @@ declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Repositories\Journal;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use Illuminate\Support\MessageBag;
|
||||
|
||||
/**
|
||||
* Interface JournalRepositoryInterface
|
||||
@ -32,6 +35,15 @@ interface JournalRepositoryInterface
|
||||
*/
|
||||
public function delete(TransactionJournal $journal): bool;
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param TransactionType $type
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function convert(TransactionJournal $journal, TransactionType $type, Account $source, Account $destination): MessageBag;
|
||||
|
||||
/**
|
||||
* Find a specific journal
|
||||
*
|
||||
|
@ -383,6 +383,11 @@ return [
|
||||
'convert_explanation_deposit_transfer' => 'If you convert this deposit into a transfer, :amount will be transferred from an asset account of your choice into <a href=":destinationRoute">:destinationName</a>.',
|
||||
'convert_explanation_transfer_withdrawal' => 'If you convert this transfer into a withdrawal, :amount will go from <a href=":sourceRoute">:sourceName</a> to a new destination as an expense, instead of to <a href=":destinationRoute">:destinationName</a> as a transfer.',
|
||||
'convert_explanation_transfer_deposit' => 'If you convert this transfer into a deposit, :amount will be deposited into account <a href=":destinationRoute">:destinationName</a> instead of being transferred there.',
|
||||
'converted_to_Withdrawal' => 'The transaction has been converted to a withdrawal',
|
||||
'converted_to_Deposit' => 'The transaction has been converted to a deposit',
|
||||
'converted_to_Transfer' => 'The transaction has been converted to a transfer',
|
||||
|
||||
|
||||
// create new stuff:
|
||||
'create_new_withdrawal' => 'Create new withdrawal',
|
||||
'create_new_deposit' => 'Create new deposit',
|
||||
|
Loading…
Reference in New Issue
Block a user