mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-21 10:41:22 +00:00
Optimised two charts, cleaned up some code.
This commit is contained in:
@@ -244,16 +244,21 @@ class CategoryController extends Controller
|
|||||||
$cache->addProperty($end);
|
$cache->addProperty($end);
|
||||||
$cache->addProperty($category->id);
|
$cache->addProperty($category->id);
|
||||||
$cache->addProperty('category');
|
$cache->addProperty('category');
|
||||||
$cache->addProperty('currentPeriod');
|
$cache->addProperty('current-period');
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
$entries = new Collection;
|
$entries = new Collection;
|
||||||
|
|
||||||
|
// get amount earned in period, grouped by day.
|
||||||
|
$spentArray = $repository->spentPerDay($category, $start, $end);
|
||||||
|
$earnedArray = $repository->earnedPerDay($category, $start, $end);
|
||||||
|
// get amount spent in period, grouped by day.
|
||||||
|
|
||||||
while ($start <= $end) {
|
while ($start <= $end) {
|
||||||
$spent = $repository->spentOnDaySum($category, $start);
|
$str = $start->format('Y-m-d');
|
||||||
$earned = $repository->earnedOnDaySum($category, $start);
|
$spent = isset($spentArray[$str]) ? $spentArray[$str] : 0;
|
||||||
|
$earned = isset($earnedArray[$str]) ? $earnedArray[$str] : 0;
|
||||||
$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();
|
||||||
@@ -263,8 +268,6 @@ class CategoryController extends Controller
|
|||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -295,12 +298,17 @@ class CategoryController extends Controller
|
|||||||
}
|
}
|
||||||
$entries = new Collection;
|
$entries = new Collection;
|
||||||
|
|
||||||
|
// get amount earned in period, grouped by day.
|
||||||
|
$spentArray = $repository->spentPerDay($category, $start, $end);
|
||||||
|
$earnedArray = $repository->earnedPerDay($category, $start, $end);
|
||||||
|
// get amount spent in period, grouped by day.
|
||||||
|
|
||||||
while ($start <= $end) {
|
while ($start <= $end) {
|
||||||
$spent = $repository->spentOnDaySum($category, $start);
|
$str = $start->format('Y-m-d');
|
||||||
$earned = $repository->earnedOnDaySum($category, $start);
|
$spent = isset($spentArray[$str]) ? $spentArray[$str] : 0;
|
||||||
$theDate = Navigation::periodShow($start, '1D');
|
$earned = isset($earnedArray[$str]) ? $earnedArray[$str] : 0;
|
||||||
$entries->push([clone $start, $theDate, $spent, $earned]);
|
$date = Navigation::periodShow($start, '1D');
|
||||||
|
$entries->push([clone $start, $date, $spent, $earned]);
|
||||||
$start->addDay();
|
$start->addDay();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,18 +84,6 @@ interface CategoryRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end);
|
public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the total amount of money related to transactions without any category connected to
|
|
||||||
* it. Returns either the spent amount.
|
|
||||||
*
|
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the total amount of money related to transactions without any category connected to
|
* Returns the total amount of money related to transactions without any category connected to
|
||||||
* it. Returns either the earned amount.
|
* it. Returns either the earned amount.
|
||||||
@@ -108,4 +96,16 @@ interface CategoryRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end);
|
public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the total amount of money related to transactions without any category connected to
|
||||||
|
* it. Returns either the spent amount.
|
||||||
|
*
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,13 @@
|
|||||||
|
|
||||||
namespace FireflyIII\Repositories\Category;
|
namespace FireflyIII\Repositories\Category;
|
||||||
|
|
||||||
|
use Auth;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use DB;
|
||||||
use FireflyIII\Models\Category;
|
use FireflyIII\Models\Category;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Repositories\Shared\ComponentRepository;
|
use FireflyIII\Repositories\Shared\ComponentRepository;
|
||||||
use FireflyIII\Support\CacheProperties;
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,6 +70,10 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* TODO this method is not optimal, and should be replaced.
|
||||||
|
*
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param \Carbon\Carbon $start
|
* @param \Carbon\Carbon $start
|
||||||
* @param \Carbon\Carbon $end
|
* @param \Carbon\Carbon $end
|
||||||
@@ -77,67 +82,47 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
|||||||
*/
|
*/
|
||||||
public function earnedInPeriod(Category $category, Carbon $start, Carbon $end)
|
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('earnedInPeriod');
|
|
||||||
|
|
||||||
if ($cache->has()) {
|
|
||||||
return $cache->get(); // @codeCoverageIgnore
|
|
||||||
}
|
|
||||||
|
|
||||||
$sum = $category->transactionjournals()->transactionTypes([TransactionType::DEPOSIT])->before($end)->after($start)->get(['transaction_journals.*'])
|
$sum = $category->transactionjournals()->transactionTypes([TransactionType::DEPOSIT])->before($end)->after($start)->get(['transaction_journals.*'])
|
||||||
->sum(
|
->sum(
|
||||||
'amount'
|
'amount'
|
||||||
);
|
);
|
||||||
|
|
||||||
$cache->store($sum);
|
|
||||||
|
|
||||||
return $sum;
|
return $sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate how much is earned in this period.
|
* Returns an array with the following key:value pairs:
|
||||||
*
|
*
|
||||||
* @param Category $category
|
* yyyy-mm-dd:<amount>
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
*
|
||||||
* @return string
|
* Where yyyy-mm-dd is the date and <amount> is the money earned using DEPOSITS in the $category
|
||||||
*/
|
* from all the users $accounts.
|
||||||
public function earnedInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end)
|
|
||||||
{
|
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
|
||||||
$sum
|
|
||||||
= $category
|
|
||||||
->transactionjournals()
|
|
||||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
||||||
->before($end)
|
|
||||||
->whereIn('transactions.account_id', $accountIds)
|
|
||||||
->transactionTypes([TransactionType::DEPOSIT])
|
|
||||||
->after($start)
|
|
||||||
->get(['transaction_journals.*'])
|
|
||||||
->sum('amount');
|
|
||||||
|
|
||||||
return $sum;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Corrected for tags.
|
|
||||||
*
|
*
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param Carbon $date
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return float
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function earnedOnDaySum(Category $category, Carbon $date)
|
public function earnedPerDay(Category $category, Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
return $category->transactionjournals()->transactionTypes([TransactionType::DEPOSIT])->onDate($date)->get(['transaction_journals.*'])->sum('amount');
|
/** @var Collection $query */
|
||||||
|
$query = Auth::user()->transactionJournals()
|
||||||
|
->transactionTypes([TransactionType::DEPOSIT])
|
||||||
|
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
|
->where('transactions.amount', '>', 0)
|
||||||
|
->before($end)
|
||||||
|
->after($start)
|
||||||
|
->where('category_transaction_journal.category_id', $category->id)
|
||||||
|
->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||||
|
|
||||||
|
$return = [];
|
||||||
|
foreach ($query->toArray() as $entry) {
|
||||||
|
$return[$entry['dateFormatted']] = $entry['sum'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,37 +206,11 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates how much is spent in this period.
|
* TODO this method is not optimal, and should be replaced.
|
||||||
*
|
*
|
||||||
* @param Category $category
|
* @deprecated
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
*
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function spentInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end)
|
|
||||||
{
|
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
|
||||||
|
|
||||||
$sum
|
|
||||||
= $category
|
|
||||||
->transactionjournals()
|
|
||||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
||||||
->after($start)
|
|
||||||
->before($end)
|
|
||||||
->whereIn('transactions.account_id', $accountIds)
|
|
||||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
|
||||||
->get(['transaction_journals.*'])
|
|
||||||
->sum('amount');
|
|
||||||
|
|
||||||
return $sum;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param \Carbon\Carbon $start
|
* @param \Carbon\Carbon $start
|
||||||
* @param \Carbon\Carbon $end
|
* @param \Carbon\Carbon $end
|
||||||
@@ -260,38 +219,48 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
|||||||
*/
|
*/
|
||||||
public function spentInPeriod(Category $category, Carbon $start, Carbon $end)
|
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([TransactionType::WITHDRAWAL])->before($end)->after($start)->get(['transaction_journals.*'])
|
$sum = $category->transactionjournals()->transactionTypes([TransactionType::WITHDRAWAL])->before($end)->after($start)->get(['transaction_journals.*'])
|
||||||
->sum(
|
->sum(
|
||||||
'amount'
|
'amount'
|
||||||
);
|
);
|
||||||
|
|
||||||
$cache->store($sum);
|
|
||||||
|
|
||||||
return $sum;
|
return $sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Corrected for tags
|
* Returns an array with the following key:value pairs:
|
||||||
|
*
|
||||||
|
* yyyy-mm-dd:<amount>
|
||||||
|
*
|
||||||
|
* Where yyyy-mm-dd is the date and <amount> is the money spent using DEPOSITS in the $category
|
||||||
|
* from all the users accounts.
|
||||||
*
|
*
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param Carbon $date
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return string
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function spentOnDaySum(Category $category, Carbon $date)
|
public function spentPerDay(Category $category, Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
return $category->transactionjournals()->transactionTypes([TransactionType::WITHDRAWAL])->onDate($date)->get(['transaction_journals.*'])->sum('amount');
|
/** @var Collection $query */
|
||||||
|
$query = Auth::user()->transactionJournals()
|
||||||
|
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||||
|
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
|
->where('transactions.amount', '<', 0)
|
||||||
|
->before($end)
|
||||||
|
->after($start)
|
||||||
|
->where('category_transaction_journal.category_id', $category->id)
|
||||||
|
->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||||
|
|
||||||
|
$return = [];
|
||||||
|
foreach ($query->toArray() as $entry) {
|
||||||
|
$return[$entry['dateFormatted']] = $entry['sum'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -50,6 +50,8 @@ interface SingleCategoryRepositoryInterface
|
|||||||
public function destroy(Category $category);
|
public function destroy(Category $category);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param \Carbon\Carbon $start
|
* @param \Carbon\Carbon $start
|
||||||
* @param \Carbon\Carbon $end
|
* @param \Carbon\Carbon $end
|
||||||
@@ -58,29 +60,22 @@ interface SingleCategoryRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function earnedInPeriod(Category $category, Carbon $start, Carbon $end);
|
public function earnedInPeriod(Category $category, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate how much is earned in this period.
|
* Returns an array with the following key:value pairs:
|
||||||
*
|
*
|
||||||
* @param Category $category
|
* yyyy-mm-dd:<amount>
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
*
|
||||||
* @return string
|
* Where yyyy-mm-dd is the date and <amount> is the money earned using DEPOSITS in the $category
|
||||||
*/
|
* from all the users accounts.
|
||||||
public function earnedInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end);
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Corrected for tags.
|
|
||||||
*
|
*
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param Carbon $date
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return float
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function earnedOnDaySum(Category $category, Carbon $date);
|
public function earnedPerDay(Category $category, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
@@ -117,6 +112,8 @@ interface SingleCategoryRepositoryInterface
|
|||||||
public function getLatestActivity(Category $category);
|
public function getLatestActivity(Category $category);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param \Carbon\Carbon $start
|
* @param \Carbon\Carbon $start
|
||||||
* @param \Carbon\Carbon $end
|
* @param \Carbon\Carbon $end
|
||||||
@@ -126,27 +123,21 @@ interface SingleCategoryRepositoryInterface
|
|||||||
public function spentInPeriod(Category $category, Carbon $start, Carbon $end);
|
public function spentInPeriod(Category $category, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates how much is spent in this period.
|
* Returns an array with the following key:value pairs:
|
||||||
*
|
*
|
||||||
* @param Category $category
|
* yyyy-mm-dd:<amount>
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
*
|
||||||
* @return string
|
* Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $category
|
||||||
*/
|
* from all the users accounts.
|
||||||
public function spentInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end);
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Corrected for tags.
|
|
||||||
*
|
*
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param Carbon $date
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return float
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function spentOnDaySum(Category $category, Carbon $date);
|
public function spentPerDay(Category $category, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
|
|||||||
Reference in New Issue
Block a user