Fix more charts.

This commit is contained in:
James Cole 2016-05-09 20:15:26 +02:00
parent a90d095609
commit cda6cfb4cd
4 changed files with 303 additions and 257 deletions

View File

@ -8,10 +8,10 @@ use Carbon\Carbon;
use FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface; use FireflyIII\Generator\Chart\Category\CategoryChartGeneratorInterface;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Category; use FireflyIII\Models\Category;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI; use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI;
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;
@ -52,29 +52,26 @@ class CategoryController extends Controller
*/ */
public function all(CRI $repository, Category $category) public function all(CRI $repository, Category $category)
{ {
/** $start = $repository->firstUseDate($category, new Collection);
// oldest transaction in category:
$start = $repository->getFirstActivityDate($category);
$range = Preferences::get('viewRange', '1M')->data; $range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($start, $range); $start = Navigation::startOfPeriod($start, $range);
$categoryCollection = new Collection([$category]);
$end = new Carbon; $end = new Carbon;
$entries = new Collection; $entries = new Collection;
// chart properties for cache: $cache = new CacheProperties;
$cache = new CacheProperties();
$cache->addProperty($start); $cache->addProperty($start);
$cache->addProperty($end); $cache->addProperty($end);
$cache->addProperty('all'); $cache->addProperty('all');
$cache->addProperty('categories'); $cache->addProperty('categories');
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); //return Response::json($cache->get());
} }
$spentArray = $repository->spentPerDay($category, $start, $end, new Collection);
$earnedArray = $repository->earnedPerDay($category, $start, $end, new Collection);
while ($start <= $end) { while ($start <= $end) {
$currentEnd = Navigation::endOfPeriod($start, $range); $currentEnd = Navigation::endOfPeriod($start, $range);
$spent = $this->getSumOfRange($start, $currentEnd, $spentArray); Log::debug('Searching for expenses from ' . $start . ' to ' . $currentEnd);
$earned = $this->getSumOfRange($start, $currentEnd, $earnedArray); $spent = $repository->spentInPeriod($categoryCollection, new Collection, $start, $currentEnd);
$earned = $repository->earnedInPeriod($categoryCollection, new Collection, $start, $currentEnd);
$date = Navigation::periodShow($start, $range); $date = Navigation::periodShow($start, $range);
$entries->push([clone $start, $date, $spent, $earned]); $entries->push([clone $start, $date, $spent, $earned]);
$start = Navigation::addPeriod($start, $range, 0); $start = Navigation::addPeriod($start, $range, 0);
@ -85,8 +82,10 @@ class CategoryController extends Controller
$data = $this->generator->all($entries); $data = $this->generator->all($entries);
$cache->store($data); $cache->store($data);
return ' ' . json_encode($data);
return Response::json($data); return Response::json($data);
* **/
} }
/** /**
@ -97,13 +96,11 @@ class CategoryController extends Controller
*/ */
public function currentPeriod(CRI $repository, Category $category) public function currentPeriod(CRI $repository, Category $category)
{ {
/**
$start = clone session('start', Carbon::now()->startOfMonth()); $start = clone session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth()); $end = session('end', Carbon::now()->endOfMonth());
$data = $this->makePeriodChart($repository, $category, $start, $end); $data = $this->makePeriodChart($repository, $category, $start, $end);
return Response::json($data); return Response::json($data);
* **/
} }
/** /**
@ -121,28 +118,28 @@ class CategoryController extends Controller
public function earnedInPeriod(CRI $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts) public function earnedInPeriod(CRI $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{ {
/** /**
$cache = new CacheProperties; // chart properties for cache: * $cache = new CacheProperties; // chart properties for cache:
$cache->addProperty($start); * $cache->addProperty($start);
$cache->addProperty($end); * $cache->addProperty($end);
$cache->addProperty($reportType); * $cache->addProperty($reportType);
$cache->addProperty($accounts); * $cache->addProperty($accounts);
$cache->addProperty('category'); * $cache->addProperty('category');
$cache->addProperty('earned-in-period'); * $cache->addProperty('earned-in-period');
if ($cache->has()) { * if ($cache->has()) {
return Response::json($cache->get()); * return Response::json($cache->get());
} * }
*
$set = $repository->earnedForAccountsPerMonth($accounts, $start, $end); * $set = $repository->earnedForAccountsPerMonth($accounts, $start, $end);
$categories = $set->unique('id')->sortBy( * $categories = $set->unique('id')->sortBy(
function (Category $category) { * function (Category $category) {
return $category->name; * return $category->name;
} * }
); * );
$entries = $this->filterCollection($start, $end, $set, $categories); * $entries = $this->filterCollection($start, $end, $set, $categories);
$data = $this->generator->earnedInPeriod($categories, $entries); * $data = $this->generator->earnedInPeriod($categories, $entries);
$cache->store($data); * $cache->store($data);
*
return $data; * return $data;
**/ **/
} }
@ -151,16 +148,12 @@ class CategoryController extends Controller
* *
* @param CRI $repository * @param CRI $repository
* *
* @param ARI $accountRepository
*
* @return \Symfony\Component\HttpFoundation\Response * @return \Symfony\Component\HttpFoundation\Response
*/ */
public function frontpage(CRI $repository, ARI $accountRepository) public function frontpage(CRI $repository)
{ {
/**
$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:
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty($start); $cache->addProperty($start);
@ -168,18 +161,23 @@ class CategoryController extends Controller
$cache->addProperty('category'); $cache->addProperty('category');
$cache->addProperty('frontpage'); $cache->addProperty('frontpage');
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); //return Response::json($cache->get());
}
$categories = $repository->getCategories();
$set = new Collection;
/** @var Category $category */
foreach ($categories as $category) {
$spent = $repository->spentInPeriod(new Collection([$category]), new Collection, $start, $end);
Log::debug('Spent for ' . $category->name . ' is ' . $spent . ' (' . bccomp($spent, '0') . ')');
if (bccomp($spent, '0') === -1) {
$category->spent = $spent;
$set->push($category);
}
} }
// get data for categories (and "no category"):
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
$set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
$outside = $repository->sumSpentNoCategory($accounts, $start, $end);
// this is a "fake" entry for the "no category" entry. // this is a "fake" entry for the "no category" entry.
$entry = new stdClass(); $entry = new stdClass;
$entry->name = trans('firefly.no_category'); $entry->name = trans('firefly.no_category');
$entry->spent = $outside; $entry->spent = $repository->spentInPeriodWithoutCategory(new Collection, $start, $end);
$set->push($entry); $set->push($entry);
$set = $set->sortBy('spent'); $set = $set->sortBy('spent');
@ -187,7 +185,7 @@ class CategoryController extends Controller
$cache->store($data); $cache->store($data);
return Response::json($data); return Response::json($data);
**/
} }
/** /**
@ -202,76 +200,76 @@ class CategoryController extends Controller
public function multiYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories) public function multiYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
{ {
/** /**
// /** @var CRI $repository * // /** @var CRI $repository
// $repository = app(CRI::class); * // $repository = app(CRI::class);
*
// chart properties for cache: * // chart properties for cache:
$cache = new CacheProperties(); * $cache = new CacheProperties();
$cache->addProperty($reportType); * $cache->addProperty($reportType);
$cache->addProperty($start); * $cache->addProperty($start);
$cache->addProperty($end); * $cache->addProperty($end);
$cache->addProperty($accounts); * $cache->addProperty($accounts);
$cache->addProperty($categories); * $cache->addProperty($categories);
$cache->addProperty('multiYearCategory'); * $cache->addProperty('multiYearCategory');
*
if ($cache->has()) { * if ($cache->has()) {
return Response::json($cache->get()); * return Response::json($cache->get());
} * }
*
$entries = new Collection; * $entries = new Collection;
$set = $repository->listMultiYear($categories, $accounts, $start, $end); * $set = $repository->listMultiYear($categories, $accounts, $start, $end);
*
/** @var Category $category * /** @var Category $category
foreach ($categories as $category) { * foreach ($categories as $category) {
$entry = ['name' => '', 'spent' => [], 'earned' => []]; * $entry = ['name' => '', 'spent' => [], 'earned' => []];
*
$currentStart = clone $start; * $currentStart = clone $start;
while ($currentStart < $end) { * while ($currentStart < $end) {
// fix the date: * // fix the date:
$year = $currentStart->year; * $year = $currentStart->year;
$currentEnd = clone $currentStart; * $currentEnd = clone $currentStart;
$currentEnd->endOfYear(); * $currentEnd->endOfYear();
*
*
// get data: * // get data:
if (is_null($category->id)) { * if (is_null($category->id)) {
$name = trans('firefly.noCategory'); * $name = trans('firefly.noCategory');
$spent = $repository->sumSpentNoCategory($accounts, $currentStart, $currentEnd); * $spent = $repository->sumSpentNoCategory($accounts, $currentStart, $currentEnd);
$earned = $repository->sumEarnedNoCategory($accounts, $currentStart, $currentEnd); * $earned = $repository->sumEarnedNoCategory($accounts, $currentStart, $currentEnd);
} else { * } else {
// get from set: * // get from set:
$entrySpent = $set->filter( * $entrySpent = $set->filter(
function (Category $cat) use ($year, $category) { * function (Category $cat) use ($year, $category) {
return ($cat->type == 'Withdrawal' && $cat->dateFormatted == $year && $cat->id == $category->id); * return ($cat->type == 'Withdrawal' && $cat->dateFormatted == $year && $cat->id == $category->id);
} * }
)->first(); * )->first();
$entryEarned = $set->filter( * $entryEarned = $set->filter(
function (Category $cat) use ($year, $category) { * function (Category $cat) use ($year, $category) {
return ($cat->type == 'Deposit' && $cat->dateFormatted == $year && $cat->id == $category->id); * return ($cat->type == 'Deposit' && $cat->dateFormatted == $year && $cat->id == $category->id);
} * }
)->first(); * )->first();
*
$name = $category->name; * $name = $category->name;
$spent = !is_null($entrySpent) ? $entrySpent->sum : 0; * $spent = !is_null($entrySpent) ? $entrySpent->sum : 0;
$earned = !is_null($entryEarned) ? $entryEarned->sum : 0; * $earned = !is_null($entryEarned) ? $entryEarned->sum : 0;
} * }
*
// save to array: * // save to array:
$entry['name'] = $name; * $entry['name'] = $name;
$entry['spent'][$year] = ($spent * -1); * $entry['spent'][$year] = ($spent * -1);
$entry['earned'][$year] = $earned; * $entry['earned'][$year] = $earned;
*
// jump to next year. * // jump to next year.
$currentStart = clone $currentEnd; * $currentStart = clone $currentEnd;
$currentStart->addDay(); * $currentStart->addDay();
} * }
$entries->push($entry); * $entries->push($entry);
} * }
// generate chart with data: * // generate chart with data:
$data = $this->generator->multiYear($entries); * $data = $this->generator->multiYear($entries);
$cache->store($data); * $cache->store($data);
*
return Response::json($data); * return Response::json($data);
* *
*/ */
@ -289,48 +287,48 @@ class CategoryController extends Controller
public function period(Category $category, string $reportType, Carbon $start, Carbon $end, Collection $accounts) public function period(Category $category, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{ {
/** /**
// chart properties for cache: * // chart properties for cache:
$cache = new CacheProperties(); * $cache = new CacheProperties();
$cache->addProperty($start); * $cache->addProperty($start);
$cache->addProperty($end); * $cache->addProperty($end);
$cache->addProperty($reportType); * $cache->addProperty($reportType);
$cache->addProperty($accounts); * $cache->addProperty($accounts);
$cache->addProperty($category->id); * $cache->addProperty($category->id);
$cache->addProperty('category'); * $cache->addProperty('category');
$cache->addProperty('period'); * $cache->addProperty('period');
if ($cache->has()) { * if ($cache->has()) {
return Response::json($cache->get()); * return Response::json($cache->get());
} * }
*
/** @var CategoryRepositoryInterface $repository * /** @var CategoryRepositoryInterface $repository
$repository = app(CategoryRepositoryInterface::class); * $repository = app(CategoryRepositoryInterface::class);
// loop over period, add by users range: * // loop over period, add by users range:
$current = clone $start; * $current = clone $start;
$viewRange = Preferences::get('viewRange', '1M')->data; * $viewRange = Preferences::get('viewRange', '1M')->data;
$format = strval(trans('config.month')); * $format = strval(trans('config.month'));
$set = new Collection; * $set = new Collection;
while ($current < $end) { * while ($current < $end) {
$currentStart = clone $current; * $currentStart = clone $current;
$currentEnd = Navigation::endOfPeriod($currentStart, $viewRange); * $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
*
$spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd, $accounts))); * $spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd, $accounts)));
$earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd, $accounts))); * $earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd, $accounts)));
*
$entry = [ * $entry = [
$category->name, * $category->name,
$currentStart->formatLocalized($format), * $currentStart->formatLocalized($format),
$spent, * $spent,
$earned, * $earned,
*
]; * ];
$set->push($entry); * $set->push($entry);
$currentEnd->addDay(); * $currentEnd->addDay();
$current = clone $currentEnd; * $current = clone $currentEnd;
} * }
$data = $this->generator->period($set); * $data = $this->generator->period($set);
$cache->store($data); * $cache->store($data);
*
return Response::json($data); * return Response::json($data);
* **/ * **/
} }
@ -345,7 +343,6 @@ class CategoryController extends Controller
*/ */
public function specificPeriod(CRI $repository, Category $category, $date) public function specificPeriod(CRI $repository, Category $category, $date)
{ {
/**
$carbon = new Carbon($date); $carbon = new Carbon($date);
$range = Preferences::get('viewRange', '1M')->data; $range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod($carbon, $range); $start = Navigation::startOfPeriod($carbon, $range);
@ -353,8 +350,6 @@ class CategoryController extends Controller
$data = $this->makePeriodChart($repository, $category, $start, $end); $data = $this->makePeriodChart($repository, $category, $start, $end);
return Response::json($data); return Response::json($data);
**/
} }
/** /**
@ -373,30 +368,30 @@ class CategoryController extends Controller
public function spentInPeriod(CRI $repository, $reportType, Carbon $start, Carbon $end, Collection $accounts) public function spentInPeriod(CRI $repository, $reportType, Carbon $start, Carbon $end, Collection $accounts)
{ {
/** /**
$cache = new CacheProperties; // chart properties for cache: * $cache = new CacheProperties; // chart properties for cache:
$cache->addProperty($start); * $cache->addProperty($start);
$cache->addProperty($end); * $cache->addProperty($end);
$cache->addProperty($reportType); * $cache->addProperty($reportType);
$cache->addProperty($accounts); * $cache->addProperty($accounts);
$cache->addProperty('category'); * $cache->addProperty('category');
$cache->addProperty('spent-in-period'); * $cache->addProperty('spent-in-period');
if ($cache->has()) { * if ($cache->has()) {
return Response::json($cache->get()); * return Response::json($cache->get());
} * }
*
*
$set = $repository->spentForAccountsPerMonth($accounts, $start, $end); * $set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
$categories = $set->unique('id')->sortBy( * $categories = $set->unique('id')->sortBy(
function (Category $category) { * function (Category $category) {
return $category->name; * return $category->name;
} * }
); * );
$entries = $this->filterCollection($start, $end, $set, $categories); * $entries = $this->filterCollection($start, $end, $set, $categories);
$entries = $this->invertSelection($entries); * $entries = $this->invertSelection($entries);
$data = $this->generator->spentInPeriod($categories, $entries); * $data = $this->generator->spentInPeriod($categories, $entries);
$cache->store($data); * $cache->store($data);
*
return $data; * return $data;
* */ * */
} }
@ -411,33 +406,33 @@ class CategoryController extends Controller
private function filterCollection(Carbon $start, Carbon $end, Collection $set, Collection $categories): Collection private function filterCollection(Carbon $start, Carbon $end, Collection $set, Collection $categories): Collection
{ {
/** /**
$entries = new Collection; * $entries = new Collection;
*
while ($start < $end) { // filter the set: * while ($start < $end) { // filter the set:
$row = [clone $start]; * $row = [clone $start];
$currentSet = $set->filter( // get possibly relevant entries from the big $set * $currentSet = $set->filter( // get possibly relevant entries from the big $set
function (Category $category) use ($start) { * function (Category $category) use ($start) {
return $category->dateFormatted == $start->format('Y-m'); * return $category->dateFormatted == $start->format('Y-m');
} * }
); * );
/** @var Category $category * /** @var Category $category
foreach ($categories as $category) { // check for each category if its in the current set. * foreach ($categories as $category) { // check for each category if its in the current set.
$entry = $currentSet->filter( // if its in there, use the value. * $entry = $currentSet->filter( // if its in there, use the value.
function (Category $cat) use ($category) { * function (Category $cat) use ($category) {
return ($cat->id == $category->id); * return ($cat->id == $category->id);
} * }
)->first(); * )->first();
if (!is_null($entry)) { * if (!is_null($entry)) {
$row[] = $entry->earned ? round($entry->earned, 2) : round($entry->spent, 2); * $row[] = $entry->earned ? round($entry->earned, 2) : round($entry->spent, 2);
} else { * } else {
$row[] = 0; * $row[] = 0;
} * }
} * }
$entries->push($row); * $entries->push($row);
$start->addMonth(); * $start->addMonth();
} * }
*
return $entries; * return $entries;
* */ * */
} }
@ -451,17 +446,17 @@ class CategoryController extends Controller
private function invertSelection(Collection $entries): Collection private function invertSelection(Collection $entries): Collection
{ {
/** /**
$result = new Collection; * $result = new Collection;
foreach ($entries as $entry) { * foreach ($entries as $entry) {
$new = [$entry[0]]; * $new = [$entry[0]];
$count = count($entry); * $count = count($entry);
for ($i = 1; $i < $count; $i++) { * for ($i = 1; $i < $count; $i++) {
$new[$i] = ($entry[$i] * -1); * $new[$i] = ($entry[$i] * -1);
} * }
$result->push($new); * $result->push($new);
} * }
*
return $result; * return $result;
* **/ * **/
} }
@ -476,27 +471,24 @@ class CategoryController extends Controller
*/ */
private function makePeriodChart(CRI $repository, Category $category, Carbon $start, Carbon $end) private function makePeriodChart(CRI $repository, Category $category, Carbon $start, Carbon $end)
{ {
/** $categoryCollection = new Collection([$category]);
// chart properties for cache:
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty($start); $cache->addProperty($start);
$cache->addProperty($end); $cache->addProperty($end);
$cache->addProperty($category->id); $cache->addProperty($category->id);
$cache->addProperty('specific-period'); $cache->addProperty('specific-period');
if ($cache->has()) { if ($cache->has()) {
return $cache->get(); // // return $cache->get();
} }
$entries = new Collection; $entries = new Collection;
Log::debug('Start is ' . $start . ' en end is ' . $end);
// get amount earned in period, grouped by day.
// get amount spent in period, grouped by day.
$spentArray = $repository->spentPerDay($category, $start, $end, new Collection);
$earnedArray = $repository->earnedPerDay($category, $start, $end, new Collection);
while ($start <= $end) { while ($start <= $end) {
$str = $start->format('Y-m-d'); Log::debug('Now at ' . $start);
$spent = $spentArray[$str] ?? '0'; $spent = $repository->spentInPeriod($categoryCollection, new Collection, $start, $start);
$earned = $earnedArray[$str] ?? '0'; Log::debug('spent: ' . $spent);
$earned = $repository->earnedInPeriod($categoryCollection, new Collection, $start, $start);
Log::debug('earned: ' . $earned);
$date = Navigation::periodShow($start, '1D'); $date = Navigation::periodShow($start, '1D');
$entries->push([clone $start, $date, $spent, $earned]); $entries->push([clone $start, $date, $spent, $earned]);
$start->addDay(); $start->addDay();
@ -506,6 +498,26 @@ class CategoryController extends Controller
$cache->store($data); $cache->store($data);
return $data; return $data;
/**
* // get amount earned in period, grouped by day.
* // get amount spent in period, grouped by day.
* $spentArray = $repository->spentPerDay($category, $start, $end, new Collection);
* $earnedArray = $repository->earnedPerDay($category, $start, $end, new Collection);
*
* while ($start <= $end) {
* $str = $start->format('Y-m-d');
* $spent = $spentArray[$str] ?? '0';
* $earned = $earnedArray[$str] ?? '0';
* $date = Navigation::periodShow($start, '1D');
* $entries->push([clone $start, $date, $spent, $earned]);
* $start->addDay();
* }
*
* $data = $this->generator->period($entries);
* $cache->store($data);
*
* return $data;
*/ */
} }

