405 lines
15 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
include dirname(__FILE__).'/../../config/config.inc.php';
$laposte_modes = array(
'DOM' => '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',
2017-01-19 17:56:05 +01:00
'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',
);
2016-01-04 12:49:26 +01:00
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')));
2016-01-04 12:49:26 +01:00
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;
2016-01-04 12:49:26 +01:00
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
2017-01-20 10:48:37 +01:00
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
');
2017-01-20 10:48:37 +01:00
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;
2016-01-04 12:49:26 +01:00
}
$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);
2018-01-15 15:38:14 +01:00
$france_address_error = FALSE;
$france_address_suggestions = array();
2016-01-04 12:49:26 +01:00
$id_order_details = array();
$sales_carriers = array();
$link = new Link(2);
$products = array();
foreach($order->getProductsDetail() as $p) {
2018-01-15 16:03:18 +01:00
// --- Get quantity sent
2018-01-15 15:45:33 +01:00
// LaPoste
$p['product_quantity_sent'] += (int) Db::getInstance()->getValue('
2017-11-13 13:00:29 +01:00
SELECT IFNULL(SUM(`quantity`), 0)
2016-01-04 12:49:26 +01:00
FROM `'._DB_PREFIX_.'lapostews`
WHERE `id_order_detail` = '.(int) $p['id_order_detail'].'
2017-11-13 13:00:29 +01:00
');
2018-01-15 15:45:33 +01:00
// Exapaq
2017-11-13 13:00:29 +01:00
$p['product_quantity_sent'] += (int) Db::getInstance()->getValue('
SELECT IFNULL(SUM(`quantity`), 0)
2016-01-04 12:49:26 +01:00
FROM `'._DB_PREFIX_.'exapaqws`
WHERE `id_order_detail` = '.(int) $p['id_order_detail'].'
');
2018-01-15 15:45:33 +01:00
// MondialRelay
$p['product_quantity_sent'] += (int) Db::getInstance()->getValue('
2017-11-13 13:00:29 +01:00
SELECT IFNULL(SUM(`quantity`), 0)
FROM `'._DB_PREFIX_.'mondialrelay_parcel`
WHERE `id_order_detail` = ' . (int) $p['id_order_detail'] . '
');
2018-01-15 15:45:33 +01:00
// Philea
2017-11-13 13:00:29 +01:00
$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'] . '
');
2018-01-15 15:45:33 +01:00
// DropShipping
2017-11-13 13:00:29 +01:00
$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'] . '
');
2016-01-04 12:49:26 +01:00
$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'].'
');
2016-06-20 18:27:54 +02:00
$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
);
2018-01-15 15:45:33 +01:00
if (!isset($sales_carriers[(int) $p['id_sale']])) {
2016-01-04 12:49:26 +01:00
$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']];
2018-01-15 15:45:33 +01:00
$p['location'] = isset($locations[(int) $p['product_id'].'-'.(int) $p['product_attribute_id']]) ?
$locations[(int) $p['product_id'].'-'.(int) $p['product_attribute_id']]: '';
2018-01-15 16:03:18 +01:00
// --- Get Shipping Numbers
2016-01-04 12:49:26 +01:00
$shipping_numbers = array();
2018-01-15 16:03:18 +01:00
// 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']).'"';
}
2018-01-15 15:45:33 +01:00
// LaPoste
2016-01-04 12:49:26 +01:00
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`)
2016-01-04 12:49:26 +01:00
WHERE h.`id_order` = '.(int) $order->id.'
') as $row) {
$shipping_numbers[] = '"'.pSQL($row['shipping_number']).'"';
}
2018-01-15 15:45:33 +01:00
// Exapaq
2016-01-04 12:49:26 +01:00
foreach(Db::getInstance()->ExecuteS('
SELECT e.`shipping_number`
2016-01-04 12:49:26 +01:00
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']).'"';
}
2018-01-15 15:45:33 +01:00
// 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`)
2016-01-04 12:49:26 +01:00
WHERE h.`id_order` = '.(int) $order->id.'
') as $row) {
$shipping_numbers[] = '"'.pSQL($row['shipping_number']).'"';
}
2018-01-15 15:45:33 +01:00
if(((int) $p['product_quantity_sent'] === 0) && Db::getInstance()->getRow('
2016-01-04 12:49:26 +01:00
SELECT h.`id_sale`
FROM `'._DB_PREFIX_.'shipping_history` h
2018-01-15 16:03:18 +01:00
WHERE h.`id_sale` = '.((int) $p['id_sale'] === 999999 ? 0: (int) $p['id_sale']).'
2016-01-04 12:49:26 +01:00
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();
2018-01-15 16:03:18 +01:00
// 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);
}
2018-01-11 13:19:25 +01:00
// Send by LAPOSTE
2018-01-15 16:03:18 +01:00
if ($l = Db::getInstance()->ExecuteS('
2018-01-11 16:22:25 +01:00
SELECT l.*, d.`product_name`, d.`product_reference`, d.`product_supplier_reference`,
e.`firstname`, e.`lastname`, "laposte" as carrier, "0" as philea
2016-01-04 12:49:26 +01:00
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 `ps_carrier` c ON c.`id_carrier` = o.`id_carrier`
2016-01-04 12:49:26 +01:00
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);
}
2018-01-11 13:19:25 +01:00
// Send by EXAPAQ
2018-01-15 16:03:18 +01:00
if ($l = Db::getInstance()->ExecuteS('
2018-01-11 16:22:25 +01:00
SELECT l.*, d.`product_name`, d.`product_reference`, d.`product_supplier_reference`,
e.`firstname`, e.`lastname`, "exapaq" as carrier, "0" as philea
2016-01-04 12:49:26 +01:00
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_.'carrier` c ON c.`id_carrier` = o.`id_carrier`
2016-01-04 12:49:26 +01:00
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);
}
2018-01-11 13:19:25 +01:00
// Send by MONDIAL RELAY
2018-01-15 16:03:18 +01:00
if ($l = Db::getInstance()->ExecuteS('
2018-01-11 16:22:25 +01:00
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 `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);
}
2018-01-11 13:19:25 +01:00
// Send by DROPSHIPPING
2018-01-15 16:03:18 +01:00
if ($l = Db::getInstance()->ExecuteS('
2018-01-11 16:22:25 +01:00
SELECT l.*, d.`product_name`, d.`product_reference`, d.`product_supplier_reference`,
"Dropshipping" as firstname, "0" as philea
2017-10-11 13:24:49 +02:00
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_.'carrier` c ON c.`id_carrier` = o.`id_carrier`
2017-10-11 13:24:49 +02:00
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);
}
2018-01-15 16:03:18 +01:00
2016-01-04 12:49:26 +01:00
} else {
$logs = array();
}
// Carriers
$_carriers = $carriers[$order->id_carrier].($delivery_infos_laposte
2016-01-04 12:49:26 +01:00
? ' ('.$laposte_modes[$delivery_infos_laposte['delivery_mode']].')'
: ($delivery_infos_exapaq
? ' (ICI Relais)'
: ($delivery_infos_mr
? ' ('.$mr_modes[$delivery_infos_mr['dlv_mode']].')'
: ''
)
2016-01-04 12:49:26 +01:00
)
);
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,
2016-01-04 12:49:26 +01:00
'france_address_suggestions' => $france_address_suggestions,
));
} else {
echo json_encode(array('errors' => 'not found'));
}
}