Skip to content

Commit

Permalink
fixed import to sharepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Nov 21, 2024
1 parent 1960946 commit a54f6e4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 74 deletions.
67 changes: 36 additions & 31 deletions src/Pohoda/RaiffeisenBank/PohodaBankClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct($bankAccount, $options = [])
*/
public function sourceString()
{
return substr(__FILE__ . '@' . gethostname(), -50);
return substr(__FILE__.'@'.gethostname(), -50);
}

/**
Expand All @@ -76,7 +76,7 @@ public function sourceString()
public static function checkCertificatePresence($certFile): void
{
if ((file_exists($certFile) === false) || (is_readable($certFile) === false)) {
fwrite(\STDERR, 'Cannot read specified certificate file: ' . $certFile . \PHP_EOL);
fwrite(\STDERR, 'Cannot read specified certificate file: '.$certFile.\PHP_EOL);

exit(1);
}
Expand Down Expand Up @@ -128,8 +128,8 @@ public function setScope($scope): void

break;
case 'this_year':
$this->since = new \DateTime('first day of January ' . date('Y'));
$this->until = new \DateTime('last day of December' . date('Y'));
$this->since = new \DateTime('first day of January '.date('Y'));
$this->until = new \DateTime('last day of December'.date('Y'));

break;
case 'January': // 1
Expand All @@ -144,8 +144,8 @@ public function setScope($scope): void
case 'October': // 10
case 'November': // 11
case 'December': // 12
$this->since = new \DateTime('first day of ' . $scope . ' ' . date('Y'));
$this->until = new \DateTime('last day of ' . $scope . ' ' . date('Y'));
$this->since = new \DateTime('first day of '.$scope.' '.date('Y'));
$this->until = new \DateTime('last day of '.$scope.' '.date('Y'));

break;
case 'auto':
Expand Down Expand Up @@ -175,7 +175,7 @@ public function setScope($scope): void
break;
}

throw new \Exception('Unknown scope ' . $scope);
throw new \Exception('Unknown scope '.$scope);
}

break;
Expand All @@ -194,7 +194,7 @@ public function setScope($scope): void
*/
public function getxRequestId()
{
return $this->getDataValue('account') . time();
return $this->getDataValue('account').time();
}

/**
Expand Down Expand Up @@ -223,8 +223,8 @@ public function checkForTransactionPresence()
public function ensureKSExists($conSym): void
{
if (!\array_key_exists($conSym, $this->constSymbols)) {
$this->constantor->insertToPohoda(['kod' => $conSym, 'poznam' => 'Created by Raiffeisen Bank importer', 'nazev' => '?!?!? ' . $conSym]);
$this->constantor->addStatusMessage('New constant ' . $conSym . ' created in flexibee', 'warning');
$this->constantor->insertToPohoda(['kod' => $conSym, 'poznam' => 'Created by Raiffeisen Bank importer', 'nazev' => '?!?!? '.$conSym]);
$this->constantor->addStatusMessage('New constant '.$conSym.' created in flexibee', 'warning');
$this->constSymbols[$conSym] = $conSym;
}
}
Expand Down Expand Up @@ -256,9 +256,8 @@ public function insertTransactionToPohoda($success)
$producedId = $this->response->producedDetails['id'];
$producedNumber = $this->response->producedDetails['number'];
$producedAction = $this->response->producedDetails['actionType'];

$this->automaticLiquidation($producedNumber);

} else {
echo '';
}
Expand All @@ -268,9 +267,7 @@ public function insertTransactionToPohoda($success)
$producedAction = 'n/a';
}

$this->addStatusMessage('#' . $producedId . ' ' . $producedAction . ' ' . $producedNumber, $result ? 'success' : 'error'); // TODO: Parse response for docID


$this->addStatusMessage('#'.$producedId.' '.$producedAction.' '.$producedNumber, $result ? 'success' : 'error'); // TODO: Parse response for docID
} else {
$this->addStatusMessage('Record with remoteNumber TODO already present in Pohoda', 'warning');
}
Expand All @@ -284,6 +281,8 @@ public function insertTransactionToPohoda($success)
* @see https://www.stormware.cz/schema/version_2/liquidation.xsd for details
* @see https://www.stormware.cz/xml/samples/version_2/import/Banka/Bank_03_v2.0.xml
*
* @param mixed $producedNumber
*
* @return bool
*/
public function automaticLiquidation($producedNumber)
Expand Down Expand Up @@ -314,7 +313,7 @@ public function automaticLiquidation($producedNumber)
<ftr:selectedNumbers>
<ftr:number>
<typ:numberRequested>KB0010003</typ:numberRequested>
<typ:numberRequested>KB0010003</typ:numberRequested>
</ftr:number>
</ftr:selectedNumbers>
Expand All @@ -324,16 +323,15 @@ public function automaticLiquidation($producedNumber)
file_put_contents($this->xmlCache, $this->generateAutomaticLiquidationXML($producedNumber));

