diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index aed1042db5..9c2f781e41 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -41,7 +41,6 @@ use FireflyIII\Support\NullArrayObject; use FireflyIII\User; use Illuminate\Support\Collection; use Log; -use function nspl\ds\defaultarray; /** * Class TransactionJournalFactory @@ -168,6 +167,8 @@ class TransactionJournalFactory return new Collection; // @codeCoverageIgnoreEnd } + $journal->completed =true; + $journal->save(); /** Link all other data to the journal. */ diff --git a/app/Helpers/Collector/GroupCollector.php b/app/Helpers/Collector/GroupCollector.php new file mode 100644 index 0000000000..e81e26c7c6 --- /dev/null +++ b/app/Helpers/Collector/GroupCollector.php @@ -0,0 +1,290 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Helpers\Collector; + +use Carbon\Carbon; +use Exception; +use FireflyIII\Models\TransactionGroup; +use FireflyIII\User; +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Query\JoinClause; +use Illuminate\Support\Collection; + +/** + * Class GroupCollector + */ +class GroupCollector implements GroupCollectorInterface +{ + /** @var array The accounts to filter on. Asset accounts or liabilities. */ + private $accountIds; + /** @var array The standard fields to select. */ + private $fields; + /** @var int The maximum number of results. */ + private $limit; + /** @var int The page to return. */ + private $page; + /** @var HasMany The query object. */ + private $query; + /** @var User The user object. */ + private $user; + + /** + * Group collector constructor. + */ + + public function __construct() + { + if ('testing' === config('app.env')) { + app('log')->warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); + } + $this->limit = 50; + $this->page = 0; + $this->fields = [ + 'transaction_groups.id as transaction_group_id', + 'transaction_groups.title as transaction_group_title', + 'transaction_journals.id as transaction_journal_id', + 'transaction_journals.transaction_type_id', + 'transaction_types.type as transaction_type_type', + 'transaction_journals.bill_id', + 'transaction_journals.description', + 'transaction_journals.date', + 'source.id as source_transaction_id', + 'source.account_id as source_account_id', + + # currency info: + 'source.amount as amount', + 'source.transaction_currency_id as transaction_currency_id', + 'currency.decimal_places as currency_decimal_places', + 'currency.symbol as currency_symbol', + + # foreign currency info + 'source.foreign_amount as foreign_amount', + 'source.foreign_currency_id as foreign_currency_id', + 'foreign_currency.decimal_places as foreign_currency_decimal_places', + 'foreign_currency.symbol as foreign_currency_symbol', + + # destination account info: + 'destination.account_id as destination_account_id', + ]; + } + + /** + * Return the groups. + * + * @return Collection + */ + public function getGroups(): Collection + { + /** @var Collection $result */ + $result = $this->query->get($this->fields); + + // now to parse this into an array. + $array = $this->parseArray($result); + + // now filter the array according to the page and the + $offset = $this->page * $this->limit; + $limited = $array->slice($offset, $this->limit); + + return $limited; + } + + /** + * Define which accounts can be part of the source and destination transactions. + * + * @param Collection $accounts + * + * @return GroupCollectorInterface + */ + public function setAccounts(Collection $accounts): GroupCollectorInterface + { + if ($accounts->count() > 0) { + $accountIds = $accounts->pluck('id')->toArray(); + $this->query->where( + function (EloquentBuilder $query) use ($accountIds) { + $query->whereIn('source.account_id', $accountIds); + $query->orWhereIn('destination.account_id', $accountIds); + } + ); + app('log')->debug(sprintf('GroupCollector: setAccounts: %s', implode(', ', $accountIds))); + $this->accountIds = $accountIds; + } + + return $this; + } + + /** + * Limit the number of returned entries. + * + * @param int $limit + * + * @return GroupCollectorInterface + */ + public function setLimit(int $limit): GroupCollectorInterface + { + $this->limit = $limit; + app('log')->debug(sprintf('GroupCollector: The limit is now %d', $limit)); + + return $this; + } + + /** + * Set the page to get. + * + * @param int $page + * + * @return GroupCollectorInterface + */ + public function setPage(int $page): GroupCollectorInterface + { + $page = 0 === $page ? 0 : $page - 1; + $this->page = $page; + app('log')->debug(sprintf('GroupCollector: page is now %d (is minus 1)', $page)); + + return $this; + } + + /** + * Set the start and end time of the results to return. + * + * @param Carbon $start + * @param Carbon $end + * + * @return GroupCollectorInterface + */ + public function setRange(Carbon $start, Carbon $end): GroupCollectorInterface + { + if ($end < $start) { + [$start, $end] = [$end, $start]; + } + $startStr = $start->format('Y-m-d H:i:s'); + $endStr = $end->format('Y-m-d H:i:s'); + + $this->query->where('transaction_journals.date', '>=', $startStr); + $this->query->where('transaction_journals.date', '<=', $endStr); + app('log')->debug(sprintf('TransactionCollector range is now %s - %s (inclusive)', $startStr, $endStr)); + + return $this; + } + + /** + * Set the user object and start the query. + * + * @param User $user + * + * @return GroupCollectorInterface + */ + public function setUser(User $user): GroupCollectorInterface + { + $this->user = $user; + $this->startQuery(); + + return $this; + } + + /** + * @param Collection $collection + * + * @return Collection + */ + private function parseArray(Collection $collection): Collection + { + $groups = []; + /** @var TransactionGroup $augumentedGroup */ + foreach ($collection as $augumentedGroup) { + $groupId = $augumentedGroup->transaction_group_id; + if (!isset($groups[$groupId])) { + // make new array + $groupArray = [ + 'id' => $augumentedGroup->id, + 'title' => $augumentedGroup->title, + 'count' => 1, + 'sum' => $augumentedGroup->amount, + 'foreign_sum' => $augumentedGroup->foreign_amount ?? '0', + 'transactions' => [$this->parseAugumentedGroup($augumentedGroup)], + ]; + $groups[$groupId] = $groupArray; + continue; + } + $groups[$groupId]['count']++; + $groups[$groupId]['sum'] = bcadd($augumentedGroup->amount, $groups[$groupId]['sum']); + $groups[$groupId]['foreign_sum'] = bcadd($augumentedGroup->foreign_amount ?? '0', $groups[$groupId]['foreign_sum']); + $groups[$groupId]['transactions'][] = $this->parseAugumentedGroup($augumentedGroup); + } + + return new Collection($groups); + } + + /** + * @param TransactionGroup $augumentedGroup + * + * @throws Exception + * @return array + */ + private function parseAugumentedGroup(TransactionGroup $augumentedGroup): array + { + $result = $augumentedGroup->toArray(); + $result['date'] = new Carbon($result['date']); + + return $result; + } + + /** + * Build the query. + */ + private function startQuery(): void + { + app('log')->debug('TransactionCollector::startQuery'); + $this->query = $this->user + ->transactionGroups() + ->leftJoin('transaction_journals', 'transaction_journals.transaction_group_id', 'transaction_groups.id') + // join source transaction. + ->leftJoin( + 'transactions as source', function (JoinClause $join) { + $join->on('source.transaction_journal_id', '=', 'transaction_journals.id') + ->where('source.amount', '<', 0); + } + ) + // join destination transaction + ->leftJoin( + 'transactions as destination', function (JoinClause $join) { + $join->on('destination.transaction_journal_id', '=', 'transaction_journals.id') + ->where('destination.amount', '>', 0); + } + ) + // left join transaction type. + ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') + ->leftJoin('transaction_currencies as currency','currency.id','=','source.transaction_currency_id') + ->leftJoin('transaction_currencies as foreign_currency','foreign_currency.id','=','source.foreign_currency_id') + ->whereNull('transaction_groups.deleted_at') + ->whereNull('transaction_journals.deleted_at') + ->whereNull('source.deleted_at') + ->whereNull('destination.deleted_at') + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->orderBy('transaction_journals.id', 'DESC') + ->orderBy('transaction_journals.description', 'DESC') + ->orderBy('source.amount', 'DESC'); + } +} \ No newline at end of file diff --git a/app/Helpers/Collector/GroupCollectorInterface.php b/app/Helpers/Collector/GroupCollectorInterface.php new file mode 100644 index 0000000000..589033b4ac --- /dev/null +++ b/app/Helpers/Collector/GroupCollectorInterface.php @@ -0,0 +1,88 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Helpers\Collector; + +use Carbon\Carbon; +use FireflyIII\User; +use Illuminate\Support\Collection; + +/** + * Interface GroupCollectorInterface + */ +interface GroupCollectorInterface +{ + /** + * Return the groups. + * + * @return Collection + */ + public function getGroups(): Collection; + + /** + * Define which accounts can be part of the source and destination transactions. + * + * @param Collection $accounts + * + * @return GroupCollectorInterface + */ + public function setAccounts(Collection $accounts): GroupCollectorInterface; + + /** + * Limit the number of returned entries. + * + * @param int $limit + * + * @return GroupCollectorInterface + */ + public function setLimit(int $limit): GroupCollectorInterface; + + /** + * Set the page to get. + * + * @param int $page + * + * @return GroupCollectorInterface + */ + public function setPage(int $page): GroupCollectorInterface; + + /** + * Set the start and end time of the results to return. + * + * @param Carbon $start + * @param Carbon $end + * + * @return GroupCollectorInterface + */ + public function setRange(Carbon $start, Carbon $end): GroupCollectorInterface; + + /** + * Set the user object and start the query. + * + * @param User $user + * + * @return GroupCollectorInterface + */ + public function setUser(User $user): GroupCollectorInterface; + +} \ No newline at end of file diff --git a/app/Helpers/Collector/TransactionCollector.php b/app/Helpers/Collector/TransactionCollector.php index eabf55e30f..e7d4777d42 100644 --- a/app/Helpers/Collector/TransactionCollector.php +++ b/app/Helpers/Collector/TransactionCollector.php @@ -55,6 +55,7 @@ use Log; /** * Class TransactionCollector * + * @deprecated * @codeCoverageIgnore */ class TransactionCollector implements TransactionCollectorInterface @@ -137,6 +138,7 @@ class TransactionCollector implements TransactionCollectorInterface if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); } + throw new FireflyException('The transaction collector is deprecated.'); } /** diff --git a/app/Helpers/Collector/TransactionCollectorInterface.php b/app/Helpers/Collector/TransactionCollectorInterface.php index f5309cff16..f917d0bcb1 100644 --- a/app/Helpers/Collector/TransactionCollectorInterface.php +++ b/app/Helpers/Collector/TransactionCollectorInterface.php @@ -35,7 +35,7 @@ use Illuminate\Support\Collection; /** * Interface TransactionCollectorInterface - * + * @deprecated */ interface TransactionCollectorInterface { diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 703e53aa53..4f9d13c62a 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -24,6 +24,7 @@ namespace FireflyIII\Http\Controllers; use Carbon\Carbon; use FireflyIII\Events\RequestedVersionCheckStatus; +use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Http\Middleware\Installer; use FireflyIII\Models\AccountType; @@ -34,7 +35,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Collection; use Log; - +use Exception; /** * Class HomeController. */ @@ -55,7 +56,7 @@ class HomeController extends Controller * Change index date range. * * @param Request $request - * + * @throws Exception * @return JsonResponse */ public function dateRange(Request $request): JsonResponse @@ -96,7 +97,7 @@ class HomeController extends Controller * Show index. * * @param AccountRepositoryInterface $repository - * + * @throws Exception * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View */ public function index(AccountRepositoryInterface $repository) @@ -127,9 +128,10 @@ class HomeController extends Controller $billCount = $billRepository->getBills()->count(); foreach ($accounts as $account) { - $collector = app(TransactionCollectorInterface::class); + /** @var GroupCollectorInterface $collector */ + $collector = app(GroupCollectorInterface::class); $collector->setAccounts(new Collection([$account]))->setRange($start, $end)->setLimit(10)->setPage(1); - $set = $collector->getTransactions(); + $set = $collector->getGroups(); $transactions[] = [$set, $account]; } diff --git a/app/Models/TransactionGroup.php b/app/Models/TransactionGroup.php index 5cc205d8f0..2dfe932ec6 100644 --- a/app/Models/TransactionGroup.php +++ b/app/Models/TransactionGroup.php @@ -25,7 +25,6 @@ namespace FireflyIII\Models; use FireflyIII\User; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; -use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -34,14 +33,14 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * Class TransactionGroup. * - * @property int $id - * @property \Illuminate\Support\Carbon|null $created_at - * @property \Illuminate\Support\Carbon|null $updated_at - * @property \Illuminate\Support\Carbon|null $deleted_at - * @property int $user_id - * @property string|null $title + * @property int $id + * @property \Illuminate\Support\Carbon|null $created_at + * @property \Illuminate\Support\Carbon|null $updated_at + * @property \Illuminate\Support\Carbon|null $deleted_at + * @property int $user_id + * @property string|null $title * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\TransactionJournal[] $transactionJournals - * @property-read \FireflyIII\User $user + * @property-read \FireflyIII\User $user * @method static bool|null forceDelete() * @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\TransactionGroup newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\TransactionGroup newQuery() @@ -57,6 +56,9 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionGroup withTrashed() * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionGroup withoutTrashed() * @mixin \Eloquent + * @property string amount + * @property string foreign_amount + * @property int transaction_group_id */ class TransactionGroup extends Model { @@ -69,10 +71,12 @@ class TransactionGroup extends Model */ protected $casts = [ + 'id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime', 'title' => 'string', + 'date' => 'datetime', ]; /** @var array Fields that can be filled */ diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index ade39b6866..110ccc2e64 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -61,9 +61,11 @@ use FireflyIII\Support\Navigation; use FireflyIII\Support\Preferences; 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; @@ -97,14 +99,16 @@ 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::addRuntimeLoader(new TransactionLoader); + //Twig::addRuntimeLoader(new AccountLoader); + //Twig::addRuntimeLoader(new TransactionJournalLoader); + //Twig::addRuntimeLoader(new TransactionGroupLoader); Twig::addExtension(new General); - Twig::addExtension(new Journal); + Twig::addExtension(new TransactionGroupTwig); + //Twig::addExtension(new Journal); Twig::addExtension(new Translation); - Twig::addExtension(new Transaction); - Twig::addExtension(new Rule); + //Twig::addExtension(new Transaction); + //Twig::addExtension(new Rule); Twig::addExtension(new AmountFormat); //Twig::addExtension(new Twig_Extension_Debug); } diff --git a/app/Providers/JournalServiceProvider.php b/app/Providers/JournalServiceProvider.php index 89bec86527..8a431f9724 100644 --- a/app/Providers/JournalServiceProvider.php +++ b/app/Providers/JournalServiceProvider.php @@ -22,6 +22,8 @@ declare(strict_types=1); namespace FireflyIII\Providers; +use FireflyIII\Helpers\Collector\GroupCollector; +use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Helpers\Collector\TransactionCollector; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Repositories\Journal\JournalRepository; @@ -49,6 +51,7 @@ class JournalServiceProvider extends ServiceProvider { $this->registerRepository(); $this->registerCollector(); + $this->registerGroupCollector(); } /** @@ -70,6 +73,25 @@ class JournalServiceProvider extends ServiceProvider ); } + /** + * + */ + private function registerGroupCollector(): void + { + $this->app->bind( + GroupCollectorInterface::class, + function (Application $app) { + /** @var GroupCollectorInterface $collector */ + $collector = app(GroupCollector::class); + if ($app->auth->check()) { + $collector->setUser(auth()->user()); + } + + return $collector; + } + ); + } + /** * Register repository. */ diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index d4b574eda7..5906f25503 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -489,7 +489,7 @@ class JournalRepository implements JournalRepositoryInterface */ public function getJournalsWithoutGroup(): array { - return TransactionJournal::whereNotNull('transaction_group_id')->get(['id', 'user_id'])->toArray(); + return TransactionJournal::whereNull('transaction_group_id')->get(['id', 'user_id'])->toArray(); } /** diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 797dce4779..bf887c3eb1 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -154,6 +154,49 @@ class Amount return $result; } + /** + * This method will properly format the given number, in color or "black and white", + * as a currency, given two things: the currency required and the current locale. + * + * @param string $symbol + * @param int $decimalPlaces + * @param string $amount + * @param bool $coloured + * + * @return string + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @noinspection MoreThanThreeArgumentsInspection + */ + public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string + { + $coloured = $coloured ?? true; + $locale = explode(',', (string)trans('config.locale')); + $locale = array_map('trim', $locale); + setlocale(LC_MONETARY, $locale); + $float = round($amount, 12); + $info = localeconv(); + $formatted = number_format($float, $decimalPlaces, $info['mon_decimal_point'], $info['mon_thousands_sep']); + + // some complicated switches to format the amount correctly: + $precedes = $amount < 0 ? $info['n_cs_precedes'] : $info['p_cs_precedes']; + $separated = $amount < 0 ? $info['n_sep_by_space'] : $info['p_sep_by_space']; + $space = true === $separated ? ' ' : ''; + $result = false === $precedes ? $formatted . $space . $symbol : $symbol . $space . $formatted; + + if (true === $coloured) { + if ($amount > 0) { + return sprintf('%s', $result); + } + if ($amount < 0) { + return sprintf('%s', $result); + } + + return sprintf('%s', $result); + } + + return $result; + } + /** * @return Collection */ diff --git a/app/Support/Twig/Extension/TransactionGroupTwig.php b/app/Support/Twig/Extension/TransactionGroupTwig.php new file mode 100644 index 0000000000..c6f773e4c1 --- /dev/null +++ b/app/Support/Twig/Extension/TransactionGroupTwig.php @@ -0,0 +1,192 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Support\Twig\Extension; + +use FireflyIII\Models\TransactionType; +use Twig_Extension; +use Twig_SimpleFunction; + +/** + * Class TransactionGroupTwig + */ +class TransactionGroupTwig extends Twig_Extension +{ + /** + * @return array + */ + public function getFunctions(): array + { + return [ + $this->transactionAmount(), + $this->groupAmount(), + ]; + } + + /** + * @return Twig_SimpleFunction + */ + public function groupAmount(): Twig_SimpleFunction + { + return new Twig_SimpleFunction( + 'groupAmount', + function (array $array): string { + $result = $this->normalGroupAmount($array); + // now append foreign amount, if any. + if (0 !== bccomp('0', $array['foreign_sum'])) { + $foreign = $this->foreignGroupAmount($array); + $result = sprintf('%s (%s)', $result, $foreign); + } + + return $result; + }, + ['is_safe' => ['html']] + ); + } + + /** + * @return Twig_SimpleFunction + */ + public function transactionAmount(): Twig_SimpleFunction + { + return new Twig_SimpleFunction( + 'transactionAmount', + function (array $array): string { + // if is not a withdrawal, amount positive. + $result = $this->normalAmount($array); + // now append foreign amount, if any. + if (null !== $array['foreign_amount']) { + $foreign = $this->foreignAmount($array); + $result = sprintf('%s (%s)', $result, $foreign); + } + + return $result; + }, + ['is_safe' => ['html']] + ); + } + + /** + * Generate foreign amount for transaction from a transaction group. + * + * @param array $array + * + * @return string + */ + private function foreignAmount(array $array): string + { + $type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL; + $amount = $array['foreign_amount'] ?? '0'; + $colored = true; + if ($type !== TransactionType::WITHDRAWAL) { + $amount = bcmul($amount, '-1'); + } + if ($type === TransactionType::TRANSFER) { + $colored = false; + } + $result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored); + if ($type === TransactionType::TRANSFER) { + $result = sprintf('%s', $result); + } + + return $result; + } + + /** + * @param array $array + * + * @return string + */ + private function foreignGroupAmount(array $array): string + { + // take cue from the first entry in the array: + $first = $array['transactions'][0]; + $type = $first['transaction_type_type'] ?? TransactionType::WITHDRAWAL; + $amount = $array['foreign_sum'] ?? '0'; + $colored = true; + if ($type !== TransactionType::WITHDRAWAL) { + $amount = bcmul($amount, '-1'); + } + if ($type === TransactionType::TRANSFER) { + $colored = false; + } + $result = app('amount')->formatFlat($first['foreign_currency_symbol'], (int)$first['foreign_currency_decimal_places'], $amount, $colored); + if ($type === TransactionType::TRANSFER) { + $result = sprintf('%s', $result); + } + + return $result; + } + + /** + * Generate normal amount for transaction from a transaction group. + * + * @param array $array + * + * @return string + */ + private function normalAmount(array $array): string + { + $type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL; + $amount = $array['amount'] ?? '0'; + $colored = true; + if ($type !== TransactionType::WITHDRAWAL) { + $amount = bcmul($amount, '-1'); + } + if ($type === TransactionType::TRANSFER) { + $colored = false; + } + $result = app('amount')->formatFlat($array['currency_symbol'], (int)$array['currency_decimal_places'], $amount, $colored); + if ($type === TransactionType::TRANSFER) { + $result = sprintf('%s', $result); + } + + return $result; + } + + /** + * @param array $array + * + * @return string + */ + private function normalGroupAmount(array $array): string + { + // take cue from the first entry in the array: + $first = $array['transactions'][0]; + $type = $first['transaction_type_type'] ?? TransactionType::WITHDRAWAL; + $amount = $array['sum'] ?? '0'; + $colored = true; + if ($type !== TransactionType::WITHDRAWAL) { + $amount = bcmul($amount, '-1'); + } + if ($type === TransactionType::TRANSFER) { + $colored = false; + } + $result = app('amount')->formatFlat($first['currency_symbol'], (int)$first['currency_decimal_places'], $amount, $colored); + if ($type === TransactionType::TRANSFER) { + $result = sprintf('%s', $result); + } + + return $result; + } +} \ No newline at end of file diff --git a/resources/views/v1/index.twig b/resources/views/v1/index.twig index e8012d06a3..171535298e 100644 --- a/resources/views/v1/index.twig +++ b/resources/views/v1/index.twig @@ -74,7 +74,7 @@ {% if data[0].count > 0 %}
- {% include 'list.journals-tiny' with {'transactions': data[0],'account': data[1]} %} + {% include 'list.groups-tiny' with {'groups': data[0],'account': data[1]} %}
{% else %}
diff --git a/resources/views/v1/list/groups-tiny.twig b/resources/views/v1/list/groups-tiny.twig new file mode 100644 index 0000000000..ff782fb1e0 --- /dev/null +++ b/resources/views/v1/list/groups-tiny.twig @@ -0,0 +1,46 @@ +
+ {% for group in groups %} + + {% for transaction in group.transactions %} + {{ transaction.description }} + + {{ transactionAmount(transaction) }} + +
+ {% endfor %} + {% if group.count > 1 %} +   + + {{ groupAmount(group) }} + + {% endif %} + +
+ + + {# if group do something with format #} + {#{% if group.count > 1 %} + (group) + {% endif %} +#} + {# loop each transaction #} + {# + + {{ transaction|transactionIcon }} + {{ transaction|transactionDescription }} + + {{ transaction|transactionAmount }} + + + + {% endfor %} + #} + + {% endfor %} + +
\ No newline at end of file diff --git a/resources/views/v1/list/journals-tiny.twig b/resources/views/v1/list/journals-tiny.twig index 04fc8b9147..e78b721bcb 100644 --- a/resources/views/v1/list/journals-tiny.twig +++ b/resources/views/v1/list/journals-tiny.twig @@ -1,4 +1,5 @@ -
+

DO NOT USE ME

+{#
{% for transaction in transactions %} {% endfor %}
+#} \ No newline at end of file