public function updateOrder(ShopgateOrder $order)
 {
     $shopgateOrder = PSShopgateOrder::instanceByOrderNumber($order->getOrderNumber());
     if (!Validate::isLoadedObject($shopgateOrder)) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_ORDER_NOT_FOUND, 'Order not found', true);
     }
     $order_states = array();
     if ($order->getUpdatePayment() && $order->getIsPaid()) {
         array_push($order_states, $this->getOrderStateId('PS_OS_PAYMENT'));
     }
     if ($order->getUpdateShipping() && !$order->getIsShippingBlocked()) {
         array_push($order_states, $this->getOrderStateId('PS_OS_PREPARATION'));
     }
     if (count($order_states)) {
         $ps_order = new Order($shopgateOrder->id_order);
         foreach ($order_states as $id_order_state) {
             if (version_compare(_PS_VERSION_, '1.4.1.0', '<')) {
                 $history = new OrderHistory();
                 $history->id_order = (int) $shopgateOrder->id_order;
                 $history->changeIdOrderState((int) $id_order_state, (int) $shopgateOrder->id_order);
             } else {
                 $ps_order->setCurrentState($id_order_state);
             }
         }
     }
     return array('external_order_id' => $shopgateOrder->id_order, 'external_order_number' => $shopgateOrder->id_order);
 }
