First create basic objects. Then, enhance.

This commit is contained in:
James Cole 2018-02-18 10:52:56 +01:00
parent f8bf6c163f
commit 94a7b6b9bd
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 11 additions and 41 deletions

View File

@ -111,30 +111,25 @@ class TransactionFactory
{
// all this data is the same for both transactions:
$currency = $this->findCurrency($data['currency_id'], $data['currency_code']);
$foreignCurrency = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']);
$budget = $this->findBudget($data['budget_id'], $data['budget_name']);
$category = $this->findCategory($data['category_id'], $data['category_name']);
$description = $journal->description === $data['description'] ? null : $data['description'];
// type of source account depends on journal type:
$sourceType = $this->accountType($journal, 'source');
$sourceAccount = $this->findAccount($sourceType, $data['source_id'], $data['source_name']);
$sourceFa = is_null($data['foreign_amount']) ? null : app('steam')->negative(strval($data['foreign_amount']));
// same for destination account:
$destinationType = $this->accountType($journal, 'destination');
$destinationAccount = $this->findAccount($destinationType, $data['destination_id'], $data['destination_name']);
$destinationFa = is_null($data['foreign_amount']) ? null : app('steam')->positive(strval($data['foreign_amount']));
// first make a "negative" (source) transaction based on the data in the array.
$sourceTransactionData = [
'description' => $description,
'amount' => app('steam')->negative(strval($data['amount'])),
'foreign_amount' => $sourceFa,
'foreign_amount' => null,
'currency' => $currency,
'foreign_currency' => $foreignCurrency,
'budget' => $budget,
'category' => $category,
'foreign_currency' => null,
'budget' => null,
'category' => null,
'account' => $sourceAccount,
'transaction_journal' => $journal,
'reconciled' => $data['reconciled'],
@ -146,11 +141,11 @@ class TransactionFactory
$destTransactionData = [
'description' => $sourceTransactionData['description'],
'amount' => app('steam')->positive(strval($data['amount'])),
'foreign_amount' => $destinationFa,
'foreign_amount' => null,
'currency' => $currency,
'foreign_currency' => $foreignCurrency,
'budget' => $budget,
'category' => $category,
'foreign_currency' => null,
'budget' => null,
'category' => null,
'account' => $destinationAccount,
'transaction_journal' => $journal,
'reconciled' => $data['reconciled'],

View File

@ -72,14 +72,13 @@ class TransactionJournalFactory
*/
public function create(array $data): TransactionJournal
{
// store basic journal first.
$type = $this->findTransactionType($data['type']);
//$bill = $this->findBill($data['bill_id'], $data['bill_name']);
//$piggyBank = $this->findPiggyBank($data['piggy_bank_id'], $data['piggy_bank_name']);
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
$values = [
'user_id' => $data['user'],
'transaction_type_id' => $type->id,
'bill_id' => is_null($bill) ? null : $bill->id,
'bill_id' => null,
'transaction_currency_id' => $defaultCurrency->id,
'description' => $data['description'],
'date' => $data['date']->format('Y-m-d'),
@ -90,6 +89,7 @@ class TransactionJournalFactory
$journal = $this->repository->storeBasic($values);
// store basic transactions:
$factory = app(TransactionFactory::class);
$factory->setUser($this->user);
@ -100,31 +100,6 @@ class TransactionJournalFactory
}
$this->repository->markCompleted($journal);
// link piggy bank:
if ($type->type === TransactionType::TRANSFER && !is_null($piggyBank)) {
/** @var PiggyBankEventFactory $factory */
$factory = app(PiggyBankEventFactory::class);
$factory->create($journal, $piggyBank);
}
// store tags and link them:
/** @var TagFactory $factory */
$factory = app(TagFactory::class);
$factory->setUser($journal->user);
foreach ($data['tags'] as $string) {
$tagData = [
'tag' => $string,
'date' => null,
'description' => null,
'latitude' => null,
'longitude' => null,
'zoom_level' => null,
'user' => $journal->user,
];
$tag = $factory->create($tagData);
$this->repository->addTag($journal, $tag);
}
return $journal;
}