Example #1
0
 /**
  * Returns array of items
  *
  * @param \Magento\Sales\Model\Order $object
  * @return OrderItem[]
  */
 protected function getItems(\Magento\Sales\Model\Order $object)
 {
     $items = [];
     foreach ($object->getItemsCollection() as $item) {
         $items[] = $this->orderItemMapper->extractDto($item);
     }
     return $items;
 }
Example #2
0
 /**
  * Initialize creation data from existing order
  *
  * @param \Magento\Sales\Model\Order $order
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function initFromOrder(\Magento\Sales\Model\Order $order)
 {
     $session = $this->getSession();
     $session->setData($order->getReordered() ? 'reordered' : 'order_id', $order->getId());
     $session->setCurrencyId($order->getOrderCurrencyCode());
     /* Check if we edit guest order */
     $session->setCustomerId($order->getCustomerId() ?: false);
     $session->setStoreId($order->getStoreId());
     /* Initialize catalog rule data with new session values */
     $this->initRuleData();
     foreach ($order->getItemsCollection($this->_salesConfig->getAvailableProductTypes(), true) as $orderItem) {
         /* @var $orderItem \Magento\Sales\Model\Order\Item */
         if (!$orderItem->getParentItem()) {
             $qty = $orderItem->getQtyOrdered();
             if (!$order->getReordered()) {
                 $qty -= max($orderItem->getQtyShipped(), $orderItem->getQtyInvoiced());
             }
             if ($qty > 0) {
                 $item = $this->initFromOrderItem($orderItem, $qty);
                 if (is_string($item)) {
                     throw new \Magento\Framework\Exception\LocalizedException(__($item));
                 }
             }
         }
     }
     $shippingAddress = $order->getShippingAddress();
     if ($shippingAddress) {
         $addressDiff = array_diff_assoc($shippingAddress->getData(), $order->getBillingAddress()->getData());
         unset($addressDiff['address_type'], $addressDiff['entity_id']);
         $shippingAddress->setSameAsBilling(empty($addressDiff));
     }
     $this->_initBillingAddressFromOrder($order);
     $this->_initShippingAddressFromOrder($order);
     $quote = $this->getQuote();
     if (!$quote->isVirtual() && $this->getShippingAddress()->getSameAsBilling()) {
         $this->setShippingAsBilling(1);
     }
     $this->setShippingMethod($order->getShippingMethod());
     $quote->getShippingAddress()->setShippingDescription($order->getShippingDescription());
     $paymentData = $order->getPayment()->getData();
     unset($paymentData['cc_type'], $paymentData['cc_last_4']);
     unset($paymentData['cc_exp_month'], $paymentData['cc_exp_year']);
     $quote->getPayment()->addData($paymentData);
     $orderCouponCode = $order->getCouponCode();
     if ($orderCouponCode) {
         $quote->setCouponCode($orderCouponCode);
     }
     if ($quote->getCouponCode()) {
         $quote->collectTotals();
     }
     $this->_objectCopyService->copyFieldsetToTarget('sales_copy_order', 'to_edit', $order, $quote);
     $this->_eventManager->dispatch('sales_convert_order_to_quote', ['order' => $order, 'quote' => $quote]);
     if (!$order->getCustomerId()) {
         $quote->setCustomerIsGuest(true);
     }
     if ($session->getUseOldShippingMethod(true)) {
         /*
          * if we are making reorder or editing old order
          * we need to show old shipping as preselected
          * so for this we need to collect shipping rates
          */
         $this->collectShippingRates();
     } else {
         /*
          * if we are creating new order then we don't need to collect
          * shipping rates before customer hit appropriate button
          */
         $this->collectRates();
     }
     $this->quoteRepository->save($quote);
     return $this;
 }
 /**
  * @param \Magento\Sales\Model\Order $order
  * @return mixed
  */
 protected function getOrderItemForTransaction(\Magento\Sales\Model\Order $order)
 {
     $order->getItemByQuoteItemId($order->getQuoteId());
     foreach ($order->getItemsCollection() as $item) {
         if (!$item->isDeleted() && !$item->getParentItemId()) {
             return $item;
         }
     }
     return null;
 }
Example #4
0
 /**
  * @param \Magento\Sales\Model\Order $order
  * @return string
  */
 protected function _getReceiptOrderLines(\Magento\Sales\Model\Order $order)
 {
     $myReceiptOrderLines = "";
     $currency = $order->getOrderCurrencyCode();
     $formattedAmountValue = $this->_currencyFactory->create()->format($order->getGrandTotal(), ['display' => \Magento\Framework\Currency::NO_SYMBOL], false);
     $taxAmount = $order->getTaxAmount();
     $formattedTaxAmount = $this->_currencyFactory->create()->format($taxAmount, ['display' => \Magento\Framework\Currency::NO_SYMBOL], false);
     $myReceiptOrderLines .= "---||C\n" . "====== YOUR ORDER DETAILS ======||CB\n" . "---||C\n" . " No. Description |Piece  Subtotal|\n";
     foreach ($order->getItemsCollection() as $item) {
         //skip dummies
         if ($item->isDummy()) {
             continue;
         }
         $singlePriceFormat = $this->_currencyFactory->create()->format($item->getPriceInclTax(), ['display' => \Magento\Framework\Currency::NO_SYMBOL], false);
         $itemAmount = $item->getPriceInclTax() * (int) $item->getQtyOrdered();
         $itemAmountFormat = $this->_currencyFactory->create()->format($itemAmount, ['display' => \Magento\Framework\Currency::NO_SYMBOL], false);
         $myReceiptOrderLines .= "  " . (int) $item->getQtyOrdered() . "  " . trim(substr($item->getName(), 0, 25)) . "| " . $currency . " " . $singlePriceFormat . "  " . $currency . " " . $itemAmountFormat . "|\n";
     }
     //discount cost
     if ($order->getDiscountAmount() > 0 || $order->getDiscountAmount() < 0) {
         $discountAmountFormat = $this->_currencyFactory->create()->format($order->getDiscountAmount(), ['display' => \Magento\Framework\Currency::NO_SYMBOL], false);
         $myReceiptOrderLines .= "  " . 1 . " " . $this->__('Total Discount') . "| " . $currency . " " . $discountAmountFormat . "|\n";
     }
     //shipping cost
     if ($order->getShippingAmount() > 0 || $order->getShippingTaxAmount() > 0) {
         $shippingAmountFormat = $this->_currencyFactory->create()->format($order->getShippingAmount(), ['display' => \Magento\Framework\Currency::NO_SYMBOL], false);
         $myReceiptOrderLines .= "  " . 1 . " " . $order->getShippingDescription() . "| " . $currency . " " . $shippingAmountFormat . "|\n";
     }
     if ($order->getPaymentFeeAmount() > 0) {
         $paymentFeeAmount = $this->_currencyFactory->create()->format($order->getPaymentFeeAmount(), ['display' => \Magento\Framework\Currency::NO_SYMBOL], false);
         $myReceiptOrderLines .= "  " . 1 . " " . $this->__('Payment Fee') . "| " . $currency . " " . $paymentFeeAmount . "|\n";
     }
     $myReceiptOrderLines .= "|--------|\n" . "|Order Total:  " . $currency . " " . $formattedAmountValue . "|B\n" . "|Tax:  " . $currency . " " . $formattedTaxAmount . "|B\n" . "||C\n";
     /*
      * New header for card details section!
      * Default location is After Header so simply add to Order Details as separator
      */
     $myReceiptOrderLines .= "---||C\n" . "====== YOUR PAYMENT DETAILS ======||CB\n" . "---||C\n";
     return $myReceiptOrderLines;
 }