hedera-web/rest/edi/load.php

208 lines
4.7 KiB
PHP
Raw Normal View History

<?php
2018-05-23 10:14:20 +00:00
require_once(__DIR__.'/lib/method.php');
require_once(__DIR__.'/lib/message.php');
2016-10-17 12:36:52 +00:00
use Vn\Lib\Type;
2018-05-23 10:14:20 +00:00
class Load extends Edi\Method {
function ediRun($db) {
$this->ediSchema = Edi\Message::loadSchema('CLOCKT');
if (!$this->ediSchema)
2018-05-23 10:14:20 +00:00
throw new Exception('Can not load EDI schema.');
2018-05-23 10:14:20 +00:00
$this->params = $db->query(
'SELECT code, name, subname, position, type, required FROM param');
2018-05-23 10:14:20 +00:00
$inbox = imap_search($this->imap, 'ALL');
2018-05-23 10:14:20 +00:00
if ($inbox) {
foreach($inbox as $msg)
$this->loadMail($db, $msg);
2018-05-23 10:14:20 +00:00
$inboxCount = count($inbox);
if ($inboxCount > 0)
2016-08-31 11:53:46 +00:00
echo "Total $inboxCount messages readed\n";
}
}
2018-05-23 10:14:20 +00:00
function loadMail($db, $msg) {
$imap = $this->imap;
// Gets EKT messages from email
2018-05-23 10:14:20 +00:00
$msgStructure = imap_fetchstructure($imap, $msg);
$result = [];
// Gets the mail sender and Message-ID
2018-05-23 10:14:20 +00:00
$header = imap_headerinfo($imap, $msg);
$from = $header->from;
2018-05-23 10:14:20 +00:00
$mailId = trim($header->message_id, '<>');
2018-05-23 10:14:20 +00:00
if ($from && count($from) > 0)
$sender = $from[0]->mailbox .'@'. $from[0]->host;
else
$sender = NULL;
// Searches the EDI message on mail parts
$matchTypes = [TYPEAPPLICATION, TYPETEXT];
2018-05-23 10:14:20 +00:00
$this->imapFindParts($msgStructure, $matchTypes, [], $result);
$count = 0;
$error = NULL;
2018-05-23 10:14:20 +00:00
foreach($result as $msgSection)
try {
$part = imap_bodystruct($imap, $msg, $msgSection);
$ediString = imap_fetchbody($imap, $msg, $msgSection);
2018-05-23 10:14:20 +00:00
switch ($part->encoding) {
case ENCBASE64:
2018-05-23 10:14:20 +00:00
$ediString = imap_base64($ediString);
break;
case ENCQUOTEDPRINTABLE:
2018-05-23 10:14:20 +00:00
$ediString = imap_qprint($ediString);
break;
}
2018-05-23 10:14:20 +00:00
if (!Edi\Message::isEdiString($ediString))
continue;
// Creates the EDI object and loads its exchanges
2018-05-23 10:14:20 +00:00
$ediMessage = new Edi\Message();
$ediMessage->parse($ediString, $this->ediSchema);
2018-05-23 10:14:20 +00:00
$db->query('START TRANSACTION');
$db->query('CALL messageNew(#mailId, #sender, @message)',
[
'mailId' => $mailId,
'sender' => $sender
]);
$unb = $ediMessage->section;
$unhs = $unb->childs['UNH'];
2018-05-23 10:14:20 +00:00
foreach($unhs as $unh)
foreach($lins = $unh->childs['LIN'] as $lin) {
$ediValues = [];
// Gets the exchange params
2018-05-23 10:14:20 +00:00
$this->params->data_seek(0);
2018-05-23 10:14:20 +00:00
while ($row = $this->params->fetch_assoc()) {
switch ($row['type']) {
case 'INTEGER':
2016-10-17 12:36:52 +00:00
$type = Type::INTEGER;
break;
case 'DOUBLE':
2016-10-17 12:36:52 +00:00
$type = Type::DOUBLE;
break;
case 'DATE':
2016-10-17 12:36:52 +00:00
$type = Type::DATE;
break;
case 'TIME':
2016-10-17 12:36:52 +00:00
$type = Type::TIME;
break;
default:
2016-10-17 12:36:52 +00:00
$type = Type::STRING;
}
2018-05-23 10:14:20 +00:00
$value = $lin->getValue(
$row['name'], $row['position'], $type, $row['subname']);
2018-05-23 10:14:20 +00:00
if (!isset($value) && $row['required'])
throw new Exception('Missing required parameter: '. $row['code']);
$ediValues[$row['code']] = $value;
}
// Gets the exchange features
2018-05-23 10:14:20 +00:00
$res = $db->query(
2016-10-27 11:22:04 +00:00
'SELECT presentation_order, feature
FROM item_feature
WHERE item_id = #ref
AND entry_date <= CURDATE()
2018-05-23 10:14:20 +00:00
AND(expiry_date IS NULL OR expiry_date >= CURDATE())
2016-10-27 11:22:04 +00:00
GROUP BY presentation_order'
,$ediValues
);
if ($res)
2018-05-23 10:14:20 +00:00
while ($row = $res->fetch_assoc()) {
$value = $lin->getValue('IMD', 2, Type::INTEGER, $row['feature']);
$ediValues['s'.$row['presentation_order']] = $value;
}
else
2018-05-23 10:14:20 +00:00
throw new Exception('Can\'t get the item features.');
for ($i = 1; $i <= 6; $i++)
2018-05-23 10:14:20 +00:00
if (!isset($ediValues['s'.$i]))
$ediValues['s'.$i] = NULL;
// Adds the exchange to the Database
2018-05-23 10:14:20 +00:00
$res = $db->queryFromFile(__DIR__.'/sql/batch-add', $ediValues);
if (!$res)
2018-05-23 10:14:20 +00:00
throw new Exception('Failed to insert the line.');
$count++;
}
2018-05-23 10:14:20 +00:00
$db->query('COMMIT');
}
2018-05-23 10:14:20 +00:00
catch (Exception $e) {
$db->query('ROLLBACK');
$error = $e->getMessage();
break;
}
if (!$error && $count == 0)
$error = 'EDI messages not found.';
// Logs information of realized operations
2018-05-23 10:14:20 +00:00
if (!$error) {
$folder = $this->imapConf['success_folder'];
echo "Mail loaded with $count lines.\n";
}
2018-05-23 10:14:20 +00:00
else {
$folder = $this->imapConf['error_folder'];
echo "Mail error: $error\n";
}
// Moves the mail to another folder
2018-05-23 10:14:20 +00:00
$folder = sprintf('%s', $folder);
2018-05-23 10:14:20 +00:00
if (!imap_mail_move($imap, $msg, $folder))
error_log('Can\'t move message to %s: %s'
,$folder
2018-05-23 10:14:20 +00:00
,imap_last_error()
);
}
2018-05-23 10:14:20 +00:00
function imapFindParts(&$part, &$matchTypes, $section, &$result) {
if (in_array($part->type, $matchTypes)) {
if (count($section) > 0)
$result[] = implode('.', $section);
else
$result[] = '1';
}
elseif ($part->type == TYPEMULTIPART)
2018-05-23 10:14:20 +00:00
foreach($part->parts as $i => $subpart) {
array_push($section, $i + 1);
$this->imapFindParts($subpart, $matchTypes, $section, $result);
array_pop($section);
}
}
}