2016-09-06 15:32:52 +02:00

358 lines
15 KiB
PHP

<?php
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTP_HOST'] = 'www.bricoprive.com';
//include(dirname(__FILE__) . '/../../../config/settings.inc.php');
include(dirname(__FILE__) . '/../../../config/config.inc.php');
include( dirname(__FILE__) . '/../philea_magistor.php');
ini_set('memory_limit', '4G');
// $id_sale = (int) $argv[1];
$id_sale = 4814;
$_id_shipping = 4; // philea
if($id_sale == 0) {
exit;
}
$magistorModule = new philea_magistor();
$id_lang = Configuration::get('PS_LANG_DEFAULT');
$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',
);
if($magistorModule->active) {
global $regex_file_out;
$regex_file_out = '@^CDC02(.*)\.(BAL|DAT)@';
@set_time_limit(0);
$db = Db::getInstance();
$id_order_state = 2;
/* @Override include orders */
$include_orders = array();
foreach($db->ExecuteS('
SELECT DISTINCT o.`id_order`
FROM `'._DB_PREFIX_.'order_state_current` o
LEFT JOIN `'._DB_PREFIX_.'order_detail` d
ON o.`id_order` = d.`id_order`
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c
ON d.`product_id` = c.`id_product`
LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` s
ON s.`id_sale` = c.`id_sale`
WHERE o.`id_order_state` IN (2, 3, 4, 9, 13, 17)
AND c.`id_sale` = '.(int) $id_sale.'
AND s.`id_shipping` = '.(int) $_id_shipping.'
AND CAST(d.`product_quantity` AS SIGNED) - CAST(d.`product_quantity_refunded` AS SIGNED) > 0
') as $row) {
$include_orders[] = (int) $row['id_order'];
}
if(count($include_orders) == 0) {
exit;
}
$exclude_orders = array();
foreach($db->ExecuteS('
SELECT DISTINCT `id_order`
FROM `'._DB_PREFIX_.'philea_magistor_sent`
WHERE `id_sale` = '.(int) $id_sale.'
') as $row) {
$exclude_orders[] = (int) $row['id_order'];
}
$orders = $db->ExecuteS('
SELECT * FROM `'._DB_PREFIX_.'orders`
WHERE `date_add` > "2015-06-01 00:00:00"
'.(count($exclude_orders) > 0? 'AND `id_order` NOT IN ('.implode(', ', $exclude_orders).')': '').'
'.(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 = (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' . date('ymdHis');
$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']);
$products = $order->getProducts();
$nbProducts = 0;
/*foreach($products as $product)
{
$productObj = new Product($product['product_id']);
if($productObj->send_philea)
$nbProducts++;
unset($productObj);
}*/
// ANTADIS
$products_ids = array();
foreach($products as $product) {
$products_ids[] = (int) $product['product_id'];
}
$nbProducts = (int) Db::getInstance()->getValue('
SELECT COUNT(s.`id_shipping`)
FROM `'._DB_PREFIX_.'product_ps_cache` c
LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` s
ON s.`id_sale` = c.`id_sale`
WHERE c.`id_product` IN ('.implode(', ', $products_ids).')
AND s.`id_shipping` = '. (int) $_id_shipping.'
AND s.`id_sale` = '.(int) $id_sale.'
');
//
if($nbProducts)
{
if(!Db::getInstance()->getValue('
SELECT SUM(`product_quantity`)
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` c
ON c.`id_product` = d.`product_id`
LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` s
ON s.`id_sale` = c.`id_sale`
WHERE d.`id_order` = '.(int) $order->id.'
AND d.`product_quantity` - GREATEST(d.`product_quantity_return`, d.`product_quantity_refunded`) > 0
AND s.`id_shipping` = '.(int) $_id_shipping.'
AND s.`id_sale` = '.(int) $id_sale.'
')) {
continue;
}
$customer = new Customer($order->id_customer);
$address_invoice = new Address($order->id_address_invoice);
$address_delivery = new Address($order->id_address_delivery);
$code_socolissimo = $db->getValue('SELECT delivery_mode FROM `'._DB_PREFIX_.'socolissimo_delivery_info` WHERE id_cart = '.$order->id_cart);
$id_relais = $db->getValue('SELECT prid 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 = 'EXAPAQ_B2B';//$socol_to_magistor['DOM'];
} else {
$carrier_value = $tab_conversion_carrier[$key_carrier];
}
}
if(($carrier_value == 'EXAPAQ_B2B' || $carrier_value == 'EXAPAQ') && (Db::getInstance()->getValue('
SELECT `id_country`
FROM `'._DB_PREFIX_.'address`
WHERE `id_address` = '.(int) $order->id_address_delivery.'
') != 8)) {
$carrier_value = 'EXAPAQ_EXPORT';
}
$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($address_invoice->firstname.' '.$address_invoice->lastname),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($address_invoice->company),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($address_invoice->address1),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($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 );
if($code_socolissimo == 'DOM' OR !$code_socolissimo)
{
$data .= str_pad( substr(utf8_decode($address_delivery->firstname.' '.$address_delivery->lastname),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($address_delivery->company),0,50), 50, ' ', STR_PAD_RIGHT );
}
else
{
$data .= str_pad( substr(utf8_decode($address_delivery->lastname),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($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( (!empty($address_delivery->phone_mobile)?$address_delivery->phone_mobile:$address_delivery->phone), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( '', 50, ' ', STR_PAD_RIGHT );
if(!$code_socolissimo)
{
$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 );
}
else
{
if($code_socolissimo == 'DOM')
{
$data .= str_pad( substr(utf8_decode($address_delivery->firstname.' '.$address_delivery->lastname),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($address_delivery->company),0,50), 50, ' ', STR_PAD_RIGHT );
}
else
{
$data .= str_pad( substr(utf8_decode($address_delivery->lastname),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( substr(utf8_decode($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( (!empty($address_delivery->phone_mobile)?$address_delivery->phone_mobile:$address_delivery->phone), 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( ($id_relais?$id_relais:''), 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') AND Configuration::get('PHILEA_MAGISTOR_ASSURANCE') <= ($order->total_paid_real - $order->total_shipping) AND 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;
foreach($products as $product)
{
$productObj = new Product($product['product_id']);
// ANTADIS
$product_shipping = (int) Db::getInstance()->getValue('
SELECT s.`id_shipping`
FROM `'._DB_PREFIX_.'product_ps_cache` c
LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` s
ON s.`id_sale` = c.`id_sale`
WHERE c.`id_product` = '.(int) $productObj->id.'
AND s.`id_sale` = '.(int) $id_sale.'
');
//
if($product_shipping === $_id_shipping)
{
$nb_ligne++;
if($code_societe == 78) {
$ref = !empty($product['product_ean13'])? $product['product_ean13']: $product['product_supplier_reference'];
} else {
$ref = ($product['product_attribute_id']?$product['product_id'].'_'.$product['product_attribute_id']:$product['product_id']);
}
$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( $ref, 50, ' ', STR_PAD_RIGHT ); //$product['product_supplier_reference'], 50, ' ', STR_PAD_RIGHT );
$data .= str_pad( max($product['product_quantity'] - max($product['product_quantity_return'], $product['product_quantity_refunded']), 0), 8, '0', STR_PAD_LEFT );
$data .= str_pad( substr(utf8_decode(cleanChar($product['product_name'])),0,50), 50, ' ', STR_PAD_RIGHT );
$data .= PHP_EOL;
}
else
$partialOrder = true;
}
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'philea_magistor_sent`
VALUES (
'.(int) $order->id.',
'.(int) $id_sale.',
NOW()
)
');
// ANTADIS
/*if(isset($partialOrder) AND $partialOrder)
{
$new_history = new OrderHistory();
$new_history->id_order = (int)$order->id;
// 17 --> Status of partial order
$new_history->changeIdOrderState(17, $order->id);
$new_history->addWithemail();
unset($partialOrder);
}*/
} //End if nbproducts
}//End foreach
if($orders and is_array($orders))
{
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)
{
$string = str_replace(chr(13),' ',$string);
$string = str_replace(chr(10),' ',$string);
$string = str_replace(chr(13).chr(10),' ',$string);
$string = str_replace(chr(10).chr(13),' ',$string);
return $string;
}