/** * 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; }
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); }
/** * @param ShopgateOrder $apiOrder * @return string */ public function getMappingHtml(ShopgateOrder $apiOrder) { switch ($apiOrder->getShippingType()) { /** * read system */ case self::DEFAULT_PLUGIN_API_KEY: return $this->_getNameByCarrierId($apiOrder->getShippingInfos()->getName()); /** * switch from mapping */ /** * switch from mapping */ default: return sprintf('%s (%s - %s)', $this->_getNameByCarrierId($this->getCarrierIdByApiOrder($apiOrder)), $apiOrder->getShippingType(), $apiOrder->getShippingInfos()->getDisplayName()); } }
/** * Checks if totals from shopgate and magento differ by 1 cent * * @param ShopgateOrder $shopgateOrder * @param Mage_Sales_Model_Order $magentoOrder * @return bool */ public function oneCentBugDetected($shopgateOrder, $magentoOrder) { $config = $this->getConfig(); $bugDetected = round(abs($shopgateOrder->getAmountComplete() - $magentoOrder->getQuoteBaseGrandTotal()), 2) == 0.01; $shouldFixBug = $config->getFixOneCentBug(); return $bugDetected && $shouldFixBug; }
public function visitOrder(ShopgateOrder $o) { // get properties $properties = $o->buildProperties(); // iterate the simple variables $properties = $this->iterateSimpleProperties($properties); // visit invoice address if (!empty($properties['invoice_address']) && $properties['invoice_address'] instanceof ShopgateAddress) { $properties['invoice_address']->accept($this); $properties['invoice_address'] = $this->array; } // visit delivery address if (!empty($properties['delivery_address']) && $properties['delivery_address'] instanceof ShopgateAddress) { $properties['delivery_address']->accept($this); $properties['delivery_address'] = $this->array; } // visit shipping info if (!empty($properties['shipping_infos']) && $properties['shipping_infos'] instanceof ShopgateShippingInfo) { $properties['shipping_infos']->accept($this); $properties['shipping_infos'] = $this->array; } // visit the items and delivery notes arrays $properties['custom_fields'] = $this->iterateObjectList($properties['custom_fields']); $properties['external_coupons'] = $this->iterateObjectList($properties['external_coupons']); $properties['shopgate_coupons'] = $this->iterateObjectList($properties['shopgate_coupons']); $properties['items'] = $this->iterateObjectList($properties['items']); $properties['delivery_notes'] = $this->iterateObjectList($properties['delivery_notes']); // set last value to converted array $this->array = $properties; }
/** * Print comments inside order * * @param Mage_Sales_Model_Order $order * @param ShopgateOrder $shopgateOrder * @return mixed */ public function printCustomFieldComments($order, $shopgateOrder) { if (Mage::getStoreConfig(Shopgate_Framework_Model_Config::XML_PATH_SHOPGATE_ORDER_CUSTOMFIELDS_TO_STATUSHISTORY, Mage::helper('shopgate/config')->getConfig()->getStoreViewId())) { $comment = ''; $customFieldSet = array($this->_getHelper()->__('[SHOPGATE] Custom fields:') => $shopgateOrder->getCustomFields(), $this->_getHelper()->__('Shipping Address fields:') => $shopgateOrder->getDeliveryAddress()->getCustomFields(), $this->_getHelper()->__('Billing Address fields:') => $shopgateOrder->getInvoiceAddress()->getCustomFields()); foreach ($customFieldSet as $title => $set) { $comment .= '<strong>' . $title . '</strong><br/>'; foreach ($set as $field) { $comment .= '"' . addslashes($field->getLabel()) . '" => "' . addslashes($field->getValue()) . '"<br />'; } } $order->addStatusHistoryComment($comment, false); } return $order; }
/** * @param Mage_Sales_Model_Order $magentoOrder * @param ShopgateOrder $shopgateOrder * * @return Mage_Sales_Model_Order */ protected function _setOrderState($magentoOrder, $shopgateOrder) { $magentoOrder = $this->_getFactory()->setOrderStatus($magentoOrder); if ($magentoOrder->getShopgateStatusSet()) { //do nothing, but we will need to pull this whole thing inside factory } elseif ($shopgateOrder->getPaymentMethod() == ShopgateOrder::PREPAY && !$shopgateOrder->getIsPaid()) { $classExists = mageFindClassFile("Mage_Payment_Model_Method_Banktransfer"); if ($classExists !== false && Mage::getStoreConfigFlag("payment/banktransfer/active")) { return $magentoOrder; } else { if ((Mage::getConfig()->getModuleConfig("Phoenix_BankPayment")->is('active', 'true') || Mage::getConfig()->getModuleConfig('Mage_BankPayment')->is('active', 'true')) && Mage::getStoreConfig('payment/bankpayment/order_status')) { $status = Mage::getStoreConfig('payment/bankpayment/order_status'); $state = $this->_getHelper()->getStateForStatus($status); $magentoOrder->setState($state, $status); } else { if (!Mage::getStoreConfig(Shopgate_Framework_Model_Config::XML_PATH_SHOPGATE_ORDER_MARK_UNBLOCKED_AS_PAID)) { if ($magentoOrder->getState() != Mage_Sales_Model_Order::STATE_HOLDED) { $magentoOrder->setHoldBeforeState($magentoOrder->getState()); $magentoOrder->setHoldBeforeStatus($magentoOrder->getStatus()); } $magentoOrder->setState(Mage_Sales_Model_Order::STATE_HOLDED, Mage_Sales_Model_Order::STATE_HOLDED); } else { $oldStatus = $shopgateOrder->getIsPaid(); $shopgateOrder->setIsPaid(true); $magentoOrder->addStatusHistoryComment($this->_getHelper()->__("[SHOPGATE] Set order as paid because shipping is not blocked and config is set to 'mark unblocked orders as paid'!"), false)->setIsCustomerNotified(false); $magentoOrder = $this->_setOrderPayment($magentoOrder, $shopgateOrder); $shopgateOrder->setIsPaid($oldStatus); } } } } else { $stateObject = new Varien_Object(); $methodInstance = $magentoOrder->getPayment()->getMethodInstance(); if ($shopgateOrder->getPaymentMethod() != ShopgateOrder::AMAZON_PAYMENT && strpos($shopgateOrder->getPaymentMethod(), 'PAYONE') === false) { // avoid calling order on amazon payment again $methodInstance->initialize($methodInstance->getConfigData('payment_action'), $stateObject); } if (!$stateObject->getState()) { $status = $methodInstance->getConfigData("order_status"); if ($shopgateOrder->getPaymentMethod() == ShopgateOrder::COD && Mage::getConfig()->getModuleConfig('Phoenix_CashOnDelivery')->is('active', 'true')) { $stateObject->setState(Mage_Sales_Model_Order::STATE_NEW); } else { $stateObject->setState($this->_getHelper()->getStateForStatus($status)); } $stateObject->setStatus($status); } $magentoOrder->setState($stateObject->getState(), $stateObject->getStatus()); if (Mage::getStoreConfig(Shopgate_Framework_Model_Config::XML_PATH_SHOPGATE_ORDER_MARK_UNBLOCKED_AS_PAID) && !$shopgateOrder->getIsPaid() && $shopgateOrder->getPaymentMethod() != ShopgateOrder::BILLSAFE) { $oldStatus = $shopgateOrder->getIsPaid(); $shopgateOrder->setIsPaid(true); $magentoOrder->addStatusHistoryComment($this->_getHelper()->__("[SHOPGATE] Set order as paid because shipping is not blocked and config is set to 'mark unblocked orders as paid'!"), false)->setIsCustomerNotified(false); $magentoOrder = $this->_setOrderPayment($magentoOrder, $shopgateOrder); $shopgateOrder->setIsPaid($oldStatus); } } $magentoOrder->save(); return $magentoOrder; }
/** * 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 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); }