if ($this->debug) {
fpassthru(fopen($this->xmlCache, 'r'));
fpassthru(fopen($this->xmlCache, 'rb'));
}

$this->addStatusMessage('Automatic liquidation', 'success');


$this->setPostFields(file_get_contents($this->xmlCache));

if ($this->debug) {
$this->addStatusMessage('validate request by: xmllint --schema ' . \dirname(__DIR__, 3) . '/vendor/vitexsoftware/pohoda-connector/doc/xsd/data.xsd ' . $this->xmlCache . ' --noout', 'debug');
$this->addStatusMessage('validate request by: xmllint --schema '.\dirname(__DIR__, 3).'/vendor/vitexsoftware/pohoda-connector/doc/xsd/data.xsd '.$this->xmlCache.' --noout', 'debug');
}

return $this->performRequest('/xml');
Expand All @@ -342,44 +340,51 @@ public function automaticLiquidation($producedNumber)
/**
* Generate XML for automatic liquidation.
*
* @param mixed $producedNumber
*
* @return string
*/
public function generateAutomaticLiquidationXML($producedNumber)
{
$xmlString = '<?xml version="1.0" encoding="Windows-1250"?>
$xmlString = <<<'EOD'
<?xml version="1.0" encoding="Windows-1250"?>
<dat:dataPack xmlns:dat="http://www.stormware.cz/schema/version_2/data.xsd"
xmlns:lqd="http://www.stormware.cz/schema/version_2/automaticLiquidation.xsd"
xmlns:ftr="http://www.stormware.cz/schema/version_2/filter.xsd"
xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd"
version="2.0" id="01" ico="'.$this->getCompanyId().'" application="Tisk" note="aut. livkidace dokladů tisk z programu Pohoda">
</dat:dataPack>';
version="2.0" id="01" ico="
EOD.$this->getCompanyId().<<<'EOD'
" application="Tisk" note="aut. livkidace dokladů tisk z programu Pohoda">
</dat:dataPack>
EOD;

$xml = new \SimpleXMLElement($xmlString);

$dataPackItem = $xml->addChild('dat:dataPackItem');
$dataPackItem->addAttribute('version', '2.0');
$dataPackItem->addAttribute('id', '001');

$automaticLiquidation = $dataPackItem->addChild('automaticLiquidation',null,'http://www.stormware.cz/schema/version_2/automaticLiquidation.xsd');
$automaticLiquidation = $dataPackItem->addChild('automaticLiquidation', null, 'http://www.stormware.cz/schema/version_2/automaticLiquidation.xsd');
$automaticLiquidation->addAttribute('version', '2.0');

$record = $automaticLiquidation->addChild('record',null,'http://www.stormware.cz/schema/version_2/automaticLiquidation.xsd');
$record = $automaticLiquidation->addChild('record', null, 'http://www.stormware.cz/schema/version_2/automaticLiquidation.xsd');

$filter = $record->addChild('filter',null,'http://www.stormware.cz/schema/version_2/filter.xsd');
$filter = $record->addChild('filter', null, 'http://www.stormware.cz/schema/version_2/filter.xsd');

$selectedNumbers = $filter->addChild('selectedNumbers');

$ftrNumber = $selectedNumbers->addChild('number');

$numberRequested = $ftrNumber->addChild('numberRequested', $producedNumber, 'http://www.stormware.cz/schema/version_2/type.xsd');
$ruleOfPairing = $automaticLiquidation->addChild('lqd:ruleOfPairing',null,'http://www.stormware.cz/schema/version_2/automaticLiquidation.xsd');
$ruleOfPairing->addChild('id', '1','http://www.stormware.cz/schema/version_2/type.xsd');
$numberRequested = $ftrNumber->addChild('numberRequested', $producedNumber, 'http://www.stormware.cz/schema/version_2/type.xsd');

$ruleOfPairing = $automaticLiquidation->addChild('lqd:ruleOfPairing', null, 'http://www.stormware.cz/schema/version_2/automaticLiquidation.xsd');
$ruleOfPairing->addChild('id', '1', 'http://www.stormware.cz/schema/version_2/type.xsd');

return $xml->asXML();
}

