255 lines
7.4 KiB
JavaScript
255 lines
7.4 KiB
JavaScript
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]) > 99) {
|
|
$(id + "Error").html("Błąd pierwszego segmentu [zakres 01-99]");
|
|
return false;
|
|
}
|
|
if (parseInt(segments[1]) > 99) {
|
|
$(id + "Error").html("Błąd drugiego segmentu [zakres 01-99]");
|
|
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;
|
|
}
|
|
}
|
|
function exportFile() {
|
|
$("#loader").show();
|
|
$.ajax({
|
|
url: "index.php?module=EcmProducts&action=&action=stockAddress&ajax=export&to_pdf=1",
|
|
}).done(function (data) {
|
|
$("#loader").hide();
|
|
if (data.status === 'Error') {
|
|
alert('Błąd: ' + data.msg);
|
|
} else {
|
|
console.log(data);
|
|
downloadFile(data.fileContent, data.fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
}
|
|
}).fail(() => {
|
|
$("#loader").hide();
|
|
alert('Błąd serwera.');
|
|
});
|
|
}
|
|
$(document).ready(function () {
|
|
$("#address").mask("AB.CD.E.F", {
|
|
placeholder: "__.__._._",
|
|
translation: {
|
|
A: { pattern: /[0-9]/ },
|
|
B: { pattern: /[0-9]/ },
|
|
C: { pattern: /[0-9]/ },
|
|
D: { pattern: /[0-9]/ },
|
|
E: { pattern: /[1-9]/ },
|
|
F: { pattern: /[0-9]/ },
|
|
}
|
|
});
|
|
if ($("#newAddress")) {
|
|
$("#newAddress").mask("AB.CD.E.F", {
|
|
placeholder: "__.__._._",
|
|
translation: {
|
|
A: { pattern: /[0-9]/ },
|
|
B: { pattern: /[0-9]/ },
|
|
C: { pattern: /[0-9]/ },
|
|
D: { pattern: /[0-9]/ },
|
|
E: { pattern: /[1-9]/ },
|
|
F: { pattern: /[0-9]/ },
|
|
}
|
|
});
|
|
$("#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();
|
|
}
|
|
});
|
|
$("#exportTrigger").click(() => {
|
|
exportFile();
|
|
});
|
|
$('[name^="showAddress').click((event) => {
|
|
$("#address").val(event.target.name.split('-')[1]);
|
|
if (isAddressValid()) {
|
|
$("#searchByAddressForm").submit();
|
|
}
|
|
});
|
|
//bind buttons
|
|
});
|
|
|
|
function downloadFile(base64Data, fileName, mimeType) {
|
|
// Stwórz blob z danych base64
|
|
const byteCharacters = atob(base64Data);
|
|
const byteArrays = [];
|
|
|
|
for (let offset = 0; offset < byteCharacters.length; offset += 512) {
|
|
const slice = byteCharacters.slice(offset, offset + 512);
|
|
|
|
const byteNumbers = new Array(slice.length);
|
|
for (let i = 0; i < slice.length; i++) {
|
|
byteNumbers[i] = slice.charCodeAt(i);
|
|
}
|
|
|
|
const byteArray = new Uint8Array(byteNumbers);
|
|
byteArrays.push(byteArray);
|
|
}
|
|
|
|
const blob = new Blob(byteArrays, {type: mimeType});
|
|
|
|
// Stwórz link do pobrania
|
|
const link = document.createElement('a');
|
|
link.href = window.URL.createObjectURL(blob);
|
|
link.download = fileName;
|
|
|
|
// Dodaj link do dokumentu i kliknij go
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
|
|
// Usuń link z dokumentu
|
|
setTimeout(function () {
|
|
document.body.removeChild(link);
|
|
window.URL.revokeObjectURL(link.href);
|
|
}, 100);
|
|
}
|