Example #2
0
 /**
  * @param Mage_Sales_Model_Order                  $magentoOrder
  * @param ShopgateOrder                           $shopgateOrder
  * @param Shopgate_Framework_Model_Shopgate_Order $magentoShopgateOrder
  *
  * @return Mage_Sales_Model_Order
  */
 protected function _setShopgateOrder($magentoOrder, $shopgateOrder, $magentoShopgateOrder = null)
 {
     if ($magentoShopgateOrder) {
         if ($shopgateOrder->getUpdatePayment()) {
             $magentoShopgateOrder->setIsPaid($shopgateOrder->getIsPaid());
         }
         if ($shopgateOrder->getUpdateShipping()) {
             $magentoShopgateOrder->setIsShippingBlocked($shopgateOrder->getIsShippingBlocked());
         }
     } else {
         $magentoShopgateOrder = Mage::getModel("shopgate/shopgate_order")->setOrderId($magentoOrder->getId())->setStoreId($this->_getConfig()->getStoreViewId())->setShopgateOrderNumber($shopgateOrder->getOrderNumber())->setIsShippingBlocked($shopgateOrder->getIsShippingBlocked())->setIsPaid($shopgateOrder->getIsPaid())->setIsTest($shopgateOrder->getIsTest())->setIsCustomerInvoiceBlocked($shopgateOrder->getIsCustomerInvoiceBlocked());
     }
     $magentoShopgateOrder->setReceivedData(serialize($shopgateOrder));
     $magentoShopgateOrder->save();
     return $magentoOrder;
 }
 /**
  * calculate the the quantity to cancel at shopgate
  *
  * @param \ShopgateOrder     $sgOrder this is the shopgate object which was stored into
  *                                     the database on addOrder request
  * @param array             $cancelledItems
  * @param                  $message
  * @param \ShopgateLogger  $log
  *
  * @return array
  * @throws \ShopgateLibraryException
  */
 private function getCancellationData(ShopgateOrder $sgOrder, $cancelledItems, &$message, ShopgateLogger $log)
 {
     if (version_compare(_PS_VERSION_, '1.5.0', '>=')) {
         $pItems = OrderDetailCore::getList($this->id_order);
     } else {
         $pOrder = new OrderCore($this->id_order);
         $pItems = $pOrder->getProductsDetail();
     }
     if (empty($pItems)) {
         $errorMessage = "No products found to shopgate order id_order:{$sgOrder->getOrderNumber()}";
         $message .= $errorMessage;
         // changed log type to debug to prevent the creation
         // of huge log files
         $log->log($errorMessage, ShopgateLogger::LOGTYPE_DEBUG);
         return array();
     }
     foreach ($sgOrder->getItems() as $sgItem) {
         foreach ($pItems as $pItem) {
             $qty = null;
             $fromHook = false;
             $sgItemNumber = $sgItem->getItemNumber();
             // generate the item number as we do it for items with attributes
             if (!empty($pItem['product_attribute_id'])) {
                 $prestaItemNumber = $pItem['product_id'] . '-' . $pItem['product_attribute_id'];
             } else {
                 $prestaItemNumber = $pItem['product_id'];
             }
             if ($sgItemNumber == $prestaItemNumber) {
                 // There is no opportunity to deliver this information to
                 // Shopgate. We need to add a message to the order.
                 $refundPriceData = Tools::getValue('partialRefundProduct');
                 $refundQtyData = Tools::getValue('partialRefundProductQuantity');
                 if (!empty($refundPriceData[$pItem['id_order_detail']])) {
                     $currency = new CurrencyCore(ConfigurationCore::get("PS_CURRENCY_DEFAULT"));
                     $noteMsg = "Please note that these information could not be transmitted to Shopgate.\n";
                     $refundPrice = $refundPriceData[$pItem['id_order_detail']];
                     $refundPriceMsg = "The price of the Product (id:{$pItem['product_id']}) was refunded({$refundPrice}{$currency->sign}).\n";
                     if (empty($this->comments)) {
                         $this->comments = array();
                     }
                     if (is_string($this->comments)) {
                         $data = base64_decode($this->comments);
                         if (method_exists("Tools", "jsonDecode")) {
                             $this->comments = Tools::jsonDecode($data);
                         } else {
                             $this->comments = json_decode($data);
                         }
                     }
                     if (is_array($this->comments)) {
                         $this->comments[] = $noteMsg;
                         $this->comments[] = $refundPriceMsg;
                     }
                 }
                 // if the hook was executed or cancel_order request was sent from
                 // shopgate, we get the right data from version 1.5.0
                 // for lower versions we got two cases:
                 // * cancel order request, we need to get the cancelled quantity
                 //   out of the database (here we need to calculate it as in 1.5.00)
                 // * if the hook was executed from prestashop, we get the actual cancelled
                 //   amount in the $_POST array (here there is no need to calculate the cancelled)
                 //   value, cause we got it at this point
                 if (empty($refundQtyData[$pItem['id_order_detail']]) && Tools::isSubmit('partialRefundProduct')) {
                     continue;
                 } else {
                     $qty = $refundQtyData[$pItem['id_order_detail']];
                 }
                 // try to retrieve an $_POST['cancelQuantity'] array
                 $cancelQuantity = Tools::getValue('cancelQuantity', null);
                 if (version_compare(_PS_VERSION_, '1.5.0', '>=') && empty($qty)) {
                     $qty = $sgItem->getQuantity() - $pItem['product_quantity'];
                 } elseif (!empty($cancelQuantity[$pItem['id_order_detail']]) && empty($qty)) {
                     $qty = $cancelQuantity[$pItem['id_order_detail']];
                     $fromHook = true;
                 }
                 if (empty($qty) && Tools::getValue('action') == "cron") {
                     $qty = $pItem['product_quantity_refunded'];
                 }
                 // nothing to cancel here
                 if (empty($qty) || $qty < 1) {
                     continue;
                 }
                 $cancelledItems[$sgItemNumber]['data']['item_number'] = $pItem['product_id'];
                 // if someone changed the quantity for this item in the past
                 // we stored it in the database
                 if (empty($cancelledItems[$sgItemNumber]['data']['quantity'])) {
                     $oldQty = 0;
                 } else {
                     $oldQty = $cancelledItems[$sgItemNumber]['data']['quantity'];
                 }
                 if (empty($cancelledItems[$sgItemNumber]['data']['quantity'])) {
                     $cancelledItems[$sgItemNumber]['data']['quantity'] = $qty;
                     $cancelledItems[$sgItemNumber]['data']['quantity_to_cancel'] = $qty;
                 } else {
                     // subtract the old quantity
                     if (version_compare(_PS_VERSION_, '1.5.0', '>=') || !$fromHook) {
                         if (Tools::isSubmit('partialRefundProduct')) {
                             $cancelQuantity = $qty;
                         } else {
                             $cancelQuantity = $qty - $oldQty;
                         }
                     } else {
                         $cancelQuantity = $qty;
                     }
                     if ($cancelQuantity < 0) {
                         $cancelQuantity *= -1;
                     }
                     if ($cancelQuantity > 0) {
                         $cancelledItems[$sgItemNumber]['data']['quantity'] += $cancelQuantity;
                         $cancelledItems[$sgItemNumber]['data']['quantity_to_cancel'] = $cancelQuantity;
                     } else {
                         $cancelledItems[$sgItemNumber]['data']['quantity_to_cancel'] = 0;
                     }
                 }
                 if ($cancelQuantity > 0) {
                     $message .= "reducing quantity ({$cancelledItems[$sgItemNumber]['data']['quantity_to_cancel']}) for the item {$sgItemNumber} \n";
                 }
             }
         }
     }
     return $cancelledItems;
 }
 /**
  * @param ShopgateOrder $order
  *
  * @return array
  * @throws ShopgateLibraryException
  */
 public function updateOrder(ShopgateOrder $order)
 {
     $paymentModel = new ShopgatePayment($this->getModule());
     $shopgateOrderItem = ShopgateOrderPrestashop::loadByOrderNumber($order->getOrderNumber());
     if (!Validate::isLoadedObject($shopgateOrderItem)) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_ORDER_NOT_FOUND, 'Order not found #' . $order->getOrderNumber(), true);
     }
     /** @var OrderCore $coreOrder */
     $coreOrder = new Order($shopgateOrderItem->id_order);
     /**
      * get order states
      */
     $changedStates = $paymentModel->getOrderStateId($order, false);
     /**
      * apply changed states
      */
     foreach ($changedStates as $changedState) {
         $coreOrder->setCurrentState($changedState);
     }
     $shopgateOrderItem->updateFromOrder($order);
     $shopgateOrderItem->save();
     return array('external_order_id' => $shopgateOrderItem->id_order, 'external_order_number' => $shopgateOrderItem->id_order);
 }