Michael RICOIS dbc9d4b8e3 CS
2018-01-11 13:19:25 +01:00

511 lines
18 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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',
'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);
/*if($delivery_address->id_country == 8) {
$replace_tests = array(
'-S-' => ' SUR ',
'-SS-' => ' SOUS ',
' ST-' => ' SAINT ',
'-ST ' => ' SAINT ',
'-ST-' => ' SAINT ',
' STE-' => ' SAINTE ',
'-STE ' => ' SAINTE ',
'-STE ' => ' SAINTE ',
' MT-' => ' MONT ',
'-MT ' => ' MONT ',
'-MT-' => ' MONT ',
' BG-' => ' BOURG ',
'-BG ' => ' BOURG ',
'-BG-' => ' BOURG ',
' GD-' => ' GRAND ',
'-GD ' => ' GRAND ',
'-GD-' => ' GRAND ',
' GDE-' => ' GRANDE ',
'-GDE ' => ' GRANDE ',
'-GDE-' => ' GRANDE ',
' MTS-' => ' MONTS ',
'-MTS ' => ' MONTS ',
'-MTS-' => ' MONTS ',
' VX-' => ' VIEUX ',
'-VX ' => ' VIEUX ',
'-VX-' => ' VIEUX ',
' HT-' => ' HAUT ',
'-HT ' => ' HAUT ',
'-HT-' => ' HAUT ',
' BS-' => ' BAS ',
'-BS ' => ' BAS ',
'-BS-' => ' BAS ',
' S ' => ' SUR ',
' SS ' => ' SOUS ',
' ST ' => ' SAINT ',
' STE ' => ' SAINTE ',
' MT ' => ' MONT ',
' BG ' => ' BOURG ',
' GD ' => ' GRAND ',
' GDE ' => ' GRANDE ',
' MTS ' => ' MONTS ',
' VX ' => ' VIEUX ',
' HT ' => ' HAUT ',
' BS ' => ' BAS ',
' ST. ' => ' SAINT ',
);
$cities = array();
foreach(Db::getInstance()->ExecuteS('
SELECT `city`
FROM `'._DB_PREFIX_.'france_postcode`
WHERE `postcode` = "'.pSQL(trim($delivery_address->postcode)).'"
') as $row) {
$cities[] = $row['city'];
}
$found = FALSE;
if($cities) {
foreach($cities as $city) {
if(str_replace(array('\\', '-', '_', ' ', '/', '\'', '"', '«', '»', '', ' ', '.', ',',), '', strtolower(trim(stripaccents($city)))) == str_replace(array('\\', '-', '_', ' ', '/', '\'', '"', '«', '»', '', ' ', '.', ',',), '', strtolower(trim(stripaccents(preg_replace('/ C(I|E)DEX(.*)$/i', '', $delivery_address->city)))))) {
$found = TRUE;
}
}
}
if(!$found) {
foreach($cities as $city) {
$clean_city = str_replace(array('\\', '-', '_', ' ', '/', '\'', '"', '«', '»', '', ' ', '.', ',',), '', strtolower(trim(stripaccents($city))));
$clean_customer_city = str_replace(array('\\', '-', '_', ' ', '/', '\'', '"', '«', '»', '', ' ', '.', ',',), '', strtolower(trim(str_ireplace('/', ' SUR ', str_ireplace(' S/', ' SUR ', str_ireplace(array_keys($replace_tests), array_values($replace_tests), stripaccents(' '.preg_replace('/ C(I|E)DEX(.*)$/i', '', $delivery_address->city).' ')))))));
if($clean_city === $clean_customer_city
|| $clean_city === 'le'.$clean_customer_city
|| $clean_city === 'la'.$clean_customer_city
|| $clean_city === 'les'.$clean_customer_city
|| $clean_city === 'l'.$clean_customer_city
|| 'le'.$clean_city === $clean_customer_city
|| 'la'.$clean_city === $clean_customer_city
|| 'les'.$clean_city === $clean_customer_city
|| 'l'.$clean_city === $clean_customer_city) {
$found = TRUE;
}
}
}
$france_address_error = !$found;
$france_address_suggestions = array();
if($france_address_error) {
$france_address_suggestions = Db::getInstance()->ExecuteS('
SELECT `postcode`, `city`
FROM `'._DB_PREFIX_.'france_postcode`
WHERE `postcode` = "'.pSQL($delivery_address->postcode).'"
OR `city` = "'.pSQL($delivery_address->city).'"
GROUP BY `postcode`, `city`
');
}
} else {*/
$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) {
$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'].'
');
$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'].'
');
/**
* @Override 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'] . '
');
/**
* @Override 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'] . '
');
/**
* @Override 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'] . '
');
$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');
/*$shipping = Db::getInstance()->getRow('
SELECT s.`id_shipping`, s.`id_sale`
FROM `'._DB_PREFIX_.'privatesale_shipping_sale` s
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c
ON c.`id_sale` = s.`id_sale`
WHERE c.`id_product` = '.(int) $p['product_id'].'
');
if(!$shipping) {
$p['id_shipping'] = 1; // PSS
$p['id_sale'] = 999999;
} else {
$p['id_shipping'] = $shipping['id_shipping'];
$p['id_sale'] = $shipping['id_sale'];
}*/
$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']]: '';
$shipping_numbers = array();
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']).'"';
}
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']).'"';
}
/*
* @Override 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']).'"';
}
/*
* @Override 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']).'"';
}
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 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`
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`
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`
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`
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`
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`
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
FROM `'._DB_PREFIX_.'ant_dropshipping_parcel` l
LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON d.`id_order_detail` = l.`id_order_detail`
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 PHILEA
if($l = Db::getInstance()->ExecuteS('
SELECT l.*, d.`product_name`, d.`product_reference`, d.`product_supplier_reference`, "Philea" as firstname, c.`name` as carrier
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);
}
} 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'));
}
}