Fix for transactions.

This commit is contained in:
James Cole 2016-04-30 22:30:11 +02:00
parent 3c1ff4d21f
commit 80350f8423
2 changed files with 59 additions and 56 deletions

View File

@ -270,6 +270,62 @@ class CategoryController extends Controller
}
/**
* @param Category $category
* @param string $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*/
public function period(Category $category, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($reportType);
$cache->addProperty($accounts);
$cache->addProperty($category->id);
$cache->addProperty('category');
$cache->addProperty('period');
if ($cache->has()) {
return Response::json($cache->get());
}
/** @var SingleCategoryRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
// loop over period, add by users range:
$current = clone $start;
$viewRange = Preferences::get('viewRange', '1M')->data;
$format = strval(trans('config.month'));
$set = new Collection;
while ($current < $end) {
$currentStart = clone $current;
$currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
$spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd, $accounts)));
$earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd, $accounts)));
$entry = [
$category->name,
$currentStart->formatLocalized($format),
$spent,
$earned,
];
$set->push($entry);
$currentEnd->addDay();
$current = clone $currentEnd;
}
$data = $this->generator->period($set);
$cache->store($data);
return Response::json($data);
}
/**
* @param SCRI $repository
* @param Category $category
@ -435,60 +491,4 @@ class CategoryController extends Controller
return $data;
}
/**
* @param Category $category
* @param string $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*/
public function period(Category $category, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($reportType);
$cache->addProperty($accounts);
$cache->addProperty($category->id);
$cache->addProperty('category');
$cache->addProperty('period');
if ($cache->has()) {
return Response::json($cache->get());
}
/** @var SingleCategoryRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
// loop over period, add by users range:
$current = clone $start;
$viewRange = Preferences::get('viewRange', '1M')->data;
$format = strval(trans('config.month'));
$set = new Collection;
while ($current < $end) {
$currentStart = clone $current;
$currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
$spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd, $accounts)));
$earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd, $accounts)));
$entry = [
$category->name,
$currentStart->formatLocalized($format),
$spent,
$earned,
];
$set->push($entry);
$currentEnd->addDay();
$current = clone $currentEnd;
}
$data = $this->generator->period($set);
$cache->store($data);
return Response::json($data);
}
}

View File

@ -284,7 +284,9 @@ class CategoryRepository implements CategoryRepositoryInterface
->before($end)
->after($start)
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->having('transaction_count', '=', 1)
->transactionTypes($types);
if (count($accountIds) > 0) {
$query->whereIn('transactions.account_id', $accountIds);
}
@ -293,6 +295,7 @@ class CategoryRepository implements CategoryRepositoryInterface
$single = $query->first(
[
DB::raw('SUM(`transactions`.`amount`) as `sum`'),
DB::raw('COUNT(`transactions`.`id`) as `transaction_count`'),
]
);
if (!is_null($single)) {