protected function insertOrderItems(ShopgateOrder $order) { $this->log('start insertOrderItems()', ShopgateLogger::LOGTYPE_DEBUG); $products = array(); //Check product quantitys $settings = Configuration::getMultiple(array('SHOPGATE_MIN_QUANTITY_CHECK', 'SHOPGATE_OUT_OF_STOCK_CHECK')); // complete weight of the order foreach ($order->getItems() as $i) { list($id_product, $id_product_attribute) = $this->getProductIdentifiers($i); if ($id_product == 0) { continue; } $wantedQty = (int) $i->getQuantity(); $product = new Product($id_product, true, (int) Configuration::get('PS_LANG_DEFAULT')); $minQty = 1; if ((int) $id_product_attribute) { $stockQty = (int) Product::getQuantity((int) $id_product, (int) $id_product_attribute); if (version_compare(_PS_VERSION_, '1.4.0.7', '>=')) { // this attribute doesn't exist before 1.4.0.7 $minQty = Attribute::getAttributeMinimalQty((int) $id_product_attribute); } } else { $stockQty = (int) Product::getQuantity((int) $id_product, NULL); if (version_compare(_PS_VERSION_, '1.4.0.2', '>=')) { // this attribute doesn't exist before 1.4.0.2 $minQty = (int) $product->minimal_quantity; } } $oos_available = Product::isAvailableWhenOutOfStock($product->out_of_stock); $qtyDifference = 0; if (!$oos_available && $wantedQty > $stockQty) { $qtyDifference = $wantedQty - $stockQty; } $p = array(); $p['id_product'] = (int) $id_product; $p['id_product_attribute'] = (int) $id_product_attribute; $p['name'] = $product->name; $p['quantity'] = $wantedQty; $p['quantity_in_stock'] = $stockQty; $p['quantity_difference'] = $qtyDifference; if (empty($p['name'])) { $p['name'] = $i->getName(); } if ($oos_available) { $stockQty = $wantedQty; } if ((bool) $settings['SHOPGATE_MIN_QUANTITY_CHECK'] && $wantedQty < $minQty) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DATABASE_ERROR, 'Minimum quantity required', true); } if ((bool) $settings['SHOPGATE_OUT_OF_STOCK_CHECK'] && $wantedQty > $stockQty) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DATABASE_ERROR, 'Out of stock', true); } array_push($products, $p); } $this->log('end insertOrderItems()', ShopgateLogger::LOGTYPE_DEBUG); return $products; }
/** * 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; }