AbstractItems.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model\Sales\Order\Pdf\Items;
  7. use Magento\Catalog\Model\Product\Type\AbstractType;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Serialize\Serializer\Json;
  10. /**
  11. * Order pdf items renderer
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. abstract class AbstractItems extends \Magento\Sales\Model\Order\Pdf\Items\AbstractItems
  16. {
  17. /**
  18. * Serializer
  19. *
  20. * @var Json
  21. */
  22. private $serializer;
  23. /**
  24. * Constructor
  25. *
  26. * @param \Magento\Framework\Model\Context $context
  27. * @param \Magento\Framework\Registry $registry
  28. * @param \Magento\Tax\Helper\Data $taxData
  29. * @param \Magento\Framework\Filesystem $filesystem
  30. * @param \Magento\Framework\Filter\FilterManager $filterManager
  31. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  32. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  33. * @param array $data
  34. * @param \Magento\Framework\Serialize\Serializer\Json $serializer
  35. */
  36. public function __construct(
  37. \Magento\Framework\Model\Context $context,
  38. \Magento\Framework\Registry $registry,
  39. \Magento\Tax\Helper\Data $taxData,
  40. \Magento\Framework\Filesystem $filesystem,
  41. \Magento\Framework\Filter\FilterManager $filterManager,
  42. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  43. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  44. array $data = [],
  45. Json $serializer = null
  46. ) {
  47. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  48. parent::__construct(
  49. $context,
  50. $registry,
  51. $taxData,
  52. $filesystem,
  53. $filterManager,
  54. $resource,
  55. $resourceCollection,
  56. $data
  57. );
  58. }
  59. /**
  60. * Getting all available children for Invoice, Shipment or CreditMemo item
  61. *
  62. * @param \Magento\Framework\DataObject $item
  63. * @return array
  64. */
  65. public function getChildren($item)
  66. {
  67. $itemsArray = [];
  68. $items = null;
  69. if ($item instanceof \Magento\Sales\Model\Order\Invoice\Item) {
  70. $items = $item->getInvoice()->getAllItems();
  71. } elseif ($item instanceof \Magento\Sales\Model\Order\Shipment\Item) {
  72. $items = $item->getShipment()->getAllItems();
  73. } elseif ($item instanceof \Magento\Sales\Model\Order\Creditmemo\Item) {
  74. $items = $item->getCreditmemo()->getAllItems();
  75. }
  76. if ($items) {
  77. foreach ($items as $value) {
  78. $parentItem = $value->getOrderItem()->getParentItem();
  79. if ($parentItem) {
  80. $itemsArray[$parentItem->getId()][$value->getOrderItemId()] = $value;
  81. } else {
  82. $itemsArray[$value->getOrderItem()->getId()][$value->getOrderItemId()] = $value;
  83. }
  84. }
  85. }
  86. if (isset($itemsArray[$item->getOrderItem()->getId()])) {
  87. return $itemsArray[$item->getOrderItem()->getId()];
  88. }
  89. return null;
  90. }
  91. /**
  92. * Retrieve is Shipment Separately flag for Item
  93. *
  94. * @param \Magento\Framework\DataObject $item
  95. * @return bool
  96. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  97. */
  98. public function isShipmentSeparately($item = null)
  99. {
  100. if ($item) {
  101. if ($item->getOrderItem()) {
  102. $item = $item->getOrderItem();
  103. }
  104. $parentItem = $item->getParentItem();
  105. if ($parentItem) {
  106. $options = $parentItem->getProductOptions();
  107. if ($options) {
  108. return (isset($options['shipment_type'])
  109. && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY);
  110. }
  111. } else {
  112. $options = $item->getProductOptions();
  113. if ($options) {
  114. return !(isset($options['shipment_type'])
  115. && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY);
  116. }
  117. }
  118. }
  119. $options = $this->getOrderItem()->getProductOptions();
  120. if ($options) {
  121. if (isset($options['shipment_type']) && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY) {
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. /**
  128. * Retrieve is Child Calculated
  129. *
  130. * @param \Magento\Framework\DataObject $item
  131. * @return bool
  132. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  133. */
  134. public function isChildCalculated($item = null)
  135. {
  136. if ($item) {
  137. if ($item->getOrderItem()) {
  138. $item = $item->getOrderItem();
  139. }
  140. $parentItem = $item->getParentItem();
  141. if ($parentItem) {
  142. $options = $parentItem->getProductOptions();
  143. if ($options) {
  144. return (isset($options['product_calculations'])
  145. && $options['product_calculations'] == AbstractType::CALCULATE_CHILD);
  146. }
  147. } else {
  148. $options = $item->getProductOptions();
  149. if ($options) {
  150. return !(isset($options['product_calculations'])
  151. && $options['product_calculations'] == AbstractType::CALCULATE_CHILD);
  152. }
  153. }
  154. }
  155. $options = $this->getOrderItem()->getProductOptions();
  156. if ($options) {
  157. if (isset($options['product_calculations'])
  158. && $options['product_calculations'] == AbstractType::CALCULATE_CHILD
  159. ) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. /**
  166. * Retrieve Bundle Options
  167. *
  168. * @param \Magento\Framework\DataObject $item
  169. * @return array
  170. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  171. */
  172. public function getBundleOptions($item = null)
  173. {
  174. $options = $this->getOrderItem()->getProductOptions();
  175. if ($options && isset($options['bundle_options'])) {
  176. return $options['bundle_options'];
  177. }
  178. return [];
  179. }
  180. /**
  181. * Retrieve Selection attributes
  182. *
  183. * @param \Magento\Framework\DataObject $item
  184. * @return mixed
  185. */
  186. public function getSelectionAttributes($item)
  187. {
  188. if ($item instanceof \Magento\Sales\Model\Order\Item) {
  189. $options = $item->getProductOptions();
  190. } else {
  191. $options = $item->getOrderItem()->getProductOptions();
  192. }
  193. if (isset($options['bundle_selection_attributes'])) {
  194. return $this->serializer->unserialize($options['bundle_selection_attributes']);
  195. }
  196. return null;
  197. }
  198. /**
  199. * Retrieve Order options
  200. *
  201. * @param \Magento\Framework\DataObject $item
  202. * @return array
  203. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  204. */
  205. public function getOrderOptions($item = null)
  206. {
  207. $result = [];
  208. $options = $this->getOrderItem()->getProductOptions();
  209. if ($options) {
  210. if (isset($options['options'])) {
  211. $result = array_merge($result, $options['options']);
  212. }
  213. if (isset($options['additional_options'])) {
  214. $result = array_merge($result, $options['additional_options']);
  215. }
  216. if (!empty($options['attributes_info'])) {
  217. $result = array_merge($options['attributes_info'], $result);
  218. }
  219. }
  220. return $result;
  221. }
  222. /**
  223. * Retrieve Order Item
  224. *
  225. * @return \Magento\Sales\Model\Order\Item
  226. */
  227. public function getOrderItem()
  228. {
  229. if ($this->getItem() instanceof \Magento\Sales\Model\Order\Item) {
  230. return $this->getItem();
  231. }
  232. return $this->getItem()->getOrderItem();
  233. }
  234. /**
  235. * Retrieve Value HTML
  236. *
  237. * @param \Magento\Sales\Model\Order\Item $item
  238. * @return string
  239. */
  240. public function getValueHtml($item)
  241. {
  242. $result = $this->filterManager->stripTags($item->getName());
  243. if (!$this->isShipmentSeparately($item)) {
  244. $attributes = $this->getSelectionAttributes($item);
  245. if ($attributes) {
  246. $result = $this->filterManager->sprintf($attributes['qty'], ['format' => '%d']) . ' x ' . $result;
  247. }
  248. }
  249. if (!$this->isChildCalculated($item)) {
  250. $attributes = $this->getSelectionAttributes($item);
  251. if ($attributes) {
  252. $result .= " " . $this->filterManager->stripTags(
  253. $this->getOrderItem()->getOrder()->formatPrice($attributes['price'])
  254. );
  255. }
  256. }
  257. return $result;
  258. }
  259. /**
  260. * Can show price info for item
  261. *
  262. * @param \Magento\Sales\Model\Order\Item $item
  263. * @return bool
  264. */
  265. public function canShowPriceInfo($item)
  266. {
  267. if ($item->getOrderItem()->getParentItem() && $this->isChildCalculated() ||
  268. !$item->getOrderItem()->getParentItem() && !$this->isChildCalculated()
  269. ) {
  270. return true;
  271. }
  272. return false;
  273. }
  274. }