diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index 790d2fd822..211862c542 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -151,10 +151,11 @@ class CategoryController extends Controller $page = intval(Input::get('page')); $set = $repository->getJournals($category, $page); $count = $repository->countJournals($category); + $totalSum = $repository->journalsSum($category); $journals = new LengthAwarePaginator($set, $count, 50, $page); $journals->setPath('categories/show/' . $category->id); - return view('categories.show', compact('category', 'journals', 'hideCategory')); + return view('categories.show', compact('category', 'journals', 'hideCategory', 'totalSum')); } /** diff --git a/app/Models/Category.php b/app/Models/Category.php index 7792eba5f0..a1c65c4667 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -25,7 +25,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Category whereUserId($value) * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Category whereEncrypted($value) * @property-read float $spent - * @property-read \Carbon\Carbon $lastActivity + * @property \Carbon\Carbon $lastActivity */ class Category extends Model { diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index 09d7369dec..5e8a4be655 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -57,6 +57,7 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito return $set; } + /** * * @param Carbon $start @@ -64,7 +65,7 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito * * @return array */ - public function getCategoriesAndExpensesCorrected($start, $end) + public function getCategoriesAndExpensesCorrected(Carbon $start, Carbon $end) { $set = Auth::user()->transactionjournals() ->leftJoin( @@ -233,4 +234,32 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito return $category; } + + /** + * This method returns the sum of the journals in the category, optionally + * limited by a start or end date. + * + * @param Category $category + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function journalsSum(Category $category, Carbon $start = null, Carbon $end = null) + { + $query = $category->transactionJournals() + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->orderBy('transaction_journals.id', 'DESC'); + if (!is_null($start)) { + $query->after($start); + } + + if (!is_null($end)) { + $query->before($end); + } + + return $query->get(['transaction_journals.*'])->sum('correct_amount'); + + } } diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php index 4c1b631fd5..b7364e518b 100644 --- a/app/Repositories/Category/CategoryRepositoryInterface.php +++ b/app/Repositories/Category/CategoryRepositoryInterface.php @@ -40,7 +40,7 @@ interface CategoryRepositoryInterface * * @return array */ - public function getCategoriesAndExpensesCorrected($start, $end); + public function getCategoriesAndExpensesCorrected(Carbon $start, Carbon $end); /** * @param Category $category @@ -57,6 +57,18 @@ interface CategoryRepositoryInterface */ public function getJournals(Category $category, $page); + /** + * This method returns the sum of the journals in the category, optionally + * limited by a start or end date. + * + * @param Category $category + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function journalsSum(Category $category, Carbon $start = null, Carbon $end = null); + /** * @param Category $category * diff --git a/resources/lang/en/firefly.php b/resources/lang/en/firefly.php index fdf5d0f408..2de14d960b 100644 --- a/resources/lang/en/firefly.php +++ b/resources/lang/en/firefly.php @@ -15,6 +15,7 @@ return [ 'cancel' => 'Cancel', 'from' => 'From', 'to' => 'To', + 'total_sum' => 'Total sum', 'showEverything' => 'Show everything', 'never' => 'Never', 'search_results_for' => 'Search results for ":query"', diff --git a/resources/lang/nl/firefly.php b/resources/lang/nl/firefly.php index 8dc336e68c..390f563579 100644 --- a/resources/lang/nl/firefly.php +++ b/resources/lang/nl/firefly.php @@ -15,6 +15,7 @@ return [ 'cancel' => 'Annuleren', 'from' => 'Van', 'to' => 'Tot', + 'total_sum' => 'Totale som', 'showEverything' => 'Laat alles zien', 'never' => 'Nooit', 'search_results_for' => 'Zoekresultaten voor ":query"', diff --git a/resources/twig/categories/show.twig b/resources/twig/categories/show.twig index 771c5d56b8..f5cece5b89 100644 --- a/resources/twig/categories/show.twig +++ b/resources/twig/categories/show.twig @@ -45,7 +45,7 @@

{{ 'transactions'|_ }}

- {% include 'list/journals' %} + {% include 'list/journals' with {showPageSum: true,showTotalSum: true} %}
diff --git a/resources/twig/list/journals.twig b/resources/twig/list/journals.twig index f517ecec8c..84e3a92d01 100644 --- a/resources/twig/list/journals.twig +++ b/resources/twig/list/journals.twig @@ -1,6 +1,7 @@ {{ journals.render|raw }} - +
+ @@ -23,6 +24,9 @@ {% endif %} + + + {% set _sum = 0 %} {% for journal in journals %} {% if invalidJournal(journal) %} @@ -36,6 +40,7 @@ {% else %} + {% set _sum = _sum + journal.correct_amount %} {% endif %} - {% endfor %} + + + {% if showPageSum %} + + + + + {% endif %} + {% if showTotalSum %} + + + + + {% endif %} + +
{{ trans('list.description') }}
Invalid journal: Found {{ journal.transactions|length }} transaction(s)
{{ 'sum'|_ }}{{ _sum|formatAmount }}
{{ 'total_sum'|_ }}{{ totalSum|formatAmount }}
{{ journals.render|raw }}