mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-29 02:11:12 -06:00
Cleanup post processing.
This commit is contained in:
parent
18b038d8ff
commit
c4ef379d0e
@ -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);
|
||||
|
35
app/Helpers/Csv/PostProcessing/Amount.php
Normal file
35
app/Helpers/Csv/PostProcessing/Amount.php
Normal 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;
|
||||
}
|
||||
}
|
37
app/Helpers/Csv/PostProcessing/Bill.php
Normal file
37
app/Helpers/Csv/PostProcessing/Bill.php
Normal 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;
|
||||
}
|
||||
}
|
40
app/Helpers/Csv/PostProcessing/Currency.php
Normal file
40
app/Helpers/Csv/PostProcessing/Currency.php
Normal 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;
|
||||
}
|
||||
}
|
38
app/Helpers/Csv/PostProcessing/Description.php
Normal file
38
app/Helpers/Csv/PostProcessing/Description.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
28
app/Helpers/Csv/PostProcessing/PostProcessorInterface.php
Normal file
28
app/Helpers/Csv/PostProcessing/PostProcessorInterface.php
Normal 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();
|
||||
}
|
@ -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) {
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user