View File

@ -20,7 +20,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property-read \Illuminate\Database\Eloquent\Collection|TransactionJournal[] $transactionjournals * @property-read \Illuminate\Database\Eloquent\Collection|TransactionJournal[] $transactionjournals
* @property-read \FireflyIII\User $user * @property-read \FireflyIII\User $user
* @property string $dateFormatted * @property string $dateFormatted
* @property float $spent * @property string $spent
* @property \Carbon\Carbon $lastActivity * @property \Carbon\Carbon $lastActivity
* @property string $type * @property string $type
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Category whereId($value) * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Category whereId($value)

View File

@ -378,7 +378,7 @@ class CategoryRepository implements CategoryRepositoryInterface
*/ */
public function earnedInPeriod(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): string public function earnedInPeriod(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): string
{ {
$types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER]; $types = [TransactionType::DEPOSIT, TransactionType::TRANSFER];
$journals = $this->journalsInPeriod($categories, $accounts, $types, $start, $end); $journals = $this->journalsInPeriod($categories, $accounts, $types, $start, $end);
$sum = '0'; $sum = '0';
foreach ($journals as $journal) { foreach ($journals as $journal) {
@ -652,6 +652,13 @@ class CategoryRepository implements CategoryRepositoryInterface
$complete = $complete->merge($first); $complete = $complete->merge($first);
$complete = $complete->merge($second); $complete = $complete->merge($second);
// sort:
$complete = $complete->sortByDesc(
function (TransactionJournal $journal) {
return $journal->date->format('Ymd');
}
);
// create paginator // create paginator
$offset = ($page - 1) * $pageSize; $offset = ($page - 1) * $pageSize;
$subSet = $complete->slice($offset, $pageSize); $subSet = $complete->slice($offset, $pageSize);
@ -677,7 +684,7 @@ class CategoryRepository implements CategoryRepositoryInterface
// first collect actual transaction journals (fairly easy) // first collect actual transaction journals (fairly easy)
$query = $this->user->transactionjournals()->expanded(); $query = $this->user->transactionjournals()->expanded();
if ($end > $start) { if ($end >= $start) {
$query->before($end)->after($start); $query->before($end)->after($start);
} }
@ -833,7 +840,7 @@ class CategoryRepository implements CategoryRepositoryInterface
*/ */
public function spentInPeriod(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): string public function spentInPeriod(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): string
{ {
$types = [TransactionType::DEPOSIT, TransactionType::TRANSFER]; $types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
$journals = $this->journalsInPeriod($categories, $accounts, $types, $start, $end); $journals = $this->journalsInPeriod($categories, $accounts, $types, $start, $end);
$sum = '0'; $sum = '0';
foreach ($journals as $journal) { foreach ($journals as $journal) {
@ -843,6 +850,24 @@ class CategoryRepository implements CategoryRepositoryInterface
return $sum; return $sum;
} }
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentInPeriodWithoutCategory(Collection $accounts, Carbon $start, Carbon $end) : string
{
$journals = $this->journalsInPeriodWithoutCategory($accounts, $start, $end);
$sum = '0';
foreach ($journals as $journal) {
$sum = bcadd(TransactionJournal::amount($journal), $sum);
}
return $sum;
}
/** /**
* @param array $data * @param array $data
* *

View File

@ -188,6 +188,15 @@ interface CategoryRepositoryInterface
*/ */
public function journalsInPeriod(Collection $categories, Collection $accounts, array $types, Carbon $start, Carbon $end): Collection; public function journalsInPeriod(Collection $categories, Collection $accounts, array $types, Carbon $start, Carbon $end): Collection;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentInPeriodWithoutCategory(Collection $accounts, Carbon $start, Carbon $end) : string;
/** /**
* @param Collection $accounts * @param Collection $accounts
* @param Carbon $start * @param Carbon $start