File: /var/www/vhost/disk-apps/teamdemo.sports-crowd.com/public/js/products.js
function convertTime12to24(time12h) {
const [time, modifier] = time12h.split(" ");
let [hours, minutes] = time.split(":");
if (hours === "12") {
hours = "00";
}
if (modifier === "PM") {
hours = parseInt(hours, 10) + 12;
}
return hours + ":" + minutes;
}
function createProduct() {
$("#createProduct").validator("update");
$("#createProduct")
.validator("update")
.on("submit", function (e) {
e.preventDefault();
var state;
if ($("#chkAgeProduct").is(":checked")) {
state = 1;
} else {
state = 0;
}
var brand = $("#createProduct select[name='brand_id']").val();
var sub_categories = $("#createProduct select[name='subcategory_id']").val();
var sucursal_id = $("#createProduct select[name='sucursal_id']").val();
if (!sucursal_id) {
sucursal_id = null;
}
if (!brand || !sub_categories) {
swal("Warning", Lang.get("messagesClient.product_tag4"), "warning");
return false;
} else {
var percentage_discount = $(" #createProduct input[name='percentage_discount']").val();
var start_discount = $(" #createProduct input[name='start_discount']").val();
var limit_discount = $(" #createProduct input[name='limit_discount']").val();
var limit_hour_discount = convertTime12to24(
$("#createProduct input[name='limit_hour_discount']").val()
);
if (percentage_discount == "") {
percentage_discount = null;
start_discount = null;
limit_discount = null;
limit_hour_discount = null;
} else if (
start_discount == "" ||
limit_discount == "" ||
limit_hour_discount == "" ||
limit_hour_discount == ":undefined"
) {
swal("Warning", Lang.get("messagesClient.product_tag9"), "warning");
return false;
}
var flash_price = $(" #createProduct input[name='flash_price']").val();
var start_flash_discount = $(" #createProduct input[name='start_flash_discount']").val();
var limit_flash_discount = $(" #createProduct input[name='limit_flash_discount']").val();
var limit_hour_flash_discount = convertTime12to24(
$("#createProduct input[name='limit_hour_flash_discount']").val()
);
if (flash_price == "") {
flash_price = null;
start_flash_discount = null;
limit_flash_discount = null;
limit_hour_flash_discount = null;
} else if (
start_flash_discount == "" ||
limit_flash_discount == "" ||
limit_hour_flash_discount == "" ||
limit_hour_flash_discount == ":undefined"
) {
swal("Warning", Lang.get("messagesClient.product_tag10"), "warning");
return false;
}
if ($("#createProduct select[name='tags']").val() && $("#createProduct select[name='tags']").val().length && !$("#createProduct input[name='special_price']").val()) {
swal("Warning", "Debe ingresar el precio de segmentación", "warning");
return;
}
var info = {
name: $("#createProduct input[name='name']").val(),
plu: $("#createProduct input[name='plu']").val(),
bar_code: $("#createProduct input[name='bar_code']").val(),
price: $("#createProduct input[name='price']").val(),
available_units: $("#createProduct input[name='available_units']").val(),
max_units_per_order: $("#createProduct input[name='max_units']").val(),
max_units_per_product: $("#createProduct input[name='max_units_product']").val(),
percentage_discount: percentage_discount,
start_discount: start_discount,
limit_discount: limit_discount,
limit_hour_discount: limit_hour_discount,
flash_price: flash_price,
start_flash_discount: start_flash_discount,
limit_flash_discount: limit_flash_discount,
limit_hour_flash_discount: limit_hour_flash_discount,
brand_id: brand,
sub_categories: sub_categories,
order: $("#createProduct input[name='order']").val(),
chkAgeProduct: state,
sucursal_id: sucursal_id,
product_link: $("#createProduct input[name='product_link']").val(),
tags: $("#createProduct select[name='tags']").val(),
special_price: $("#createProduct input[name='special_price']").val(),
store_type: localStorage.getItem("storeType"),
tags_assigns: $("#createProduct select[name='tags_assigns']").val(),
};
$("#btn-create-product").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
swal(Lang.get("messagesClient.product_tag5"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$.ajax({
url: "/products/create",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $(
'meta[name="csrf-token"]'
).attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr('href', '/products/' + localStorage.getItem("storeType"));
}
});
} else {
swal("Error", r.m, "error");
$("#btn-create-product").removeClass(
"displayNone"
);
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.product_tag6"), "error");
$("#btn-create-product").removeClass(
"displayNone"
);
$("#spinnerButton").addClass("displayNone");
},
});
} else {
$("#btn-create-product").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
});
}
});
}
function clickAttributes(value) {
_table = $("#example2").DataTable();
index = _table.page();
localStorage.setItem("productsTableIndex", index);
$(location).attr("href", "/productAttributes/" + value + "/product");
}
function clickEditProduct(value) {
_table = $("#example2").DataTable();
index = _table.page();
localStorage.setItem("productsTableIndex", index);
$(location).attr("href", "/products/" + localStorage.getItem("storeType") + "/edit/" + value);
}
function editProduct() {
$("#editProduct").validator("update");
$("#editProduct")
.validator("update")
.on("submit", function (e) {
e.preventDefault();
var state;
if ($("#chkAgeProduct").is(":checked")) {
state = 1;
} else {
state = 0;
}
var brand = $("#editProduct select[name='brand_id']").val();
var sub_categories = $("#editProduct select[name='subcategory_id']").val();
var sucursal_id = $("#editProduct select[name='sucursal_id']").val();
if (!sucursal_id) {
sucursal_id = null;
}
var limit_hour_discount = convertTime12to24(
$("#editProduct input[name='limit_hour_discount']").val()
);
var limit_hour_flash_discount = convertTime12to24(
$("#editProduct input[name='limit_hour_flash_discount']").val()
);
if (limit_hour_discount == ":undefined") {
limit_hour_discount = null;
}
if (limit_hour_flash_discount == ":undefined") {
limit_hour_flash_discount = null;
}
if ($("#editProduct select[name='tags']").val() && $("#editProduct select[name='tags']").val().length && !$("#editProduct input[name='special_price']").val()) {
swal("Warning", "Debe ingresar el precio de segmentación", "warning");
return;
}
if (brand == "" || sub_categories == "") {
swal("Warning", "Debe seleccionar una marca y una categoría", "warning");
return false;
} else {
var info = {
id: $("#btn-edit-product").attr("data-id"),
name: $("#editProduct input[name='name']").val(),
plu: $("#editProduct input[name='plu']").val(),
bar_code: $("#editProduct input[name='bar_code']").val(),
price: $("#editProduct input[name='price']").val(),
available_units: $("#editProduct input[name='available_units']").val(),
max_units_per_order: $("#editProduct input[name='max_units']").val(),
max_units_per_product: $("#editProduct input[name='max_units_product']").val(),
percentage_discount: $("#editProduct input[name='percentage_discount']").val(),
start_discount: $("#editProduct input[name='start_discount']").val(),
limit_discount: $("#editProduct input[name='limit_discount']").val(),
limit_hour_discount: limit_hour_discount,
flash_price: $("#editProduct input[name='flash_price']").val(),
start_flash_discount: $("#editProduct input[name='start_flash_discount']").val(),
limit_flash_discount: $("#editProduct input[name='limit_flash_discount']").val(),
limit_hour_flash_discount: limit_hour_flash_discount,
brand_id: brand,
sub_categories: sub_categories,
order: $("#editProduct input[name='order']").val(),
chkAgeProduct: state,
sucursal_id: sucursal_id,
product_link: $("#editProduct input[name='product_link']").val(),
tags: $("#editProduct select[name='tags']").val(),
special_price: $("#editProduct input[name='special_price']").val(),
store_type: localStorage.getItem("storeType"),
tags_assigns: $("#editProduct select[name='tags_assigns']").val(),
};
$("#btn-edit-product").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
swal(Lang.get("messagesClient.product_tag7"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$.ajax({
url: "/products/update",
type: "PUT",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $(
'meta[name="csrf-token"]'
).attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
localStorage.setItem(
"setPageTable",
true
);
$(location).attr("href", "/products/" + localStorage.getItem("storeType"));
}
});
} else {
swal("Error", r.m, "error");
$("#btn-edit-product").removeClass(
"displayNone"
);
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.product_tag8"), "error");
$("#btn-edit-product").removeClass(
"displayNone"
);
$("#spinnerButton").addClass("displayNone");
},
});
} else {
$("#btn-edit-product").prop("disabled", false);
$("#btn-edit-product").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
});
}
});
}
/**Activar/Desactivar Banner usando switch*/
function chkProduct(value) {
var state;
if ($("#Checkactive" + value).is(":checked")) {
state = 1;
} else {
state = 0;
}
var info = {
id: value,
state: state,
};
$.ajax({
url: "/products/active",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal("Ok", r.m);
} else {
swal("Error", r.m, "error");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.banner_tag7"), "error");
},
});
}
/**Método para eliminar banner*/
function clickDeleteProduct(value, productImagesName) {
swal(Lang.get("messagesClient.product_tag2"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
var info = {
id: value,
product_name: productImagesName,
};
$.ajax({
url: "/products/delete",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr('href', '/products/' + localStorage.getItem("storeType"));
}
});
} else {
swal("Error", r.m, "error");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.product_tag3"), "error");
},
});
}
});
}
/**Método para eliminar solamente la imagen*/
function clickDeleteProductImage(value, imageToDelete) {
swal(Lang.get("messagesClient.product_tag11"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
var info = {
id: value,
product_name: imageToDelete,
};
$.ajax({
url: "/products/deleteImage",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr('href', '/products/' + localStorage.getItem("storeType"));
}
});
} else {
swal("Error", r.m, "error");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.product_tag12"), "error");
},
});
}
});
}
function importFileProduct() {
var fExcel = document.getElementById("importProducts");
fExcel = fExcel.files[0];
if (fExcel) {
var sucursal_id = $("#sucursal_id").val();
if (sucursal_id === null) {
alert("Debes seleccionar una sucursal.");
}
var formData = new FormData();
formData.append("importProducts", fExcel);
formData.append("storeType", localStorage.getItem("storeType"));
if (sucursal_id) {
formData.append("sucursal_id", sucursal_id);
}
$("#button-import-product").addClass("displayNone");
$("#spinnerButtonProduct").removeClass("displayNone");
$.ajax({
type: "POST",
dataType: "json",
processData: false,
contentType: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
data: formData,
url: "/products/import",
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr('href', '/products/' + localStorage.getItem("storeType"));
}
});
} else {
swal("Error", r.m, "error");
$("#button-import-product").removeClass("displayNone");
$("#spinnerButtonProduct").addClass("displayNone");
}
},
});
} else {
alert("No hay archivo.");
}
}
function importFileProductsExternal() {
var fExcel = document.getElementById("importProductsExternal");
fExcel = fExcel.files[0];
if (fExcel) {
var sucursal_id = $("#statusSucursalProducts").val();
var formData = new FormData();
formData.append("importProductsExternal", fExcel);
formData.append("sucursal_id", sucursal_id);
$("#button-import-product-external").addClass("displayNone");
$("#spinnerButtonProductExternal").removeClass("displayNone");
$.ajax({
type: "POST",
dataType: "json",
processData: false,
contentType: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
data: formData,
url: "/products/importExternal",
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr('href', '/products/' + localStorage.getItem("storeType"));
}
});
} else {
swal("Error", Lang.get("Error al crear productos, por favor verifique el contenido de la plantilla"), "error");
$("#button-import-product").removeClass("displayNone");
$("#spinnerButtonProduct").addClass("displayNone");
}
},
});
} else {
alert("No hay archivo.");
}
}
function importFileCategories() {
var fExcel = document.getElementById("importCategories");
fExcel = fExcel.files[0];
if (fExcel) {
var sucursal_id = $("#categories_sucursal_id").val();
if (sucursal_id === null) {
alert("Debes seleccionar una sucursal.");
}
var formData = new FormData();
formData.append("importCategories", fExcel);
if (sucursal_id) {
formData.append("sucursal_id", sucursal_id);
}
$("#button-import-category").addClass("displayNone");
$("#spinnerButtonCategory").removeClass("displayNone");
$.ajax({
type: "POST",
dataType: "json",
processData: false,
contentType: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
data: formData,
url: "/products/importCatAndSub",
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr('href', '/products/' + localStorage.getItem("storeType"));
}
});
} else {
$("#button-import-category").removeClass("displayNone");
$("#spinnerButtonCategory").addClass("displayNone");
swal("Error", Lang.get(r.m), "error");
}
},
});
} else {
alert("No hay archivo.");
}
}
// llama el modal
function clickScraper(id) {
var value = $("#scraper" + id).attr("data-text");
$("#scraperTable").dataTable().fnDestroy();
$("#tbody-scraper").empty();
$("#input-scraper").val(value);
$("#modal_scraper").modal();
}
function searchScraper() {
$("#scraperTable").dataTable().fnDestroy();
var value = $("#input-scraper").val();
if (value.length > 0) {
$.ajax({
url: "/finder/word/" + value + "/F",
type: "GET",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"), "Content-Type": "application/json",
},
success: function (r) {
if (r.r) {
fillTableScraper(value);
}
},
});
}
}
function fillTableScraper(word) {
$.ajax({
url: "/result/word/" + word,
type: "GET",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
success: function (r) {
if (r) {
for (let i = 0; i < r.length; i++) {
tr = `<tr><td>${r[i].word}</td><td>${r[i].store}</td><td>${r[i].word_store}</td><td>${r[i].price_store}</td></tr>`;
$("#tbody-scraper").append(tr);
}
reloadTableScraper();
}
},
});
}
function reloadTableScraper() {
$("#scraperTable").DataTable({
paging: true,
lengthChange: true,
searching: true,
ordering: true,
info: true,
autoWidth: true,
language: {
url: window.DATATABLES_LANGUAGE_URL,
},
});
}
// metodos para no permitir el ingreso de puntos en los precios
$(function () {
$("#price").keyup(function () {
// this.value = (this.value + "").replace(/[^0-9]/g, "");
});
$("#flash_price").keyup(function () {
// this.value = (this.value + "").replace(/[^0-9]/g, "");
});
$("#sync_shopify").click(function () {
swal("¿Está seguro que desea sincronizar el catálogo con shopify?", {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$("#sync_shopify").addClass("hide");
$("#sync_shopify_spin").removeClass("hide");
$.ajax({
type: "GET",
dataType: "json",
processData: false,
contentType: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
"content"
),
},
url: "/shopify/syncProducts",
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr('href', '/products/' + localStorage.getItem("storeType"));
}
});
} else {
$("#sync_shopify").removeClass("hide");
$("#sync_shopify_spin").addClass("hide");
swal(r.m, { icon: "danger" });
}
},
});
}
});
});
$("#sync_woocommerce").click(function () {
swal(
"¿Está seguro que desea sincronizar el catálogo con woocommerce?",
{
buttons: {
cancel: "No",
Ok: true,
},
}
).then((val) => {
if (val == "Ok") {
$("#sync_woocommerce").addClass("hide");
$("#sync_woocommerce_spin").removeClass("hide");
$.ajax({
type: "GET",
dataType: "json",
processData: false,
contentType: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
"content"
),
},
url: "/woocommerce/syncProducts",
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr('href', '/products/' + localStorage.getItem("storeType"));
}
});
} else {
$("#sync_woocommerce").removeClass("hide");
$("#sync_woocommerce_spin").addClass("hide");
swal(r.m, { icon: "danger" });
}
},
});
}
});
});
});
function setPageTable() {
localStorage.setItem("setPageTable", true);
$(location).attr('href', '/products/' + localStorage.getItem("storeType"));
}