/**
  * Triggered by cron job set_shipping_completed to sync shipments for orders
  * that were most likely update by ERP systems.
  *
  * @param string         $message
  * @param int            $errorcount
  */
 public function setOrderShippingCompleted(&$message, &$errorcount)
 {
     $unsyncedOrders = ShopgateOrderPrestashop::getUnsyncedShopgatOrderIds($this->getLanguageId());
     foreach ($unsyncedOrders as $unsyncedOrder) {
         $this->log("Try to set shipping completed for order with shopgate-order-number #{$unsyncedOrder['order_number']}", ShopgateLogger::LOGTYPE_DEBUG);
         $sgOrder = ShopgateOrderPrestashop::loadByOrderId($unsyncedOrder['id_order']);
         $sgOrder->setShippingComplete($message, $errorcount);
     }
 }
 /**
  * This method gets called when the order status is changed in the admin area of the Prestashop backend
  *
  * @param $params
  */
 public function hookUpdateOrderStatus($params)
 {
     $id_order = $params['id_order'];
     $newOrderState = $params['newOrderStatus'];
     $shopgateOrder = ShopgateOrderPrestashop::loadByOrderId($id_order);
     $shopgateConfig = new ShopgateConfigPrestashop();
     $cancellationStatus = Configuration::get('SG_CANCELLATION_STATUS');
     $shopgateBuilder = new ShopgateBuilder($shopgateConfig);
     $shopgateMerchantApi = $shopgateBuilder->buildMerchantApi();
     if (!is_object($shopgateOrder) || !$shopgateOrder->id_shopgate_order) {
         return;
     }
     try {
         $shippedOrderStates = array();
         if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
             $orderStates = OrderState::getOrderStates($this->context->language->id);
             foreach ($orderStates as $orderState) {
                 if ($orderState['shipped']) {
                     $shippedOrderStates[$orderState['id_order_state']] = 1;
                 }
             }
         } else {
             // Default methods for Prestashop version < 1.5.0.0
             $shippedOrderStates[_PS_OS_DELIVERED_] = 1;
             $shippedOrderStates[_PS_OS_SHIPPING_] = 1;
         }
         if (!empty($shippedOrderStates[$newOrderState->id])) {
             $shopgateMerchantApi->setOrderShippingCompleted($shopgateOrder->order_number);
         }
         $message = '';
         if ($cancellationStatus == $newOrderState->id) {
             $shopgateOrder->cancelOrder($message);
         }
     } catch (ShopgateMerchantApiException $e) {
         $msg = new Message();
         $msg->message = $this->l('On order state') . ': ' . $orderState->name . ' - ' . $this->l('Shopgate status was not updated because of following error') . ': ' . $e->getMessage();
         $msg->id_order = $id_order;
         $msg->id_employee = isset($params['cookie']->id_employee) ? $params['cookie']->id_employee : 0;
         $msg->private = true;
         $msg->add();
     }
 }