bebeboutik/modules/philea_magistor/script/recept_expedition.php
2016-09-06 15:32:52 +02:00

368 lines
13 KiB
PHP

<?php
include('../../../config/settings.inc.php');
include('../../../config/config.inc.php');
$_SERVER['SERVER_PORT'] = 80;
include( dirname(__FILE__) . '/../philea_magistor.php');
global $regex_file_in;
$regex_file_in = '@^CRE(.*)\.(BAL|DAT)@';
@ini_set('display_errors', 'on');
$magistorModule = new philea_magistor();
$id_shipping = 4; // PHILEA
$CRE = array(
'OP_CODE' => array(1,10),
'CODE_SOC' => array(11,20),
'NO_CLIENT' => array(31,8),
'N_CDE' => array(39,50),
'NO_COLIS' => array(89,50),
'NO_TRACKING' => array(139,50),
'NO_EXPEDITION' => array(189,50),
'DATE_EXPED' => array(239,8),
'TRANSPORTEUR' => array(247,50),
);
$tab_conversion_carrier = philea_magistor::getTabState();
$socol_to_magistor = array(
'DOM' => 'SOCOLMDSS',
'DOS' => 'SOCOLMDS',
'RDV' => 'SOCOLMRDV',
'CIT' => 'SOCOLCITY',
'BPR' => 'SOCOLMBP',
'CDI' => 'SOCOLMBP',
'A2P' => 'SOCOLMC',
'ACP' => 'SOCOLMBP',
);
$mr_to_magistor = array(
'24R' => 'MRMDSS', // Point relais
'DRI' => 'MRMDS', // Colis drive
'LD1' => 'MRMRDV', // Domicile RDC (1 pers)
'LDS' => 'MRCITY', // Domicile spé (2 pers)
'HOM' => 'MRMBP', // Domicile spé
);
$format = $CRE;
if($magistorModule->active) {
require_once('connection_ftp.php');
@set_time_limit(0);
// Checking for ".bal" files with a "CRE" prefix that has an equivalent ".dat" file and process them...
$inFolder = dirname(__FILE__) . '/IN/';
$iterator = new DirectoryIterator($inFolder);
$id_order_state = Configuration::get('PHILEA_MAGISTOR_STATUS_CRE');
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
if( preg_match( '@^CRE(.*)\.BAL@', $fileinfo->getFilename() ) ) {
$datFile = $fileinfo->getPath() . '/' . preg_replace( '@BAL$@', 'DAT', $fileinfo->getFilename() );
if( file_exists( $datFile ) ) {
$content = file_get_contents( $datFile );
$lines = preg_split( '@\n@', $content );
if( !empty($lines) ) {
foreach( $lines as $line ) {
$data = array();
foreach($format as $field=>$value)
$data[] = substr($line, ($value[0]-1), $value[1]);
if( !isset($data[2]) || (isset($data[2]) && !$data[2]) )
continue;
$id_sale = 0;
$id_order = (string) $data[3];
if(substr($id_order, 0, 2) == 'OP') {
$id_order = explode('-', $id_order);
if(count($id_order) < 2) {
continue;
}
$id_sale = (int) str_replace('OP', '', $id_order[0]);
$id_order = (int) $id_order[1];
} else {
$id_order = (int) $id_order;
}
$order = new Order((int) ($id_order));
if(Validate::isLoadedObject($order))
{
$order->shipping_number = trim($data[5]);
$order->id_sale_shipping = $id_sale;
$new_id_order_state = $id_order_state;
## GET SHIPPING CARRIER ID
$id_active_carrier = FALSE;
$id_inactive_carrier = FALSE;
$carrier_found = FALSE;
if (trim($data[5]) && isset($data[8]) && $data[8]){
$carrier_name = trim($data[8]);
if ($carrier_name == '315')
$carrier_name = 'DUCROS';
$carriers = philea_magistor::getCarriersIdByRef($carrier_name);
if ($carriers){
foreach ($carriers as $carrier) {
if ($carrier['name'] == 'GLS' && $carrier_name == 'DPD')
continue;
if ($carrier['active'] == 1 && $carrier['deleted'] == 0)
$id_active_carrier = (int) $carrier['id_carrier'];
if ($carrier['active'] == 0 && $carrier['deleted'] == 0)
$id_inactive_carrier = (int) $carrier['id_carrier'];
if ((int) $order->id_carrier == (int) $carrier['id_carrier']){
$carrier_found = (int) $order->id_carrier;
}
}
}
}
if ($carrier_found)
$id_carrier = (int) $carrier_found;
elseif ($id_active_carrier)
$id_carrier = (int) $id_active_carrier;
elseif ($id_inactive_carrier)
$id_carrier = (int) $id_inactive_carrier;
else
$id_carrier = (int) $order->id_carrier;
$order->id_carrier_shipping = (int) $id_carrier;
$order->update();
if (version_compare(_PS_VERSION_,'1.5','<'))
$last_order_state = $order->getCurrentState();
else
$last_order_state = $order->getCurrentOrderState();
// ANTADIS
$is_single = !((bool) Db::getInstance()->getRow('
SELECT *
FROM `'._DB_PREFIX_.'product_ps_cache` c
LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON (d.`product_id` = c.`id_product`)
WHERE d.`id_order` = '.(int) $order->id.'
'));
// PRODUCTS FROM OTHER SALES
if(!$is_single) {
$sent = TRUE;
// GET QUANTITIES - SENT BY LAPOSTE
foreach(Db::getInstance()->ExecuteS('
SELECT d.`id_order_detail`, d.`product_id`, d.`product_attribute_id`, SUM(d.`product_quantity` - GREATEST(d.`product_quantity_return`, d.`product_quantity_refunded`) - IFNULL(l.`quantity`, 0)) AS `quantity`
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'lapostews` l
ON d.`id_order_detail` = l.`id_order_detail`
WHERE d.`id_order` = '.(int) $order->id.'
GROUP BY d.`id_order_detail`
') as $row) {
// GET QUANTITIES - SENT BY LAPOSTE - SENT BY EXAPAQ
$row['quantity'] -= (int) Db::getInstance()->getValue('
SELECT SUM(`quantity`)
FROM `'._DB_PREFIX_.'exapaqws`
WHERE `id_order_detail` = '.(int) $row['id_order_detail'].'
');
// IF QTIES > 0 (NOT ALL SENT) GET IF PRODUCT HAS BEEN SENT
// order has been registered in shipping_history for another shipping methode
if((int) $row['quantity'] > 0) {
if(!Db::getInstance()->getRow('
SELECT c.`id_sale`
FROM `'._DB_PREFIX_.'product_ps_cache` c
LEFT JOIN `'._DB_PREFIX_.'shipping_history` h
ON h.`id_sale` = c.`id_sale`
WHERE c.`id_product` = '.(int) $row['product_id'].'
AND h.`id_order` = '.(int) $order->id.'
')) {
$sent = FALSE;
break;
}
}
}
if(!$sent) {
$new_id_order_state = 17;
}
}
$products_sent = '';
mail('marion@antadis.com', 'philea id op', serialize($data[3]));
if(substr((string) $data[3], 0, 2) == 'OP') {
$id_sale = explode('-', substr((string) $data[3], 2));
if(count($id_sale) < 2) {
mail('marion@antadis.com', '[BBB] Philea erreur', serialize($data));
exit;
}
$id_sale = (int) $id_sale[0];
} else {
$id_sale = 0;
}
foreach(Db::getInstance()->ExecuteS('
SELECT d.`product_name`, d.`product_quantity` - GREATEST(d.`product_quantity_return`, d.`product_quantity_refunded`) AS `quantity`
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c
ON c.`id_product` = d.`product_id`
WHERE c.`id_sale` = '.(int) $id_sale.'
AND d.`id_order` = '.(int) $id_order.'
') as $p) {
if((int) $p['quantity'] > 0) {
$products_sent .= '<br />'."\r\n".$p['quantity'].' x '.$p['product_name'];
}
}
$templateVars = array(
'{product_list}' => !empty($products_sent)? '<strong>Contenu du colis :</strong>'.$products_sent: '',
'{product_list_txt}' => !empty($products_sent)? 'Contenu du colis :'.strip_tags($products_sent): '',
);
if(/*$last_order_state->id*/ $last_order_state != $new_id_order_state && $last_order_state != 5 && ($new_id_order_state != 17 || $last_order_state != 4))
{
$id_order = (int)$order->id;
$new_history = new OrderHistory();
$new_history->id_order = (int)$order->id;
$new_history->changeIdOrderState((int)$new_id_order_state, $id_order);
$new_history->addWithemail(TRUE, $templateVars);
}
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'philea_magistor_parcel`
VALUES (
'.(int) $order->id.',
"'.pSQl(trim($order->shipping_number)).'",
'.(int) $id_sale.',
NOW()
)
');
if ($data[5])
{
// $code_socolissimo = Db::getInstance()->getValue('SELECT delivery_mode FROM `'._DB_PREFIX_.'socolissimo_delivery_info` WHERE id_cart = '.$order->id_cart);
// $key_carrier = $order->id_carrier.':'.($code_socolissimo? $code_socolissimo: '');
// if($code_socolissimo && isset($socol_to_magistor[$code_socolissimo])) {
// $carrier_value = $socol_to_magistor[$code_socolissimo];
// } else {
// if(!isset($tab_conversion_carrier[$key_carrier])) {
// $carrier_value = $socol_to_magistor['DOM'];
// } else {
// $carrier_value = $tab_conversion_carrier[$key_carrier];
// }
// }
// $code_socolissimo = FALSE;
// $code_mr = FALSE;
// $relay_code = FALSE;
// // GET DELIVERY METHOD
// $code_socolissimo = Db::getInstance()->getValue('SELECT delivery_mode FROM `'._DB_PREFIX_.'socolissimo_delivery_info` WHERE id_cart = '.$order->id_cart);
// // Socolissimo delivery method
// if ($code_socolissimo){
// $relay_code = $code_socolissimo;
// }
// elseif (Module::isInstalled('mondialrelay')){
// $code_mr = Db::getInstance()->getValue('
// SELECT mr_m.`dlv_mode`
// FROM `'._DB_PREFIX_.'mr_selected` mr_s
// LEFT JOIN `'._DB_PREFIX_.'mr_method` mr_m
// ON (mr_m.id_mr_method = mr_s.id_method)
// WHERE mr_s.`id_cart` = ' . (int) $order->id_cart
// );
// // Mondial Relay delivery method
// if ($code_mr)
// $relay_code = $code_mr;
// }
// $key_carrier = $order->id_carrier.':'.($relay_code? $relay_code: '');
// if($code_socolissimo && isset($socol_to_magistor[$code_socolissimo])) {
// $carrier_value = $socol_to_magistor[$code_socolissimo];
// } elseif($code_mr && isset($mr_to_magistor[$code_mr])) {
// $carrier_value = $mr_to_magistor[$code_mr];
// } else {
// if(!isset($tab_conversion_carrier[$key_carrier])) {
// $carrier_value = $socol_to_magistor['DOM'];
// } else {
// $carrier_value = $tab_conversion_carrier[$key_carrier];
// }
// }
// if($code_socolissimo && isset($socol_to_magistor[$code_socolissimo])) {
// $carrier_value = $socol_to_magistor[$code_socolissimo];
// } else {
// if(!isset($tab_conversion_carrier[$key_carrier])) {
// $carrier_value = $socol_to_magistor['DOM'];
// } else {
// $carrier_value = $tab_conversion_carrier[$key_carrier];
// }
// }
## GET DELIVERY METHOD
// $carrier = new Carrier((int)($order->id_carrier));
$customer = new Customer((int) $order->id_customer);
$carrier = new Carrier((int) $order->id_carrier_shipping);
if (!Validate::isLoadedObject($customer) OR !Validate::isLoadedObject($carrier))
die(Tools::displayError());
if((int) $order->id_lang == 3) {
if(preg_match('/colissimo/i', $carrier->name)) {
$carrier->url .= '&language=es_ES';
}
$content_html = '<strong>Contenido del paquete:</strong>';
$content_txt = 'Contenido del paquete:';
} else {
$content_html = '<strong>Contenu du colis :</strong>';
$content_txt = 'Contenu du colis :';
}
$templateVars = array(
'{product_list}' => !empty($products_sent)? $content_html.$products_sent: '',
'{product_list_txt}' => !empty($products_sent)? $content_txt.strip_tags($products_sent): '',
'{followup}' => str_replace('@', trim($order->shipping_number), $carrier->url),
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{id_order}' => (int)($order->id)
);
$title = array(
2 => 'Livraison en cours',
3 => 'Paquete en transito',
5 => 'Consegna in corso'
);
Mail::Send((int)($order->id_lang), 'in_transit', $title[(int)$order->id_lang], $templateVars,
$customer->email, $customer->firstname.' '.$customer->lastname, NULL, NULL, NULL, NULL,
_PS_MAIL_DIR_, true);
}
}
}
}
$repo_archive = './archives/IN/LIVRAISON/';
$repo_paths = array(date('Y'), date('m'));
foreach ($repo_paths as $repo_path) {
$repo_archive .= $repo_path . '/';
if (!file_exists($repo_archive))
mkdir($repo_archive);
}
copy($datFile, $repo_archive . preg_replace( '@BAL$@', 'DAT', $fileinfo->getFilename() ));
unlink($inFolder.$fileinfo->getFilename());
unlink($datFile);
}
}
}
}
}
echo 'fin';