2016-07-16 01:25:39 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CurrencyName.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\Models\TransactionCurrency;
|
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
|
|
|
use Log;
|
2016-07-16 01:25:39 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CurrencyName
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Import\Converter
|
|
|
|
*/
|
|
|
|
class CurrencyName extends BasicConverter implements ConverterInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
2016-07-24 11:47:55 -05:00
|
|
|
* @return TransactionCurrency
|
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 CurrencyName', ['value' => $value]);
|
|
|
|
|
|
|
|
if ($value === 0) {
|
2016-07-29 14:40:58 -05:00
|
|
|
$this->setCertainty(0);
|
2016-07-24 11:47:55 -05:00
|
|
|
return new TransactionCurrency;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var CurrencyRepositoryInterface $repository */
|
|
|
|
$repository = app(CurrencyRepositoryInterface::class, [$this->user]);
|
|
|
|
|
|
|
|
if (isset($this->mapping[$value])) {
|
|
|
|
Log::debug('Found currency in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
|
|
|
|
$currency = $repository->find(intval($this->mapping[$value]));
|
|
|
|
if (!is_null($currency->id)) {
|
|
|
|
Log::debug('Found currency by ID', ['id' => $currency->id]);
|
2016-07-29 14:40:58 -05:00
|
|
|
$this->setCertainty(100);
|
2016-07-24 11:47:55 -05:00
|
|
|
return $currency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not mapped? Still try to find it first:
|
|
|
|
$currency = $repository->findByName($value);
|
|
|
|
if (!is_null($currency->id)) {
|
|
|
|
Log::debug('Found currency by name ', ['id' => $currency->id]);
|
2016-07-29 14:40:58 -05:00
|
|
|
$this->setCertainty(100);
|
2016-07-24 11:47:55 -05:00
|
|
|
return $currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new currency
|
|
|
|
$currency = $repository->store(
|
|
|
|
[
|
|
|
|
'name' => $value,
|
|
|
|
'code' => strtoupper(substr($value, 0, 3)),
|
|
|
|
'symbol' => strtoupper(substr($value, 0, 1)),
|
|
|
|
]
|
|
|
|
);
|
2016-07-29 14:40:58 -05:00
|
|
|
$this->setCertainty(100);
|
2016-07-24 11:47:55 -05:00
|
|
|
|
|
|
|
return $currency;
|
2016-07-16 01:25:39 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|