Improve tests, models and views.

This commit is contained in:
James Cole
2019-04-16 16:20:46 +02:00
parent 5ac39dbdef
commit 66c55b7bbe
44 changed files with 13710 additions and 5067 deletions

View File

@@ -291,7 +291,7 @@ class TransactionController extends Controller
$selectedGroup = $collector->getGroups()->first();
if (null === $selectedGroup) {
throw new NotFoundHttpException();
throw new NotFoundHttpException(); // @codeCoverageIgnore
}
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
@@ -335,7 +335,7 @@ class TransactionController extends Controller
$selectedGroup = $collector->getGroups()->first();
if (null === $selectedGroup) {
throw new NotFoundHttpException();
throw new NotFoundHttpException(); // @codeCoverageIgnore
}
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);

View File

@@ -182,7 +182,7 @@ class TransactionStoreRequest extends Request
// the group must have a description if > 1 journal.
$this->validateGroupDescription($validator);
// TODO validate that the currency fits the source and/or destination account.
// TODO the currency info must match the accounts involved.
}
);
@@ -217,8 +217,8 @@ class TransactionStoreRequest extends Request
'foreign_currency_code' => $this->stringFromValue($object['foreign_currency_code']),
// amount and foreign amount. Cannot be 0.
'amount' => $this->stringFromValue($object['amount']),
'foreign_amount' => $this->stringFromValue($object['foreign_amount']),
'amount' => $this->stringFromValue((string)$object['amount']),
'foreign_amount' => $this->stringFromValue((string)$object['foreign_amount']),
// description.
'description' => $this->stringFromValue($object['description']),

View File

@@ -237,13 +237,12 @@ class TransactionUpdateRequest extends Request
// all transaction types must be equal:
$this->validateTransactionTypesForUpdate($validator);
// if type is set, source + destination info is mandatory.
$this->validateAccountPresence($validator);
// validate source/destination is equal, depending on the transaction journal type.
$this->validateEqualAccountsForUpdate($validator, $transactionGroup);
// TODO if type is set, source + destination info is mandatory.
// TODO validate that the currency fits the source and/or destination account.
// TODO the currency info must match the accounts involved.
// all journals must have a description
//$this->validateDescriptions($validator);
@@ -308,7 +307,7 @@ class TransactionUpdateRequest extends Request
foreach ($this->arrayFields as $fieldName) {
if (array_key_exists($fieldName, $transaction)) {
$current[$fieldName] = $this->arrayFromValue((string)$transaction[$fieldName]);
$current[$fieldName] = $this->arrayFromValue($transaction[$fieldName]);
}
}
$return[] = $current;

View File

@@ -83,12 +83,13 @@ class TransactionIdentifier extends Command
->where('t_count', '>', 2)
->select(['id', 't_count']);
$journalIds = array_unique($result->pluck('id')->toArray());
$count= 0;
foreach ($journalIds as $journalId) {
$this->updateJournalidentifiers((int)$journalId);
$count++;
}
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Verified and fixed transaction identifiers in %s seconds.', $end));
$this->info(sprintf('Verified and fixed %d transaction identifiers in %s seconds.', $count, $end));
$this->markAsExecuted();
return 0;

View File

@@ -124,6 +124,8 @@ class TransactionFactory
$sourceAccount = $this->getAccount($type, 'source', (int)$data['source_id'], $data['source_name']);
$destinationAccount = $this->getAccount($type, 'destination', (int)$data['destination_id'], $data['destination_name']);
// at this point we know the
$amount = $this->getAmount($data['amount']);
$foreignAmount = $this->getForeignAmount($data['foreign_amount']);
$one = $this->create($sourceAccount, $currency, app('steam')->negative($amount));

View File

@@ -1,6 +1,6 @@
<?php
/**
* ShowController.php
* ViewController.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
@@ -28,6 +28,8 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\Transformers\TransactionGroupTransformer;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class ShowController
@@ -35,21 +37,22 @@ use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface
class ShowController extends Controller
{
/** @var TransactionGroupRepositoryInterface */
private $groupRepository;
private $repository;
/**
* SingleController constructor.
* ConvertController constructor.
*/
public function __construct()
{
parent::__construct();
// some useful repositories:
$this->middleware(
function ($request, $next) {
$this->groupRepository = app(TransactionGroupRepositoryInterface::class);
$this->repository = app(TransactionGroupRepositoryInterface::class);
app('view')->share('title', (string)trans('firefly.transactions'));
app('view')->share('mainTitleIcon', 'fa-repeat');
app('view')->share('mainTitleIcon', 'fa-exchange');
return $next($request);
}
@@ -64,16 +67,58 @@ class ShowController extends Controller
public function show(TransactionGroup $transactionGroup)
{
/** @var TransactionJournal $first */
$first = $transactionGroup->transactionJournals->first();
$groupType = $first->transactionType->type;
$description = $transactionGroup->title;
if ($transactionGroup->transactionJournals()->count() > 1) {
$description = $first->description;
$first = $transactionGroup->transactionJournals->first();
$splits = $transactionGroup->transactionJournals->count();
$type = (string)trans(sprintf('firefly.%s', strtolower($first->transactionType->type)));
$title = 1 === $splits ? $first->description : $transactionGroup->title;
$subTitle = sprintf('%s: "%s"', $type, $title);
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
$transformer->setParameters(new ParameterBag);
$groupArray = $transformer->transformObject($transactionGroup);
// do some amount calculations:
$amounts = [];
foreach ($groupArray['transactions'] as $transaction) {
$symbol = $transaction['currency_symbol'];
if (!isset($amounts[$symbol])) {
$amounts[$symbol] = [
'amount' => '0',
'symbol' => $symbol,
'decimal_places' => $transaction['currency_decimal_places'],
];
}
$amounts[$symbol]['amount'] = bcadd($amounts[$symbol]['amount'], $transaction['amount']);
if (null !== $transaction['foreign_amount']) {
// same for foreign currency:
$foreignSymbol = $transaction['foreign_currency_symbol'];
if (!isset($amounts[$foreignSymbol])) {
$amounts[$foreignSymbol] = [
'amount' => '0',
'symbol' => $foreignSymbol,
'decimal_places' => $transaction['foreign_currency_decimal_places'],
];
}
$amounts[$symbol]['amount'] = bcadd($amounts[$symbol]['amount'], $transaction['foreign_amount']);
}
}
$subTitle = sprintf('%s: "%s"', $groupType, $description);
$events = $this->repository->getPiggyEvents($transactionGroup);
$attachments = $this->repository->getAttachments($transactionGroup);
$links = $this->repository->getLinks($transactionGroup);
return view('transactions.show', compact('transactionGroup', 'subTitle'));
// TODO links to other journals, use the API
// TODO links to attachments, use the API.
// TODO links to piggy bank events.
return view(
'transactions.show', compact(
'transactionGroup', 'amounts', 'first', 'type', 'subTitle', 'splits', 'groupArray',
'events', 'attachments', 'links'
)
);
}
}

View File

@@ -212,61 +212,6 @@ class TransactionController extends Controller
return response()->json([true]);
}
/**
* Show a transaction.
*
* @param TransactionJournal $journal
* @param LinkTypeRepositoryInterface $linkTypeRepository
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
* @throws FireflyException
*/
public function show(TransactionJournal $journal, LinkTypeRepositoryInterface $linkTypeRepository)
{
if ($this->isOpeningBalance($journal)) {
return $this->redirectToAccount($journal);
}
$transactionType = $journal->transactionType->type;
if (TransactionType::RECONCILIATION === $transactionType) {
return redirect(route('accounts.reconcile.show', [$journal->id])); // @codeCoverageIgnore
}
$linkTypes = $linkTypeRepository->get();
$links = $linkTypeRepository->getLinks($journal);
// get attachments:
$attachments = $this->repository->getAttachments($journal);
$attachments = $attachments->each(
function (Attachment $attachment) {
$attachment->file_exists = $this->attachmentRepository->exists($attachment);
return $attachment;
}
);
// get transactions using the collector:
$collector = app(TransactionCollectorInterface::class);
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
// filter on specific journals.
$collector->setJournals(new Collection([$journal]));
$set = $collector->getTransactions();
$transactions = [];
/** @var TransactionTransformer $transformer */
$transformer = app(TransactionTransformer::class);
$transformer->setParameters(new ParameterBag);
/** @var Transaction $transaction */
foreach ($set as $transaction) {
$transactions[] = $transformer->transform($transaction);
}
$events = $this->repository->getPiggyBankEvents($journal);
$what = strtolower($transactionType);
$subTitle = trans('firefly.' . $what) . ' "' . $journal->description . '"';
return view('transactions.show', compact('journal', 'attachments', 'events', 'subTitle', 'what', 'transactions', 'linkTypes', 'links'));
}
}

