Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Refactor temporary account storage and fix a bug in the bunq import. #1607
James Cole3a427dd0f446
diff --git a/app/Http/Controllers/Import/JobStatusController.php b/app/Http/Controllers/Import/JobStatusController.phpindex d211001939..9d256a5817 100644--- a/app/Http/Controllers/Import/JobStatusController.php+++ b/app/Http/Controllers/Import/JobStatusController.php@@ -85,7 +85,7 @@ class JobStatusController extends Controller*/public function json(ImportJob $importJob): JsonResponse{- $count = \count($importJob->transactions);+ $count = $this->repository->countTransactions($importJob);$json = ['status' => $importJob->status,'errors' => $importJob->errors,diff --git a/app/Import/Storage/ImportArrayStorage.php b/app/Import/Storage/ImportArrayStorage.phpindex 9cc5ab6871..4d649883f9 100644--- a/app/Import/Storage/ImportArrayStorage.php+++ b/app/Import/Storage/ImportArrayStorage.php@@ -72,12 +72,12 @@ class ImportArrayStorage*/public function setImportJob(ImportJob $importJob): void{- $this->importJob = $importJob;- $this->countTransfers();-+ $this->importJob = $importJob;$this->repository = app(ImportJobRepositoryInterface::class);$this->repository->setUser($importJob->user);+ $this->countTransfers();+$this->journalRepos = app(JournalRepositoryInterface::class);$this->journalRepos->setUser($importJob->user);@@ -157,7 +157,9 @@ class ImportArrayStorage{Log::debug('Now in countTransfers()');/** @var array $array */- $array = $this->importJob->transactions;+ $array = $this->repository->getTransactions($this->importJob);++$count = 0;foreach ($array as $index => $transaction) {if (strtolower(TransactionType::TRANSFER) === strtolower($transaction['type'])) {@@ -418,7 +420,7 @@ class ImportArrayStorageprivate function storeArray(): Collection{/** @var array $array */- $array = $this->importJob->transactions;+ $array = $this->repository->getTransactions($this->importJob);$count = \count($array);$toStore = [];@@ -541,8 +543,9 @@ class ImportArrayStorageLog::debug(sprintf('Comparison is a hit! (%s)', $hits));// compare description:- Log::debug(sprintf('Comparing "%s" to "%s"', $description, $transfer->description));- if ($description !== $transfer->description) {+ $comparison = '(empty description)' === $transfer->description ? '' : $transfer->description;+ Log::debug(sprintf('Comparing "%s" to "%s" (original: "%s")', $description, $transfer->description, $comparison));+ if ($description !== $comparison) {continue; // @codeCoverageIgnore}++$hits;diff --git a/app/Models/ImportJob.php b/app/Models/ImportJob.phpindex 82de695919..b45502a5cf 100644--- a/app/Models/ImportJob.php+++ b/app/Models/ImportJob.php@@ -22,6 +22,7 @@ declare(strict_types=1);namespace FireflyIII\Models;+use Carbon\Carbon;use FireflyIII\User;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsTo;@@ -45,6 +46,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;* @property array $errors* @property array extended_status* @property int id+ * @property Carbon $created_at*/class ImportJob extends Model{diff --git a/app/Repositories/ImportJob/ImportJobRepository.php b/app/Repositories/ImportJob/ImportJobRepository.phpindex a6d6f54e46..2a9fec1634 100644--- a/app/Repositories/ImportJob/ImportJobRepository.php+++ b/app/Repositories/ImportJob/ImportJobRepository.php@@ -84,24 +84,34 @@ class ImportJobRepository implements ImportJobRepositoryInterface* @param array $transactions** @return ImportJob+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException*/public function appendTransactions(ImportJob $job, array $transactions): ImportJob{Log::debug(sprintf('Now in appendTransactions(%s)', $job->key));- $existingTransactions = $job->transactions;- if (!\is_array($existingTransactions)) {- $existingTransactions = [];- }- $new = array_merge($existingTransactions, $transactions);+ $existingTransactions = $this->getTransactions($job);+ $new = array_merge($existingTransactions, $transactions);Log::debug(sprintf('Old transaction count: %d', \count($existingTransactions)));Log::debug(sprintf('To be added transaction count: %d', \count($transactions)));Log::debug(sprintf('New count: %d', \count($new)));- $job->transactions = $new;+ $this->setTransactions($job, $new);+ return $job;+ }- $job->save();+ /**+ * @param ImportJob $job+ *+ * @return int+ */+ public function countTransactions(ImportJob $job): int+ {+ $info = $job->transactions ?? [];+ if (isset($info['count'])) {+ return (int)$info['count'];+ }- return $job;+ return 0;}/**@@ -201,6 +211,31 @@ class ImportJobRepository implements ImportJobRepositoryInterfacereturn [];}+ /**+ * Return transactions from attachment.+ *+ * @param ImportJob $job+ *+ * @return array+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException+ */+ public function getTransactions(ImportJob $job): array+ {+ // this will overwrite all transactions currently in the job.+ $disk = Storage::disk('upload');+ $filename = sprintf('%s-%s.crypt.json', $job->created_at->format('Ymd'), $job->key);+ $array = [];+ if ($disk->exists($filename)) {+ $json = Crypt::decrypt($disk->get($filename));+ $array = json_decode($json, true);+ }+ if (false === $array) {+ $array = [];+ }++ return $array;+ }+/*** @param ImportJob $job* @param array $configuration@@ -272,8 +307,17 @@ class ImportJobRepository implements ImportJobRepositoryInterface*/public function setTransactions(ImportJob $job, array $transactions): ImportJob{- $job->transactions = $transactions;+ // this will overwrite all transactions currently in the job.+ $disk = Storage::disk('upload');+ $filename = sprintf('%s-%s.crypt.json', $job->created_at->format('Ymd'), $job->key);+ $json = Crypt::encrypt(json_encode($transactions));++ // set count for easy access+ $array = ['count' => \count($transactions)];+ $job->transactions = $array;$job->save();+ // store file.+ $disk->put($filename, $json);return $job;}@@ -377,7 +421,7 @@ class ImportJobRepository implements ImportJobRepositoryInterface$fileObject->rewind();- if(0 === $file->getSize()) {+ if (0 === $file->getSize()) {throw new FireflyException('Cannot upload empty or non-existent file.');}@@ -390,7 +434,6 @@ class ImportJobRepository implements ImportJobRepositoryInterfacereturn new MessageBag;}-/*** @codeCoverageIgnore*diff --git a/app/Repositories/ImportJob/ImportJobRepositoryInterface.php b/app/Repositories/ImportJob/ImportJobRepositoryInterface.phpindex f63fd236bc..ad621010f1 100644--- a/app/Repositories/ImportJob/ImportJobRepositoryInterface.php+++ b/app/Repositories/ImportJob/ImportJobRepositoryInterface.php@@ -35,6 +35,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;*/interface ImportJobRepositoryInterface{+/*** Add message to job.*@@ -55,6 +56,13 @@ interface ImportJobRepositoryInterface*/public function appendTransactions(ImportJob $job, array $transactions): ImportJob;+ /**+ * @param ImportJob $job+ *+ * @return int+ */+ public function countTransactions(ImportJob $job): int;+/*** @param string $importProvider*@@ -96,6 +104,15 @@ interface ImportJobRepositoryInterface*/public function getExtendedStatus(ImportJob $job): array;+ /**+ * Return transactions from attachment.+ *+ * @param ImportJob $job+ *+ * @return array+ */+ public function getTransactions(ImportJob $job): array;+/*** @param ImportJob $job* @param array $configuration