bebeboutik/adm/mraddressedit.php
2017-07-07 12:38:21 +02:00

336 lines
16 KiB
PHP

<?php
require_once(dirname(__FILE__).'/../config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');
if($cookie->isLoggedBack() && $cart = Tools::getValue('id_cart')) {
$address = Db::getInstance()->getRow('
SELECT s.*, m.*, r.`narel`
FROM `'._DB_PREFIX_.'mr_selected` s
INNER JOIN `'._DB_PREFIX_.'mr_method` m
ON m.`id_mr_method` = s.`id_method`
LEFT JOIN `'._DB_PREFIX_.'mondialrelay_relais` r
ON SUBSTRING(s.`MR_Selected_Num`, 2) = r.`direction`
AND s.`MR_Selected_Pays` = r.`prpay`
WHERE `id_cart` = '.pSQL(intval(Tools::getValue('id_cart'))).'
');
if($address['MR_Selected_Pays'] == 'F' || $address['MR_Selected_Pays'] == '') {
$address['MR_Selected_Pays'] = 'FR';
}
$deliv_address = Db::getInstance()->getRow('
SELECT *
FROM `'._DB_PREFIX_.'address` a
LEFT JOIN `'._DB_PREFIX_.'orders` o
ON a.`id_address` = o.`id_address_delivery`
WHERE o.`id_cart` = '.(int) Tools::getValue('id_cart').'
');
if($address) {
if(isset($_POST['submitMR'])) {
$num = Tools::getValue('MR_Selected_Num', '');
if($num != '') {
$rel = Db::getInstance()->getRow('
SELECT *
FROM `'._DB_PREFIX_.'mondialrelay_relais`
WHERE `direction` = "'.pSQL(sprintf('%05d', (int) ((string) substr($num, -5)))).'"
AND `prpay` = "'.pSQL($address['MR_Selected_Pays']).'"
');
if($rel) {
Db::getInstance()->Execute('
UPDATE `'._DB_PREFIX_.'mr_selected`
SET `MR_Selected_Num` = "'.pSQL($num).'",
`MR_Selected_LgAdr1` = "'.pSQL(trim($rel['colnom'])).'",
`MR_Selected_LgAdr2` = "'.pSQL(trim($rel['colbat'])).'",
`MR_Selected_LgAdr3` = "'.pSQL(trim($rel['colrue'])).'",
`MR_Selected_LgAdr4` = "'.pSQL(trim($rel['coldit'])).'",
`MR_Selected_CP` = "'.pSQL(trim($rel['codpos'])).'",
`MR_Selected_Ville` = "'.pSQL(trim($rel['libbp'])).'",
`MR_Selected_Pays` = "'.pSQL(trim($rel['prpay'])).'"
WHERE `id_cart` = '.(int) Tools::getValue('id_cart').'
');
}
}
}
$order = Db::getInstance()->getRow('
SELECT `id_carrier`, `id_order`, `id_address_delivery`
FROM `'._DB_PREFIX_.'orders`
WHERE `id_cart` = '.(int) Tools::getValue('id_cart').'
');
$weight = (float) Db::getInstance()->getValue('
SELECT SUM(`product_weight` * `product_quantity`)
FROM `'._DB_PREFIX_.'order_detail`
WHERE `id_order` = '.(int) $order['id_order'].'
');
require_once _PS_MODULE_DIR_.'mondialrelay/classes/MRGetRelayPoint.php';
require_once _PS_MODULE_DIR_.'mondialrelay/classes/MRRelayDetail.php';
$mr = new MondialRelay();
$delivery_mode = $address['dlv_mode'];
$params = array(
'action' => $delivery_mode,
'id_carrier' => $order['id_carrier'],
'weight' => Tools::ps_round($weight, 2),
'id_address_delivery' => $order['id_address_delivery'],
);
$api = new MRGetRelayPoint($params, $mr);
$api->init();
$api->send();
$result = $api->getResult();
$relay_points = array();
if(count($result['error']) === 0) {
$success = (array)$result['success'];
ksort($success);
$relay_points = array_values($success);
}
?>
<!doctype html>
<html><head>
<meta charset="utf-8" />
<style type="text/css">
body {
overflow: auto;
font-size: 12px;
color: #222222;
font-family: sans-serif;
}
label {
font-weight: bold;
display: block;
}
fieldset {
overflow: auto;
float: left;
width: 200px;
}
p {
overflow: auto;
}
#map_canvas {
width: 470px;
height: 300px;
display: inline-block;
}
h1 {
clear: both;
font-size: 16px;
padding-top: 20px;
}
#pr_list {
width: 200px;
height: 300px;
overflow-y: auto;
display: inline-block;
margin-left: 10px;
}
#pr_chooser {
clear: both;
}
#pr_chooser ul {
list-style: outside none;
margin: 0px;
padding: 0px;
}
#pr_chooser li {
cursor: pointer;
font-size: 12px;
margin: 10px 10px 10px 0px;
padding: 10px;
}
#pr_chooser li:hover {
background: #ccddee;
}
.maplink {
color: #1122ff;
cursor: pointer;
}
.maplink:hover {
text-decoration: underline;
}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="/js/jquery/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="/js/jquery/jquery.ui.map.js"></script>
<script type="text/javascript" src="/js/jquery/jquery.ui.map.services.js"></script>
</head><body onload="<?php if(isset($_POST['submitMR'])) { ?>window.close();<?php } ?>">
<?php
if($order['id_carrier'] == 216) {
echo '<p style="border: 1px solid #ff0000; background: #ffeeee; font-weight: bold; padding: 10px 20px;">La modification des livraisons en point relais XL n\'est pas possible.</p>';
} else {
?>
<form action="" method="post">
<fieldset>
<p>
<label>Nom destinataire</label>
<span style="display: block; min-height: 1em;"><?php echo htmlentities($deliv_address['lastname'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?></span>
</p>
<p>
<label>Prénom destinataire</label>
<span style="display: block; min-height: 1em;"><?php echo htmlentities($deliv_address['firstname'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?></span>
</p>
<p>
<label>Entreprise</label>
<span style="display: block; min-height: 1em;"><?php echo htmlentities($deliv_address['company'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?></span>
</p>
<p>
<label>Téléphone</label>
<span style="display: block; min-height: 1em;"><?php echo htmlentities($deliv_address['phone'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?></span>
</p>
<p>
<label>Email</label>
<span style="display: block; min-height: 1em;"><?php echo htmlentities($deliv_address['email'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?></span>
</p>
</fieldset>
<fieldset>
<p style="display: none;">
<label>Mode de livraison</label>
<select autocomplete="off" name="delivery_mode">
<?php
$modes = array('24R' => 'Point relais (24R)',
'24L' => 'Point relais XL (24L)', '24X' => 'Point relais XXL (24X)', 'HOM' => 'Livraison à domicile Home (HOM)', 'LD1' => 'Livraison à domicile 1 personne (LD1)',
'LDS' => 'Livraison à domicile 2 personnes (LDS)', 'DRI' => 'Livraison Colis Drive (DRI)');
foreach($modes as $k => $v) {
echo '<option value="'.$k.'"'.($delivery_mode == $k? ' selected="selected"': '').'>'.$v.'</option>';
}
?>
</select>
</p>
<p>
<label>Adresse 1</label>
<input autocomplete="off" type="text" name="MR_Selected_LgAdr1" readonly="true" value="<?php echo htmlentities($address['MR_Selected_LgAdr1'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?>" />
</p>
<p>
<label>Adresse 2</label>
<input autocomplete="off" type="text" name="MR_Selected_LgAdr2" readonly="true" value="<?php echo htmlentities($address['MR_Selected_LgAdr2'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?>" />
</p>
<p>
<label>Adresse 3</label>
<input autocomplete="off" type="text" name="MR_Selected_LgAdr3" readonly="true" value="<?php echo htmlentities($address['MR_Selected_LgAdr3'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?>" />
</p>
<p>
<label>Adresse 4</label>
<input autocomplete="off" type="text" name="MR_Selected_LgAdr4" readonly="true" value="<?php echo htmlentities($address['MR_Selected_LgAdr4'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?>" />
</p>
<p>
<label>Code postal</label>
<input autocomplete="off" type="text" name="MR_Selected_CP" readonly="true" value="<?php echo htmlentities($address['MR_Selected_CP'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?>" />
</p>
<p>
<label>Ville</label>
<input autocomplete="off" type="text" name="MR_Selected_Ville" readonly="true" value="<?php echo htmlentities($address['MR_Selected_Ville'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?>" />
</p>
<p>
<label>ID point retrait</label>
<input autocomplete="off" type="text" name="MR_Selected_Num" readonly="true" value="<?php echo htmlentities($address['MR_Selected_Num'], ENT_COMPAT | ENT_HTML401, 'UTF-8'); ?>" />
</p>
<p><input type="submit" value="Envoyer" name="submitMR" /></p>
</fieldset>
</form>
<?php
echo '
<script type="text/javascript">
<!--
markers = [];
-->
</script>
<div id="pr_chooser"><h1>Livraison hors domicile - Recherche d\'un point de retrait</h1>
<div id="map_canvas"></div><div id="pr_list"><ul></ul></div>
<script type="text/javascript">
<!--
function updateForm(delivery_mode, address1, address2, address3, address4, postcode, city, prid) {
$("select[name=delivery_mode]").val("");
$("input[name=MR_Selected_LgAdr1]").val("");
$("input[name=MR_Selected_LgAdr2]").val("");
$("input[name=MR_Selected_LgAdr3]").val("");
$("input[name=MR_Selected_LgAdr4]").val("");
$("input[name=MR_Selected_CP]").val("");
$("input[name=MR_Selected_Ville]").val("");
$("input[name=MR_Selected_Num]").val("");
$("select[name=delivery_mode]").val(delivery_mode);
$("input[name=MR_Selected_LgAdr1]").val(address1);
$("input[name=MR_Selected_LgAdr2]").val(address2);
$("input[name=MR_Selected_LgAdr3]").val(address3);
$("input[name=MR_Selected_LgAdr4]").val(address4);
$("input[name=MR_Selected_CP]").val(postcode);
$("input[name=MR_Selected_Ville]").val(city);
$("input[name=MR_Selected_Num]").val(prid);
}
$(document).ready(function() {
delivery_modes = {};
$("select[name=delivery_mode] option").each(function(id, el) {
delivery_modes[$(el).attr("value")] = $(el).text();
});
$("#map_canvas").gmap('.($pr_coords? '{ "center": "'.(float) $pr_coords['xcoords'].','.(float) $pr_coords['ycoords'].'" }': '').').bind("init", function(ev, map) {';
$current_address = htmlentities(preg_replace('/(, ){2,}/', ', ', preg_replace('/(, ){2,}/', ', ', implode(', ', array(trim($deliv_address['address1']), trim($deliv_address['address2'])))).', '.trim($deliv_address['postcode']).' '.$deliv_address['city'].', '.Country::getNameById(2, $deliv_address['id_country'])), ENT_COMPAT | ENT_HTML401, 'UTF-8');
echo '
$("#map_canvas").gmap("search", {"address": "'.$current_address.'"}, function(results, status) {
if ( status === "OK" ) {
var lat = null;
var lng = null;
if(results[0].geometry.location.H) {
lat = results[0].geometry.location.H;
lng = results[0].geometry.location.L;
} else if(results[0].geometry.location.G) {
lat = results[0].geometry.location.G;
lng = results[0].geometry.location.K;
} else {
var loc = results[0].geometry.location.toString().replace("(", "").replace(")", "").replace(" ", "").split(",");
lat = loc[0];
lng = loc[1];
}
$("#map_canvas").gmap("addMarker", {"icon": "http://static.privatesportshop.com/img/admin/marker_home.png", "idpr": "pr_home", "position": lat + "," + lng, "bounds": true}).click(function() {
$("#map_canvas").gmap("openInfoWindow", {"content": "Adresse actuelle"}, this);
});
';
for($i= 0, $l = count($relay_points); $i < $l; $i++) {
$relay = $relay_points[$i];
if($relay->Pays == "") {
continue;
}
echo '
$("#map_canvas").gmap("search", {"address": "'.htmlentities(preg_replace('/(, ){2,}/', ', ', preg_replace('/(, ){2,}/', ', ', implode(', ', array(trim($relay->LgAdr3), trim($relay->LgAdr2), trim($relay->LgAdr4), trim($relay->CP).' '.trim($relay->Ville), Country::getNameById(2, $relay->Pays != 'FR'? Country::getByIso($relay->Pays): 8))))), ENT_COMPAT | ENT_HTML401, 'UTF-8').'"}, function(results, status) {
if ( status === "OK" ) {
var lat = null;
var lng = null;
if(results[0].geometry.location.H) {
lat = results[0].geometry.location.H;
lng = results[0].geometry.location.L;
} else if(results[0].geometry.location.G) {
lat = results[0].geometry.location.G;
lng = results[0].geometry.location.K;
} else {
var loc = results[0].geometry.location.toString().replace("(", "").replace(")", "").replace(" ", "").split(",");
lat = loc[0];
lng = loc[1];
}
var prtext = ["", "'.htmlentities(trim($relay->LgAdr1), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities(trim($relay->LgAdr2), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities(trim($relay->LgAdr3), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities(trim($relay->LgAdr4), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities($relay->CP, ENT_COMPAT | ENT_HTML401, 'UTF-8').' '.htmlentities($relay->Ville, ENT_COMPAT | ENT_HTML401, 'UTF-8').'"].filter(function(x) { return x != ""; }).join("<br />");
var prtext_action = "<br /><a class=\"maplink\" onclick=\"$(\'#pr_list li[data-index='.$i.']\').trigger(\'click\'); return false;\">Sélectionner</a>";
markers.push($("#map_canvas").gmap("addMarker", { "id": "pr_'.htmlentities($relay->Num, ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "position": lat + "," +lng, "bounds": true, "content": prtext + prtext_action }));
markers[markers.length - 1].click(function() {
$("#map_canvas").gmap("openInfoWindow", {"content": $(this).attr("content")}, this);
});
var litem = $("<li />").attr("data-index", '.$i.').html(prtext).click(function() {
$.each(markers, function(id, el) {
if(el[0].id == "pr_'.htmlentities(trim($relay->Num), ENT_COMPAT | ENT_HTML401, 'UTF-8').'") {
google.maps.event.trigger(el[0], "click");
}
});
$("#map_canvas").gmap("option", "center", markers[parseInt($(this).attr("data-index"))][0].getPosition());
updateForm("24R", "'.htmlentities(trim($relay->LgAdr1), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities(trim($relay->LgAdr2), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities(trim($relay->LgAdr3), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities(trim($relay->LgAdr4), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities(trim($relay->CP), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities(trim($relay->Ville), ENT_COMPAT | ENT_HTML401, 'UTF-8').'", "'.htmlentities(trim($relay->Num), ENT_COMPAT | ENT_HTML401, 'UTF-8').'");
});
$("#pr_list ul").append(litem);
}
});
';
}
echo '
}
});
});
});
-->
</script>
</div>';
}
?>
</body></html>
<?php
}
}
?>