2016-07-16 01:25:39 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OpposingAccountNumber.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Import\Converter;
|
|
|
|
|
2016-07-24 11:47:55 -05:00
|
|
|
use FireflyIII\Crud\Account\AccountCrudInterface;
|
2016-07-16 01:25:39 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-07-24 11:47:55 -05:00
|
|
|
use FireflyIII\Models\Account;
|
2016-08-10 11:49:16 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2016-07-24 11:47:55 -05:00
|
|
|
use Log;
|
2016-07-16 01:25:39 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class OpposingAccountNumber
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Import\Converter
|
|
|
|
*/
|
|
|
|
class OpposingAccountNumber extends BasicConverter implements ConverterInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
2016-07-24 11:47:55 -05:00
|
|
|
* @return Account
|
2016-07-16 01:25:39 -05:00
|
|
|
*/
|
|
|
|
public function convert($value)
|
|
|
|
{
|
2016-07-24 11:47:55 -05:00
|
|
|
$value = trim($value);
|
|
|
|
Log::debug('Going to convert using OpposingAccountNumber', ['value' => $value]);
|
|
|
|
|
|
|
|
if (strlen($value) === 0) {
|
2016-07-29 14:40:58 -05:00
|
|
|
$this->setCertainty(0);
|
2016-07-24 11:47:55 -05:00
|
|
|
return new Account;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var AccountCrudInterface $repository */
|
|
|
|
$repository = app(AccountCrudInterface::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]);
|
2016-07-29 14:40:58 -05:00
|
|
|
$this->setCertainty(100);
|
2016-07-24 11:47:55 -05:00
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not mapped? Still try to find it first:
|
|
|
|
$account = $repository->findByAccountNumber($value, []);
|
|
|
|
if (!is_null($account->id)) {
|
2016-07-29 14:40:58 -05:00
|
|
|
Log::debug('Found account by number', ['id' => $account->id]);
|
|
|
|
$this->setCertainty(50);
|
2016-07-24 11:47:55 -05:00
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
|
2016-08-10 11:49:16 -05:00
|
|
|
// try to find by the name we would give it:
|
|
|
|
$accountName = 'Import account with number ' . e($value);
|
|
|
|
$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-07-24 11:47:55 -05:00
|
|
|
|
|
|
|
$account = $repository->store(
|
2016-08-10 11:49:16 -05:00
|
|
|
['name' => $accountName, 'openingBalance' => 0, 'iban' => null, 'user' => $this->user->id,
|
|
|
|
'accountType' => 'import',
|
|
|
|
'virtualBalance' => 0, 'accountNumber' => $value, 'active' => true]
|
2016-07-24 11:47:55 -05:00
|
|
|
);
|
2016-07-29 14:40:58 -05:00
|
|
|
$this->setCertainty(100);
|
2016-07-24 11:47:55 -05:00
|
|
|
return $account;
|
2016-07-16 01:25:39 -05:00
|
|
|
|
|
|
|
}
|
2016-08-12 08:10:03 -05:00
|
|
|
}
|