mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-29 14:42:59 +00:00
Fixed some query things.
This commit is contained in:
@@ -20,6 +20,7 @@ use FireflyIII\Models\TransactionJournal;
|
|||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Log;
|
||||||
use Navigation;
|
use Navigation;
|
||||||
use Preferences;
|
use Preferences;
|
||||||
use Response;
|
use Response;
|
||||||
@@ -35,19 +36,14 @@ class BudgetController extends Controller
|
|||||||
/** @var BudgetChartGeneratorInterface */
|
/** @var BudgetChartGeneratorInterface */
|
||||||
protected $generator;
|
protected $generator;
|
||||||
|
|
||||||
/** @var BudgetRepositoryInterface */
|
|
||||||
protected $repository;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* BudgetController constructor.
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
// create chart generator:
|
// create chart generator:
|
||||||
$this->generator = app(BudgetChartGeneratorInterface::class);
|
$this->generator = app(BudgetChartGeneratorInterface::class);
|
||||||
|
|
||||||
$this->repository = app(BudgetRepositoryInterface::class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -143,8 +139,9 @@ class BudgetController extends Controller
|
|||||||
*
|
*
|
||||||
* @return \Symfony\Component\HttpFoundation\Response
|
* @return \Symfony\Component\HttpFoundation\Response
|
||||||
*/
|
*/
|
||||||
public function frontpage()
|
public function frontpage(BudgetRepositoryInterface $repository)
|
||||||
{
|
{
|
||||||
|
Log::debug('Hello');
|
||||||
$start = session('start', Carbon::now()->startOfMonth());
|
$start = session('start', Carbon::now()->startOfMonth());
|
||||||
$end = session('end', Carbon::now()->endOfMonth());
|
$end = session('end', Carbon::now()->endOfMonth());
|
||||||
// chart properties for cache:
|
// chart properties for cache:
|
||||||
@@ -156,8 +153,8 @@ class BudgetController extends Controller
|
|||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get());
|
return Response::json($cache->get());
|
||||||
}
|
}
|
||||||
$budgets = $this->repository->getActiveBudgets();
|
$budgets = $repository->getActiveBudgets();
|
||||||
$repetitions = $this->repository->getAllBudgetLimitRepetitions($start, $end);
|
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
|
||||||
$allEntries = new Collection;
|
$allEntries = new Collection;
|
||||||
|
|
||||||
/** @var Budget $budget */
|
/** @var Budget $budget */
|
||||||
@@ -166,15 +163,15 @@ class BudgetController extends Controller
|
|||||||
$reps = $this->filterRepetitions($repetitions, $budget, $start, $end);
|
$reps = $this->filterRepetitions($repetitions, $budget, $start, $end);
|
||||||
|
|
||||||
if ($reps->count() === 0) {
|
if ($reps->count() === 0) {
|
||||||
$collection = $this->spentInPeriodSingle($budget, $start, $end);
|
$collection = $this->spentInPeriodSingle($repository, $budget, $start, $end);
|
||||||
$allEntries = $allEntries->merge($collection);
|
$allEntries = $allEntries->merge($collection);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$collection = $this->spentInPeriodMulti($budget, $reps);
|
$collection = $this->spentInPeriodMulti($repository, $budget, $reps);
|
||||||
$allEntries = $allEntries->merge($collection);
|
$allEntries = $allEntries->merge($collection);
|
||||||
|
|
||||||
}
|
}
|
||||||
$entry = $this->spentInPeriodWithout($start, $end);
|
$entry = $this->spentInPeriodWithout($repository, $start, $end);
|
||||||
$allEntries->push($entry);
|
$allEntries->push($entry);
|
||||||
$data = $this->generator->frontpage($allEntries);
|
$data = $this->generator->frontpage($allEntries);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
@@ -216,6 +213,7 @@ class BudgetController extends Controller
|
|||||||
if (in_array(strval($repetition->budget_id), $budgetIds)) {
|
if (in_array(strval($repetition->budget_id), $budgetIds)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -292,6 +290,7 @@ class BudgetController extends Controller
|
|||||||
if ($repetition->budget_id === $budget->id && $repetition->startdate == $currentStart) {
|
if ($repetition->budget_id === $budget->id && $repetition->startdate == $currentStart) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -329,28 +328,31 @@ class BudgetController extends Controller
|
|||||||
if ($repetition->startdate < $end && $repetition->enddate > $start && $repetition->budget_id === $budget->id) {
|
if ($repetition->startdate < $end && $repetition->enddate > $start && $repetition->budget_id === $budget->id) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param BudgetRepositoryInterface $repository
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
* @param Collection $repetitions
|
* @param Collection $repetitions
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
private function spentInPeriodMulti(Budget $budget, Collection $repetitions): Collection
|
private function spentInPeriodMulti(BudgetRepositoryInterface $repository, Budget $budget, Collection $repetitions): Collection
|
||||||
{
|
{
|
||||||
$format = strval(trans('config.month_and_day'));
|
$format = strval(trans('config.month_and_day'));
|
||||||
$collection = new Collection;
|
$collection = new Collection;
|
||||||
$name = $budget->name;
|
$name = $budget->name;
|
||||||
/** @var LimitRepetition $repetition */
|
/** @var LimitRepetition $repetition */
|
||||||
foreach ($repetitions as $repetition) {
|
foreach ($repetitions as $repetition) {
|
||||||
$expenses = $this->repository->spentInPeriod(new Collection([$budget]), new Collection, $repetition->startdate, $repetition->enddate);
|
$expenses = $repository->spentInPeriod(new Collection([$budget]), new Collection, $repetition->startdate, $repetition->enddate);
|
||||||
|
|
||||||
if ($repetitions->count() > 1) {
|
if ($repetitions->count() > 1) {
|
||||||
$name = $budget->name . ' ' . trans('firefly.between_dates',
|
$name = $budget->name . ' ' . trans(
|
||||||
|
'firefly.between_dates',
|
||||||
['start' => $repetition->startdate->formatLocalized($format), 'end' => $repetition->enddate->formatLocalized($format)]
|
['start' => $repetition->startdate->formatLocalized($format), 'end' => $repetition->enddate->formatLocalized($format)]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -366,18 +368,19 @@ class BudgetController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param BudgetRepositoryInterface $repository
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
private function spentInPeriodSingle(Budget $budget, Carbon $start, Carbon $end): Collection
|
private function spentInPeriodSingle(BudgetRepositoryInterface $repository, Budget $budget, Carbon $start, Carbon $end): Collection
|
||||||
{
|
{
|
||||||
$collection = new Collection;
|
$collection = new Collection;
|
||||||
$amount = '0';
|
$amount = '0';
|
||||||
$left = '0';
|
$left = '0';
|
||||||
$spent = $this->repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
|
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
|
||||||
$overspent = '0';
|
$overspent = '0';
|
||||||
$array = [$budget->name, $left, $spent, $overspent, $amount, $spent];
|
$array = [$budget->name, $left, $spent, $overspent, $amount, $spent];
|
||||||
$collection->push($array);
|
$collection->push($array);
|
||||||
@@ -386,14 +389,15 @@ class BudgetController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param BudgetRepositoryInterface $repository
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function spentInPeriodWithout(Carbon $start, Carbon $end):array
|
private function spentInPeriodWithout(BudgetRepositoryInterface $repository, Carbon $start, Carbon $end):array
|
||||||
{
|
{
|
||||||
$list = $this->repository->journalsInPeriodWithoutBudget(new Collection, $start, $end);
|
$list = $repository->journalsInPeriodWithoutBudget(new Collection, $start, $end);
|
||||||
$sum = '0';
|
$sum = '0';
|
||||||
/** @var TransactionJournal $entry */
|
/** @var TransactionJournal $entry */
|
||||||
foreach ($list as $entry) {
|
foreach ($list as $entry) {
|
||||||
|
|||||||
@@ -358,8 +358,29 @@ class TransactionJournal extends TransactionJournalSupport
|
|||||||
// left join transaction currency:
|
// left join transaction currency:
|
||||||
$query->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id');
|
$query->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id');
|
||||||
|
|
||||||
// left join destination (for amount and account info).
|
// extend group by:
|
||||||
$query->groupBy('transaction_journals.id');
|
$query->groupBy(
|
||||||
|
[
|
||||||
|
'transaction_journals.id',
|
||||||
|
'transaction_journals.created_at',
|
||||||
|
'transaction_journals.updated_at',
|
||||||
|
'transaction_journals.deleted_at',
|
||||||
|
'transaction_journals.user_id',
|
||||||
|
'transaction_journals.transaction_type_id',
|
||||||
|
'transaction_journals.bill_id',
|
||||||
|
'transaction_journals.transaction_currency_id',
|
||||||
|
'transaction_journals.description',
|
||||||
|
'transaction_journals.date',
|
||||||
|
'transaction_journals.interest_date',
|
||||||
|
'transaction_journals.book_date',
|
||||||
|
'transaction_journals.process_date',
|
||||||
|
'transaction_journals.order',
|
||||||
|
'transaction_journals.tag_count',
|
||||||
|
'transaction_journals.encrypted',
|
||||||
|
'transaction_journals.completed',
|
||||||
|
'transaction_types.type',
|
||||||
|
'transaction_currencies.code',
|
||||||
|
]);
|
||||||
$query->with(['categories', 'budgets', 'attachments', 'bill', 'transactions']);
|
$query->with(['categories', 'budgets', 'attachments', 'bill', 'transactions']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -430,6 +430,12 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
$fields[] = 'source.amount as source_amount';
|
$fields[] = 'source.amount as source_amount';
|
||||||
$fields[] = 'destination.account_id as destination_account_id';
|
$fields[] = 'destination.account_id as destination_account_id';
|
||||||
$fields[] = 'destination.amount as destination_amount';
|
$fields[] = 'destination.amount as destination_amount';
|
||||||
|
|
||||||
|
$query->groupBy(['source.account_id',
|
||||||
|
'source.amount',
|
||||||
|
'destination.account_id',
|
||||||
|
'destination.amount',]);
|
||||||
|
|
||||||
$complete = $query->get($fields);
|
$complete = $query->get($fields);
|
||||||
|
|
||||||
return $complete;
|
return $complete;
|
||||||
|
|||||||
Reference in New Issue
Block a user