Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function create($orderId)
 {
     $order = $this->orderRepository->get($orderId);
     if ($order->getCustomerId()) {
         throw new AlreadyExistsException(__("This order already has associated customer account"));
     }
     $customerData = $this->objectCopyService->copyFieldsetToTarget('order_address', 'to_customer', $order->getBillingAddress(), []);
     $addresses = $order->getAddresses();
     foreach ($addresses as $address) {
         $addressData = $this->objectCopyService->copyFieldsetToTarget('order_address', 'to_customer_address', $address, []);
         /** @var \Magento\Customer\Api\Data\AddressInterface $customerAddress */
         $customerAddress = $this->addressFactory->create(['data' => $addressData]);
         if (is_string($address->getRegion())) {
             /** @var \Magento\Customer\Api\Data\RegionInterface $region */
             $region = $this->regionFactory->create();
             $region->setRegion($address->getRegion());
             $region->setRegionCode($address->getRegionCode());
             $region->setRegionId($address->getRegionId());
             $customerAddress->setRegion($region);
         }
         $customerData['addresses'][] = $customerAddress;
     }
     /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
     $customer = $this->customerFactory->create(['data' => $customerData]);
     $account = $this->accountManagement->createAccount($customer);
     $order->setCustomerId($account->getId());
     $this->orderRepository->save($order);
     return $account;
 }
 public function test_execute()
 {
     /** @var \Magento\Framework\Event\Observer $event */
     $event = $this->_manObj->create(\Magento\Framework\Event\Observer::class);
     /** @var \Magento\Sales\Api\Data\OrderInterface $order */
     $order = $this->_mageRepoSaleOrder->get(1);
     $event->setData(CheckoutSubmitAllAfter::DATA_ORDER, $order);
     $this->obj->execute($event);
 }
 public function test_execute()
 {
     $event = new \Magento\Framework\Event\Observer();
     /* load Magento order */
     $mageOrder = $this->repoSaleOrder->get(1);
     $event->setOrder($mageOrder);
     $resp = $this->obj->execute($event);
     $this->assertNotNull($resp);
 }
 public function test_orderSave()
 {
     $req = new Request\OrderSave();
     /* load Magento order */
     $mageOrder = $this->_mageRepoSaleOrder->get(70);
     $req->setSaleOrder($mageOrder);
     $resp = $this->obj->orderSave($req);
     $this->assertNotNull($resp);
 }
 /**
  * Select orders to be pushed into Odoo (in case of "on event" push was failed).
  */
 public function getOrdersToReplicate()
 {
     $result = [];
     $orders = $this->_repoSaleOrder->getIdsToSaveToOdoo();
     foreach ($orders as $data) {
         $id = $data[Cfg::E_SALE_ORDER_A_ENTITY_ID];
         $order = $this->_repoMageSalesOrder->get($id);
         $result[$id] = $order;
     }
     return $result;
 }
Esempio n. 6
0
 /**
  * Check is it possible to reorder
  *
  * @param int $orderId
  * @return bool
  */
 public function canReorder($orderId)
 {
     $order = $this->orderRepository->get($orderId);
     if (!$this->isAllowed($order->getStore())) {
         return false;
     }
     if ($this->customerSession->isLoggedIn()) {
         return $order->canReorder();
     } else {
         return true;
     }
 }
