firefly-iii/app/Helpers/Collection/Expense.php

86 lines
1.7 KiB
PHP
Raw Normal View History

2015-05-16 06:06:38 -05:00
<?php
/**
* Expense.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-05-16 06:06:38 -05:00
namespace FireflyIII\Helpers\Collection;
use Illuminate\Support\Collection;
use stdClass;
/**
2015-05-17 02:04:55 -05:00
*
2015-05-16 06:06:38 -05:00
* Class Expense
*
* @package FireflyIII\Helpers\Collection
*/
class Expense
{
/** @var Collection */
protected $expenses;
2015-05-24 01:00:40 -05:00
/** @var string */
protected $total = '0';
2015-05-16 06:06:38 -05:00
/**
*
*/
public function __construct()
{
$this->expenses = new Collection;
}
/**
* @param stdClass $entry
2015-05-16 06:06:38 -05:00
*/
public function addOrCreateExpense(stdClass $entry)
2015-05-16 06:06:38 -05:00
{
$this->expenses->put($entry->id, $entry);
2015-05-16 06:06:38 -05:00
}
/**
2016-02-05 02:25:15 -06:00
* @param string $add
2015-05-16 06:06:38 -05:00
*/
2016-02-05 02:25:15 -06:00
public function addToTotal(string $add)
2015-05-16 06:06:38 -05:00
{
2015-12-12 13:16:30 -06:00
$add = strval(round($add, 2));
2015-12-12 13:20:18 -06:00
if (bccomp('0', $add) === -1) {
2015-12-12 13:16:30 -06:00
$add = bcmul($add, '-1');
}
// if amount is positive, the original transaction
// was a transfer. But since this is an expense report,
// that amount must be negative.
2015-05-24 01:00:40 -05:00
$this->total = bcadd($this->total, $add);
2015-05-16 06:06:38 -05:00
}
/**
* @return Collection
*/
2016-02-18 03:04:26 -06:00
public function getExpenses(): Collection
2015-05-16 06:06:38 -05:00
{
2015-12-11 11:31:15 -06:00
$set = $this->expenses->sortBy(
2015-06-06 16:09:12 -05:00
function (stdClass $object) {
2015-05-16 06:06:38 -05:00
return $object->amount;
}
);
2015-06-16 23:11:35 -05:00
return $set;
2015-05-16 06:06:38 -05:00
}
/**
2015-05-24 01:00:40 -05:00
* @return string
2015-05-16 06:06:38 -05:00
*/
2016-02-18 03:04:26 -06:00
public function getTotal(): string
2015-05-16 06:06:38 -05:00
{
2015-05-24 01:00:40 -05:00
return strval(round($this->total, 2));
2015-05-16 06:06:38 -05:00
}
2015-05-20 12:56:14 -05:00
}