2016-07-16 00:58:25 -05:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-24 11:47:55 -05:00
|
|
|
* AccountId.php
|
2016-07-16 00:58:25 -05:00
|
|
|
* 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-23 14:37:06 -05:00
|
|
|
use FireflyIII\Crud\Account\AccountCrudInterface;
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use Log;
|
2016-07-16 00:58:25 -05:00
|
|
|
|
|
|
|
/**
|
2016-07-24 11:47:55 -05:00
|
|
|
* Class AccountId
|
2016-07-16 00:58:25 -05:00
|
|
|
*
|
|
|
|
* @package FireflyIII\Import\Converter
|
|
|
|
*/
|
2016-07-24 11:47:55 -05:00
|
|
|
class AccountId extends BasicConverter implements ConverterInterface
|
2016-07-16 00:58:25 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
2016-07-23 14:37:06 -05:00
|
|
|
* @return Account
|
2016-07-16 00:58:25 -05:00
|
|
|
*/
|
|
|
|
public function convert($value)
|
|
|
|
{
|
2016-07-23 14:37:06 -05:00
|
|
|
$value = intval(trim($value));
|
|
|
|
Log::debug('Going to convert using AssetAccountId', ['value' => $value]);
|
|
|
|
|
|
|
|
if ($value === 0) {
|
|
|
|
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]);
|
|
|
|
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not mapped? Still try to find it first:
|
|
|
|
$account = $repository->find($value);
|
|
|
|
if (!is_null($account->id)) {
|
|
|
|
Log::debug('Found account by ID ', ['id' => $account->id]);
|
|
|
|
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not really happen. If the ID does not match FF, what is FF supposed to do?
|
|
|
|
return new Account;
|
2016-07-16 00:58:25 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|