View File

@@ -98,7 +98,9 @@ class TransactionGroup extends Model
/** @var User $user */
$user = auth()->user();
/** @var TransactionGroup $group */
$group = $user->transactionGroups()->where('transaction_groups.id', $groupId)->first(['transaction_groups.*']);
$group = $user->transactionGroups()
->with(['transactionJournals','transactionJournals.transactions'])
->where('transaction_groups.id', $groupId)->first(['transaction_groups.*']);
if (null !== $group) {
return $group;
}

View File

@@ -368,7 +368,7 @@ class TransactionJournal extends Model
* @codeCoverageIgnore
* @return BelongsTo
*/
public function transactionGroup(): BelongsToMany
public function transactionGroup(): BelongsTo
{
return $this->belongsTo(TransactionGroup::class);
}

View File

@@ -63,13 +63,7 @@ use FireflyIII\Support\Steam;
use FireflyIII\Support\Twig\AmountFormat;
use FireflyIII\Support\Twig\Extension\TransactionGroupTwig;
use FireflyIII\Support\Twig\General;
use FireflyIII\Support\Twig\Journal;
use FireflyIII\Support\Twig\Loader\AccountLoader;
use FireflyIII\Support\Twig\Loader\TransactionGroupLoader;
use FireflyIII\Support\Twig\Loader\TransactionJournalLoader;
use FireflyIII\Support\Twig\Loader\TransactionLoader;
use FireflyIII\Support\Twig\Rule;
use FireflyIII\Support\Twig\Transaction;
use FireflyIII\Support\Twig\Translation;
use FireflyIII\Validation\FireflyValidator;
use Illuminate\Foundation\Application;
@@ -101,14 +95,9 @@ class FireflyServiceProvider extends ServiceProvider
);
$config = app('config');
Twig::addExtension(new Functions($config));
Twig::addRuntimeLoader(new TransactionLoader);
Twig::addRuntimeLoader(new AccountLoader);
Twig::addRuntimeLoader(new TransactionJournalLoader);
Twig::addExtension(new General);
Twig::addExtension(new TransactionGroupTwig);
Twig::addExtension(new Journal);
Twig::addExtension(new Translation);
Twig::addExtension(new Transaction);
Twig::addExtension(new Rule);
Twig::addExtension(new AmountFormat);
Twig::addExtension(new Twig_Extension_Debug);

View File

@@ -29,11 +29,14 @@ use DB;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\TransactionGroupFactory;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Services\Internal\Update\GroupUpdateService;
use FireflyIII\Support\NullArrayObject;
use Illuminate\Database\Eloquent\Builder;
/**
* Class TransactionGroupRepository
@@ -52,6 +55,80 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
}
}
/**
* Return all attachments for all journals in the group.
*
* @param TransactionGroup $group
*
* @return array
*/
public function getAttachments(TransactionGroup $group): array
{
$journals = $group->transactionJournals->pluck('id')->toArray();
$set = Attachment::whereIn('attachable_id', $journals)
->where('attachable_type', TransactionJournal::class)
->whereNull('deleted_at')->get();
$result = [];
/** @var Attachment $attachment */
foreach ($set as $attachment) {
$current = $attachment->toArray();
$current['file_exists'] = true;
$current['journal_title'] = $attachment->attachable->description;
$result[] = $current;
}
//$result = $set->toArray();
return $result;
}
/**
* Return all journal links for all journals in the group.
*
* @param TransactionGroup $group
*
* @return array
*/
public function getLinks(TransactionGroup $group): array
{
$return = [];
$journals = $group->transactionJournals->pluck('id')->toArray();
$set = TransactionJournalLink
::where(
static function (Builder $q) use ($journals) {
$q->whereIn('source_id', $journals);
$q->orWhereIn('destination_id', $journals);
}
)
->with(['source', 'destination'])
->leftJoin('link_types', 'link_types.id', '=', 'journal_links.link_type_id')
->get(['journal_links.*', 'link_types.inward', 'link_types.outward']);
/** @var TransactionJournalLink $entry */
foreach ($set as $entry) {
$journalId = in_array($entry->source_id, $journals, true) ? $entry->source_id : $entry->destination_id;
$return[$journalId] = $return[$journalId] ?? [];
if ($journalId === $entry->source_id) {
$return[$journalId][] = [
'link' => $entry->outward,
'group' => $entry->destination->transaction_group_id,
'description' => $entry->destination->description,
];
}
if ($journalId === $entry->destination_id) {
$return[$journalId][] = [
'link' => $entry->inward,
'group' => $entry->source->transaction_group_id,
'description' => $entry->source->description,
];
}
}
return $return;
}
/**
* Return object with all found meta field things as Carbon objects.
*
@@ -124,6 +201,18 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
return $note->text;
}
/**
* Return all piggy bank events for all journals in the group.
*
* @param TransactionGroup $group
*
* @return array
*/
public function getPiggyEvents(TransactionGroup $group): array
{
return [];
}
/**
* Get the tags for a journal (by ID).
*

View File

@@ -33,6 +33,25 @@ use FireflyIII\User;
*/
interface TransactionGroupRepositoryInterface
{
/**
* Return all attachments for all journals in the group.
*
* @param TransactionGroup $group
*
* @return array
*/
public function getAttachments(TransactionGroup $group): array;
/**
* Return all journal links for all journals in the group.
*
* @param TransactionGroup $group
*
* @return array
*/
public function getLinks(TransactionGroup $group): array;
/**
* Return object with all found meta field things as Carbon objects.
*
@@ -62,6 +81,15 @@ interface TransactionGroupRepositoryInterface
*/
public function getNoteText(int $journalId): ?string;
/**
* Return all piggy bank events for all journals in the group.
*
* @param TransactionGroup $group
*
* @return array
*/
public function getPiggyEvents(TransactionGroup $group): array;
/**
* Get the tags for a journal (by ID).
*

View File

@@ -28,6 +28,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Jobs\CreateRecurringTransactions;
use FireflyIII\Models\Configuration;
use Log;
use Preferences;
/**
* Class RecurringCronjob
@@ -67,6 +68,7 @@ class RecurringCronjob extends AbstractCronjob
Log::error($e->getTraceAsString());
throw new FireflyException(sprintf('Could not run recurring transaction cron job: %s', $e->getMessage()));
}
Preferences::mark();
return true;
}

View File

@@ -1,51 +0,0 @@
<?php
/**
* Account.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig\Extension;
use FireflyIII\Models\Account as AccountModel;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Twig_Extension;
/**
* Class Account.
*/
class Account extends Twig_Extension
{
/**
* @param AccountModel $account
* @param string $field
*
* @return string
*/
public function getMetaField(AccountModel $account, string $field): string
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$result = $repository->getMetaValue($account, $field);
if (null === $result) {
return '';
}
return $result;
}
}

