2016-08-26 12:43:45 +00:00
|
|
|
<?php
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
require_once(__DIR__.'/lib/method.php');
|
|
|
|
require_once(__DIR__.'/lib/message.php');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
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');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
if (!$this->ediSchema)
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new Exception('Can not load EDI schema.');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$this->params = $db->query(
|
2016-08-26 12:43:45 +00:00
|
|
|
'SELECT code, name, subname, position, type, required FROM param');
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$inbox = imap_search($this->imap, 'ALL');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($inbox) {
|
2018-06-06 11:08:17 +00:00
|
|
|
foreach ($inbox as $msg)
|
2018-05-23 10:14:20 +00:00
|
|
|
$this->loadMail($db, $msg);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$inboxCount = count($inbox);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
if ($inboxCount > 0)
|
2016-08-31 11:53:46 +00:00
|
|
|
echo "Total $inboxCount messages readed\n";
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function loadMail($db, $msg) {
|
2016-08-26 12:43:45 +00:00
|
|
|
$imap = $this->imap;
|
|
|
|
|
|
|
|
// Gets EKT messages from email
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$msgStructure = imap_fetchstructure($imap, $msg);
|
2016-08-26 12:43:45 +00:00
|
|
|
$result = [];
|
|
|
|
|
|
|
|
// Gets the mail sender and Message-ID
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$header = imap_headerinfo($imap, $msg);
|
2016-08-26 12:43:45 +00:00
|
|
|
$from = $header->from;
|
2018-05-23 10:14:20 +00:00
|
|
|
$mailId = trim($header->message_id, '<>');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($from && count($from) > 0)
|
2016-08-26 12:43:45 +00:00
|
|
|
$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);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$error = NULL;
|
|
|
|
|
2018-06-06 11:08:17 +00:00
|
|
|
foreach ($result as $msgSection)
|
2018-05-23 10:14:20 +00:00
|
|
|
try {
|
|
|
|
$part = imap_bodystruct($imap, $msg, $msgSection);
|
|
|
|
$ediString = imap_fetchbody($imap, $msg, $msgSection);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
switch ($part->encoding) {
|
2016-08-26 12:43:45 +00:00
|
|
|
case ENCBASE64:
|
2018-05-23 10:14:20 +00:00
|
|
|
$ediString = imap_base64($ediString);
|
2016-08-26 12:43:45 +00:00
|
|
|
break;
|
|
|
|
case ENCQUOTEDPRINTABLE:
|
2018-05-23 10:14:20 +00:00
|
|
|
$ediString = imap_qprint($ediString);
|
2016-08-26 12:43:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!Edi\Message::isEdiString($ediString))
|
2016-08-26 12:43:45 +00:00
|
|
|
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);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$db->query('START TRANSACTION');
|
|
|
|
$db->query('CALL messageNew(#mailId, #sender, @message)',
|
2016-08-26 12:43:45 +00:00
|
|
|
[
|
|
|
|
'mailId' => $mailId,
|
|
|
|
'sender' => $sender
|
|
|
|
]);
|
|
|
|
|
|
|
|
$unb = $ediMessage->section;
|
|
|
|
$unhs = $unb->childs['UNH'];
|
|
|
|
|
2018-06-06 11:08:17 +00:00
|
|
|
foreach ($unhs as $unh)
|
|
|
|
foreach ($lins = $unh->childs['LIN'] as $lin) {
|
2016-08-26 12:43:45 +00:00
|
|
|
$ediValues = [];
|
|
|
|
|
|
|
|
// Gets the exchange params
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$this->params->data_seek(0);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
while ($row = $this->params->fetch_assoc()) {
|
|
|
|
switch ($row['type']) {
|
2016-08-26 12:43:45 +00:00
|
|
|
case 'INTEGER':
|
2016-10-17 12:36:52 +00:00
|
|
|
$type = Type::INTEGER;
|
2016-08-26 12:43:45 +00:00
|
|
|
break;
|
|
|
|
case 'DOUBLE':
|
2016-10-17 12:36:52 +00:00
|
|
|
$type = Type::DOUBLE;
|
2016-08-26 12:43:45 +00:00
|
|
|
break;
|
|
|
|
case 'DATE':
|
2016-10-17 12:36:52 +00:00
|
|
|
$type = Type::DATE;
|
2016-08-26 12:43:45 +00:00
|
|
|
break;
|
|
|
|
case 'TIME':
|
2016-10-17 12:36:52 +00:00
|
|
|
$type = Type::TIME;
|
2016-08-26 12:43:45 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-10-17 12:36:52 +00:00
|
|
|
$type = Type::STRING;
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$value = $lin->getValue(
|
2016-08-26 12:43:45 +00:00
|
|
|
$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']);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
$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'
|
2016-08-26 12:43:45 +00:00
|
|
|
,$ediValues
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($res)
|
2018-05-23 11:09:55 +00:00
|
|
|
while ($row = $res->fetch_assoc()) {
|
|
|
|
$value = $lin->getValue('IMD', 2, Type::INTEGER, $row['feature']);
|
|
|
|
$ediValues['s'.$row['presentation_order']] = $value;
|
|
|
|
}
|
2016-08-26 12:43:45 +00:00
|
|
|
else
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new Exception('Can\'t get the item features.');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
for ($i = 1; $i <= 6; $i++)
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!isset($ediValues['s'.$i]))
|
2016-08-26 12:43:45 +00:00
|
|
|
$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);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
if (!$res)
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new Exception('Failed to insert the line.');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$db->query('COMMIT');
|
2018-05-23 11:09:55 +00:00
|
|
|
} catch (Exception $e) {
|
2018-05-23 10:14:20 +00:00
|
|
|
$db->query('ROLLBACK');
|
|
|
|
$error = $e->getMessage();
|
2016-08-26 12:43:45 +00:00
|
|
|
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) {
|
2016-08-26 12:43:45 +00:00
|
|
|
$folder = $this->imapConf['success_folder'];
|
2016-09-01 11:42:11 +00:00
|
|
|
echo "Mail loaded with $count lines.\n";
|
2018-05-23 11:09:55 +00:00
|
|
|
} else {
|
2016-08-26 12:43:45 +00:00
|
|
|
$folder = $this->imapConf['error_folder'];
|
2016-09-01 11:42:11 +00:00
|
|
|
echo "Mail error: $error\n";
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Moves the mail to another folder
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$folder = sprintf('%s', $folder);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!imap_mail_move($imap, $msg, $folder))
|
|
|
|
error_log('Can\'t move message to %s: %s'
|
2016-08-26 12:43:45 +00:00
|
|
|
,$folder
|
2018-05-23 10:14:20 +00:00
|
|
|
,imap_last_error()
|
2016-08-26 12:43:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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);
|
2016-08-26 12:43:45 +00:00
|
|
|
else
|
|
|
|
$result[] = '1';
|
2018-05-23 11:09:55 +00:00
|
|
|
} elseif ($part->type == TYPEMULTIPART)
|
2018-06-06 11:08:17 +00:00
|
|
|
foreach ($part->parts as $i => $subpart) {
|
2018-05-23 11:09:55 +00:00
|
|
|
array_push($section, $i + 1);
|
|
|
|
$this->imapFindParts($subpart, $matchTypes, $section, $result);
|
|
|
|
array_pop($section);
|
|
|
|
}
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|