Expand code for #384

This commit is contained in:
James Cole 2017-12-10 12:00:08 +01:00
parent 06683c57dd
commit b93a96db23
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 63 additions and 4 deletions

View File

@ -23,7 +23,12 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Report; namespace FireflyIII\Http\Controllers\Report;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\Collection;
/** /**
@ -31,5 +36,58 @@ use FireflyIII\Http\Controllers\Controller;
*/ */
class ExpenseController extends Controller class ExpenseController extends Controller
{ {
/** @var AccountRepositoryInterface */
protected $accountRepository;
/**
*
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
$this->accountRepository = app(AccountRepositoryInterface::class);
return $next($request);
}
);
}
/**
* @param Collection $accounts
* @param Collection $expense
* @param Carbon $start
* @param Carbon $end
*/
public function spentGrouped(Collection $accounts, Collection $expense, Carbon $start, Carbon $end)
{
$combined = $this->combineAccounts($expense);
// for period, get spent and earned for each account (by name)
/**
* @var string $name
* @var Collection $combi
*/
foreach($combined as $name => $combi) {
}
}
protected function combineAccounts(Collection $accounts): array
{
$combined = [];
/** @var Account $expenseAccount */
foreach ($accounts as $expenseAccount) {
$combined[$expenseAccount->name] = [$expenseAccount];
$revenue = $this->accountRepository->findByName($expenseAccount->name, [AccountType::REVENUE]);
if (!is_null($revenue->id)) {
$combined[$expenseAccount->name][] = $revenue;
}
}
return $combined;
}
} }

View File

@ -160,6 +160,7 @@ class AccountRepository implements AccountRepositoryInterface
* @param array $data * @param array $data
* *
* @return Account * @return Account
* @throws FireflyException
*/ */
public function store(array $data): Account public function store(array $data): Account
{ {
@ -364,13 +365,13 @@ class AccountRepository implements AccountRepositoryInterface
'date' => $data['openingBalanceDate'], 'date' => $data['openingBalanceDate'],
] ]
); );
Log::debug(sprintf('Created new opening balance journal: #%d', $journal->id)); Log::notice(sprintf('Created new opening balance journal: #%d', $journal->id));
$firstAccount = $account; $firstAccount = $account;
$secondAccount = $opposing; $secondAccount = $opposing;
$firstAmount = $amount; $firstAmount = $amount;
$secondAmount = bcmul($amount, '-1'); $secondAmount = bcmul($amount, '-1');
Log::debug(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount)); Log::notice(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount));
if (bccomp($amount, '0') === -1) { if (bccomp($amount, '0') === -1) {
Log::debug(sprintf('%s is a negative number.', $amount)); Log::debug(sprintf('%s is a negative number.', $amount));
@ -378,7 +379,7 @@ class AccountRepository implements AccountRepositoryInterface
$secondAccount = $account; $secondAccount = $account;
$firstAmount = bcmul($amount, '-1'); $firstAmount = bcmul($amount, '-1');
$secondAmount = $amount; $secondAmount = $amount;
Log::debug(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount)); Log::notice(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount));
} }
$one = new Transaction( $one = new Transaction(
@ -400,7 +401,7 @@ class AccountRepository implements AccountRepositoryInterface
); );
$two->save(); // second transaction: to $two->save(); // second transaction: to
Log::debug(sprintf('Stored two transactions, #%d and #%d', $one->id, $two->id)); Log::notice(sprintf('Stored two transactions for new account, #%d and #%d', $one->id, $two->id));
return $journal; return $journal;
} }