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