CSV importer now indicates the problems it has.

This commit is contained in:
James Cole
2015-07-05 08:45:05 +02:00
parent f8936210cf
commit 540dde135e
13 changed files with 312 additions and 297 deletions

View File

@@ -37,18 +37,6 @@ class Importer
/** @var array */
protected $roles;
/**
* @param $value
*/
public function parseRaboDebetCredit($value)
{
if ($value == 'D') {
return -1;
}
return 1;
}
/**
*
*/
@@ -58,10 +46,11 @@ class Importer
$this->roles = $this->data->getRoles();
$this->mapped = $this->data->getMapped();
foreach ($this->data->getReader() as $index => $row) {
Log::debug('Now at row ' . $index);
$result = $this->importRow($row);
if (!($result === true)) {
Log::error('Caught error at row #' . $index . ': ' . $result);
$this->errors[$index] = $result;
Log::error('ImportRow: ' . $result);
}
}
@@ -96,16 +85,10 @@ class Importer
$converter->setData($data); // the complete array so far.
$converter->setField($field);
$converter->setIndex($index);
$converter->setMapped($this->mapped);
$converter->setValue($value);
$converter->setRole($role);
// if (is_array($field)) {
// $convertResult = $converter->convert();
// foreach ($field as $fieldName) {
// $data[$fieldName] = $convertResult[$fieldName];
// }
// } else {
$data[$field] = $converter->convert();
// }
}
$data = $this->postProcess($data, $row);
@@ -164,6 +147,10 @@ class Importer
$accountType = AccountType::where('type', 'Revenue account')->first();
}
if(strlen($data['description']) == 0) {
$data['description'] = trans('firefly.csv_empty_description');
}
// do bank specific fixes:
$specifix = new Specifix();

View File

@@ -1,26 +1,29 @@
<?php
/**
* Created by PhpStorm.
* User: sander
* Date: 05/07/15
* Time: 08:35
*/
namespace FireflyIII\Helpers\Csv;
namespace FireflyIII\Helpers\Csv\Mapper;
use Auth;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionCurrency;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Class DataGrabber
* Class AssetAccount
*
* Class dedicated to retreiving all sorts of data related to the CSV import.
*
* @package FireflyIII\Helpers\Csv
* @package FireflyIII\Helpers\Csv\Mapper
*/
class DataGrabber
class AssetAccount implements MapperInterface
{
/**
* @return array
*/
public function getAssetAccounts()
public function getMap()
{
$result = Auth::user()->accounts()->with(
['accountmeta' => function (HasMany $query) {
@@ -36,19 +39,4 @@ class DataGrabber
return $list;
}
/**
* @return array
*/
public function getCurrencies()
{
$currencies = TransactionCurrency::get();
$list = [];
foreach ($currencies as $currency) {
$list[$currency->id] = $currency->name . ' (' . $currency->code . ')';
}
return $list;
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace FireflyIII\Helpers\Csv\Mapper;
/**
* Interface MapperInterface
*
* @package FireflyIII\Helpers\Csv\Mapper
*/
interface MapperInterface
{
/**
* @return array
*/
public function getMap();
}

View File

@@ -0,0 +1,28 @@
<?php
namespace FireflyIII\Helpers\Csv\Mapper;
use FireflyIII\Models\TransactionCurrency as TC;
/**
* Class TransactionCurrency
*
* @package FireflyIII\Helpers\Csv\Mapper
*/
class TransactionCurrency implements MapperInterface
{
/**
* @return array
*/
public function getMap()
{
$currencies = TC::get();
$list = [];
foreach ($currencies as $currency) {
$list[$currency->id] = $currency->name . ' (' . $currency->code . ')';
}
return $list;
}
}

View File

@@ -1,11 +1,14 @@
<?php
namespace FireflyIII\Helpers\Csv;
use App;
use Auth;
use Config;
use Crypt;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Csv\Mapper\MapperInterface;
use League\Csv\Reader;
use ReflectionException;
use Session;
/**
@@ -126,34 +129,24 @@ class Wizard implements WizardInterface
*/
public function showOptions(array $map)
{
$dataGrabber = new DataGrabber;
$options = [];
foreach ($map as $index => $columnRole) {
/*
* Depending on the column role, get the relevant data from the database.
* This needs some work to be optimal.
*/
switch ($columnRole) {
default:
throw new FireflyException('Cannot map field of type "' . $columnRole . '".');
break;
case 'account-iban':
$set = $dataGrabber->getAssetAccounts();
break;
case 'currency-code':
$set = $dataGrabber->getCurrencies();
break;
$mapper = Config::get('csv.roles.' . $columnRole . '.mapper');
if (is_null($mapper)) {
throw new FireflyException('Cannot map field of type "' . $columnRole . '".');
}
/*
* Make select list kind of thing:
*/
$class = 'FireflyIII\Helpers\Csv\Mapper\\' . $mapper;
try {
/** @var MapperInterface $mapObject */
$mapObject = App::make($class);
} catch (ReflectionException $e) {
throw new FireflyException('Column "' . $columnRole . '" cannot be mapped because class ' . $mapper . ' does not exist.');
}
$set = $mapObject->getMap();
$options[$index] = $set;
}
return $options;
}