Esempio n. 7
0
 /**
  * Retrieve the order the shipment for created for
  *
  * @return \Magento\Sales\Model\Order
  */
 public function getOrder()
 {
     if (!$this->_order instanceof \Magento\Sales\Model\Order) {
         $this->_order = $this->orderRepository->get($this->getOrderId());
     }
     return $this->_order->setHistoryEntityName($this->entityType);
 }
 /**
  * Initialize shipment model instance
  *
  * @return bool|\Magento\Sales\Model\Order\Shipment
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function load()
 {
     $shipment = false;
     $orderId = $this->getOrderId();
     $shipmentId = $this->getShipmentId();
     if ($shipmentId) {
         $shipment = $this->shipmentRepository->get($shipmentId);
     } elseif ($orderId) {
         $order = $this->orderRepository->get($orderId);
         /**
          * Check order existing
          */
         if (!$order->getId()) {
             $this->messageManager->addError(__('The order no longer exists.'));
             return false;
         }
         /**
          * Check shipment is available to create separate from invoice
          */
         if ($order->getForcedShipmentWithInvoice()) {
             $this->messageManager->addError(__('Cannot do shipment for the order separately from invoice.'));
             return false;
         }
         /**
          * Check shipment create availability
          */
         if (!$order->canShip()) {
             $this->messageManager->addError(__('Cannot do shipment for the order.'));
             return false;
         }
         $shipment = $this->shipmentFactory->create($order, $this->getItemQtys(), $this->getTracking());
     }
     $this->registry->register('current_shipment', $shipment);
     return $shipment;
 }
Esempio n. 9
0
 /**
  * Update status in Magento2 Order
  *
  * @return bool
  */
 private function updateOrderStatus()
 {
     $this->_library->setEnvironment();
     $this->_library->setCharset();
     $this->_library->setLog();
     $transaction = $this->getTransaction();
     $order = $this->_order->get($this->_helperData->getReferenceDecryptOrderID($transaction->getReference()));
     $status = $this->_helperData->getStatusFromKey($transaction->getStatus());
     if (!$this->compareStatus($status, $order->getStatus())) {
         $history = array('status' => $this->_history->setStatus($status), 'comment' => $this->_history->setComment('PagSeguro Notification'));
         $transactionCode = $transaction->getCode();
         $orderId = $order->getId();
         $order->setStatus($status);
         $order->setStatusHistories($history);
         $order->save();
         $this->updateSalesOrderGridTransactionCode($orderId, $transactionCode);
         $this->updatePagSeguroOrdersTransactionCode($orderId, $transactionCode);
     }
     return true;
 }
 /**
  * @param \Magento\Framework\Event\Observer $observer
  * @throws \Exception
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $orderIds = $observer->getEvent()->getData('order_ids');
     if (isset($orderIds[0])) {
         $order = $this->orderRepository->get($orderIds[0]);
         if (!$order->getCustomerId()) {
             $isEmailAvailable = $this->accountManagement->isEmailAvailable($order->getCustomerEmail());
             //$this->logger->addInfo(print_r($isEmailAvailable,true));
             if ($isEmailAvailable) {
                 try {
                     $this->orderCustomerService->create($orderIds[0]);
                 } catch (\Exception $e) {
                     $this->messageManager->addException($e, $e->getMessage());
                     throw $e;
                 }
                 $this->coreRegistry->register('automatic_account', true);
             }
         }
     }
 }
Esempio n. 11
0
 /**
  * Validate order addresses
  *
  * @return bool
  */
 protected function validateAddresses()
 {
     $order = $this->orderRepository->get($this->checkoutSession->getLastOrderId());
     $addresses = $order->getAddresses();
     foreach ($addresses as $address) {
         $result = $this->addressValidator->validateForCustomer($address);
         if (is_array($result) && !empty($result)) {
             return false;
         }
     }
     return true;
 }
 /**
  * @param int $orderId
  * @param string $message
  */
 protected function addOrderComment($orderId, $message)
 {
     /* @var $order \Magento\Sales\Api\Data\OrderInterface */
     $order = $this->orderRepository->get($orderId);
     // create comment
     $orderStatusHistory = $this->orderStatusHistoryFactory->create();
     $orderStatusHistory->setParentId($orderId);
     $orderStatusHistory->setComment($message);
     $orderStatusHistory->setIsCustomerNotified(false);
     $orderStatusHistory->setIsVisibleOnFront(false);
     $orderStatusHistory->setEntityName(Queue::ENTITY_TYPE_CODE_ORDER);
     $orderStatusHistory->setStatus($order->getStatus());
     // add comment to order
     $this->orderManagement->addComment($orderId, $orderStatusHistory);
 }
