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

90 lines
1.9 KiB
PHP
Raw Normal View History

2015-05-16 13:06:38 +02:00
<?php
namespace FireflyIII\Helpers\Collection;
2015-12-31 20:12:49 +01:00
use Crypt;
2015-05-16 13:06:38 +02:00
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;
2015-05-26 20:18:21 +02:00
/** @var string */
2015-05-16 13:06:38 +02:00
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-12-31 20:12:49 +01:00
$newObject->amount = strval(round($entry->journalAmount, 2));
$newObject->name = Crypt::decrypt($entry->account_name);
2015-05-16 13:06:38 +02:00
$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-24 08:00:40 +02:00
bcscale(2);
2015-05-24 11:41:52 +02:00
$existing = $this->incomes->get($accountId);
2015-12-31 20:12:49 +01:00
$existing->amount = bcadd($existing->amount, $entry->journalAmount);
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)
{
2015-05-24 08:00:40 +02:00
$add = strval(round($add, 2));
bcscale(2);
$this->total = bcadd($this->total, $add);
2015-05-16 13:06:38 +02:00
}
/**
* @return Collection
*/
public function getIncomes()
{
2015-06-17 06:11:35 +02:00
$set = $this->incomes->sortByDesc(
2015-06-06 23:09:12 +02:00
function (stdClass $object) {
2015-05-16 13:06:38 +02:00
return $object->amount;
}
);
2015-06-17 06:11:35 +02:00
return $set;
2015-05-16 13:06:38 +02:00
}
/**
* @return string
2015-05-16 13:06:38 +02:00
*/
public function getTotal()
{
2015-05-24 08:00:40 +02:00
return strval(round($this->total, 2));
2015-05-16 13:06:38 +02:00
}
2015-05-20 19:56:14 +02:00
}