|
| 1 | +diff --git a/vendor/magento/module-purchase-order/Model/PurchaseOrderManagement.php b/vendor/magento/module-purchase-order/Model/PurchaseOrderManagement.php |
| 2 | +index 26c100baa1..2c52beab06 100644 |
| 3 | +--- a/vendor/magento/module-purchase-order/Model/PurchaseOrderManagement.php |
| 4 | ++++ b/vendor/magento/module-purchase-order/Model/PurchaseOrderManagement.php |
| 5 | +@@ -171,7 +171,20 @@ class PurchaseOrderManagement implements PurchaseOrderManagementInterface |
| 6 | + /** |
| 7 | + * @inheritdoc |
| 8 | + */ |
| 9 | +- public function createSalesOrder(PurchaseOrderInterface $purchaseOrder, $actorId = null) : OrderInterface |
| 10 | ++ public function createSalesOrder(PurchaseOrderInterface $purchaseOrder, $actorId = null): OrderInterface |
| 11 | ++ { |
| 12 | ++ $this->validatePurchaseOrderAndUpdateStatus($purchaseOrder); |
| 13 | ++ $quote = $this->getQuote($purchaseOrder->getQuoteId(), (string)$purchaseOrder->getIncrementId()); |
| 14 | ++ return $this->processOrder($purchaseOrder, $quote, $actorId); |
| 15 | ++ } |
| 16 | ++ |
| 17 | ++ /** |
| 18 | ++ * Validate purchase order and set correct status |
| 19 | ++ * |
| 20 | ++ * @param PurchaseOrderInterface $purchaseOrder |
| 21 | ++ * @throws LocalizedException |
| 22 | ++ */ |
| 23 | ++ private function validatePurchaseOrderAndUpdateStatus(PurchaseOrderInterface $purchaseOrder): void |
| 24 | + { |
| 25 | + if (!$this->validatorLocator->getValidator('placeorder')->validate($purchaseOrder)) { |
| 26 | + throw new LocalizedException( |
| 27 | +@@ -180,64 +193,57 @@ class PurchaseOrderManagement implements PurchaseOrderManagementInterface |
| 28 | + $purchaseOrder->getIncrementId() |
| 29 | + ) |
| 30 | + ); |
| 31 | +- }; |
| 32 | ++ } |
| 33 | + |
| 34 | + $purchaseOrder->setStatus(PurchaseOrderInterface::STATUS_ORDER_IN_PROGRESS); |
| 35 | + $this->purchaseOrderRepository->save($purchaseOrder); |
| 36 | ++ } |
| 37 | + |
| 38 | ++ /** |
| 39 | ++ * Fetch quote using quiteId |
| 40 | ++ * |
| 41 | ++ * @param int|string $quoteId |
| 42 | ++ * @param string $purchaseOrderIncrementId |
| 43 | ++ * @return CartInterface |
| 44 | ++ * @throws LocalizedException |
| 45 | ++ */ |
| 46 | ++ private function getQuote($quoteId, string $purchaseOrderIncrementId): CartInterface |
| 47 | ++ { |
| 48 | + try { |
| 49 | +- $quote = $this->quoteRepository->get($purchaseOrder->getQuoteId()); |
| 50 | ++ return $this->quoteRepository->get((int)$quoteId); |
| 51 | + } catch (NoSuchEntityException $e) { |
| 52 | + throw new LocalizedException( |
| 53 | + __( |
| 54 | + 'Order cannot be placed with purchase order #%1.', |
| 55 | +- $purchaseOrder->getIncrementId() |
| 56 | ++ $purchaseOrderIncrementId |
| 57 | + ) |
| 58 | + ); |
| 59 | + } |
| 60 | ++ } |
| 61 | + |
| 62 | ++ /** |
| 63 | ++ * Process order and send email for order confirmation |
| 64 | ++ * |
| 65 | ++ * @param PurchaseOrderInterface $purchaseOrder |
| 66 | ++ * @param CartInterface $quote |
| 67 | ++ * @param mixed $actorId |
| 68 | ++ * @return OrderInterface |
| 69 | ++ * @throws LocalizedException |
| 70 | ++ */ |
| 71 | ++ private function processOrder( |
| 72 | ++ PurchaseOrderInterface $purchaseOrder, |
| 73 | ++ CartInterface $quote, |
| 74 | ++ $actorId = null |
| 75 | ++ ): OrderInterface { |
| 76 | + try { |
| 77 | + $this->storeManager->setCurrentStore($quote->getStore()->getId()); |
| 78 | + $order = $this->placeOrder($quote); |
| 79 | +- $purchaseOrder->setOrderId($order->getId()); |
| 80 | +- $purchaseOrder->setOrderIncrementId($order->getIncrementId()); |
| 81 | +- $purchaseOrder->setStatus(PurchaseOrderInterface::STATUS_ORDER_PLACED); |
| 82 | +- $this->purchaseOrderRepository->save($purchaseOrder); |
| 83 | +- $this->purchaseOrderLogManagement->logAction( |
| 84 | +- $purchaseOrder, |
| 85 | +- 'place_order', |
| 86 | +- [ |
| 87 | +- 'increment_id' => $purchaseOrder->getIncrementId(), |
| 88 | +- 'order_increment_id' => $order->getIncrementId() |
| 89 | +- ], |
| 90 | +- $actorId |
| 91 | +- ); |
| 92 | +- |
| 93 | +- /** @var NegotiableQuoteInterface $negotiableQuote */ |
| 94 | +- $negotiableQuote = $quote->getExtensionAttributes()->getNegotiableQuote(); |
| 95 | +- |
| 96 | +- if ($negotiableQuote !== null && $negotiableQuote->getQuoteId() !== null) { |
| 97 | +- $negotiableQuote->setStatus(NegotiableQuoteInterface::STATUS_ORDERED); |
| 98 | +- $this->quoteRepository->save($quote); |
| 99 | +- $this->negotiableQuoteHistory->updateLog($negotiableQuote->getQuoteId()); |
| 100 | +- } |
| 101 | +- |
| 102 | +- $purchaseOrderIncrementId = $purchaseOrder->getIncrementId(); |
| 103 | +- $orderIncrementId = $order->getIncrementId(); |
| 104 | +- $grandTotal = $quote->getGrandTotal(); |
| 105 | +- $this->logger->info( |
| 106 | +- "Purchase Order Id: {$purchaseOrderIncrementId} & Order Id: {$orderIncrementId} & Total: {$grandTotal}" |
| 107 | +- ); |
| 108 | +- $this->orderEmailSender->send($order); |
| 109 | +- |
| 110 | +- /** @var NegotiableQuoteInterface $negotiableQuote */ |
| 111 | +- $negotiableQuote = $quote->getExtensionAttributes()->getNegotiableQuote(); |
| 112 | +- |
| 113 | +- if ($negotiableQuote !== null && $negotiableQuote->getQuoteId() !== null) { |
| 114 | +- $negotiableQuote->setStatus(NegotiableQuoteInterface::STATUS_ORDERED); |
| 115 | +- $this->quoteRepository->save($quote); |
| 116 | +- $this->negotiableQuoteHistory->updateLog($negotiableQuote->getQuoteId()); |
| 117 | ++ $this->updatePurchaseOrderAfterOrderPlacement($purchaseOrder, $order, $actorId); |
| 118 | ++ $this->logPurchaseOrder($purchaseOrder, $order, $quote); |
| 119 | ++ if (!$order->getEmailSent()) { |
| 120 | ++ $this->orderEmailSender->send($order); |
| 121 | + } |
| 122 | ++ $this->updateNegotiableQuoteStatus($quote); |
| 123 | + return $order; |
| 124 | + } catch (LocalizedException $e) { |
| 125 | + $purchaseOrder->setStatus(PurchaseOrderInterface::STATUS_ORDER_FAILED); |
| 126 | +@@ -245,7 +251,7 @@ class PurchaseOrderManagement implements PurchaseOrderManagementInterface |
| 127 | + throw $e; |
| 128 | + } catch (\Exception $exception) { |
| 129 | + $this->logger->critical($exception); |
| 130 | +- $this->failOrderPlace($purchaseOrder, 'An error occurred on the server. Please try again.'); |
| 131 | ++ $this->failOrderPlace($purchaseOrder, __('An error occurred on the server. Please try again.')); |
| 132 | + throw new LocalizedException( |
| 133 | + __('An error occurred on the server. Please try again.'), |
| 134 | + $exception |
| 135 | +@@ -253,6 +259,72 @@ class PurchaseOrderManagement implements PurchaseOrderManagementInterface |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | ++ /** |
| 140 | ++ * Update purchase order once order is placed |
| 141 | ++ * |
| 142 | ++ * @param PurchaseOrderInterface $purchaseOrder |
| 143 | ++ * @param OrderInterface $order |
| 144 | ++ * @param mixed|null $actorId |
| 145 | ++ * @throws CouldNotSaveException |
| 146 | ++ * @throws InputException |
| 147 | ++ */ |
| 148 | ++ private function updatePurchaseOrderAfterOrderPlacement( |
| 149 | ++ PurchaseOrderInterface $purchaseOrder, |
| 150 | ++ OrderInterface $order, |
| 151 | ++ $actorId = null |
| 152 | ++ ): void { |
| 153 | ++ $purchaseOrder->setOrderId($order->getId()); |
| 154 | ++ $purchaseOrder->setOrderIncrementId($order->getIncrementId()); |
| 155 | ++ $purchaseOrder->setStatus(PurchaseOrderInterface::STATUS_ORDER_PLACED); |
| 156 | ++ $this->purchaseOrderRepository->save($purchaseOrder); |
| 157 | ++ $this->purchaseOrderLogManagement->logAction( |
| 158 | ++ $purchaseOrder, |
| 159 | ++ 'place_order', |
| 160 | ++ [ |
| 161 | ++ 'increment_id' => $purchaseOrder->getIncrementId(), |
| 162 | ++ 'order_increment_id' => $order->getIncrementId() |
| 163 | ++ ], |
| 164 | ++ $actorId |
| 165 | ++ ); |
| 166 | ++ } |
| 167 | ++ |
| 168 | ++ /** |
| 169 | ++ * Record purchase order log |
| 170 | ++ * |
| 171 | ++ * @param PurchaseOrderInterface $purchaseOrder |
| 172 | ++ * @param OrderInterface $order |
| 173 | ++ * @param CartInterface $quote |
| 174 | ++ */ |
| 175 | ++ private function logPurchaseOrder( |
| 176 | ++ PurchaseOrderInterface $purchaseOrder, |
| 177 | ++ OrderInterface $order, |
| 178 | ++ CartInterface $quote |
| 179 | ++ ): void { |
| 180 | ++ $purchaseOrderIncrementId = $purchaseOrder->getIncrementId(); |
| 181 | ++ $orderIncrementId = $order->getIncrementId(); |
| 182 | ++ $grandTotal = $quote->getGrandTotal(); |
| 183 | ++ $this->logger->info( |
| 184 | ++ "Purchase Order Id: {$purchaseOrderIncrementId} & Order Id: {$orderIncrementId} & Total: {$grandTotal}" |
| 185 | ++ ); |
| 186 | ++ } |
| 187 | ++ |
| 188 | ++ /** |
| 189 | ++ * Update negotiable quote if exits |
| 190 | ++ * |
| 191 | ++ * @param CartInterface $quote |
| 192 | ++ */ |
| 193 | ++ private function updateNegotiableQuoteStatus(CartInterface $quote): void |
| 194 | ++ { |
| 195 | ++ /** @var NegotiableQuoteInterface $negotiableQuote */ |
| 196 | ++ $negotiableQuote = $quote->getExtensionAttributes()->getNegotiableQuote(); |
| 197 | ++ |
| 198 | ++ if ($negotiableQuote !== null && $negotiableQuote->getQuoteId() !== null) { |
| 199 | ++ $negotiableQuote->setStatus(NegotiableQuoteInterface::STATUS_ORDERED); |
| 200 | ++ $this->quoteRepository->save($quote); |
| 201 | ++ $this->negotiableQuoteHistory->updateLog($negotiableQuote->getQuoteId()); |
| 202 | ++ } |
| 203 | ++ } |
| 204 | ++ |
| 205 | + /** |
| 206 | + * Process order placement failure. |
| 207 | + * |
0 commit comments