Esempio n. 13
0
 /**
  * Initialize order model instance
  *
  * @return \Magento\Sales\Api\Data\OrderInterface|false
  */
 protected function _initOrder()
 {
     $id = $this->getRequest()->getParam('order_id');
     try {
         $order = $this->orderRepository->get($id);
     } catch (NoSuchEntityException $e) {
         $this->messageManager->addError(__('This order no longer exists.'));
         $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
         return false;
     } catch (InputException $e) {
         $this->messageManager->addError(__('This order no longer exists.'));
         $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
         return false;
     }
     $this->_coreRegistry->register('sales_order', $order);
     $this->_coreRegistry->register('current_order', $order);
     return $order;
 }
Esempio n. 14
0
 /**
  * Order un hold
  *
  * @param int $id
  * @return bool
  */
 public function unHold($id)
 {
     return (bool) $this->orderRepository->get($id)->unhold();
 }
Esempio n. 15
0
 /**
  * Retrieve order model object
  *
  * @codeCoverageIgnore
  * @return Order
  */
 public function getOrder()
 {
     if (!$this->_order && $this->getParentId()) {
         $this->_order = $this->orderRepository->get($this->getParentId());
     }
     return $this->_order;
 }
Esempio n. 16
0
 /**
  * Set order instance for transaction depends on transaction behavior
  * If $order equals to true, method isn't loading new order instance.
  *
  * @param \Magento\Sales\Model\Order|null|boolean $order
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function setOrder($order = null)
 {
     if (null === $order || $order === true) {
         if ($this->getOrderId()) {
             $this->_order = $this->orderRepository->get($this->getOrderId());
         } else {
             $this->_order = false;
         }
     } elseif (!$this->getId() || $this->getOrderId() == $order->getId()) {
         $this->_order = $order;
     } else {
         throw new \Magento\Framework\Exception\LocalizedException(__('Set order for existing transactions not allowed'));
     }
     return $this;
 }
Esempio n. 17
0
 /**
  * Checks whether the capture is final
  *
  * @param string $orderId
  * @param string $amount
  * @return bool
  */
 protected function isFinalCapture($orderId, $amount)
 {
     if (!empty($orderId)) {
         $order = $this->orderRepository->get($orderId);
         return (double) $order->getTotalDue() === (double) $amount;
     }
     return false;
 }
Esempio n. 18
0
 /**
  * Order un hold
  *
  * @param int $id
  * @return bool
  */
 public function unHold($id)
 {
     $object = $this->orderRepository->get($id);
     $object->unhold();
     return (bool) $this->orderRepository->save($object);
 }
