Include file from server

This commit is contained in:
Michael RICOIS 2018-03-13 12:02:24 +01:00
parent 37b955667f
commit e54395e5d1
2 changed files with 1031 additions and 0 deletions

View File

@ -0,0 +1,456 @@
<?php
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
include(dirname(__FILE__) . '/../../../config/config.inc.php');
include( dirname(__FILE__) . '/../philea_magistor.php');
ini_set('memory_limit', '4G');
// Vairable à changer en fonction du besoin
$id_sale = 8340;
$include_orders = array(731011,729025,725538);
$product_restrictions = array(5238967,5238975,5238962);
$_id_shipping = 1; // philea
if($id_sale == 0) {
exit;
}
$dateNow = date('ymdHis');
$multi = array();
$magistorModule = new philea_magistor();
$id_lang = Configuration::get('PS_LANG_DEFAULT');
$tab_conversion_carrier = philea_magistor::getTabState();
$socol_to_magistor = array(
'DOMINTER' => 'COLINT', // Livraison internationnale à domicile
'DOM' => 'SOCOLMDSS', // Livraison à domicile
'DOS' => 'SOCOLMDS',
'RDV' => 'SOCOLMRDV', // Livraison sur Rendez-vous
'CIT' => 'SOCOLCITY', // Livraison en Cityssimo
'BPR' => 'SOCOLMBP', // Livraison en Bureau de Poste
'CDI' => 'SOCOLMBP', // Centre de distribution
'A2P' => 'SOCOLMC', // Livraison Commerce de proximité
'ACP' => 'SOCOLMBP', // Agence ColiPoste
);
$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é
);
if($magistorModule->active) {
global $regex_file_out;
$regex_file_out = '@^CDC02(.*)\.(BAL|DAT)@';
@set_time_limit(0);
$db = Db::getInstance();
$id_order_state = 2;
$orders = $db->ExecuteS('
SELECT * FROM `'._DB_PREFIX_.'orders`
WHERE `date_add` > "2017-10-01 00:00:00"
'.(count($include_orders) > 0? 'AND `id_order` IN ('.implode(', ', $include_orders).')': '').'
');
if(count($orders) == 0) {
exit;
}
if((int) Db::getInstance()->getValue('
SELECT `featured`
FROM `'._DB_PREFIX_.'privatesale`
WHERE `id_sale` = '.(int) $id_sale.'
')) {
$code_societe = 78;//(int)(Configuration::get('PHILEA_MAGISTOR_CODE_STE'));
} else {
$code_societe = (int)(Configuration::get('PHILEA_MAGISTOR_CODE_STE'));
}
$delai_livraison = 2; //(int)(Configuration::get('PHILEA_MAGISTOR_DELAI_LIVRAISON'));
//@TODO rendre configurable le champs "reference" entre "reference" et "ean13"
$referenceField = 'reference';//Configuration::get('PHILEA_MAGISTOR_REF_FIELD');
$fileName = dirname(__FILE__) . '/OUT/CDC02' . $dateNow;
$repo_archive = dirname(__FILE__) . '/archives/OUT/CMD/';
$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);
}
$fileArchive = $repo_archive . 'CDC02' . date('ymdHis');
$data = '';
foreach($orders as $o) {
$partialOrder = false;
$order = new Order($o['id_order']);
$sales = array();
$sales[(int)$id_sale] = (int) $id_sale;
// ANTADIS - check if multi and order already sent
// $other_sales = Db::getInstance()->getValue('
// SELECT COUNT(`id_sale`)
// FROM `'._DB_PREFIX_.'philea_sent`
// WHERE `id_order` = '.(int) $o['id_order'].'
// ');
// if($other_sales > 0){
// // order already sent to philea
// continue;
// }
$products = $order->getProducts();
// ANTADIS
$products_ids = array();
foreach($products as $product) {
$products_ids[] = (int) $product['product_id'];
}
$nbProducts = 0;
$nbProducts = (int) Db::getInstance()->getValue('
SELECT COUNT(d.`id_order_detail`)
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c ON d.`product_id` = c.`id_product`
WHERE c.`id_sale` = '.(int) $id_sale.'
AND d.`id_order` = '.(int) $o['id_order'].'
AND d.`product_quantity` - d.`product_quantity_refunded` > 0
');
$prid = false;
$delivery_info = false;
$carrier_value = false;
if($nbProducts) {
$carriers_socol = array(67,87,88,89);
$carriers_mr = array(91);
$customer = new Customer($order->id_customer);
$address_invoice = new Address($order->id_address_invoice);
$address_delivery = new Address($order->id_address_delivery);
if(in_array((int) $order->id_carrier, $carriers_socol)) {
$delivery_info = $db->getRow('
SELECT *
FROM `'._DB_PREFIX_.'socolissimo_delivery_info`
WHERE `id_cart` = '.(int) $order->id_cart.'
AND `delivery_mode` != ""
');
/* SOCOL */
if($delivery_info) {
if($delivery_info['cecountry'] != 'FR') {
$carrier_value = $socol_to_magistor['DOMINTER'];
} else {
$carrier_value = $socol_to_magistor[$delivery_info['delivery_mode']];
}
$prid = $delivery_info['prid'];
}
} elseif(in_array((int) $order->id_carrier, $carriers_mr)) {
/* MONDIAL RELAY */
$delivery_info = $db->getRow('
SELECT mr_m.`col_mode`, mr_m.`dlv_mode`, mr_s.*
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.'
AND mr_m.`id_carrier` = '.(int) $order->id_carrier.'
AND `MR_Selected_Num` IS NOT NULL
');
if($delivery_info) {
$carrier_value = $mr_to_magistor[$delivery_info['dlv_mode']];
$prid = substr($delivery_info['MR_Selected_Num'], -5);
}
}
if(!$carrier_value) {
if((int)$address_delivery->id_country == 8) {
$carrier_value = $tab_conversion_carrier[$order->id_carrier.':'];
} else {
$carrier_value = $socol_to_magistor['DOMINTER'];
}
}
## ASSIGN DATA
$data .= str_pad( 'E01', 10, ' ', STR_PAD_RIGHT );
$data .= str_pad( $code_societe, 20, ' ', STR_PAD_RIGHT );
$data .= str_pad( 'OP'.(int) $id_sale.'-'.$order->id, 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( $order->id_customer, 8, ' ', STR_PAD_RIGHT );
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( date('Ymd',strtotime($order->date_add)+86400*$delai_livraison), 8, '0', STR_PAD_LEFT );
$data .= str_pad( '', 4, '0', STR_PAD_LEFT );
$data .= str_pad( $carrier_value , 50, ' ', STR_PAD_RIGHT );//$db->getValue('SELECT name FROM `'._DB_PREFIX_.'carrier` WHERE id_carrier = '.$order->id_carrier)
$data .= str_pad( substr(utf8_decode(cleanChar($address_invoice->firstname.' '.$address_invoice->lastname)),0,50), 50, ' ', STR_PAD_RIGHT );
if(in_array((int) $order->id_carrier, $carriers_mr)
&& $delivery_info
&& !in_array($delivery_info['dlv_mode'], array('LD1', 'LDS', 'HOM'))
) {
// no company name for a delivery in relay point
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT );
} else {
$data .= str_pad( substr(utf8_decode(cleanChar($address_invoice->company)),0,50), 50, ' ', STR_PAD_RIGHT );
}
$data .= str_pad( substr(utf8_decode(cleanChar($address_invoice->address1)),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode(cleanChar($address_invoice->address2)),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode(cleanChar($address_invoice->other)),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($address_invoice->postcode),0,8), 8, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($address_invoice->city),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( utf8_decode($db->getValue('SELECT iso_code FROM `'._DB_PREFIX_.'country` WHERE id_country = '.$address_invoice->id_country)), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( (!empty($address_invoice->phone_mobile)?$address_invoice->phone_mobile:$address_invoice->phone), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($customer->email), 0, 50), 50, ' ', STR_PAD_RIGHT );
// LIVRAISON DOMICILE SOCOL
if(in_array((int) $order->id_carrier, $carriers_socol)
&& $delivery_info
&& $delivery_info['delivery_mode'] == 'DOM'
) {
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode(cleanChar($address_invoice->firstname.' '.$address_invoice->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
}
// LIVRAISON DOMICILE MONDIAL RELAY
elseif(in_array((int) $order->id_carrier, $carriers_mr)
&& $delivery_info
&& in_array($delivery_info['dlv_mode'], array('LD1', 'LDS', 'HOM'))
) {
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode(cleanChar($address_invoice->firstname.' '.$address_invoice->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
}
// LIVRAISON MR (NO DOMICILE)
elseif(in_array((int) $order->id_carrier, $carriers_mr)
&& $delivery_info
&& !in_array($delivery_info['dlv_mode'], array('LD1', 'LDS', 'HOM'))
) {
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->firstname.' '.$address_delivery->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
// no company name for a delivery in relay point
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT );
}
else {
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->firstname.' '.$address_delivery->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->company)), 0, 50), 50, ' ', STR_PAD_RIGHT);
}
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->address1)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->address2)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->other)),0, 50), 50, ' ', STR_PAD_RIGHT); //ADRESSE 3
$data .= str_pad(substr(utf8_decode($address_delivery->postcode), 0, 8), 8, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode($address_delivery->city), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(utf8_decode($db->getValue('SELECT iso_code FROM `'._DB_PREFIX_.'country` WHERE id_country = '.$address_delivery->id_country)), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad((!empty($address_delivery->phone_mobile)?$address_delivery->phone_mobile:$address_delivery->phone), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT );
// LIVRAISON SOCOL
if(in_array((int) $order->id_carrier, $carriers_socol) && $delivery_info) {
if($delivery_info['delivery_mode'] == 'DOM') {
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->firstname.' '.$address_delivery->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->company)), 0, 50), 50, ' ', STR_PAD_RIGHT);
} else {
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode(cleanChar($address_invoice->firstname.' '.$address_invoice->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
}
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->address1)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->address2)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode(cleanChar($address_delivery->other)), 0, 50), 50, ' ', STR_PAD_RIGHT); //ADRESSE 3
$data .= str_pad(substr(utf8_decode($address_delivery->postcode), 0, 8), 8, ' ', STR_PAD_RIGHT);
$data .= str_pad(substr(utf8_decode($address_delivery->city), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad(utf8_decode($db->getValue('SELECT `iso_code` FROM `'._DB_PREFIX_.'country` WHERE `id_country` = '.$address_delivery->id_country)), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad((isset($address_delivery->phone_mobile)?$address_delivery->phone_mobile:$address_delivery->phone), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad('', 50, ' ', STR_PAD_RIGHT);
}
// LIVRAISON MONDIAL RELAY
elseif (in_array((int) $order->id_carrier, $carriers_mr) && $delivery_info) {
// MR DOMICILE
if (in_array($delivery_info['dlv_mode'], array('LD1', 'LDS', 'HOM'))){
$data .= str_pad( substr(utf8_decode(cleanChar($address_delivery->firstname.' '.$address_delivery->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad( substr(utf8_decode(cleanChar($address_delivery->company)), 0, 50), 50, ' ', STR_PAD_RIGHT);
} else {
$data .= str_pad( substr(utf8_decode(cleanChar($address_delivery->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
$data .= str_pad( substr(utf8_decode(cleanChar($address_invoice->firstname.' '.$address_invoice->lastname)), 0, 50), 50, ' ', STR_PAD_RIGHT);
}
$data .= str_pad( substr(utf8_decode($address_delivery->address1),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($address_delivery->address2),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode(cleanChar($address_delivery->other)),0,50), 50, ' ', STR_PAD_RIGHT );//ADRESSE 3
$data .= str_pad( substr(utf8_decode($address_delivery->postcode),0,8), 8, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($address_delivery->city),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( utf8_decode($db->getValue('SELECT iso_code FROM `'._DB_PREFIX_.'country` WHERE id_country = '.$address_delivery->id_country)), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( (isset($address_delivery->phone_mobile)?$address_delivery->phone_mobile:$address_delivery->phone), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT );
}
// LIVRAISON DOMICILE
else{
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT);
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT);
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT);
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT);
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT);//ADRESSE 3
$data .= str_pad( '', 8, ' ', STR_PAD_RIGHT);
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT);
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT);
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT);
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT);
}
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_1
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_2
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_3
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_4
$data .= str_pad($prid? $prid: '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_5
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_6
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_7
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT); // INFO_RELAIS_8
if(Configuration::get('PHILEA_MAGISTOR_ASSURANCE')
&& Configuration::get('PHILEA_MAGISTOR_ASSURANCE') <= ($order->total_paid_real - $order->total_shipping)
&& Configuration::get('PHILEA_MAGISTOR_ASSURANCE') > 0
) {
$data .= str_pad( str_replace('.',',',($order->total_paid_real - $order->total_shipping)), 50, ' ', STR_PAD_RIGHT ); // VALEUR DE COMMANDE
} else {
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT ); // VALEUR DE COMMANDE
}
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT );// ZONE_10
$data .= str_pad(utf8_decode(cleanChar($address_delivery->other)), 400, ' ', STR_PAD_RIGHT );
$data .= PHP_EOL;
$products = $order->getProducts();
$nb_ligne = 0;
$product_packs = array();
$product_rows = array();
foreach($products as $product)
{
if(!empty($product_restrictions) && !in_array($product['product_id'],$product_restrictions) ){
continue;
}
if(!Db::getInstance()->getRow('
SELECT `id_product`
FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_sale` = '.(int) $id_sale.'
AND `id_product` = '.(int) $product['product_id'].'
')) {
$other_sale_id = Db::getInstance()->getValue('
SELECT `id_sale`
FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_product` = '.(int) $product['product_id'].'
');
$sales[(int)$other_sale_id] = (int)$other_sale_id;
}
/**
* @Override get pack items
*/
if(Pack::isPack((int)$product['product_id'])) {
$pack_items = Pack::getCompleteSimplePack((int) $product['product_id']);
foreach ($pack_items as $pack_item) {
$ean = !empty($pack_item['ean13'])? $pack_item['ean13']: $pack_item['supplier_reference'];
$ref = $pack_item['id_product'];
$p_key = $pack_item['id_product'];
if (!isset($product_rows[$p_key])) {
$product_rows[$p_key] = array(
str_replace(array("\r", "\n"), '', $ref),
0,
substr(utf8_decode(cleanChar($pack_item['name'])),0,50),
substr(str_replace(array("\r", "\n"), "", $ean),0,14)
);
}
$product_rows[$p_key][1] += (max($product['product_quantity'] - max($product['product_quantity_return'], $product['product_quantity_refunded']), 0) * $pack_item['pack_quantity']);
}
} else {
$ean = !empty($product['product_ean13'])? $product['product_ean13']: $product['product_supplier_reference'];
$ref = ($product['product_attribute_id']?$product['product_id'].'_'.$product['product_attribute_id']:$product['product_id']);
$p_key = ($product['product_attribute_id']?$product['product_id'].'_'.$product['product_attribute_id']:$product['product_id']);
if (!isset($product_rows[$p_key])) {
$product_rows[$p_key] = array(
str_replace(array("\r", "\n"), '', $ref),
0,
substr(utf8_decode(cleanChar($product['product_name'])),0,50),
substr(str_replace(array("\r", "\n"), "", $ean),0,14),
);
}
$product_rows[$p_key][1] += max($product['product_quantity'] - max($product['product_quantity_return'], $product['product_quantity_refunded']), 0);
}
}
// Add products to data
$nb_ligne = 0;
foreach ($product_rows as $p_key => $product) {
$nb_ligne++;
$data .= str_pad('L01', 10, ' ', STR_PAD_RIGHT);
$data .= str_pad('OP'.(int) $id_sale.'-'.$order->id, 50, ' ', STR_PAD_RIGHT);
$data .= str_pad($nb_ligne, 4, '0', STR_PAD_LEFT);
$data .= str_pad($product[0], 50, ' ', STR_PAD_RIGHT); // ref
$data .= str_pad($product[1], 8, '0', STR_PAD_LEFT); // quantity
$data .= str_pad($product[2], 50, ' ', STR_PAD_RIGHT); // product name
$data .= str_pad($product[3], 14, ' ', STR_PAD_LEFT); // ean
$data .= PHP_EOL;
}
foreach ($sales as $key => $sale_id) {
$already_existing = Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'philea_sent` WHERE `id_order` = '.(int)$order->id.' AND `id_sale` = '.(int)$sale_id);
if($already_existing) {
Db::getInstance()->ExecuteS('
UPDATE INTO `'._DB_PREFIX_.'philea_sent`
SET `date_add` = NOW()
WHERE `id_order` = '.(int) $order->id.' AND `id_sale` = '.(int) $sale_id.'
');
} else {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'philea_sent`
VALUES (
'.(int) $order->id.',
'.(int) $sale_id.',
NOW()
)
');
}
}
// if(count($sales)>1) {
// $multi[] = (int) $order->id;
// }
} //End if nbproducts
}//End foreach
if($orders and is_array($orders))
{
$file = 'CDC02'.$dateNow.'.DAT';
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'philea_syncreport` (`id_sale`, `filename`, `date_add`)
VALUES (
'.(int)$id_sale.',
"'.pSQL($file).'",
NOW()
)
');
file_put_contents($fileName . '.DAT', "\xEF\xBB\xBF".utf8_encode($data));
file_put_contents($fileName . '.BAL', '');
file_put_contents($fileArchive . '.DAT', "\xEF\xBB\xBF".utf8_encode($data));
require_once('connection_ftp.php');
}
}
function cleanChar($string) {
$unicode_0 = json_decode('"\u0006"');
$unicode_1 = json_decode('"\u200b"');
$string = str_replace("\r\n",' ',$string);
$string = str_replace("\r",' ',$string);
$string = str_replace("\r",' ',$string);
$string = str_replace("\n",' ',$string);
$string = str_replace("\n",' ',$string);
$string = str_replace(array('®', '©', '™'), array('', '' ,''), $string);
$string = str_replace(
array('"', 'ľ', '&deg;', '&ordm;', '&ordf;', 'Á', '°', 'º', 'º', 'º', 'ª', "\t", "\n", "\r", ' ', '', '', $unicode_0, $unicode_1, '«', '»',),
array('', 'l\'', '.', '.', '.', 'A', '', '', '', '', 'a', ' ', ' ', ' ', ' ', '-', '', '', '', '', '',),
$string
);
return str_replace(array('&deg;', '&ordm;', '&ordf;', '&', ';'), '', $string);
}

