Esempio n. 1
0
 /**
  * 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;
 }
Esempio n. 2
0
 /**
  * @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);
 }