Some code cleanup.

This commit is contained in:
James Cole
2016-05-18 07:01:27 +02:00
parent b0a5b53abb
commit 95b7da89f0
5 changed files with 120 additions and 188 deletions

View File

@@ -54,40 +54,6 @@ class Journal implements JournalInterface
return true; return true;
} }
/**
* @param array $data
*
* @return TransactionJournal
*/
public function storeJournal(array $data) : TransactionJournal
{
// find transaction type.
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
$journal = new TransactionJournal(
[
'user_id' => $this->user->id,
'transaction_type_id' => $transactionType->id,
'transaction_currency_id' => $data['journal_currency_id'],
'description' => $data['journal_description'],
'completed' => 0,
'date' => $data['date'],
'interest_date' => $data['interest_date'],
'book_date' => $data['book_date'],
'process_date' => $data['process_date'],
]
);
$journal->save();
foreach ($data['transactions'] as $transaction) {
$this->storeTransaction($journal, $transaction);
}
$journal->completed = true;
$journal->save();
return $journal;
}
/** /**
* @param TransactionJournal $journal * @param TransactionJournal $journal
* @param array $transaction * @param array $transaction
@@ -101,32 +67,20 @@ class Journal implements JournalInterface
// store transaction one way: // store transaction one way:
/** @var Transaction $one */ /** @var Transaction $one */
$one = Transaction::create( // first transaction. $one = Transaction::create(
[ ['account_id' => $sourceAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'] * -1,
'account_id' => $sourceAccount->id, 'description' => $transaction['description']]
'transaction_journal_id' => $journal->id, );
'amount' => $transaction['amount'] * -1, $two = Transaction::create(
'description' => $transaction['description'], ['account_id' => $destinationAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'],
] 'description' => $transaction['description']]
); );
// store transaction the other way:
$two = Transaction::create( // first transaction.
[
'account_id' => $destinationAccount->id,
'transaction_journal_id' => $journal->id,
'amount' => $transaction['amount'],
'description' => $transaction['description'],
]
);
// store or get category and connect:
if (strlen($transaction['category']) > 0) { if (strlen($transaction['category']) > 0) {
$category = Category::firstOrCreateEncrypted(['name' => $transaction['category'], 'user_id' => $journal->user_id]); $category = Category::firstOrCreateEncrypted(['name' => $transaction['category'], 'user_id' => $journal->user_id]);
$one->categories()->save($category); $one->categories()->save($category);
$two->categories()->save($category); $two->categories()->save($category);
} }
// store or get budget
if (intval($transaction['budget_id']) > 0) { if (intval($transaction['budget_id']) > 0) {
$budget = Budget::find($transaction['budget_id']); $budget = Budget::find($transaction['budget_id']);
$one->budgets()->save($budget); $one->budgets()->save($budget);
@@ -134,14 +88,11 @@ class Journal implements JournalInterface
} }
if ($transaction['piggy_bank_id'] > 0) { if ($transaction['piggy_bank_id'] > 0) {
// add some extra meta information to the transaction data $transaction['date'] = $journal->date->format('Y-m-d');
$transaction['transaction_journal_id'] = $journal->id;
$transaction['date'] = $journal->date->format('Y-m-d');
event(new TransactionStored($transaction)); event(new TransactionStored($transaction));
} }
return new Collection([$one, $two]); return new Collection([$one, $two]);
} }
/** /**
@@ -215,20 +166,20 @@ class Journal implements JournalInterface
$destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['destination_account_id'])->first(['accounts.*']); $destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['destination_account_id'])->first(['accounts.*']);
if (strlen($data['source_account_name']) > 0) { if (strlen($data['source_account_name']) > 0) {
$fromType = AccountType::where('type', 'Revenue account')->first(); $sourceType = AccountType::where('type', 'Revenue account')->first();
$fromAccount = Account::firstOrCreateEncrypted( $sourceAccount = Account::firstOrCreateEncrypted(
['user_id' => $this->user->id, 'account_type_id' => $fromType->id, 'name' => $data['source_account_name'], 'active' => 1] ['user_id' => $this->user->id, 'account_type_id' => $sourceType->id, 'name' => $data['source_account_name'], 'active' => 1]
); );
return [$fromAccount, $destinationAccount]; return [$sourceAccount, $destinationAccount];
} else {
$fromType = AccountType::where('type', 'Cash account')->first();
$fromAccount = Account::firstOrCreateEncrypted(
['user_id' => $this->user->id, 'account_type_id' => $fromType->id, 'name' => 'Cash account', 'active' => 1]
);
} }
return [$fromAccount, $destinationAccount]; $sourceType = AccountType::where('type', 'Cash account')->first();
$sourceAccount = Account::firstOrCreateEncrypted(
['user_id' => $this->user->id, 'account_type_id' => $sourceType->id, 'name' => 'Cash account', 'active' => 1]
);
return [$sourceAccount, $destinationAccount];
} }
/** /**

View File

@@ -27,13 +27,6 @@ interface JournalInterface
*/ */
public function markAsComplete(TransactionJournal $journal); public function markAsComplete(TransactionJournal $journal);
/**
* @param array $data
*
* @return TransactionJournal
*/
public function storeJournal(array $data) : TransactionJournal;
/** /**
* @param TransactionJournal $journal * @param TransactionJournal $journal
* @param array $transaction * @param array $transaction

View File

@@ -13,6 +13,7 @@ namespace FireflyIII\Export\Exporter;
use FireflyIII\Export\Entry\Entry; use FireflyIII\Export\Entry\Entry;
use FireflyIII\Export\Entry\EntryAccount; use FireflyIII\Export\Entry\EntryAccount;
use FireflyIII\Models\ExportJob; use FireflyIII\Models\ExportJob;
use Illuminate\Support\Collection;
use League\Csv\Writer; use League\Csv\Writer;
use SplFileObject; use SplFileObject;
@@ -55,92 +56,33 @@ class CsvExporter extends BasicExporter implements ExporterInterface
// necessary for CSV writer: // necessary for CSV writer:
$fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName; $fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName;
$writer = Writer::createFromPath(new SplFileObject($fullPath, 'a+'), 'w');
$rows = [];
// create CSV writer: // Count the maximum number of sources and destinations each entry has. May need to expand the number of export fields:
$writer = Writer::createFromPath(new SplFileObject($fullPath, 'a+'), 'w'); $maxSourceAccounts = 1;
$maxDestAccounts = 1;
// all rows:
$rows = [];
/*
* Count the maximum number of sources and destinations
* each entry has. May need to expand the number of export fields:
*/
$maxSourceAccounts = 1;
$maxDestinationAccounts = 1;
/** @var Entry $entry */ /** @var Entry $entry */
foreach ($this->getEntries() as $entry) { foreach ($this->getEntries() as $entry) {
$sources = $entry->sourceAccounts->count(); $sources = $entry->sourceAccounts->count();
$destinations = $entry->destinationAccounts->count(); $destinations = $entry->destinationAccounts->count();
$maxSourceAccounts = max($maxSourceAccounts, $sources); $maxSourceAccounts = max($maxSourceAccounts, $sources);
$maxDestinationAccounts = max($maxDestinationAccounts, $destinations); $maxDestAccounts = max($maxDestAccounts, $destinations);
} }
$rows[] = array_keys($this->getFieldsAndTypes($maxSourceAccounts, $maxDestAccounts));
// add header:
$rows[] = array_keys($this->getFieldsAndTypes($maxSourceAccounts, $maxDestinationAccounts));
// then the rest:
/** @var Entry $entry */ /** @var Entry $entry */
foreach ($this->getEntries() as $entry) { foreach ($this->getEntries() as $entry) {
// order is defined in Entry::getFieldsAndTypes. // order is defined in Entry::getFieldsAndTypes.
$current = [ $current = [$entry->description, $entry->amount, $entry->date];
$entry->description, $sourceData = $this->getAccountData($maxSourceAccounts, $entry->sourceAccounts);
$entry->amount, $current = array_merge($current, $sourceData);
$entry->date]; $destData = $this->getAccountData($maxDestAccounts, $entry->destinationAccounts);
for ($i = 0; $i < $maxSourceAccounts; $i++) { $current = array_merge($current, $destData);
/** @var EntryAccount $source */ $rest = [$entry->budget->budgetId, $entry->budget->name, $entry->category->categoryId, $entry->category->name, $entry->bill->billId,
$source = $entry->sourceAccounts->get($i); $entry->bill->name];
$currentId = ''; $current = array_merge($current, $rest);
$currentName = ''; $rows[] = $current;
$currentIban = '';
$currentType = '';
$currentNumber = '';
if ($source) {
$currentId = $source->accountId;
$currentName = $source->name;
$currentIban = $source->iban;
$currentType = $source->type;
$currentNumber = $source->number;
}
$current[] = $currentId;
$current[] = $currentName;
$current[] = $currentIban;
$current[] = $currentType;
$current[] = $currentNumber;
}
unset($source);
for ($i = 0; $i < $maxDestinationAccounts; $i++) {
/** @var EntryAccount $destination */
$destination = $entry->destinationAccounts->get($i);
$currentId = '';
$currentName = '';
$currentIban = '';
$currentType = '';
$currentNumber = '';
if ($destination) {
$currentId = $destination->accountId;
$currentName = $destination->name;
$currentIban = $destination->iban;
$currentType = $destination->type;
$currentNumber = $destination->number;
}
$current[] = $currentId;
$current[] = $currentName;
$current[] = $currentIban;
$current[] = $currentType;
$current[] = $currentNumber;
}
unset($destination);
$current[] = $entry->budget->budgetId;
$current[] = $entry->budget->name;
$current[] = $entry->category->categoryId;
$current[] = $entry->category->name;
$current[] = $entry->bill->billId;
$current[] = $entry->bill->name;
$rows[] = $current;
} }
$writer->insertAll($rows); $writer->insertAll($rows);
@@ -148,6 +90,44 @@ class CsvExporter extends BasicExporter implements ExporterInterface
} }
/** /**
* @param int $max
* @param Collection $accounts
*
* @return array
*/
private function getAccountData(int $max, Collection $accounts): array
{
$current = [];
for ($i = 0; $i < $max; $i++) {
/** @var EntryAccount $source */
$source = $accounts->get($i);
$currentId = '';
$currentName = '';
$currentIban = '';
$currentType = '';
$currentNumber = '';
if ($source) {
$currentId = $source->accountId;
$currentName = $source->name;
$currentIban = $source->iban;
$currentType = $source->type;
$currentNumber = $source->number;
}
$current[] = $currentId;
$current[] = $currentName;
$current[] = $currentIban;
$current[] = $currentType;
$current[] = $currentNumber;
}
unset($source);
return $current;
}
/**
* @param int $sources
* @param int $destinations
*
* @return array * @return array
*/ */
private function getFieldsAndTypes(int $sources, int $destinations): array private function getFieldsAndTypes(int $sources, int $destinations): array

View File

@@ -18,12 +18,10 @@ use FireflyIII\Helpers\Collection\BalanceEntry;
use FireflyIII\Helpers\Collection\BalanceHeader; use FireflyIII\Helpers\Collection\BalanceHeader;
use FireflyIII\Helpers\Collection\BalanceLine; use FireflyIII\Helpers\Collection\BalanceLine;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\Budget as BudgetModel;
use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\LimitRepetition;
use FireflyIII\Models\Tag; use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -37,20 +35,16 @@ class BalanceReportHelper implements BalanceReportHelperInterface
/** @var BudgetRepositoryInterface */ /** @var BudgetRepositoryInterface */
protected $budgetRepository; protected $budgetRepository;
/** @var TagRepositoryInterface */
protected $tagRepository;
/** /**
* ReportHelper constructor. * ReportHelper constructor.
* *
* *
* @param BudgetRepositoryInterface $budgetRepository * @param BudgetRepositoryInterface $budgetRepository
* @param TagRepositoryInterface $tagRepository
*/ */
public function __construct(BudgetRepositoryInterface $budgetRepository, TagRepositoryInterface $tagRepository) public function __construct(BudgetRepositoryInterface $budgetRepository)
{ {
$this->budgetRepository = $budgetRepository; $this->budgetRepository = $budgetRepository;
$this->tagRepository = $tagRepository;
} }
@@ -148,13 +142,11 @@ class BalanceReportHelper implements BalanceReportHelperInterface
* *
* @return BalanceLine * @return BalanceLine
*/ */
private function createBalanceLine(BudgetModel $budget, LimitRepetition $repetition, Collection $accounts): BalanceLine private function createBalanceLine(Budget $budget, LimitRepetition $repetition, Collection $accounts): BalanceLine
{ {
$line = new BalanceLine; $line = new BalanceLine;
$budget->amount = $repetition->amount; $budget->amount = $repetition->amount;
$line->setBudget($budget); $line->setBudget($budget);
$line->setStartDate($repetition->startdate); $line->setStartDate($repetition->startdate);
$line->setEndDate($repetition->enddate); $line->setEndDate($repetition->enddate);
@@ -185,7 +177,6 @@ class BalanceReportHelper implements BalanceReportHelperInterface
$noBudgetEntries = $noBudgetLine->getBalanceEntries(); $noBudgetEntries = $noBudgetLine->getBalanceEntries();
$tagEntries = $coveredByTagLine->getBalanceEntries(); $tagEntries = $coveredByTagLine->getBalanceEntries();
/** @var BalanceEntry $entry */
foreach ($noBudgetEntries as $entry) { foreach ($noBudgetEntries as $entry) {
$account = $entry->getAccount(); $account = $entry->getAccount();
$tagEntry = $tagEntries->filter( $tagEntry = $tagEntries->filter(
@@ -221,7 +212,6 @@ class BalanceReportHelper implements BalanceReportHelperInterface
foreach ($accounts as $account) { foreach ($accounts as $account) {
$spent = $this->budgetRepository->spentInPeriodWithoutBudget(new Collection([$account]), $start, $end); $spent = $this->budgetRepository->spentInPeriodWithoutBudget(new Collection([$account]), $start, $end);
//$spent ='0';
// budget // budget
$budgetEntry = new BalanceEntry; $budgetEntry = new BalanceEntry;
$budgetEntry->setAccount($account); $budgetEntry->setAccount($account);
@@ -278,11 +268,9 @@ class BalanceReportHelper implements BalanceReportHelperInterface
{ {
$set = $balance->getBalanceLines(); $set = $balance->getBalanceLines();
$newSet = new Collection; $newSet = new Collection;
/** @var BalanceLine $entry */
foreach ($set as $entry) { foreach ($set as $entry) {
if (!is_null($entry->getBudget()->id)) { if (!is_null($entry->getBudget()->id)) {
$sum = '0'; $sum = '0';
/** @var BalanceEntry $balanceEntry */
foreach ($entry->getBalanceEntries() as $balanceEntry) { foreach ($entry->getBalanceEntries() as $balanceEntry) {
$sum = bcadd($sum, $balanceEntry->getSpent()); $sum = bcadd($sum, $balanceEntry->getSpent());
} }

View File

@@ -16,7 +16,6 @@ use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
use FireflyIII\Helpers\Collection\BudgetLine; use FireflyIII\Helpers\Collection\BudgetLine;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\LimitRepetition;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -27,6 +26,18 @@ use Illuminate\Support\Collection;
*/ */
class BudgetReportHelper implements BudgetReportHelperInterface class BudgetReportHelper implements BudgetReportHelperInterface
{ {
/** @var BudgetRepositoryInterface */
private $repository;
/**
* BudgetReportHelper constructor.
*
* @param BudgetRepositoryInterface $repository
*/
public function __construct(BudgetRepositoryInterface $repository)
{
$this->repository = $repository;
}
/** /**
* @param Carbon $start * @param Carbon $start
@@ -38,9 +49,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): BudgetCollection public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): BudgetCollection
{ {
$object = new BudgetCollection; $object = new BudgetCollection;
/** @var BudgetRepositoryInterface $repository */ $set = $this->repository->getBudgets();
$repository = app(BudgetRepositoryInterface::class);
$set = $repository->getBudgets();
/** @var Budget $budget */ /** @var Budget $budget */
foreach ($set as $budget) { foreach ($set as $budget) {
@@ -49,7 +58,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface
// no repetition(s) for this budget: // no repetition(s) for this budget:
if ($repetitions->count() == 0) { if ($repetitions->count() == 0) {
// spent for budget in time range: // spent for budget in time range:
$spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end); $spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);
if ($spent > 0) { if ($spent > 0) {
$budgetLine = new BudgetLine; $budgetLine = new BudgetLine;
@@ -63,24 +72,20 @@ class BudgetReportHelper implements BudgetReportHelperInterface
// one or more repetitions for budget: // one or more repetitions for budget:
/** @var LimitRepetition $repetition */ /** @var LimitRepetition $repetition */
foreach ($repetitions as $repetition) { foreach ($repetitions as $repetition) {
$data = $this->calculateExpenses($budget, $repetition, $accounts);
$budgetLine = new BudgetLine; $budgetLine = new BudgetLine;
$budgetLine->setBudget($budget); $budgetLine->setBudget($budget);
$budgetLine->setRepetition($repetition); $budgetLine->setRepetition($repetition);
$expenses = $repository->spentInPeriod(new Collection([$budget]), $accounts, $repetition->startdate, $repetition->enddate); $budgetLine->setLeft($data['left']);
$left = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? bcadd($repetition->amount, $expenses) : '0'; $budgetLine->setSpent($data['expenses']);
$spent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? $expenses : '0'; $budgetLine->setOverspent($data['overspent']);
$overspent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $repetition->amount);
$budgetLine->setLeft($left);
$budgetLine->setSpent($expenses);
$budgetLine->setOverspent($overspent);
$budgetLine->setBudgeted($repetition->amount); $budgetLine->setBudgeted($repetition->amount);
$object->addBudgeted($repetition->amount); $object->addBudgeted($repetition->amount);
$object->addSpent($spent); $object->addSpent($data['spent']);
$object->addLeft($left); $object->addLeft($data['left']);
$object->addOverspent($overspent); $object->addOverspent($data['overspent']);
$object->addBudgetLine($budgetLine); $object->addBudgetLine($budgetLine);
} }
@@ -88,12 +93,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface
} }
// stuff outside of budgets: // stuff outside of budgets:
$outsideBudget = $repository->journalsInPeriodWithoutBudget($accounts, $start, $end);
$noBudget = '0'; $noBudget = $this->repository->spentInPeriodWithoutBudget($accounts, $start, $end);
/** @var TransactionJournal $journal */
foreach ($outsideBudget as $journal) {
$noBudget = bcadd($noBudget, TransactionJournal::amount($journal));
}
$budgetLine = new BudgetLine; $budgetLine = new BudgetLine;
$budgetLine->setOverspent($noBudget); $budgetLine->setOverspent($noBudget);
$budgetLine->setSpent($noBudget); $budgetLine->setSpent($noBudget);
@@ -159,4 +160,23 @@ class BudgetReportHelper implements BudgetReportHelperInterface
return $sum; return $sum;
} }
/**
* @param Budget $budget
* @param LimitRepetition $repetition
* @param Collection $accounts
*
* @return array
*/
private function calculateExpenses(Budget $budget, LimitRepetition $repetition, Collection $accounts): array
{
$array = [];
$expenses = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $repetition->startdate, $repetition->enddate);
$array['left'] = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? bcadd($repetition->amount, $expenses) : '0';
$array['spent'] = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? $expenses : '0';
$array['overspent'] = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $repetition->amount);
return $array;
}
} }