public function getCompanyId() {
public function getCompanyId()
{
return \Ease\Shared::cfg('POHODA_ICO');
}
}
22 changes: 13 additions & 9 deletions src/Pohoda/RaiffeisenBank/Statementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ class Statementor extends PohodaBankClient

/**
* Downloaded XML statements.
*
* @var array<string>
*/
private array $statementsXML = [];

/**
* Downloaded PDF statements.
*
* @var array<string>
*/
private array $statementsPDF = [];

/**
* Bank Statement Helper
* @param string $bankAccount
* @param array<string,string> $options
* Bank Statement Helper.
*
* @param array<string, string> $options
*/

public function __construct(string $bankAccount, array $options = [])
{
parent::__construct($bankAccount, $options);
Expand Down Expand Up @@ -72,14 +73,18 @@ public function importXML(string $xmlFile)
return $this->import();
}

public function downloadXML(): void
public function downloadXML(): bool
{
$this->statementsXML = $this->obtainer->download($this->statementsDir, $this->obtainer->getStatements(), 'xml');

return empty($this->statementsXML) === false;
}

public function downloadPDF(): void
public function downloadPDF(): bool
{
$this->statementsPDF = $this->obtainer->download($this->statementsDir, $this->obtainer->getStatements(), 'pdf');

return empty($this->statementsPDF) === false;
}

/**
Expand All @@ -95,8 +100,8 @@ public function importOnline()

/**
* Import Raiffeisen bank XML statement into Pohoda.
*
* @return array<string,array>
*
* @return array<string, array>
*/
public function import(): array
{
Expand Down Expand Up @@ -127,7 +132,6 @@ public function import(): array
echo ''; // WTF?
}
}

}

$this->addStatusMessage($statementNumberLong.' Import done. '.$success.' of '.$entries.' imported');
Expand Down
2 changes: 1 addition & 1 deletion src/pohoda-raiffeisenbank-xml-statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

