277 lines
9.0 KiB
JavaScript
277 lines
9.0 KiB
JavaScript
|
/**
|
||
|
* TNT OFFICIAL MODULE FOR PRESTASHOP
|
||
|
*
|
||
|
* @author GFI Informatique <www.gfi.fr>
|
||
|
* @copyright 2016 GFI Informatique, 2016 TNT
|
||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||
|
*/
|
||
|
|
||
|
$(document).ready(function () {
|
||
|
$("#parcelsTbody").on('click', function (objEvent) {
|
||
|
var $elmt = $(objEvent.target);
|
||
|
if ($elmt.hasClass('removeParcel')) {
|
||
|
removeParcel($elmt.val());
|
||
|
}
|
||
|
if ($elmt.hasClass('updateParcel')) {
|
||
|
updateParcel($elmt.val());
|
||
|
}
|
||
|
});
|
||
|
|
||
|
$("#submitAddParcel").on('click', function () {
|
||
|
addParcel();
|
||
|
});
|
||
|
|
||
|
$("a#fancyBoxAddParcelLink").fancybox({
|
||
|
"afterClose": function () {
|
||
|
$("#addParcelFancyBox #addParcelError").hide();
|
||
|
$("#addParcelWeight").val("");
|
||
|
},
|
||
|
"transitionIn": 'elastic',
|
||
|
"transitionOut": 'elastic',
|
||
|
"type": 'inline',
|
||
|
"speedIn": 600,
|
||
|
"speedOut": 200,
|
||
|
"overlayShow": false,
|
||
|
"autoDimensions": true,
|
||
|
"autoCenter": false,
|
||
|
"helpers": {
|
||
|
overlay: {
|
||
|
closeClick: false,
|
||
|
locked: false
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
$("input[id*='parcelWeight-']").on('change', function () {
|
||
|
var value = parseFloat($(this).val());
|
||
|
if (value.toFixed(1) == 0.0) {
|
||
|
value = 0.1;
|
||
|
}
|
||
|
$(this).val(value.toFixed(1));
|
||
|
});
|
||
|
|
||
|
$('#shipping_date').datepicker({
|
||
|
"minDate": startDate,
|
||
|
"dateFormat": 'dd/mm/yy',
|
||
|
"onSelect": function () {
|
||
|
$('#formAdminShippingDatePanel .alert').hide();
|
||
|
$('.waitimage').show();
|
||
|
var data = {};
|
||
|
data['orderId'] = id_order;
|
||
|
data['shippingDate'] = $('#shipping_date').val();
|
||
|
$.ajax({
|
||
|
type: 'POST',
|
||
|
url: checkShippingDateValidUrl,
|
||
|
data: data,
|
||
|
global: false,
|
||
|
success: function (json) {
|
||
|
var data = jQuery.parseJSON(json);
|
||
|
if (data.error) {
|
||
|
if (data.error.length) {
|
||
|
$("#delivery-date-error").html(data.error);
|
||
|
} else {
|
||
|
$("#delivery-date-error").html('La date n\'est pas valide.');
|
||
|
}
|
||
|
$("#delivery-date-error").show();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (data.dueDate) {
|
||
|
$("#due-date").html(data.dueDate);
|
||
|
}
|
||
|
|
||
|
if (data.mdwMessage) {
|
||
|
$("#delivery-date-success").html(data.mdwMessage);
|
||
|
} else {
|
||
|
$("#delivery-date-success").html('La date est valide.');
|
||
|
}
|
||
|
$("#delivery-date-success").show();
|
||
|
|
||
|
},
|
||
|
error: function () {
|
||
|
$("#delivery-date-error").html('Une erreur s\'est produite, merci de réessayer dans quelques minutes.');
|
||
|
$("#delivery-date-error").show();
|
||
|
},
|
||
|
complete: function () {
|
||
|
$('.waitimage').hide();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
if (typeof shippingDate != 'undefined') {
|
||
|
$("#shipping_date").datepicker("setDate", shippingDate);
|
||
|
}
|
||
|
if (boolIsShipped) {
|
||
|
$("#shipping_date").datepicker("option", "disabled", true);
|
||
|
}
|
||
|
|
||
|
if (!boolIsAddressEditable) {
|
||
|
$("#addressShipping form :input").attr("disabled", true);
|
||
|
$('#addressShipping a').css("cursor", "not-allowed");
|
||
|
$('#addressShipping a').on('click', function (objEvent) {
|
||
|
objEvent.preventDefault();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
updateTotalWeight();
|
||
|
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* remove a parcel
|
||
|
* @param rowNumber
|
||
|
*/
|
||
|
function removeParcel(parcelId) {
|
||
|
var data = {
|
||
|
"parcelId": parcelId
|
||
|
};
|
||
|
var parcelCount = getParcelRowCount();
|
||
|
|
||
|
if (parcelCount <= 1) {
|
||
|
$("#updateParcelErrorMessage-" + parcelId).html(atLeastOneParcelStr);
|
||
|
$("#parcelWeightError-" + parcelId).show();
|
||
|
}
|
||
|
else {
|
||
|
$.ajax({
|
||
|
type: 'POST',
|
||
|
url: removeParcelUrl,
|
||
|
data: data,
|
||
|
success: function (json) {
|
||
|
$("#row-parcel-" + parcelId).remove();
|
||
|
updateTotalWeight();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Update a parcel
|
||
|
* @param parcelId
|
||
|
*/
|
||
|
function updateParcel(parcelId) {
|
||
|
$("#parcelWeightError-" + parcelId).hide();
|
||
|
$("#parcelWeightSuccess-" + parcelId).hide();
|
||
|
var data = {};
|
||
|
data['parcelId'] = parcelId;
|
||
|
data['weight'] = $("#parcelWeight-" + parcelId).val();
|
||
|
data['orderId'] = id_order;
|
||
|
if (isNaN($("#parcelWeight-" + parcelId).val()) || $("#parcelWeight-" + parcelId).val() <= 0) {
|
||
|
$("#updateParcelErrorMessage-" + parcelId).html("Le poids n'est pas valide");
|
||
|
$("#parcelWeightError-" + parcelId).show();
|
||
|
} else {
|
||
|
$.ajax({
|
||
|
type: 'POST',
|
||
|
url: updateParcelUrl,
|
||
|
data: data,
|
||
|
success: function (json) {
|
||
|
var response = JSON.parse(json);
|
||
|
if (!response.result) {
|
||
|
$("#updateParcelErrorMessage-" + parcelId).html(response.error);
|
||
|
$("#parcelWeightError-" + parcelId).show();
|
||
|
} else {
|
||
|
$("#parcelWeight-" + parcelId).val(response.weight);
|
||
|
$("#parcelWeightSuccess-" + parcelId).show();
|
||
|
updateTotalWeight();
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Add a parcel
|
||
|
*/
|
||
|
function addParcel() {
|
||
|
$("#addParcelFancyBox #addParcelError").hide();
|
||
|
//check if the weight value is valid
|
||
|
if (isNaN($("#addParcelWeight").val()) || ($("#addParcelWeight").val() <= 0)) {
|
||
|
$("#addParcelFancyBox #addParcelErrorMessage").html("Le poids n'est pas valide");
|
||
|
$("#addParcelFancyBox #addParcelError").show();
|
||
|
} else {
|
||
|
var data = {};
|
||
|
data['orderId'] = id_order;
|
||
|
data['weight'] = $("#addParcelWeight").val();
|
||
|
$.ajax({
|
||
|
type: 'POST',
|
||
|
url: addParcelUrl,
|
||
|
data: data,
|
||
|
success: function (json) {
|
||
|
var response = JSON.parse(json);
|
||
|
if (response.result) {
|
||
|
$.fancybox.close();
|
||
|
addRowParcel(json);
|
||
|
updateTotalWeight();
|
||
|
} else {
|
||
|
$("#addParcelFancyBox #addParcelErrorMessage").html(response.error);
|
||
|
$("#addParcelFancyBox #addParcelError").show();
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Get the TNT admin controller token
|
||
|
* @returns {*|jQuery}
|
||
|
*/
|
||
|
function getAdminTNTOfficielToken() {
|
||
|
return $("#AdminTNTOfficielToken").val();
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* add a row in the parcels table
|
||
|
*/
|
||
|
function addRowParcel(json) {
|
||
|
var nextRowNumber = getNexttParcelNumber();
|
||
|
var data = JSON.parse(json);
|
||
|
|
||
|
$("#parcelsTbody").append('\
|
||
|
<tr class="current-edit hidden-print" id="row-parcel-' + data['parcel'][0]['id_parcel'] + '">\
|
||
|
<td>\
|
||
|
<div class="input-group fixed-width-xl">' + nextRowNumber + '</div>\
|
||
|
</td>\
|
||
|
<td>\
|
||
|
<input id="parcelWeight-' + data['parcel'][0]['id_parcel'] + '" value="' + data['parcel'][0]['weight'] + '" /> \
|
||
|
<div class="alert alert-danger alert-danger-small" \
|
||
|
id="parcelWeightError-' + data['parcel'][0]['id_parcel'] + '" \
|
||
|
style="display: none" \
|
||
|
><p id="updateParcelErrorMessage-' + data['parcel'][0]['id_parcel'] + '"></p>\
|
||
|
</div>\
|
||
|
<div class="alert alert-success alert-danger-small" \
|
||
|
id="parcelWeightSuccess-' + data['parcel'][0]['id_parcel'] + '" \
|
||
|
style="display: none" \
|
||
|
><p id="updateParcelSuccessMessage-' + data['parcel'][0]['id_parcel'] + '">' + updateSuccessfulStr + '</p>\
|
||
|
</div>\
|
||
|
</td>\
|
||
|
<td class="actions">\
|
||
|
<button class="btn btn-primary updateParcel" value="' + data['parcel'][0]['id_parcel'] + '">' + updateStr + '</button> \
|
||
|
<button class="btn btn-primary removeParcel" value="' + data['parcel'][0]['id_parcel'] + '">' + deleteStr + '</button>\
|
||
|
</td>\
|
||
|
</tr>');
|
||
|
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Add or update total weight
|
||
|
*/
|
||
|
function updateTotalWeight() {
|
||
|
var sum = 0;
|
||
|
$("[id*='parcelWeight-']").each(function () {
|
||
|
var value = $(this).val();
|
||
|
// add only if the value is number
|
||
|
if (!isNaN(value) && value.length != 0) {
|
||
|
sum += parseFloat(value);
|
||
|
}
|
||
|
});
|
||
|
$("#total-weight").html(sum);
|
||
|
}
|
||
|
|
||
|
function getParcelRowCount() {
|
||
|
return $("#parcelsTable > #parcelsTbody tr").length++;
|
||
|
}
|
||
|
|
||
|
function getNexttParcelNumber() {
|
||
|
return parseInt($("#parcelsTable > #parcelsTbody tr:last-child td:first-child div.input-group").html()) + 1
|
||
|
}
|