2018-10-01 12:24:24 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace FireflyIII\Support\FinTS;
|
|
|
|
|
2018-10-03 06:57:17 -05:00
|
|
|
use Fhp\Model\SEPAAccount;
|
2018-10-01 12:24:24 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-10-03 06:56:53 -05:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
2018-10-01 12:24:24 -05:00
|
|
|
|
|
|
|
class FinTS
|
|
|
|
{
|
|
|
|
/** @var \Fhp\FinTs */
|
|
|
|
private $finTS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $config
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function __construct(array $config)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
!isset($config['fints_url']) or
|
|
|
|
!isset($config['fints_port']) or
|
|
|
|
!isset($config['fints_bank_code']) or
|
|
|
|
!isset($config['fints_username']) or
|
|
|
|
!isset($config['fints_password']))
|
|
|
|
throw new FireflyException(
|
|
|
|
"Constructed FinTS with incomplete config."
|
|
|
|
);
|
|
|
|
$this->finTS = new \Fhp\FinTs(
|
|
|
|
$config['fints_url'],
|
|
|
|
$config['fints_port'],
|
|
|
|
$config['fints_bank_code'],
|
|
|
|
$config['fints_username'],
|
2018-10-03 06:56:53 -05:00
|
|
|
Crypt::decrypt($config['fints_password'])
|
2018-10-01 12:24:24 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkConnection()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->finTS->getSEPAAccounts();
|
|
|
|
return true;
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
return $exception->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-03 06:57:17 -05:00
|
|
|
* @return SEPAAccount[]
|
2018-10-01 12:24:24 -05:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function getAccounts()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->finTS->getSEPAAccounts();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
throw new FireflyException($exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accountNumber
|
2018-10-03 06:57:17 -05:00
|
|
|
* @return SEPAAccount
|
2018-10-01 12:24:24 -05:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
2018-10-03 06:57:17 -05:00
|
|
|
public function getAccount(string $accountNumber)
|
2018-10-01 12:24:24 -05:00
|
|
|
{
|
|
|
|
$accounts = $this->getAccounts();
|
2018-10-03 06:57:17 -05:00
|
|
|
$filteredAccounts = array_filter($accounts, function (SEPAAccount $account) use ($accountNumber) {
|
2018-10-01 12:24:24 -05:00
|
|
|
return $account->getAccountNumber() == $accountNumber;
|
|
|
|
});
|
|
|
|
if (count($filteredAccounts) != 1) {
|
|
|
|
throw new FireflyException("Cannot find account with number " . $accountNumber);
|
|
|
|
}
|
|
|
|
return $filteredAccounts[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-03 06:57:17 -05:00
|
|
|
* @param SEPAAccount $account
|
2018-10-01 12:24:24 -05:00
|
|
|
* @param \DateTime $from
|
|
|
|
* @param \DateTIme $to
|
|
|
|
* @return \Fhp\Model\StatementOfAccount\StatementOfAccount|null
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
2018-10-03 06:57:17 -05:00
|
|
|
public function getStatementOfAccount(SEPAAccount $account, \DateTime $from, \DateTIme $to)
|
2018-10-01 12:24:24 -05:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->finTS->getStatementOfAccount($account, $from, $to);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
throw new FireflyException($exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|