try {
$ctx->executeQuery();
} catch (Exception $exc) {
} catch (\Exception $exc) {
fwrite(fopen('php://stderr', 'wb'), $exc->getMessage().\PHP_EOL);

exit(1);
Expand Down
86 changes: 53 additions & 33 deletions src/pohodaSQL-raiffeisenbank-statements-sharepoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,18 @@
/**
* Get today's Statements list.
*/
\Ease\Shared::init([
'POHODA_URL', 'POHODA_USERNAME', 'POHODA_PASSWORD', 'POHODA_ICO',
Shared::init([
'POHODA_URL', 'POHODA_USERNAME', 'POHODA_PASSWORD', 'POHODA_ICO',
'CERT_FILE', 'CERT_PASS', 'XIBMCLIENTID', 'ACCOUNT_NUMBER',
'DB_CONNECTION', 'DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD'
'DB_CONNECTION', 'DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD',
], $argv[1] ?? '../.env');

PohodaBankClient::checkCertificatePresence(\Ease\Shared::cfg('CERT_FILE'));
$engine = new Statementor(\Ease\Shared::cfg('ACCOUNT_NUMBER'));
$engine->setScope(\Ease\Shared::cfg('IMPORT_SCOPE', 'last_month'));
PohodaBankClient::checkCertificatePresence(Shared::cfg('CERT_FILE'));
$engine = new Statementor(Shared::cfg('ACCOUNT_NUMBER'));
$engine->setScope(Shared::cfg('IMPORT_SCOPE', 'last_month'));
$engine->logBanner('', 'Scope: '.$engine->scope);

$inserted = $engine->importOnline();

if ($inserted) {
//
// [243] => Array
// (
// [id] => 243
// [number] => KB102023
// [actionType] => add
// )
//
// [244] => Array
// (
// [id] => 244
// [number] => KB102023
// [actionType] => add
// )
//
if ($engine->downloadPDF()) {
sleep(5);

$pdfs = $engine->getPdfStatements();
Expand All @@ -78,22 +61,59 @@

try {
$ctx->executeQuery();
} catch (Exception $exc) {
$uploaded = $ctx->getBaseUrl().'/_layouts/15/download.aspx?SourceUrl='.urlencode($uploadFile->getServerRelativeUrl());
$engine->addStatusMessage(_('Uploaded').': '.$uploaded, 'success');
} catch (\Exception $exc) {
fwrite(fopen('php://stderr', 'wb'), $exc->getMessage().\PHP_EOL);

exit(1);
}

$fileUrl = $ctx->getBaseUrl().'/_layouts/15/download.aspx?SourceUrl='.urlencode($uploadFile->getServerRelativeUrl());
$fileUrls[basename($filename)] = $uploaded;
}
} else {
$engine->addStatusMessage(_('Error obtaining PDF'), 'error');
}

$doc = new \SpojeNet\PohodaSQL\DOC();
$doc->setDataValue('RelAgID', \SpojeNet\PohodaSQL\Agenda::BANK); // Bank
sleep(5);

if ($engine->downloadXML()) {
$inserted = $engine->import();

if ($inserted) {
//
// [243] => Array
// (
// [id] => 243
// [number] => KB102023
// [actionType] => add
// )
//
// [244] => Array
// (
// [id] => 244
// [number] => KB102023
// [actionType] => add
// )
//

$doc = new \SpojeNet\PohodaSQL\DOC();
$doc->setDataValue('RelAgID', \SpojeNet\PohodaSQL\Agenda::BANK); // Bank

foreach ($inserted as $id => $importInfo) {
$filename = key($fileUrls);
$statement = next($fileUrls);
// $url = \Ease\Shared::cfg('DOWNLOAD_LINK_PREFIX') . urlencode(basename($statement));
$result = $doc->urlAttachment((int) $id, $filename, basename($statement));
$doc->addStatusMessage($importInfo['number'].' '.$fileUrl, null === $result ? 'error' : 'success');
}
} else {
$engine->addStatusMessage(_('Error Importing XML statements to Pohoda'), 'error');

foreach ($inserted as $id => $importInfo) {
$statement = current($pdfs);
// $url = \Ease\Shared::cfg('DOWNLOAD_LINK_PREFIX') . urlencode(basename($statement));
$result = $doc->urlAttachment($id, $fileUrl, basename($statement));
$doc->addStatusMessage($importInfo['number'].' '.$fileUrl, null === $result ? 'error' : 'success');
exit(2);
}
} else {
$engine->addStatusMessage(_('Error Obtaining XML statements'), 'error');

exit(3);
}

0 comments on commit a54f6e4

Please sign in to comment.