示例#1
0
 private function addStatusOnOrder($id_order, $neteven_order)
 {
     /* Update order state in order */
     $order_state = array_merge($this->getValue('order_state_before'), array($this->getValue('id_order_state_neteven')), $this->getValue('order_state_after'));
     if (is_array($order_state) && count($order_state) > 0) {
         foreach ($order_state as $id_order_state) {
             if (class_exists('OrderInvoiceOverride' && method_exists('OrderInvoiceOverride', 'clearCacheTotalPaid'))) {
                 OrderInvoiceOverride::clearCacheTotalPaid();
             }
             $new_history = new OrderHistory();
             $new_history->id_order = (int) $id_order;
             $new_history->changeIdOrderState((int) $id_order_state, $id_order);
             $new_history->addWithemail(true, array());
             Toolbox::addLogLine(self::getL('Save order state Id') . ' ' . (int) $id_order_state . ' ' . self::getL('NetEven Order Id') . ' ' . (int) $neteven_order->OrderID);
         }
     }
 }
 /**
  * Creating of the PrestaShop order
  * @param $neteven_order
  * @param $neteven_orders
  * @return int
  */
 private function createOrder($neteven_order, $neteven_orders)
 {
     if (constant('_PS_VERSION_') >= 1.5) {
         include_once dirname(__FILE__) . '/OrderInvoiceOverride.php';
     }
     // Treatment of customer
     $id_customer = $this->addCustomerInBDD($neteven_order);
     if ($this->time_analyse) {
         $this->current_time_2 = time();
         Toolbox::displayDebugMessage(self::getL('Customer') . ' : ' . ((int) $this->current_time_2 - (int) $this->current_time_0) . 's');
     }
     // Treatment of addresses of the customer
     $id_address_billing = $this->addAddresseInBDD($neteven_order->OrderID, $neteven_order->BillingAddress, 'facturation', $id_customer);
     $id_address_shipping = $this->addAddresseInBDD($neteven_order->OrderID, $neteven_order->ShippingAddress, 'livraison', $id_customer);
     if ($this->time_analyse) {
         $this->current_time_0 = time();
         Toolbox::displayDebugMessage(self::getL('Address') . ' : ' . ((int) $this->current_time_0 - (int) $this->current_time_2) . 's');
     }
     // Get secure key of customer
     $secure_key_default = md5(uniqid(rand(), true));
     if ($secure_key = Db::getInstance()->getValue('SELECT `secure_key` FROM `' . _DB_PREFIX_ . 'customer` WHERE `id_customer` = ' . (int) $id_customer)) {
         $secure_key_default = $secure_key;
     } else {
         Toolbox::addLogLine(self::getL('Problem with a secure key recovery for the customer / NetEven Order Id') . ' ' . $neteven_order->OrderID);
     }
     // Treatment of order informations
     $total_wt = 0;
     $total_product = 0;
     $total_product_wt = 0;
     foreach ($neteven_orders as $neteven_order_temp) {
         if ($neteven_order_temp->OrderID == $neteven_order->OrderID) {
             if (in_array($neteven_order_temp->Status, $this->getValue('t_list_order_status'))) {
                 continue;
             }
             $total_product += floatval($neteven_order_temp->Price->_) - floatval($neteven_order_temp->VAT->_);
             $total_product_wt += floatval($neteven_order_temp->Price->_);
         }
     }
     $total_wt = $total_product_wt + $neteven_order->OrderShippingCost->_;
     $date_now = date('Y-m-d H:i:s');
     if ($this->time_analyse) {
         $this->current_time_2 = time();
         Toolbox::displayDebugMessage(self::getL('Order total') . ' : ' . ((int) $this->current_time_2 - (int) $this->current_time_0) . 's');
     }
     // Creating and add order in PrestaShop
     if (!($res = Db::getInstance()->getRow('SELECT * FROM `' . _DB_PREFIX_ . 'orders_gateway` WHERE `id_order_neteven` = ' . (int) $neteven_order->OrderID . ' AND `id_order_detail_neteven` = 0'))) {
         // Creating cart
         $cart = new Cart();
         $cart->id_address_delivery = (int) $id_address_shipping;
         $cart->id_address_invoice = (int) $id_address_billing;
         $cart->id_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
         $cart->id_customer = (int) $id_customer;
         $cart->id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
         $cart->id_carrier = (int) Configuration::get('PS_CARRIER_DEFAULT');
         $cart->recyclable = 1;
         $cart->gift = 0;
         $cart->gift_message = '';
         $cart->date_add = $date_now;
         $cart->secure_key = $secure_key_default;
         $cart->date_upd = $date_now;
         if (!$cart->add()) {
             Toolbox::addLogLine(self::getL('Failed for cart creation / NetEven Order Id') . ' ' . (int) $neteven_order->OrderID);
         }
         if ($this->time_analyse) {
             $this->current_time_0 = time();
             Toolbox::displayDebugMessage(self::getL('Cart') . ' : ' . ((int) $this->current_time_0 - (int) $this->current_time_2) . 's');
         }
         // Creating order
         $id_order_temp = 0;
         $order = new Order();
         $order->id_carrier = Configuration::get('PS_CARRIER_DEFAULT');
         $order->id_lang = Configuration::get('PS_LANG_DEFAULT');
         $order->id_customer = $id_customer;
         $order->id_cart = $cart->id;
         $order->id_currency = Configuration::get('PS_CURRENCY_DEFAULT');
         $order->id_address_delivery = $id_address_shipping;
         $order->id_address_invoice = $id_address_billing;
         $order->secure_key = $secure_key_default;
         $order->payment = $neteven_order->PaymentMethod;
         $order->conversion_rate = 1;
         $order->module = 'nqgatewayneteven';
         $order->recyclable = 0;
         $order->gift = 0;
         $order->gift_message = '';
         $order->shipping_number = '';
         //generate reference order
         $nbr_order_neteven = Configuration::get('NUMBER_ORDER_NETEVEN');
         if (false === $nbr_order_neteven) {
             $nbr_order_neteven = 1;
         } else {
             $nbr_order_neteven = (int) str_replace('N', '', $nbr_order_neteven);
             $nbr_order_neteven++;
         }
         $next_ref_gen_order_neteven = 'N' . sprintf('%07s', $nbr_order_neteven);
         Configuration::updateValue('NUMBER_ORDER_NETEVEN', $next_ref_gen_order_neteven);
         $order->reference = $next_ref_gen_order_neteven;
         //-----
         $carrier = new Carrier((int) $order->id_carrier);
         $carrier_tax_rate = false;
         if (method_exists($carrier, 'getTaxesRate')) {
             $carrier_tax_rate = $carrier->getTaxesRate(new Address($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
         }
         $total_shipping_tax_excl = $carrier_tax_rate ? $neteven_order->OrderShippingCost->_ / ($carrier_tax_rate / 100) : $neteven_order->OrderShippingCost->_;
         $total_wt = $total_product_wt + $neteven_order->OrderShippingCost->_;
         $total = $total_product + $total_shipping_tax_excl;
         $order->total_discounts_tax_excl = 0;
         $order->total_discounts_tax_incl = 0;
         $order->total_discounts = 0;
         $order->total_wrapping_tax_excl = 0;
         $order->total_wrapping_tax_incl = 0;
         $order->total_wrapping = 0;
         $order->total_products = (double) number_format($total_product, 2, '.', '');
         $order->total_products_wt = (double) number_format($total_product_wt, 2, '.', '');
         $order->total_shipping_tax_excl = (double) number_format($total_shipping_tax_excl, 2, '.', '');
         $order->total_shipping_tax_incl = (double) number_format($neteven_order->OrderShippingCost->_, 2, '.', '');
         $order->total_shipping = (double) number_format($neteven_order->OrderShippingCost->_, 2, '.', '');
         $order->total_paid_tax_excl = (double) number_format($total, 2, '.', '');
         $order->total_paid_tax_incl = (double) number_format($total_wt, 2, '.', '');
         $order->total_paid_real = (double) number_format($total_wt, 2, '.', '');
         $order->total_paid = (double) number_format($total_wt, 2, '.', '');
         $order->carrier_tax_rate = 0;
         $order->total_wrapping = 0;
         $order->invoice_number = 0;
         $order->delivery_number = 0;
         $order->invoice_date = $date_now;
         $order->delivery_date = $date_now;
         $order->valid = 1;
         $order->date_add = $date_now;
         $order->date_upd = $date_now;
         if (Configuration::get('PS_SHOP_ENABLE')) {
             $order->id_shop = (int) Configuration::get('PS_SHOP_DEFAULT');
         }
         if (!$order->add()) {
             Toolbox::addLogLine(self::getL('Failed for order creation / NetEven Order Id') . ' ' . (int) $neteven_order->OrderID);
         } else {
             $id_order_temp = $order->id;
             Db::getInstance()->Execute('INSERT INTO `' . _DB_PREFIX_ . 'order_carrier` (`id_order`, `id_carrier`, `id_order_invoice`, `weight`, `shipping_cost_tax_excl`, `shipping_cost_tax_incl`, `tracking_number`, `date_add`) VALUES (' . (int) $id_order_temp . ', ' . (int) Configuration::get('PS_CARRIER_DEFAULT') . ', 0, 0, 0, 0, 0,"' . pSQL(date('Y-m-d H:i:s')) . '")');
             Db::getInstance()->Execute('INSERT INTO `' . _DB_PREFIX_ . 'message` (`id_order`, `message`, `date_add`) VALUES (' . (int) $id_order_temp . ', "Place de marché ' . $neteven_order->MarketPlaceName . '", "' . pSQL(date('Y-m-d H:i:s')) . '")');
             Db::getInstance()->Execute('INSERT INTO `' . _DB_PREFIX_ . 'message` (`id_order`, `message`, `date_add`) VALUES (' . (int) $id_order_temp . ', "ID order NetEven ' . $neteven_order->MarketPlaceOrderId . '", "' . pSQL(date('Y-m-d H:i:s')) . '")');
             if ($this->time_analyse) {
                 $this->current_time_2 = time();
                 Toolbox::displayDebugMessage(self::getL('Order') . ' : ' . ((int) $this->current_time_2 - (int) $this->current_time_0) . 's');
             }
             Toolbox::addLogLine(self::getL('Add order Id') . ' ' . (int) $id_order_temp . ' ' . self::getL('NetEven Order Id') . ' ' . (int) $neteven_order->OrderID);
             // Update order state in order
             $order_state = array_merge($this->getValue('order_state_before'), array($this->getValue('id_order_state_neteven')), $this->getValue('order_state_after'));
             if (is_array($order_state) && count($order_state) > 0) {
                 foreach ($order_state as $id_order_state) {
                     if (class_exists('OrderInvoiceOverride' && method_exists('OrderInvoiceOverride', 'clearCacheTotalPaid'))) {
                         OrderInvoiceOverride::clearCacheTotalPaid();
                     }
                     $new_history = new OrderHistory();
                     $new_history->id_order = (int) $id_order_temp;
                     $new_history->changeIdOrderState((int) $id_order_state, $id_order_temp);
                     $new_history->addWithemail(true, array());
                     Toolbox::addLogLine(self::getL('Save order state Id') . ' ' . (int) $id_order_state . ' ' . self::getL('NetEven Order Id') . ' ' . (int) $neteven_order->OrderID);
                 }
             }
             if ($this->time_analyse) {
                 $this->current_time_0 = time();
                 Toolbox::displayDebugMessage(self::getL('History') . ' : ' . ((int) $this->current_time_0 - (int) $this->current_time_2) . 's');
             }
             // Insert order in orders_gateway table
             if (!Db::getInstance()->Execute('INSERT INTO `' . _DB_PREFIX_ . 'orders_gateway` (`id_order_neteven`, `id_order`, `id_order_detail_neteven`, `date_add`, `date_upd`) VALUES (' . (int) $neteven_order->OrderID . ', ' . (int) $id_order_temp . ', 0, "' . pSQL($date_now) . '", "' . pSQL($date_now) . '")')) {
                 Toolbox::addLogLine(self::getL('Failed for save export NetEven order Id') . ' ' . (int) $neteven_order->OrderID);
             } else {
                 Toolbox::addLogLine(self::getL('Save export NetEven order Id') . ' ' . (int) $neteven_order->OrderID);
             }
         }
     } else {
         $id_order_temp = $res['id_order'];
         Toolbox::addLogLine(self::getL('Get already exported order Id') . ' ' . $res['id_order'] . ' ' . self::getL('NetEven Order Id') . ' ' . (int) $neteven_order->OrderID);
     }
     return $id_order_temp;
 }
 public static function clearCacheTotalPaid()
 {
     self::$_total_paid_cache = array();
 }