2015-05-16 13:53:08 +02:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* Category.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
2016-02-05 12:08:25 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-16 19:08:54 +02:00
|
|
|
*
|
2015-05-16 14:51:23 +02:00
|
|
|
* Class Category
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers\Collection
|
|
|
|
*/
|
|
|
|
class Category
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @var Collection */
|
|
|
|
protected $categories;
|
2015-05-24 08:00:40 +02:00
|
|
|
/** @var string */
|
|
|
|
protected $total = '0';
|
2015-05-16 14:51:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->categories = new Collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CategoryModel $category
|
|
|
|
*/
|
|
|
|
public function addCategory(CategoryModel $category)
|
|
|
|
{
|
2015-08-02 07:04:43 +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);
|
2015-12-11 18:15:37 +01:00
|
|
|
$this->addTotal($category->spent);
|
2015-05-16 14:51:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-05 09:25:15 +01:00
|
|
|
* @param string $add
|
2015-05-16 14:51:23 +02:00
|
|
|
*/
|
2016-02-05 09:25:15 +01:00
|
|
|
public function addTotal(string $add)
|
2015-05-16 14:51:23 +02:00
|
|
|
{
|
2016-03-19 16:51:52 +01:00
|
|
|
$add = strval(round($add, 2));
|
2015-05-24 08:00:40 +02:00
|
|
|
$this->total = bcadd($this->total, $add);
|
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(
|
2015-06-06 23:09:12 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2015-05-24 08:00:40 +02:00
|
|
|
return strval(round($this->total, 2));
|
2015-05-16 14:51:23 +02:00
|
|
|
}
|
2015-05-16 13:53:08 +02:00
|
|
|
|
|
|
|
|
2015-05-20 19:56:14 +02:00
|
|
|
}
|