Skip to content

Commit

Permalink
Support for foregin currency rates added
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Jan 29, 2025
1 parent 943bd1d commit 58d6102
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ STATEMENT_SAVE_DIR=/tmp/rb
CNB_CACHE=http://localhost/cnb-cache/
RATE_OFFSET=today
FIXED_RATE=25.1
FIXED_RATE_AMOUNT=1
POHODA_ICO=12345678
POHODA_URL=http://10.11.25.25:10010
Expand Down Expand Up @@ -103,8 +105,10 @@ DB_SETTINGS=encrypt=false
## Foregin Currency Transactions

If you have transactions in foreign currency, you can use `FIXED_RATE` to convert them to CZK using preconfigured fixed rate.
(for some currencies eg. 💴 you can use FIXED_RATE_AMOUNT=100)

Otherwise you can use `CNB_CACHE` to get actual rate from CNB.
Then you can also need to specify `RATE_OFFSET`=yesterday to get rate for previous day.
In this case you can also need to specify `RATE_OFFSET`=yesterday to get rate for previous day.

[For CNB currency rates you need to have running CNB cache server!](https://github.com/Spoje-NET/CNB-Cache)

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"bin": ["src/pohoda-raiffeisenbank-transactions.php", "src/pohoda-raiffeisenbank-setup.php"],
"require": {
"vgrem/php-spo": "^3",
"vitexsoftware/pohoda-connector": "^0.5",
"vitexsoftware/pohoda-connector": "dev-main",
"vitexsoftware/rbczpremiumapi": "dev-main",
"spojenet/pohoda-sql": "dev-main"
},
Expand Down
10 changes: 8 additions & 2 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
pohoda-raiffeisenbank (1.2.2) UNRELEASED; urgency=medium
pohoda-raiffeisenbank (1.3.0) UNRELEASED; urgency=medium

* Currency rates support added

-- vitex <[email protected]> Wed, 29 Jan 2025 12:38:29 +0100

pohoda-raiffeisenbank (1.2.2) jammy; urgency=medium

* better logging and exit codes

-- vitex <[email protected]> Tue, 28 Jan 2025 10:48:38 +0100
-- vitex <[email protected]> Wed, 29 Jan 2025 12:37:40 +0100

pohoda-raiffeisenbank (1.2.1) jammy; urgency=medium

Expand Down
59 changes: 35 additions & 24 deletions src/Pohoda/RaiffeisenBank/Statementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Statementor extends PohodaBankClient
public string $currency = 'CZK';
public string $account;
public string $statementLine = 'MAIN';
protected string $cnbCache = '';
protected float $fixedRate = 0;
protected int $fixedRateAmount = 1;
protected int $rateOffset = 0;
private \VitexSoftware\Raiffeisenbank\Statementor $obtainer;

/**
Expand All @@ -42,9 +46,6 @@ class Statementor extends PohodaBankClient
* @var array<string, string>
*/
private array $statementsPDF = [];
protected string $cnbCache = '';
private float $fixedRate = 0;
private int $rateOffset = 0;

/**
* Bank Statement Helper.
Expand All @@ -57,19 +58,20 @@ public function __construct(string $bankAccount, array $options = [])
parent::__construct($bankAccount, $options);
$this->setObjectName($bankAccount.'@'.$this->getObjectName());
$this->obtainer = new \VitexSoftware\Raiffeisenbank\Statementor($bankAccount);
$this->setupProperty($options,'currency','ACCOUNT_CURRENCY');
$this->setupProperty($options,'cnbCache','CNB_CACHE');
$this->setupFloatProperty($options,'fixedRate','FIXED_RATE');
$this->rateOffset = \Ease\Shared::cfg('RATE_OFFSET') == 'yesterday' ? 1 : 0;

if(($this->currency != 'CZK') && empty($this->fixedRate) && empty($this->cnbCache)){
$this->setupProperty($options, 'currency', 'ACCOUNT_CURRENCY');
$this->setupProperty($options, 'cnbCache', 'CNB_CACHE');
$this->setupFloatProperty($options, 'fixedRate', 'FIXED_RATE');
$this->setupIntProperty($options, 'fixedRateAmount', 'FIXED_RATE_AMOUNT');
$this->rateOffset = \Ease\Shared::cfg('RATE_OFFSET') === 'yesterday' ? 1 : 0;

if (($this->currency !== 'CZK') && empty($this->fixedRate) && empty($this->cnbCache)) {
throw new \InvalidArgumentException(_('No FIXED_RATE or CNB_CACHE specified for foregin currency'));
}

$this->statementsDir = \Ease\Shared::cfg('STATEMENT_SAVE_DIR', sys_get_temp_dir().'/rb');

if (file_exists($this->statementsDir) === false) {
$this->addStatusMessage(sprintf(_('Creating Statements directory'), $this->statementsDir, mkdir($this->statementsDir, 0777, true) ? 'success' : 'error' ));
$this->addStatusMessage(sprintf(_('Creating Statements directory'), $this->statementsDir, mkdir($this->statementsDir, 0777, true) ? 'success' : 'error'));
}
}

Expand Down Expand Up @@ -245,7 +247,10 @@ public function entryToPohoda(\SimpleXMLElement $entry): array
if (\array_key_exists('Ccy', $amountAttributes) && $amountAttributes['Ccy'] !== 'CZK') {
$data['foreignCurrency'] = ['priceSum' => abs((float) $entry->Amt)]; // "price3", "price3Sum", "price3VAT", "priceHigh", "priceHighSum", "priceHighVAT", "priceLow", "priceLowSum", "priceLowVAT", "priceNone", "round"
$data['foreignCurrency']['currency'] = $amountAttributes['Ccy'];
$rate = $this->getMovementRate(new \DateTime($data['datePayment']));
$rateInfo = $this->getRateInfo(new \DateTime($data['datePayment']));

$data['foreignCurrency']['rate'] = $rateInfo['rate'];
$data['foreignCurrency']['amount'] = $rateInfo['amount'];
} else {
$data['homeCurrency'] = ['priceNone' => abs((float) $entry->Amt)]; // "price3", "price3Sum", "price3VAT", "priceHigh", "priceHighSum", "priceHighVAT", "priceLow", "priceLowSum", "priceLowVAT", "priceNone", "round"
}
Expand Down Expand Up @@ -508,20 +513,26 @@ public function getMessages()
return $this->messages;
}

public function getMovementRate(\DateTime $movementDate)
public function getRateInfo(\DateTime $movementDate): array
{
/*
CNB_CACHE=http://localhost/cnb-cache/
RATE_OFFSET=today
FIXED_RATE=25.1
*/


if($this->cnb_cache){

if ($this->fixedRate) {
$rateInfo = ['rate' => $this->fixedRate, 'amount' => $this->fixedRateAmount];
} else {
if ($this->rateOffset) {
$date = $movementDate->modify('-'.$this->rateOffset.' day')->format('Y-m-d');
} else {

$date = $movementDate->format('Y-m-d');
}


$rateInfoRaw = file_get_contents(\Ease\Functions::addUrlParams($this->cnbCache, ['currency' => $this->currency, 'date' => $date]));

if (\is_string($rateInfoRaw) && json_validate($rateInfoRaw)) {
$rateInfo = json_decode($rateInfoRaw, true);
} else {
throw new \RuntimeException(sprintf(_('CNB_CACHE'), $this->cnbCache));
}
}

return $rateInfo;
}
}
5 changes: 4 additions & 1 deletion src/pohoda-raiffeisenbank-offline-statement-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
*/
\Ease\Shared::init(['POHODA_URL', 'POHODA_USERNAME', 'POHODA_PASSWORD', 'POHODA_ICO', 'POHODA_BANK_IDS', 'ACCOUNT_NUMBER'], \array_key_exists('environment', $options) ? $options['environment'] : '../.env');
$engine = new Statementor(Shared::cfg('ACCOUNT_NUMBER'));
$engine->logBanner('', 'Importing file: ' . $statementFile);

if (Shared::cfg('APP_DEBUG')) {
$engine->logBanner('', 'Importing file: '.$statementFile);
}

$report['input'] = $statementFile;

Expand Down

0 comments on commit 58d6102

Please sign in to comment.