diff --git a/app/Import/ImportEntry.php b/app/Import/ImportEntry.php index 1cfc657669..8d5efc9c05 100644 --- a/app/Import/ImportEntry.php +++ b/app/Import/ImportEntry.php @@ -12,6 +12,10 @@ declare(strict_types = 1); namespace FireflyIII\Import; use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Models\Account; +use FireflyIII\Models\Bill; +use FireflyIII\Models\Budget; +use FireflyIII\Models\Category; use Log; /** @@ -21,10 +25,31 @@ use Log; */ class ImportEntry { + /** @var float */ public $amount; + /** @var Account */ + public $assetAccount; + /** @var int */ + public $assetAccountCertainty = 0; + /** @var Bill */ + public $bill; + /** @var int */ + public $billCertainty; + /** @var Budget */ + public $budget; + /** @var int */ + public $budgetCertainty; + /** @var Account */ + public $opposingAccount; + /** @var int */ + public $opposingAccountCertainty = 0; + /** @var Category */ + public $category; + /** @var int */ + public $categoryCertainty; /** * @param string $role @@ -51,6 +76,28 @@ class ImportEntry $this->setAmount($convertedValue); return; + case 'account-id': + case 'account-iban': + case 'account-name': + $this->setAssetAccount($convertedValue, $certainty); + break; + case 'opposing-number': + $this->setOpposingAccount($convertedValue, $certainty); + break; + case 'bill-id': + case 'bill-name': + $this->setBill($convertedValue, $certainty); + break; + case 'budget-id': + case 'budget-name': + $this->setObject('budget', 'budgetCertainty', $convertedValue, $certainty); + //$this->setBudget($convertedValue, $certainty); + break; + case 'category-id': + case 'category-name': + $this->setObject('category', 'categoryCertainty', $convertedValue, $certainty); + break; + } } @@ -61,4 +108,91 @@ class ImportEntry { $this->amount = $amount; } + + /** + * @param Account $account + * @param int $certainty + */ + private function setAssetAccount(Account $account, int $certainty) + { + if ($certainty > $this->assetAccountCertainty && !is_null($account->id)) { + Log::debug(sprintf('ImportEntry: Asset Account ID is now %d with certainty %d', $account->id, $certainty)); + $this->assetAccount = $account; + $this->assetAccountCertainty = $certainty; + + return; + } + Log::error(sprintf('Will not set asset account based on certainty %d (current certainty is %d) or NULL id.', $certainty, $this->assetAccountCertainty)); + } + + /** + * @param Bill $bill + * @param int $certainty + */ + private function setBill(Bill $bill, int $certainty) + { + if ($certainty > $this->billCertainty && !is_null($bill->id)) { + Log::debug(sprintf('ImportEntry: Bill-ID is now %d with certainty %d', $bill->id, $certainty)); + $this->bill = $bill; + $this->billCertainty = $certainty; + + return; + } + Log::error(sprintf('Will not set bill based on certainty %d (current certainty is %d) or NULL id.', $certainty, $this->billCertainty)); + } + + /** + * @param Budget $budget + * @param int $certainty + */ + private function setBudget(Budget $budget, int $certainty) + { + if ($certainty > $this->budgetCertainty && !is_null($budget->id)) { + Log::debug(sprintf('ImportEntry: Budget-ID is now %d with certainty %d', $budget->id, $certainty)); + $this->budget = $budget; + $this->budgetCertainty = $certainty; + + return; + } + Log::error(sprintf('Will not set budget based on certainty %d (current certainty is %d) or NULL id.', $certainty, $this->budgetCertainty)); + } + + /** + * @param string $field + * @param string $cert + * @param $object + * @param int $certainty + */ + private function setObject(string $field, string $cert, $object, int $certainty) + { + if ($certainty > $this->$cert && !is_null($object->id)) { + Log::debug(sprintf('ImportEntry: %s ID is now %d with certainty %d', $field, $object->id, $certainty)); + $this->$field = $object; + $this->$cert = $certainty; + + return; + } + Log::error(sprintf('Will not set %s based on certainty %d (current certainty is %d) or NULL id.', $field, $certainty, $this->$cert)); + + } + + /** + * @param Account $account + * @param int $certainty + */ + private function setOpposingAccount(Account $account, int $certainty) + { + if ($certainty > $this->opposingAccountCertainty && !is_null($account->id)) { + Log::debug(sprintf('ImportEntry: Opposing Account ID is now %d with certainty %d', $account->id, $certainty)); + $this->assetAccount = $account; + $this->assetAccountCertainty = $certainty; + + return; + } + Log::error( + sprintf('Will not set opposing account based on certainty %d (current certainty is %d) or NULL id.', $certainty, $this->assetAccountCertainty) + ); + } + + } \ No newline at end of file