2016-07-16 08:25:39 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CurrencySymbol.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* 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;
|
|
|
|
|
2016-07-24 18:47:55 +02:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
|
|
|
use Log;
|
2016-07-16 08:25:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CurrencySymbol
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Import\Converter
|
|
|
|
*/
|
|
|
|
class CurrencySymbol extends BasicConverter implements ConverterInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
2016-07-24 18:47:55 +02:00
|
|
|
* @return TransactionCurrency
|
2016-07-16 08:25:39 +02:00
|
|
|
*/
|
|
|
|
public function convert($value)
|
|
|
|
{
|
2016-07-24 18:47:55 +02:00
|
|
|
$value = trim($value);
|
|
|
|
Log::debug('Going to convert using CurrencySymbol', ['value' => $value]);
|
|
|
|
|
2016-07-30 16:29:04 +02:00
|
|
|
if (strlen($value) === 0) {
|
2016-07-29 21:40:58 +02:00
|
|
|
$this->setCertainty(0);
|
2016-08-26 08:21:31 +02:00
|
|
|
|
2016-07-24 18:47:55 +02: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 21:40:58 +02:00
|
|
|
$this->setCertainty(100);
|
2016-08-26 08:21:31 +02:00
|
|
|
|
2016-07-24 18:47:55 +02:00
|
|
|
return $currency;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not mapped? Still try to find it first:
|
|
|
|
$currency = $repository->findBySymbol($value);
|
|
|
|
if (!is_null($currency->id)) {
|
|
|
|
Log::debug('Found currency by symbol ', ['id' => $currency->id]);
|
2016-07-29 21:40:58 +02:00
|
|
|
$this->setCertainty(100);
|
2016-08-26 08:21:31 +02:00
|
|
|
|
2016-07-24 18:47:55 +02:00
|
|
|
return $currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new currency
|
|
|
|
$currency = $repository->store(
|
|
|
|
[
|
|
|
|
'name' => 'Currency ' . $value,
|
|
|
|
'code' => $value,
|
|
|
|
'symbol' => $value,
|
|
|
|
]
|
|
|
|
);
|
2016-07-29 21:40:58 +02:00
|
|
|
$this->setCertainty(100);
|
2016-07-24 18:47:55 +02:00
|
|
|
|
|
|
|
return $currency;
|
2016-07-16 08:25:39 +02:00
|
|
|
|
|
|
|
}
|
2016-08-12 15:10:03 +02:00
|
|
|
}
|