View File

@@ -1,426 +0,0 @@
<?php
/**
* Transaction.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig\Extension;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\Transaction as TransactionModel;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Lang;
use Log;
use Twig_Extension;
/**
* Class Transaction.
*/
class Transaction extends Twig_Extension
{
/**
* Can show the amount of a transaction, if that transaction has been collected by the journal collector.
*
* @param TransactionModel $transaction
*
* @return string
*/
public function amount(TransactionModel $transaction): string
{
// at this point amount is always negative.
$amount = bcmul(app('steam')->positive((string)$transaction->transaction_amount), '-1');
$format = '%s';
$coloured = true;
if (TransactionType::RECONCILIATION === $transaction->transaction_type_type && 1 === bccomp((string)$transaction->transaction_amount, '0')) {
$amount = bcmul($amount, '-1');
}
if (TransactionType::DEPOSIT === $transaction->transaction_type_type) {
$amount = bcmul($amount, '-1');
}
if (TransactionType::TRANSFER === $transaction->transaction_type_type) {
$amount = app('steam')->positive($amount);
$coloured = false;
$format = '<span class="text-info">%s</span>';
}
if (TransactionType::OPENING_BALANCE === $transaction->transaction_type_type) {
$amount = (string)$transaction->transaction_amount;
}
$currency = new TransactionCurrency;
$currency->symbol = $transaction->transaction_currency_symbol;
$currency->decimal_places = $transaction->transaction_currency_dp;
$str = sprintf($format, app('amount')->formatAnything($currency, $amount, $coloured));
if (null !== $transaction->transaction_foreign_amount) {
$amount = bcmul(app('steam')->positive((string)$transaction->transaction_foreign_amount), '-1');
if (TransactionType::DEPOSIT === $transaction->transaction_type_type) {
$amount = bcmul($amount, '-1');
}
if (TransactionType::TRANSFER === $transaction->transaction_type_type) {
$amount = app('steam')->positive($amount);
$coloured = false;
$format = '<span class="text-info">%s</span>';
}
$currency = new TransactionCurrency;
$currency->symbol = $transaction->foreign_currency_symbol;
$currency->decimal_places = $transaction->foreign_currency_dp;
$str .= ' (' . sprintf($format, app('amount')->formatAnything($currency, $amount, $coloured)) . ')';
}
return $str;
}
/**
* @param array $transaction
*
* @return string
*/
public function amountArray(array $transaction): string
{
// first display amount:
$amount = (string)$transaction['amount'];
$fakeCurrency = new TransactionCurrency;
$fakeCurrency->decimal_places = $transaction['currency_decimal_places'];
$fakeCurrency->symbol = $transaction['currency_symbol'];
$string = app('amount')->formatAnything($fakeCurrency, $amount, true);
// then display (if present) the foreign amount:
if (null !== $transaction['foreign_amount']) {
$amount = (string)$transaction['foreign_amount'];
$fakeCurrency = new TransactionCurrency;
$fakeCurrency->decimal_places = $transaction['foreign_currency_decimal_places'];
$fakeCurrency->symbol = $transaction['foreign_currency_symbol'];
$string .= ' (' . app('amount')->formatAnything($fakeCurrency, $amount, true) . ')';
}
return $string;
}
/**
*
* @param TransactionModel $transaction
*
* @return string
*/
public function budgets(TransactionModel $transaction): string
{
$txt = '';
// journal has a budget:
if (null !== $transaction->transaction_journal_budget_id) {
$name = $transaction->transaction_journal_budget_name;
$txt = sprintf('<a href="%s" title="%s">%s</a>', route('budgets.show', [$transaction->transaction_journal_budget_id]), $name, $name);
}
// transaction has a budget
if (null !== $transaction->transaction_budget_id && '' === $txt) {
$name = $transaction->transaction_budget_name;
$txt = sprintf('<a href="%s" title="%s">%s</a>', route('budgets.show', [$transaction->transaction_budget_id]), $name, $name);
}
if ('' === $txt) {
// see if the transaction has a budget:
$budgets = $transaction->budgets()->get();
if (0 === $budgets->count()) {
$budgets = $transaction->transactionJournal()->first()->budgets()->get();
}
if ($budgets->count() > 0) {
$str = [];
foreach ($budgets as $budget) {
$str[] = sprintf('<a href="%s" title="%s">%s</a>', route('budgets.show', [$budget->id]), $budget->name, $budget->name);
}
$txt = implode(', ', $str);
}
}
return $txt;
}
/**
* @param TransactionModel $transaction
*
* @return string
*/
public function categories(TransactionModel $transaction): string
{
$txt = '';
// journal has a category:
if (null !== $transaction->transaction_journal_category_id) {
$name = $transaction->transaction_journal_category_name;
$txt = sprintf('<a href="%s" title="%s">%s</a>', route('categories.show', [$transaction->transaction_journal_category_id]), $name, $name);
}
// transaction has a category:
if (null !== $transaction->transaction_category_id && '' === $txt) {
$name = $transaction->transaction_category_name;
$txt = sprintf('<a href="%s" title="%s">%s</a>', route('categories.show', [$transaction->transaction_category_id]), $name, $name);
}
if ('' === $txt) {
// see if the transaction has a category:
$categories = $transaction->categories()->get();
if (0 === $categories->count()) {
$categories = $transaction->transactionJournal()->first()->categories()->get();
}
if ($categories->count() > 0) {
$str = [];
foreach ($categories as $category) {
$str[] = sprintf('<a href="%s" title="%s">%s</a>', route('categories.show', [$category->id]), $category->name, $category->name);
}
$txt = implode(', ', $str);
}
}
return $txt;
}
/**
* @param TransactionModel $transaction
*
* @return string
*/
public function description(TransactionModel $transaction): string
{
$description = $transaction->description;
if ('' !== (string)$transaction->transaction_description) {
$description = $transaction->transaction_description . ' (' . $transaction->description . ')';
}
return $description;
}
/**
* @param TransactionModel $transaction
*
* @return string
*/
public function destinationAccount(TransactionModel $transaction): string
{
if (TransactionType::RECONCILIATION === $transaction->transaction_type_type) {
return '&mdash;';
}
$name = $transaction->account_name;
$iban = $transaction->account_iban;
$transactionId = (int)$transaction->account_id;
$type = $transaction->account_type;
// name is present in object, use that one:
if (null !== $transaction->opposing_account_id && bccomp($transaction->transaction_amount, '0') === -1) {
$name = $transaction->opposing_account_name;
$transactionId = (int)$transaction->opposing_account_id;
$type = $transaction->opposing_account_type;
$iban = $transaction->opposing_account_iban;
}
// Find the opposing account and use that one:
if (null === $transaction->opposing_account_id && bccomp($transaction->transaction_amount, '0') === -1) {
// if the amount is negative, find the opposing account and use that one:
$journalId = $transaction->journal_id;
/** @var TransactionModel $other */
$other = TransactionModel
::where('transaction_journal_id', $journalId)
->where('transactions.id', '!=', $transaction->id)
->where('amount', '=', bcmul($transaction->transaction_amount, '-1'))
->where('identifier', $transaction->identifier)
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->first(['transactions.account_id', 'accounts.encrypted', 'accounts.name', 'account_types.type']);
if (null === $other) {
Log::error(sprintf('Cannot find other transaction for journal #%d', $journalId));
return '';
}
$name = $other->name;
$transactionId = $other->account_id;
$type = $other->type;
}
if (AccountType::CASH === $type) {
$txt = '<span class="text-success">(' . trans('firefly.cash') . ')</span>';
return $txt;
}
$txt = sprintf('<a title="%3$s" href="%2$s">%1$s</a>', e($name), route('accounts.show', [$transactionId]), $iban);
return $txt;
}
/**
* @param TransactionModel $transaction
*
* @return string
*/
public function hasAttachments(TransactionModel $transaction): string
{
$res = '';
if (\is_int($transaction->attachmentCount) && $transaction->attachmentCount > 0) {
$res = sprintf(
'<i class="fa fa-paperclip" title="%s"></i>', Lang::choice(
'firefly.nr_of_attachments',
$transaction->attachmentCount, ['count' => $transaction->attachmentCount]
)
);
}
if (null === $transaction->attachmentCount) {
$journalId = (int)$transaction->journal_id;
$count = Attachment::whereNull('deleted_at')
->where('attachable_type', TransactionJournal::class)
->where('attachable_id', $journalId)
->count();
if ($count > 0) {
$res = sprintf('<i class="fa fa-paperclip" title="%s"></i>', Lang::choice('firefly.nr_of_attachments', $count, ['count' => $count]));
}
}
return $res;
}
/**
* @param TransactionModel $transaction
*
* @return string
*/
public function icon(TransactionModel $transaction): string
{
switch ($transaction->transaction_type_type) {
case TransactionType::WITHDRAWAL:
$txt = sprintf('<i class="fa fa-long-arrow-left fa-fw" title="%s"></i>', (string)trans('firefly.withdrawal'));
break;
case TransactionType::DEPOSIT:
$txt = sprintf('<i class="fa fa-long-arrow-right fa-fw" title="%s"></i>', (string)trans('firefly.deposit'));
break;
case TransactionType::TRANSFER:
$txt = sprintf('<i class="fa fa-fw fa-exchange" title="%s"></i>', (string)trans('firefly.transfer'));
break;
case TransactionType::OPENING_BALANCE:
$txt = sprintf('<i class="fa-fw fa fa-star-o" title="%s"></i>', (string)trans('firefly.opening_balance'));
break;
case TransactionType::RECONCILIATION:
$txt = sprintf('<i class="fa-fw fa fa-calculator" title="%s"></i>', (string)trans('firefly.reconciliation_transaction'));
break;
default:
$txt = '';
break;
}
return $txt;
}
/**
* @param TransactionModel $transaction
*
* @return string
*/
public function isReconciled(TransactionModel $transaction): string
{
$icon = '';
if (1 === (int)$transaction->reconciled) {
$icon = '<i class="fa fa-check"></i>';
}
return $icon;
}
/**
* Returns an icon when the transaction is a split transaction.
*
* @param TransactionModel $transaction
*
* @return string
*/
public function isSplit(TransactionModel $transaction): string
{
$res = '';
if (true === $transaction->is_split) {
$res = '<i class="fa fa-fw fa-share-alt" aria-hidden="true"></i>';
}
if (null === $transaction->is_split) {
$journalId = (int)$transaction->journal_id;
$count = TransactionModel::where('transaction_journal_id', $journalId)->whereNull('deleted_at')->count();
if ($count > 2) {
$res = '<i class="fa fa-fw fa-share-alt" aria-hidden="true"></i>';
}
}
return $res;
}
/**
* @param TransactionModel $transaction
*
* @return string
*/
public function sourceAccount(TransactionModel $transaction): string
{
if (TransactionType::RECONCILIATION === $transaction->transaction_type_type) {
return '&mdash;';
}
// if the amount is negative, assume that the current account (the one in $transaction) is indeed the source account.
$name = $transaction->account_name;
$transactionId = (int)$transaction->account_id;
$type = $transaction->account_type;
$iban = $transaction->account_iban;
// name is present in object, use that one:
if (null !== $transaction->opposing_account_id && 1 === bccomp($transaction->transaction_amount, '0')) {
$name = $transaction->opposing_account_name;
$transactionId = (int)$transaction->opposing_account_id;
$type = $transaction->opposing_account_type;
$iban = $transaction->opposing_account_iban;
}
// Find the opposing account and use that one:
if (null === $transaction->opposing_account_id && 1 === bccomp($transaction->transaction_amount, '0')) {
$journalId = $transaction->journal_id;
/** @var TransactionModel $other */
$other = TransactionModel::where('transaction_journal_id', $journalId)->where('transactions.id', '!=', $transaction->id)
->where('amount', '=', bcmul($transaction->transaction_amount, '-1'))->where(
'identifier',
$transaction->identifier
)
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->first(['transactions.account_id', 'accounts.encrypted', 'accounts.name', 'account_types.type']);
$name = $other->name;
$transactionId = $other->account_id;
$type = $other->type;
}
if (AccountType::CASH === $type) {
$txt = '<span class="text-success">(' . trans('firefly.cash') . ')</span>';
return $txt;
}
$txt = sprintf('<a title="%3$s" href="%2$s">%1$s</a>', e($name), route('accounts.show', [$transactionId]), $iban);
return $txt;
}
}

