ShipmentReference.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Shipment;
  6. use Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;
  7. use Temando\Shipping\Api\Data\Shipment\ShipmentReferenceInterface;
  8. use Temando\Shipping\Setup\SetupSchema;
  9. /**
  10. * Temando Shipment Resource Model
  11. *
  12. * @package Temando\Shipping\Model
  13. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  14. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  15. * @link http://www.temando.com/
  16. */
  17. class ShipmentReference extends AbstractDb
  18. {
  19. /**
  20. * Init main table and primary key.
  21. *
  22. * @return void
  23. */
  24. protected function _construct()
  25. {
  26. $this->_init(SetupSchema::TABLE_SHIPMENT, ShipmentReferenceInterface::ENTITY_ID);
  27. }
  28. /**
  29. * Query primary key by given shipment id.
  30. *
  31. * @param int $shipmentId
  32. * @return int|null
  33. */
  34. public function getIdByShipmentId($shipmentId)
  35. {
  36. try {
  37. $connection = $this->getConnection();
  38. $tableName = $this->getMainTable();
  39. $table = $this->getTable($tableName);
  40. $select = $connection->select()
  41. ->from($table, ShipmentReferenceInterface::ENTITY_ID)
  42. ->where('shipment_id = :shipment_id');
  43. $bind = [':shipment_id' => (string)$shipmentId];
  44. $entityId = $connection->fetchOne($select, $bind);
  45. return $entityId ? (int) $entityId : null;
  46. } catch (\Exception $exception) {
  47. return null;
  48. }
  49. }
  50. /**
  51. * Query primary key by given platform shipment id.
  52. *
  53. * @param string $shipmentId
  54. * @return int|null
  55. */
  56. public function getIdByExtShipmentId($shipmentId)
  57. {
  58. try {
  59. $connection = $this->getConnection();
  60. $tableName = $this->getMainTable();
  61. $table = $this->getTable($tableName);
  62. $select = $connection->select()
  63. ->from($table, ShipmentReferenceInterface::ENTITY_ID)
  64. ->where('ext_shipment_id = :ext_shipment_id');
  65. $bind = [':ext_shipment_id' => (string)$shipmentId];
  66. $entityId = $connection->fetchOne($select, $bind);
  67. return $entityId ? (int) $entityId : null;
  68. } catch (\Exception $exception) {
  69. return null;
  70. }
  71. }
  72. /**
  73. * Query primary key by given platform return shipment id.
  74. *
  75. * @param string $returnShipmentId
  76. * @return int|null
  77. */
  78. public function getIdByExtReturnShipmentId($returnShipmentId)
  79. {
  80. try {
  81. $connection = $this->getConnection();
  82. $table = $this->getMainTable();
  83. $select = $connection->select()
  84. ->from($table, ShipmentReferenceInterface::ENTITY_ID)
  85. ->where('ext_return_shipment_id = :ext_return_shipment_id');
  86. $bind = [':ext_return_shipment_id' => (string)$returnShipmentId];
  87. $entityId = $connection->fetchOne($select, $bind);
  88. return $entityId ? (int) $entityId : null;
  89. } catch (\Exception $exception) {
  90. return null;
  91. }
  92. }
  93. /**
  94. * Query multiple primary keys by given platform shipment ids.
  95. *
  96. * @param string[] $extShipmentIds
  97. * @return int[]
  98. */
  99. public function getShipmentIdsByExtShipmentIds(array $extShipmentIds)
  100. {
  101. try {
  102. $connection = $this->getConnection();
  103. $tableName = $this->getMainTable();
  104. $table = $this->getTable($tableName);
  105. $bind = [];
  106. foreach ($extShipmentIds as $index => $extShipmentId) {
  107. $bind[":id{$index}"] = $extShipmentId;
  108. }
  109. $select = $connection->select()
  110. ->from($table, [ShipmentReferenceInterface::EXT_SHIPMENT_ID, ShipmentReferenceInterface::SHIPMENT_ID])
  111. ->where('ext_shipment_id in (' . implode(',', array_keys($bind)) . ')');
  112. return $connection->fetchPairs($select, $bind);
  113. } catch (\Exception $exception) {
  114. return [];
  115. }
  116. }
  117. }