. */ declare(strict_types=1); namespace FireflyIII\Http\Controllers\Json; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Support\SingleCacheProperties; use Illuminate\Http\Request; use Response; class TransactionController extends Controller { public function amounts(Request $request, JournalRepositoryInterface $repository) { $ids = $request->get('transactions'); $cache = new SingleCacheProperties; $cache->addProperty('json-reconcile-amounts'); $cache->addProperty($ids); if ($cache->has()) { return Response::json($cache->get()); } $totals = []; // for each transaction, get amount(s) foreach ($ids as $transactionId) { $transaction = $repository->findTransaction(intval($transactionId)); $transactionType = $transaction->transactionJournal->transactionType->type; // default amount: $currencyId = $transaction->transaction_currency_id; if (!isset($totals[$currencyId])) { $totals[$currencyId] = [ 'amount' => '0', 'currency' => $transaction->transactionCurrency, 'type' => $transactionType, ]; } // add default amount: $totals[$currencyId]['amount'] = bcadd($totals[$currencyId]['amount'], app('steam')->positive($transaction->amount)); // foreign amount: if (null !== $transaction->foreign_amount) { $currencyId = $transaction->foreign_currency_id; if (!isset($totals[$currencyId])) { $totals[$currencyId] = [ 'amount' => '0', 'currency' => $transaction->foreignCurrency, 'type' => $transactionType, ]; } // add foreign amount: $totals[$currencyId]['amount'] = bcadd($totals[$currencyId]['amount'], app('steam')->positive($transaction->foreign_amount)); } } $entries = []; foreach ($totals as $entry) { $amount = $entry['amount']; if (TransactionType::WITHDRAWAL === $entry['type']) { $amount = bcmul($entry['amount'], '-1'); } $entries[] = app('amount')->formatAnything($entry['currency'], $amount, false); } $result = ['amounts' => join(' / ', $entries)]; $cache->store($result); return Response::json($result); } }