Fixed some query things.

This commit is contained in:
James Cole 2016-09-16 09:09:54 +02:00
parent bd53410c71
commit fccdf56c5d
3 changed files with 59 additions and 28 deletions

View File

@ -20,6 +20,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
use Log;
use Navigation;
use Preferences;
use Response;
@ -35,19 +36,14 @@ class BudgetController extends Controller
/** @var BudgetChartGeneratorInterface */
protected $generator;
/** @var BudgetRepositoryInterface */
protected $repository;
/**
*
* BudgetController constructor.
*/
public function __construct()
{
parent::__construct();
// create chart generator:
$this->generator = app(BudgetChartGeneratorInterface::class);
$this->repository = app(BudgetRepositoryInterface::class);
}
/**
@ -143,8 +139,9 @@ class BudgetController extends Controller
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function frontpage()
public function frontpage(BudgetRepositoryInterface $repository)
{
Log::debug('Hello');
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
// chart properties for cache:
@ -156,8 +153,8 @@ class BudgetController extends Controller
if ($cache->has()) {
return Response::json($cache->get());
}
$budgets = $this->repository->getActiveBudgets();
$repetitions = $this->repository->getAllBudgetLimitRepetitions($start, $end);
$budgets = $repository->getActiveBudgets();
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
$allEntries = new Collection;
/** @var Budget $budget */
@ -166,15 +163,15 @@ class BudgetController extends Controller
$reps = $this->filterRepetitions($repetitions, $budget, $start, $end);
if ($reps->count() === 0) {
$collection = $this->spentInPeriodSingle($budget, $start, $end);
$collection = $this->spentInPeriodSingle($repository, $budget, $start, $end);
$allEntries = $allEntries->merge($collection);
continue;
}
$collection = $this->spentInPeriodMulti($budget, $reps);
$collection = $this->spentInPeriodMulti($repository, $budget, $reps);
$allEntries = $allEntries->merge($collection);
}
$entry = $this->spentInPeriodWithout($start, $end);
$entry = $this->spentInPeriodWithout($repository, $start, $end);
$allEntries->push($entry);
$data = $this->generator->frontpage($allEntries);
$cache->store($data);
@ -216,6 +213,7 @@ class BudgetController extends Controller
if (in_array(strval($repetition->budget_id), $budgetIds)) {
return true;
}
return false;
}
);
@ -292,6 +290,7 @@ class BudgetController extends Controller
if ($repetition->budget_id === $budget->id && $repetition->startdate == $currentStart) {
return true;
}
return false;
}
);
@ -329,28 +328,31 @@ class BudgetController extends Controller
if ($repetition->startdate < $end && $repetition->enddate > $start && $repetition->budget_id === $budget->id) {
return true;
}
return false;
}
);
}
/**
* @param Budget $budget
* @param Collection $repetitions
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
* @param Collection $repetitions
*
* @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'));
$collection = new Collection;
$name = $budget->name;
/** @var LimitRepetition $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) {
$name = $budget->name . ' ' . trans('firefly.between_dates',
$name = $budget->name . ' ' . trans(
'firefly.between_dates',
['start' => $repetition->startdate->formatLocalized($format), 'end' => $repetition->enddate->formatLocalized($format)]
);
}
@ -366,18 +368,19 @@ class BudgetController extends Controller
}
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @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;
$amount = '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';
$array = [$budget->name, $left, $spent, $overspent, $amount, $spent];
$collection->push($array);
@ -386,14 +389,15 @@ class BudgetController extends Controller
}
/**
* @param Carbon $start
* @param Carbon $end
* @param BudgetRepositoryInterface $repository
* @param Carbon $start
* @param Carbon $end
*
* @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';
/** @var TransactionJournal $entry */
foreach ($list as $entry) {

View File

@ -358,8 +358,29 @@ class TransactionJournal extends TransactionJournalSupport
// left join transaction currency:
$query->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id');
// left join destination (for amount and account info).
$query->groupBy('transaction_journals.id');
// extend group by:
$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']);
}

View File

@ -430,6 +430,12 @@ class AccountRepository implements AccountRepositoryInterface
$fields[] = 'source.amount as source_amount';
$fields[] = 'destination.account_id as destination_account_id';
$fields[] = 'destination.amount as destination_amount';
$query->groupBy(['source.account_id',
'source.amount',
'destination.account_id',
'destination.amount',]);
$complete = $query->get($fields);
return $complete;