AbstractTotal.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote\Address\Total;
  7. /**
  8. * Sales Quote Address Total abstract model
  9. *
  10. * @api
  11. * @SuppressWarnings(PHPMD.NumberOfChildren)
  12. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  13. * @since 100.0.2
  14. */
  15. abstract class AbstractTotal implements CollectorInterface, ReaderInterface
  16. {
  17. /**
  18. * Total Code name
  19. *
  20. * @var string
  21. */
  22. protected $_code;
  23. /**
  24. * @var string
  25. */
  26. protected $_address = null;
  27. /**
  28. * Various abstract abilities
  29. *
  30. * @var bool
  31. */
  32. protected $_canAddAmountToAddress = true;
  33. /**
  34. * Various abstract abilities
  35. *
  36. * @var bool
  37. */
  38. protected $_canSetAddressAmount = true;
  39. /**
  40. * Key for item row total getting
  41. *
  42. * @var string
  43. */
  44. protected $_itemRowTotalKey = null;
  45. /**
  46. * Set total code code name
  47. *
  48. * @param string $code
  49. * @return $this
  50. */
  51. public function setCode($code)
  52. {
  53. $this->_code = $code;
  54. return $this;
  55. }
  56. /**
  57. * Retrieve total code name
  58. *
  59. * @return string
  60. */
  61. public function getCode()
  62. {
  63. return $this->_code;
  64. }
  65. /**
  66. * Label getter
  67. *
  68. * @return string
  69. */
  70. public function getLabel()
  71. {
  72. return '';
  73. }
  74. /**
  75. * Collect totals process.
  76. *
  77. * @param \Magento\Quote\Model\Quote $quote
  78. * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
  79. * @param \Magento\Quote\Model\Quote\Address\Total $total
  80. * @return $this
  81. */
  82. public function collect(
  83. \Magento\Quote\Model\Quote $quote,
  84. \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
  85. \Magento\Quote\Model\Quote\Address\Total $total
  86. ) {
  87. $this->_setAddress($shippingAssignment->getShipping()->getAddress());
  88. $this->_setTotal($total);
  89. /**
  90. * Reset amounts
  91. */
  92. $this->_setAmount(0);
  93. $this->_setBaseAmount(0);
  94. return $this;
  95. }
  96. /**
  97. * Fetch (Retrieve data as array)
  98. *
  99. * @param \Magento\Quote\Model\Quote $quote
  100. * @param \Magento\Quote\Model\Quote\Address\Total $total
  101. * @return array
  102. * @internal param \Magento\Quote\Model\Quote\Address $address
  103. */
  104. public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
  105. {
  106. return [];
  107. }
  108. /**
  109. * Set address which can be used inside totals calculation
  110. *
  111. * @param \Magento\Quote\Model\Quote\Address $address
  112. * @return $this
  113. */
  114. protected function _setAddress(\Magento\Quote\Model\Quote\Address $address)
  115. {
  116. $this->_address = $address;
  117. return $this;
  118. }
  119. /**
  120. * Get quote address object
  121. *
  122. * @return \Magento\Quote\Model\Quote\Address
  123. * @throws \Magento\Framework\Exception\LocalizedException if address not declared
  124. */
  125. protected function _getAddress()
  126. {
  127. if ($this->_address === null) {
  128. throw new \Magento\Framework\Exception\LocalizedException(__('The address model is not defined.'));
  129. }
  130. return $this->_address;
  131. }
  132. /**
  133. * @var \Magento\Quote\Model\Quote\Address\Total
  134. */
  135. protected $total;
  136. /**
  137. * @param \Magento\Quote\Model\Quote\Address\Total $total
  138. * @return $this
  139. */
  140. public function _setTotal(\Magento\Quote\Model\Quote\Address\Total $total)
  141. {
  142. $this->total = $total;
  143. return $this;
  144. }
  145. /**
  146. * @return \Magento\Quote\Model\Quote\Address\Total
  147. */
  148. protected function _getTotal()
  149. {
  150. return $this->total;
  151. }
  152. /**
  153. * Set total model amount value to address
  154. *
  155. * @param float $amount
  156. * @return $this
  157. */
  158. protected function _setAmount($amount)
  159. {
  160. if ($this->_canSetAddressAmount) {
  161. $this->_getTotal()->setTotalAmount($this->getCode(), $amount);
  162. }
  163. return $this;
  164. }
  165. /**
  166. * Set total model base amount value to address
  167. *
  168. * @param float $baseAmount
  169. * @internal param float $amount
  170. * @return $this
  171. */
  172. protected function _setBaseAmount($baseAmount)
  173. {
  174. if ($this->_canSetAddressAmount) {
  175. $this->_getTotal()->setBaseTotalAmount($this->getCode(), $baseAmount);
  176. }
  177. return $this;
  178. }
  179. /**
  180. * Add total model amount value to address
  181. *
  182. * @param float $amount
  183. * @return $this
  184. */
  185. protected function _addAmount($amount)
  186. {
  187. if ($this->_canAddAmountToAddress) {
  188. $this->_getTotal()->addTotalAmount($this->getCode(), $amount);
  189. }
  190. return $this;
  191. }
  192. /**
  193. * Add total model base amount value to address
  194. *
  195. * @param float $baseAmount
  196. * @return $this
  197. */
  198. protected function _addBaseAmount($baseAmount)
  199. {
  200. if ($this->_canAddAmountToAddress) {
  201. $this->_getTotal()->addBaseTotalAmount($this->getCode(), $baseAmount);
  202. }
  203. return $this;
  204. }
  205. /**
  206. * Get all items
  207. *
  208. * @param \Magento\Quote\Model\Quote\Address $address
  209. * @return array
  210. */
  211. protected function _getAddressItems(\Magento\Quote\Model\Quote\Address $address)
  212. {
  213. return $address->getAllItems();
  214. }
  215. /**
  216. * Getter for row default total
  217. *
  218. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  219. * @return float|int
  220. */
  221. public function getItemRowTotal(\Magento\Quote\Model\Quote\Item\AbstractItem $item)
  222. {
  223. if (!$this->_itemRowTotalKey) {
  224. return 0;
  225. }
  226. return $item->getDataUsingMethod($this->_itemRowTotalKey);
  227. }
  228. /**
  229. * Getter for row default base total
  230. *
  231. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  232. * @return float|int
  233. */
  234. public function getItemBaseRowTotal(\Magento\Quote\Model\Quote\Item\AbstractItem $item)
  235. {
  236. if (!$this->_itemRowTotalKey) {
  237. return 0;
  238. }
  239. return $item->getDataUsingMethod('base_' . $this->_itemRowTotalKey);
  240. }
  241. /**
  242. * Whether the item row total may be compounded with others
  243. *
  244. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  245. * @return bool
  246. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  247. */
  248. public function getIsItemRowTotalCompoundable(\Magento\Quote\Model\Quote\Item\AbstractItem $item)
  249. {
  250. if ($item->getData("skip_compound_{$this->_itemRowTotalKey}")) {
  251. return false;
  252. }
  253. return true;
  254. }
  255. /**
  256. * Process model configuration array.
  257. * This method can be used for changing models apply sort order
  258. *
  259. * @param array $config
  260. * @param store $store
  261. * @return array
  262. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  263. */
  264. public function processConfigArray($config, $store)
  265. {
  266. return $config;
  267. }
  268. }