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

86 lines
1.7 KiB
PHP
Raw Normal View History

2015-05-16 13:06:38 +02:00
<?php
namespace FireflyIII\Helpers\Collection;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
use stdClass;
/**
2015-05-16 19:08:54 +02:00
* @codeCoverageIgnore
2015-05-17 09:04:55 +02:00
*
2015-05-16 13:06:38 +02:00
* Class Income
*
* @package FireflyIII\Helpers\Collection
*/
class Income
{
/** @var Collection */
protected $incomes;
/** @var float */
protected $total;
/**
*
*/
public function __construct()
{
$this->incomes = new Collection;
}
/**
* @param TransactionJournal $entry
*/
public function addOrCreateIncome(TransactionJournal $entry)
{
2015-05-17 10:30:18 +02:00
$accountId = $entry->account_id;
if (!$this->incomes->has($accountId)) {
2015-05-16 13:06:38 +02:00
$newObject = new stdClass;
2015-05-20 07:20:02 +02:00
$newObject->amount = floatval($entry->amount);
2015-05-16 13:06:38 +02:00
$newObject->name = $entry->name;
$newObject->count = 1;
2015-05-17 10:30:18 +02:00
$newObject->id = $accountId;
$this->incomes->put($accountId, $newObject);
2015-05-16 13:06:38 +02:00
} else {
2015-05-17 10:30:18 +02:00
$existing = $this->incomes->get($accountId);
2015-05-20 07:20:02 +02:00
$existing->amount += floatval($entry->amount);
2015-05-16 13:06:38 +02:00
$existing->count++;
2015-05-17 10:30:18 +02:00
$this->incomes->put($accountId, $existing);
2015-05-16 13:06:38 +02:00
}
}
/**
* @param $add
*/
public function addToTotal($add)
{
$this->total += floatval($add);
}
/**
* @return Collection
*/
public function getIncomes()
{
$this->incomes->sortByDesc(
function (stdClass $object) {
return $object->amount;
}
);
return $this->incomes;
}
/**
* @return float
*/
public function getTotal()
{
return $this->total;
}
2015-05-20 19:56:14 +02:00
}