mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Some refactoring.
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
use Amount;
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Report\ReportQueryInterface;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI;
|
||||
@@ -84,13 +83,11 @@ class JsonController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ReportQueryInterface $reportQuery
|
||||
*
|
||||
* @param ARI $accountRepository
|
||||
* @param ARI $accountRepository
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function boxIn(ReportQueryInterface $reportQuery, ARI $accountRepository)
|
||||
public function boxIn(ARI $accountRepository)
|
||||
{
|
||||
$start = session('start', Carbon::now()->startOfMonth());
|
||||
$end = session('end', Carbon::now()->endOfMonth());
|
||||
@@ -104,28 +101,23 @@ class JsonController extends Controller
|
||||
return Response::json($cache->get());
|
||||
}
|
||||
$accounts = $accountRepository->getAccountsByType(['Default account', 'Asset account', 'Cash account']);
|
||||
$amount = $reportQuery->income($accounts, $start, $end)->sum('journalAmount');
|
||||
|
||||
$data = ['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
|
||||
$amount = $accountRepository->earnedInPeriod($accounts, $start, $end);
|
||||
$data = ['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
|
||||
$cache->store($data);
|
||||
|
||||
return Response::json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ReportQueryInterface $reportQuery
|
||||
*
|
||||
* @param ARI $accountRepository
|
||||
* @param ARI $accountRepository
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function boxOut(ReportQueryInterface $reportQuery, ARI $accountRepository)
|
||||
public function boxOut(ARI $accountRepository)
|
||||
{
|
||||
$start = session('start', Carbon::now()->startOfMonth());
|
||||
$end = session('end', Carbon::now()->endOfMonth());
|
||||
|
||||
$accounts = $accountRepository->getAccountsByType(['Default account', 'Asset account', 'Cash account']);
|
||||
|
||||
// works for json too!
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($start);
|
||||
@@ -135,7 +127,8 @@ class JsonController extends Controller
|
||||
return Response::json($cache->get());
|
||||
}
|
||||
|
||||
$amount = $reportQuery->expense($accounts, $start, $end)->sum('journalAmount');
|
||||
$accounts = $accountRepository->getAccountsByType(['Default account', 'Asset account', 'Cash account']);
|
||||
$amount = $accountRepository->spentInPeriod($accounts, $start, $end);
|
||||
|
||||
$data = ['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
|
||||
$cache->store($data);
|
||||
|
@@ -16,6 +16,7 @@ use DB;
|
||||
use ExpandedForm;
|
||||
use FireflyIII\Events\TransactionJournalStored;
|
||||
use FireflyIII\Events\TransactionJournalUpdated;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
||||
use FireflyIII\Http\Requests\JournalFormRequest;
|
||||
use FireflyIII\Http\Requests\MassDeleteJournalRequest;
|
||||
@@ -420,11 +421,46 @@ class TransactionController extends Controller
|
||||
}
|
||||
);
|
||||
|
||||
// TODO different for each transaction type!
|
||||
/** @var Collection $transactions */
|
||||
$transactions = $journal->transactions()->groupBy('transactions.account_id')->orderBy('amount', 'ASC')->get(
|
||||
['transactions.*', DB::raw('SUM(`transactions`.`amount`) as `sum`')]
|
||||
);
|
||||
switch ($journal->transactionType->type) {
|
||||
case TransactionType::DEPOSIT:
|
||||
/** @var Collection $transactions */
|
||||
$transactions = $journal->transactions()
|
||||
->groupBy('transactions.account_id')
|
||||
->where('amount', '<', 0)
|
||||
->orderBy('amount', 'ASC')->get(
|
||||
['transactions.*', DB::raw('SUM(`transactions`.`amount`) as `sum`')]
|
||||
);
|
||||
$final = $journal->transactions()
|
||||
->groupBy('transactions.account_id')
|
||||
->where('amount', '>', 0)
|
||||
->orderBy('amount', 'ASC')->first(
|
||||
['transactions.*', DB::raw('SUM(`transactions`.`amount`) as `sum`')]
|
||||
);
|
||||
$final->description = '';
|
||||
$transactions->push($final);
|
||||
break;
|
||||
case TransactionType::WITHDRAWAL:
|
||||
/** @var Collection $transactions */
|
||||
$transactions = $journal->transactions()
|
||||
->groupBy('transactions.account_id')
|
||||
->where('amount', '>', 0)
|
||||
->orderBy('amount', 'ASC')->get(
|
||||
['transactions.*', DB::raw('SUM(`transactions`.`amount`) as `sum`')]
|
||||
);
|
||||
$final = $journal->transactions()
|
||||
->groupBy('transactions.account_id')
|
||||
->where('amount', '<', 0)
|
||||
->orderBy('amount', 'ASC')->first(
|
||||
['transactions.*', DB::raw('SUM(`transactions`.`amount`) as `sum`')]
|
||||
);
|
||||
$final->description = '';
|
||||
$transactions->push($final);
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException('Cannot handle ' . $journal->transactionType->type);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// foreach do balance thing
|
||||
$transactions->each(
|
||||
|
Reference in New Issue
Block a user