public function importOrders($orders) { foreach ($orders as $order) { if (!$order->isCompleted()) { $order->addErrorMessage($this->l('Status not complete, amount less than 0.1 or no matching product')); continue; } if ($order->exists()) { $order->addErrorMessage($this->l('Order already imported')); continue; } // no order in ebay order table with this order_ref if (!$order->hasValidContact()) { $order->addErrorMessage($this->l('Invalid e-mail')); continue; } $id_customer = $order->getOrAddCustomer(); $id_address = $order->updateOrAddAddress(); if (!$order->hasAllProductsWithAttributes()) { $order->addErrorMessage($this->l('Could not find the products in database')); continue; } $order->addCart($this->ebay_country); //Create a Cart for the order if (!$order->updateCartQuantities()) { $order->deleteCart(); $order->addErrorMessage($this->l('Could not add product to cart (maybe your stock quantity is 0)')); continue; } // Fix on sending e-mail Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => 'NOSEND-EBAY'), 'UPDATE', '`id_customer` = ' . (int) $id_customer); $customer_clear = new Customer(); if (method_exists($customer_clear, 'clearCache')) { $customer_clear->clearCache(true); } // if the carrier is disabled, we enable it for the order validation and then disable it again $carrier = new Carrier((int) EbayShipping::getPsCarrierByEbayCarrier($order->shippingService)); if (!$carrier->active) { $carrier->active = true; $carrier->save(); $has_disabled_carrier = true; } // Validate order $id_order = $order->validate(); // we now disable the carrier if required if (isset($has_disabled_carrier) && $has_disabled_carrier) { $carrier->active = false; $carrier->save(); } // Fix on sending e-mail Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => pSQL($order->getEmail())), 'UPDATE', '`id_customer` = ' . (int) $id_customer); // Update price (because of possibility of price impact) $order->updatePrice(); $order->add(); if (!version_compare(_PS_VERSION_, '1.5', '>')) { foreach ($order->getProducts() as $product) { $this->hookAddProduct(array('product' => new Product((int) $product['id_product']))); } } } $orders_ar = array(); foreach ($orders as $order) { $orders_ar[] = array('id_order_ref' => $order->getIdOrderRef(), 'id_order_seller' => $order->getIdOrderSeller(), 'amount' => $order->getAmount(), 'status' => $order->getStatus(), 'date' => $order->getDate(), 'email' => $order->getEmail(), 'products' => $order->getProducts(), 'error_messages' => $order->getErrorMessages()); } file_put_contents(dirname(__FILE__) . '/log/orders.php', "<?php\n\n" . '$dateLastImport = ' . '\'' . date('d/m/Y H:i:s') . "';\n\n" . '$orders = ' . var_export($orders_ar, true) . ";\n\n"); }
public function importOrders($orders) { $errors_email = array(); foreach ($orders as $order) { $errors = array(); if (!$order->isCompleted()) { $message = $this->l('Status not complete, amount less than 0.1 or no matching product'); $errors[] = $message; $order->addErrorMessage($message); continue; } if ($order->exists()) { $message = $this->l('Order already imported'); $errors[] = $message; $order->addErrorMessage($message); continue; } // no order in ebay order table with this order_ref if (!$order->hasValidContact()) { $message = $this->l('Invalid e-mail'); $errors[] = $message; $order->addErrorMessage($message); continue; } if (!$order->hasAllProductsWithAttributes()) { $message = $this->l('Could not find the products in database'); $errors[] = $message; $order->addErrorMessage($message); continue; } if ($this->is_multishop) { $shops_data = $order->getProductsAndProfileByShop(); $id_shops = array_keys($shops_data); if (count($id_shops) > 1) { $product_ids = $order->getProductIds(); $first_id_shop = $id_shops[0]; if (version_compare(_PS_VERSION_, '1.5', '>')) { $sql = 'SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_shop` ps WHERE ps.`id_shop` = ' . (int) $first_id_shop . ' AND ps.`active` = 1 AND ps.`id_product` IN (' . implode(',', $product_ids) . ')'; } else { $sql = 'SELECT count(*) FROM `' . _DB_PREFIX_ . 'product` p WHERE p.`active` = 1 AND p.`id_product` IN (' . implode(',', $product_ids) . ')'; } $nb_products_in_shop = Db::getInstance()->getValue($sql); if ($nb_products_in_shop == count($product_ids)) { $id_shops = array($first_id_shop); $has_shared_customers = true; } else { $sql = 'SELECT count(*) FROM `' . _DB_PREFIX_ . 'shop` s INNER JOIN `' . _DB_PREFIX_ . 'shop_group` sg ON s.`id_shop_group` = sg.`id_shop_group` AND sg.`share_customer` = 1'; $nb_shops_sharing = Db::getInstance()->getValue($sql); $has_shared_customers = $nb_shops_sharing == count($id_shops); } } else { $has_shared_customers = true; } } else { $default_shop = Configuration::get('PS_SHOP_DEFAULT') ? Configuration::get('PS_SHOP_DEFAULT') : 1; $id_shops = array($default_shop); $has_shared_customers = true; } $customer_ids = array(); if ($has_shared_customers) { // in case of shared customers in multishop, we take the profile of the first shop if ($this->is_multishop) { $shop_data = reset($shops_data); $ebay_profile = new EbayProfile($shop_data['id_ebay_profiles'][0]); } else { $ebay_profile = EbayProfile::getCurrent(); } $id_customer = $order->getOrAddCustomer($ebay_profile); $id_address = $order->updateOrAddAddress($ebay_profile); $customer_ids[] = $id_customer; // Fix on sending e-mail Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => 'NOSEND-EBAY'), 'UPDATE', '`id_customer` = ' . (int) $id_customer); $customer_clear = new Customer(); if (method_exists($customer_clear, 'clearCache')) { $customer_clear->clearCache(true); } } foreach ($id_shops as $id_shop) { if ($this->is_multishop) { $id_ebay_profile = (int) $shops_data[$id_shop]['id_ebay_profiles'][0]; $ebay_profile = new EbayProfile($id_ebay_profile); } else { $ebay_profile = EbayProfile::getCurrent(); } if (!$has_shared_customers) { $id_customer = $order->getOrAddCustomer($ebay_profile); $id_address = $order->updateOrAddAddress($ebay_profile); $customer_ids[] = $id_customer; // Fix on sending e-mail Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => 'NOSEND-EBAY'), 'UPDATE', '`id_customer` = ' . (int) $id_customer); $customer_clear = new Customer(); if (method_exists($customer_clear, 'clearCache')) { $customer_clear->clearCache(true); } } $cart = $order->addCart($ebay_profile, $this->ebay_country); //Create a Cart for the order if (!$order->updateCartQuantities($ebay_profile)) { $order->deleteCart($ebay_profile->id_shop); $message = $this->l('Could not add product to cart (maybe your stock quantity is 0)'); $errors[] = $message; $order->addErrorMessage($message); continue; } // if the carrier is disabled, we enable it for the order validation and then disable it again $carrier = new Carrier((int) EbayShipping::getPsCarrierByEbayCarrier($ebay_profile->id, $order->shippingService)); if (!$carrier->active) { $carrier->active = true; $carrier->save(); $has_disabled_carrier = true; } else { $has_disabled_carrier = false; } // Validate order $id_order = $order->validate($ebay_profile->id_shop, $this->ebay_profile->id); // we now disable the carrier if required if ($has_disabled_carrier) { $carrier->active = false; $carrier->save(); } // Update price (because of possibility of price impact) $order->updatePrice($ebay_profile); } $order->add($this->ebay_profile->id); if (!version_compare(_PS_VERSION_, '1.5', '>')) { foreach ($order->getProducts() as $product) { $this->hookAddProduct(array('product' => new Product((int) $product['id_product']))); } } foreach ($customer_ids as $id_customer) { // Fix on sending e-mail Db::getInstance()->autoExecute(_DB_PREFIX_ . 'customer', array('email' => pSQL($order->getEmail())), 'UPDATE', '`id_customer` = ' . (int) $id_customer); } } $orders_ar = array(); foreach ($orders as $order) { $orders_ar[] = array('id_order_ref' => $order->getIdOrderRef(), 'id_order_seller' => $order->getIdOrderSeller(), 'amount' => $order->getAmount(), 'status' => $order->getStatus(), 'date' => $order->getDate(), 'email' => $order->getEmail(), 'products' => $order->getProducts(), 'error_messages' => $order->getErrorMessages()); } file_put_contents(dirname(__FILE__) . '/log/orders.php', "<?php\n\n" . '$dateLastImport = ' . '\'' . date('d/m/Y H:i:s') . "';\n\n" . '$orders = ' . var_export($orders_ar, true) . ";\n\n"); if (Configuration::get('EBAY_ACTIVATE_MAILS') && $errors_email) { $data = ''; foreach ($errors_email as $e) { $data .= '<p>Id order : <strong>' . $e['id_order_seller'] . '</strong></p><ul>'; foreach ($e['messages'] as $m) { $data .= '<li>' . $m . '</li>'; } $data .= '</ul><br/>'; } Mail::Send((int) Configuration::get('PS_LANG_DEFAULT'), 'errorsImportEbay', Mail::l('Errors import', (int) Configuration::get('PS_LANG_DEFAULT')), array('{errors_email}' => $data), strval(Configuration::get('PS_SHOP_EMAIL')), null, strval(Configuration::get('PS_SHOP_EMAIL')), strval(Configuration::get('PS_SHOP_NAME')), null, null, dirname(__FILE__) . '/views/templates/mails/'); } }
public function updatePrice() { $total_price_tax_excl = 0; $total_shipping_tax_incl = 0; $total_shipping_tax_excl = 0; $id_carrier = (int) EbayShipping::getPsCarrierByEbayCarrier($this->shippingService); if (version_compare(_PS_VERSION_, '1.4.0.5', '<')) { $carrier_tax_rate = (double) $this->_getTaxByCarrier((int) $id_carrier); } else { $carrier_tax_rate = (double) Tax::getCarrierTaxRate((int) $id_carrier); } foreach ($this->product_list as $product) { if (version_compare(_PS_VERSION_, '1.4.0.5', '<')) { $tax_rate = (double) $this->_getTaxByProduct((int) $product['id_product']); } else { $tax_rate = (double) Tax::getProductTaxRate((int) $product['id_product']); } $coef_rate = 1 + $tax_rate / 100; $detail_data = array('product_price' => (double) ($product['price'] / $coef_rate), 'unit_price_tax_incl' => (double) $product['price'], 'unit_price_tax_excl' => (double) ($product['price'] / $coef_rate), 'total_price_tax_incl' => (double) ($product['price'] * $product['quantity']), 'total_price_tax_excl' => (double) ($product['price'] / $coef_rate * $product['quantity']), 'reduction_percent' => 0, 'reduction_amount' => 0); Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_detail', $detail_data, 'UPDATE', '`id_order` = ' . (int) $this->id_order . ' AND `product_id` = ' . (int) $product['id_product'] . ' AND `product_attribute_id` = ' . (int) $product['id_product_attribute']); if (version_compare(_PS_VERSION_, '1.5', '>')) { $detail_tax_data = array('unit_amount' => (double) ($product['price'] - $product['price'] / $coef_rate), 'total_amount' => (double) ($product['price'] - $product['price'] / $coef_rate) * $product['quantity']); DB::getInstance()->autoExecute(_DB_PREFIX_ . 'order_detail_tax', $detail_tax_data, 'UPDATE', '`id_order_detail` = (SELECT `id_order_detail` FROM `' . _DB_PREFIX_ . 'order_detail` WHERE `id_order` = ' . (int) $this->id_order . ' AND `product_id` = ' . (int) $product['id_product'] . ' AND `product_attribute_id` = ' . (int) $product['id_product_attribute'] . ') '); } $total_price_tax_excl += (double) ($product['price'] / $coef_rate * $product['quantity']); // ebay get one shipping cost by product $total_shipping_tax_incl += $this->shippingServiceCost; $total_shipping_tax_excl += $this->shippingServiceCost / (1 + $carrier_tax_rate / 100); } $data = array('total_paid' => (double) $this->amount, 'total_paid_real' => (double) $this->amount, 'total_products' => (double) $total_price_tax_excl, 'total_products_wt' => (double) ($this->amount - $this->shippingServiceCost), 'total_shipping' => (double) $total_shipping_tax_incl, 'total_shipping_tax_incl' => (double) $total_shipping_tax_incl, 'total_shipping_tax_excl' => (double) $total_shipping_tax_excl); if ((double) $this->shippingServiceCost == 0) { $data = array_merge($data, array('total_shipping_tax_excl' => 0, 'total_shipping_tax_incl' => 0)); } if (version_compare(_PS_VERSION_, '1.5', '>')) { $order = new Order((int) $this->id_order); $data_old = $data; $data = array_merge($data, array('total_paid_tax_incl' => (double) $this->amount, 'total_paid_tax_excl' => (double) ($total_price_tax_excl + $order->total_shipping_tax_excl))); // Update Incoice $invoice_data = $data; unset($invoice_data['total_paid'], $invoice_data['total_paid_real'], $invoice_data['total_shipping']); Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_invoice', $invoice_data, 'UPDATE', '`id_order` = ' . (int) $this->id_order); // Update payment $payment_data = array('amount' => (double) $this->amount); Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_payment', $payment_data, 'UPDATE', '`order_reference` = "' . pSQL($order->reference) . '" '); } return Db::getInstance()->autoExecute(_DB_PREFIX_ . 'orders', $data, 'UPDATE', '`id_order` = ' . (int) $this->id_order); }