/**
  * @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);
 }
 /**
  * @return mixed
  */
 public function getContent()
 {
     include_once dirname(__FILE__) . '/backward_compatibility/backward.php';
     /** @var ShopgateConfigPrestashop $shopgateConfig */
     $shopgateConfig = new ShopgateConfigPrestashop();
     /** @var mixed $errorMessage */
     $errorMessage = false;
     /** @var LanguageCore $lang */
     $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
     /**
      * prepare carrier list
      */
     $allCarriers = defined('Carrier::ALL_CARRIERS') ? Carrier::ALL_CARRIERS : ShopgateShipping::SG_ALL_CARRIERS;
     $carrierList = Carrier::getCarriers($lang->id, true, false, false, null, $allCarriers);
     $carrierIdColumn = version_compare(_PS_VERSION_, '1.5.0.1', '>=') ? 'id_reference' : 'id_carrier';
     $nativeCarriers = $carrierList;
     /**
      * save on submit
      */
     if (Tools::isSubmit('saveConfigurations')) {
         $configs = Tools::getValue('configs', array());
         /**
          * set and store configs
          */
         foreach ($configs as $key => $value) {
             $shopgateConfig->setByKey($key, $value);
         }
         try {
             foreach ($shopgateConfig->initFolders() as $key => $value) {
                 $shopgateConfig->setByKey($key, $value);
             }
             $shopgateConfig->store();
         } catch (ShopgateLibraryException $e) {
             $errorMessage = $e->getAdditionalInformation();
         }
         $settings = array();
         foreach ($carrierList as $carrier) {
             $settings['SG_MOBILE_CARRIER'][(int) $carrier[$carrierIdColumn]] = 0;
         }
         foreach (Tools::getValue('settings', array()) as $key => $value) {
             if (!empty($value) && is_array($value) && !empty($settings[$key])) {
                 $settings[$key] = $value + $settings[$key];
             } else {
                 $settings[$key] = $value;
             }
         }
         /**
          * store settings
          */
         foreach ($settings as $key => $value) {
             if (in_array($key, ShopgateSettings::getSettingKeys())) {
                 if (is_array($value)) {
                     $value = base64_encode(serialize($value));
                 }
                 Configuration::updateValue($key, htmlentities($value, ENT_QUOTES));
             }
         }
     }
     $languages = array();
     foreach (Language::getLanguages() as $l) {
         $languages[$l['iso_code']] = $l['name'];
     }
     $orderStates = array();
     foreach (OrderState::getOrderStates($lang->id) as $key => $orderState) {
         $orderStates[$orderState['id_order_state']] = $orderState['name'];
     }
     $newOrderStateMapping = array();
     foreach ($this->shopgatePaymentModel->getPaymentMethods() as $key => $method) {
         $newOrderStateMapping[ShopgateSettings::getOrderStateKey($key)] = $method;
     }
     /**
      * prepare css
      */
     if (version_compare(_PS_VERSION_, '1.6', '<')) {
         $configCss = 'configurations_without_bs.css';
     } else {
         $configCss = 'configurations.css';
     }
     $mobileCarrierUse = unserialize(base64_decode(Configuration::get('SG_MOBILE_CARRIER')));
     $resultNativeCarriers = array();
     foreach ($nativeCarriers as $nativeCarrier) {
         if ($nativeCarrier['external_module_name'] != ShopgateShipping::DEFAULT_EXTERNAL_MODULE_CARRIER_NAME) {
             $nativeCarrier['identifier'] = $nativeCarrier[$carrierIdColumn];
             if (!is_array($mobileCarrierUse)) {
                 $nativeCarrier['mobile_used'] = 1;
             } else {
                 if (!empty($mobileCarrierUse[$nativeCarrier['identifier']])) {
                     $nativeCarrier['mobile_used'] = 1;
                 } else {
                     $nativeCarrier['mobile_used'] = 0;
                 }
             }
             $resultNativeCarriers[] = $nativeCarrier;
         }
     }
     $shopgateCarrier = new Carrier(Configuration::get('SG_CARRIER_ID'), $lang->id);
     $carrierList[] = array('name' => $shopgateCarrier->name, 'id_carrier' => $shopgateCarrier->id);
     /**
      * price types
      */
     $priceTypes = array(Shopgate_Model_Catalog_Price::DEFAULT_PRICE_TYPE_NET => $this->l('Net'), Shopgate_Model_Catalog_Price::DEFAULT_PRICE_TYPE_GROSS => $this->l('Gross'));
     /**
      * fill smarty params
      */
     $this->context->smarty->assign('error_message', $errorMessage);
     $this->context->smarty->assign('settings', Configuration::getMultiple(ShopgateSettings::getSettingKeys()));
     $this->context->smarty->assign('configs', $shopgateConfig->toArray());
     $this->context->smarty->assign('mod_dir', $this->_path);
     $this->context->smarty->assign('video_url', ShopgateHelper::getVideoLink($this->context));
     $this->context->smarty->assign('offer_url', ShopgateHelper::getOfferLink($this->context));
     $this->context->smarty->assign('api_url', ShopgateHelper::getApiUrl($this->context));
     $this->context->smarty->assign('currencies', Currency::getCurrencies());
     $this->context->smarty->assign('servers', ShopgateHelper::getEnvironments($this));
     $this->context->smarty->assign('shipping_service_list', $this->shopgateShippingModel->getShippingServiceList());
     $this->context->smarty->assign('product_export_descriptions', ShopgateSettings::getProductExportDescriptionsArray($this));
     $this->context->smarty->assign('languages', $languages);
     $this->context->smarty->assign('carrier_list', $carrierList);
     $this->context->smarty->assign('shippingModel', $this->shopgateShippingModel);
     $this->context->smarty->assign('configCss', $configCss);
     $this->context->smarty->assign('product_export_price_type', $priceTypes);
     $this->context->smarty->assign('native_carriers', $resultNativeCarriers);
     $this->context->smarty->assign('order_state_mapping', $orderStates);
     return $this->display(__FILE__, 'views/templates/admin/configurations.tpl');
 }