Files
redline.com.pl/modules/inpostship/views/js/inpostship_front.js
2024-11-11 18:46:54 +01:00

507 lines
19 KiB
JavaScript

/**
* NOTICE OF LICENSE
*
* This file is licenced under the Software License Agreement.
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You must not modify, adapt or create derivative works of this source code
*
* @author PrestaHelp.com
* @copyright 2019 PrestaHelp
* @license LICENSE.txt
*/
$(document).ready(function () {
if (typeof checkout == 'undefined') {
checkout = 'default';
}
if (typeof showWeek == 'undefined') {
showWeek = 0;
}
if (checkout == 'default') {
if ($("#cgv").length) {
// $("#cgv").unbind('click', inpostCGV($('#cgv'))).bind('click', inpostCGV($('#cgv')));
} else {
$("button.standard-checkout").unbind('click', inpostCGV($('#cgv'))).bind('click', inpostCGV($('#cgv')));
}
if (showWeek == 0) {
$('.delivery-option').each(function() {
var idd = parseInt($(this).find('input').val());
if (inpostkey_week.indexOf(idd) || inpostkey_week_cod.indexOf(idd)) {
$(this).hide();
}
});
}
}
if (checkout == 'onepagecheckoutps') {
$('.approval-payment #regulamin').on('click', function() {
// console.log('onepagecheckoutps_step_three_container')
})
$("#regulamin").unbind('click', inpostCGV($("#regulamin"))).bind('click', inpostCGV($("#regulamin")));
}
if (checkout == 'onepagecheckout') {
if ($("#cgv").length) {
// $("#cgv").unbind('click', inpostCGV($('#cgv'))).bind('click', inpostCGV($('#cgv')));
} else {
$("button.confirm_button").unbind('click', inpostCGV($('#cgv'))).bind('click', inpostCGV($('#cgv')));
}
setTimeout(function() {
initInpost($('#carrierTable .carrier_action input:checked'));
}, 500);
}
$('.delivery_option_radio').off('click', '.delivery_option_radio', inpostBindRadio);
$('.delivery_option_radio').on('click', '.delivery_option_radio', inpostBindRadio);
initInpost($('.delivery_option_radio' + ":checked"));
$('#inpostshipmodal .modal-footer button').on('click', function() {
$('#inpostshipmodal').modal('hide');
});
$('#inpostshipmodal .modal-header button').on('click', function() {
$('#inpostshipmodal').modal('hide');
});
$(document).on('click', '#inpostshipmodal .modal-footer button', function() {
$('#inpostshipmodal').modal('hide');
});
$(document).on('click', '#inpostshipmodal .modal-header button', function() {
$('#inpostshipmodal').modal('hide');
});
});
var assetsServer = 'https://api-pl-points.easypack24.net/v1';
function openModalMap(id) {
$('#easypack-widget').empty();
$('#easypack-map').empty();
var pointType = ['parcel_locker_only'];
if (pTypes == 1) {
pointType = ['parcel_locker', 'pop'];
}
if (mapBox == 1) {
if (imap == 'google') {
easyPack.init({
instance: 'pl',
defaultLocale: 'pl',
points: {
types: pointType
},
locales: 'pl',
apiEndpoint: assetsServer,
mapType: imap,
searchType: imap,
map: {
useGeolocation: true,
initialTypes: pointType,
googleKey: imgm
},
});
} else {
easyPack.init({
instance: 'pl',
defaultLocale: 'pl',
points: {
types: pointType
},
locales: 'pl',
apiEndpoint: assetsServer,
mapType: imap,
searchType: imap,
map: {
useGeolocation: true,
initialTypes: pointType
},
});
}
if (imw == 1) {
var width = 500;
var height = ($(window).height() * 0.7); // 600
} else {
var width = $(window).width() - 25;
var height = ($(window).height() -25); // 600
}
if (mapShow == 2) {
width = ($(window).width() * 0.7);
}
easyPack.modalMap(function (point, modal) {
updateInpostInfo(cart, id, point.name);
modal.closeModal();
return false;
}, {width: width, height: 600});
} else {
if (imap == 'google') {
easyPack.init({
instance: 'pl',
defaultLocale: 'pl',
points: {
types: pointType
},
locales: 'pl',
apiEndpoint: assetsServer,
mapType: imap,
searchType: imap,
map: {
useGeolocation: true,
initialTypes: pointType,
googleKey: imgm
},
});
} else {
easyPack.init({
instance: 'pl',
defaultLocale: 'pl',
points: {
types: pointType
},
locales: 'pl',
apiEndpoint: assetsServer,
mapType: imap,
searchType: imap,
map: {
useGeolocation: true,
initialTypes: pointType,
},
});
}
easyPack.mapWidget('easypack-widget', function(point) {
updateInpostInfo(cart, id, point.name);
});
}
}
function inpostCGVUncheck() {
$("#cgv").prop('checked', false);
if (typeof $.uniform != 'undefined') {
$.uniform.update();
}
}
function inpostBindRadio(e) {
inpostCGVUncheck();
initInpost($(this), true);
}
function updateInpostInfo(id_cart, id_carrier, point) {
$('.saveInfo').remove();
if (point.length < 9) {
$('.tr-inpost-box .inpostship-main').css({
'border': '1px solid #999'
});
$('.point-info').remove();
var week = 0;
if ($('.weekPack').is(':checked')){
week = 1;
}
$.ajax({
type: 'POST',
headers: {"cache-control": "no-cache"},
url: baseDir + 'modules/inpostship/setPoint.php',
async: false,
cache: false,
dataType: 'json',
data: 'id_cart=' + id_cart
+ '&id_carrier=' + id_carrier
+ '&point=' + point
+ '&week='+week,
success: function (jsonData) {
$('.saveInfo').remove();
if (jsonData === false) {
$('.tr-inpost-box .inpostship-main').css({
'border': '3px solid #ff0000'
});
$('.tr-inpost-box .inpostship-main').append('<small class="saveInfo text-danger">Paczkomat nie został poprawnie zapisany</small>');
} else {
$('.tr-inpost-box .inpostship-main').css({
'border': '3px solid #67A901'
});
$('.tr-inpost-box .inpostship-main').append('<p class="point-info">Wybrany paczkomat:<br /><b>' + jsonData.point_code + ', ul. ' + jsonData.point_address1 + ', ' + jsonData.point_address2 + '</b><br />' + jsonData.point_desc + '</p>');
selectedPointInfo = jsonData.point_code;
if (selectedPointInfo != '') {
$('.tr-inpost-box .inpostship-main').append('<small class="saveInfo text-success">Paczkomat został poprawnie zapisany</small>');
} else {
$('.tr-inpost-box .inpostship-main').append('<small class="saveInfo text-danger">Paczkomat nie został poprawnie zapisany</small>');
}
}
setTimeout(function() {
$('.saveInfo').remove();
}, 3000);
}
});
}
}
function updateInpostInfoCourier(id_cart, id_carrier) {
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: baseDir + 'modules/inpostship/setCourier.php',
async: false,
cache: false,
dataType: 'json',
data: 'id_cart='+id_cart+'&id_carrier='+id_carrier,
success: function(jsonData) {
}
});
}
var inpostCGV = function(obj){
if (typeof inpostkey === "undefined") {
inpostkey = [];
}
if (typeof inpostkey_cod === "undefined") {
inpostkey_cod = [];
}
if (typeof inpostkey_week === "undefined") {
inpostkey_week = [];
}
if (typeof inpostkey_week_cod === "undefined") {
inpostkey_week_cod = [];
}
id = parseInt($("input.delivery_option_radio:checked").val());
if (inpostkey.indexOf(id) > 0){
if (selectedPointInfo == '') {
$('#inpostshipmodal').modal();
$(obj).parent().removeClass('checked');
return false;
}
}
if (inpostkey_cod.indexOf(id) > 0) {
if (selectedPointInfo == '') {
$('#inpostshipmodal').modal();
$(obj).parent().removeClass('checked');
return false;
}
}
if (inpostkey_week.indexOf(id) > 0) {
if (selectedPointInfo == '') {
$('#inpostshipmodal').modal();
return false;
}
}
if (inpostkey_week_cod.indexOf(id) > 0) {
if (selectedPointInfo == '') {
$('#inpostshipmodal').modal();
return false;
}
}
};
var initInpost = function(elem){
if (typeof checkout === 'undefined') {
checkout = 'default';
}
if (typeof inpostkey === "undefined") {
inpostkey = [];
}
if (typeof inpostkey_cod === "undefined") {
inpostkey_cod = [];
}
if (typeof inpostkey_week === "undefined") {
inpostkey_week = [];
}
if (typeof inpostkey_week_cod === "undefined") {
inpostkey_week_cod = [];
}
inpostCGVUncheck();
id = parseInt(elem.val());
if (checkout == 'onepagecheckout') {
var idString = id.toString();
var id3 = idString.slice(0, -3)
var id2 = id3.slice(1);
id = parseInt(id2);
}
$('.tr-inpost-box').remove();
if (inpostkey.indexOf(id) > -1 || inpostkey_week.indexOf(id) > -1) {
if (opc == 1) {
if (box_pos == 1) {
elem.closest('tbody').append(createInpostBox(id, checkout));
} else {
var closest = '.delivery_options';
if (checkout == 'onepagecheckoutps') {
closest = '.delivery_option.selected'
}
elem.closest(closest).append(createInpostBox(id, checkout));
}
} else {
var closest = 'tbody';
if (checkout == 'onepagecheckoutps') {
closest = '.delivery_option'
}
elem.closest(closest).append(createInpostBox(id, checkout));
}
if (nps == 1) {
var obj = $('.inpostship-main');
$.ajax({
url: baseDirShop + 'modules/inpostship/ajax.php',
type: 'POST',
headers: {"cache-control": "no-cache"},
async: false,
cache: false,
dataType: 'json',
data: 'action=getUserPoints',
success: function (jsonData) {
if (jsonData) {
if (jsonData.points != '') {
var htmls = '<div class="inpostNear"><h4>Paczkomaty w pobliżu Twojego adresu:</h4>';
$.each(jsonData.points, function (index, value) {
htmls += '<div class="nearInpost" data-name="' + value.name + '" data-id="' + id + '" data-cart="' + cart + '"><p><img src="https://geowidget.easypack24.net/images/desktop/icons/parcel_locker.png?4.12.8" /><b>' + value.name + '</b><br />' + value.address + '<br />' + value.desc + '</p></div>';
});
htmls += '</div>';
obj.append(htmls);
}
}
}
});
}
if (selectedPointInfo != '' || selectedPointInfo != 'undefined') {
$('.tr-inpost-box .inpostship-main').append('<p class="point-info"></p>');
selectedPointInfo = selectedPointInfo.replace('|| ', '<br />').replace(';;', '<b>').replace(';; ', '</b>').replace('||', '<br />');
$('.tr-inpost-box .inpostship-main .point-info').html(selectedPointInfo);
}
} else if (inpostkey_cod.indexOf(id) > -1 || inpostkey_week.indexOf(id) > -1) {
if (opc == 1) {
if (box_pos == 1) {
elem.closest('tbody').append(createInpostBox(id, checkout));
} else {
var closest = '.delivery_options';
if (checkout == 'onepagecheckoutps') {
closest = '.delivery_option.selected'
}
elem.closest(closest).append(createInpostBox(id, checkout));
}
} else {
var closest = 'tbody';
if (checkout == 'onepagecheckoutps') {
closest = '.delivery_option'
}
elem.closest(closest).append(createInpostBox(id, checkout));
}
if (nps == 1) {
var obj = $('.inpostship-main');
$.ajax({
url: baseDirShop + 'modules/inpostship/ajax.php',
type: 'POST',
headers: {"cache-control": "no-cache"},
async: false,
cache: false,
dataType: 'json',
data: 'action=getUserPoints&cod=1',
success: function (jsonData) {
if (jsonData) {
if (jsonData.points != '') {
var htmls = '<div class="inpostNear"><h4>Paczkomaty w pobliżu Twojego adresu:</h4>';
$.each(jsonData.points, function (index, value) {
htmls += '<div class="nearInpost" data-name="' + value.name + '" data-id="' + id + '" data-cart="' + cart + '"><p><img src="https://geowidget.easypack24.net/images/desktop/icons/parcel_locker.png?4.12.8" /><b>' + value.name + '</b><br />' + value.address + '<br />' + value.desc + '</p></div>';
});
htmls += '</div>';
obj.append(htmls);
}
}
}
});
}
if (selectedPointInfo != '' || selectedPointInfo != 'undefined') {
$('.tr-inpost-box .inpostship-main').append('<p class="point-info"></p>');
selectedPointInfo = selectedPointInfo.replace('|| ', '<br />').replace(';;', '<b>').replace(';; ', '</b>').replace('||', '<br />');
$('.tr-inpost-box .inpostship-main .point-info').html(selectedPointInfo);
}
} else {
if (typeof courierkey === "undefined") {
courierkey = [];
}
if (typeof courierkey_cod === "undefined") {
courierkey_cod = [];
}
if (courierkey.indexOf(id) > -1) {
updateInpostInfoCourier(cart, id);
}
if (courierkey_cod.indexOf(id) > -1) {
updateInpostInfoCourier(cart, id);
}
}
}
function createInpostBox(idCarrier, checkout)
{
if (geoWidget == 4) {
var html = '';
if (isTable == 1) {
html += '<tr><td colspan="4">';
}
html += '<div class="row tr-inpost-box inpost-' + checkout + '"><div class="inpostship-main i-go"><div class="inpostship-main-box">';
html += '<table>' +
'<tbody>';
if (showWeek == 1) {
// html += '<tr><td colspan="2"><span class="custom-checkbox" style="float: left;margin-right: 15px;"><input type="checkbox" value="1" class="weekPack" /><span><i class="material-icons rtl-no-flip checkbox-checked"></i></span></span> Paczka w weekend (+' + weekPrice + ' zł)</td> </tr>';
}
html += '<tr>' +
'<td>' +
'<span class="btn btn-success btn-md btn-block openMap" onclick="openModalMap(' + idCarrier + ');">Wybierz paczkomat z mapy</span>' +
'</td>' +
'</tr>' +
'</tbody>' +
'</table>';
html += '</div><div id="easypack-widget"></div><div id="easypack-widget2"></div></div><br /></div>';
if (isTable == 1) {
html += '</td></tr>';
}
} else {
let config = 'parcelCollect';
if (cod == 1) {
config = 'parcelCollectPayment';
}
if (sandbox == 1) {
html = '<link rel="stylesheet" href="https://sandbox-easy-geowidget-sdk.easypack24.net/inpost-geowidget.css"/>'+
'<script src="https://sandbox-easy-geowidget-sdk.easypack24.net/inpost-geowidget.js" defer></script>';
} else {
html = '<link rel="stylesheet" href="https://geowidget.inpost.pl/inpost-geowidget.css"/>' +
'<script src="https://geowidget.inpost.pl/inpost-geowidget.js" defer></script>';
}
html += '<div class="inpostship-content row"><div class="inpostship-main-box"><span class="btn btn-success btn-md btn-block openMap" data-cod="'+cod+'">Wybierz paczkomat z mapy</span><div class="load-geo-box"></div></div></div>';
}
return html;
}
function afterPointSelected(point) {
updateInpostInfo(cart, id, point.name);
}
$(document).ready(function () {
$('.tr-inpost-box .inpostship-main .inpost-point').on('change', function(){
var vl = $(this).val();
if (vl != '' && vl != null && selectedPointInfo != '') {
var vla = vl.split(',');
updateInpostInfo(cart, inpostkey, vla[0]);
} else {
alert(alertPoint);
$('#cgv').removeAttr('checked');
$('#cgv').parent().removeClass('checked');
// updatePaymentMethodsDisplay();
return false;
}
});
$(document).on('click', '.nearInpost', function() {
var point = $(this).data('name');
var cart = $(this).data('cart');
var id = $(this).data('id');
updateInpostInfo(cart, id, point);
});
$(document).on('click', '.openMap', function() {
let config = 'parcelCollect',
cod = $(this).data('cod');
if (cod == 1) {
config = 'parcelCollectPayment';
}
let html = '<inpost-geowidget class="inpost-geowidget-box" onpoint="afterPointSelected" token="'+geoWidgetToken+'" language="pl" config="'+config+'"></inpost-geowidget>';
$('.load-geo-box').append(html);
})
});