0
1
Fork 0

Merge branch 'master' into test

This commit is contained in:
Juan Ferrer 2022-07-11 11:26:33 +02:00
commit a43e0522f5
7 changed files with 117 additions and 55 deletions

2
debian/changelog vendored
View File

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

View File

@ -1,6 +1,6 @@
{
"name": "hedera-web",
"version": "1.407.80",
"version": "1.407.81",
"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');
@ -54,15 +66,6 @@ class Load extends Edi\Method {
else
$sender = NULL;
$db->query('CALL mail_new(#messageId, #sender, @mailFk)', [
'messageId' => $messageId,
'sender' => $sender
]);
$mailId = $db->getValue("SELECT @mailFk");
echo "Message from: $sender\n";
echo " -> Message id: $messageId\n";
if ($this->restrictToSenders) {
$isAllowed = $db->getValue(
"SELECT COUNT(*) > 0 FROM mailSender WHERE mail = #",
@ -72,7 +75,13 @@ class Load extends Edi\Method {
if (!$isAllowed)
throw new Exception('Mail processing from unknown senders is disabled');
}
$db->query('CALL mail_new(#, #, @mailFk)', [$messageId, $sender]);
$mailId = $db->getValue("SELECT @mailFk");
echo "Message from: $sender\n";
echo " -> Message id: $messageId\n";
// Searches the EDI message on mail parts
$matchTypes = [TYPEAPPLICATION, TYPETEXT];
@ -97,27 +106,32 @@ class Load extends Edi\Method {
if (!Edi\Message::isEdiString($ediString))
continue;
$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;
@ -135,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
@ -156,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.');
@ -169,17 +183,72 @@ 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;
if (!$res)
throw new Exception('Failed to insert the line.');
$deliveryNumber = nullIf($ediValues, 'deliveryNumber');
$fec = nullIf($ediValues, 'fec');
$year = isset($fec) ? $fec->format('Y') : null;
$insertValues['entryYear'] = $year;
$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 && isset($year) && isset($deliveryNumber)) {
$ektFk = $db->getValue(
"SELECT id
FROM ekt
WHERE deliveryNumber = #
AND entryYear = #",
[$deliveryNumber, $year]
);
$canUpdate = $ektFk && $db->getValue(
"SELECT COUNT(*) = 0
FROM ekt t
JOIN `exchange` b ON b.ektFk = t.id
JOIN exchangeConfig c
WHERE t.id = #
AND b.typeFk != c.presaleFk",
[$ektFk]
);
if ($canUpdate) {
$db->update('ekt',
$insertValues,
['id' => $ektFk]
);
}
}
$db->call('ekt_refresh', [$ektFk, $mailId]);
if ($isNew) $db->call('ekt_load', [$ektFk]);
$db->insert('exchange', [
'mailFk' => $mailId,
'typeFk' => $ediValues['bgm'],
'ektFk' => $ektFk
]);
$count++;
}
$db->query('COMMIT');
$db->commit();
} catch (Exception $e) {
$db->query('ROLLBACK');
$db->rollback();
throw $e;
}
@ -190,24 +259,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]
);
}
@ -221,7 +284,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)
@ -236,4 +299,3 @@ class Load extends Edi\Method {
}
}
}

View File

@ -1,5 +0,0 @@
CALL exchange_new (
#mailId, #art, #bgm, #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
)