EDI: environments, new params, refactor
gitea/hedera-web/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2022-07-07 14:55:34 +02:00
parent be23ceed4b
commit 574c63ea4e
7 changed files with 106 additions and 85 deletions

2
debian/changelog vendored
View File

@ -1,4 +1,4 @@
hedera-web (1.408.3) stable; urgency=low
hedera-web (1.408.4) stable; urgency=low
* Initial Release.

View File

@ -1,6 +1,6 @@
{
"name": "hedera-web",
"version": "1.408.3",
"version": "1.408.4",
"description": "Verdnatura web page",
"license": "GPL-3.0",
"repository": {

View File

@ -6,7 +6,10 @@ class Clean extends Edi\Method {
function ediRun($db) {
$imap = $this->imap;
$cleanPeriod = $db->getValue('SELECT cleanPeriod FROM imapConfig');
$cleanPeriod = $db->getValue(
"SELECT ic.cleanPeriod
FROM imapConfig ic
JOIN util.config c ON c.environment = ic.environment");
$deleted = 0;
$date = new DateTime(NULL);

View File

@ -113,7 +113,7 @@ class Message {
if (!isset($string{$pos}))
return NULL;
if (in_array($string{$pos}, ['+', ':', '\''])) {
if (in_array($string[$pos], ['+', ':', '\''])) {
if (!$empty) {
$values[] =
trim(substr($string, $start, $pos - $start));
@ -125,7 +125,7 @@ class Message {
$empty = FALSE;
}
if ($string{$pos} === '\'')
if ($string[$pos] === '\'')
break;
$pos++;

View File

@ -13,8 +13,10 @@ abstract class Method extends \Vn\Lib\Method {
$db->selectDb('edi');
$imapConf = $db->getRow(
'SELECT host, user, pass, successFolder, errorFolder FROM imapConfig');
"SELECT ic.host, ic.user, ic.pass, ic.successFolder, ic.errorFolder
FROM imapConfig ic
JOIN util.config c ON c.environment = ic.environment");
$this->mailbox = sprintf('{%s/imap/ssl/novalidate-cert}',
$imapConf['host']);

View File

@ -14,8 +14,20 @@ class Load extends Edi\Method {
$this->restrictToSenders = $db->getValue(
"SELECT restrictToSenders FROM exchangeConfig LIMIT 1");
$this->params = $db->query(
"SELECT code, name, subname, position, type, required FROM param");
$this->paramsRes = $db->query(
"SELECT `code`, `name`, `subname`, `position`, `type`, `required`
FROM `param`"
);
$res = $db->query(
"SELECT COLUMN_NAME columnName
FROM information_schema.`COLUMNS`
WHERE TABLE_NAME = 'ekt' AND TABLE_SCHEMA = SCHEMA()"
);
$this->columns = [];
while ($row = $res->fetch_object())
$this->columns[$row->columnName] = true;
$inbox = imap_search($this->imap, 'ALL');
@ -64,10 +76,7 @@ class Load extends Edi\Method {
throw new Exception('Mail processing from unknown senders is disabled');
}
$db->query('CALL mail_new(#messageId, #sender, @mailFk)', [
'messageId' => $messageId,
'sender' => $sender
]);
$db->query('CALL mail_new(#, #, @mailFk)', [$messageId, $sender]);
$mailId = $db->getValue("SELECT @mailFk");
echo "Message from: $sender\n";
@ -98,34 +107,31 @@ class Load extends Edi\Method {
if (!Edi\Message::isEdiString($ediString))
continue;
$db->query(
'UPDATE mail
SET source = #source
WHERE id = #mailId', [
'source' => $ediString,
'mailId' => $mailId
]);
$db->update('mail',
['source' => $ediString],
['id' => $mailId]
);
// Creates the EDI object and loads its exchanges
$ediMessage = new Edi\Message();
$ediMessage->parse($ediString, $this->ediSchema);
$db->query('START TRANSACTION');
$db->startTransaction();
$unb = $ediMessage->section;
$unhs = $unb->childs['UNH'];
foreach ($unhs as $unh)
foreach ($lins = $unh->childs['LIN'] as $lin) {
$ediValues = ['mailId' => $mailId];
// Gets the exchange params
$this->params->data_seek(0);
$this->paramsRes->data_seek(0);
while ($row = $this->params->fetch_assoc()) {
switch ($row['type']) {
while ($row = $this->paramsRes->fetch_object()) {
switch ($row->type) {
case 'INTEGER':
$type = Type::INTEGER;
break;
@ -143,12 +149,12 @@ class Load extends Edi\Method {
}
$value = $lin->getValue(
$row['name'], $row['position'], $type, $row['subname']);
$row->name, $row->position, $type, $row->subname);
if (!isset($value) && $row['required'])
throw new Exception('Missing required parameter: '. $row['code']);
if (!isset($value) && $row->required)
throw new Exception('Missing required parameter: '. $row->code);
$ediValues[$row['code']] = $value;
$ediValues[$row->code] = $value;
}
// Gets the exchange features
@ -164,9 +170,9 @@ class Load extends Edi\Method {
);
if ($res)
while ($row = $res->fetch_assoc()) {
$value = $lin->getValue('IMD', 2, Type::INTEGER, $row['feature']);
$ediValues['s'.$row['presentation_order']] = $value;
while ($row = $res->fetch_object()) {
$value = $lin->getValue('IMD', 2, Type::INTEGER, $row->feature);
$ediValues['s'.$row->presentation_order] = $value;
}
else
throw new Exception('Can\'t get the item features.');
@ -177,7 +183,60 @@ class Load extends Edi\Method {
// Adds the exchange to the Database
$res = $db->queryFromFile(__DIR__.'/sql/exchange-new', $ediValues);
$insertValues = [];
foreach ($ediValues as $code => $value)
if (isset($this->columns[$code]) && !empty($value))
$insertValues[$code] = $value;
$isNew = false;
$update = false;
try {
$db->insert('ekt', $insertValues);
$ektFk = $db->lastInsertId();
$isNew = true;
} catch (Exception $e) {
if ($e->getCode() == 1062)
$update = true;
else
throw $e;
}
if ($update) {
$updateExistent = $db->getValue(
"SELECT COUNT(*) = 0
FROM ekt t
JOIN `exchange` b ON b.ektFk = t.id
JOIN exchangeConfig c
WHERE t.deliveryNumber = #deliveryNumber
AND t.entryYear = YEAR(#fec)
AND b.typeFk != c.presaleFk",
$ediValues
);
if ($updateExistent) {
$ektFk = $db->getValue(
"SELECT id
FROM ekt
WHERE deliveryNumber = #deliveryNumber
AND entryYear = YEAR(#fec)",
$ediValues
);
$db->update('ekt',
$insertValues,
['id' => $ektFk]
);
}
}
$db->insert('exchange', [
'mailFk' => $mailId,
'typeFk' => $ediValues['bgm'],
'ektFk' => $ektFk
]);
$res = $db->call('ekt_refresh', [$ektFk]);
if ($isNew) $res = $db->call('ekt_load', [$ektFk]);
if (!$res)
throw new Exception('Failed to insert the line.');
@ -185,9 +244,9 @@ class Load extends Edi\Method {
$count++;
}
$db->query('COMMIT');
$db->commit();
} catch (Exception $e) {
$db->query('ROLLBACK');
$db->rollback();
throw $e;
}
@ -198,24 +257,18 @@ class Load extends Edi\Method {
echo " -> Loaded exchanges: $count\n";
$folder = $this->imapConf['successFolder'];
$db->query(
"UPDATE mail
SET nExchanges = #,
error = NULL
WHERE id = #",
[$count, $mailId]
$db->update('mail',
['nExchanges' => $count],
['id' => $mailId]
);
} catch (Exception $e) {
$error = $e->getMessage();
error_log($error);
$folder = $this->imapConf['errorFolder'];
$db->query(
"UPDATE mail
SET error = #
WHERE id = #",
[$error, $mailId]
$db->update('mail',
['error' => $error],
['id' => $mailId]
);
}
@ -229,7 +282,7 @@ class Load extends Edi\Method {
,imap_last_error()
);
}
function imapFindParts(&$part, &$matchTypes, $section, &$result) {
if (in_array($part->type, $matchTypes)) {
if (count($section) > 0)
@ -244,4 +297,3 @@ class Load extends Edi\Method {
}
}
}

View File

@ -1,36 +0,0 @@
CALL exchange_new (
#mailId,
#art,
#bgm,
#batchNumber,
#vendorOrderNumber,
#aaj,
#fec,
#hor,
#ref,
#agj,
#cat,
#pac,
#sub,
#kop,
#ptd,
#pro,
#ori,
#ptj,
#qty,
#pri,
#klo,
#s1,
#s2,
#s3,
#s4,
#s5,
#s6,
#k1,
#k2,
#p1,
#p2,
#auction,
#package,
#putOrderFk
)