OrderCollection.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Batch;
  6. use Magento\Sales\Api\Data\OrderInterface;
  7. use Magento\Sales\Model\ResourceModel\Order\Collection;
  8. use Temando\Shipping\Setup\SetupSchema;
  9. /**
  10. * Temando Batch Order Collection
  11. *
  12. * @package Temando\Shipping\Model
  13. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  14. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class OrderCollection extends Collection
  19. {
  20. /**
  21. * @param string $carrierCode
  22. */
  23. public function addCarrierFilter($carrierCode)
  24. {
  25. $this->addFieldToFilter('shipping_method', ['like' => ["$carrierCode%"]]);
  26. }
  27. /**
  28. * Filter orders which can be shipped. This is not complete. Filtering
  29. * remaining items in a loop is still necessary.
  30. * @see \Magento\Sales\Model\Order::canShip()
  31. *
  32. * @link https://magento.stackexchange.com/a/72600
  33. *
  34. * @return void
  35. */
  36. public function addCanShipFilter()
  37. {
  38. $orderItemTable = $this->getTable('sales_order_item');
  39. $pickupLocationTable = $this->getTable(SetupSchema::TABLE_ORDER_PICKUP_LOCATION);
  40. $select = $this->getSelect();
  41. $select->join(
  42. ['order_item' => $orderItemTable],
  43. 'main_table.entity_id = order_item.order_id',
  44. []
  45. );
  46. $select->joinLeft(
  47. ['pickup_location' => $pickupLocationTable],
  48. 'main_table.shipping_address_id = pickup_location.recipient_address_id',
  49. []
  50. );
  51. $this->addFieldToFilter(sprintf('main_table.%s', OrderInterface::IS_VIRTUAL), 0);
  52. $this->addFieldToFilter(OrderInterface::STATE, ['nin' => [
  53. \Magento\Sales\Model\Order::STATE_HOLDED,
  54. \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW,
  55. \Magento\Sales\Model\Order::STATE_CANCELED,
  56. ]]);
  57. $this->addExpressionFieldToSelect(
  58. 'qty_to_ship',
  59. 'FLOOR(SUM({{qty_ordered}}) - SUM({{qty_shipped}}))',
  60. [
  61. 'qty_ordered' => 'order_item.qty_ordered',
  62. 'qty_shipped' => 'order_item.qty_shipped',
  63. ]
  64. );
  65. $this->addExpressionFieldToSelect(
  66. 'locked',
  67. 'SUM(COALESCE(locked_do_ship, 0))',
  68. [
  69. 'qty_ordered' => 'order_item.qty_ordered',
  70. 'qty_shipped' => 'order_item.qty_shipped',
  71. ]
  72. );
  73. $select->group(['main_table.entity_id']);
  74. $select->having('qty_to_ship > 0 AND locked = 0');
  75. $select->where('pickup_location.pickup_location_id IS NULL');
  76. }
  77. /**
  78. * Retain GROUP BY, use sub-select for counting.
  79. *
  80. * @return \Magento\Framework\DB\Select
  81. */
  82. public function getSelectCountSql()
  83. {
  84. $this->_renderFilters();
  85. $select = clone $this->getSelect();
  86. $select->reset(\Magento\Framework\DB\Select::ORDER);
  87. $select->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
  88. $select->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
  89. $countSelect = $this->_conn->select();
  90. $countSelect->from(['s' => $select]);
  91. $countSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
  92. $countSelect->columns(new \Zend_Db_Expr('COUNT(*)'));
  93. return $countSelect;
  94. }
  95. }