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;
}

View File

@@ -9,18 +9,14 @@
namespace FireflyIII\Http\Controllers;
use App;
use Carbon\Carbon;
use Config;
use Crypt;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Csv\Data;
use FireflyIII\Helpers\Csv\Importer;
use FireflyIII\Helpers\Csv\WizardInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionCurrency;
use Illuminate\Http\Request;
use Input;
use League\Csv\Reader;
use Log;
use Redirect;
use Session;
use View;
@@ -80,7 +76,7 @@ class CsvController extends Controller
$map = $this->data->getMap();
for ($i = 1; $i <= $count; $i++) {
$headers[] = trans('firefly.csv_row') . ' #' . $i;
$headers[] = trans('firefly.csv_column') . ' #' . $i;
}
if ($this->data->getHasHeaders()) {
$headers = $firstRow;
@@ -157,12 +153,24 @@ class CsvController extends Controller
Session::forget('csv-roles');
Session::forget('csv-mapped');
// get values which are yet unsaveable or unmappable:
$unsupported = [];
foreach (Config::get('csv.roles') as $role) {
if (!isset($role['converter'])) {
$unsupported[] = trans('firefly.csv_unsupported_value', ['columnRole' => $role['name']]);
}
if ($role['mappable'] === true && !isset($role['mapper'])) {
$unsupported[] = trans('firefly.csv_unsupported_map', ['columnRole' => $role['name']]);
}
}
sort($unsupported);
// can actually upload?
$uploadPossible = is_writable(storage_path('upload'));
$path = storage_path('upload');
return view('csv.index', compact('subTitle', 'uploadPossible', 'path'));
return view('csv.index', compact('subTitle', 'uploadPossible', 'path','unsupported'));
}
/**
@@ -209,7 +217,8 @@ class CsvController extends Controller
* Or simply start processing.
*/
return Redirect::route('csv.process');
// proceed to download config
return Redirect::route('csv.download-config-page');
}
@@ -284,104 +293,21 @@ class CsvController extends Controller
return Redirect::route('csv.index');
}
//
Log::debug('Created importer');
$importer = new Importer;
$importer->setData($this->data);
try {
$importer->run();
} catch (FireflyException $e) {
Log::error('Catch error: ' . $e->getMessage());
return view('error', ['message' => $e->getMessage()]);
}
Log::debug('Done importing!');
echo 'display result';
exit;
// loop the original file again:
$content = file_get_contents(Session::get('csv-file'));
$hasHeaders = Session::get('csv-has-headers');
$reader = Reader::createFromString(Crypt::decrypt($content));
// dump stuff
$dateFormat = Session::get('csv-date-format');
$roles = Session::get('csv-roles');
$mapped = Session::get('csv-mapped');
/*
* Loop over the CSV and collect mappable data:
*/
foreach ($reader as $index => $row) {
if (($hasHeaders && $index > 1) || !$hasHeaders) {
// this is the data we need to store the new transaction:
$amount = 0;
$amountModifier = 1;
$description = '';
$assetAccount = null;
$opposingAccount = null;
$currency = null;
$date = null;
foreach ($row as $index => $value) {
if (isset($roles[$index])) {
switch ($roles[$index]) {
default:
throw new FireflyException('Cannot process role "' . $roles[$index] . '"');
break;
case 'account-iban':
// find ID in "mapped" (if present).
if (isset($mapped[$index])) {
$searchID = $mapped[$index][$value];
$assetAccount = Account::find($searchID);
} else {
// create account
}
break;
case 'opposing-name':
// don't know yet if its going to be a
// revenue or expense account.
$opposingAccount = $value;
break;
case 'currency-code':
// find ID in "mapped" (if present).
if (isset($mapped[$index])) {
$searchValue = $mapped[$index][$value];
$currency = TransactionCurrency::whereCode($searchValue);
} else {
// create account
}
break;
case 'date-transaction':
// unmappable:
$date = Carbon::createFromFormat($dateFormat, $value);
break;
case 'rabo-debet-credet':
if ($value == 'D') {
$amountModifier = -1;
}
break;
case 'amount':
$amount = $value;
break;
case 'description':
$description .= ' ' . $value;
break;
case 'sepa-ct-id':
$description .= ' ' . $value;
break;
}
}
}
// do something with all this data:
// do something.
var_dump($row);
}
}
}
/**