'SO à domicile', 'RDV' => 'SO sur RDV', 'BPR' => 'SO en bureau de poste', 'ACP' => 'SO en agence ColiPoste', 'CDI' => 'SO en centre de distribution', 'A2P' => 'SO en commerce de proximité', 'MRL' => 'SO à domicile (+?)', 'CIT' => 'SO en espace Cityssimo', 'DOS' => 'SO à domicile contre signature', 'COLI' => 'Colissimo Expert Inter', 'COL' => 'Colissimo Expert France', 'COM' => 'Colissimo Access OM', 'COLD' => 'Colissimo Access France', 'COE' => 'Colissimo Eco OM', 'CORE' => 'Colissimo Service France', 'CDS' => 'Colissimo Expert OM', 'COP' => 'Colissimo Expert Interne', ); $mr_modes = array( '24R' => '24R : Livraison dans un Point Relais', '24L' => '24L : Livraison dans un Point Relais XL', '24X' => '24X : Livraison dans un Point Relais XXL', 'DRI' => 'DRI : Livraison Colis Drive', 'LD1' => 'LD1 : Livraison à domicile RDC (1 personne)', 'LDS' => 'LDS : Livraison à domicile spécialisée (2 personnes)', 'HOM' => 'HOM : Livraison à domicile spécialisée', ); function stripaccents($str) { return preg_replace( '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);~i', '$1', htmlentities($str, ENT_NOQUOTES, 'UTF-8') ); } $mr_carriers = array_map('intval',explode(',', Configuration::get('ANT_CARRIERS_MR'))); if(($id_order = (int) Tools::getValue('id_order')) && (Tools::getValue('token') === Tools::getAdminToken('AdminLogistics'.(int) Tab::getIdFromClassName('AdminLogistics').(int) Tools::getValue('id_employee')))) { $order = new Order($id_order); if(Validate::isLoadedObject($order)) { $customer = Db::getInstance()->getRow(' SELECT `firstname`, `lastname`, `email` FROM `'._DB_PREFIX_.'customer` WHERE `id_customer` = '.(int) $order->id_customer.' '); $delivery_infos_laposte = FALSE; $delivery_infos_exapaq = FALSE; $delivery_infos_mr = FALSE; if($order->id_carrier == Configuration::get('SOCOLISSIMO_CARRIER_ID')) { $delivery_infos_laposte = Db::getInstance()->getRow(' SELECT i.*, cl.`name` AS `country_name` FROM `'._DB_PREFIX_.'socolissimo_delivery_info` i LEFT JOIN `'._DB_PREFIX_.'country` c ON i.`cecountry` = c.`iso_code` LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON c.`id_country` = cl.`id_country` WHERE i.`id_cart` = '.(int) $order->id_cart.' AND cl.`id_lang` = 2 '); } elseif(in_array((int) $order->id_carrier, $mr_carriers)) { /** * @Override MondialRelay */ $delivery_infos_mr = Db::getInstance()->getRow(' SELECT mr_s.*, mr_m.*, cl.`name` AS `country_name` FROM `'._DB_PREFIX_.'mr_selected` mr_s LEFT JOIN `'._DB_PREFIX_.'country` c ON (mr_s.`MR_Selected_Pays` = c.`iso_code`) LEFT JOIN `'._DB_PREFIX_.'mr_method` mr_m ON (mr_m.`id_mr_method` = mr_s.`id_method`) LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (c.`id_country` = cl.`id_country`) WHERE mr_s.`id_cart` = '.(int) $order->id_cart.' AND cl.`id_lang` = 2 '); if(!$delivery_infos_mr || empty($delivery_infos_mr)) { $delivery_infos_mr = Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'mr_selected` s INNER JOIN `'._DB_PREFIX_.'mr_method` m ON (m.`id_mr_method` = s.`id_method`) WHERE s.`id_cart` = '.(int) $order->id_cart.' '); } // $delivery_infos_laposte = FALSE; } $delivery_infos_exapaq = Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'icirelais_selected` WHERE `id_cart` = '.(int) $order->id_cart.' '); $status = Db::getInstance()->getRow(' SELECT `name` FROM `'._DB_PREFIX_.'order_state_lang` WHERE `id_order_state` = '.(int) $order->getCurrentState().' AND `id_lang` = 2 '); $locations = array(); foreach(Db::getInstance()->ExecuteS(' SELECT d.`product_id`, d.`product_attribute_id`, IF(IFNULL(a.`location`, "") = "", p.`location`, a.`location`) AS `location` FROM `'._DB_PREFIX_.'order_detail` d LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = d.`product_id` LEFT JOIN `'._DB_PREFIX_.'product_attribute` a ON a.`id_product_attribute` = d.`product_attribute_id` WHERE d.`id_order` = '.(int) $order->id.' ') as $row) { $locations[(int) $row['product_id'].'-'.(int) $row['product_attribute_id']] = $row['location']; } $invoice_address = new Address($order->id_address_invoice); $delivery_address = new Address($order->id_address_delivery); $france_address_error = FALSE; $france_address_suggestions = array(); $id_order_details = array(); $sales_carriers = array(); $link = new Link(2); $products = array(); foreach($order->getProductsDetail() as $p) { // --- Get quantity sent // LaPoste $p['product_quantity_sent'] += (int) Db::getInstance()->getValue(' SELECT IFNULL(SUM(`quantity`), 0) FROM `'._DB_PREFIX_.'lapostews` WHERE `id_order_detail` = '.(int) $p['id_order_detail'].' '); // Exapaq $p['product_quantity_sent'] += (int) Db::getInstance()->getValue(' SELECT IFNULL(SUM(`quantity`), 0) FROM `'._DB_PREFIX_.'exapaqws` WHERE `id_order_detail` = '.(int) $p['id_order_detail'].' '); // MondialRelay $p['product_quantity_sent'] += (int) Db::getInstance()->getValue(' SELECT IFNULL(SUM(`quantity`), 0) FROM `'._DB_PREFIX_.'mondialrelay_parcel` WHERE `id_order_detail` = ' . (int) $p['id_order_detail'] . ' '); // Philea $p['product_quantity_sent'] += (int) Db::getInstance()->getValue(' SELECT IFNULL(SUM(`quantity`), 0) FROM `'._DB_PREFIX_.'philea_parcel` WHERE `id_order_detail` = ' . (int) $p['id_order_detail'] . ' '); // DropShipping $p['product_quantity_sent'] += (int) Db::getInstance()->getValue(' SELECT IFNULL(SUM(`quantity`), 0) FROM `'._DB_PREFIX_.'ant_dropshipping_parcel` WHERE `id_order_detail` = ' . (int) $p['id_order_detail'] . ' '); // Image $p['id_image'] = 0; if ((int) $p['product_attribute_id'] != 0) { $img = Db::getInstance()->getRow(' SELECT `id_image` FROM `'._DB_PREFIX_.'product_attribute_image` WHERE `id_product_attribute` = '.(int) $p['product_attribute_id'].' '); $p['id_image'] = $img['id_image']; } if ($p['id_image'] == 0) { $img = Db::getInstance()->getRow(' SELECT `id_image` FROM `'._DB_PREFIX_.'image` WHERE `cover` = 1 AND `id_product` = '.(int) $p['product_id'].' '); $p['id_image'] = $img['id_image']; } $p['image'] = '//'.$link->getImageLink('image', $p['product_id'].'-'.$p['id_image'], 'home'); $p['id_shipping'] = 1; $p['id_sale'] = (int) Db::getInstance()->getValue(' SELECT `id_sale` FROM `'._DB_PREFIX_.'product_ps_cache` WHERE `id_product` = '.(int) $p['product_id'].' '); $p['is_pending'] = Db::getInstance()->getValue(' SELECT COUNT(id_product) FROM `'._DB_PREFIX_.'logistics_pending` WHERE `id_product` = '.(int) $p['product_id'].' AND id_order = '.(int)$id_order ); if (!isset($sales_carriers[(int) $p['id_sale']])) { $sales_carriers[(int) $p['id_sale']] = array(); foreach(Db::getInstance()->ExecuteS(' SELECT `id_carrier` FROM `'._DB_PREFIX_.'privatesale_carrier` WHERE `id_sale` = '.(int) $p['id_sale'].' ') as $row) { $sales_carriers[(int) $p['id_sale']][] = (int) $row['id_carrier']; } } $p['carriers'] = $sales_carriers[(int) $p['id_sale']]; $p['location'] = isset($locations[(int) $p['product_id'].'-'.(int) $p['product_attribute_id']]) ? $locations[(int) $p['product_id'].'-'.(int) $p['product_attribute_id']]: ''; // --- Get Shipping Numbers $shipping_numbers = array(); // Philea foreach(Db::getInstance()->ExecuteS(' SELECT l.`shipping_number` FROM `'._DB_PREFIX_.'philea_parcel` l LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON (d.`id_order_detail` = l.`id_order_detail`) LEFT JOIN `'._DB_PREFIX_.'order_history` h ON (h.`id_order` = d.`id_order`) WHERE h.`id_order` = '.(int) $order->id.' ') as $row) { $shipping_numbers[] = '"'.pSQL($row['shipping_number']).'"'; } // LaPoste foreach(Db::getInstance()->ExecuteS(' SELECT l.`shipping_number` FROM `'._DB_PREFIX_.'lapostews` l LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON (d.`id_order_detail` = l.`id_order_detail`) LEFT JOIN `'._DB_PREFIX_.'order_history` h ON (h.`id_order` = d.`id_order`) WHERE h.`id_order` = '.(int) $order->id.' ') as $row) { $shipping_numbers[] = '"'.pSQL($row['shipping_number']).'"'; } // Exapaq foreach(Db::getInstance()->ExecuteS(' SELECT e.`shipping_number` FROM `'._DB_PREFIX_.'exapaqws` e LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON (d.`id_order_detail` = e.`id_order_detail`) LEFT JOIN `'._DB_PREFIX_.'order_history` h ON (h.`id_order` = d.`id_order`) WHERE h.`id_order` = '.(int) $order->id.' ') as $row) { $shipping_numbers[] = '"'.pSQL($row['shipping_number']).'"'; } // MondialRelay foreach(Db::getInstance()->ExecuteS(' SELECT l.`shipping_number` FROM `'._DB_PREFIX_.'mondialrelay_parcel` l LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON (d.`id_order_detail` = l.`id_order_detail`) LEFT JOIN `'._DB_PREFIX_.'order_history` h ON (h.`id_order` = d.`id_order`) WHERE h.`id_order` = '.(int) $order->id.' ') as $row) { $shipping_numbers[] = '"'.pSQL($row['shipping_number']).'"'; } if (((int) $p['product_quantity_sent'] === 0) && Db::getInstance()->getRow(' SELECT h.`id_sale` FROM `'._DB_PREFIX_.'shipping_history` h WHERE h.`id_sale` = '.((int) $p['id_sale'] === 999999 ? 0 : (int) $p['id_sale']).' AND h.`id_order` = '.(int) $order->id.' '.(count($shipping_numbers) > 0 ? 'AND h.`shipping_number` NOT IN ('.implode(', ', $shipping_numbers).')': '').' ')) { $p['product_quantity_sent'] = $p['product_quantity'] - ( $p['product_quantity_return'] > 0 ? $p['product_quantity_return']: $p['product_quantity_refunded'] ); } $products[] = $p; $id_order_details[] = (int) $p['id_order_detail']; } $id_carriers = array((int) $order->id_carrier); foreach(array_values($sales_carriers) as $c) { $id_carriers = array_merge($id_carriers, $c); } $carriers = array(); foreach(Db::getInstance()->ExecuteS(' SELECT `id_carrier`, `name` FROM `'._DB_PREFIX_.'carrier` WHERE `id_carrier` IN ('.implode(', ', array_unique($id_carriers)).') ') as $row) { $carriers[(int) $row['id_carrier']] = $row['name']; } if(count($id_order_details) > 0) { $logs = array(); // Send by PHILEA if ($l = Db::getInstance()->ExecuteS(' SELECT l.*, d.`product_name`, d.`product_reference`, d.`product_supplier_reference`, "Philea" as firstname, (CASE c.`external_module_name` WHEN "soflexibilite" THEN "laposte" ELSE c.`external_module_name` END) as carrier, "1" as philea FROM `'._DB_PREFIX_.'philea_parcel` l LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON d.`id_order_detail` = l.`id_order_detail` LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order` LEFT JOIN `'._DB_PREFIX_.'carrier` c ON c.`id_carrier` = o.`id_carrier` WHERE l.`id_order_detail` IN ('.implode(', ', $id_order_details).') AND l.`quantity` > 0 ORDER BY l.`date_add` DESC ')) { $logs = array_merge($logs, $l); } // Send by LAPOSTE if ($l = Db::getInstance()->ExecuteS(' SELECT l.*, d.`product_name`, d.`product_reference`, d.`product_supplier_reference`, e.`firstname`, e.`lastname`, "laposte" as carrier, "0" as philea FROM `'._DB_PREFIX_.'lapostews` l LEFT JOIN `'._DB_PREFIX_.'employee` e ON l.`id_employee` = e.`id_employee` LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON d.`id_order_detail` = l.`id_order_detail` LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order` LEFT JOIN `ps_carrier` c ON c.`id_carrier` = o.`id_carrier` WHERE l.`id_order_detail` IN ('.implode(', ', $id_order_details).') AND l.`quantity` > 0 ORDER BY l.`date_add` DESC ')) { $logs = array_merge($logs, $l); } // Send by EXAPAQ if ($l = Db::getInstance()->ExecuteS(' SELECT l.*, d.`product_name`, d.`product_reference`, d.`product_supplier_reference`, e.`firstname`, e.`lastname`, "exapaq" as carrier, "0" as philea FROM `'._DB_PREFIX_.'exapaqws` l LEFT JOIN `'._DB_PREFIX_.'employee` e ON l.`id_employee` = e.`id_employee` LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON d.`id_order_detail` = l.`id_order_detail` LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order` LEFT JOIN `'._DB_PREFIX_.'carrier` c ON c.`id_carrier` = o.`id_carrier` WHERE l.`id_order_detail` IN ('.implode(', ', $id_order_details).') AND l.`quantity` > 0 ORDER BY l.`date_add` DESC ')) { $logs = array_merge($logs, $l); } // Send by MONDIAL RELAY if ($l = Db::getInstance()->ExecuteS(' SELECT l.*, d.`product_name`, d.`product_reference`, d.`product_supplier_reference`, e.`firstname`, e.`lastname`, "mondialrelay" as carrier, "0" as philea FROM `'._DB_PREFIX_.'mondialrelay_parcel` l LEFT JOIN `'._DB_PREFIX_.'employee` e ON l.`id_employee` = e.`id_employee` LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON d.`id_order_detail` = l.`id_order_detail` LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order` LEFT JOIN `ps_carrier` c ON c.`id_carrier` = o.`id_carrier` WHERE l.`id_order_detail` IN ('.implode(', ', $id_order_details).') AND l.`quantity` > 0 ORDER BY l.`date_add` DESC ')) { $logs = array_merge($logs, $l); } // Send by DROPSHIPPING if ($l = Db::getInstance()->ExecuteS(' SELECT l.*, d.`product_name`, d.`product_reference`, d.`product_supplier_reference`, "Dropshipping" as firstname, "0" as philea FROM `'._DB_PREFIX_.'ant_dropshipping_parcel` l LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON d.`id_order_detail` = l.`id_order_detail` LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order` LEFT JOIN `'._DB_PREFIX_.'carrier` c ON c.`id_carrier` = o.`id_carrier` WHERE l.`id_order_detail` IN ('.implode(', ', $id_order_details).') AND l.`quantity` > 0 ORDER BY l.`date_add` DESC ')) { $logs = array_merge($logs, $l); } } else { $logs = array(); } // Carriers $_carriers = $carriers[$order->id_carrier].($delivery_infos_laposte ? ' ('.$laposte_modes[$delivery_infos_laposte['delivery_mode']].')' : ($delivery_infos_exapaq ? ' (ICI Relais)' : ($delivery_infos_mr ? ' ('.$mr_modes[$delivery_infos_mr['dlv_mode']].')' : '' ) ) ); echo json_encode(array( 'errors' => FALSE, 'order' => $order, 'products' => $products, 'status' => $status['name'], 'carrier' => $_carriers, 'carriers' => $carriers, 'customer' => $customer['firstname'].' '.$customer['lastname'].' ('.$customer['email'].')', 'invoice_address' => $invoice_address, 'delivery_address' => $delivery_address, 'delivery_infos_laposte' => $delivery_infos_laposte, 'delivery_infos_exapaq' => $delivery_infos_exapaq, 'delivery_infos_mr' => $delivery_infos_mr, 'weight' => $order->getTotalWeight(), 'logs' => $logs, 'france_address_error' => $france_address_error, 'france_address_suggestions' => $france_address_suggestions, )); } else { echo json_encode(array('errors' => 'not found')); } }