mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-19 04:49:30 +00:00
Refactor and rename some import things.
This commit is contained in:
@@ -12,6 +12,9 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Import\Object;
|
||||
|
||||
|
||||
use FireflyIII\Import\Converter\ConverterInterface;
|
||||
use Steam;
|
||||
|
||||
class ImportTransaction
|
||||
{
|
||||
/** @var string */
|
||||
@@ -39,6 +42,39 @@ class ImportTransaction
|
||||
$this->modifiers[] = $modifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAmount(): string
|
||||
{
|
||||
// use converter:
|
||||
$this->amount = strval($this->parseAmount());
|
||||
|
||||
|
||||
// also apply modifiers:
|
||||
$this->amount = Steam::positive($this->amount);
|
||||
|
||||
// Can handle ING
|
||||
foreach ($this->modifiers as $modifier) {
|
||||
$class = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $modifier['role'])));
|
||||
/** @var ConverterInterface $converter */
|
||||
$converter = app($class);
|
||||
if ($converter->convert($modifier['value']) === -1) {
|
||||
$this->amount = Steam::negative($this->amount);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
*/
|
||||
public function setAmount(string $amount)
|
||||
{
|
||||
$this->amount = $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ImportCurrency
|
||||
*/
|
||||
@@ -55,14 +91,6 @@ class ImportTransaction
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
*/
|
||||
public function setAmount(string $amount)
|
||||
{
|
||||
$this->amount = $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*/
|
||||
@@ -87,4 +115,43 @@ class ImportTransaction
|
||||
$this->positive = $positive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
|
||||
* - Jamie Zawinski
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
private function parseAmount()
|
||||
{
|
||||
$value = $this->amount;
|
||||
$len = strlen($value);
|
||||
$decimalPosition = $len - 3;
|
||||
$decimal = null;
|
||||
|
||||
if (($len > 2 && $value{$decimalPosition} == '.') || ($len > 2 && strpos($value, '.') > $decimalPosition)) {
|
||||
$decimal = '.';
|
||||
}
|
||||
if ($len > 2 && $value{$decimalPosition} == ',') {
|
||||
$decimal = ',';
|
||||
}
|
||||
|
||||
// if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
|
||||
if ($decimal === '.') {
|
||||
$search = [',', ' '];
|
||||
$value = str_replace($search, '', $value);
|
||||
}
|
||||
if ($decimal === ',') {
|
||||
$search = ['.', ' '];
|
||||
$value = str_replace($search, '', $value);
|
||||
$value = str_replace(',', '.', $value);
|
||||
}
|
||||
if (is_null($decimal)) {
|
||||
// replace all:
|
||||
$search = ['.', ' ', ','];
|
||||
$value = str_replace($search, '', $value);
|
||||
}
|
||||
|
||||
return round(floatval($value), 12);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user