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. * Initial Release.

View File

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

View File

@ -6,7 +6,10 @@ class Clean extends Edi\Method {
function ediRun($db) { function ediRun($db) {
$imap = $this->imap; $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; $deleted = 0;
$date = new DateTime(NULL); $date = new DateTime(NULL);

View File

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

View File

@ -13,8 +13,10 @@ abstract class Method extends \Vn\Lib\Method {
$db->selectDb('edi'); $db->selectDb('edi');
$imapConf = $db->getRow( $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}', $this->mailbox = sprintf('{%s/imap/ssl/novalidate-cert}',
$imapConf['host']); $imapConf['host']);

View File

@ -14,8 +14,20 @@ class Load extends Edi\Method {
$this->restrictToSenders = $db->getValue( $this->restrictToSenders = $db->getValue(
"SELECT restrictToSenders FROM exchangeConfig LIMIT 1"); "SELECT restrictToSenders FROM exchangeConfig LIMIT 1");
$this->params = $db->query( $this->paramsRes = $db->query(
"SELECT code, name, subname, position, type, required FROM param"); "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'); $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'); throw new Exception('Mail processing from unknown senders is disabled');
} }
$db->query('CALL mail_new(#messageId, #sender, @mailFk)', [ $db->query('CALL mail_new(#, #, @mailFk)', [$messageId, $sender]);
'messageId' => $messageId,
'sender' => $sender
]);
$mailId = $db->getValue("SELECT @mailFk"); $mailId = $db->getValue("SELECT @mailFk");
echo "Message from: $sender\n"; echo "Message from: $sender\n";
@ -98,34 +107,31 @@ class Load extends Edi\Method {
if (!Edi\Message::isEdiString($ediString)) if (!Edi\Message::isEdiString($ediString))
continue; continue;
$db->query( $db->update('mail',
'UPDATE mail ['source' => $ediString],
SET source = #source ['id' => $mailId]
WHERE id = #mailId', [ );
'source' => $ediString,
'mailId' => $mailId
]);
// Creates the EDI object and loads its exchanges // Creates the EDI object and loads its exchanges
$ediMessage = new Edi\Message(); $ediMessage = new Edi\Message();
$ediMessage->parse($ediString, $this->ediSchema); $ediMessage->parse($ediString, $this->ediSchema);
$db->query('START TRANSACTION'); $db->startTransaction();
$unb = $ediMessage->section; $unb = $ediMessage->section;
$unhs = $unb->childs['UNH']; $unhs = $unb->childs['UNH'];
foreach ($unhs as $unh) foreach ($unhs as $unh)
foreach ($lins = $unh->childs['LIN'] as $lin) { foreach ($lins = $unh->childs['LIN'] as $lin) {
$ediValues = ['mailId' => $mailId]; $ediValues = ['mailId' => $mailId];
// Gets the exchange params // Gets the exchange params
$this->params->data_seek(0); $this->paramsRes->data_seek(0);
while ($row = $this->params->fetch_assoc()) { while ($row = $this->paramsRes->fetch_object()) {
switch ($row['type']) { switch ($row->type) {
case 'INTEGER': case 'INTEGER':
$type = Type::INTEGER; $type = Type::INTEGER;
break; break;
@ -143,12 +149,12 @@ class Load extends Edi\Method {
} }
$value = $lin->getValue( $value = $lin->getValue(
$row['name'], $row['position'], $type, $row['subname']); $row->name, $row->position, $type, $row->subname);
if (!isset($value) && $row['required']) if (!isset($value) && $row->required)
throw new Exception('Missing required parameter: '. $row['code']); throw new Exception('Missing required parameter: '. $row->code);
$ediValues[$row['code']] = $value; $ediValues[$row->code] = $value;
} }
// Gets the exchange features // Gets the exchange features
@ -164,9 +170,9 @@ class Load extends Edi\Method {
); );
if ($res) if ($res)
while ($row = $res->fetch_assoc()) { while ($row = $res->fetch_object()) {
$value = $lin->getValue('IMD', 2, Type::INTEGER, $row['feature']); $value = $lin->getValue('IMD', 2, Type::INTEGER, $row->feature);
$ediValues['s'.$row['presentation_order']] = $value; $ediValues['s'.$row->presentation_order] = $value;
} }
else else
throw new Exception('Can\'t get the item features.'); throw new Exception('Can\'t get the item features.');
@ -177,7 +183,60 @@ class Load extends Edi\Method {
// Adds the exchange to the Database // 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) if (!$res)
throw new Exception('Failed to insert the line.'); throw new Exception('Failed to insert the line.');
@ -185,9 +244,9 @@ class Load extends Edi\Method {
$count++; $count++;
} }
$db->query('COMMIT'); $db->commit();
} catch (Exception $e) { } catch (Exception $e) {
$db->query('ROLLBACK'); $db->rollback();
throw $e; throw $e;
} }
@ -198,24 +257,18 @@ class Load extends Edi\Method {
echo " -> Loaded exchanges: $count\n"; echo " -> Loaded exchanges: $count\n";
$folder = $this->imapConf['successFolder']; $folder = $this->imapConf['successFolder'];
$db->query( $db->update('mail',
"UPDATE mail ['nExchanges' => $count],
SET nExchanges = #, ['id' => $mailId]
error = NULL
WHERE id = #",
[$count, $mailId]
); );
} catch (Exception $e) { } catch (Exception $e) {
$error = $e->getMessage(); $error = $e->getMessage();
error_log($error); error_log($error);
$folder = $this->imapConf['errorFolder']; $folder = $this->imapConf['errorFolder'];
$db->query( $db->update('mail',
"UPDATE mail ['error' => $error],
SET error = # ['id' => $mailId]
WHERE id = #",
[$error, $mailId]
); );
} }
@ -229,7 +282,7 @@ class Load extends Edi\Method {
,imap_last_error() ,imap_last_error()
); );
} }
function imapFindParts(&$part, &$matchTypes, $section, &$result) { function imapFindParts(&$part, &$matchTypes, $section, &$result) {
if (in_array($part->type, $matchTypes)) { if (in_array($part->type, $matchTypes)) {
if (count($section) > 0) 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
)