Properly show spent and earned. #109

This commit is contained in:
James Cole 2015-09-13 07:40:37 +02:00
parent fa586dba7e
commit 51c7d4fb1b
3 changed files with 86 additions and 2 deletions

View File

@ -65,8 +65,9 @@ class CategoryController extends Controller
while ($start <= $end) {
$currentEnd = Navigation::endOfPeriod($start, $range);
$spent = $repository->balanceInPeriod($category, $start, $currentEnd, true);
$entries->push([clone $start, $spent]);
$spent = $repository->spentInPeriod($category, $start, $currentEnd);
$earned = $repository->earnedInPeriod($category, $start, $currentEnd);
$entries->push([clone $start, $spent, $earned]);
$start = Navigation::addPeriod($start, $range, 0);
}

View File

@ -8,6 +8,7 @@ use Crypt;
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Shared\ComponentRepository;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
/**
@ -262,4 +263,64 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito
return $query->get(['transaction_journals.*'])->sum('correct_amount');
}
/**
* @param Category $category
* @param \Carbon\Carbon $start
* @param \Carbon\Carbon $end
*
* @param bool $shared
*
* @return string
*/
public function spentInPeriod(Category $category, Carbon $start, Carbon $end)
{
$cache = new CacheProperties; // we must cache this.
$cache->addProperty($category->id);
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('spentInPeriod');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$sum = $category->transactionjournals()->transactionTypes(['Withdrawal'])->before($end)->after($start)->get(['transaction_journals.*'])->sum(
'correct_amount'
);
$cache->store($sum);
return $sum;
}
/**
* @param Category $category
* @param \Carbon\Carbon $start
* @param \Carbon\Carbon $end
*
* @param bool $shared
*
* @return string
*/
public function earnedInPeriod(Category $category, Carbon $start, Carbon $end)
{
$cache = new CacheProperties; // we must cache this.
$cache->addProperty($category->id);
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('spentInPeriod');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$sum = $category->transactionjournals()->transactionTypes(['Deposit'])->before($end)->after($start)->get(['transaction_journals.*'])->sum(
'correct_amount'
);
$cache->store($sum);
return $sum;
}
}

View File

@ -97,6 +97,28 @@ interface CategoryRepositoryInterface
*/
public function balanceInPeriod(Category $category, Carbon $start, Carbon $end, $shared = false);
/**
* @param Category $category
* @param \Carbon\Carbon $start
* @param \Carbon\Carbon $end
*
* @param bool $shared
*
* @return string
*/
public function spentInPeriod(Category $category, Carbon $start, Carbon $end);
/**
* @param Category $category
* @param \Carbon\Carbon $start
* @param \Carbon\Carbon $end
*
* @param bool $shared
*
* @return string
*/
public function earnedInPeriod(Category $category, Carbon $start, Carbon $end);
/**
*
* Corrected for tags.