RateQuery.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate;
  7. /**
  8. * Query builder for table rate
  9. */
  10. class RateQuery
  11. {
  12. /**
  13. * @var \Magento\Quote\Model\Quote\Address\RateRequest
  14. */
  15. private $request;
  16. /**
  17. * RateQuery constructor.
  18. * @param \Magento\Quote\Model\Quote\Address\RateRequest $request
  19. */
  20. public function __construct(
  21. \Magento\Quote\Model\Quote\Address\RateRequest $request
  22. ) {
  23. $this->request = $request;
  24. }
  25. /**
  26. * Prepare select
  27. *
  28. * @param \Magento\Framework\DB\Select $select
  29. * @return \Magento\Framework\DB\Select
  30. */
  31. public function prepareSelect(\Magento\Framework\DB\Select $select)
  32. {
  33. $select->where(
  34. 'website_id = :website_id'
  35. )->order(
  36. ['dest_country_id DESC', 'dest_region_id DESC', 'dest_zip DESC', 'condition_value DESC']
  37. )->limit(
  38. 1
  39. );
  40. // Render destination condition
  41. $orWhere = '(' . implode(
  42. ') OR (',
  43. [
  44. "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = :postcode",
  45. "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = :postcode_prefix",
  46. "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = ''",
  47. // Handle asterisk in dest_zip field
  48. "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = '*'",
  49. "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = '*'",
  50. "dest_country_id = '0' AND dest_region_id = :region_id AND dest_zip = '*'",
  51. "dest_country_id = '0' AND dest_region_id = 0 AND dest_zip = '*'",
  52. "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ''",
  53. "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = :postcode",
  54. "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = :postcode_prefix"
  55. ]
  56. ) . ')';
  57. $select->where($orWhere);
  58. // Render condition by condition name
  59. if (is_array($this->request->getConditionName())) {
  60. $orWhere = [];
  61. foreach (range(0, count($this->request->getConditionName())) as $conditionNumber) {
  62. $bindNameKey = sprintf(':condition_name_%d', $conditionNumber);
  63. $bindValueKey = sprintf(':condition_value_%d', $conditionNumber);
  64. $orWhere[] = "(condition_name = {$bindNameKey} AND condition_value <= {$bindValueKey})";
  65. }
  66. if ($orWhere) {
  67. $select->where(implode(' OR ', $orWhere));
  68. }
  69. } else {
  70. $select->where('condition_name = :condition_name');
  71. $select->where('condition_value <= :condition_value');
  72. }
  73. return $select;
  74. }
  75. /**
  76. * Returns query bindings
  77. *
  78. * @return array
  79. */
  80. public function getBindings()
  81. {
  82. $bind = [
  83. ':website_id' => (int)$this->request->getWebsiteId(),
  84. ':country_id' => $this->request->getDestCountryId(),
  85. ':region_id' => (int)$this->request->getDestRegionId(),
  86. ':postcode' => $this->request->getDestPostcode(),
  87. ':postcode_prefix' => $this->getDestPostcodePrefix()
  88. ];
  89. // Render condition by condition name
  90. if (is_array($this->request->getConditionName())) {
  91. $i = 0;
  92. foreach ($this->request->getConditionName() as $conditionName) {
  93. $bindNameKey = sprintf(':condition_name_%d', $i);
  94. $bindValueKey = sprintf(':condition_value_%d', $i);
  95. $bind[$bindNameKey] = $conditionName;
  96. $bind[$bindValueKey] = $this->request->getData($conditionName);
  97. $i++;
  98. }
  99. } else {
  100. $bind[':condition_name'] = $this->request->getConditionName();
  101. $bind[':condition_value'] = round($this->request->getData($this->request->getConditionName()), 4);
  102. }
  103. return $bind;
  104. }
  105. /**
  106. * Returns rate request
  107. *
  108. * @return \Magento\Quote\Model\Quote\Address\RateRequest
  109. */
  110. public function getRequest()
  111. {
  112. return $this->request;
  113. }
  114. /**
  115. * Returns the entire postcode if it contains no dash or the part of it prior to the dash in the other case
  116. *
  117. * @return string
  118. */
  119. private function getDestPostcodePrefix()
  120. {
  121. if (!preg_match("/^(.+)-(.+)$/", $this->request->getDestPostcode(), $zipParts)) {
  122. return $this->request->getDestPostcode();
  123. }
  124. return $zipParts[1];
  125. }
  126. }