RmaShipment.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\Rma;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Framework\Model\AbstractModel;
  8. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  9. use Temando\Shipping\Setup\RmaSetupSchema;
  10. /**
  11. * The RMA Shipment Resource Model grants access to the RMA-Shipment associations.
  12. *
  13. * @package Temando\Shipping\Model
  14. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  15. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link http://www.temando.com/
  17. */
  18. class RmaShipment extends AbstractDb
  19. {
  20. const RMA_ID = 'rma_id';
  21. const RMA_SHIPMENT_ID = 'ext_shipment_id';
  22. /**
  23. * Resource initialization
  24. *
  25. * @return void
  26. */
  27. protected function _construct()
  28. {
  29. $this->_setMainTable(RmaSetupSchema::TABLE_RMA_SHIPMENT);
  30. }
  31. /**
  32. * @param AbstractModel $object
  33. * @param int $value
  34. * @param string|null $field
  35. *
  36. * @return $this
  37. * @throws LocalizedException
  38. */
  39. public function load(AbstractModel $object, $value, $field = null)
  40. {
  41. if (!$field || $field === self::RMA_SHIPMENT_ID) {
  42. // query RMA ID for given shipment
  43. $connection = $this->getConnection();
  44. $table = $this->getMainTable();
  45. $select = $connection
  46. ->select()
  47. ->from($table, self::RMA_ID)
  48. ->where(self::RMA_SHIPMENT_ID . ' = ?', $value);
  49. $rmaId = $connection->fetchOne($select);
  50. $object->setData([
  51. self::RMA_ID => $rmaId,
  52. self::RMA_SHIPMENT_ID => $value,
  53. ]);
  54. }
  55. return $this;
  56. }
  57. /**
  58. * @param AbstractModel $object
  59. *
  60. * @return $this
  61. * @throws \Exception
  62. */
  63. public function save(AbstractModel $object)
  64. {
  65. $rmaId = $object->getData(self::RMA_ID);
  66. $shipmentId = $object->getData(self::RMA_SHIPMENT_ID);
  67. $this->saveShipmentIds($rmaId, [$shipmentId]);
  68. return $this;
  69. }
  70. /**
  71. * Query all external shipment IDs associated to the given RMA ID.
  72. *
  73. * @param int $rmaId
  74. *
  75. * @return string[]
  76. */
  77. public function getShipmentIds($rmaId)
  78. {
  79. try {
  80. $connection = $this->getConnection();
  81. $table = $this->getMainTable();
  82. $select = $connection
  83. ->select()
  84. ->from($table, self::RMA_SHIPMENT_ID)
  85. ->where(self::RMA_ID . ' = ?', $rmaId);
  86. return $connection->fetchCol($select);
  87. } catch (\Exception $exception) {
  88. return [];
  89. }
  90. }
  91. /**
  92. * @param int $rmaId
  93. * @param string[] $shipmentIds
  94. * @return int Number of saved entries
  95. * @throws \Exception
  96. */
  97. public function saveShipmentIds($rmaId, array $shipmentIds)
  98. {
  99. $connection = $this->getConnection();
  100. $table = $this->getMainTable();
  101. $data = array_map(function ($shipmentId) use ($rmaId) {
  102. return [
  103. self::RMA_ID => $rmaId,
  104. self::RMA_SHIPMENT_ID => $shipmentId,
  105. ];
  106. }, $shipmentIds);
  107. return $connection->insertOnDuplicate($table, $data);
  108. }
  109. /**
  110. * @param int $rmaId
  111. * @param string[] $shipmentIds
  112. * @return int Number of dropped entries
  113. * @throws \Exception
  114. */
  115. public function deleteShipmentIds($rmaId, array $shipmentIds)
  116. {
  117. $connection = $this->getConnection();
  118. $table = $this->getMainTable();
  119. $conditions = [
  120. sprintf("`%s` = ?", self::RMA_ID),
  121. sprintf("`%s` in (?)", self::RMA_SHIPMENT_ID),
  122. ];
  123. $terms = [
  124. $rmaId,
  125. $shipmentIds,
  126. ];
  127. $where = array_combine($conditions, $terms);
  128. return $connection->delete($table, $where);
  129. }
  130. }