2015-05-16 13:06:38 +02:00
|
|
|
<?php
|
2016-02-05 12:08:25 +01:00
|
|
|
declare(strict_types = 1);
|
2015-05-16 13:06:38 +02:00
|
|
|
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-17 09:04:55 +02:00
|
|
|
*
|
2015-05-16 13:06:38 +02:00
|
|
|
* Class Expense
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers\Collection
|
|
|
|
*/
|
|
|
|
class Expense
|
|
|
|
{
|
|
|
|
/** @var Collection */
|
|
|
|
protected $expenses;
|
2015-05-24 08:00:40 +02:00
|
|
|
/** @var string */
|
|
|
|
protected $total = '0';
|
2015-05-16 13:06:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->expenses = new Collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $entry
|
|
|
|
*/
|
|
|
|
public function addOrCreateExpense(TransactionJournal $entry)
|
|
|
|
{
|
2015-12-12 20:19:40 +01:00
|
|
|
|
2015-05-17 10:30:18 +02:00
|
|
|
$accountId = $entry->account_id;
|
2015-12-31 20:12:49 +01:00
|
|
|
$amount = strval(round($entry->journalAmount, 2));
|
2015-12-12 20:20:18 +01:00
|
|
|
if (bccomp('0', $amount) === -1) {
|
2015-12-12 20:19:40 +01:00
|
|
|
$amount = bcmul($amount, '-1');
|
|
|
|
}
|
|
|
|
|
2015-05-17 10:30:18 +02:00
|
|
|
if (!$this->expenses->has($accountId)) {
|
2015-05-16 13:06:38 +02:00
|
|
|
$newObject = new stdClass;
|
2015-12-12 20:19:40 +01:00
|
|
|
$newObject->amount = $amount;
|
2015-12-31 20:12:49 +01:00
|
|
|
$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->expenses->put($accountId, $newObject);
|
2015-05-16 13:06:38 +02:00
|
|
|
} else {
|
2015-05-24 08:00:40 +02:00
|
|
|
$existing = $this->expenses->get($accountId);
|
2015-12-12 20:19:40 +01:00
|
|
|
$existing->amount = bcadd($existing->amount, $amount);
|
2015-05-16 13:06:38 +02:00
|
|
|
$existing->count++;
|
2015-05-17 10:30:18 +02:00
|
|
|
$this->expenses->put($accountId, $existing);
|
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
|
|
|
{
|
2015-12-12 20:16:30 +01:00
|
|
|
|
2015-12-12 20:19:40 +01:00
|
|
|
|
2015-12-12 20:16:30 +01:00
|
|
|
$add = strval(round($add, 2));
|
2015-12-12 20:20:18 +01:00
|
|
|
if (bccomp('0', $add) === -1) {
|
2015-12-12 20:16:30 +01: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 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 getExpenses(): Collection
|
2015-05-16 13:06:38 +02:00
|
|
|
{
|
2015-12-11 18:31:15 +01:00
|
|
|
$set = $this->expenses->sortBy(
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-24 08:00:40 +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
|
|
|
}
|