Files
crm.e5.pl/modules/EcmProducts/javascript/stockAddress.js

200 lines
5.7 KiB
JavaScript
Raw Normal View History

2024-04-27 09:23:34 +02:00
function openProductCard(productId) {
window.open(`index.php?module=EcmProducts&action=DetailView&record=${productId}`).focus();
}
function searchByIndex(index) {
$("#index").val(index);
$("#productId").val(CODES.find(x => x.code === $("#index").val())?.id);
$("#searchByIndexForm").submit();
}
function addProductToAddress() {
if (!isProductValid("#addProductInput")) {
return;
}
if (!isAddressValid("#address")) {
return;
}
const productId = CODES.find(x => x.code === $("#addProductInput").val())?.id;
$("#loader").show();
$.ajax({
url: "index.php?module=EcmProducts&action=&action=stockAddress&ajax=addAddress&to_pdf=1&productId=" + productId + "&address=" + $("#address").val(),
}).done(function (data) {
$("#loader").hide();
var res = JSON.parse(data);
if (res.status === 'Error') {
alert('Błąd: ' + res.msg);
} else {
$("#searchByAddressForm").submit();
}
}).fail(() => {
$("#loader").hide();
alert('Błąd serwera.');
});
}
function removeProductFromAddress() {
if (!isAddressValid("#address")) {
return;
}
$("#loader").show();
$.ajax({
url: "index.php?module=EcmProducts&action=&action=stockAddress&ajax=removeAddress&to_pdf=1&address=" + $("#address").val(),
}).done(function (data) {
$("#loader").hide();
var res = JSON.parse(data);
if (res.status === 'Error') {
alert('Błąd: ' + res.msg);
} else {
$("#searchByAddressForm").submit();
}
}).fail(() => {
$("#loader").hide();
alert('Błąd serwera.')
});
}
function removeAddressFromProduct(address) {
$("#loader").show();
$.ajax({
url: "index.php?module=EcmProducts&action=&action=stockAddress&ajax=removeAddress&to_pdf=1&address=" + address,
}).done(function (data) {
$("#loader").hide();
var res = JSON.parse(data);
if (res.status === 'Error') {
alert('Błąd: ' + res.msg);
} else {
$("#searchByIndexTrigger").click();
}
}).fail(() => {
$("#loader").hide();
alert('Błąd serwera.');
});
}
function addAddressToProduct() {
if (!isProductValid("#index")) {
return;
}
const productId = CODES.find(x => x.code === $("#index").val())?.id;
if (!isAddressValid("#newAddress")) {
return;
}
const isNotFull = $("#isNotFull").is(':checked') ? 1 : 0;
$("#loader").show();
$.ajax({
url: "index.php?module=EcmProducts&action=&action=stockAddress&ajax=addAddress&to_pdf=1&productId=" + productId + "&address=" + $("#newAddress").val() + "&isNotFull=" + isNotFull,
}).done(function (data) {
$("#loader").hide();
var res = JSON.parse(data);
if (res.status === 'Error') {
alert('Błąd: ' + res.msg);
} else {
$("#searchByIndexTrigger").click();
}
}).fail(() => {
$("#loader").hide();
alert('Błąd serwera.')
});
}
function setAddressIsNotFull(address, checkbox) {
$.ajax({
url: "index.php?module=EcmProducts&action=&action=stockAddress&ajax=setIsNotFull&to_pdf=1&address=" + address + "&isNotFull=" + $(checkbox).is(':checked'),
}).done(function (data) {
$("#loader").hide();
var res = JSON.parse(data);
console.log(res);
if (res.status === 'Error') {
alert('Błąd: ' + res.msg);
}
}).fail(() => {
$("#loader").hide();
alert('Błąd serwera.')
});
}
function isAddressValid(id) {
if (!id) { id = "#address"; }
if ($(id).val().length < 9) {
$(id + "Error").html("Błędny format [ __.__._._ ]");
return false;
}
const segments = $(id).val().split(".");
if (parseInt(segments[0]) > 17) {
$(id + "Error").html("Błąd pierwszego segmentu [zakres 01-20]");
return false;
}
if (parseInt(segments[1]) > 15) {
$(id + "Error").html("Błąd drugiego segmentu [zakres 01-15]");
return false;
}
return true;
}
function isProductValid(id) {
if (!id) { id = "#index"; }
const index = $(id).val();
if (CODES.find(x => x.code === index)) {
return true;
} else {
$(id + "Error").html("Niepoprawny indeks produktu [Produkt musi być aktywny]");
return false;
}
}
$(document).ready(function () {
$("#address").mask("AB.CD.E.F", {
placeholder: "__.__._._",
translation: {
A: { pattern: /[0-2]/ },
B: { pattern: /[0-9]/ },
C: { pattern: /[0-1]/ },
D: { pattern: /[0-9]/ },
E: { pattern: /[1-5]/ },
F: { pattern: /[0-5]/ },
}
});
if ($("#newAddress")) {
$("#newAddress").mask("AB.CD.E.F", {
placeholder: "__.__._._",
translation: {
A: { pattern: /[0-2]/ },
B: { pattern: /[0-9]/ },
C: { pattern: /[0-1]/ },
D: { pattern: /[0-9]/ },
E: { pattern: /[1-5]/ },
F: { pattern: /[0-5]/ },
}
});
$("#newAddress").focus(() => $("#newAddressError").html(""));
$("#newAddress").blur(() => isAddressValid("#newAddress"));
}
$("#address").focus(() => $("#addressError").html(""));
$("#index").focus(() => $("#indexError").html(""));
$("#index").autocomplete({
source: CODES.map(x => x.code),
minLength: 4,
max: 20,
scroll: true
});
if ($("#addProductInput")) {
$("#addProductInput").autocomplete({
source: CODES.map(x => x.code),
minLength: 4,
max: 20,
scroll: true
});
$("#addProductInput").focus(() => $("#addProductInputError").html(""));
}
$("#searchByAddressTrigger").click(() => {
if (isAddressValid()) {
$("#searchByAddressForm").submit();
}
});
$("#searchByIndexTrigger").click(() => {
if (isProductValid()) {
$("#productId").val(CODES.find(x => x.code === $("#index").val())?.id);
$("#searchByIndexForm").submit();
}
});
$('[name^="showAddress').click((event) => {
$("#address").val(event.target.name.split('-')[1]);
if (isAddressValid()) {
$("#searchByAddressForm").submit();
}
});
//bind buttons
});