First working version, very beta.

This commit is contained in:
James Cole
2015-07-05 06:59:05 +02:00
parent 1069db3c13
commit 1dc6d8de40
4 changed files with 183 additions and 53 deletions

View File

@@ -11,6 +11,7 @@ namespace FireflyIII\Helpers\Csv\Converter;
use Auth; use Auth;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use Log;
/** /**
* Class AccountIban * Class AccountIban
@@ -36,9 +37,13 @@ class AccountIban extends BasicConverter implements ConverterInterface
'name' => $this->value, 'name' => $this->value,
//'iban' => $this->value, //'iban' => $this->value,
'user_id' => Auth::user()->id, 'user_id' => Auth::user()->id,
'account_type_id' => $accountType->id 'account_type_id' => $accountType->id,
'active' => true,
] ]
); );
if ($account->getErrors()->count() > 0) {
Log::error('Create or find asset account: ' . json_encode($account->getErrors()->all()));
}
} }
return $account; return $account;

View File

@@ -0,0 +1,21 @@
<?php
namespace FireflyIII\Helpers\Csv\Converter;
/**
* Class Description
*
* @package FireflyIII\Helpers\Csv\Converter
*/
class Description extends BasicConverter implements ConverterInterface
{
/**
* @return mixed
*/
public function convert()
{
return trim($this->data['description'] . ' ' . $this->value);
}
}

View File

@@ -3,10 +3,18 @@
namespace FireflyIII\Helpers\Csv; namespace FireflyIII\Helpers\Csv;
use App; use App;
use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use Config; use Config;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Csv\Converter\ConverterInterface; use FireflyIII\Helpers\Csv\Converter\ConverterInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Illuminate\Support\MessageBag;
use Log;
/** /**
* Class Importer * Class Importer
@@ -18,13 +26,12 @@ class Importer
/** @var Data */ /** @var Data */
protected $data; protected $data;
/** @var array */
protected $errors;
/** @var array */ /** @var array */
protected $map; protected $map;
/** @var array */ /** @var array */
protected $mapped; protected $mapped;
/** @var array */ /** @var array */
protected $roles; protected $roles;
@@ -48,22 +55,29 @@ class Importer
$this->map = $this->data->getMap(); $this->map = $this->data->getMap();
$this->roles = $this->data->getRoles(); $this->roles = $this->data->getRoles();
$this->mapped = $this->data->getMapped(); $this->mapped = $this->data->getMapped();
foreach ($this->data->getReader() as $row) { foreach ($this->data->getReader() as $index => $row) {
$this->importRow($row); $result = $this->importRow($row);
if (!($result === true)) {
$this->errors[$index] = $result;
Log::error('ImportRow: ' . $result);
}
} }
return count($this->errors);
} }
/** /**
* @param $row * @param $row
* *
* @throws FireflyException * @throws FireflyException
* @return string|bool
*/ */
protected function importRow($row) protected function importRow($row)
{ {
/* /*
* These fields are necessary to create a new transaction journal. Some are optional: * These fields are necessary to create a new transaction journal. Some are optional:
*/ */
$data = $this->getFiller(); $data = $this->getFiller();
foreach ($row as $index => $value) { foreach ($row as $index => $value) {
$role = isset($this->roles[$index]) ? $this->roles[$index] : '_ignore'; $role = isset($this->roles[$index]) ? $this->roles[$index] : '_ignore';
$class = Config::get('csv.roles.' . $role . '.converter'); $class = Config::get('csv.roles.' . $role . '.converter');
@@ -91,37 +105,19 @@ class Importer
$data[$field] = $converter->convert(); $data[$field] = $converter->convert();
// } // }
// case 'description':
// $data['description'] .= ' ' . $value;
// break;
// case '_ignore':
// ignore! (duh)
// break;
// case 'account-iban':
// $data['asset-account'] = $this->findAssetAccount($index, $value);
// break;
// case 'currency-code':
// $data['currency'] = $this->findCurrency($index, $value, $role);
// break;
// case 'date-transaction':
// $data['date'] = $this->parseDate($value);
// break;
// case 'rabo-debet-credit':
// $data['amount-modifier'] = $this->parseRaboDebetCredit($value);
// break;
// default:
// throw new FireflyException('Cannot process row of type "' . $role . '".');
// break;
} }
$data = $this->postProcess($data); $data = $this->postProcess($data);
var_dump($data); $result = $this->validateData($data);
if ($result === true) {
$result = $this->createTransactionJournal($data);
} else {
Log::error('Validator: ' . $result);
}
if ($result instanceof TransactionJournal) {
return true;
}
return 'Not a journal.';
exit;
} }
@@ -131,13 +127,16 @@ class Importer
protected function getFiller() protected function getFiller()
{ {
return [ return [
'description' => '', 'description' => '',
'asset-account' => null, 'asset-account' => null,
'date' => null, 'opposing-account' => '',
'currency' => null, 'opposing-account-object' => null,
'amount' => null, 'date' => null,
'amount-modifier' => 1, 'currency' => null,
'ignored' => null, 'amount' => null,
'amount-modifier' => 1,
'ignored' => null,
'date-rent' => null,
]; ];
} }
@@ -149,12 +148,109 @@ class Importer
*/ */
protected function postProcess(array $data) protected function postProcess(array $data)
{ {
bcscale(2);
$data['description'] = trim($data['description']); $data['description'] = trim($data['description']);
$data['amount'] = bcmul($data['amount'], $data['amount-modifier']);
if ($data['amount'] < 0) {
// create expense account:
$accountType = AccountType::where('type', 'Expense account')->first();
} else {
// create revenue account:
$accountType = AccountType::where('type', 'Revenue account')->first();
}
$data['opposing-account-object'] = Account::firstOrCreateEncrypted(
[
'user_id' => Auth::user()->id,
'name' => ucwords($data['opposing-account']),
'account_type_id' => $accountType->id,
'active' => 1,
]
);
return $data; return $data;
} }
/**
* @param $data
*
* @return bool|string
*/
protected function validateData($data)
{
if (is_null($data['date']) && is_null($data['date-rent'])) {
return 'No date value for this row.';
}
if (strlen($data['description']) == 0) {
return 'No valid description';
}
if (is_null($data['opposing-account-object'])) {
return 'Opposing account is null';
}
return true;
}
/**
* @param array $data
*
* @return static
*/
protected function createTransactionJournal(array $data)
{
bcscale(2);
$date = $data['date'];
if (is_null($data['date'])) {
$date = $data['date-rent'];
}
if ($data['amount'] < 0) {
$transactionType = TransactionType::where('type', 'Withdrawal')->first();
} else {
$transactionType = TransactionType::where('type', 'Deposit')->first();
}
$errors = new MessageBag;
$journal = TransactionJournal::create(
[
'user_id' => Auth::user()->id,
'transaction_type_id' => $transactionType->id,
'bill_id' => null,
'transaction_currency_id' => $data['currency']->id,
'description' => $data['description'],
'completed' => 0,
'date' => $date,
]
);
$errors = $journal->getErrors()->merge($errors);
if ($journal->getErrors()->count() == 0) {
// create both transactions:
$transaction = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $data['asset-account']->id,
'amount' => $data['amount']
]
);
$errors = $transaction->getErrors()->merge($errors);
$transaction = Transaction::create(
[
'transaction_journal_id' => $journal->id,
'account_id' => $data['opposing-account-object']->id,
'amount' => bcmul($data['amount'], -1)
]
);
$errors = $transaction->getErrors()->merge($errors);
}
if($errors->count() == 0) {
$journal->completed = 1;
$journal->save();
}
return $journal;
}
/** /**
* @param Data $data * @param Data $data
*/ */