Esempio n. 19
0
 /**
  * Creates and returns a populated getTaxRequest for a invoice
  *
  * @param \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface $object
  * @return GetTaxRequest
  */
 public function getGetTaxRequestForSalesObject($object)
 {
     $order = $this->orderRepository->get($object->getOrderId());
     $lines = [];
     $items = $object->getItems();
     $this->taxClassHelper->populateCorrectTaxClasses($items, $object->getStoreId());
     /** @var \Magento\Tax\Api\Data\QuoteDetailsItemInterface $item */
     foreach ($items as $item) {
         $line = $this->interactionLine->getLine($item);
         if ($line) {
             $lines[] = $line;
         }
     }
     $objectIsCreditMemo = $object instanceof \Magento\Sales\Api\Data\CreditmemoInterface;
     $credit = $objectIsCreditMemo;
     $line = $this->interactionLine->getShippingLine($object, $credit);
     if ($line) {
         $lines[] = $line;
     }
     $line = $this->interactionLine->getGiftWrapItemsLine($object, $credit);
     if ($line) {
         $lines[] = $line;
     }
     $line = $this->interactionLine->getGiftWrapOrderLine($object, $credit);
     if ($line) {
         $lines[] = $line;
     }
     $line = $this->interactionLine->getGiftWrapCardLine($object, $credit);
     if ($line) {
         $lines[] = $line;
     }
     if ($objectIsCreditMemo) {
         $line = $this->interactionLine->getPositiveAdjustmentLine($object);
         if ($line) {
             $lines[] = $line;
         }
         $line = $this->interactionLine->getNegativeAdjustmentLine($object);
         if ($line) {
             $lines[] = $line;
         }
     }
     /** @var \Magento\Sales\Api\Data\OrderAddressInterface $address */
     if (!$order->getIsVirtual()) {
         $address = $order->getShippingAddress();
     } else {
         $address = $order->getBillingAddress();
     }
     $avaTaxAddress = $this->address->getAddress($address);
     $store = $this->storeRepository->getById($object->getStoreId());
     $currentDate = $this->getFormattedDate($store, $object->getCreatedAt());
     $taxOverride = null;
     if ($object instanceof \Magento\Sales\Api\Data\InvoiceInterface) {
         $docType = DocumentType::$SalesInvoice;
         if ($this->areTimesDifferentDays($order->getCreatedAt(), $object->getCreatedAt(), $object->getStoreId())) {
             $taxCalculationDate = $this->getFormattedDate($store, $order->getCreatedAt());
             // Set the tax date for calculation
             $taxOverride = $this->taxOverrideFactory->create();
             $taxOverride->setTaxDate($taxCalculationDate);
             $taxOverride->setTaxOverrideType(\AvaTax\TaxOverrideType::$TaxDate);
             $taxOverride->setTaxAmount(0.0);
             $taxOverride->setReason(self::AVATAX_INVOICE_OVERRIDE_REASON);
         }
     } else {
         $docType = DocumentType::$ReturnInvoice;
         $invoice = $this->getInvoice($object->getInvoiceId());
         // If a Creditmemo was generated for an invoice, use the created_at value from the invoice
         if ($invoice) {
             $taxCalculationDate = $this->getFormattedDate($store, $invoice->getCreatedAt());
         } else {
             $taxCalculationDate = $this->getFormattedDate($store, $order->getCreatedAt());
         }
         // Set the tax date for calculation
         $taxOverride = $this->taxOverrideFactory->create();
         $taxOverride->setTaxDate($taxCalculationDate);
         $taxOverride->setTaxOverrideType(\AvaTax\TaxOverrideType::$TaxDate);
         $taxOverride->setTaxAmount(0.0);
         $taxOverride->setReason(self::AVATAX_CREDITMEMO_OVERRIDE_REASON);
     }
     $customer = $this->getCustomerById($order->getCustomerId());
     $customerUsageType = $customer ? $this->taxClassHelper->getAvataxTaxCodeForCustomer($customer) : null;
     $data = ['StoreId' => $store->getId(), 'Commit' => $this->config->getCommitSubmittedTransactions($store), 'TaxOverride' => $taxOverride, 'CurrencyCode' => $order->getOrderCurrencyCode(), 'CustomerCode' => $this->getCustomerCode($order), 'CustomerUsageType' => $customerUsageType, 'DestinationAddress' => $avaTaxAddress, 'DocCode' => $object->getIncrementId() . '123-' . rand(10000000, 90000000000), 'DocDate' => $currentDate, 'DocType' => $docType, 'ExchangeRate' => $this->getExchangeRate($store, $order->getBaseCurrencyCode(), $order->getOrderCurrencyCode()), 'ExchangeRateEffDate' => $currentDate, 'Lines' => $lines, 'DetailLevel' => DetailLevel::$Document, 'PaymentDate' => $currentDate, 'PurchaseOrderNo' => $object->getIncrementId()];
     $data = array_merge($this->retrieveGetTaxRequestFields($store, $address, $object), $data);
     try {
         $data = $this->metaDataObject->validateData($data);
     } catch (ValidationException $e) {
         $this->avaTaxLogger->error('Error validating data: ' . $e->getMessage(), ['data' => var_export($data, true)]);
     }
     /** @var $getTaxRequest GetTaxRequest */
     $getTaxRequest = $this->getTaxRequestFactory->create();
     $this->populateGetTaxRequest($data, $getTaxRequest);
     return $getTaxRequest;
 }