Files
firefly-iii/app/Helpers/Collection/Category.php

98 lines
2.2 KiB
PHP
Raw Normal View History

2015-05-16 13:53:08 +02:00
<?php
/**
* Category.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-05-16 13:53:08 +02:00
namespace FireflyIII\Helpers\Collection;
2015-05-16 14:51:23 +02:00
use FireflyIII\Models\Category as CategoryModel;
use Illuminate\Support\Collection;
/**
2017-11-15 12:25:49 +01:00
* Class Category.
2018-08-25 20:45:42 +02:00
*
* @codeCoverageIgnore
2015-05-16 14:51:23 +02:00
*/
class Category
{
/** @var Collection The categories */
2015-05-16 14:51:23 +02:00
protected $categories;
/** @var string Total amount */
2015-05-24 08:00:40 +02:00
protected $total = '0';
2015-05-16 14:51:23 +02:00
/**
* Category constructor.
2015-05-16 14:51:23 +02:00
*/
public function __construct()
{
$this->categories = new Collection;
}
/**
* Add a category.
*
2015-05-16 14:51:23 +02:00
* @param CategoryModel $category
*/
2018-07-07 21:17:46 +02:00
public function addCategory(CategoryModel $category): void
2015-05-16 14:51:23 +02:00
{
// spent is minus zero for an expense report:
if ($category->spent < 0) {
2015-05-16 14:51:23 +02:00
$this->categories->push($category);
2018-07-07 21:17:46 +02:00
$this->addTotal((string)$category->spent);
2015-05-16 14:51:23 +02:00
}
}
/**
* Add to the total amount.
*
2016-02-05 09:25:15 +01:00
* @param string $add
2015-05-16 14:51:23 +02:00
*/
2018-07-07 21:17:46 +02:00
public function addTotal(string $add): void
2015-05-16 14:51:23 +02:00
{
2015-05-24 08:00:40 +02:00
$this->total = bcadd($this->total, $add);
2015-05-16 14:51:23 +02:00
}
/**
* Get all categories.
*
2015-05-16 14:51:23 +02:00
* @return Collection
*/
2016-02-18 10:04:26 +01:00
public function getCategories(): Collection
2015-05-16 14:51:23 +02:00
{
2015-12-11 18:05:07 +01:00
$set = $this->categories->sortBy(
2019-08-02 05:25:24 +02:00
static function (CategoryModel $category) {
2015-05-16 14:51:23 +02:00
return $category->spent;
}
);
2015-06-17 06:11:35 +02:00
return $set;
2015-05-16 14:51:23 +02:00
}
/**
* Get the total.
*
2015-05-24 08:00:40 +02:00
* @return string
2015-05-16 14:51:23 +02:00
*/
2016-02-18 10:04:26 +01:00
public function getTotal(): string
2015-05-16 14:51:23 +02:00
{
2016-12-30 13:45:02 +01:00
return $this->total;
2015-05-16 14:51:23 +02:00
}
2015-05-20 19:56:14 +02:00
}