mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-07 06:31:22 +00:00
Test every happy path for journal creation.
This commit is contained in:
@@ -25,12 +25,15 @@ namespace FireflyIII\Factory;
|
||||
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class TagFactory
|
||||
*/
|
||||
class TagFactory
|
||||
{
|
||||
/** @var Collection */
|
||||
private $tags;
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
@@ -42,14 +45,6 @@ class TagFactory
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
@@ -59,14 +54,56 @@ class TagFactory
|
||||
{
|
||||
return Tag::create(
|
||||
[
|
||||
'user_id' => $data['user']->id,
|
||||
'tag' => $data['tag'],
|
||||
'tagMode' => 'nothing',
|
||||
'date' => $data['date'],
|
||||
'description'=> $data['description'],
|
||||
|
||||
'user_id' => $this->user->id,
|
||||
'tag' => $data['tag'],
|
||||
'tagMode' => 'nothing',
|
||||
'date' => $data['date'],
|
||||
'description' => $data['description'],
|
||||
'latitude' => $data['latitude'],
|
||||
'longitude ' => $data['longitude'],
|
||||
'zoomLevel' => $data['zoom_level'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $tag
|
||||
*
|
||||
* @return Tag|null
|
||||
*/
|
||||
public function findOrCreate(string $tag): ?Tag
|
||||
{
|
||||
if (is_null($this->tags)) {
|
||||
$this->tags = $this->user->tags()->get();
|
||||
}
|
||||
|
||||
/** @var Tag $object */
|
||||
foreach ($this->tags as $object) {
|
||||
if ($object->tag === $tag) {
|
||||
return $object;
|
||||
}
|
||||
}
|
||||
$newTag = $this->create(
|
||||
[
|
||||
'tag' => $tag,
|
||||
'date' => null,
|
||||
'description' => null,
|
||||
'latitude' => null,
|
||||
'longitude' => null,
|
||||
'zoom_level' => null,
|
||||
]
|
||||
);
|
||||
$this->tags->push($newTag);
|
||||
|
||||
return $newTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -78,8 +78,7 @@ class TransactionFactory
|
||||
*/
|
||||
public function create(array $data): Transaction
|
||||
{
|
||||
$foreignCurrencyId = is_null($data['foreign_currency']) ? null : $data['foreign_currency']->id;
|
||||
$values = [
|
||||
$values = [
|
||||
'reconciled' => $data['reconciled'],
|
||||
'account_id' => $data['account']->id,
|
||||
'transaction_journal_id' => $data['transaction_journal']->id,
|
||||
@@ -87,14 +86,10 @@ class TransactionFactory
|
||||
'transaction_currency_id' => $data['currency']->id,
|
||||
'amount' => $data['amount'],
|
||||
'foreign_amount' => $data['foreign_amount'],
|
||||
'foreign_currency_id' => $foreignCurrencyId,
|
||||
'foreign_currency_id' => null,
|
||||
'identifier' => $data['identifier'],
|
||||
];
|
||||
$transaction = $this->repository->storeBasicTransaction($values);
|
||||
|
||||
// todo: add budget, category, etc.
|
||||
// todo link budget, category
|
||||
|
||||
$transaction = $this->repository->storeBasicTransaction($values);
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
@@ -110,8 +105,8 @@ class TransactionFactory
|
||||
public function createPair(TransactionJournal $journal, array $data): Collection
|
||||
{
|
||||
// all this data is the same for both transactions:
|
||||
$currency = $this->findCurrency($data['currency_id'], $data['currency_code']);
|
||||
$description = $journal->description === $data['description'] ? null : $data['description'];
|
||||
$currency = $this->findCurrency($data['currency_id'], $data['currency_code']);
|
||||
$description = $journal->description === $data['description'] ? null : $data['description'];
|
||||
|
||||
// type of source account depends on journal type:
|
||||
$sourceType = $this->accountType($journal, 'source');
|
||||
@@ -120,38 +115,52 @@ class TransactionFactory
|
||||
// same for destination account:
|
||||
$destinationType = $this->accountType($journal, 'destination');
|
||||
$destinationAccount = $this->findAccount($destinationType, $data['destination_id'], $data['destination_name']);
|
||||
|
||||
// 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' => null,
|
||||
'currency' => $currency,
|
||||
'foreign_currency' => null,
|
||||
'budget' => null,
|
||||
'category' => null,
|
||||
'account' => $sourceAccount,
|
||||
'transaction_journal' => $journal,
|
||||
'reconciled' => $data['reconciled'],
|
||||
'identifier' => $data['identifier'],
|
||||
];
|
||||
$source = $this->create($sourceTransactionData);
|
||||
|
||||
$source = $this->create(
|
||||
[
|
||||
'description' => $description,
|
||||
'amount' => app('steam')->negative(strval($data['amount'])),
|
||||
'foreign_amount' => null,
|
||||
'currency' => $currency,
|
||||
'account' => $sourceAccount,
|
||||
'transaction_journal' => $journal,
|
||||
'reconciled' => $data['reconciled'],
|
||||
'identifier' => $data['identifier'],
|
||||
]
|
||||
);
|
||||
// then make a "positive" transaction based on the data in the array.
|
||||
$destTransactionData = [
|
||||
'description' => $sourceTransactionData['description'],
|
||||
'amount' => app('steam')->positive(strval($data['amount'])),
|
||||
'foreign_amount' => null,
|
||||
'currency' => $currency,
|
||||
'foreign_currency' => null,
|
||||
'budget' => null,
|
||||
'category' => null,
|
||||
'account' => $destinationAccount,
|
||||
'transaction_journal' => $journal,
|
||||
'reconciled' => $data['reconciled'],
|
||||
'identifier' => $data['identifier'],
|
||||
];
|
||||
$dest = $this->create($destTransactionData);
|
||||
$dest = $this->create(
|
||||
[
|
||||
'description' => $description,
|
||||
'amount' => app('steam')->positive(strval($data['amount'])),
|
||||
'foreign_amount' => null,
|
||||
'currency' => $currency,
|
||||
'account' => $destinationAccount,
|
||||
'transaction_journal' => $journal,
|
||||
'reconciled' => $data['reconciled'],
|
||||
'identifier' => $data['identifier'],
|
||||
]
|
||||
);
|
||||
// set foreign currency
|
||||
$foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']);
|
||||
$this->setForeignCurrency($source, $foreign);
|
||||
$this->setForeignCurrency($dest, $foreign);
|
||||
|
||||
// set foreign amount:
|
||||
if (!is_null($data['foreign_amount'])) {
|
||||
$this->setForeignAmount($source, app('steam')->negative(strval($data['foreign_amount'])));
|
||||
$this->setForeignAmount($dest, app('steam')->positive(strval($data['foreign_amount'])));
|
||||
}
|
||||
|
||||
// set budget:
|
||||
$budget = $this->findBudget($data['budget_id'], $data['budget_name']);
|
||||
$this->setBudget($source, $budget);
|
||||
$this->setBudget($dest, $budget);
|
||||
|
||||
// set category
|
||||
$category = $this->findCategory($data['category_id'], $data['category_name']);
|
||||
$this->setCategory($source, $category);
|
||||
$this->setCategory($dest, $category);
|
||||
|
||||
return new Collection([$source, $dest]);
|
||||
}
|
||||
@@ -356,4 +365,57 @@ class TransactionFactory
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param Budget|null $budget
|
||||
*/
|
||||
protected function setBudget(Transaction $transaction, ?Budget $budget): void
|
||||
{
|
||||
if (is_null($budget)) {
|
||||
return;
|
||||
}
|
||||
$transaction->budgets()->save($budget);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param Category|null $category
|
||||
*/
|
||||
protected function setCategory(Transaction $transaction, ?Category $category): void
|
||||
{
|
||||
if (is_null($category)) {
|
||||
return;
|
||||
}
|
||||
$transaction->categories()->save($category);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param string $amount
|
||||
*/
|
||||
protected function setForeignAmount(Transaction $transaction, string $amount): void
|
||||
{
|
||||
$transaction->foreign_amount = $amount;
|
||||
$transaction->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param TransactionCurrency|null $currency
|
||||
*/
|
||||
protected function setForeignCurrency(Transaction $transaction, ?TransactionCurrency $currency): void
|
||||
{
|
||||
if (is_null($currency)) {
|
||||
return;
|
||||
}
|
||||
$transaction->foreign_currency_id = $currency->id;
|
||||
$transaction->save();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ namespace FireflyIII\Factory;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
@@ -95,11 +96,31 @@ class TransactionJournalFactory
|
||||
|
||||
/** @var array $trData */
|
||||
foreach ($data['transactions'] as $trData) {
|
||||
$trData['reconciled'] = $data['reconciled'] ?? false;
|
||||
$factory->createPair($journal, $trData);
|
||||
}
|
||||
$this->repository->markCompleted($journal);
|
||||
|
||||
// link bill:
|
||||
$this->connectBill($journal, $data);
|
||||
|
||||
// link piggy bank:
|
||||
$this->connectPiggyBank($journal, $data);
|
||||
|
||||
// link tags:
|
||||
$this->connectTags($journal, $data);
|
||||
|
||||
// store note:
|
||||
$this->storeNote($journal, $data['notes']);
|
||||
|
||||
// store date meta fields (if present):
|
||||
$this->storeMeta($journal, $data, 'interest_date');
|
||||
$this->storeMeta($journal, $data, 'book_date');
|
||||
$this->storeMeta($journal, $data, 'process_date');
|
||||
$this->storeMeta($journal, $data, 'due_date');
|
||||
$this->storeMeta($journal, $data, 'payment_date');
|
||||
$this->storeMeta($journal, $data, 'invoice_date');
|
||||
$this->storeMeta($journal, $data, 'internal_reference');
|
||||
|
||||
return $journal;
|
||||
}
|
||||
|
||||
@@ -116,6 +137,49 @@ class TransactionJournalFactory
|
||||
$this->piggyRepository->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect bill if present.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $data
|
||||
*/
|
||||
protected function connectBill(TransactionJournal $journal, array $data): void
|
||||
{
|
||||
$bill = $this->findBill($data['bill_id'], $data['bill_name']);
|
||||
if (!is_null($bill)) {
|
||||
$journal->bill_id = $bill->id;
|
||||
$journal->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $data
|
||||
*/
|
||||
protected function connectPiggyBank(TransactionJournal $journal, array $data): void
|
||||
{
|
||||
$piggyBank = $this->findPiggyBank($data['piggy_bank_id'], $data['piggy_bank_name']);
|
||||
if (!is_null($piggyBank)) {
|
||||
/** @var PiggyBankEventFactory $factory */
|
||||
$factory = app(PiggyBankEventFactory::class);
|
||||
$factory->create($journal, $piggyBank);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $data
|
||||
*/
|
||||
protected function connectTags(TransactionJournal $journal, array $data): void
|
||||
{
|
||||
$factory = app(TagFactory::class);
|
||||
$factory->setUser($journal->user);
|
||||
foreach ($data['tags'] as $string) {
|
||||
$tag = $factory->findOrCreate($string);
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the given bill based on the ID or the name. ID takes precedence over the name.
|
||||
*
|
||||
@@ -203,4 +267,39 @@ class TransactionJournalFactory
|
||||
return $transactionType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $data
|
||||
* @param string $field
|
||||
*/
|
||||
protected function storeMeta(TransactionJournal $journal, array $data, string $field): void
|
||||
{
|
||||
$value = $data[$field];
|
||||
if (!is_null($value)) {
|
||||
$set = [
|
||||
'journal' => $journal,
|
||||
'name' => $field,
|
||||
'data' => $data[$field],
|
||||
];
|
||||
/** @var TransactionJournalMetaFactory $factory */
|
||||
$factory = app(TransactionJournalMetaFactory::class);
|
||||
$factory->updateOrCreate($set);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $notes
|
||||
*/
|
||||
protected function storeNote(TransactionJournal $journal, string $notes): void
|
||||
{
|
||||
if (strlen($notes) > 0) {
|
||||
$note = new Note;
|
||||
$note->noteable()->associate($journal);
|
||||
$note->text = $notes;
|
||||
$note->save();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
58
app/Factory/TransactionJournalMetaFactory.php
Normal file
58
app/Factory/TransactionJournalMetaFactory.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* TransactionJournalMetaFactory.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Factory;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\TransactionJournalMeta;
|
||||
|
||||
/**
|
||||
* Class TransactionJournalMetaFactory
|
||||
*/
|
||||
class TransactionJournalMetaFactory
|
||||
{
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionJournalMeta
|
||||
*/
|
||||
public function updateOrCreate(array $data): TransactionJournalMeta
|
||||
{
|
||||
$value = $data['data'];
|
||||
if ($data['data'] instanceof Carbon) {
|
||||
$value = $data['data']->toW3cString();
|
||||
}
|
||||
|
||||
$entry = $data['journal']->transactionJournalMeta()->where('name', $data['name'])->first();
|
||||
if (null === $entry) {
|
||||
$entry = new TransactionJournalMeta();
|
||||
$entry->transactionJournal()->associate($data['journal']);
|
||||
$entry->name = $data['name'];
|
||||
}
|
||||
$entry->data = $value;
|
||||
$entry->save();
|
||||
|
||||
return $entry;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user