File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/public/js/inventory_tweaks.js
function create() {
$("#form_create").validator("update");
$("#form_create")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
let product_id = $(
"#form_create select[name='product_id']"
).val();
let attribute_select = $(
" #form_create select[name='product_attribute_id'] option"
);
let inventory_movement_type_id = $(
"#inventory_movement_type_id"
).val();
inventory_movement_type_id = parseInt(
inventory_movement_type_id
);
// Validar que se indique un producto
if (!product_id) {
swal(
"Error",
Lang.get(
"messagesClient.inventory_tweaks.error_product"
),
"error"
);
return;
}
if (!inventory_movement_type_id) {
swal(
"Error",
Lang.get(
"messagesClient.inventory_tweaks.error_inventory_type_id"
),
"error"
);
return;
}
// Validar que en caso de poderse, se indique un atributo.
if (attribute_select.length > 0) {
if (
!$(
" #form_create select[name='product_attribute_id']"
).val()
) {
swal(
"Error",
Lang.get(
"messagesClient.inventory_tweaks.error_product_attribute"
),
"error"
);
return;
}
}
$("#btn-create").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
var info = {
product_id,
product_attribute_id: $(
" #form_create select[name='product_attribute_id']"
).val(),
quantity: $("#form_create input[name='quantity']").val(),
description: $(
"#form_create input[name='description']"
).val(),
product_attribute_value: $("#product_attribute_id")
.find(":selected")
.data("value"),
product_attribute_sku: $("#product_attribute_id")
.find(":selected")
.data("sku"),
product_attribute_real_id: $("#product_attribute_id")
.find(":selected")
.data("product-attribute-id"),
inventory_movement_type_id,
};
swal(Lang.get("messagesClient.inventory_tweaks.create"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$.ajax({
url: "/inventory_tweaks",
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) {
if (r.type == "error") {
swal(
"Advertencia",
"No se puede modificar esa cantidad",
"warning"
);
$("#btn-create").removeClass(
"displayNone"
);
$("#spinnerButton").addClass(
"displayNone"
);
return;
} else {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr(
"href",
"/inventory_tweaks"
);
}
});
}
} else {
swal(r.m, "error");
$("#btn-create").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal("¡Hubo un Error al crear!", "error");
$("#btn-create").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
},
});
} else {
$("#btn-create").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
});
}
});
}
function findAttributes() {
var product_id = $("#product_id").val();
// Elimino atributos anteriores.
var selectProductAttributes = $("#product_attribute_id");
selectProductAttributes.empty();
$.ajax({
url: "/inventory_tweaks/attributes_product/" + product_id,
type: "GET",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
success: function (r) {
if (r.length > 0) {
for (var i = 0; i < r.length; i++) {
selectProductAttributes.append(
`<option value="${r[i].attribute.id}" data-value="${r[i].value}" data-sku="${r[i].sku}" data-product-attribute-id="${r[i].id}">${r[i].sku} - ${r[i].attribute.name} - ${r[i].value}</option>`
);
}
} else {
selectProductAttributes.empty();
}
},
});
}