Files
firefly-iii/app/Import/Converter/OpposingAccountNumber.php

91 lines
2.6 KiB
PHP
Raw Normal View History

2016-07-16 08:25:39 +02:00
<?php
/**
* OpposingAccountNumber.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
2016-07-16 08:25:39 +02:00
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2016-10-10 07:12:39 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Log;
2016-07-16 08:25:39 +02:00
/**
* Class OpposingAccountNumber
*
* @package FireflyIII\Import\Converter
*/
class OpposingAccountNumber extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return Account
2016-07-16 08:25:39 +02:00
*/
public function convert($value)
{
$value = trim($value);
Log::debug('Going to convert using OpposingAccountNumber', ['value' => $value]);
if (strlen($value) === 0) {
$this->setCertainty(0);
2016-08-26 08:21:31 +02:00
return new Account;
}
2016-10-10 07:12:39 +02:00
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class, [$this->user]);
if (isset($this->mapping[$value])) {
Log::debug('Found account in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$account = $repository->find(intval($this->mapping[$value]));
if (!is_null($account->id)) {
Log::debug('Found account by ID', ['id' => $account->id]);
$this->setCertainty(100);
2016-08-26 08:21:31 +02:00
return $account;
}
}
// not mapped? Still try to find it first:
2016-10-10 07:14:01 +02:00
$account = $repository->findByAccountNumber($value, []);
if (!is_null($account->id)) {
Log::debug('Found account by number', ['id' => $account->id]);
$this->setCertainty(50);
2016-08-26 08:21:31 +02:00
return $account;
}
// try to find by the name we would give it:
$accountName = 'Import account with number ' . e($value);
2016-10-10 07:20:49 +02:00
$account = $repository->findByName($accountName, [AccountType::IMPORT]);
if (!is_null($account->id)) {
Log::debug('Found account by name', ['id' => $account->id]);
$this->setCertainty(50);
return $account;
}
2016-10-10 08:03:03 +02:00
$account = $repository->store(
['name' => $accountName, 'openingBalance' => 0, 'iban' => null, 'user' => $this->user->id,
'accountType' => 'import',
'virtualBalance' => 0, 'accountNumber' => $value, 'active' => true]
);
$this->setCertainty(100);
2016-08-26 08:21:31 +02:00
return $account;
2016-07-16 08:25:39 +02:00
}
2016-08-12 15:10:03 +02:00
}