Files
firefly-iii/app/Helpers/Csv/PostProcessing/OpposingAccount.php

211 lines
5.7 KiB
PHP
Raw Normal View History

2015-07-05 18:18:44 +02:00
<?php
2015-07-05 19:31:58 +02:00
namespace FireflyIII\Helpers\Csv\PostProcessing;
2015-07-05 21:47:59 +02:00
use Auth;
2015-07-05 18:18:44 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2015-07-05 21:47:59 +02:00
use Log;
2015-07-05 19:57:44 +02:00
use Validator;
2015-07-05 18:18:44 +02:00
/**
* Class OpposingAccount
*
2015-07-05 19:31:58 +02:00
* @package FireflyIII\Helpers\Csv\PostProcessing
2015-07-05 18:18:44 +02:00
*/
2015-07-05 19:31:58 +02:00
class OpposingAccount implements PostProcessorInterface
2015-07-05 18:18:44 +02:00
{
2015-07-05 19:31:58 +02:00
2015-07-05 18:18:44 +02:00
/** @var array */
protected $data;
/**
2015-07-05 19:31:58 +02:00
* @return array
2015-07-05 18:18:44 +02:00
*/
2015-07-05 19:31:58 +02:00
public function process()
2015-07-05 18:18:44 +02:00
{
2015-07-09 19:23:49 +02:00
// three values:
// opposing-account-id, opposing-account-iban, opposing-account-name
2015-07-07 01:07:19 +02:00
$result = $this->checkIdNameObject();
if (!is_null($result)) {
return $result;
2015-07-05 18:18:44 +02:00
}
2015-07-05 19:31:58 +02:00
2015-07-07 01:07:19 +02:00
$result = $this->checkIbanString();
if (!is_null($result)) {
return $result;
2015-07-05 18:18:44 +02:00
}
2015-07-05 19:31:58 +02:00
2015-07-07 01:07:19 +02:00
$result = $this->checkNameString();
if (!is_null($result)) {
return $result;
2015-07-05 18:18:44 +02:00
}
2015-07-07 01:07:19 +02:00
return null;
}
/**
* @param array $data
*/
public function setData(array $data)
{
$this->data = $data;
}
/**
* @return array
*/
protected function checkIdNameObject()
{
if ($this->data['opposing-account-id'] instanceof Account) { // first priority. try to find the account based on ID, if any
2015-07-09 19:23:49 +02:00
Log::debug('OpposingAccountPostProcession: opposing-account-id is an Account.');
2015-07-07 01:07:19 +02:00
$this->data['opposing-account-object'] = $this->data['opposing-account-id'];
2015-07-05 19:31:58 +02:00
return $this->data;
2015-07-05 18:18:44 +02:00
}
2015-07-07 01:07:19 +02:00
if ($this->data['opposing-account-iban'] instanceof Account) { // second: try to find the account based on IBAN, if any.
2015-07-09 19:23:49 +02:00
Log::debug('OpposingAccountPostProcession: opposing-account-iban is an Account.');
2015-07-07 01:07:19 +02:00
$this->data['opposing-account-object'] = $this->data['opposing-account-iban'];
2015-07-05 19:31:58 +02:00
return $this->data;
2015-07-05 18:18:44 +02:00
}
return null;
2015-07-05 19:31:58 +02:00
}
/**
2015-07-07 01:07:19 +02:00
* @return array|null
2015-07-05 19:31:58 +02:00
*/
2015-07-07 01:07:19 +02:00
protected function checkIbanString()
2015-07-05 19:31:58 +02:00
{
2015-07-07 01:07:19 +02:00
$rules = ['iban' => 'iban'];
2015-07-09 19:23:49 +02:00
$iban = $this->data['opposing-account-iban'];
$check = ['iban' => $iban];
2015-07-07 01:07:19 +02:00
$validator = Validator::make($check, $rules);
2015-07-09 19:23:49 +02:00
if (is_string($iban) && strlen($iban) > 0 && !$validator->fails()) {
Log::debug('OpposingAccountPostProcession: opposing-account-iban is a string (******).');
2015-07-07 01:07:19 +02:00
$this->data['opposing-account-object'] = $this->parseIbanString();
return $this->data;
}
return null;
2015-07-05 18:18:44 +02:00
}
/**
* @return Account|null
*/
protected function parseIbanString()
{
// create by name and/or iban.
2015-07-07 01:07:19 +02:00
$accounts = Auth::user()->accounts()->get();
2015-07-05 18:18:44 +02:00
foreach ($accounts as $entry) {
if ($entry->iban == $this->data['opposing-account-iban']) {
2015-07-09 19:23:49 +02:00
Log::debug('OpposingAccountPostProcession: opposing-account-iban matches an Account.');
2015-07-05 18:18:44 +02:00
return $entry;
}
}
2015-07-07 01:07:19 +02:00
$account = $this->createAccount();
2015-07-09 19:23:49 +02:00
2015-07-07 01:07:19 +02:00
return $account;
}
/**
* @return Account|null
*/
protected function createAccount()
{
$accountType = $this->getAccountType();
2015-07-05 18:18:44 +02:00
// create if not exists:
2015-07-05 21:47:59 +02:00
$name = is_string($this->data['opposing-account-name']) && strlen($this->data['opposing-account-name']) > 0 ? $this->data['opposing-account-name']
: $this->data['opposing-account-iban'];
2015-07-05 18:18:44 +02:00
$account = Account::firstOrCreateEncrypted(
[
'user_id' => Auth::user()->id,
'account_type_id' => $accountType->id,
2015-07-05 21:47:59 +02:00
'name' => $name,
2015-07-05 18:18:44 +02:00
'iban' => $this->data['opposing-account-iban'],
'active' => true,
]
);
2015-07-09 19:23:49 +02:00
Log::debug('OpposingAccountPostProcession: created a new account.');
2015-07-05 18:18:44 +02:00
return $account;
}
/**
*
* @return AccountType
*/
protected function getAccountType()
{
// opposing account type:
if ($this->data['amount'] < 0) {
// create expense account:
return AccountType::where('type', 'Expense account')->first();
} else {
// create revenue account:
return AccountType::where('type', 'Revenue account')->first();
2015-07-05 21:47:59 +02:00
2015-07-05 18:18:44 +02:00
}
}
2015-07-07 01:07:19 +02:00
/**
* @return array|null
*/
protected function checkNameString()
{
if ($this->data['opposing-account-name'] instanceof Account) { // third: try to find account based on name, if any.
2015-07-09 19:23:49 +02:00
Log::debug('OpposingAccountPostProcession: opposing-account-name is an Account.');
2015-07-07 01:07:19 +02:00
$this->data['opposing-account-object'] = $this->data['opposing-account-name'];
return $this->data;
}
if (is_string($this->data['opposing-account-name'])) {
2015-07-09 19:23:49 +02:00
2015-07-07 01:07:19 +02:00
$this->data['opposing-account-object'] = $this->parseNameString();
return $this->data;
}
return null;
}
2015-07-05 18:18:44 +02:00
/**
* @return Account|null
*/
protected function parseNameString()
{
$accountType = $this->getAccountType();
$accounts = Auth::user()->accounts()->where('account_type_id', $accountType->id)->get();
foreach ($accounts as $entry) {
if ($entry->name == $this->data['opposing-account-name']) {
Log::debug('Found an account with this name (#' . $entry->id . ': ******)');
2015-07-05 21:47:59 +02:00
2015-07-05 18:18:44 +02:00
return $entry;
}
}
// create if not exists:
$account = Account::firstOrCreateEncrypted(
[
'user_id' => Auth::user()->id,
'account_type_id' => $accountType->id,
'name' => $this->data['opposing-account-name'],
'iban' => '',
'active' => true,
]
);
return $account;
}
2015-07-09 21:26:40 +02:00
}