View File

@@ -34,8 +34,10 @@ return [
'mappable' => true, 'mappable' => true,
], ],
'description' => [ 'description' => [
'name' => 'Description', 'name' => 'Description',
'mappable' => false, 'mappable' => false,
'converter' => 'Description',
'field' => 'description',
], ],
'date-transaction' => [ 'date-transaction' => [
'name' => 'Date', 'name' => 'Date',
@@ -44,8 +46,8 @@ return [
'field' => 'date', 'field' => 'date',
], ],
'date-rent' => [ 'date-rent' => [
'name' => 'Rent calculation date', 'name' => 'Rent calculation date',
'mappable' => false, 'mappable' => false,
'converter' => 'Date', 'converter' => 'Date',
'field' => 'date-rent', 'field' => 'date-rent',
], ],
@@ -114,16 +116,22 @@ return [
'field' => 'amount', 'field' => 'amount',
], ],
'sepa-ct-id' => [ 'sepa-ct-id' => [
'name' => 'SEPA Credit Transfer end-to-end ID', 'name' => 'SEPA Credit Transfer end-to-end ID',
'mappable' => false, 'mappable' => false,
'converter' => 'Description',
'field' => 'description',
], ],
'sepa-ct-op' => [ 'sepa-ct-op' => [
'name' => 'SEPA Credit Transfer opposing account', 'name' => 'SEPA Credit Transfer opposing account',
'mappable' => false, 'mappable' => false,
'converter' => 'Description',
'field' => 'description',
], ],
'sepa-db' => [ 'sepa-db' => [
'name' => 'SEPA Direct Debet', 'name' => 'SEPA Direct Debet',
'mappable' => false, 'mappable' => false,
'converter' => 'Description',
'field' => 'description',
], ],
] ]
]; ];