View File

@@ -1,178 +0,0 @@
<?php
/**
* TransactionJournal.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig\Extension;
use Carbon\Carbon;
use FireflyIII\Models\Transaction as TransactionModel;
use FireflyIII\Models\TransactionJournal as JournalModel;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Twig_Extension;
/**
* Class TransactionJournal
*/
class TransactionJournal extends Twig_Extension
{
/**
* @param JournalModel $journal
* @param string $field
*
* @return null|Carbon
*/
public function getMetaDate(JournalModel $journal, string $field): ?Carbon
{
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
return $repository->getMetaDate($journal, $field);
}
/**
* @param JournalModel $journal
* @param string $field
*
* @return string
*/
public function getMetaField(JournalModel $journal, string $field): string
{
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$result = $repository->getMetaField($journal, $field);
if (null === $result) {
return '';
}
return $result;
}
/**
* Return if journal HAS field.
*
* @param JournalModel $journal
* @param string $field
*
* @return bool
*/
public function hasMetaField(JournalModel $journal, string $field): bool
{
// HIER BEN JE
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$result = $repository->getMetaField($journal, $field);
if (null === $result) {
return false;
}
if ('' === (string)$result) {
return false;
}
return true;
}
/**
* @param JournalModel $journal
*
* @return string
*/
public function totalAmount(JournalModel $journal): string
{
$type = $journal->transactionType->type;
$totals = $this->getTotalAmount($journal);
$array = [];
foreach ($totals as $total) {
if (TransactionType::WITHDRAWAL === $type) {
$total['amount'] = bcmul($total['amount'], '-1');
}
if (null !== $total['currency']) {
$array[] = app('amount')->formatAnything($total['currency'], $total['amount']);
}
}
return implode(' / ', $array);
}
/**
* @param JournalModel $journal
*
* @return string
*/
public function totalAmountPlain(JournalModel $journal): string
{
$type = $journal->transactionType->type;
$totals = $this->getTotalAmount($journal);
$array = [];
foreach ($totals as $total) {
if (TransactionType::WITHDRAWAL === $type) {
$total['amount'] = bcmul($total['amount'], '-1');
}
$array[] = app('amount')->formatAnything($total['currency'], $total['amount'], false);
}
return implode(' / ', $array);
}
/**
* @param JournalModel $journal
*
* @return array
*/
private function getTotalAmount(JournalModel $journal): array
{
$transactions = $journal->transactions()->where('amount', '>', 0)->get();
$totals = [];
/** @var TransactionModel $transaction */
foreach ($transactions as $transaction) {
$currencyId = $transaction->transaction_currency_id;
$currency = $transaction->transactionCurrency;
if (!isset($totals[$currencyId])) {
$totals[$currencyId] = [
'amount' => '0',
'currency' => $currency,
];
}
$totals[$currencyId]['amount'] = bcadd($transaction->amount, $totals[$currencyId]['amount']);
if (null !== $transaction->foreign_currency_id) {
$foreignAmount = $transaction->foreign_amount ?? '0';
$foreignId = $transaction->foreign_currency_id;
$foreign = $transaction->foreignCurrency;
if (!isset($totals[$foreignId])) {
$totals[$foreignId] = [
'amount' => '0',
'currency' => $foreign,
];
}
$totals[$foreignId]['amount'] = bcadd(
$foreignAmount,
$totals[$foreignId]['amount']
);
}
}
return $totals;
}
}

