diff --git a/rest/edi/load.php b/rest/edi/load.php index a2afe8d3..ace564f9 100644 --- a/rest/edi/load.php +++ b/rest/edi/load.php @@ -124,125 +124,7 @@ class Load extends Edi\Method { foreach ($unhs as $unh) foreach ($lins = $unh->childs['LIN'] as $lin) { - $ediValues = ['mailId' => $mailId]; - - // Gets the exchange params - - $this->paramsRes->data_seek(0); - - while ($row = $this->paramsRes->fetch_object()) { - switch ($row->type) { - case 'INTEGER': - $type = Type::INTEGER; - break; - case 'DOUBLE': - $type = Type::DOUBLE; - break; - case 'DATE': - $type = Type::DATE; - break; - case 'TIME': - $type = Type::TIME; - break; - default: - $type = Type::STRING; - } - - $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( - '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' - ,$ediValues - ); - - if ($res) - 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.'); - - for ($i = 1; $i <= 6; $i++) - if (!isset($ediValues['s'.$i])) - $ediValues['s'.$i] = NULL; - - // Adds the exchange to the Database - - $insertValues = []; - foreach ($ediValues as $code => $value) - if (isset($this->columns[$code]) && !empty($value)) - $insertValues[$code] = $value; - - $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 - ]); - + $this->processMessage($db, $unh, $lin, $mailId); $count++; } @@ -285,6 +167,132 @@ class Load extends Edi\Method { ); } + function processMessage($db, $unh, $lin, $mailId) { + $ediValues = ['mailId' => $mailId]; + + // Gets the exchange params + + $this->paramsRes->data_seek(0); + + while ($row = $this->paramsRes->fetch_object()) { + switch ($row->type) { + case 'INTEGER': + $type = Type::INTEGER; + break; + case 'DOUBLE': + $type = Type::DOUBLE; + break; + case 'DATE': + $type = Type::DATE; + break; + case 'TIME': + $type = Type::TIME; + break; + default: + $type = Type::STRING; + } + + $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( + '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' + ,$ediValues + ); + + if ($res) + 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.'); + + for ($i = 1; $i <= 6; $i++) + if (!isset($ediValues['s'.$i])) + $ediValues['s'.$i] = NULL; + + // Adds the exchange to the Database + + $insertValues = []; + foreach ($ediValues as $code => $value) + if (isset($this->columns[$code]) && !empty($value)) + $insertValues[$code] = $value; + + $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]); + + try { + if ($isNew) $db->call('ekt_load', [$ektFk]); + } catch (Exception $e) { + error_log("CALL ekt_load($ektFk): {$e->getMessage()}"); + } + + $db->insert('exchange', [ + 'mailFk' => $mailId, + 'typeFk' => $ediValues['bgm'], + 'ektFk' => $ektFk + ]); + } + function imapFindParts(&$part, &$matchTypes, $section, &$result) { if (in_array($part->type, $matchTypes)) { if (count($section) > 0)