![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/InventoryQuote/Model/QuoteManagement/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ namespace Ecombricks\InventoryQuote\Model\QuoteManagement; /** * Quote management model source trait */ trait SourceTrait { /** * New customer addresses * * @var array */ protected $newCustomerAddresses = []; /** * Get payment method checks * * @return array */ protected function getPaymentMethodChecks() { return [ \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT, \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY, \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY, \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX, \Magento\Payment\Model\Method\AbstractMethod::CHECK_ZERO_TOTAL, ]; } /** * Prepare quote payment * * @param \Magento\Quote\Api\Data\CartInterface $quote * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod * @return \Magento\Quote\Api\CartManagementInterface */ protected function prepareQuotePayment( \Magento\Quote\Api\Data\CartInterface $quote, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod = null ) { if ($paymentMethod === null) { $quote->collectTotals(); return $this; } $paymentMethod->setChecks($this->getPaymentMethodChecks()); $quotePayment = $quote->getPayment(); $quotePayment->setQuote($quote); $quotePayment->importData($paymentMethod->getData()); return $this; } /** * Prepare quote customer * * @param \Magento\Quote\Api\Data\CartInterface $quote * @return \Magento\Quote\Api\CartManagementInterface */ protected function prepareQuoteCustomer(\Magento\Quote\Api\Data\CartInterface $quote) { if ($quote->getCheckoutMethod() !== static::METHOD_GUEST) { return $this; } $quote->setCustomerId(null); $billingAddress = $quote->getBillingAddress(); $quote->setCustomerEmail($billingAddress->getEmail()); if ($quote->getCustomerFirstname() === null && $quote->getCustomerLastname() === null) { $quote->setCustomerFirstname($billingAddress->getFirstname()); $quote->setCustomerLastname($billingAddress->getLastname()); if ($billingAddress->getMiddlename() === null) { $quote->setCustomerMiddlename($billingAddress->getMiddlename()); } } $quote->setCustomerIsGuest(true); $quote->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID); return $this; } /** * Prepare quote * * @param \Magento\Quote\Api\Data\CartInterface $quote * @return \Magento\Quote\Api\CartManagementInterface */ protected function prepareQuote(\Magento\Quote\Api\Data\CartInterface $quote) { $remoteAddress = $this->inventoryRemoteAddress->getRemoteAddress(); if ($remoteAddress === false) { return $this; } $quote->setRemoteIp($remoteAddress); $quote->setXForwardedFor($this->inventoryRequest->getServer('HTTP_X_FORWARDED_FOR')); return $this; } /** * Update checkout session * * @param \Magento\Quote\Api\Data\CartInterface $quote * @param array $orders * @return \Magento\Quote\Api\CartManagementInterface */ protected function updateCheckoutSession( \Magento\Quote\Api\Data\CartInterface $quote, $orders ) { $quoteId = $quote->getId(); $this->checkoutSession->setLastQuoteId($quoteId); $this->checkoutSession->setLastSuccessQuoteId($quoteId); $orderIds = []; $orderIncrementIds = []; $orderStatuses = []; foreach ($orders as $order) { $orderId = $order->getId(); $orderIds[] = $orderId; $orderIncrementIds[$orderId] = $order->getIncrementId(); $orderStatuses[$orderId] = $order->getStatus(); } $this->checkoutSession->setLastOrderIds($orderIds); $this->checkoutSession->setLastRealOrderIds($orderIncrementIds); $this->checkoutSession->setLastOrderStatuses($orderStatuses); $lastOrder = end($orders); $this->checkoutSession->setLastOrderId($lastOrder->getId()); $this->checkoutSession->setLastRealOrderId($lastOrder->getIncrementId()); $this->checkoutSession->setLastOrderStatus($lastOrder->getStatus()); return $this; } /** * Prepare quote addresses for logged in customer * * @param \Magento\Quote\Api\Data\CartInterface $quote * @return $this */ protected function prepareQuoteAddressesForLoggedInCustomer(\Magento\Quote\Api\Data\CartInterface $quote) { if ($quote->getCustomerIsGuest()) { return $this; } if (!$quote->getCustomerId()) { $this->customerManagement->populateCustomerInfo($quote); return $this; } $quoteBillingAddress = $quote->getBillingAddress(); $quoteShippingAddress = $quote->isVirtual() ? null : $quote->getShippingAddress(); $customer = $this->customerRepository->getById($quote->getCustomerId()); $hasDefaultBilling = (bool) $customer->getDefaultBilling(); $hasDefaultShipping = (bool) $customer->getDefaultShipping(); if ( $quoteShippingAddress && !$quoteShippingAddress->getSameAsBilling() && (!$quoteShippingAddress->getCustomerId() || $quoteShippingAddress->getSaveInAddressBook()) ) { if ($quoteShippingAddress->getQuoteId()) { $customerShippingAddress = $quoteShippingAddress->exportCustomerAddress(); } else { $customerDefaultShippingAddress = $this->customerRepository->getById($customer->getId())->getDefaultShipping(); if ($customerDefaultShippingAddress) { try { $customerShippingAddress = $this->customerAddressRepository->getById($customerDefaultShippingAddress); } catch (\Magento\Framework\Exception\LocalizedException $exception) { } } } if (isset($customerShippingAddress)) { if (!$hasDefaultShipping) { $customerShippingAddress->setIsDefaultShipping(true); $hasDefaultShipping = true; if (!$hasDefaultBilling && !$quoteBillingAddress->getSaveInAddressBook()) { $customerShippingAddress->setIsDefaultBilling(true); $hasDefaultBilling = true; } } $customerShippingAddress->setCustomerId($quote->getCustomerId()); $this->customerAddressRepository->save($customerShippingAddress); $quote->addCustomerAddress($customerShippingAddress); $quoteShippingAddress->setCustomerAddressData($customerShippingAddress); $customerShippingAddressId = $customerShippingAddress->getId(); $this->newCustomerAddresses[] = $customerShippingAddressId; $quoteShippingAddress->setCustomerAddressId($customerShippingAddressId); } } if (!$quoteBillingAddress->getCustomerId() || $quoteBillingAddress->getSaveInAddressBook()) { if ($quoteBillingAddress->getQuoteId()) { $customerBillingAddress = $quoteBillingAddress->exportCustomerAddress(); } else { $customerDefaultBillingAddress = $this->customerRepository->getById($customer->getId())->getDefaultBilling(); if ($customerDefaultBillingAddress) { try { $customerBillingAddress = $this->customerAddressRepository->getById($customerDefaultBillingAddress); } catch (\Magento\Framework\Exception\LocalizedException $exception) { } } } if (isset($customerBillingAddress)) { if (!$hasDefaultBilling) { if (!$hasDefaultShipping) { $customerBillingAddress->setIsDefaultShipping(true); } $customerBillingAddress->setIsDefaultBilling(true); } $customerBillingAddress->setCustomerId($quote->getCustomerId()); $this->customerAddressRepository->save($customerBillingAddress); $quote->addCustomerAddress($customerBillingAddress); $quoteBillingAddress->setCustomerAddressData($customerBillingAddress); $customerBillingAddressId = $customerBillingAddress->getId(); $this->newCustomerAddresses[] = $customerBillingAddressId; $quoteBillingAddress->setCustomerAddressId($customerBillingAddressId); } } if ($quoteShippingAddress && !$quoteShippingAddress->getCustomerId() && !$hasDefaultBilling) { $quoteShippingAddress->setIsDefaultBilling(true); } $this->customerManagement->validateAddresses($quote); $this->customerManagement->populateCustomerInfo($quote); return $this; } /** * Delete new customer addresses * * @return $this */ protected function deleteNewCustomerAddresses() { if (empty($this->newCustomerAddresses)) { return $this; } foreach ($this->newCustomerAddresses as $customerAddressId) { $this->customerAddressRepository->deleteById($customerAddressId); } return $this; } /** * Copy quote address to order * * @param \Magento\Quote\Api\Data\AddressInterface $quoteAddress * @param \Magento\Sales\Api\Data\OrderInterface $order * @param array $orderData * @return $this */ protected function copyQuoteAddressToOrder( \Magento\Quote\Api\Data\AddressInterface $quoteAddress, \Magento\Sales\Api\Data\OrderInterface $order, $orderData = [] ) { $this->dataObjectHelper->mergeDataObjects( \Magento\Sales\Api\Data\OrderInterface::class, $order, $this->quoteAddressToOrder->convert($quoteAddress, $orderData) ); return $this; } /** * Convert quote address to order address * * @param \Magento\Quote\Api\Data\AddressInterface $quoteAddress * @return type */ protected function convertQuoteAddressToOrderAddress(\Magento\Quote\Api\Data\AddressInterface $quoteAddress) { return $this->quoteAddressToOrderAddress->convert( $quoteAddress, [ 'address_type' => $quoteAddress->getAddressType(), 'email' => $quoteAddress->getQuote()->getCustomerEmail(), ] ); } /** * Create source order items * * @param \Magento\Quote\Api\Data\CartInterface $quote * @return array */ protected function createSourceOrderItems(\Magento\Quote\Api\Data\CartInterface $quote) { $orderItems = []; foreach ($quote->getAllItems() as $quoteItem) { $quoteItemId = $quoteItem->getId(); if (!empty($orderItems[$quoteItemId])) { continue; } $parentQuoteItemId = $quoteItem->getParentItemId(); if ($parentQuoteItemId && !isset($orderItems[$parentQuoteItemId])) { $orderItems[$parentQuoteItemId] = $this->quoteItemToOrderItem->convert($quoteItem->getParentItem(), ['parent_item' => null]); } $parentOrderItem = isset($orderItems[$parentQuoteItemId]) ? $orderItems[$parentQuoteItemId] : null; $orderItems[$quoteItemId] = $this->quoteItemToOrderItem->convert($quoteItem, ['parent_item' => $parentOrderItem]); } return array_values($orderItems); } /** * Create source order * * @param \Magento\Quote\Api\Data\CartInterface $quote * @param string $sourceCode * @param array $orderData * @return \Magento\Sales\Api\Data\OrderInterface */ protected function createSourceOrder( \Magento\Quote\Api\Data\CartInterface $quote, $sourceCode, $orderData = [] ) { $order = $this->orderFactory->create(); $sourceOrderData = !empty($orderData[$sourceCode]) ? $orderData[$sourceCode] : []; $quote->unsReservedOrderId(); if (!empty($sourceOrderData['increment_id'])) { $quote->setReservedOrderId($sourceOrderData['increment_id']); } $quote->reserveOrderId(); $sourceQuote = $quote->createSourceClone($sourceCode); $orderAddresses = []; $quoteBillingAddress = $sourceQuote->getBillingAddress(); if (!$sourceQuote->isVirtual()) { $quoteShippingAddress = $sourceQuote->getShippingAddress(); $this->copyQuoteAddressToOrder($quoteShippingAddress, $order, $sourceOrderData); $orderShippingAddress = $this->convertQuoteAddressToOrderAddress($quoteShippingAddress); $orderShippingAddress->setData('quote_address_id', $quoteShippingAddress->getId()); $orderAddresses[] = $orderShippingAddress; $order->setShippingAddress($orderShippingAddress); $order->setShippingMethod($quoteShippingAddress->getShippingMethod()); $quoteShippingAddress->generateShippingDescription(); $order->setShippingDescription($quoteShippingAddress->getShippingDescription()); } else { $this->copyQuoteAddressToOrder($quoteBillingAddress, $order, $sourceOrderData); } $orderBillingAddress = $this->convertQuoteAddressToOrderAddress($quoteBillingAddress); $orderBillingAddress->setData('quote_address_id', $quoteBillingAddress->getId()); $orderAddresses[] = $orderBillingAddress; $order->setBillingAddress($orderBillingAddress); $order->setAddresses($orderAddresses); $order->setPayment($this->quotePaymentToOrderPayment->convert($sourceQuote->getPayment())); $order->setItems($this->createSourceOrderItems($sourceQuote)); if ($sourceQuote->getCustomer()) { $order->setCustomerId($sourceQuote->getCustomer()->getId()); } $order->setQuoteId($sourceQuote->getId()); $order->setCustomerEmail($sourceQuote->getCustomerEmail()); $order->setCustomerFirstname($sourceQuote->getCustomerFirstname()); $order->setCustomerMiddlename($sourceQuote->getCustomerMiddlename()); $order->setCustomerLastname($sourceQuote->getCustomerLastname()); return $order; } /** * Rollback customer addresses * * @param \Magento\Quote\Api\Data\CartInterface $quote * @param \Magento\Sales\Api\Data\OrderInterface $order * @param \Exception $exception * @return $this * @throws \Exception */ protected function rollbackCustomerAddresses( \Magento\Quote\Api\Data\CartInterface $quote, \Magento\Sales\Api\Data\OrderInterface $order, \Exception $exception ) { try { $this->deleteNewCustomerAddresses(); $this->eventManager->dispatch( 'sales_model_service_quote_submit_failure', ['order' => $order, 'quote' => $quote, 'exception' => $exception] ); } catch (\Exception $consecutiveException) { $message = sprintf( "An exception occurred on 'sales_model_service_quote_submit_failure' event: %s", $consecutiveException->getMessage() ); throw new \Exception($message, 0, $exception); } return $this; } /** * Submit quote * * @param \Magento\Quote\Api\Data\CartInterface $quote * @param array $orderData * @return array * @throws \Exception * @throws \Magento\Framework\Exception\LocalizedException */ protected function inventorySubmitQuote( \Magento\Quote\Api\Data\CartInterface $quote, $orderData = [] ) { $placedOrders = []; $this->inventoryQuoteValidator->validateBeforeSubmit($quote); $this->prepareQuoteAddressesForLoggedInCustomer($quote); foreach ($quote->getSourceCodes() as $sourceCode) { $order = $this->createSourceOrder($quote, $sourceCode, $orderData); $this->eventManager->dispatch('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]); try { $placedOrder = $this->orderManagement->place($order); $this->eventManager->dispatch('sales_model_service_quote_submit_success', ['order' => $placedOrder, 'quote' => $quote]); $placedOrders[$sourceCode] = $placedOrder; } catch (\Exception $exception) { $this->rollbackCustomerAddresses($quote, $order, $exception); throw $exception; } } if (!count($placedOrders)) { return $placedOrders; } $quote->setIsActive(false); $this->quoteRepository->save($quote); return $placedOrders; } /** * Submit * * @param \Magento\Quote\Api\Data\CartInterface $quote * @param array $orderData * @return array * @throws \Exception * @throws \Magento\Framework\Exception\LocalizedException */ public function inventorySubmit( \Magento\Quote\Api\Data\CartInterface $quote, $orderData = [] ) { if (!$quote->getAllVisibleItems()) { $quote->setIsActive(false); return []; } return $this->inventorySubmitQuote($quote, $orderData); } /** * Place order * * @param int $cartId * @param \Magento\Quote\Api\Data\PaymentInterface|null $paymentMethod * @throws \Magento\Framework\Exception\CouldNotSaveException * @return array */ public function inventoryPlaceOrder($cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod = null) { $quote = $this->quoteRepository->getActive($cartId); $this->prepareQuotePayment($quote, $paymentMethod); $this->prepareQuoteCustomer($quote); $this->prepareQuote($quote); $this->eventManager->dispatch('checkout_submit_before', ['quote' => $quote]); $orders = $this->submit($quote); if (!count($orders)) { throw new \Magento\Framework\Exception\LocalizedException( __('A server error stopped your orders from being placed. Please try to place your orders again.') ); } $this->updateCheckoutSession($quote, $orders); $orderIds = []; foreach ($orders as $order) { $orderIds[] = $order->getId(); $this->eventManager->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $quote]); } return $orderIds; } }