bebeboutik-scripts/reassign_mr.php
2018-03-21 16:30:34 +01:00

91 lines
2.5 KiB
PHP

<?php
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
//require_once realpath(dirname(__FILE__)).'/www/config/config.inc.php';
require_once realpath(dirname(__FILE__).'/../').'/bebeboutik/config/config.inc.php';
$longopts = array(
'verbose',
'dry-run',
'debug',
'sale:',
);
$shortopts = "";
$options = getopt($shortopts, $longopts);
// Options
$optVerbose = false;
if (isset($options['verbose'])) {
$optVerbose = true;
}
$optTest = false;
if (isset($options['dry-run'])) {
$optTest = true;
}
$optDebug = false;
if (isset($options['debug'])) {
$optDebug = true;
}
$sale = false;
if (isset($options['sale'])) {
$sale = $options['sale'];
}
if (empty($sale)) {
echo "No sale selected\n";
exit;
}
// Start
echo date('Y-m-d H:i:s')." - START \n";
$order = Db::getInstance()->ExecuteS('
SELECT mr_s.*
FROM `'._DB_PREFIX_.'mr_selected` mr_s
WHERE mr_s.`id_order` IN (
SELECT DISTINCT o.id_order
FROM `'._DB_PREFIX_.'orders` 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`
WHERE c.`id_sale` = '.$sale.' AND o.id_carrier = 91
) AND MR_Selected_Num = ""
');
$orderNb = count($order);
echo date('Y-m-d H:i:s')." - $orderNb commandes sans num MR\n";
if ($orderNb > 0) {
foreach ($order as $o) {
// Looks for MR
$mr = Db::getInstance()->ExecuteS('
SELECT * FROM `'._DB_PREFIX_.'mondialrelay_relais`
WHERE colnom = "'.$o['MR_Selected_LgAdr1'].'"
AND colrue = "'.$o['MR_Selected_LgAdr3'].'"
AND codpos = "'.$o['MR_Selected_CP'].'"
');
if (count($mr) == 1) {
$relai = $mr[0];
echo "Order ".$o['id_order']." - MR : ".$relai['direction']."\n";
// Update
if (!empty($relai['direction'])) {
$sql = 'UPDATE `'._DB_PREFIX_.'mr_selected` SET MR_Selected_Num = '.$relai['direction'].
' WHERE id_mr_selected = '.$o['id_mr_selected'].' AND id_order = '.$o['id_order'];
if ($optVerbose) {
echo $sql."\n";
}
if (!$optTest) {
Db::getInstance()->ExecuteS($sql);
}
}
}
elseif (count($mr) > 1) {
echo "Order ".$o['id_order']." - MR > 1 - manual.\n";
} else {
echo "Order ".$o['id_order']." - MR not found.\n";
}
}
}
echo date('Y-m-d H:i:s')." - END \n";