OrderReference.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Order;
  6. use Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;
  7. use Temando\Shipping\Api\Data\Order\OrderReferenceInterface;
  8. use Temando\Shipping\Setup\SetupSchema;
  9. /**
  10. * Temando Order Reference Resource Model
  11. *
  12. * @package Temando\Shipping\Model
  13. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  14. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  15. * @link https://www.temando.com/
  16. */
  17. class OrderReference 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_ORDER, OrderReferenceInterface::ENTITY_ID);
  27. }
  28. /**
  29. * Read entity id by using sales order id.
  30. *
  31. * @param int $orderId
  32. * @return int|null
  33. */
  34. public function getIdByOrderId($orderId)
  35. {
  36. try {
  37. $connection = $this->getConnection();
  38. $tableName = $this->getMainTable();
  39. $table = $this->getTable($tableName);
  40. $select = $connection->select()
  41. ->from($table, OrderReferenceInterface::ENTITY_ID)
  42. ->where('order_id = :order_id');
  43. $bind = [':order_id' => (string)$orderId];
  44. $entityId = $connection->fetchOne($select, $bind);
  45. return $entityId ? (int) $entityId : null;
  46. } catch (\Exception $exception) {
  47. return null;
  48. }
  49. }
  50. /**
  51. * Read entity id by using platform order id.
  52. *
  53. * @param string $extOrderId
  54. * @return int|null
  55. */
  56. public function getIdByExtOrderId($extOrderId)
  57. {
  58. try {
  59. $connection = $this->getConnection();
  60. $tableName = $this->getMainTable();
  61. $table = $this->getTable($tableName);
  62. $select = $connection->select()
  63. ->from($table, OrderReferenceInterface::ENTITY_ID)
  64. ->where('ext_order_id = :ext_order_id');
  65. $bind = [':ext_order_id' => (string)$extOrderId];
  66. $entityId = $connection->fetchOne($select, $bind);
  67. return $entityId ? (int) $entityId : null;
  68. } catch (\Exception $exception) {
  69. return null;
  70. }
  71. }
  72. /**
  73. * Read sales order id by using platform order id.
  74. *
  75. * @param string $extOrderId
  76. * @return int|null
  77. */
  78. public function getOrderIdByExtOrderId($extOrderId)
  79. {
  80. try {
  81. $connection = $this->getConnection();
  82. $tableName = $this->getMainTable();
  83. $table = $this->getTable($tableName);
  84. $select = $connection->select()
  85. ->from($table, OrderReferenceInterface::ORDER_ID)
  86. ->where('ext_order_id = :ext_order_id');
  87. $bind = [':ext_order_id' => (string)$extOrderId];
  88. $entityId = $connection->fetchOne($select, $bind);
  89. return $entityId ? (int) $entityId : null;
  90. } catch (\Exception $exception) {
  91. return null;
  92. }
  93. }
  94. /**
  95. * Read platform order id by using sales order id.
  96. *
  97. * @param string $orderId
  98. * @return string|null
  99. */
  100. public function getExtOrderIdByOrderId($orderId)
  101. {
  102. try {
  103. $connection = $this->getConnection();
  104. $tableName = $this->getMainTable();
  105. $table = $this->getTable($tableName);
  106. $select = $connection->select()
  107. ->from($table, OrderReferenceInterface::EXT_ORDER_ID)
  108. ->where('order_id = :order_id');
  109. $bind = [':order_id' => (string)$orderId];
  110. $extOrderId = $connection->fetchOne($select, $bind);
  111. return $extOrderId ? (string) $extOrderId : null;
  112. } catch (\Exception $exception) {
  113. return null;
  114. }
  115. }
  116. }