Skip to content

Instantly share code, notes, and snippets.

@Samuell1
Created August 26, 2019 15:18
Show Gist options
  • Save Samuell1/c73a61b41b7b12bf0996b238dafc694d to your computer and use it in GitHub Desktop.
Save Samuell1/c73a61b41b7b12bf0996b238dafc694d to your computer and use it in GitHub Desktop.
<?php
use October\Rain\Support\Collection;
use Genkgo\Camt\Config as GenkgoConfig;
use Genkgo\Camt\Reader as GenkgoReader;
use Money\Currencies\ISOCurrencies;
use Money\Formatter\DecimalMoneyFormatter;
class PaymentsParse
{
public $file;
public function __construct($file) {
$this->file = $file;
}
public function getEntries()
{
$reader = new GenkgoReader(GenkgoConfig::getDefault());
$message = $reader->readFile($this->file);
$statements = $message->getRecords();
foreach ($statements as $statement) {
$entries = $statement->getEntries();
}
return $entries;
}
public function parseVariableSymbol($value)
{
$EndToEndId = explode("/", $value);
$variableSymbol = isset($EndToEndId[1]) && str_contains($EndToEndId[1], 'VS')
? str_replace('VS', '', $EndToEndId[1])
: null;
return (int) $variableSymbol;
}
public function getMessage($detail)
{
try {
return $detail->getRemittanceInformation()->getMessage();
} catch (\BadMethodCallException $e) {
return null;
}
}
public function getVariableSymbol($detail)
{
if ($references = $detail->getReferences())
{
$variableSymbol = $this->parseVariableSymbol($references[0]->getEndToEndId());
}
if (!$variableSymbol)
{
$variableSymbol = $this->parseVariableSymbol($this->getMessage($detail));
}
return $variableSymbol;
}
public function getPartyDebtors($parties)
{
$party = [];
foreach ($parties as $party)
{
if (get_class($party->getRelatedPartyType()) !== 'Genkgo\Camt\DTO\Debtor') {
continue;
}
if ($party->getRelatedPartyType() instanceof \Genkgo\Camt\DTO\Debtor) {
if ($party->getRelatedPartyType() instanceof \Genkgo\Camt\DTO\UltimateDebtor) {
$party = [
'type' => 'UltimateDebtor',
'name' => ucwords(strtolower($party->getRelatedPartyType()->getName())),
'iban' => null,
'country' => null,
'address' => $party->getRelatedPartyType()->getAddress() ? $party->getRelatedPartyType()->getAddress()->getAddressLines() : []
];
} else {
$party = [
'type' => 'Debtor',
'name' => ucwords(strtolower($party->getRelatedPartyType()->getName())),
'iban' => $party->getAccount()->getIdentification(),
'country' => $party->getRelatedPartyType()->getAddress() ? $party->getRelatedPartyType()->getAddress()->getCountry() : null,
'address' => $party->getRelatedPartyType()->getAddress() ? $party->getRelatedPartyType()->getAddress()->getAddressLines() : []
];
}
}
}
return $party;
}
public function getPayments()
{
$bankPayments = new Collection(); // make collection for better searching in array
foreach ($this->getEntries() as $entry)
{
$detail = $entry->getTransactionDetail();
if ($detail->getAdditionalTransactionInformation() != 'Prijata platba') continue;
$parties = $detail->getRelatedParties();
$party = (object) $this->getPartyDebtors($parties);
if (!$party) continue; // skip if no parties
$variableSymbol = $this->getVariableSymbol($detail);
if (!$variableSymbol) continue; // skip if payment has not variable symbol
$moneyFormatter = new DecimalMoneyFormatter(new ISOCurrencies());
$bankPayments->push([
'fullName' => $party->name,
'message' => $this->getMessage($detail),
'address' => $party->address,
'price' => (float) $moneyFormatter->format($entry->getAmount()),
'variableSymbol' => $variableSymbol,
'bookingDate' => $entry->getBookingDate()->format('Y-m-d'),
'date' => $entry->getValueDate()->format('Y-m-d')
]);
}
return $bankPayments;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment