287 lines
9.5 KiB
PHP
Executable File
287 lines
9.5 KiB
PHP
Executable File
<?php
|
|
if(isset($_SERVER['REMOTE_ADDR'])) {
|
|
exit;
|
|
}
|
|
|
|
$_SERVER['HTTP_PORT'] = 80;
|
|
$_SERVER['SERVER_PORT'] = 80;
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
|
|
include dirname(__FILE__).'/../../../../config/config.inc.php';
|
|
|
|
$regex = '/^trc\.(.*)\.txt$/';
|
|
|
|
## GET trc.Daammjj.Hhhmmss.txt FILE
|
|
$id_ftp = ftp_connect(Configuration::get('MONDIALRELAYWS_FTP_HOST'));
|
|
ftp_login($id_ftp, Configuration::get('MONDIALRELAYWS_FTP_LOGIN'), Configuration::get('MONDIALRELAYWS_FTP_PASSWORD'));
|
|
ftp_chdir($id_ftp, 'depuismrelay');
|
|
$list = ftp_nlist($id_ftp, '.');
|
|
foreach($list as $name_file)
|
|
{
|
|
$name_file;
|
|
if(!preg_match($regex, $name_file))
|
|
continue;
|
|
touch(dirname(__FILE__) . '/trace/' . $name_file);
|
|
chmod(dirname(__FILE__) . '/trace/' . $name_file, 0777);
|
|
$handle = fopen(dirname(__FILE__) . '/trace/' . $name_file, 'w+');
|
|
if (!ftp_fget($id_ftp, $handle, $name_file, FTP_BINARY)){
|
|
die('erreur lors de l\'ouverture du fichier : ' . $name_file . chr(10));
|
|
}
|
|
else{
|
|
ftp_delete($id_ftp, $name_file);
|
|
}
|
|
fclose($handle);
|
|
}
|
|
ftp_close($id_ftp);
|
|
|
|
## PROCESS FOR ALL "TRACE" FILES
|
|
$inFolder = dirname(__FILE__) . '/trace/';
|
|
$iterator = new DirectoryIterator($inFolder);
|
|
if(!is_object($iterator) || !count($iterator))
|
|
die('No file');
|
|
|
|
foreach ($iterator as $fileinfo) {
|
|
|
|
if (!$fileinfo->isFile() || $fileinfo->getFilename() == '..' || $fileinfo->getFilename() == '.')
|
|
continue;
|
|
if (!preg_match( $regex, $fileinfo->getFilename() ))
|
|
continue;
|
|
|
|
$file = fopen(dirname(__FILE__).'/trace/'.$fileinfo->getFilename(), 'r');
|
|
$trc_data = array();
|
|
$expeditions = array();
|
|
while($line = fgets($file)) {
|
|
if (substr($line, 1, 1) != '2')
|
|
continue;
|
|
$expedition_number = substr($line, 16, 8);
|
|
$situation = substr($line, 54, 3);
|
|
$info = substr($line, 57, 3);
|
|
$new_expedition_number = substr($line, 62, 15);
|
|
$date = explode('.', substr($line, 30, 10));
|
|
$date = $date[2].'-'.$date[1].'-'.$date[0].' '.substr($line, 50, 2).':'.substr($line, 52, 2).':00';
|
|
$trc_data[$expedition_number] = array(
|
|
'expedition_number' => $expedition_number,
|
|
'situation' => $situation,
|
|
'info' => $info,
|
|
'new_expedition_number' => $new_expedition_number,
|
|
'date' => $date
|
|
);
|
|
|
|
if ($situation == 'DPC')
|
|
$expeditions[] = $expedition_number;
|
|
}
|
|
if (!count($expeditions))
|
|
continue;
|
|
|
|
$expedition_orders = array();
|
|
$expedition_order_details = array();
|
|
$expedition_shipping = array();
|
|
$order_detail_qty = array();
|
|
foreach (Db::getInstance()->executeS('
|
|
SELECT d.`id_order`, s.`expedition_number`, s.`shipping_number`, d.`id_order_detail`, s.`quantity`
|
|
FROM `' . _DB_PREFIX_ . 'mondialrelay_parcel` s
|
|
LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` d
|
|
ON d.`id_order_detail` = s.`id_order_detail`
|
|
LEFT JOIN `' . _DB_PREFIX_ . 'order_state_current` osc
|
|
ON osc.`id_order` = d.`id_order`
|
|
WHERE s.`expedition_number` IN (' . implode(', ', $expeditions) . ')
|
|
AND osc.`id_order_state` IN (2, 3, 12, 13, 17, 25)
|
|
') as $row) {
|
|
$expedition_shipping[$row['expedition_number']] = $row['shipping_number'];
|
|
$expedition_orders[$row['expedition_number']] = (int) $row['id_order'];
|
|
if (!isset($expedition_order_details[$row['expedition_number']]))
|
|
$expedition_order_details[$row['expedition_number']] = array();
|
|
$expedition_order_details[$row['expedition_number']][] = (int) $row['id_order_detail'];
|
|
$order_detail_qty[(int) $row['id_order_detail']] = (int) $row['quantity'];
|
|
}
|
|
|
|
$mr_carriers = array();
|
|
foreach (Db::getInstance()->executeS('
|
|
SELECT `id_carrier`
|
|
FROM `' . _DB_PREFIX_ . 'mr_method`
|
|
ORDER BY `is_deleted` ASC') as $carrier) {
|
|
$mr_carriers[] = (int) $carrier['id_carrier'];
|
|
}
|
|
|
|
foreach ($expedition_orders as $expedition_number => $id_order) {
|
|
|
|
if (!isset($expedition_shipping[$expedition_number]) || !$expedition_shipping[$expedition_number])
|
|
continue;
|
|
$shipping_number = $expedition_shipping[$expedition_number];
|
|
|
|
if (!isset($expedition_order_details[$expedition_number]))
|
|
continue;
|
|
$order_details = $expedition_order_details[$expedition_number];
|
|
|
|
if (!isset($expedition_orders[$expedition_number]))
|
|
continue;
|
|
$order = new Order((int) $id_order);
|
|
if (!Validate::isLoadedObject($order))
|
|
continue;
|
|
|
|
$fully_sent = FALSE;
|
|
$products_sent = array();
|
|
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT d.`id_order_detail`, IF(
|
|
(d.`product_quantity` - IF(
|
|
d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded`
|
|
) - IFNULL(SUM(s.`quantity`), 0)) > 0, 1, 0
|
|
) AS `remain`
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
LEFT OUTER JOIN `'._DB_PREFIX_.'lapostews` s
|
|
ON s.`id_order_detail` = d.`id_order_detail`
|
|
WHERE d.`id_order` = '.(int) $order->id.'
|
|
GROUP BY d.`id_order_detail`
|
|
') as $quantity_remain) {
|
|
if((int) $quantity_remain['remain'] == 0) {
|
|
$products_sent[] = (int) $quantity_remain['id_order_detail'];
|
|
}
|
|
}
|
|
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT d.`id_order_detail`, IF(
|
|
(d.`product_quantity` - IF(
|
|
d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded`
|
|
) - IFNULL(SUM(s.`quantity`), 0)) > 0, 1, 0
|
|
) AS `remain`
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
LEFT OUTER JOIN `'._DB_PREFIX_.'exapaqws` s
|
|
ON s.`id_order_detail` = d.`id_order_detail`
|
|
WHERE d.`id_order` = '.(int) $order->id.'
|
|
GROUP BY d.`id_order_detail`
|
|
') as $quantity_remain) {
|
|
if((int) $quantity_remain['remain'] == 0) {
|
|
$products_sent[] = (int) $quantity_remain['id_order_detail'];
|
|
}
|
|
}
|
|
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT d.`id_order_detail`, IF(
|
|
(d.`product_quantity` - IF(
|
|
d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded`
|
|
) - IFNULL(SUM(s.`quantity`), 0)) > 0, 1, 0
|
|
) AS `remain`
|
|
FROM `'._DB_PREFIX_.'order_detail` d
|
|
LEFT OUTER JOIN `'._DB_PREFIX_.'mondialrelay_parcel` s
|
|
ON s.`id_order_detail` = d.`id_order_detail`
|
|
WHERE d.`id_order` = '.(int) $order->id.'
|
|
GROUP BY d.`id_order_detail`
|
|
') as $quantity_remain) {
|
|
if((int) $quantity_remain['remain'] == 0) {
|
|
$products_sent[] = (int) $quantity_remain['id_order_detail'];
|
|
}
|
|
}
|
|
|
|
$remaining = array();
|
|
foreach($order_details as $d) {
|
|
if(!in_array($d, $products_sent)) {
|
|
$remaining[] = (int) $d;
|
|
}
|
|
}
|
|
|
|
if(count($remaining) == 0) {
|
|
$fully_sent = TRUE;
|
|
} else {
|
|
$id_sales = array();
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT DISTINCT c.`id_sale`
|
|
FROM `'._DB_PREFIX_.'product_ps_cache` c
|
|
LEFT JOIN `'._DB_PREFIX_.'order_detail` d
|
|
ON c.`id_product` = d.`product_id`
|
|
WHERE d.`id_order_detail` IN ('.implode(', ', $remaining).')
|
|
') as $row) {
|
|
$id_sales[] = (int) $row['id_sale'];
|
|
}
|
|
|
|
$sent_sales = array();
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT DISTINCT `id_sale`
|
|
FROM `'._DB_PREFIX_.'shipping_history`
|
|
WHERE `id_order` = '.(int) $order->id.'
|
|
AND `id_sale` IN ('.implode(', ', $id_sales).')
|
|
') as $row) {
|
|
$sent_sales[] = (int) $row['id_sale'];
|
|
}
|
|
|
|
$remaining = array();
|
|
foreach($id_sales as $s) {
|
|
if(!in_array($s, $sent_sales)) {
|
|
$remaining[] = (int) $s;
|
|
break;
|
|
}
|
|
}
|
|
|
|
$fully_sent = (count($remaining) == 0);
|
|
}
|
|
|
|
$html_products_sent = '';
|
|
$products_names = array();
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT `id_order_detail`, `product_name`
|
|
FROM `'._DB_PREFIX_.'order_detail`
|
|
WHERE `id_order_detail` IN ('.implode(', ', $order_details).')
|
|
') as $p) {
|
|
if (isset($order_detail_qty[(int) $p['id_order_detail']]))
|
|
$html_products_sent .= '<br />'."\r\n".$order_detail_qty[(int) $p['id_order_detail']] . 'x' . ' ' . $p['product_name'];
|
|
}
|
|
|
|
$id_carrier = (int) $order->id_carrier;
|
|
if (!in_array($id_carrier, $mr_carriers))
|
|
$id_carrier = $mr_carriers[0];
|
|
$carrier = new Carrier($id_carrier, (int) $order->id_lang);
|
|
$customer = new Customer((int) $order->id_customer);
|
|
|
|
if((int) $order->id_lang == 3) {
|
|
$content_html = '<strong>Contenido del paquete:</strong>';
|
|
$content_txt = 'Contenido del paquete:';
|
|
} elseif((int) $order->id_lang == 5) {
|
|
$content_html = '<strong>Contenuto del package:</strong>';
|
|
$content_txt = 'Contenuto del package:';
|
|
} else {
|
|
$content_html = '<strong>Contenu du colis :</strong>';
|
|
$content_txt = 'Contenu du colis :';
|
|
}
|
|
|
|
$templateVars = array(
|
|
'{followup}' => str_replace('@', $shipping_number, $carrier->url),
|
|
'{firstname}' => $customer->firstname,
|
|
'{lastname}' => $customer->lastname,
|
|
'{id_order}' => (int) $order->id,
|
|
'{product_list}' => !empty($html_products_sent)? $content_html.$html_products_sent: '',
|
|
'{product_list_txt}' => !empty($html_products_sent)? $content_txt.strip_tags($html_products_sent): '',
|
|
);
|
|
|
|
$history = new OrderHistory();
|
|
$history->id_order = (int) $order->id;
|
|
|
|
$history->changeIdOrderState(($fully_sent? Configuration::get('PS_OS_SHIPPING'): 17), (int) $order->id);
|
|
$history->id_employee = 0;
|
|
$history->addWithemail(TRUE, $templateVars);
|
|
|
|
global $_LANGMAIL;
|
|
$subject = 'Package in transit';
|
|
Mail::Send(
|
|
intval($order->id_lang),
|
|
'in_transit',
|
|
(
|
|
(is_array($_LANGMAIL) && key_exists($subject, $_LANGMAIL))
|
|
? $_LANGMAIL[$subject]
|
|
: $subject
|
|
),
|
|
$templateVars,
|
|
$customer->email,
|
|
$customer->firstname.' '.$customer->lastname
|
|
);
|
|
}
|
|
|
|
## ARCHIVE FILE
|
|
$repo_archive = dirname(__FILE__) . '/archives/trace/';
|
|
$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);
|
|
}
|
|
rename(dirname(__FILE__).'/trace/'.$fileinfo->getFilename(), $repo_archive . $fileinfo->getFilename());
|
|
} |