View File

@ -0,0 +1,575 @@
<?php
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
include ('../../../config/settings.inc.php');
include ('../../../config/config.inc.php');
// Commande en état indéterminé pour ne pas faire n'importe quoi
$unknownO = array();
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),
'CODE_ART' => array(297, 50),
'QTE' => array(347, 10)
);
$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_TEST/';
$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);
$id_order_details = array();
$shipping_numbers = array();
$parcel_carrier = array();
if (! empty($lines)) {
$details = array();
$order_details = array();
// GET DATA
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;
}
// Test
if ($id_sale == 0 || $id_sale != 9007) {
$unknownO[] = $id_order;
}
if (! isset($details[(int) $id_order])) {
$details[(int) $id_order] = array();
}
$code_art = explode("_", trim($data[9]));
if (count($code_art) > 1) {
list ($id_product, $id_attribute) = $code_art;
} else {
$id_product = $code_art[0];
$id_attribute = 0;
}
$order = new Order((int) $id_order);
// GET CARRIER ID
$carrier_found = false;
$id_active_carrier = false;
$id_inactive_carrier = false;
if (isset($data[8]) && $data[8]) {
$carrier_name = trim($data[8]);
if ($carrier_name == 'COLINT') {
$carrier_name = 'SOCOLMDSS';
}
$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;
if (isset($id_carrier) && $id_carrier) {
$parcel_carrier[(int) $id_order . '|' . trim($data[5])] = $id_carrier;
}
$shipping_numbers[] = '"' . pSQL(trim($data[5])) . '"';
if (! isset($details[(int) $id_order][$id_product . '_' . $id_attribute])) {
$details[(int) $id_order][$id_product . '_' . $id_attribute] = array(
'shipping_numbers' => array(),
'quantity' => 0
);
}
$details[(int) $id_order][$id_product . '_' . $id_attribute]['quantity'] += (int) trim($data[10]);
$details[(int) $id_order][$id_product . '_' . $id_attribute]['shipping_numbers'][] = array(
'number' => trim($data[5]),
'date' => pSQL(substr($data[7], 0, 4) . '-' . substr($data[7], 4, 2) . '-' . substr($data[7], 6, 2) . ' 18:00:00'),
'quantity' => (int) trim($data[10])
);
if (! isset($order_details[(int) $id_order])) {
$order_details[(int) $id_order] = array();
foreach (Db::getInstance()->ExecuteS('
SELECT DISTINCT `id_order_detail`, `product_id`, `product_attribute_id`, `product_quantity` - GREATEST(`product_quantity_return`, `product_quantity_refunded`) AS `quantity`
FROM `' . _DB_PREFIX_ . 'order_detail`
WHERE `id_order` = ' . (int) $id_order . '
') as $key => $row) {
$sent = (int) Db::getInstance()->getValue('
SELECT SUM(`quantity`)
FROM `' . _DB_PREFIX_ . 'philea_parcel`
WHERE `id_order_detail` = ' . (int) $row['id_order_detail'] . '
');
$id_order_details[] = (int) $row['id_order_detail'];
if (Pack::isPack((int) $row['product_id'])) {
$items = Pack::getSimplePack($row['product_id']);
foreach ($items as $item) {
if (! isset($order_details[(int) $id_order][$item['id_product_item'] . '_0'])) {
$order_details[(int) $id_order][$item['id_product_item'] . '_0'] = array(
'id_order_details' => array(),
'quantity_to_sent' => 0
);
}
$order_details[$id_order][$item['id_product_item'] . '_0']['quantity_to_sent'] += (($row['quantity'] - $sent) * $item['quantity']);
$order_details[$id_order][$item['id_product_item'] . '_0']['id_order_details'][$row['id_order_detail']] = array(
'detail_quantity' => ($row['quantity'] - $sent),
'id_order_detail' => $row['id_order_detail']
);
}
} else {
if (! isset($order_details[(int) $id_order][$row['product_id'] . '_' . $row['product_attribute_id']])) {
$order_details[(int) $id_order][$row['product_id'] . '_' . $row['product_attribute_id']] = array(
'id_order_details' => array(),
'quantity_to_sent' => 0
);
}
$order_details[(int) $id_order][$row['product_id'] . '_' . $row['product_attribute_id']]['quantity_to_sent'] += ($row['quantity'] - $sent);
$order_details[(int) $id_order][$row['product_id'] . '_' . $row['product_attribute_id']]['id_order_details'][$row['id_order_detail']] = array(
'detail_quantity' => ($row['quantity'] - $sent),
'id_order_detail' => $row['id_order_detail']
);
}
}
}
} // end foreach lines
foreach ($order_details as $order => $order_detail) {
foreach ($order_detail as $key => $product) {
if (isset($details[(int) $order][$key])) {
if (in_array($order, $unknownO)) {
if (! isset($badOrder)) {
$badOrder = 0;
}
if ($details[(int) $order][$key]['quantity'] > $product['quantity_to_sent']) {
$badOrder ++;
echo $order . ' - ' . $badOrder . ' : ' . $details[(int) $order][$key]['quantity'] . ' <= ' . $product['quantity_to_sent'] . "\n";
// $details[(int)$order][$key]['quantity'] = $product['quantity_to_sent'];
print_r($details[(int) $order][$key]['shipping_numbers']);
}
// Traitement commande non envoyée
if (count($details[(int) $order][$key]['shipping_numbers']) > 1) {
foreach ($details[(int) $order][$key]['shipping_numbers'] as $shipping_number) {
foreach ($product['id_order_details'] as $id_order_detail => $row) {
Db::getInstance()->ExecuteS('
INSERT INTO `' . _DB_PREFIX_ . 'philea_parcel`
VALUES (
' . (int) $id_order_detail . ',
' . (int) $row['detail_quantity'] . ',
"' . pSQL(trim($shipping_number['number'])) . '",
"' . $shipping_number['date'] . '"
)
');
}
}
} else {
foreach ($product['id_order_details'] as $id_order_detail => $row) {
Db::getInstance()->ExecuteS('
INSERT INTO `' . _DB_PREFIX_ . 'philea_parcel`
VALUES (
' . (int) $id_order_detail . ',
' . (int) $row['detail_quantity'] . ',
"' . pSQL($details[(int) $order][$key]['shipping_numbers'][0]['number']) . '",
"' . $details[(int) $order][$key]['shipping_numbers'][0]['date'] . '"
)
');
}
}
}
// Partially sent
if ($details[(int) $order][$key]['quantity'] <= $product['quantity_to_sent']) {
if (count($details[(int) $order][$key]['shipping_numbers']) > 1) {
foreach ($details[(int) $order][$key]['shipping_numbers'] as $shipping_number) {
foreach ($product['id_order_details'] as $id_order_detail => $row) {
Db::getInstance()->ExecuteS('
INSERT INTO `' . _DB_PREFIX_ . 'philea_parcel`
VALUES (
' . (int) $id_order_detail . ',
' . (int) $row['detail_quantity'] . ',
"' . pSQL(trim($shipping_number['number'])) . '",
"' . $shipping_number['date'] . '"
)
');
}
}
} else {
foreach ($product['id_order_details'] as $id_order_detail => $row) {
Db::getInstance()->ExecuteS('
INSERT INTO `' . _DB_PREFIX_ . 'philea_parcel`
VALUES (
' . (int) $id_order_detail . ',
' . (int) $row['detail_quantity'] . ',
"' . pSQL($details[(int) $order][$key]['shipping_numbers'][0]['number']) . '",
"' . $details[(int) $order][$key]['shipping_numbers'][0]['date'] . '"
)
');
}
}
} else {
// $message = "Commande : ".$order."\r\n Produit : ".$key."\r\n Quantity to sent : ".$product['quantity_to_sent']."\r\n Quantity : ".$details[(int)$order][$key]['quantity'];
// mail('marion@antadis.com', '[BBB] Erreurs quantity expe Philea', $message);
}
} else {
// $message = "Commande : ".$order."\r\n Produit : ".$key."\r\n Produit non présent dans le fichier expe philea";
// mail('marion@antadis.com', '[BBB] Erreurs produit expe Philea', $message);
}
}
}
unset($details);
unset($order_details);
} // end if lines
$id_order_details = array_unique($id_order_details);
$shipping_numbers = array_unique($shipping_numbers);
if (count($id_order_details) > 0) {
$id_orders = array();
$orders_infos = array();
foreach (Db::getInstance()->ExecuteS('
SELECT d.`id_order`, p.`shipping_number`, p.`date_add`
FROM `' . _DB_PREFIX_ . 'philea_parcel` p
LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` d ON (p.`id_order_detail` = d.`id_order_detail`)
WHERE d.`id_order_detail` IN (' . implode(', ', $id_order_details) . ')
AND p.`shipping_number` IN (' . implode(', ', $shipping_numbers) . ')
GROUP BY d.`id_order`, p.`shipping_number`
') as $row) {
Db::getInstance()->ExecuteS('
INSERT INTO `' . _DB_PREFIX_ . 'shipping_history`
VALUES (
' . (int) $row['id_order'] . ',
"' . pSQL($row['shipping_number']) . '",
"' . pSQL($row['date_add']) . '",
' . (int) $parcel_carrier[(int) $row['id_order'] . '|' . $row['shipping_number']] . ',
0
)
');
$id_orders[] = (int) $row['id_order'];
if (! isset($orders_infos[(int) $row['id_order']])) {
$orders_infos[(int) $row['id_order']] = array();
}
$orders_infos[(int) $row['id_order']][] = array(
$row['shipping_number'],
$row['date_add'],
(int) $parcel_carrier[(int) $row['id_order'] . '|' . $row['shipping_number']]
);
}
$id_orders = array_unique($id_orders);
$products = Db::getInstance()->ExecuteS('
SELECT d.`id_order_detail`, d.`id_order`, c.`id_sale`, d.`product_quantity` - GREATEST(d.`product_quantity_refunded`, d.`product_quantity_return`) AS `quantity`
FROM ps_order_detail d
LEFT JOIN `' . _DB_PREFIX_ . 'product_ps_cache` c ON (d.`product_id` = c.`id_product`)
WHERE d.`id_order` IN (' . implode(', ', $id_orders) . ')
');
$sent_sales = array();
foreach (Db::getInstance()->ExecuteS('
SELECT `id_order`, `id_sale`
FROM `' . _DB_PREFIX_ . 'shipping_history`
WHERE `id_order` IN (' . implode(', ', $id_orders) . ')
AND `id_sale` != 0
GROUP BY `id_order`, `id_sale`
') as $row) {
if (! isset($sent_sales[(int) $row['id_order']])) {
$sent_sales[(int) $row['id_order']] = array();
}
$sent_sales[(int) $row['id_order']][] = (int) $row['id_sale'];
}
$to_send = array();
foreach ($products as $product) {
if ((int) $product['quantity'] > 0 && (! isset($sent_sales[(int) $product['id_order']]) || ! in_array((int) $product['id_sale'], $sent_sales[(int) $product['id_order']]))) {
$to_send[] = $product;
}
}
unset($products);
$sent_products = array();
foreach (Db::getInstance()->ExecuteS('
SELECT l.`id_order_detail`, SUM(l.`quantity`) AS `quantity`
FROM `' . _DB_PREFIX_ . 'lapostews` l
LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` d ON (l.`id_order_detail` = d.`id_order_detail`)
WHERE d.`id_order` IN (' . implode(', ', $id_orders) . ')
GROUP BY l.`id_order_detail`
') as $row) {
$sent_products[(int) $row['id_order_detail']] = (int) $row['quantity'];
}
foreach (Db::getInstance()->ExecuteS('
SELECT l.`id_order_detail`, SUM(l.`quantity`) AS `quantity`
FROM `' . _DB_PREFIX_ . 'mondialrelay_parcel` l
LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` d ON (l.`id_order_detail` = d.`id_order_detail`)
WHERE d.`id_order` IN (' . implode(', ', $id_orders) . ')
GROUP BY l.`id_order_detail`
') as $row) {
$sent_products[(int) $row['id_order_detail']] = (int) $row['quantity'];
}
foreach (Db::getInstance()->ExecuteS('
SELECT l.`id_order_detail`, SUM(l.`quantity`) AS `quantity`
FROM `' . _DB_PREFIX_ . 'philea_parcel` l
LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` d ON (l.`id_order_detail` = d.`id_order_detail`)
WHERE d.`id_order` IN (' . implode(', ', $id_orders) . ')
GROUP BY l.`id_order_detail`
') as $row) {
$sent_products[(int) $row['id_order_detail']] = (int) $row['quantity'];
}
foreach (Db::getInstance()->ExecuteS('
SELECT l.`id_order_detail`, SUM(l.`quantity`) AS `quantity`
FROM `' . _DB_PREFIX_ . 'ant_dropshipping_parcel` l
LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` d ON (l.`id_order_detail` = d.`id_order_detail`)
WHERE d.`id_order` IN (' . implode(', ', $id_orders) . ')
GROUP BY l.`id_order_detail`
') as $row) {
$sent_products[(int) $row['id_order_detail']] = (int) $row['quantity'];
}
$orders_to_send = array();
$orders_list = array();
$total = count($to_send);
foreach ($to_send as $product) {
if (! in_array((int) $product['id_order'], $orders_list)) {
$orders_list[] = (int) $product['id_order'];
}
if (! in_array((int) $product['id_order'], $orders_to_send)) {
if (! isset($sent_products[(int) $product['id_order_detail']]) || $product['quantity'] > $sent_products[(int) $product['id_order_detail']]) {
$orders_to_send[] = (int) $product['id_order'];
}
}
}
unset($sent_products);
unset($to_send);
$orders_sent = array();
foreach ($orders_list as $order) {
if (! in_array($order, $orders_to_send)) {
$orders_sent[] = $order;
}
}
unset($orders_list);
$status_sent = array();
$status_partially_sent = array();
foreach ($orders_sent as $order) {
if ((int) Db::getInstance()->getValue('
SELECT `id_order_state`
FROM `' . _DB_PREFIX_ . 'order_history`
WHERE `id_order` = ' . (int) $order . '
ORDER BY `date_add` DESC LIMIT 1
') != 4) {
$status_sent[] = (int) $order;
}
}
foreach ($id_orders as $order) {
if (! in_array($order, $status_sent)) {
if (! in_array((int) Db::getInstance()->getValue('
SELECT `id_order_state`
FROM `' . _DB_PREFIX_ . 'order_history`
WHERE `id_order` = ' . (int) $order . '
ORDER BY `date_add` DESC LIMIT 1
'), array(4, 17, 9, 19))) {
$status_partially_sent[] = (int) $order;
}
}
}
unset($orders_sent);
unset($orders_to_send);
foreach ($orders_infos as $id_order => $parcels) {
$order = new Order($id_order);
$customer = new Customer((int) $order->id_customer);
foreach ($parcels as $parcel) {
$products_sent = '';
$products_names = array();
foreach (Db::getInstance()->ExecuteS('
SELECT d.`id_order_detail`, d.`product_name`, SUM(p.`quantity`) AS `quantity`
FROM `' . _DB_PREFIX_ . 'philea_parcel` p
LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` d ON (p.`id_order_detail` = d.`id_order_detail`)
WHERE d.`id_order` = ' . (int) $id_order . '
AND p.`shipping_number` = "' . pSQL($parcel[0]) . '"
AND p.`date_add` = "' . pSQL($parcel[1]) . '"
GROUP BY d.`id_order_detail`
') as $p) {
$products_sent .= '<br />' . "\r\n" . $p['quantity'] . ' x ' . $p['product_name'];
}
$carrier = new Carrier($parcel[2], (int) $order->id_lang);
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(
'{followup}' => str_replace('@', $parcel[0], $carrier->url),
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{id_order}' => (int) $order->id,
'{product_list}' => ! empty($products_sent) ? $content_html . $products_sent : '',
'{product_list_txt}' => ! empty($products_sent) ? $content_txt . strip_tags($products_sent) : ''
);
$order->shipping_number = $parcel[0];
$order->update();
$last_state = Db::getInstance()->getValue('
SELECT `id_order_state`
FROM `' . _DB_PREFIX_ . 'order_state_current`
WHERE `id_order` =' . (int) $order->id);
if (in_array($order->id, $status_sent) && (int) $last_state != 4) {
$history = new OrderHistory();
$history->id_order = (int) $order->id;
$history->changeIdOrderState(4, (int) $order->id);
$history->addWithemail(true, $templateVars);
} elseif (in_array($order->id, $status_partially_sent)) {
$history = new OrderHistory();
$history->id_order = (int) $order->id;
$history->changeIdOrderState(17, (int) $order->id);
$history->addWithemail(true, $templateVars);
}
$subjects = array(
1 => 'Package in transit',
2 => 'Livraison en cours',
3 => 'Pedido en tránsito'
);
Mail::Send(intval($order->id_lang), 'in_transit', $subjects[(int) $order->id_lang], $templateVars, $customer->email, $customer->firstname . ' ' . $customer->lastname);
}
}
}
$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';