mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
use journal repository instead of direct calls.
This commit is contained in:
parent
99983a5c8f
commit
1b304bf85e
@ -28,6 +28,7 @@ use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
@ -182,6 +183,9 @@ class PopupReport implements PopupReportInterface
|
||||
*/
|
||||
public function byIncome(Account $account, array $attributes): Collection
|
||||
{
|
||||
/** @var JournalRepositoryInterface $repository */
|
||||
$repository = app(JournalRepositoryInterface::class);
|
||||
$repository->setUser($account->user);
|
||||
/** @var JournalCollectorInterface $collector */
|
||||
$collector = app(JournalCollectorInterface::class);
|
||||
$collector->setAccounts(new Collection([$account]))->setRange($attributes['startDate'], $attributes['endDate'])
|
||||
@ -191,9 +195,10 @@ class PopupReport implements PopupReportInterface
|
||||
|
||||
// filter the set so the destinations outside of $attributes['accounts'] are not included.
|
||||
$journals = $journals->filter(
|
||||
function (Transaction $transaction) use ($report) {
|
||||
function (Transaction $transaction) use ($report, $repository) {
|
||||
// get the destinations:
|
||||
$destinations = $transaction->transactionJournal->destinationAccountList()->pluck('id')->toArray();
|
||||
$journal = $transaction->transactionJournal;
|
||||
$destinations = $repository->getJournalDestinationAccounts($journal)->pluck('id')->toArray();
|
||||
|
||||
// do these intersect with the current list?
|
||||
return !empty(array_intersect($report, $destinations));
|
||||
|
@ -52,6 +52,9 @@ use Session;
|
||||
*/
|
||||
class ReconcileController extends Controller
|
||||
{
|
||||
/** @var JournalRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@ -64,6 +67,7 @@ class ReconcileController extends Controller
|
||||
function ($request, $next) {
|
||||
app('view')->share('mainTitleIcon', 'fa-credit-card');
|
||||
app('view')->share('title', trans('firefly.accounts'));
|
||||
$this->repository = app(JournalRepositoryInterface::class);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
@ -84,10 +88,10 @@ class ReconcileController extends Controller
|
||||
$subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
|
||||
|
||||
// journal related code
|
||||
$pTransaction = $journal->positiveTransaction(); // TODO replace
|
||||
$pTransaction = $this->repository->getFirstPosTransaction($journal);
|
||||
$preFilled = [
|
||||
'date' => $journal->dateAsString(),
|
||||
'category' => $journal->categoryAsString(),
|
||||
'date' => $this->repository->getJournalDate($journal, null),
|
||||
'category' => $this->repository->getJournalCategoryName($journal),
|
||||
'tags' => join(',', $journal->tags->pluck('tag')->toArray()),
|
||||
'amount' => $pTransaction->amount,
|
||||
];
|
||||
@ -130,10 +134,8 @@ class ReconcileController extends Controller
|
||||
$clearedAmount = '0';
|
||||
$route = route('accounts.reconcile.submit', [$account->id, $start->format('Ymd'), $end->format('Ymd')]);
|
||||
// get sum of transaction amounts:
|
||||
/** @var JournalRepositoryInterface $repository */
|
||||
$repository = app(JournalRepositoryInterface::class);
|
||||
$transactions = $repository->getTransactionsById($transactionIds);
|
||||
$cleared = $repository->getTransactionsById($clearedIds);
|
||||
$transactions = $this->repository->getTransactionsById($transactionIds);
|
||||
$cleared = $this->repository->getTransactionsById($clearedIds);
|
||||
$countCleared = 0;
|
||||
|
||||
/** @var Transaction $transaction */
|
||||
@ -224,12 +226,11 @@ class ReconcileController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param JournalRepositoryInterface $repository
|
||||
* @param TransactionJournal $journal
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
||||
*/
|
||||
public function show(JournalRepositoryInterface $repository, TransactionJournal $journal)
|
||||
public function show(TransactionJournal $journal)
|
||||
{
|
||||
|
||||
if (TransactionType::RECONCILIATION !== $journal->transactionType->type) {
|
||||
@ -238,7 +239,7 @@ class ReconcileController extends Controller
|
||||
$subTitle = trans('firefly.reconciliation') . ' "' . $journal->description . '"';
|
||||
|
||||
// get main transaction:
|
||||
$transaction = $repository->getAssetTransaction($journal);
|
||||
$transaction = $this->repository->getAssetTransaction($journal);
|
||||
$account = $transaction->account;
|
||||
|
||||
return view('accounts.reconcile.show', compact('journal', 'subTitle', 'transaction', 'account'));
|
||||
@ -384,12 +385,11 @@ class ReconcileController extends Controller
|
||||
|
||||
/**
|
||||
* @param ReconciliationUpdateRequest $request
|
||||
* @param JournalRepositoryInterface $repository
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function update(ReconciliationUpdateRequest $request, JournalRepositoryInterface $repository, TransactionJournal $journal)
|
||||
public function update(ReconciliationUpdateRequest $request, TransactionJournal $journal)
|
||||
{
|
||||
if (TransactionType::RECONCILIATION !== $journal->transactionType->type) {
|
||||
return redirect(route('transactions.show', [$journal->id]));
|
||||
@ -403,8 +403,8 @@ class ReconcileController extends Controller
|
||||
$submitted = $request->getJournalData();
|
||||
|
||||
// amount pos neg influences the accounts:
|
||||
$source = $repository->getSourceAccount($journal);
|
||||
$destination = $repository->getDestinationAccount($journal);
|
||||
$source = $this->repository->getSourceAccount($journal);
|
||||
$destination = $this->repository->getDestinationAccount($journal);
|
||||
if (bccomp($submitted['amount'], '0') === 1) {
|
||||
// amount is positive, switch accounts:
|
||||
list($source, $destination) = [$destination, $source];
|
||||
@ -443,10 +443,10 @@ class ReconcileController extends Controller
|
||||
'category_name' => $submitted['category'],
|
||||
],
|
||||
],
|
||||
'notes' => $repository->getNote($journal),
|
||||
'notes' => $this->repository->getNoteText($journal),
|
||||
];
|
||||
|
||||
$repository->update($journal, $data);
|
||||
$this->repository->update($journal, $data);
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
if (1 === intval($request->get('return_to_edit'))) {
|
||||
|
@ -43,6 +43,8 @@ use View;
|
||||
*/
|
||||
class BulkController extends Controller
|
||||
{
|
||||
/** @var JournalRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
|
||||
/**
|
||||
@ -54,6 +56,7 @@ class BulkController extends Controller
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
$this->repository = app(JournalRepositoryInterface::class);
|
||||
app('view')->share('title', trans('firefly.transactions'));
|
||||
app('view')->share('mainTitleIcon', 'fa-repeat');
|
||||
|
||||
@ -77,8 +80,8 @@ class BulkController extends Controller
|
||||
$messages = [];
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$sources = $journal->sourceAccountList();
|
||||
$destinations = $journal->destinationAccountList();
|
||||
$sources = $this->repository->getJournalSourceAccounts($journal);
|
||||
$destinations = $this->repository->getJournalDestinationAccounts($journal);
|
||||
if ($sources->count() > 1) {
|
||||
$messages[] = trans('firefly.cannot_edit_multiple_source', ['description' => $journal->description, 'id' => $journal->id]);
|
||||
continue;
|
||||
@ -88,13 +91,13 @@ class BulkController extends Controller
|
||||
$messages[] = trans('firefly.cannot_edit_multiple_dest', ['description' => $journal->description, 'id' => $journal->id]);
|
||||
continue;
|
||||
}
|
||||
if (TransactionType::OPENING_BALANCE === $journal->transactionType->type) {
|
||||
if (TransactionType::OPENING_BALANCE === $this->repository->getTransactionType($journal)) {
|
||||
$messages[] = trans('firefly.cannot_edit_opening_balance');
|
||||
continue;
|
||||
}
|
||||
|
||||
// cannot edit reconciled transactions / journals:
|
||||
if ($journal->transactions->first()->reconciled) {
|
||||
if ($this->repository->isJournalReconciled($journal)) {
|
||||
$messages[] = trans('firefly.cannot_edit_reconciled', ['description' => $journal->description, 'id' => $journal->id]);
|
||||
continue;
|
||||
}
|
||||
|
@ -43,6 +43,9 @@ class ConvertController extends Controller
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $accounts;
|
||||
|
||||
/** @var JournalRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* ConvertController constructor.
|
||||
*/
|
||||
@ -53,7 +56,8 @@ class ConvertController extends Controller
|
||||
// some useful repositories:
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
$this->accounts = app(AccountRepositoryInterface::class);
|
||||
$this->accounts = app(AccountRepositoryInterface::class);
|
||||
$this->repository = app(JournalRepositoryInterface::class);
|
||||
|
||||
app('view')->share('title', trans('firefly.transactions'));
|
||||
app('view')->share('mainTitleIcon', 'fa-exchange');
|
||||
@ -76,8 +80,7 @@ class ConvertController extends Controller
|
||||
return $this->redirectToAccount($journal);
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
$positiveAmount = $journal->amountPositive();
|
||||
$positiveAmount = $this->repository->getJournalTotal($journal);
|
||||
$assetAccounts = ExpandedForm::makeSelectList($this->accounts->getActiveAccountsByType([AccountType::DEFAULT, AccountType::ASSET]));
|
||||
$sourceType = $journal->transactionType;
|
||||
$subTitle = trans('firefly.convert_to_' . $destinationType->type, ['description' => $journal->description]);
|
||||
@ -98,8 +101,8 @@ class ConvertController extends Controller
|
||||
}
|
||||
|
||||
// get source and destination account:
|
||||
$sourceAccount = $journal->sourceAccountList()->first();
|
||||
$destinationAccount = $journal->destinationAccountList()->first();
|
||||
$sourceAccount = $this->repository->getJournalSourceAccounts($journal)->first();
|
||||
$destinationAccount = $this->repository->getJournalDestinationAccounts($journal)->first();
|
||||
|
||||
return view(
|
||||
'transactions.convert',
|
||||
@ -183,8 +186,8 @@ class ConvertController extends Controller
|
||||
{
|
||||
/** @var AccountRepositoryInterface $accountRepository */
|
||||
$accountRepository = app(AccountRepositoryInterface::class);
|
||||
$sourceAccount = $journal->sourceAccountList()->first();
|
||||
$destinationAccount = $journal->destinationAccountList()->first();
|
||||
$sourceAccount = $this->repository->getJournalSourceAccounts($journal)->first();
|
||||
$destinationAccount = $this->repository->getJournalDestinationAccounts($journal)->first();
|
||||
$sourceType = $journal->transactionType;
|
||||
$joined = $sourceType->type . '-' . $destinationType->type;
|
||||
switch ($joined) {
|
||||
@ -239,8 +242,8 @@ class ConvertController extends Controller
|
||||
{
|
||||
/** @var AccountRepositoryInterface $accountRepository */
|
||||
$accountRepository = app(AccountRepositoryInterface::class);
|
||||
$sourceAccount = $journal->sourceAccountList()->first();
|
||||
$destinationAccount = $journal->destinationAccountList()->first();
|
||||
$sourceAccount = $this->repository->getJournalSourceAccounts($journal)->first();
|
||||
$destinationAccount = $this->repository->getJournalDestinationAccounts($journal)->first();
|
||||
$sourceType = $journal->transactionType;
|
||||
$joined = $sourceType->type . '-' . $destinationType->type;
|
||||
switch ($joined) {
|
||||
|
@ -43,6 +43,9 @@ use View;
|
||||
*/
|
||||
class MassController extends Controller
|
||||
{
|
||||
/** @var JournalRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@ -54,6 +57,7 @@ class MassController extends Controller
|
||||
function ($request, $next) {
|
||||
app('view')->share('title', trans('firefly.transactions'));
|
||||
app('view')->share('mainTitleIcon', 'fa-repeat');
|
||||
$this->repository = app(JournalRepositoryInterface::class);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
@ -76,12 +80,11 @@ class MassController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MassDeleteJournalRequest $request
|
||||
* @param JournalRepositoryInterface $repository
|
||||
* @param MassDeleteJournalRequest $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function destroy(MassDeleteJournalRequest $request, JournalRepositoryInterface $repository)
|
||||
public function destroy(MassDeleteJournalRequest $request)
|
||||
{
|
||||
$ids = $request->get('confirm_mass_delete');
|
||||
$set = new Collection;
|
||||
@ -89,7 +92,7 @@ class MassController extends Controller
|
||||
/** @var int $journalId */
|
||||
foreach ($ids as $journalId) {
|
||||
/** @var TransactionJournal $journal */
|
||||
$journal = $repository->find(intval($journalId));
|
||||
$journal = $this->repository->find(intval($journalId));
|
||||
if (null !== $journal->id && intval($journalId) === $journal->id) {
|
||||
$set->push($journal);
|
||||
}
|
||||
@ -100,7 +103,7 @@ class MassController extends Controller
|
||||
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($set as $journal) {
|
||||
$repository->delete($journal);
|
||||
$this->repository->delete($journal);
|
||||
++$count;
|
||||
}
|
||||
|
||||
@ -134,8 +137,8 @@ class MassController extends Controller
|
||||
$messages = [];
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$sources = $journal->sourceAccountList();
|
||||
$destinations = $journal->destinationAccountList();
|
||||
$sources = $this->repository->getJournalSourceAccounts($journal);
|
||||
$destinations = $this->repository->getJournalDestinationAccounts($journal);
|
||||
if ($sources->count() > 1) {
|
||||
$messages[] = trans('firefly.cannot_edit_multiple_source', ['description' => $journal->description, 'id' => $journal->id]);
|
||||
continue;
|
||||
@ -172,8 +175,8 @@ class MassController extends Controller
|
||||
$transaction = $journal->positiveTransaction();
|
||||
$currency = $transaction->transactionCurrency;
|
||||
$journal->amount = floatval($transaction->amount);
|
||||
$sources = $journal->sourceAccountList();
|
||||
$destinations = $journal->destinationAccountList();
|
||||
$sources = $this->repository->getJournalSourceAccounts($journal);
|
||||
$destinations = $this->repository->getJournalDestinationAccounts($journal);
|
||||
$journal->transaction_count = $journal->transactions()->count();
|
||||
$journal->currency_symbol = $currency->symbol;
|
||||
$journal->transaction_type_type = $journal->transactionType->type;
|
||||
|
@ -104,13 +104,13 @@ class SingleController extends Controller
|
||||
*/
|
||||
public function cloneTransaction(TransactionJournal $journal)
|
||||
{
|
||||
$source = $journal->sourceAccountList()->first();
|
||||
$destination = $journal->destinationAccountList()->first();
|
||||
$budget = $journal->budgets()->first();
|
||||
$budgetId = null === $budget ? 0 : $budget->id;
|
||||
$category = $journal->categories()->first();
|
||||
$categoryName = null === $category ? '' : $category->name;
|
||||
$tags = join(',', $journal->tags()->get()->pluck('tag')->toArray());
|
||||
$source = $this->repository->getJournalSourceAccounts($journal)->first();
|
||||
$destination = $this->repository->getJournalDestinationAccounts($journal)->first();
|
||||
$budgetId = $this->repository->getJournalBudgetId($journal);
|
||||
$categoryName = $this->repository->getJournalCategoryName($journal);
|
||||
|
||||
$tags = join(',',$this->repository->getTags($journal));
|
||||
// todo less direct database access. Use collector?
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->first();
|
||||
$amount = app('steam')->positive($transaction->amount);
|
||||
@ -398,7 +398,7 @@ class SingleController extends Controller
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
$data = $request->getJournalData();
|
||||
$data = $request->getJournalData();
|
||||
$journal = $repository->update($journal, $data);
|
||||
/** @var array $files */
|
||||
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
|
||||
|
@ -124,9 +124,10 @@ class SplitController extends Controller
|
||||
|
||||
return view(
|
||||
'transactions.split.edit', compact(
|
||||
'subTitleIcon', 'currencies', 'optionalFields', 'preFilled', 'subTitle', 'uploadSize', 'assetAccounts', 'budgets', 'journal', 'accountArray',
|
||||
'previous'
|
||||
)
|
||||
'subTitleIcon', 'currencies', 'optionalFields', 'preFilled', 'subTitle', 'uploadSize', 'assetAccounts', 'budgets',
|
||||
'journal', 'accountArray',
|
||||
'previous'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@ -186,7 +187,7 @@ class SplitController extends Controller
|
||||
$destinationAccounts = $this->repository->getJournalDestinationAccounts($journal);
|
||||
$array = [
|
||||
'journal_description' => $request->old('journal_description', $journal->description),
|
||||
'journal_amount' => $journal->amountPositive(),
|
||||
'journal_amount' => $this->repository->getJournalTotal($journal),
|
||||
'sourceAccounts' => $sourceAccounts,
|
||||
'journal_source_account_id' => $request->old('journal_source_account_id', $sourceAccounts->first()->id),
|
||||
'journal_source_account_name' => $request->old('journal_source_account_name', $sourceAccounts->first()->name),
|
||||
|
@ -103,24 +103,6 @@ class Tag extends Model
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public static function tagSum(Tag $tag): string
|
||||
{
|
||||
$sum = '0';
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($tag->transactionjournals as $journal) {
|
||||
bcadd($sum, $journal->amount());
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
|
@ -29,6 +29,7 @@ use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Services\Internal\Destroy\BillDestroyService;
|
||||
use FireflyIII\Services\Internal\Update\BillUpdateService;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
@ -253,12 +254,15 @@ class BillRepository implements BillRepositoryInterface
|
||||
*/
|
||||
public function getOverallAverage(Bill $bill): string
|
||||
{
|
||||
/** @var JournalRepositoryInterface $repos */
|
||||
$repos = app(JournalRepositoryInterface::class);
|
||||
$repos->setUser($this->user);
|
||||
$journals = $bill->transactionJournals()->get();
|
||||
$sum = '0';
|
||||
$count = strval($journals->count());
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$sum = bcadd($sum, $journal->amountPositive());
|
||||
$sum = bcadd($sum, $repos->getJournalTotal($journal));
|
||||
}
|
||||
$avg = '0';
|
||||
if ($journals->count() > 0) {
|
||||
@ -376,15 +380,19 @@ class BillRepository implements BillRepositoryInterface
|
||||
*/
|
||||
public function getYearAverage(Bill $bill, Carbon $date): string
|
||||
{
|
||||
/** @var JournalRepositoryInterface $repos */
|
||||
$repos = app(JournalRepositoryInterface::class);
|
||||
$repos->setUser($this->user);
|
||||
|
||||
$journals = $bill->transactionJournals()
|
||||
->where('date', '>=', $date->year . '-01-01 00:00:00')
|
||||
->where('date', '<=', $date->year . '-12-31 00:00:00')
|
||||
->where('date', '<=', $date->year . '-12-31 23:59:59')
|
||||
->get();
|
||||
$sum = '0';
|
||||
$count = strval($journals->count());
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$sum = bcadd($sum, $journal->amountPositive());
|
||||
$sum = bcadd($sum, $repos->getJournalTotal($journal));
|
||||
}
|
||||
$avg = '0';
|
||||
if ($journals->count() > 0) {
|
||||
@ -494,15 +502,20 @@ class BillRepository implements BillRepositoryInterface
|
||||
if (false === $journal->isWithdrawal()) {
|
||||
return false;
|
||||
}
|
||||
$destinationAccounts = $journal->destinationAccountList();
|
||||
$sourceAccounts = $journal->sourceAccountList();
|
||||
|
||||
/** @var JournalRepositoryInterface $repos */
|
||||
$repos = app(JournalRepositoryInterface::class);
|
||||
$repos->setUser($this->user);
|
||||
|
||||
$destinationAccounts = $repos->getJournalDestinationAccounts($journal);
|
||||
$sourceAccounts = $repos->getJournalDestinationAccounts($journal);
|
||||
$matches = explode(',', $bill->match);
|
||||
$description = strtolower($journal->description) . ' ';
|
||||
$description .= strtolower(join(' ', $destinationAccounts->pluck('name')->toArray()));
|
||||
$description .= strtolower(join(' ', $sourceAccounts->pluck('name')->toArray()));
|
||||
|
||||
$wordMatch = $this->doWordMatch($matches, $description);
|
||||
$amountMatch = $this->doAmountMatch($journal->amountPositive(), $bill->amount_min, $bill->amount_max);
|
||||
$amountMatch = $this->doAmountMatch($repos->getJournalTotal($journal), $bill->amount_min, $bill->amount_max);
|
||||
|
||||
// when both, update!
|
||||
if ($wordMatch && $amountMatch) {
|
||||
@ -561,6 +574,7 @@ class BillRepository implements BillRepositoryInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO refactor
|
||||
* @param float $amount
|
||||
* @param float $min
|
||||
* @param float $max
|
||||
@ -577,6 +591,7 @@ class BillRepository implements BillRepositoryInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO refactor
|
||||
* @param array $matches
|
||||
* @param $description
|
||||
*
|
||||
|
@ -356,6 +356,30 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return total amount of journal. Is always positive.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getJournalTotal(TransactionJournal $journal): string
|
||||
{
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($journal->id);
|
||||
$cache->addProperty('amount-positive');
|
||||
if ($cache->has()) {
|
||||
return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
// saves on queries:
|
||||
$amount = $journal->transactions()->where('amount', '>', 0)->get()->sum('amount');
|
||||
$amount = strval($amount);
|
||||
$cache->store($amount);
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return value of a meta field (or NULL) as a string.
|
||||
*
|
||||
@ -475,6 +499,24 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will tell you if journal is reconciled or not.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isJournalReconciled(TransactionJournal $journal): bool
|
||||
{
|
||||
foreach ($journal->transactions as $transaction) {
|
||||
if ($transaction->reconciled) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
|
@ -36,7 +36,6 @@ use Illuminate\Support\MessageBag;
|
||||
*/
|
||||
interface JournalRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param TransactionType $type
|
||||
@ -165,6 +164,15 @@ interface JournalRepositoryInterface
|
||||
*/
|
||||
public function getJournalSourceAccounts(TransactionJournal $journal): Collection;
|
||||
|
||||
/**
|
||||
* Return total amount of journal. Is always positive.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getJournalTotal(TransactionJournal $journal): string;
|
||||
|
||||
/**
|
||||
* Return value of a meta field (or NULL).
|
||||
*
|
||||
@ -230,6 +238,15 @@ interface JournalRepositoryInterface
|
||||
*/
|
||||
public function getTransactionsById(array $transactionIds): Collection;
|
||||
|
||||
/**
|
||||
* Will tell you if journal is reconciled or not.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isJournalReconciled(TransactionJournal $journal): bool;
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
|
@ -28,6 +28,7 @@ use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\PiggyBankEvent;
|
||||
use FireflyIII\Models\PiggyBankRepetition;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
@ -222,8 +223,12 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
*/
|
||||
public function getExactAmount(PiggyBank $piggyBank, PiggyBankRepetition $repetition, TransactionJournal $journal): string
|
||||
{
|
||||
$amount = $journal->amountPositive();
|
||||
$sources = $journal->sourceAccountList()->pluck('id')->toArray();
|
||||
/** @var JournalRepositoryInterface $repos */
|
||||
$repos = app(JournalRepositoryInterface::class);
|
||||
$repos->setUser($this->user);
|
||||
|
||||
$amount = $repos->getJournalTotal($journal);
|
||||
$sources = $repos->getJournalSourceAccounts($journal)->pluck('id')->toArray();
|
||||
$room = bcsub(strval($piggyBank->targetamount), strval($repetition->currentamount));
|
||||
$compare = bcmul($repetition->currentamount, '-1');
|
||||
|
||||
|
@ -63,66 +63,6 @@ trait TransactionJournalTrait
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function amount(): string
|
||||
{
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($this->id);
|
||||
$cache->addProperty('transaction-journal');
|
||||
$cache->addProperty('amount');
|
||||
if ($cache->has()) {
|
||||
return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
// saves on queries:
|
||||
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
|
||||
|
||||
if ($this->isWithdrawal()) {
|
||||
$amount = $amount * -1;
|
||||
}
|
||||
$amount = strval($amount);
|
||||
$cache->store($amount);
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function amountPositive(): string
|
||||
{
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($this->id);
|
||||
$cache->addProperty('transaction-journal');
|
||||
$cache->addProperty('amount-positive');
|
||||
if ($cache->has()) {
|
||||
return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
// saves on queries:
|
||||
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
|
||||
|
||||
$amount = strval($amount);
|
||||
$cache->store($amount);
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function budgetId(): int
|
||||
{
|
||||
$budget = $this->budgets()->first();
|
||||
if (null !== $budget) {
|
||||
return $budget->id;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
@ -133,51 +73,6 @@ trait TransactionJournalTrait
|
||||
*/
|
||||
abstract public function categories(): BelongsToMany;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function categoryAsString(): string
|
||||
{
|
||||
$category = $this->categories()->first();
|
||||
if (null !== $category) {
|
||||
return $category->name;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dateField
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function dateAsString(string $dateField = ''): string
|
||||
{
|
||||
if ('' === $dateField) {
|
||||
return $this->date->format('Y-m-d');
|
||||
}
|
||||
if (null !== $this->$dateField && $this->$dateField instanceof Carbon) {
|
||||
// make field NULL
|
||||
$carbon = clone $this->$dateField;
|
||||
$this->$dateField = null;
|
||||
$this->save();
|
||||
|
||||
// create meta entry
|
||||
$this->setMeta($dateField, $carbon);
|
||||
|
||||
// return that one instead.
|
||||
return $carbon->format('Y-m-d');
|
||||
}
|
||||
$metaField = $this->getMeta($dateField);
|
||||
if (null !== $metaField) {
|
||||
$carbon = new Carbon($metaField);
|
||||
|
||||
return $carbon->format('Y-m-d');
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
|
@ -349,29 +349,4 @@ class General extends Twig_Extension
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Twig_SimpleFunction
|
||||
*/
|
||||
protected function steamPositive()
|
||||
{
|
||||
return new Twig_SimpleFunction(
|
||||
'steam_positive',
|
||||
function (string $str): string {
|
||||
return Steam::positive($str);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Twig_SimpleFunction
|
||||
*/
|
||||
private function getAmountFromJournal()
|
||||
{
|
||||
return new Twig_SimpleFunction(
|
||||
'getAmount',
|
||||
function (TransactionJournal $journal): string {
|
||||
return $journal->amount();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\TransactionRules\Triggers;
|
||||
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@ -65,7 +66,12 @@ final class AmountExactly extends AbstractTrigger implements TriggerInterface
|
||||
*/
|
||||
public function triggered(TransactionJournal $journal): bool
|
||||
{
|
||||
$amount = $journal->destination_amount ?? $journal->amountPositive();
|
||||
/** @var JournalRepositoryInterface $repos */
|
||||
$repos = app(JournalRepositoryInterface::class);
|
||||
$repos->setUser($journal->user);
|
||||
|
||||
|
||||
$amount = $journal->destination_amount ?? $repos->getJournalTotal($journal);
|
||||
$compare = $this->triggerValue;
|
||||
$result = bccomp($amount, $compare);
|
||||
if (0 === $result) {
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\TransactionRules\Triggers;
|
||||
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@ -65,7 +66,11 @@ final class AmountLess extends AbstractTrigger implements TriggerInterface
|
||||
*/
|
||||
public function triggered(TransactionJournal $journal): bool
|
||||
{
|
||||
$amount = $journal->destination_amount ?? $journal->amountPositive();
|
||||
/** @var JournalRepositoryInterface $repos */
|
||||
$repos = app(JournalRepositoryInterface::class);
|
||||
$repos->setUser($journal->user);
|
||||
|
||||
$amount = $journal->destination_amount ?? $repos->getJournalTotal($journal);
|
||||
$compare = $this->triggerValue;
|
||||
$result = bccomp($amount, $compare);
|
||||
if ($result === -1) {
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\TransactionRules\Triggers;
|
||||
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@ -71,7 +72,11 @@ final class AmountMore extends AbstractTrigger implements TriggerInterface
|
||||
*/
|
||||
public function triggered(TransactionJournal $journal): bool
|
||||
{
|
||||
$amount = $journal->destination_amount ?? $journal->amountPositive();
|
||||
/** @var JournalRepositoryInterface $repos */
|
||||
$repos = app(JournalRepositoryInterface::class);
|
||||
$repos->setUser($journal->user);
|
||||
|
||||
$amount = $journal->destination_amount ?? $repos->getJournalTotal($journal);
|
||||
$compare = $this->triggerValue;
|
||||
$result = bccomp($amount, $compare);
|
||||
if (1 === $result) {
|
||||
|
@ -316,15 +316,6 @@ class SplitControllerTest extends TestCase
|
||||
];
|
||||
|
||||
$journalRepos->shouldReceive('first')->once()->andReturn($opening);
|
||||
// $journalRepos->shouldReceive('getJournalSourceAccounts')->andReturn(new Collection([$account]));
|
||||
// $journalRepos->shouldReceive('getJournalDestinationAccounts')->andReturn(new Collection([$account]));
|
||||
//$journalRepos->shouldReceive('getTransactionType')->once()->andReturn('Opening balance');
|
||||
// $journalRepos->shouldReceive('getJournalDate')->andReturn('2018-01-01')->once();
|
||||
// $journalRepos->shouldReceive('getMetaField')->andReturn('');
|
||||
// $journalRepos->shouldReceive('getNoteText')->andReturn('Some note')->once();
|
||||
// $journalRepos->shouldReceive('getJournalBudgetId')->andReturn(0);
|
||||
// $journalRepos->shouldReceive('getCategoryName')->andReturn('');
|
||||
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->post(route('transactions.split.update', [$opening->id]), $data);
|
||||
|
Loading…
Reference in New Issue
Block a user