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

102 lines
2.2 KiB
PHP
Raw Normal View History

2015-05-16 13:06:38 +02:00
<?php
/**
* Income.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:06:38 +02:00
namespace FireflyIII\Helpers\Collection;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
use stdClass;
/**
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 */
2016-02-05 09:25:15 +01:00
protected $total = '0';
2015-05-16 13:06:38 +02:00
/**
*
*/
public function __construct()
{
$this->incomes = new Collection;
}
/**
* @param TransactionJournal $entry
*/
public function addOrCreateIncome(TransactionJournal $entry)
{
2016-05-14 13:51:33 +02:00
// add each account individually:
$sources = TransactionJournal::sourceTransactionList($entry);
2016-05-14 13:51:33 +02:00
foreach ($sources as $transaction) {
$amount = strval($transaction->amount);
$account = $transaction->account;
$amount = bcmul($amount, '-1');
2016-05-14 13:51:33 +02:00
$object = new stdClass;
$object->amount = $amount;
$object->name = $account->name;
$object->count = 1;
$object->id = $account->id;
// overrule some properties:
if ($this->incomes->has($account->id)) {
$object = $this->incomes->get($account->id);
$object->amount = bcadd($object->amount, $amount);
$object->count++;
}
$this->incomes->put($account->id, $object);
2015-05-16 13:06:38 +02:00
}
2016-05-14 13:51:33 +02:00
2015-05-16 13:06:38 +02:00
}
/**
2016-02-05 09:25:15 +01:00
* @param string $add
2015-05-16 13:06:38 +02:00
*/
2016-02-05 09:25:15 +01:00
public function addToTotal(string $add)
2015-05-16 13:06:38 +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 13:06:38 +02:00
}
/**
* @return Collection
*/
2016-02-18 10:04:26 +01:00
public function getIncomes(): Collection
2015-05-16 13:06:38 +02:00
{
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
*/
2016-02-18 10:04:26 +01:00
public function getTotal(): string
2015-05-16 13:06:38 +02:00
{
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
}