mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-18 20:38:57 +00:00
Move import to factory #1222
This commit is contained in:
@@ -25,9 +25,9 @@ namespace FireflyIII\Import\Storage;
|
||||
use ErrorException;
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\TransactionJournalFactory;
|
||||
use FireflyIII\Import\Object\ImportJournal;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
@@ -75,6 +75,8 @@ class ImportStorage
|
||||
private $applyRules = false;
|
||||
/** @var string */
|
||||
private $dateFormat = 'Ymd';
|
||||
/** @var TransactionJournalFactory */
|
||||
private $factory;
|
||||
/** @var bool */
|
||||
private $matchBills = false;
|
||||
/** @var Collection */
|
||||
@@ -92,6 +94,7 @@ class ImportStorage
|
||||
$this->objects = new Collection;
|
||||
$this->journals = new Collection;
|
||||
$this->errors = new Collection;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,6 +114,8 @@ class ImportStorage
|
||||
$this->journalRepository = app(JournalRepositoryInterface::class);
|
||||
$this->repository->setUser($job->user);
|
||||
$this->journalRepository->setUser($job->user);
|
||||
$this->factory = app(TransactionJournalFactory::class);
|
||||
$this->factory->setUser($job->user);
|
||||
|
||||
$config = $this->repository->getConfiguration($job);
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($job->user);
|
||||
@@ -158,6 +163,7 @@ class ImportStorage
|
||||
} catch (FireflyException | ErrorException | Exception $e) {
|
||||
$this->errors->push($e->getMessage());
|
||||
Log::error(sprintf('Cannot import row #%d because: %s', $index, $e->getMessage()));
|
||||
Log::error($e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -179,12 +185,12 @@ class ImportStorage
|
||||
Log::debug(sprintf('Going to store object #%d/%d with description "%s"', ($index + 1), $this->total, $importJournal->getDescription()));
|
||||
$assetAccount = $importJournal->asset->getAccount();
|
||||
$amount = $importJournal->getAmount();
|
||||
$foreignAmount = $importJournal->getForeignAmount();
|
||||
$currencyId = $this->getCurrencyId($importJournal);
|
||||
$foreignCurrencyId = $this->getForeignCurrencyId($importJournal, $currencyId);
|
||||
$date = $importJournal->getDate($this->dateFormat)->format('Y-m-d');
|
||||
$opposingAccount = $this->getOpposingAccount($importJournal->opposing, $assetAccount->id, $amount);
|
||||
$transactionType = $this->getTransactionType($amount, $opposingAccount);
|
||||
$description = $importJournal->getDescription();
|
||||
$this->addStep();
|
||||
|
||||
/**
|
||||
@@ -192,7 +198,7 @@ class ImportStorage
|
||||
*/
|
||||
$parameters = [
|
||||
'type' => $transactionType,
|
||||
'description' => $description,
|
||||
'description' => $importJournal->getDescription(),
|
||||
'amount' => $amount,
|
||||
'date' => $date,
|
||||
'asset' => $assetAccount->name,
|
||||
@@ -211,52 +217,83 @@ class ImportStorage
|
||||
unset($parameters);
|
||||
$this->addStep();
|
||||
|
||||
// store journal and create transactions:
|
||||
$parameters = [
|
||||
'type' => $transactionType,
|
||||
'currency' => $currencyId,
|
||||
'foreign_currency' => $foreignCurrencyId,
|
||||
'asset' => $assetAccount,
|
||||
'opposing' => $opposingAccount,
|
||||
'description' => $description,
|
||||
'date' => $date,
|
||||
'hash' => $importJournal->hash,
|
||||
'amount' => $amount,
|
||||
];
|
||||
$journal = $this->storeJournal($parameters);
|
||||
unset($parameters);
|
||||
$this->addStep();
|
||||
|
||||
// store meta object things:
|
||||
$this->storeCategory($journal, $importJournal->category->getCategory());
|
||||
$this->storeBudget($journal, $importJournal->budget->getBudget());
|
||||
try {
|
||||
$budget = $importJournal->budget->getBudget();
|
||||
$category = $importJournal->category->getCategory();
|
||||
$bill = $importJournal->bill->getBill();
|
||||
$source = $assetAccount;
|
||||
$destination = $opposingAccount;
|
||||
|
||||
// to save bill, also give it the amount:
|
||||
$importJournal->bill->setAmount($amount);
|
||||
|
||||
$this->storeBill($journal, $importJournal->bill->getBill());
|
||||
$this->storeMetaDates($journal, $importJournal->metaDates);
|
||||
$this->storeTags($importJournal->tags, $journal);
|
||||
|
||||
foreach ($importJournal->metaFields as $field => $value) {
|
||||
$this->journalRepository->setMetaString($journal, $field, $value);
|
||||
if ($transactionType === TransactionType::DEPOSIT) {
|
||||
$destination = $assetAccount;
|
||||
$source = $opposingAccount;
|
||||
}
|
||||
Log::debug(
|
||||
sprintf('Will make #%s (%s) the source and #%s (%s) the destination.', $source->id, $source->name, $destination->id, $destination->name)
|
||||
);
|
||||
$data = [
|
||||
'user' => $this->job->user_id,
|
||||
'type' => $transactionType,
|
||||
'date' => $importJournal->getDate($this->dateFormat),
|
||||
'description' => $importJournal->getDescription(),
|
||||
'piggy_bank_id' => null,
|
||||
'piggy_bank_name' => null,
|
||||
'bill_id' => is_null($bill) ? null : $bill->id,
|
||||
'bill_name' => null,
|
||||
'tags' => $importJournal->tags,
|
||||
'interest_date' => $importJournal->getMetaDate('interest_date'),
|
||||
'book_date' => $importJournal->getMetaDate('book_date'),
|
||||
'process_date' => $importJournal->getMetaDate('process_date'),
|
||||
'due_date' => $importJournal->getMetaDate('due_date'),
|
||||
'payment_date' => $importJournal->getMetaDate('payment_date'),
|
||||
'invoice_date' => $importJournal->getMetaDate('invoice_date'),
|
||||
'internal_reference' => $importJournal->metaFields['internal_reference'] ?? null,
|
||||
'notes' => $importJournal->notes,
|
||||
'sepa-cc' => $importJournal->getMetaString('sepa-cc'),
|
||||
'sepa-ct-op' => $importJournal->getMetaString('sepa-ct-op'),
|
||||
'sepa-ct-id' => $importJournal->getMetaString('sepa-ct-id'),
|
||||
'sepa-db' => $importJournal->getMetaString('sepa-db'),
|
||||
'sepa-country' => $importJournal->getMetaString('sepa-country'),
|
||||
'sepa-ep' => $importJournal->getMetaString('sepa-ep'),
|
||||
'sepa-ci' => $importJournal->getMetaString('sepa-ci'),
|
||||
'transactions' => [
|
||||
// single transaction:
|
||||
[
|
||||
'description' => null,
|
||||
'amount' => $amount,
|
||||
'currency_id' => intval($currencyId),
|
||||
'currency_code' => null,
|
||||
'foreign_amount' => $foreignAmount,
|
||||
'foreign_currency_id' => $foreignCurrencyId,
|
||||
'foreign_currency_code' => null,
|
||||
'budget_id' => is_null($budget) ? null : $budget->id,
|
||||
'budget_name' => null,
|
||||
'category_id' => is_null($category) ? null : $category->id,
|
||||
'category_name' => null,
|
||||
'source_id' => $source->id,
|
||||
'source_name' => null,
|
||||
'destination_id' => $destination->id,
|
||||
'destination_name' => null,
|
||||
'reconciled' => false,
|
||||
'identifier' => 0,
|
||||
],
|
||||
],
|
||||
];
|
||||
$factoryJournal = $this->factory->create($data);
|
||||
$this->journals->push($factoryJournal);
|
||||
} catch (FireflyException $e) {
|
||||
Log::error(sprintf('Could not use factory to store journal: %s', $e->getMessage()));
|
||||
Log::error($e->getTraceAsString());
|
||||
}
|
||||
|
||||
// set notes for journal:
|
||||
$dbNote = new Note();
|
||||
$dbNote->noteable()->associate($journal);
|
||||
$dbNote->text = trim($importJournal->notes);
|
||||
$dbNote->save();
|
||||
|
||||
// set journal completed:
|
||||
$journal->completed = true;
|
||||
$journal->save();
|
||||
$this->addStep();
|
||||
$this->addStep();
|
||||
|
||||
// run rules if config calls for it:
|
||||
if (true === $this->applyRules) {
|
||||
Log::info('Will apply rules to this journal.');
|
||||
$this->applyRules($journal);
|
||||
$this->applyRules($factoryJournal);
|
||||
}
|
||||
Preferences::setForUser($this->job->user, 'lastActivity', microtime());
|
||||
|
||||
@@ -268,7 +305,7 @@ class ImportStorage
|
||||
// match bills if config calls for it.
|
||||
if (true === $this->matchBills) {
|
||||
Log::info('Will match bills.');
|
||||
$this->matchBills($journal);
|
||||
$this->matchBills($factoryJournal);
|
||||
}
|
||||
|
||||
if (!(true === $this->matchBills)) {
|
||||
@@ -276,9 +313,14 @@ class ImportStorage
|
||||
}
|
||||
$this->addStep();
|
||||
|
||||
$this->journals->push($journal);
|
||||
$this->journals->push($factoryJournal);
|
||||
|
||||
Log::info(sprintf('Imported new journal #%d: "%s", amount %s %s.', $journal->id, $journal->description, $journal->transactionCurrency->code, $amount));
|
||||
Log::info(
|
||||
sprintf(
|
||||
'Imported new journal #%d: "%s", amount %s %s.', $factoryJournal->id, $factoryJournal->description, $factoryJournal->transactionCurrency->code,
|
||||
$amount
|
||||
)
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user