mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-20 19:35:16 +00:00
Cleanup post processing.
This commit is contained in:
@@ -7,6 +7,7 @@ use Auth;
|
|||||||
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\Helpers\Csv\PostProcessing\PostProcessorInterface;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
@@ -171,33 +172,15 @@ class Importer
|
|||||||
*/
|
*/
|
||||||
protected function postProcess(array $data)
|
protected function postProcess(array $data)
|
||||||
{
|
{
|
||||||
// fix two simple fields:
|
$set = Config::get('csv.post_processors');
|
||||||
bcscale(2);
|
foreach ($set as $className) {
|
||||||
$data['description'] = trim($data['description']);
|
/** @var PostProcessorInterface $postProcessor */
|
||||||
$data['amount'] = bcmul($data['amount'], $data['amount-modifier']);
|
$postProcessor = App::make('FireflyIII\Helpers\Csv\PostProcessing\\' . $className);
|
||||||
|
$postProcessor->setData($data);
|
||||||
if (strlen($data['description']) == 0) {
|
$data = $postProcessor->process();
|
||||||
$data['description'] = trans('firefly.csv_empty_description');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fix currency
|
// do bank specific fixes: TODO
|
||||||
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:
|
|
||||||
|
|
||||||
// $specifix = new Specifix();
|
// $specifix = new Specifix();
|
||||||
// $specifix->setData($data);
|
// $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
|
<?php
|
||||||
|
|
||||||
namespace FireflyIII\Helpers\Csv;
|
namespace FireflyIII\Helpers\Csv\PostProcessing;
|
||||||
|
|
||||||
use Auth;
|
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
|
use Auth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OpposingAccount
|
* Class OpposingAccount
|
||||||
*
|
*
|
||||||
* @package FireflyIII\Helpers\Csv
|
* @package FireflyIII\Helpers\Csv\PostProcessing
|
||||||
*/
|
*/
|
||||||
class OpposingAccount
|
class OpposingAccount implements PostProcessorInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected $data;
|
protected $data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function __construct(array $data)
|
public function process()
|
||||||
{
|
|
||||||
$this->data = $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \FireflyIII\Models\Account|null
|
|
||||||
*/
|
|
||||||
public function parse()
|
|
||||||
{
|
{
|
||||||
// first priority. try to find the account based on ID,
|
// first priority. try to find the account based on ID,
|
||||||
// if any.
|
// if any.
|
||||||
if ($this->data['opposing-account-id'] instanceof Account) {
|
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.
|
// second: try to find the account based on IBAN, if any.
|
||||||
if ($this->data['opposing-account-iban'] instanceof Account) {
|
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'])) {
|
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.
|
// third: try to find account based on name, if any.
|
||||||
if ($this->data['opposing-account-name'] instanceof Account) {
|
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'])) {
|
if (is_string($this->data['opposing-account-name'])) {
|
||||||
return $this->parseNameString();
|
$this->data['opposing-account-object'] = $this->parseNameString();
|
||||||
|
|
||||||
|
return $this->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
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-mapped');
|
||||||
Session::forget('csv-specifix');
|
Session::forget('csv-specifix');
|
||||||
|
|
||||||
|
// specifix TODO
|
||||||
|
|
||||||
// get values which are yet unsaveable or unmappable:
|
// get values which are yet unsaveable or unmappable:
|
||||||
$unsupported = [];
|
$unsupported = [];
|
||||||
foreach (Config::get('csv.roles') as $role) {
|
foreach (Config::get('csv.roles') as $role) {
|
||||||
|
@@ -1,6 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
return [
|
return [
|
||||||
'roles' => [
|
'specifix' => [
|
||||||
|
'rabo_description'
|
||||||
|
],
|
||||||
|
'post_processors' => [
|
||||||
|
'OpposingAccount',
|
||||||
|
'Description',
|
||||||
|
'Amount',
|
||||||
|
'Currency',
|
||||||
|
'Bill'
|
||||||
|
|
||||||
|
],
|
||||||
|
'roles' => [
|
||||||
'_ignore' => [
|
'_ignore' => [
|
||||||
'name' => '(ignore this column)',
|
'name' => '(ignore this column)',
|
||||||
'mappable' => false,
|
'mappable' => false,
|
||||||
|
Reference in New Issue
Block a user