/** * @param $plugin * * @throws ShopgateLibraryException */ public function __construct($plugin) { parent::__construct($plugin); /** @var CartCore $cart */ $cart = new Cart(); $cart->id_lang = $this->getPlugin()->getLanguageId(); $cart->id_currency = $this->getPlugin()->getContext()->currency->id; $cart->recyclable = 0; $cart->gift = 0; $this->getPlugin()->getContext()->cart = $cart; /** * check / create shopgate carrier */ /** @var CarrierCore $sgCarrier */ $sgCarrier = new Carrier(Configuration::get('SG_CARRIER_ID')); if (!$sgCarrier->id) { $shopgateShippingModel = new ShopgateShipping(new ShopGate()); $shopgateShippingModel->createShopgateCarrier(); } /** * check all needed table columns */ $shopGate = new ShopGate(); if (!$shopGate->updateTables()) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DATABASE_ERROR, sprintf('Cannot update shopgate_order_table')); } }
/** * @param ShopGate $module * * @return bool */ function upgrade_module_2_9_52($module) { $shopgateCarrierId = Configuration::get('SG_CARRIER_ID'); /** * set current shopgate carrier as deleted */ if ($shopgateCarrierId) { /** @var CarrierCore $carrier */ $carrier = new Carrier($shopgateCarrierId); $carrier->deleted = true; $carrier->save(); } $shopgateShippingModel = new ShopgateShipping($module); $shopgateShippingModel->createShopgateCarrier(); $module->updateTables(); return true; }
/** * @param ShopgatePluginPrestashop $module * @return array */ public static function getShopgateSettings($module) { $result = array(); /** * customer groups */ $result['customer_groups'] = ShopgateSettings::getCustomerGroups($module); /** * product tax */ $result['tax']['product_tax_classes'] = ShopgateSettings::getProductTaxClasses($module); /** * customer tax classes */ $result['tax']['customer_tax_classes'] = array(array('key' => 'default', 'is_default' => true)); if (version_compare(_PS_VERSION_, '1.4.0.4', '<=')) { $result['tax']['tax_rates'] = ShopgateSettings::getTaxRatesOldVersions($module); $result['tax']['tax_rules'] = ShopgateSettings::getTaxRulesOldVersions($result['tax']['product_tax_classes'], $result['tax']['customer_tax_classes'], $result['tax']['tax_rates']); } else { $result['tax']['tax_rates'] = ShopgateSettings::getTaxRates($module); $result['tax']['tax_rules'] = ShopgateSettings::getTaxRules($module); } /** * allowed_shipping_countries and allowed_address_countries */ $result['allowed_shipping_countries'] = array(); $result['allowed_address_countries'] = array(); $countryResultItems = array(); $addressResultItems = array(); foreach (ShopgateShipping::getDeliveryCountries($module->context->language->id, true, true) as $country) { if (is_array($country['states'])) { $resultStates = array(); foreach ($country['states'] as $state) { $resultStates[] = $state['iso_code']; } } else { $resultStates = array(ShopgateShipping::CARRIER_CODE_ALL); } $countryResultItems[$country['iso_code']] = $resultStates; } foreach ($countryResultItems as $country => $state) { $item = array('country' => $country, 'state' => $state); $result['allowed_shipping_countries'][] = $item; } foreach (Country::getCountries($module->context->language->id, true) as $country) { if (is_array($country['states'])) { $resultStates = array(); foreach ($country['states'] as $state) { $resultStates[] = $state['iso_code']; } } else { $resultStates = array(ShopgateShipping::CARRIER_CODE_ALL); } $addressResultItems[$country['iso_code']] = $resultStates; } foreach ($addressResultItems as $country => $state) { $item = array('country' => $country, 'state' => $state); $result['allowed_address_countries'][] = $item; } return $result; }
/** * create dummy customer * * @param ShopgateCart $cart */ protected function _createCustomer(ShopgateCart $cart) { /** * prepare customer group */ if ($cart->getExternalCustomerId()) { /** * load exist customer */ $this->getPlugin()->getContext()->customer = new Customer($cart->getExternalCustomerId()); if (!Validate::isLoadedObject($this->getPlugin()->getContext()->customer)) { $this->_addException(ShopgateLibraryException::UNKNOWN_ERROR_CODE, sprintf('Customer with id #%s not found', $cart->getExternalCustomerId())); } } else { /** * create dummy customer */ $customerGroup = $this->_getCustomerGroups($cart); $this->getPlugin()->getContext()->customer = new Customer(); $this->getPlugin()->getContext()->customer->lastname = self::DEFAULT_CUSTOMER_LAST_NAME; $this->getPlugin()->getContext()->customer->firstname = self::DEFAULT_CUSTOMER_FIRST_NAME; $this->getPlugin()->getContext()->customer->email = self::DEFAULT_CUSTOMER_EMAIL; $this->getPlugin()->getContext()->customer->passwd = self::DEFAULT_CUSTOMER_PASSWD; $this->getPlugin()->getContext()->customer->id_default_group = current($customerGroup->getCustomerGroups())->getId(); $this->getPlugin()->getContext()->customer->add(); $this->_isDummyCustomer = true; } /** * add customer to cart */ $this->getPlugin()->getContext()->cart->id_customer = $this->getPlugin()->getContext()->customer->id; /** * add carrier id */ $shippingModel = new ShopgateShipping($this->getModule()); $tmpOrder = new ShopgateOrder(); $tmpOrder->setShippingType($cart->getShippingType() ? $cart->getShippingType() : ShopgateShipping::DEFAULT_PLUGIN_API_KEY); $tmpOrder->setShippingGroup($cart->getShippingGroup()); $tmpOrder->setShippingInfos($cart->getShippingInfos()); /** @var CarrierCore $carrierItem */ $carrierItem = new Carrier($shippingModel->getCarrierIdByApiOrder($tmpOrder)); if (!Validate::isLoadedObject($carrierItem)) { $this->_addException(ShopgateLibraryException::UNKNOWN_ERROR_CODE, sprintf('Invalid carrier ID #%s', $shippingModel->getCarrierIdByApiOrder($tmpOrder))); } $this->getPlugin()->getContext()->cart->id_carrier = $carrierItem->id; $this->getPlugin()->getContext()->cart->save(); }
/** * @param ShopgateOrder $order * * @return array * @throws PrestaShopException * @throws ShopgateLibraryException */ public function addOrder(ShopgateOrder $order) { /** * check exits shopgate order */ if (ShopgateOrderPrestashop::loadByOrderNumber($order->getOrderNumber())->status == 1) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DUPLICATE_ORDER, sprintf('external_order_id: %s', $order->getOrderNumber()), true); } /** @var CarrierCore $sgCarrier */ $sgCarrier = new Carrier(Configuration::get('SG_CARRIER_ID')); if (version_compare(_PS_VERSION_, '1.5.0', '<') && empty($sgCarrier->name)) { $sgCarrier->name = 'shopgate_tmp_carrier'; } if ($order->getShippingType() != ShopgateShipping::DEFAULT_PLUGIN_API_KEY) { if ($order->getShippingInfos()->getAmount() > 0) { if (version_compare(_PS_VERSION_, '1.5.0', '<')) { ShopgateModObjectModel::updateShippingPrice(pSQL($order->getShippingInfos()->getAmount())); } else { $data = array('price' => pSQL($order->getShippingInfos()->getAmount())); $where = 'a.id_carrier = ' . (int) Configuration::get('SG_CARRIER_ID'); ObjectModel::updateMultishopTable('Delivery', $data, $where); } $sgCarrier->is_free = 0; } else { $sgCarrier->is_free = 1; } } $sgCarrier->update(); $customerModel = new ShopgateItemsCustomerImportJson($this->getPlugin()); $paymentModel = new ShopgatePayment($this->getModule()); $shippingModel = new ShopgateShipping($this->getModule()); /** * read / check customer */ if (!($customerId = Customer::customerExists($order->getMail(), true, false))) { /** * prepare customer */ $shopgateCustomerItem = new ShopgateCustomer(); $shopgateCustomerItem->setLastName($order->getInvoiceAddress()->getLastName()); $shopgateCustomerItem->setFirstName($order->getInvoiceAddress()->getFirstName()); $shopgateCustomerItem->setGender($order->getInvoiceAddress()->getGender()); $shopgateCustomerItem->setBirthday($order->getInvoiceAddress()->getBirthday()); $shopgateCustomerItem->setNewsletterSubscription(Configuration::get('SG_SUBSCRIBE_NEWSLETTER') ? true : false); $customerId = $customerModel->registerCustomer($order->getMail(), md5(_COOKIE_KEY_ . time()), $shopgateCustomerItem); } /** @var CustomerCore $customer */ $customer = new Customer($customerId); /** * prepare cart */ if (!$order->getDeliveryAddress()->getPhone()) { $order->getDeliveryAddress()->setPhone($order->getPhone()); } if (!$order->getInvoiceAddress()->getPhone()) { $order->getInvoiceAddress()->setPhone($order->getPhone()); } $this->getCart()->id_address_delivery = $customerModel->createAddress($order->getDeliveryAddress(), $customer); $this->getCart()->id_address_invoice = $customerModel->createAddress($order->getInvoiceAddress(), $customer); $this->getCart()->id_customer = $customerId; // id_guest is a connection to a ps_guest entry which includes screen width etc. // is_guest field only exists in Prestashop 1.4.1.0 and higher if (version_compare(_PS_VERSION_, '1.4.1.0', '>=')) { $this->getCart()->id_guest = $customer->is_guest; } $this->getCart()->secure_key = $customer->secure_key; $this->getCart()->id_carrier = $shippingModel->getCarrierIdByApiOrder($order); $shopgateCustomFieldsHelper = new ShopgateCustomFieldsHelper(); $shopgateCustomFieldsHelper->saveCustomFields($this->getCart(), $order->getCustomFields()); $this->getCart()->add(); /** * add cart items */ $canCreateOrder = true; $errorMessages = array(); foreach ($order->getItems() as $item) { list($productId, $attributeId) = ShopgateHelper::getProductIdentifiers($item); if ($productId == 0) { continue; } $updateCart = $this->getCart()->updateQty($item->getQuantity(), $productId, $attributeId, false, 'up', $this->getCart()->id_address_delivery); if ($updateCart !== true) { $canCreateOrder = false; $errorMessages[] = array('product_id' => $productId, 'attribute_id' => $attributeId, 'quantity' => $item->getQuantity(), 'result' => $updateCart, 'reason' => $updateCart == -1 ? 'minimum quantity not reached' : ''); } } /** * coupons */ foreach ($order->getExternalCoupons() as $coupon) { /** @var CartRuleCore $cartRule */ $cartRule = new CartRule(CartRule::getIdByCode($coupon->getCode())); if (Validate::isLoadedObject($cartRule)) { $this->getCart()->addCartRule($cartRule->id); $this->getCart()->save(); } } if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) { /** * this field is not available in version 1.4.x.x * set delivery option */ $delivery_option = array($this->getCart()->id_address_delivery => $shippingModel->getCarrierIdByApiOrder($order) . ','); $this->getCart()->setDeliveryOption($delivery_option); } /** * store shopgate order */ $shopgateOrderItem = new ShopgateOrderPrestashop(); $shopgateOrderItem->fillFromOrder($this->getCart(), $order, $this->getPlugin()->getShopgateConfig()->getShopNumber()); if (version_compare(_PS_VERSION_, '1.6.0.0', '<')) { $shopgateOrderItem->add(); } /** * create order */ if ($canCreateOrder) { /** * get first item from order stats */ $this->getCart()->save(); $idOrderState = reset($paymentModel->getOrderStateId($order)); $validateOder = $this->getModule()->validateOrder($this->getCart()->id, $idOrderState, $this->getCart()->getOrderTotal(true, defined('Cart::BOTH') ? Cart::BOTH : 3), $paymentModel->getPaymentTitleByKey($order->getPaymentMethod()), null, array(), null, false, $this->getCart()->secure_key); if (version_compare(_PS_VERSION_, '1.5.0.0', '<') && (int) $this->getModule()->currentOrder > 0 && $order->getShippingType() != ShopgateShipping::DEFAULT_PLUGIN_API_KEY && $order->getShippingInfos()->getAmount() > 0) { ShopgateLogger::log('PS < 1.5.0.0: update shipping and payment cost', ShopgateLogger::LOGTYPE_DEBUG); // in versions below 1.5.0.0 the shipping and payment costs must be updated after the order was imported /** @var OrderCore $updateShopgateOrder */ $updateShopgateOrder = new Order($this->getModule()->currentOrder); $updateShopgateOrder->total_shipping = $order->getAmountShipping() + $order->getAmountShopPayment(); $updateShopgateOrder->total_paid += $order->getAmountShipping() + $order->getAmountShopPayment(); $updateShopgateOrder->total_paid_real += $order->getAmountShipping() + $order->getAmountShopPayment(); $updateShopgateOrder->update(); } /** * update shopgate order */ if ($validateOder) { $shopgateOrderItem->id_order = $this->getModule()->currentOrder; $shopgateOrderItem->status = 1; $shopgateOrderItem->save(); return array('external_order_id' => $shopgateOrderItem->id_order, 'external_order_number' => $shopgateOrderItem->id_order); } } $shopgateOrderItem->delete(); throw new ShopgateLibraryException(ShopgateLibraryException::UNKNOWN_ERROR_CODE, 'Unable to create order:' . print_r($errorMessages, true), true); }
/** * @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'); }