View File

@@ -24,8 +24,8 @@ namespace FireflyIII\Support\Twig;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Support\Twig\Extension\Account as AccountExtension;
use League\CommonMark\CommonMarkConverter;
use Route;
use Twig_Extension;
@@ -56,14 +56,12 @@ class General extends Twig_Extension
public function getFunctions(): array
{
return [
$this->getCurrencyCode(),
$this->getCurrencySymbol(),
$this->phpdate(),
$this->activeRouteStrict(),
$this->activeRoutePartial(),
$this->activeRoutePartialWhat(),
$this->formatDate(),
new Twig_SimpleFunction('accountGetMetaField', [AccountExtension::class, 'getMetaField']),
$this->getMetaField(),
$this->hasRole(),
];
}
@@ -102,7 +100,7 @@ class General extends Twig_Extension
return new Twig_SimpleFunction(
'activeRoutePartialWhat',
function ($context): string {
[, $route, $what] = \func_get_args();
[, $route, $what] = func_get_args();
$activeWhat = $context['what'] ?? false;
if ($what === $activeWhat && !(false === stripos(Route::getCurrentRoute()->getName(), $route))) {
@@ -139,6 +137,8 @@ class General extends Twig_Extension
}
/**
* Show account balance. Only used on the front page of Firefly III.
*
* @return Twig_SimpleFilter
*/
protected function balance(): Twig_SimpleFilter
@@ -158,6 +158,8 @@ class General extends Twig_Extension
}
/**
* Formats a string as a thing by converting it to a Carbon first.
*
* @return Twig_SimpleFunction
*/
protected function formatDate(): Twig_SimpleFunction
@@ -173,6 +175,8 @@ class General extends Twig_Extension
}
/**
* Used to convert 1024 to 1kb etc.
*
* @return Twig_SimpleFilter
*/
protected function formatFilesize(): Twig_SimpleFilter
@@ -198,25 +202,19 @@ class General extends Twig_Extension
/**
* @return Twig_SimpleFunction
*/
protected function getCurrencyCode(): Twig_SimpleFunction
protected function getMetaField(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'getCurrencyCode',
function (): string {
return app('amount')->getCurrencyCode();
}
);
}
'accountGetMetaField',
static function (Account $account, string $field): string {
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$result = $repository->getMetaValue($account, $field);
if (null === $result) {
return '';
}
/**
* @return Twig_SimpleFunction
*/
protected function getCurrencySymbol(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'getCurrencySymbol',
function (): string {
return app('amount')->getCurrencySymbol();
return $result;
}
);
}
@@ -257,6 +255,8 @@ class General extends Twig_Extension
}
/**
* Show icon with attachment.
*
* @return Twig_SimpleFilter
*/
protected function mimeIcon(): Twig_SimpleFilter
@@ -334,6 +334,8 @@ class General extends Twig_Extension
}
/**
* Basic example thing for some views.
*
* @return Twig_SimpleFunction
*/
protected function phpdate(): Twig_SimpleFunction

