Cleanup post processing.

This commit is contained in:
James Cole 2015-07-05 19:31:58 +02:00
parent 18b038d8ff
commit c4ef379d0e
9 changed files with 230 additions and 47 deletions

View File

@ -7,6 +7,7 @@ use Auth;
use Config;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Csv\Converter\ConverterInterface;
use FireflyIII\Helpers\Csv\PostProcessing\PostProcessorInterface;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
@ -171,33 +172,15 @@ class Importer
*/
protected function postProcess(array $data)
{
// fix two simple fields:
bcscale(2);
$data['description'] = trim($data['description']);
$data['amount'] = bcmul($data['amount'], $data['amount-modifier']);
if (strlen($data['description']) == 0) {
$data['description'] = trans('firefly.csv_empty_description');
$set = Config::get('csv.post_processors');
foreach ($set as $className) {
/** @var PostProcessorInterface $postProcessor */
$postProcessor = App::make('FireflyIII\Helpers\Csv\PostProcessing\\' . $className);
$postProcessor->setData($data);
$data = $postProcessor->process();
}
// fix currency
if (is_null($data['currency'])) {
$currencyPreference = Preferences::get('currencyPreference', 'EUR');
$data['currency'] = TransactionCurrency::whereCode($currencyPreference->data)->first();
}
// get bill id.
if (!is_null($data['bill'])) {
$data['bill-id'] = $data['bill']->id;
}
// opposing account can be difficult.
// get opposing account, which is quite complex:
$opposingAccount = new OpposingAccount($data);
$data['opposing-account-object'] = $opposingAccount->parse();
// do bank specific fixes:
// do bank specific fixes: TODO
// $specifix = new Specifix();
// $specifix->setData($data);

View File

@ -0,0 +1,35 @@
<?php
namespace FireflyIII\Helpers\Csv\PostProcessing;
/**
* Class Amount
*
* @package FireflyIII\Helpers\Csv\PostProcessing
*/
class Amount implements PostProcessorInterface
{
/** @var array */
protected $data;
/**
* @return array
*/
public function process()
{
bcscale(2);
$this->data['amount'] = bcmul($this->data['amount'], $this->data['amount-modifier']);
return $this->data;
}
/**
* @param array $data
*/
public function setData(array $data)
{
$this->data = $data;
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace FireflyIII\Helpers\Csv\PostProcessing;
/**
* Class Bill
*
* @package FireflyIII\Helpers\Csv\PostProcessing
*/
class Bill implements PostProcessorInterface
{
/** @var array */
protected $data;
/**
* @return array
*/
public function process()
{
// get bill id.
if (!is_null($this->data['bill'])) {
$this->data['bill-id'] = $this->data['bill']->id;
}
return $this->data;
}
/**
* @param array $data
*/
public function setData(array $data)
{
$this->data = $data;
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace FireflyIII\Helpers\Csv\PostProcessing;
use Preferences;
use FireflyIII\Models\TransactionCurrency;
/**
* Class Currency
*
* @package FireflyIII\Helpers\Csv\PostProcessing
*/
class Currency implements PostProcessorInterface
{
/** @var array */
protected $data;
/**
* @return array
*/
public function process()
{
// fix currency
if (is_null($this->data['currency'])) {
$currencyPreference = Preferences::get('currencyPreference', 'EUR');
$this->data['currency'] = TransactionCurrency::whereCode($currencyPreference->data)->first();
}
return $this->data;
}
/**
* @param array $data
*/
public function setData(array $data)
{
$this->data = $data;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace FireflyIII\Helpers\Csv\PostProcessing;
/**
* Class Description
*
* @package FireflyIII\Helpers\Csv\PostProcessing
*/
class Description implements PostProcessorInterface
{
/** @var array */
protected $data;
/**
* @return array
*/
public function process()
{
$this->data['description'] = trim($this->data['description']);
if (strlen($this->data['description']) == 0) {
$this->data['description'] = trans('firefly.csv_empty_description');
}
return $this->data;
}
/**
* @param array $data
*/
public function setData(array $data)
{
$this->data = $data;
}
}

View File

@ -1,65 +1,74 @@
<?php
namespace FireflyIII\Helpers\Csv;
use Auth;
namespace FireflyIII\Helpers\Csv\PostProcessing;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Auth;
/**
* Class OpposingAccount
*
* @package FireflyIII\Helpers\Csv
* @package FireflyIII\Helpers\Csv\PostProcessing
*/
class OpposingAccount
class OpposingAccount implements PostProcessorInterface
{
/** @var array */
protected $data;
/**
* @param array $data
* @return array
*/
public function __construct(array $data)
{
$this->data = $data;
}
/**
* @return \FireflyIII\Models\Account|null
*/
public function parse()
public function process()
{
// first priority. try to find the account based on ID,
// if any.
if ($this->data['opposing-account-id'] instanceof Account) {
$this->data['opposing-account-object'] = $this->data['opposing-account-id'];
return $this->data['opposing-account-id'];
return $this->data;
}
// second: try to find the account based on IBAN, if any.
if ($this->data['opposing-account-iban'] instanceof Account) {
return $this->data['opposing-account-iban'];
$this->data['opposing-account-object'] = $this->data['opposing-account-iban'];
return $this->data;
}
if (is_string($this->data['opposing-account-iban'])) {
return $this->parseIbanString();
$this->data['opposing-account-object'] = $this->parseIbanString();
return $this->data;
}
// third: try to find account based on name, if any.
if ($this->data['opposing-account-name'] instanceof Account) {
return $this->data['opposing-account-name'];
$this->data['opposing-account-object'] = $this->data['opposing-account-name'];
return $this->data;
}
if (is_string($this->data['opposing-account-name'])) {
return $this->parseNameString();
$this->data['opposing-account-object'] = $this->parseNameString();
return $this->data;
}
return null;
// if nothing, create expense/revenue, never asset. TODO
}
/**
* @param array $data
*/
public function setData(array $data)
{
$this->data = $data;
}
/**

View File

@ -0,0 +1,28 @@
<?php
/**
* Created by PhpStorm.
* User: sander
* Date: 05/07/15
* Time: 19:20
*/
namespace FireflyIII\Helpers\Csv\PostProcessing;
/**
* Interface PostProcessorInterface
*
* @package FireflyIII\Helpers\Csv\PostProcessing
*/
interface PostProcessorInterface {
/**
* @param array $data
*/
public function setData(array $data);
/**
* @return array
*/
public function process();
}

View File

@ -153,6 +153,8 @@ class CsvController extends Controller
Session::forget('csv-mapped');
Session::forget('csv-specifix');
// specifix TODO
// get values which are yet unsaveable or unmappable:
$unsupported = [];
foreach (Config::get('csv.roles') as $role) {

View File

@ -1,6 +1,17 @@
<?php
return [
'roles' => [
'specifix' => [
'rabo_description'
],
'post_processors' => [
'OpposingAccount',
'Description',
'Amount',
'Currency',
'Bill'
],
'roles' => [
'_ignore' => [
'name' => '(ignore this column)',
'mappable' => false,