Create.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Controller\Adminhtml\Order;
  7. use Magento\Backend\App\Action;
  8. use Magento\Framework\View\Result\PageFactory;
  9. use Magento\Backend\Model\View\Result\ForwardFactory;
  10. /**
  11. * Adminhtml sales orders creation process controller
  12. *
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @SuppressWarnings(PHPMD.NumberOfChildren)
  15. */
  16. abstract class Create extends \Magento\Backend\App\Action
  17. {
  18. /**
  19. * @var \Magento\Framework\Escaper
  20. */
  21. protected $escaper;
  22. /**
  23. * @var PageFactory
  24. */
  25. protected $resultPageFactory;
  26. /**
  27. * @var \Magento\Backend\Model\View\Result\ForwardFactory
  28. */
  29. protected $resultForwardFactory;
  30. /**
  31. * @param Action\Context $context
  32. * @param \Magento\Catalog\Helper\Product $productHelper
  33. * @param \Magento\Framework\Escaper $escaper
  34. * @param PageFactory $resultPageFactory
  35. * @param ForwardFactory $resultForwardFactory
  36. */
  37. public function __construct(
  38. Action\Context $context,
  39. \Magento\Catalog\Helper\Product $productHelper,
  40. \Magento\Framework\Escaper $escaper,
  41. PageFactory $resultPageFactory,
  42. ForwardFactory $resultForwardFactory
  43. ) {
  44. parent::__construct($context);
  45. $productHelper->setSkipSaleableCheck(true);
  46. $this->escaper = $escaper;
  47. $this->resultPageFactory = $resultPageFactory;
  48. $this->resultForwardFactory = $resultForwardFactory;
  49. }
  50. /**
  51. * Retrieve session object
  52. *
  53. * @return \Magento\Backend\Model\Session\Quote
  54. */
  55. protected function _getSession()
  56. {
  57. return $this->_objectManager->get(\Magento\Backend\Model\Session\Quote::class);
  58. }
  59. /**
  60. * Retrieve quote object
  61. *
  62. * @return \Magento\Quote\Model\Quote
  63. */
  64. protected function _getQuote()
  65. {
  66. return $this->_getSession()->getQuote();
  67. }
  68. /**
  69. * Retrieve order create model
  70. *
  71. * @return \Magento\Sales\Model\AdminOrder\Create
  72. */
  73. protected function _getOrderCreateModel()
  74. {
  75. return $this->_objectManager->get(\Magento\Sales\Model\AdminOrder\Create::class);
  76. }
  77. /**
  78. * Retrieve gift message save model
  79. *
  80. * @return \Magento\GiftMessage\Model\Save
  81. */
  82. protected function _getGiftmessageSaveModel()
  83. {
  84. return $this->_objectManager->get(\Magento\GiftMessage\Model\Save::class);
  85. }
  86. /**
  87. * Initialize order creation session data
  88. *
  89. * @return $this
  90. */
  91. protected function _initSession()
  92. {
  93. /**
  94. * Identify customer
  95. */
  96. if ($customerId = $this->getRequest()->getParam('customer_id')) {
  97. $this->_getSession()->setCustomerId((int)$customerId);
  98. }
  99. /**
  100. * Identify store
  101. */
  102. if ($storeId = $this->getRequest()->getParam('store_id')) {
  103. $this->_getSession()->setStoreId((int)$storeId);
  104. }
  105. /**
  106. * Identify currency
  107. */
  108. if ($currencyId = $this->getRequest()->getParam('currency_id')) {
  109. $this->_getSession()->setCurrencyId((string)$currencyId);
  110. $this->_getOrderCreateModel()->setRecollect(true);
  111. }
  112. return $this;
  113. }
  114. /**
  115. * Processing request data
  116. *
  117. * @return $this
  118. */
  119. protected function _processData()
  120. {
  121. return $this->_processActionData();
  122. }
  123. /**
  124. * Process request data with additional logic for saving quote and creating order
  125. *
  126. * @param string $action
  127. * @return $this
  128. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  129. * @SuppressWarnings(PHPMD.NPathComplexity)
  130. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  131. */
  132. protected function _processActionData($action = null)
  133. {
  134. $eventData = [
  135. 'order_create_model' => $this->_getOrderCreateModel(),
  136. 'request_model' => $this->getRequest(),
  137. 'session' => $this->_getSession(),
  138. ];
  139. $this->_eventManager->dispatch('adminhtml_sales_order_create_process_data_before', $eventData);
  140. /**
  141. * Import post data, in order to make order quote valid
  142. */
  143. if ($data = $this->getRequest()->getPost('order')) {
  144. $this->_getOrderCreateModel()->importPostData($data);
  145. }
  146. /**
  147. * Initialize catalog rule data
  148. */
  149. $this->_getOrderCreateModel()->initRuleData();
  150. /**
  151. * init first billing address, need for virtual products
  152. */
  153. $this->_getOrderCreateModel()->getBillingAddress();
  154. /**
  155. * Flag for using billing address for shipping
  156. */
  157. if (!$this->_getOrderCreateModel()->getQuote()->isVirtual()) {
  158. $syncFlag = $this->getRequest()->getPost('shipping_as_billing');
  159. $shippingMethod = $this->_getOrderCreateModel()->getShippingAddress()->getShippingMethod();
  160. if ($syncFlag === null
  161. && $this->_getOrderCreateModel()->getShippingAddress()->getSameAsBilling() && empty($shippingMethod)
  162. ) {
  163. $this->_getOrderCreateModel()->setShippingAsBilling(1);
  164. } else {
  165. $this->_getOrderCreateModel()->setShippingAsBilling((int)$syncFlag);
  166. }
  167. }
  168. /**
  169. * Change shipping address flag
  170. */
  171. if (!$this->_getOrderCreateModel()->getQuote()->isVirtual() && $this->getRequest()->getPost('reset_shipping')
  172. ) {
  173. $this->_getOrderCreateModel()->resetShippingMethod(true);
  174. }
  175. /**
  176. * Collecting shipping rates
  177. */
  178. if (!$this->_getOrderCreateModel()->getQuote()->isVirtual() && $this->getRequest()->getPost(
  179. 'collect_shipping_rates'
  180. )
  181. ) {
  182. $this->_getOrderCreateModel()->collectShippingRates();
  183. }
  184. /**
  185. * Apply mass changes from sidebar
  186. */
  187. if ($data = $this->getRequest()->getPost('sidebar')) {
  188. $this->_getOrderCreateModel()->applySidebarData($data);
  189. }
  190. $this->_eventManager->dispatch('adminhtml_sales_order_create_process_item_before', $eventData);
  191. /**
  192. * Adding product to quote from shopping cart, wishlist etc.
  193. */
  194. if ($productId = (int)$this->getRequest()->getPost('add_product')) {
  195. $this->_getOrderCreateModel()->addProduct($productId, $this->getRequest()->getPostValue());
  196. }
  197. /**
  198. * Adding products to quote from special grid
  199. */
  200. if ($this->getRequest()->has('item') && !$this->getRequest()->getPost('update_items') && !($action == 'save')
  201. ) {
  202. $items = $this->getRequest()->getPost('item');
  203. $items = $this->_processFiles($items);
  204. $this->_getOrderCreateModel()->addProducts($items);
  205. }
  206. /**
  207. * Update quote items
  208. */
  209. if ($this->getRequest()->getPost('update_items')) {
  210. $items = $this->getRequest()->getPost('item', []);
  211. $items = $this->_processFiles($items);
  212. $this->_getOrderCreateModel()->updateQuoteItems($items);
  213. }
  214. /**
  215. * Remove quote item
  216. */
  217. $removeItemId = (int)$this->getRequest()->getPost('remove_item');
  218. $removeFrom = (string)$this->getRequest()->getPost('from');
  219. if ($removeItemId && $removeFrom) {
  220. $this->_getOrderCreateModel()->removeItem($removeItemId, $removeFrom);
  221. $this->_getOrderCreateModel()->recollectCart();
  222. }
  223. /**
  224. * Move quote item
  225. */
  226. $moveItemId = (int)$this->getRequest()->getPost('move_item');
  227. $moveTo = (string)$this->getRequest()->getPost('to');
  228. $moveQty = (int)$this->getRequest()->getPost('qty');
  229. if ($moveItemId && $moveTo) {
  230. $this->_getOrderCreateModel()->moveQuoteItem($moveItemId, $moveTo, $moveQty);
  231. }
  232. $this->_eventManager->dispatch('adminhtml_sales_order_create_process_item_after', $eventData);
  233. if ($paymentData = $this->getRequest()->getPost('payment')) {
  234. $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
  235. }
  236. $eventData = [
  237. 'order_create_model' => $this->_getOrderCreateModel(),
  238. 'request' => $this->getRequest()->getPostValue(),
  239. ];
  240. $this->_eventManager->dispatch('adminhtml_sales_order_create_process_data', $eventData);
  241. $this->_getOrderCreateModel()->saveQuote();
  242. if ($paymentData = $this->getRequest()->getPost('payment')) {
  243. $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
  244. }
  245. /**
  246. * Saving of giftmessages
  247. */
  248. $giftmessages = $this->getRequest()->getPost('giftmessage');
  249. if ($giftmessages) {
  250. $this->_getGiftmessageSaveModel()->setGiftmessages($giftmessages)->saveAllInQuote();
  251. }
  252. /**
  253. * Importing gift message allow items from specific product grid
  254. */
  255. if ($data = $this->getRequest()->getPost('add_products')) {
  256. $this->_getGiftmessageSaveModel()->importAllowQuoteItemsFromProducts(
  257. $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonDecode($data)
  258. );
  259. }
  260. /**
  261. * Importing gift message allow items on update quote items
  262. */
  263. if ($this->getRequest()->getPost('update_items')) {
  264. $items = $this->getRequest()->getPost('item', []);
  265. $this->_getGiftmessageSaveModel()->importAllowQuoteItemsFromItems($items);
  266. }
  267. $data = $this->getRequest()->getPost('order');
  268. $couponCode = '';
  269. if (isset($data) && isset($data['coupon']['code'])) {
  270. $couponCode = trim($data['coupon']['code']);
  271. }
  272. if (!empty($couponCode)) {
  273. $isApplyDiscount = false;
  274. foreach ($this->_getQuote()->getAllItems() as $item) {
  275. if (!$item->getNoDiscount()) {
  276. $isApplyDiscount = true;
  277. break;
  278. }
  279. }
  280. if (!$isApplyDiscount) {
  281. $this->messageManager->addErrorMessage(
  282. __(
  283. '"%1" coupon code was not applied. Do not apply discount is selected for item(s)',
  284. $this->escaper->escapeHtml($couponCode)
  285. )
  286. );
  287. } else {
  288. if ($this->_getQuote()->getCouponCode() !== $couponCode) {
  289. $this->messageManager->addErrorMessage(
  290. __(
  291. 'The "%1" coupon code isn\'t valid. Verify the code and try again.',
  292. $this->escaper->escapeHtml($couponCode)
  293. )
  294. );
  295. } else {
  296. $this->messageManager->addSuccessMessage(__('The coupon code has been accepted.'));
  297. }
  298. }
  299. }
  300. return $this;
  301. }
  302. /**
  303. * Process buyRequest file options of items
  304. *
  305. * @param array $items
  306. * @return array
  307. */
  308. protected function _processFiles($items)
  309. {
  310. /* @var $productHelper \Magento\Catalog\Helper\Product */
  311. $productHelper = $this->_objectManager->get(\Magento\Catalog\Helper\Product::class);
  312. foreach ($items as $id => $item) {
  313. $buyRequest = new \Magento\Framework\DataObject($item);
  314. $params = ['files_prefix' => 'item_' . $id . '_'];
  315. $buyRequest = $productHelper->addParamsToBuyRequest($buyRequest, $params);
  316. if ($buyRequest->hasData()) {
  317. $items[$id] = $buyRequest->toArray();
  318. }
  319. }
  320. return $items;
  321. }
  322. /**
  323. * @return $this
  324. */
  325. protected function _reloadQuote()
  326. {
  327. $id = $this->_getQuote()->getId();
  328. $this->_getQuote()->load($id);
  329. return $this;
  330. }
  331. /**
  332. * Acl check for admin
  333. *
  334. * @return bool
  335. */
  336. protected function _isAllowed()
  337. {
  338. return $this->_authorization->isAllowed($this->_getAclResource());
  339. }
  340. /**
  341. * Get acl resource
  342. *
  343. * @return string
  344. */
  345. protected function _getAclResource()
  346. {
  347. $action = strtolower($this->getRequest()->getActionName());
  348. if (in_array($action, ['index', 'save', 'cancel']) && $this->_getSession()->getReordered()) {
  349. $action = 'reorder';
  350. }
  351. switch ($action) {
  352. case 'index':
  353. case 'save':
  354. $aclResource = 'Magento_Sales::create';
  355. break;
  356. case 'reorder':
  357. $aclResource = 'Magento_Sales::reorder';
  358. break;
  359. case 'cancel':
  360. $aclResource = 'Magento_Sales::cancel';
  361. break;
  362. default:
  363. $aclResource = 'Magento_Sales::actions';
  364. break;
  365. }
  366. return $aclResource;
  367. }
  368. }