View File

@@ -1,223 +0,0 @@
<?php
/**
* Journal.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Twig\Extension\TransactionJournal as TransactionJournalExtension;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
* Class Journal.
*/
class Journal extends Twig_Extension
{
/**
* @return Twig_SimpleFunction
*/
public function getDestinationAccount(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'destinationAccount',
function (TransactionJournal $journal) {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('destination-account-string');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$list = $repository->getJournalDestinationAccounts($journal);
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {
if (AccountType::CASH === $entry->accountType->type) {
$array[] = '<span class="text-success">(cash)</span>';
continue;
}
$array[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($entry->name), route('accounts.show', $entry->id));
}
$array = array_unique($array);
$result = implode(', ', $array);
$cache->store($result);
return $result;
}
);
}
/**
* @return array
*/
public function getFilters(): array
{
$filters = [
new Twig_SimpleFilter('journalTotalAmount', [TransactionJournalExtension::class, 'totalAmount'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('journalTotalAmountPlain', [TransactionJournalExtension::class, 'totalAmountPlain'], ['is_safe' => ['html']]),
];
return $filters;
}
/**
* @return array
*/
public function getFunctions(): array
{
$functions = [
$this->getSourceAccount(),
$this->getDestinationAccount(),
$this->journalBudgets(),
$this->journalCategories(),
new Twig_SimpleFunction('journalGetMetaField', [TransactionJournalExtension::class, 'getMetaField']),
new Twig_SimpleFunction('journalHasMeta', [TransactionJournalExtension::class, 'hasMetaField']),
new Twig_SimpleFunction('journalGetMetaDate', [TransactionJournalExtension::class, 'getMetaDate']),
];
return $functions;
}
/**
* @return Twig_SimpleFunction
*/
public function getSourceAccount(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'sourceAccount',
function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('source-account-string');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$list = $repository->getJournalSourceAccounts($journal);
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {
if (AccountType::CASH === $entry->accountType->type) {
$array[] = '<span class="text-success">(cash)</span>';
continue;
}
$array[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($entry->name), route('accounts.show', $entry->id));
}
$array = array_unique($array);
$result = implode(', ', $array);
$cache->store($result);
return $result;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalBudgets(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalBudgets',
function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('budget-string');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$budgets = [];
// get all budgets:
foreach ($journal->budgets as $budget) {
$budgets[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($budget->name), route('budgets.show', $budget->id));
}
// and more!
foreach ($journal->transactions as $transaction) {
foreach ($transaction->budgets as $budget) {
$budgets[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($budget->name), route('budgets.show', $budget->id));
}
}
$string = implode(', ', array_unique($budgets));
$cache->store($string);
return $string;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalCategories(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalCategories',
function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('category-string');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$categories = [];
// get all categories for the journal itself (easy):
foreach ($journal->categories as $category) {
$categories[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($category->name), route('categories.show', $category->id));
}
if (0 === \count($categories)) {
$set = Category::distinct()->leftJoin('category_transaction', 'categories.id', '=', 'category_transaction.category_id')
->leftJoin('transactions', 'category_transaction.transaction_id', '=', 'transactions.id')
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('categories.user_id', $journal->user_id)
->where('transaction_journals.id', $journal->id)
->whereNull('transactions.deleted_at')
->get(['categories.*']);
/** @var Category $category */
foreach ($set as $category) {
$categories[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($category->name), route('categories.show', $category->id));
}
}
$string = implode(', ', array_unique($categories));
$cache->store($string);
return $string;
}
);
}
}

View File

@@ -1,52 +0,0 @@
<?php
/**
* AccountLoader.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig\Loader;
use FireflyIII\Support\Twig\Extension\Account;
use Twig_RuntimeLoaderInterface;
/**
* Class AccountLoader.
*/
class AccountLoader implements Twig_RuntimeLoaderInterface
{
/**
* Creates the runtime implementation of a Twig element (filter/function/test).
*
* @param string $class A runtime class
*
* @return object|null The runtime instance or null if the loader does not know how to create the runtime for this class
*/
public function load($class)
{
// implement the logic to create an instance of $class
// and inject its dependencies
// most of the time, it means using your dependency injection container
if (Account::class === $class) {
return app(Account::class);
}
return null;
}
}

View File

@@ -1,52 +0,0 @@
<?php
/**
* TransactionJournalLoader.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig\Loader;
use FireflyIII\Support\Twig\Extension\TransactionJournal;
use Twig_RuntimeLoaderInterface;
/**
* Class TransactionJournalLoader.
*/
class TransactionJournalLoader implements Twig_RuntimeLoaderInterface
{
/**
* Creates the runtime implementation of a Twig element (filter/function/test).
*
* @param string $class A runtime class
*
* @return object|null The runtime instance or null if the loader does not know how to create the runtime for this class
*/
public function load($class)
{
// implement the logic to create an instance of $class
// and inject its dependencies
// most of the time, it means using your dependency injection container
if (TransactionJournal::class === $class) {
return app(TransactionJournal::class);
}
return null;
}
}

View File

@@ -1,52 +0,0 @@
<?php
/**
* TransactionLoader.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig\Loader;
use FireflyIII\Support\Twig\Extension\Transaction;
use Twig_RuntimeLoaderInterface;
/**
* Class TransactionLoader.
*/
class TransactionLoader implements Twig_RuntimeLoaderInterface
{
/**
* Creates the runtime implementation of a Twig element (filter/function/test).
*
* @param string $class A runtime class
*
* @return object|null The runtime instance or null if the loader does not know how to create the runtime for this class
*/
public function load($class)
{
// implement the logic to create an instance of $class
// and inject its dependencies
// most of the time, it means using your dependency injection container
if (Transaction::class === $class) {
return app(Transaction::class);
}
return null;
}
}

View File

@@ -1,55 +0,0 @@
<?php
/**
* Transaction.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use FireflyIII\Support\Twig\Extension\Transaction as TransactionExtension;
use Twig_Extension;
use Twig_SimpleFilter;
/**
* Class Transaction.
*/
class Transaction extends Twig_Extension
{
/**
* @return array
*/
public function getFilters(): array
{
$filters = [
new Twig_SimpleFilter('transactionIcon', [TransactionExtension::class, 'icon'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('transactionDescription', [TransactionExtension::class, 'description']),
new Twig_SimpleFilter('transactionIsSplit', [TransactionExtension::class, 'isSplit'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('transactionReconciled', [TransactionExtension::class, 'isReconciled'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('transactionHasAtt', [TransactionExtension::class, 'hasAttachments'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('transactionAmount', [TransactionExtension::class, 'amount'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('transactionArrayAmount', [TransactionExtension::class, 'amountArray'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('transactionBudgets', [TransactionExtension::class, 'budgets'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('transactionCategories', [TransactionExtension::class, 'categories'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('transactionSourceAccount', [TransactionExtension::class, 'sourceAccount'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('transactionDestinationAccount', [TransactionExtension::class, 'destinationAccount'], ['is_safe' => ['html']]),
];
return $filters;
}
}

View File

@@ -23,6 +23,8 @@ declare(strict_types=1);
namespace FireflyIII\Support\Twig\Extension;
use Carbon\Carbon;
use DB;
use FireflyIII\Models\TransactionType;
use Twig_Extension;
use Twig_SimpleFunction;
@@ -40,6 +42,9 @@ class TransactionGroupTwig extends Twig_Extension
return [
$this->transactionAmount(),
$this->groupAmount(),
$this->journalHasMeta(),
$this->journalGetMetaDate(),
$this->journalGetMetaField()
];
}
@@ -64,6 +69,71 @@ class TransactionGroupTwig extends Twig_Extension
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalGetMetaDate(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalGetMetaDate',
static function (int $journalId, string $metaField) {
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first();
if (null === $entry) {
return new Carbon;
}
return new Carbon(json_decode($entry->data, false));
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalGetMetaField(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalGetMetaField',
static function (int $journalId, string $metaField) {
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first();
if (null === $entry) {
return '';
}
return json_decode($entry->data, true);
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalHasMeta(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalHasMeta',
static function (int $journalId, string $metaField) {
$count = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->count();
return 1 === $count;
}
);
}
/**
* @return Twig_SimpleFunction
*/

View File

@@ -48,36 +48,4 @@ class Translation extends Twig_Extension
return $filters;
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->journalLinkTranslation(),
];
}
/**
* @return Twig_SimpleFunction
*/
public function journalLinkTranslation(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalLinkTranslation',
function (string $direction, string $original) {
$key = sprintf('firefly.%s_%s', $original, $direction);
$translation = trans($key);
if ($key === $translation) {
return $original;
}
return $translation;
},
['is_safe' => ['html']]
);
}
}

View File

@@ -23,9 +23,15 @@ declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\Support\NullArrayObject;
use Illuminate\Support\Collection;
/**
* Class TransactionGroupTransformer
@@ -88,6 +94,172 @@ class TransactionGroupTransformer extends AbstractTransformer
return $result;
}
/**
* @param TransactionGroup $group
*
* @return array
*/
public function transformObject(TransactionGroup $group): array
{
//$first = $group->transactionJournals->first();
$result = [
'id' => (int)$group->id,
'created_at' => $group->created_at->toAtomString(),
'updated_at' => $group->updated_at->toAtomString(),
'user' => (int)$group->user_id,
'group_title' => $group->title,
'transactions' => $this->transformJournals($group->transactionJournals),
'links' => [
[
'rel' => 'self',
'uri' => '/transactions/' . $group->id,
],
],
];
// do something else.
return $result;
}
/**
* @param TransactionJournal $journal
*
* @return Transaction
*/
private function getDestinationTransaction(TransactionJournal $journal): Transaction
{
return $journal->transactions->first(
static function (Transaction $transaction) {
return (float)$transaction->amount > 0;
}
);
}
/**
* @param TransactionJournal $journal
*
* @return Transaction
*/
private function getSourceTransaction(TransactionJournal $journal): Transaction
{
return $journal->transactions->first(
static function (Transaction $transaction) {
return (float)$transaction->amount < 0;
}
);
}
/**
* @param Collection $transactionJournals
*
* @return array
*/
private function transformJournals(Collection $transactionJournals): array
{
$result = [];
/** @var TransactionJournal $journal */
foreach ($transactionJournals as $journal) {
$source = $this->getSourceTransaction($journal);
$destination = $this->getDestinationTransaction($journal);
$type = $journal->transactionType->type;
// get amount
$amount = app('steam')->positive($source->amount);
if (TransactionType::WITHDRAWAL !== $type) {
$amount = app('steam')->negative($source->amount);
}
// get foreign amount:
$foreignAmount = null;
if (null !== $source->foreign_amount) {
$foreignAmount = TransactionType::WITHDRAWAL !== $type
? app('steam')->positive($source->foreign_amount)
: app('steam')->negative($source->foreign_amount);
}
$metaFieldData = $this->groupRepos->getMetaFields($journal->id, $this->metaFields);
$metaDateData = $this->groupRepos->getMetaDateFields($journal->id, $this->metaDateFields);
/** @var Budget $budget */
$budget = $journal->budgets->first();
/** @var Category $category */
$category = $journal->categories->first();
$currency = $source->transactionCurrency;
$result[] = [
'user' => (int)$journal->user_id,
'transaction_journal_id' => $journal->id,
'type' => strtolower($type),
'date' => $journal->date->toAtomString(),
'order' => $journal->order,
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'foreign_currency_id' => $source->foreignCurrency ? $source->foreignCurrency->id : null,
'foreign_currency_code' => $source->foreignCurrency ? $source->foreignCurrency->code : null,
'foreign_currency_symbol' => $source->foreignCurrency ? $source->foreignCurrency->symbol : null,
'foreign_currency_decimal_places' => $source->foreignCurrency ? $source->foreignCurrency->decimal_places : null,
'amount' => $amount,
'foreign_amount' => $foreignAmount,
'description' => $journal->description,
'source_id' => $source->account_id,
'source_name' => $source->account->name,
'source_iban' => $source->account->iban,
'source_type' => $source->account->accountType->type,
'destination_id' => $destination->account_id,
'destination_name' => $destination->account->name,
'destination_iban' => $destination->account->iban,
'destination_type' => $destination->account->accountType->type,
'budget_id' => $budget ? $budget->id : null,
'budget_name' => $budget ? $budget->name : null,
'category_id' => $category ? $category->id : null,
'category_name' => $category ? $category->name : null,
'bill_id' => $journal->bill_id ?: null,
'bill_name' => $journal->bill_id ? $journal->bill->name : null,
'reconciled' => $source->reconciled,
'notes' => $this->groupRepos->getNoteText($journal->id),
'tags' => $this->groupRepos->getTags($journal->id),
'internal_reference' => $metaFieldData['internal_reference'],
'external_id' => $metaFieldData['external_id'],
'original_source' => $metaFieldData['original_source'],
'recurrence_id' => $metaFieldData['recurrence_id'],
'bunq_payment_id' => $metaFieldData['bunq_payment_id'],
'import_hash_v2' => $metaFieldData['import_hash_v2'],
'sepa_cc' => $metaFieldData['sepa_cc'],
'sepa_ct_op' => $metaFieldData['sepa_ct_op'],
'sepa_ct_id' => $metaFieldData['sepa_ct_id'],
'sepa_db' => $metaFieldData['sepa_ddb'],
'sepa_country' => $metaFieldData['sepa_country'],
'sepa_ep' => $metaFieldData['sepa_ep'],
'sepa_ci' => $metaFieldData['sepa_ci'],
'sepa_batch_id' => $metaFieldData['sepa_batch_id'],
'interest_date' => $metaDateData['interest_date'] ? $metaDateData['interest_date']->toAtomString() : null,
'book_date' => $metaDateData['book_date'] ? $metaDateData['book_date']->toAtomString() : null,
'process_date' => $metaDateData['process_date'] ? $metaDateData['process_date']->toAtomString() : null,
'due_date' => $metaDateData['due_date'] ? $metaDateData['due_date']->toAtomString() : null,
'payment_date' => $metaDateData['payment_date'] ? $metaDateData['payment_date']->toAtomString() : null,
'invoice_date' => $metaDateData['invoice_date'] ? $metaDateData['invoice_date']->toAtomString() : null,
];
}
return $result;
}
/**
* @param NullArrayObject $data
*

View File

@@ -68,11 +68,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property bool blocked
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property string $password
* @property string|null $remember_token
* @property string|null $reset
* @property bool $blocked
* @property string|null $blocked_code
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Account[] $accounts
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Attachment[] $attachments
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\AvailableBudget[] $availableBudgets
@@ -87,7 +84,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\PiggyBank[] $piggyBanks
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Preference[] $preferences
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Recurrence[] $recurrences
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Role[] $roles
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\RuleGroup[] $ruleGroups
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Rule[] $rules
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Tag[] $tags

View File

@@ -65,6 +65,9 @@ class AccountValidator
$this->combinations = config('firefly.source_dests');
/** @var AccountRepositoryInterface accountRepository */
$this->accountRepository = app(AccountRepositoryInterface::class);
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
/**

View File

@@ -110,15 +110,20 @@ trait TransactionValidation
$data = $validator->getData();
$transactions = $data['transactions'] ?? [];
foreach ($transactions as $index => $transaction) {
// must have currency info.
if (isset($transaction['foreign_amount'])
&& !(isset($transaction['foreign_currency_id'])
|| isset($transaction['foreign_currency_code']))) {
// if foreign amount is present, then the currency must be as well.
if (isset($transaction['foreign_amount']) && !(isset($transaction['foreign_currency_id']) || isset($transaction['foreign_currency_code']))) {
$validator->errors()->add(
'transactions.' . $index . '.foreign_amount',
(string)trans('validation.require_currency_info')
);
}
// if the currency is present, then the amount must be present as well.
if ((isset($transaction['foreign_currency_id']) || isset($transaction['foreign_currency_code'])) && !isset($transaction['foreign_amount'])) {
$validator->errors()->add(
'transactions.' . $index . '.foreign_amount',
(string)trans('validation.require_currency_amount')
);
}
}
}
@@ -196,16 +201,6 @@ trait TransactionValidation
}
}
/**
* If type is set, source + destination info is mandatory.
*
* @param Validator $validator
*/
protected function validateAccountPresence(Validator $validator): void
{
// TODO
}
/**
* @param Validator $validator
*/
@@ -299,113 +294,12 @@ trait TransactionValidation
return;
}
foreach ($transactions as $index => $transaction) {
$journalId = (int)($transaction['transaction_journal_id'] ?? 0);
$journalId = $transaction['transaction_journal_id'] ?? null;
$journalId = null === $journalId ? null : (int)$journalId;
$count = $transactionGroup->transactionJournals()->where('id', $journalId)->count();
if (0 === $journalId || 0 === $count) {
if (null === $journalId || (null !== $journalId && 0 !== $journalId && 0 === $count)) {
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), (string)trans('validation.need_id_in_edit'));
}
}
}
// /**
// * Throws an error when this asset account is invalid.
// *
// * @noinspection MoreThanThreeArgumentsInspection
// *
// * @param Validator $validator
// * @param int|null $accountId
// * @param null|string $accountName
// * @param string $idField
// * @param string $nameField
// *
// * @return null|Account
// */
// protected function assetAccountExists(Validator $validator, ?int $accountId, ?string $accountName, string $idField, string $nameField): ?Account
// {
// /** @var User $admin */
// $admin = auth()->user();
// $accountId = (int)$accountId;
// $accountName = (string)$accountName;
// // both empty? hard exit.
// if ($accountId < 1 && '' === $accountName) {
// $validator->errors()->add($idField, (string)trans('validation.filled', ['attribute' => $idField]));
//
// return null;
// }
// // ID belongs to user and is asset account:
// /** @var AccountRepositoryInterface $repository */
// $repository = app(AccountRepositoryInterface::class);
// $repository->setUser($admin);
// $set = $repository->getAccountsById([$accountId]);
// Log::debug(sprintf('Count of accounts found by ID %d is: %d', $accountId, $set->count()));
// if (1 === $set->count()) {
// /** @var Account $first */
// $first = $set->first();
// if ($first->accountType->type !== AccountType::ASSET) {
// $validator->errors()->add($idField, (string)trans('validation.belongs_user'));
//
// return null;
// }
//
// // we ignore the account name at this point.
// return $first;
// }
//
// $account = $repository->findByName($accountName, [AccountType::ASSET]);
// if (null === $account) {
// $validator->errors()->add($nameField, (string)trans('validation.belongs_user'));
//
// return null;
// }
//
// return $account;
// }
//
// /**
// * Throws an error when the given opposing account (of type $type) is invalid.
// * Empty data is allowed, system will default to cash.
// *
// * @noinspection MoreThanThreeArgumentsInspection
// *
// * @param Validator $validator
// * @param string $type
// * @param int|null $accountId
// * @param null|string $accountName
// * @param string $idField
// *
// * @return null|Account
// */
// protected function opposingAccountExists(Validator $validator, string $type, ?int $accountId, ?string $accountName, string $idField): ?Account
// {
// /** @var User $admin */
// $admin = auth()->user();
// $accountId = (int)$accountId;
// $accountName = (string)$accountName;
// // both empty? done!
// if ($accountId < 1 && '' === $accountName) {
// return null;
// }
// if (0 !== $accountId) {
// // ID belongs to user and is $type account:
// /** @var AccountRepositoryInterface $repository */
// $repository = app(AccountRepositoryInterface::class);
// $repository->setUser($admin);
// $set = $repository->getAccountsById([$accountId]);
// if (1 === $set->count()) {
// /** @var Account $first */
// $first = $set->first();
// if ($first->accountType->type !== $type) {
// $validator->errors()->add($idField, (string)trans('validation.belongs_user'));
//
// return null;
// }
//
// // we ignore the account name at this point.
// return $first;
// }
// }
//
// // not having an opposing account by this name is NOT a problem.
// return null;
// }
}