home/autoph/public_html/projects/api/public/assets/js/catalog/form.js 0000644 00000206707 15025005130 0022012 0 ustar 00 /*
* Author: Clarence A Andaya
* Date: 31 Mar 2022
*/
//Only const Variable Here
(function () {
"use strict";
//Logic,Variables, Functions Here
var variation_list = [];
var variation_images = [];
var product_images = [];
var variation_attributes = [];
var selected_variation_key = null;
var myDzProductPhotos = null;
var myDzProductMainPhotos = null;
var myVariationList = null;
// Global default options
Dropzone.autoDiscover = false;
var selected_attributes = [];
function dzProductPhotos() {
myDzProductPhotos = new Dropzone("#dz_product_photo", {
url: "/admin/storage/upload", // If not using a form element
acceptedFiles: ".png,.jpg,.jpeg,.gif", //allowed filetypes
maxFilesize: 8,
maxFiles: 20,
clickable: true,
addRemoveLinks: true,
dictCancelUpload: "",
removedfile: function (file) {
// console.log(file);
if (selected_variation_key != null) {
// load images
//remove in varialist image array
$.each(
variation_list[selected_variation_key].images,
function (i, v) {
if (v.id == file.id) {
delete variation_list[selected_variation_key].images[i];
// clean and refresh variations attributes
variation_list[selected_variation_key].images = filterArray(
variation_list[selected_variation_key].images
);
}
}
);
//remove in current variation_images array only for already uploaded image (mock image)
$.each(variation_images, function (k, j) {
if (j.id == file.id) {
delete variation_images[k];
variation_images = filterArray(variation_images);
}
});
}
if (typeof file.upload != "undefined") {
$.each(variation_images, function (k, j) {
// console.log(j.uuid + ' ' + file.upload.uuid);
if (j.uuid == file.upload.uuid) {
delete variation_images[k];
variation_images = filterArray(variation_images);
}
});
}
//remove in variation_images array only for newly upload
// $.ajax({
// type: 'POST',
// url: '/admin/storage/remove',
// dataType: 'json',
// data: {
// id_or_name: file.upload.filename,
// dir: "products",
// csrf_token: $('#csrf_token').val()
// },
// sucess: function(data) {
// console.log('success: ' + data);
// }
// });
var _ref;
return (_ref = file.previewElement) != null
? _ref.parentNode.removeChild(file.previewElement)
: void 0;
},
init: function () {
this.on("maxfilesexceeded", function (file) {
// $("#proceed-button-vehicle").attr("disabled", false);
toast("error", "Only 20 images.");
$("#btn_save_variation").prop("disabled", false);
return 0;
});
this.on("error", function (file) {
toast("error", "Error has occured.");
$("#btn_save_variation").prop("disabled", false);
return;
});
this.on("success", function (file, response) {
if (response.status == 0) {
toast("error", response.message);
return false;
}
variation_images.push({
uuid: file.upload.uuid,
id: response.id,
path: response.path,
size: response.size,
name: response.name,
});
$("#btn_save_variation").prop("disabled", false);
// console.log(variation_images);
});
this.on("sending", function (file, xhr, formData) {
formData.append("dir", "products");
formData.append("csrf_token", $("#csrf_token").val());
});
this.on("addedfiles", function (files) {
$("#btn_save_variation").prop("disabled", true);
});
},
});
}
function dzProductPhotosMain() {
myDzProductMainPhotos = new Dropzone("#catalog_dz_product_photo", {
url: "/admin/storage/upload", // If not using a form element
acceptedFiles: ".png,.jpg,.jpeg,.gif", //allowed filetypes
maxFilesize: 8,
maxFiles: 3,
clickable: true,
addRemoveLinks: true,
dictCancelUpload: "",
removedfile: function (file) {
// console.log(file);
if (typeof file.upload != "undefined") {
$.each(product_images, function (k, j) {
if (j.uuid == file.upload.uuid) {
delete product_images[k];
product_images = filterArray(product_images);
}
});
} else {
//remove in current product_images array only for already uploaded image (mock image)
$.each(product_images, function (k, j) {
if (j.id == file.id) {
delete product_images[k];
product_images = filterArray(product_images);
}
});
}
var _ref;
return (_ref = file.previewElement) != null
? _ref.parentNode.removeChild(file.previewElement)
: void 0;
},
init: function () {
this.on("maxfilesexceeded", function (file) {
// $("#proceed-button-vehicle").attr("disabled", false);
toast("error", "Only 20 images.");
$("#btn_save_product").prop("disabled", false);
return 0;
});
this.on("error", function (file) {
toast("error", "Error has occured.");
$("#btn_save_product").prop("disabled", false);
return;
});
this.on("success", function (file, response) {
if (response.status == 0) {
toast("error", response.message);
return false;
}
product_images.push({
uuid: file.upload.uuid,
id: response.id,
path: response.path,
size: response.size,
name: response.name,
});
$("#btn_save_product").prop("disabled", false);
});
this.on("sending", function (file, xhr, formData) {
formData.append("dir", "products");
formData.append("csrf_token", $("#csrf_token").val());
});
this.on("addedfiles", function (files) {
$("#btn_save_product").prop("disabled", true);
});
},
});
}
function isDuplicateObject(object1, object2, key) {
var uniqueResultOne = object1.filter(function (obj) {
return !object2.some(function (obj2) {
return obj[key] == obj2[key];
});
});
//Find values that are in obj2 but not in obj1
var uniqueResultTwo = object2.filter(function (obj) {
return !object1.some(function (obj2) {
return obj[key] == obj2[key];
});
});
//Combine the two arrays of unique entries
var result = uniqueResultOne.concat(uniqueResultTwo);
if (result.length) {
return false;
} else {
return true;
}
}
function isBelowZero($el, msg, focus = true) {
if ($el.val() != "") {
if (parseInt($el.val()) < 0) {
toast("error", msg);
if (focus) {
$el.trigger("focus");
}
return true;
}
}
return false;
}
function isBelowN($el, msg, num, focus = true) {
if ($el.val() != "") {
if (parseInt($el.val()) < num) {
toast("error", msg);
if (focus) {
$el.trigger("focus");
}
return true;
}
}
return false;
}
function checkDuplicateVariation() {
var isDuplicateVariation = false;
$.each(variation_list, function (i, v) {
var result1 = JSON.parse(JSON.stringify(v.attributes));
$.each(variation_list, function (j, k) {
if (i == j) {
return;
}
var result2 = JSON.parse(JSON.stringify(k.attributes));
if (isDuplicateObject(result1, result2, "child_id")) {
isDuplicateVariation = true;
}
});
});
return isDuplicateVariation;
}
function checkDuplicateAddVariation() {
var isDuplicateVariation = false;
var result1 = JSON.parse(JSON.stringify(variation_attributes));
$.each(variation_list, function (i, v) {
if (selected_variation_key != null) {
if (i == selected_variation_key) {
return;
}
}
var result2 = JSON.parse(JSON.stringify(v.attributes));
if (isDuplicateObject(result1, result2, "child_id")) {
isDuplicateVariation = true;
}
});
return isDuplicateVariation;
}
function variationForm() {
//clean attributes array (remove empty elements)
var filtered = filterArray(selected_attributes);
if (!filtered.length) {
toast("error", "No attributes selected.");
return false;
}
//ajax calling the variation form
$.ajax({
url: "/admin/products/variation",
data: {
attributes: filtered,
csrf_token: $("#csrf_token").val(),
},
type: "GET",
dataType: "json",
beforeSend: function () {
variation_images = [];
variation_attributes = [];
$(".modal_container").empty();
$("#btn_add_variation").prop("disabled", true);
},
success: function (result) {
$(".modal_container").html(result.data);
//show variant form
$("#modal_variation").modal("show");
//fill data to form if is update
if (selected_variation_key != null) {
// console.log(variation_list[selected_variation_key]);
//load fields
$.each(variation_list[selected_variation_key], function (i, v) {
if (!Array.isArray(v)) {
// alert('input[name="' + i + '"]');
$('input[name="' + i + '"]').val(v);
}
});
//load locations
var prod_loc = variation_list[selected_variation_key];
if (typeof prod_loc.warehouse != "undefined") {
var whOption = new Option(
prod_loc.warehouse.title,
prod_loc.warehouse.id,
true,
true
);
$('select[name="warehouse"]').append(whOption).trigger("change");
}
if (typeof prod_loc.floor != "undefined") {
var whOption = new Option(
prod_loc.floor.title,
prod_loc.floor.id,
true,
true
);
$('select[name="floor"]').append(whOption).trigger("change");
}
if (typeof prod_loc.rack != "undefined") {
var whOption = new Option(
prod_loc.rack.title,
prod_loc.rack.id,
true,
true
);
$('select[name="rack"]').append(whOption).trigger("change");
}
if (typeof prod_loc.bin != "undefined") {
var whOption = new Option(
prod_loc.bin.title,
prod_loc.bin.id,
true,
true
);
$('select[name="bin"]').append(whOption).trigger("change");
}
//load attributes
$.each(
variation_list[selected_variation_key].attributes,
function (i, v) {
$(
'select[name="attribute-' + v.parent_name.toLowerCase() + '"]'
).val(v.child_id);
}
);
}
//Initialze dropzone
// dzProductPhotos("#dz_product_photo");
dzProductPhotos();
//load images
if (selected_variation_key != null) {
// load images
$.each(
variation_list[selected_variation_key].images,
function (i, v) {
variation_images.push({
id: v.id,
path: v.path,
size: v.size,
name: v.name,
});
var mockFile = {
id: v.id,
name: v.name, //v.path,
size: v.size, //'12345' //v.unreadable_size
};
myDzProductPhotos.emit("addedfile", mockFile);
myDzProductPhotos.emit("complete", mockFile);
myDzProductPhotos.emit(
"thumbnail",
mockFile,
APP_DISK + "thumbnails/" + v.path
);
myDzProductPhotos.files.push(mockFile); // here you add them into the files array
}
);
}
//initialize datepicker
$(".datetimepicker").datetimepicker({
format: "MM/DD/YYYY hh:mm A",
// minDate: moment().subtract(1, "day"),
});
// $("[data-mask]").inputmask();
//init product location warehouse,floor,rack,bin
$('select[name="warehouse"]').select2({
theme: "bootstrap4",
language: {
noResults: function () {
return "Select store and enter warehouse name.";
},
},
escapeMarkup: function (markup) {
return markup;
},
placeholder: "Please Select",
ajax: {
url: "/admin/inventory/locations",
dataType: "json",
delay: 250,
data: function (data) {
return {
search: data.term,
limit: 15,
store_id: $('select[name="catalog_store"]').val(),
};
},
processResults: function (response) {
return {
results: response,
};
},
cache: true,
},
});
$('select[name="floor"]').select2({
theme: "bootstrap4",
language: {
noResults: function () {
return "Select warehouse and enter floor name.";
},
},
escapeMarkup: function (markup) {
return markup;
},
placeholder: "Please Select",
ajax: {
url: "/admin/inventory/locations",
dataType: "json",
delay: 250,
data: function (data) {
return {
search: data.term,
limit: 15,
parent_id: $('select[name="warehouse"]').val(),
type: "floor",
};
},
processResults: function (response) {
return {
results: response,
};
},
cache: true,
},
});
$('select[name="rack"]').select2({
theme: "bootstrap4",
language: {
noResults: function () {
return "Select floor and enter rack name.";
},
},
escapeMarkup: function (markup) {
return markup;
},
placeholder: "Please Select",
ajax: {
url: "/admin/inventory/locations",
dataType: "json",
delay: 250,
data: function (data) {
return {
search: data.term,
limit: 15,
parent_id: $('select[name="floor"]').val(),
type: "rack",
};
},
processResults: function (response) {
return {
results: response,
};
},
cache: true,
},
});
$('select[name="bin"]').select2({
theme: "bootstrap4",
language: {
noResults: function () {
return "Select floor and enter bin name.";
},
},
escapeMarkup: function (markup) {
return markup;
},
placeholder: "Please Select",
ajax: {
url: "/admin/inventory/locations",
dataType: "json",
delay: 250,
data: function (data) {
return {
search: data.term,
limit: 15,
parent_id: $('select[name="rack"]').val(),
type: "bin",
};
},
processResults: function (response) {
return {
results: response,
};
},
cache: true,
},
});
$('select[name="warehouse"]').on("change", function () {
$('select[name="floor"]').val(null).trigger("change");
});
$('select[name="floor"]').on("change", function () {
$('select[name="rack"]').val(null).trigger("change");
});
$('select[name="rack"]').on("change", function () {
$('select[name="bin"]').val(null).trigger("change");
});
$("#modal_variation").on("hidden.bs.modal", function () {
//destroy all elements in modal
// myDzProductPhotos.destroy();
myDzProductPhotos.files = [];
$(this).remove();
});
$("#btn_add_variation").prop("disabled", false);
$("#btn_save_variation").on("click", function () {
//validations
if ($('input[name="price"]').val() == "") {
toast("error", "Price cannot be empty.");
$('input[name="price"]').trigger("focus");
return false;
}
if (
isBelowN(
$('input[name="price"]'),
"Price cannot be negative value.",
0
)
) {
return false;
}
if (
isBelowN(
$('input[name="min_order_qty"]'),
"Minimum order quantity must be equal or greater than 1",
1
)
) {
return false;
}
if (
isBelowN(
$('input[name="max_order_qty"]'),
"Maximum order quantity must be equal or greater than 1",
1
)
) {
return false;
}
if (
isBelowN(
$('input[name="sale_price"]'),
"Sale price cannot be negative value.",
0
)
) {
return false;
}
if (
isBelowN(
$('input[name="quantity"]'),
"Quantity price cannot be negative value.",
0
)
) {
return false;
}
if (
isBelowN(
$('input[name="max_quantity"]'),
"Max quantity price cannot be negative value.",
0
)
) {
return false;
}
if (
isBelowN(
$('input[name="order_quantity"]'),
"Order quantity price cannot be negative value.",
0
)
) {
return false;
}
var is_empty_attr = false;
$("[data-attribute]").each(function () {
if (!$(this).val()) {
$(this).trigger("focus");
is_empty_attr = true;
return false;
}
});
if (is_empty_attr) {
toast("error", "Invalid attribute value.");
return false;
}
//sales price validation
if (
($("#sale_price_start").val() && !$("#sale_price_end").val()) ||
(!$("#sale_price_start").val() && $("#sale_price_end").val()) ||
moment($("#sale_price_start").val()) >=
moment($("#sale_price_end").val())
) {
toast("error", "Invalid sale date range.");
$("#sale_price_start").trigger("focus");
return false;
}
if (
($("#sale_price_start").val() &&
!$('input[name="sale_price"]').val()) ||
($("#sale_price_end").val() && !$('input[name="sale_price"]').val())
) {
toast("error", "Sale price cannot be empty.");
$('input[name="sale_price"]').trigger("focus");
return false;
}
if (
parseInt($('input[name="price"]').val()) <=
parseInt($('input[name="sale_price"]').val())
) {
toast("error", "Sale price must not greater or equal than price.");
$('input[name="sale_price"]').trigger("focus");
return false;
}
if ($('input[name="max_order_qty"]').val() != "") {
if (
parseInt($('input[name="max_order_qty"]').val()) <
parseInt($('input[name="min_order_qty"]').val())
) {
toast(
"error",
"Minimum order quantity must not greater than max order quantity."
);
$('input[name="min_order_qty"]').trigger("focus");
return false;
}
}
if ($('input[name="max_quantity"]').val() != "") {
//deprecated
// if (
// parseInt($('input[name="max_quantity"]').val()) <
// parseInt($('input[name="min_order_qty"]').val())
// ) {
// toast(
// "error",
// "Minimum order quantity must not greater than max quantity."
// );
// $('input[name="min_order_qty"]').trigger("focus");
// return false;
// }
// if (
// parseInt($('input[name="max_quantity"]').val()) <
// parseInt($('input[name="max_order_qty"]').val())
// ) {
// toast(
// "error",
// "Maximum order quantity must not greater than max quantity."
// );
// $('input[name="max_order_qty"]').trigger("focus");
// return false;
// }
if (
parseInt($('input[name="max_quantity"]').val()) <
parseInt($('input[name="quantity"]').val())
) {
toast("error", "Quantity must not greater than max quantity.");
$('input[name="quantity"]').trigger("focus");
return false;
}
if (
parseInt($('input[name="max_quantity"]').val()) <
parseInt($('input[name="order_quantity"]').val())
) {
toast(
"error",
"Order quantity must not greater than max quantity."
);
$('input[name="order_quantity"]').trigger("focus");
return false;
}
}
variation_attributes = [];
var obj_data = formObj("data-obj", $("[data-obj]"));
// console.log(obj_data);
// return false;
// Add Locations
if (parseInt($('select[name="warehouse"]').val())) {
obj_data["warehouse"] = {
id: $('select[name="warehouse"]').val(),
title: $('select[name="warehouse"] option:selected').text(),
type: "wh",
};
}
if (parseInt($('select[name="floor"]').val())) {
obj_data["floor"] = {
id: $('select[name="floor"]').val(),
title: $('select[name="floor"] option:selected').text(),
type: "floor",
};
}
if (parseInt($('select[name="rack"]').val())) {
obj_data["rack"] = {
id: $('select[name="rack"]').val(),
title: $('select[name="rack"] option:selected').text(),
type: "rack",
};
}
if (parseInt($('select[name="bin"]').val())) {
obj_data["bin"] = {
id: $('select[name="bin"]').val(),
title: $('select[name="bin"] option:selected').text(),
type: "bin",
};
}
//Add attributes to variation
$("[data-attribute]").each(function () {
var attr_obj = {
parent_id: parseInt($(this).attr("data-id")),
parent_name: $(this).attr("data-name"),
child_id: $(this).val(),
child_name: $("option:selected", this).text(),
};
variation_attributes.push(attr_obj);
});
//Legacy code for checking if attributes is exist
// //start check if variations has equal attributes
// $.each(variation_list, function(i, v) {
// // console.log('error');
// var result1 = JSON.parse(JSON.stringify(variation_attributes));
// var result2 = JSON.parse(JSON.stringify(v.attributes));
// if (result1.length != result2.length) {
// var uniqueResultOne = result1.filter(function(obj) {
// return !result2.some(function(obj2) {
// return obj.parent_id == obj2.parent_id;
// });
// });
// //Find values that are in result2 but not in result1
// var uniqueResultTwo = result2.filter(function(obj) {
// return !result1.some(function(obj2) {
// return obj.parent_id == obj2.parent_id;
// });
// });
// //Combine the two arrays of unique entries
// var result = JSON.parse(JSON.stringify(uniqueResultOne)); //uniqueResultOne.concat(uniqueResultTwo);
// result[0].child_id = '';
// result[0].child_name = '';
// console.log('added');
// $.each(result, function(k, j) {
// variation_list[i].attributes.push(j);
// });
// }
// });
//start check duplicate variations
// var isDuplicateVariation = false;
// $.each(variation_list, function (i, v) {
// if (selected_variation_key != null) {
// if (i == selected_variation_key) {
// return;
// }
// }
// var result1 = JSON.parse(JSON.stringify(variation_attributes));
// var result2 = JSON.parse(JSON.stringify(v.attributes));
// //Find values that are in result1 but not in result2
// var uniqueResultOne = result1.filter(function (obj) {
// return !result2.some(function (obj2) {
// return obj.child_id == obj2.child_id;
// });
// });
// //Find values that are in result2 but not in result1
// var uniqueResultTwo = result2.filter(function (obj) {
// return !result1.some(function (obj2) {
// return obj.child_id == obj2.child_id;
// });
// });
// //Combine the two arrays of unique entries
// var result = uniqueResultOne.concat(uniqueResultTwo);
// if (!result.length) {
// isDuplicateVariation = true;
// return false;
// }
// });
// if (isDuplicateVariation) {
// toast("error", "This variation is already added.");
// return false;
// }
if (checkDuplicateAddVariation()) {
toast("error", "This variation is already added.");
return false;
}
//end check duplicate variations
obj_data["attributes"] = JSON.parse(
JSON.stringify(variation_attributes)
);
//Add images from dropzone
obj_data["images"] = JSON.parse(JSON.stringify(variation_images));
$("#modal_variation").modal("hide");
toast("success", "Variation save.");
if (selected_variation_key != null) {
obj_data["default"] =
variation_list[selected_variation_key]["default"];
obj_data["id"] = variation_list[selected_variation_key].id;
variation_list[selected_variation_key] = obj_data;
} else {
//Set default variation
if (variation_list.length <= 0) {
obj_data["default"] = 1;
} else {
obj_data["default"] = 0;
}
obj_data["id"] = "";
variation_list.push(obj_data);
}
// refresh variation table
variationList();
});
},
error: function () {
toast("error", "Error has occurred. Try again.");
$("#btn_add_variation").prop("disabled", false);
},
});
}
function variationList() {
console.log("List: ");
console.log(variation_list);
var variationTableId = "#tbl_variations";
if ($.fn.DataTable.isDataTable(variationTableId)) {
// $(variationTableId).dataTable();
myVariationList.clear();
myVariationList.destroy();
}
$(variationTableId + " tbody").empty();
$(variationTableId + " thead").empty();
if (variation_list.length <= 0) {
return false;
}
//Object to table logic & algorithm : Clrkz 04/19/22
//fix cols
var cols = [
{
title: "Image",
data: "image",
className: "align-middle p-1 dt-center",
render: function (data, type, row, meta) {
return (
`
`
);
},
width: "5%",
},
{
title: "Price",
data: "price",
className: "align-middle p-1 dt-center",
ordering: true,
width: "15%",
render: function (data, type, row, meta) {
var sale_price_end = moment(variation_list[row.key].sale_price_end);
// console.log(sale_price_end);
var date_now = moment();
// console.log(sale_price_end);
// console.log(date_now);
// date is past
var price =
CURRENCY_SYMBOL +
parseFloat(row.price).toLocaleString("en-US", {
minimumFractionDigits: 2,
});
var sale_price = variation_list[row.key].sale_price;
var final_sale_price =
CURRENCY_SYMBOL +
parseFloat(sale_price).toLocaleString("en-US", {
minimumFractionDigits: 2,
}) +
`
` +
price +
``;
if (sale_price_end.isValid()) {
if (sale_price_end > date_now) {
// var sale_price = variation_list[row.key].sale_price;
return final_sale_price;
}
} else {
if (sale_price) {
return final_sale_price;
}
}
return price;
},
},
{
title: "Default",
data: "default",
render: function (data, type, row, meta) {
return (
`
`
);
},
className: "align-middle p-1 dt-center",
width: "5%",
},
{
title: "Action",
data: "key",
render: function (data, type, row, meta) {
return (
`
`
);
},
className: "align-middle p-1 dt-center",
width: "10%",
},
];
//get the variation as table cols
var col_attribute = [];
$.each(variation_list[0].attributes, function (i, v) {
if (!v) {
return;
}
col_attribute.push({
title: capitalizeFirstLetter(v.parent_name),
data: v.parent_name.toLowerCase().trim(),
className: "align-middle p-1",
});
});
//apend variation to fix cols
$.each(col_attribute.reverse(), function (i, v) {
cols.splice(1, 0, col_attribute[i]);
});
//Table Data
var data = [];
$.each(variation_list, function (i, v) {
var fix_data = {
image:
v.images.length <= 0
? APP_DISK + "products/default.png"
: APP_DISK + v.images[0].path,
price: v.price,
default: v.default,
key: i,
};
var data_attribute = [];
$.each(v.attributes, function (j, k) {
if (!k) {
return;
}
data_attribute[k.parent_name.toLowerCase().trim()] = k.child_name;
});
var merge_data = {};
Object.assign(merge_data, fix_data, data_attribute);
// console.log(merge_data)
data.push(merge_data);
});
myVariationList = $(variationTableId).DataTable({
retrieve: true,
columns: cols,
data: data,
paging: true,
lengthChange: false,
searching: false,
ordering: false,
info: true,
autoWidth: false,
responsive: true,
});
}
function formatCategory(category) {
if (!category.id) {
return category.text;
}
var baseUrl = "/assets/img/logo/AGC_TRANSPARENT.png";
var $category = $('
');
// Use .text() instead of HTML string concatenation to avoid script injection issues
$category.find("span").text(category.text.replaceAll("¦––", ""));
// $category.find("img").attr("src", baseUrl + "/" + category.element.value.toLowerCase() + ".png");
return $category;
}
function filterArray(array) {
return array.filter(function (el) {
return el != null;
});
}
function disableSelected() {
$(".repeater")
.find("select")
.each(function () {
var $current_el = $(this);
$("option:disabled", this).removeAttr("disabled");
// $.each(selected_attributes, function(index, value) {
var current_value = $(this).val();
$("option", $current_el).each(function () {
if (current_value != $(this).val()) {
var $option_el = $(this);
if (jQuery.inArray($option_el.val(), selected_attributes) !== -1) {
$option_el.attr("disabled", "disabled");
}
// $.each(selected_attributes, function(index, value) {
// if ($option_el.val() == value) {
// $option_el.attr("disabled", "disabled");
// }
// });
}
});
});
}
function gotoTab(nav, pane) {
$(".nav-1").removeClass("active");
$(".pane-1").removeClass("active show");
$(nav).addClass("active");
$(pane).addClass("active show");
}
//Update Products
function fetchProductInformation() {
console.log("Product Information: ");
console.log(product_information_json);
if (!Object.keys(product_information_json).length) {
return false;
}
$('input[name="catalog_name"]').val(product_information_json.title);
$("#catalog_description").summernote(
"code",
product_information_json.content
);
//catehories
$("select[name=catalog_brand]")
.val(product_information_json.brandId)
.trigger("change");
$("select[name=catalog_tax]")
.val(product_information_json.taxId)
.trigger("change");
if (product_information_json.available) {
$("input[name=catalog_status]").prop("checked", true).trigger("change");
}
$.each(product_information_json.categories, function (i, v) {
$("#catalog_category option[value='" + v + "']")
.prop("selected", true)
.trigger("change");
});
//Main images
$.each(product_information_json.product_images, function (i, v) {
product_images.push({
id: v.id,
path: v.path,
size: v.size,
name: v.name,
});
var mockFile = {
id: v.id,
name: v.name, //v.path,
size: v.size, //'12345' //v.unreadable_size
};
myDzProductMainPhotos.emit("addedfile", mockFile);
myDzProductMainPhotos.emit("complete", mockFile);
myDzProductMainPhotos.emit(
"thumbnail",
mockFile,
APP_DISK + "thumbnails/" + v.path
);
myDzProductMainPhotos.files.push(mockFile); // here you add them into the files array
});
// variation_list = product_information_json.variations;
if (product_information_json.attribute_sets.length) {
$(".variations").show();
$(".non-variations").hide();
}
//Variations and Attributes
$.each(product_information_json.attribute_sets, function (i, v) {
$("[data-repeater-create]").trigger("click");
var $attr_el = $(
'select[name="attribute_list[' + i + '][attribute_name]"]'
);
$attr_el.val(v.id);
});
setTimeout(function () {
$.each(product_information_json.attribute_sets, function (i, v) {
var $attr_el = $(
'select[name="attribute_list[' + i + '][attribute_name]"]'
);
$attr_el.trigger("change");
// var key = $attr_el.attr("data-select2-id");
// selected_attributes[key] = $attr_el.val();
});
// disableSelected();
// console.log(selected_attributes)
variation_list = product_information_json.variations;
variationList();
console.log(selected_attributes);
}, 1000);
// var key = $(this).find(".select2").attr("data-select2-id");
// selected_attributes[key] = $(this).val();
//Pricing
$('input[name="catalog_price"]').val(product_information_json.price);
$('input[name="catalog_sale_price"]').val(
product_information_json.discount
);
// if (product_information_json.startsAt) {
// $('input[name="catalog_sale_price_start"]').val(
// moment(product_information_json.startsAt).format("M/D/YYYY hh:mm A")
// );
$('input[name="catalog_sale_price_start"]').val(
product_information_json.startsAt
);
// }
// if (product_information_json.endsAt) {
$('input[name="catalog_sale_price_end"]').val(
// moment(product_information_json.endsAt).format("M/D/YYYY hh:mm A")
product_information_json.endsAt
);
// }
// Inventory
$('input[name="catalog_sku"]').val(product_information_json.sku);
$('input[name="catalog_quantity"]').val(product_information_json.quantity);
$('input[name="catalog_max_quantity"]').val(
product_information_json.maxQty
);
$('input[name="catalog_order_quantity"]').val(
product_information_json.reorderQty
);
$('input[name="catalog_min_order_qty"]').val(
product_information_json.minOrderQty
);
$('input[name="catalog_max_order_qty"]').val(
product_information_json.maxOrderQty
);
//Locations
var prod_loc = product_information_json.locations;
if (typeof prod_loc[0] != "undefined") {
var whOption = new Option(prod_loc[0].title, prod_loc[0].id, true, true);
$('select[name="catalog_warehouse"]').append(whOption).trigger("change");
}
if (typeof prod_loc[1] != "undefined") {
var floorOption = new Option(
prod_loc[1].title,
prod_loc[1].id,
true,
true
);
$('select[name="catalog_floor"]').append(floorOption).trigger("change");
}
if (typeof prod_loc[2] != "undefined") {
var rackOption = new Option(
prod_loc[2].title,
prod_loc[2].id,
true,
true
);
$('select[name="catalog_rack"]').append(rackOption).trigger("change");
}
if (typeof prod_loc[3] != "undefined") {
var binOption = new Option(prod_loc[3].title, prod_loc[3].id, true, true);
$('select[name="catalog_bin"]').append(binOption).trigger("change");
}
}
$(function () {
"use strict";
$.fn.datetimepicker.Constructor.Default = $.extend(
{},
$.fn.datetimepicker.Constructor.Default,
{
icons: {
time: "fa fa-clock",
date: "fa fa-calendar",
up: "fa fa-arrow-up",
down: "fa fa-arrow-down",
previous: "fa fa-chevron-left",
next: "fa fa-chevron-right",
today: "fa fa-calendar-check-o",
clear: "fa fa-trash",
close: "fa fa-times",
},
}
);
$("[name='catalog_status']").change(function () {
if ($(this).is(":checked")) {
$(this).val("1");
} else {
$(this).val("0");
}
});
$(".select2_category").select2({
theme: "bootstrap4",
templateSelection: formatCategory,
});
$("#catalog_description").summernote({
tabsize: 2,
height: 250,
});
$("#catalog_name").on("click", function () {
if ($("#catalog_description").summernote("isEmpty")) {
// alert('editor content is empty');
} else {
//summernote codes
var html = $("#catalog_description").summernote("code");
// alert(html);
}
});
removeSubmitButtonOffsetOn([
"#vert-tabs-attributes",
"#vert-tabs-variations",
"#vert-tabs-images",
]);
// Catalog form
$.validator.addMethod(
"catalogDescription",
function (value, element, param) {
if ($("#catalog_description").summernote("isEmpty")) {
gotoTab("#vert-tabs-general-tab", "#vert-tabs-general");
return false;
}
return true;
},
"This field is required."
);
$.validator.addMethod(
"catalogQuantity",
function (value, element, param) {
//if no attributes
var filtered_selected_attributes = filterArray(selected_attributes);
if (!filtered_selected_attributes.length) {
// if (parseInt($('input[name="catalog_max_quantity"]').val()) > 0) {
if ($('input[name="catalog_max_quantity"]').val() != "") {
if (
parseInt($('input[name="catalog_max_quantity"]').val()) <
parseInt($('input[name="catalog_quantity"]').val())
) {
gotoTab("#vert-tabs-inventory-tab", "#vert-tabs-inventory");
// $('input[name="catalog_quantity"]').trigger("focus");
return false;
}
}
}
return true;
},
"Quantity must not greater than max quantity."
);
$.validator.addMethod(
"catalogMinQuantity",
function (value, element, param) {
//if no attributes
var filtered_selected_attributes = filterArray(selected_attributes);
if (!filtered_selected_attributes.length) {
if ($('input[name="catalog_max_order_qty"]').val() != "") {
if (
parseInt($('input[name="catalog_max_order_qty"]').val()) <
parseInt($('input[name="catalog_min_order_qty"]').val())
) {
gotoTab("#vert-tabs-inventory-tab", "#vert-tabs-inventory");
return false;
}
}
}
return true;
},
"Minimum order quantity must not greater than max order quantity."
);
$.validator.addMethod(
"catalogOrderQuantity",
function (value, element, param) {
//if no attributes
var filtered_selected_attributes = filterArray(selected_attributes);
if (!filtered_selected_attributes.length) {
if (
parseInt($('input[name="catalog_max_quantity"]').val()) <
parseInt($('input[name="catalog_order_quantity"]').val())
) {
$.validator.messages.catalogOrderQuantity =
"Order quantity must not greater than max quantity.";
gotoTab("#vert-tabs-inventory-tab", "#vert-tabs-inventory");
// $('input[name="catalog_order_quantity"]').trigger("focus");
return false;
}
}
return true;
},
$.validator.messages.catalogOrderQuantity
);
$.validator.addMethod(
"catalogPrice",
function (value, element, param) {
//if no attributes
var filtered_selected_attributes = filterArray(selected_attributes);
if (!filtered_selected_attributes.length) {
if ($('input[name="catalog_price"]').val() == "") {
$.validator.messages.catalogPrice = "This field is required.";
gotoTab("#vert-tabs-price-tab", "#vert-tabs-price");
// $('input[name="catalog_price"]').trigger("focus");
return false;
}
}
return true;
},
$.validator.messages.catalogPrice
);
// if (
// ($("#sale_price_start").val() &&
// !$('input[name="sale_price"]').val()) ||
// ($("#sale_price_end").val() && !$('input[name="sale_price"]').val())
// ) {
// toast("error", "Sale price cannot be empty.");
// $('input[name="sale_price"]').trigger("focus");
// return false;
// }
// if (
// parseInt($('input[name="price"]').val()) <=
// parseInt($('input[name="sale_price"]').val())
// ) {
// toast("error", "Sale price must not greater or equal than price.");
// $('input[name="sale_price"]').trigger("focus");
// return false;
// }
$.validator.addMethod(
"catalogSalePriceEnd",
function (value, element, param) {
//if no attributes
var filtered_selected_attributes = filterArray(selected_attributes);
if (!filtered_selected_attributes.length) {
if (
($("#catalog_sale_price_start").val() &&
!$("#catalog_sale_price_end").val()) ||
(!$("#catalog_sale_price_start").val() &&
$("#catalog_sale_price_end").val()) ||
moment($("#catalog_sale_price_start").val()) >=
moment($("#catalog_sale_price_end").val())
) {
$.validator.messages.catalogSalePriceEnd =
"Invalid sale date range.";
gotoTab("#vert-tabs-price-tab", "#vert-tabs-price");
// $('input[name="catalog_sale_price_start"]').trigger("focus");
return false;
}
}
return true;
},
$.validator.messages.catalogSalePriceEnd
);
$.validator.addMethod(
"catalogSalePrice",
function (value, element, param) {
//if no attributes
var filtered_selected_attributes = filterArray(selected_attributes);
if (!filtered_selected_attributes.length) {
if (
($("#catalog_sale_price").val() &&
!$('input[name="catalog_sale_price"]').val()) ||
($("#catalog_sale_price_end").val() &&
!$('input[name="catalog_sale_price"]').val())
) {
$.validator.messages.catalogSalePrice =
"Sale price cannot be empty.";
gotoTab("#vert-tabs-price-tab", "#vert-tabs-price");
// $('input[name="catalog_sale_price_start"]').trigger("focus");
return false;
}
if (
parseInt($('input[name="catalog_price"]').val()) <=
parseInt($('input[name="catalog_sale_price"]').val())
) {
$.validator.messages.catalogSalePrice =
"Sale price must not greater or equal than price.";
gotoTab("#vert-tabs-price-tab", "#vert-tabs-price");
// $('input[name="catalog_sale_price"]').trigger("focus");
return false;
}
}
return true;
},
$.validator.messages.catalogSalePrice
);
$.validator.setDefaults({
submitHandler: function (form) {
// console.log(variation_list);
// return false;
window.onbeforeunload = false;
var formData = new FormData(form);
formData.append(
"id",
Object.keys(product_information_json).length
? product_information_json.id
: ""
);
var filtered_selected_attributes = filterArray(selected_attributes);
if (filtered_selected_attributes.length) {
if (variation_list.length) {
//chyeck if all variations attribute has filled
var isVariationAttributeNotEmpty = true;
// check if all variations has assigned attribute
$.each(variation_list, function (i, v) {
$.each(v.attributes, function (k, j) {
if (!j.child_id) {
isVariationAttributeNotEmpty = false;
return false;
}
});
});
if (isVariationAttributeNotEmpty) {
// $('input[name="variations"]').val(JSON.stringify(variation_list));
formData.append("variations", JSON.stringify(variation_list));
} else {
gotoTab("#vert-tabs-variations-tab", "#vert-tabs-variations");
toast(
"error",
"There's an incomplete variation, check attribute values."
);
return false;
}
//check if no duplicate variaions attributes
if (checkDuplicateVariation()) {
gotoTab("#vert-tabs-variations-tab", "#vert-tabs-variations");
toast("error", "There are duplicate variations");
return false;
}
} else {
gotoTab("#vert-tabs-variations-tab", "#vert-tabs-variations");
toast("error", "Add at least 1 product variation.");
return false;
}
}
//else {
// $('input[name="product_images"]').val(JSON.stringify(product_images));
formData.append("product_images", JSON.stringify(product_images));
// }
// var $form = $("#catalogForm");
// $form.append("test", "nice");
$.ajax({
url: "/admin/products",
type: "POST",
data: formData,
// timeout: 9000,
processData: false,
contentType: false,
beforeSend: function () {
$("#btn_save_product").prop("disabled", true);
},
success: function (result) {
// console.log(result);
// $("#btn_save_product").prop("disabled", false);
window.location.replace("/admin/products");
// $("#btn_save_product").prop("disabled", false);
return false;
},
error: function () {
toast("error", "Error has occurred. Try again.");
$("#btn_save_product").prop("disabled", false);
},
});
return false;
},
ignore: [],
});
$("#catalogForm").validate({
rules: {
catalog_name: {
required: true,
},
catalog_description: {
catalogDescription: true,
},
catalog_price: {
catalogPrice: true,
min: 0,
},
catalog_sale_price_end: {
catalogSalePriceEnd: true,
},
catalog_sale_price: {
catalogSalePrice: true,
min: 0,
},
catalog_quantity: {
catalogQuantity: true,
min: 0,
},
catalog_max_quantity: {
min: 0,
},
catalog_order_quantity: {
catalogOrderQuantity: true,
min: 0,
},
catalog_min_order_qty: {
catalogMinQuantity: true,
min: 0,
},
catalog_max_order_qty: {
min: 0,
},
},
messages: {
// catalog_name: "Meow",
},
errorElement: "span",
errorPlacement: function (error, element) {
error.addClass("invalid-feedback");
element.closest(".form-group").append(error);
},
highlight: function (element, errorClass, validClass) {
$(element).addClass("is-invalid");
},
unhighlight: function (element, errorClass, validClass) {
$(element).removeClass("is-invalid");
},
});
//fux summernote error on validator
$("#catalogForm").each(function () {
if ($(this).data("validator"))
$(this).data("validator").settings.ignore = ".note-editor *";
});
$(".repeater").repeater({
repeaters: [
{
// (Required)
// Specify the jQuery selector for this nested repeater
selector: ".inner-repeater",
},
],
hide: function (deleteElement) {
var option_key = $(this).find("select").val();
if (option_key) {
Swal.fire({
title: "Are you sure?",
text: "This attribute will be remove to all variants of this product.",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, remove it!",
showClass: {
backdrop: "swal2-noanimation", // disable backdrop animation
popup: "", // disable popup animation
icon: "", // disable icon animation
},
hideClass: {
popup: "", // disable popup fade-out animation
},
}).then((result) => {
if (result.isConfirmed) {
// remove this attribute to every variation
$.each(variation_list, function (i, v) {
$.each(v.attributes, function (k, j) {
if (j.parent_id == option_key) {
delete variation_list[i].attributes[k];
// clean and refresh variations attributes
variation_list[i].attributes = variation_list[
i
].attributes.filter(function (el) {
return el != null;
});
}
});
});
//check if theres more attribute to variation then if none remove all variation
var variation_counter = 0;
// $.each(variation_list, function (i, v) {
// $.each(v.attributes, function (k, j) {
// variation_counter++;
// });
// return false;
// });
if (variation_list.length) {
variation_counter = variation_list[0].attributes.length;
// $.each(variation_list[0].attributes, function (k, j) {
// if (parent_id == j.parent_id) {
// hasAttribute = true;
// return false;
// }
// });
}
// console.log(variation_counter);
if (!variation_counter) {
variation_list = [];
var variationTableId = "#tbl_variations";
if ($.fn.DataTable.isDataTable(variationTableId)) {
// $(variationTableId).dataTable();
myVariationList.clear();
myVariationList.destroy();
}
// if (myVariationList != null) {
// myVariationList.clear();
// // myVariationList.destroy(false);
// }
$(variationTableId + " tbody").empty();
$(variationTableId + " thead").empty();
} else {
variationList();
}
$(this).slideUp(function () {
//remove key in array
// var key = $(this).find(".select2").attr("data-select2-id");
var key = $(this).find("select").attr("data-select2-id");
// alert(key);
delete selected_attributes[key];
disableSelected();
deleteElement();
// itemRemove(); //another function if necessary
var repeatCount = $(".repeater").find("select").length;
var optionCount = $(this)
.find("select")
.children("option").length;
if (repeatCount != optionCount) {
$("[data-repeater-create]").show();
}
var filtered_selected_attributes =
filterArray(selected_attributes);
if (!filtered_selected_attributes.length) {
$(".variations").hide();
$(".non-variations").show();
}
});
}
});
} else {
//remove if no option selected
$(this).slideUp(function () {
//remove key in array
// var key = $(this).find(".select2").attr("data-select2-id");
var key = $(this).find("select").attr("data-select2-id");
delete selected_attributes[key];
disableSelected();
deleteElement();
// itemRemove(); //another function if necessary
var repeatCount = $(".repeater").find("select").length;
var optionCount = $(this).find("select").children("option").length;
if (repeatCount != optionCount) {
$("[data-repeater-create]").show();
}
var filtered_selected_attributes = filterArray(selected_attributes);
if (!filtered_selected_attributes.length) {
$(".variations").hide();
$(".non-variations").show();
}
});
}
},
show: function () {
// $(this).slideDown();
$(this).slideDown(function () {
//init select2 after adding repeater
$(this).find(".select2repeater").select2({
placeholder: "Select an option",
minimumResultsForSearch: -1,
});
//fix select2 arrow ui
$(this).find(".select2-selection__arrow").addClass("mt-1");
$(this)
.find("select")
.on("change", function () {
//add select2 id and selected value to array
var key = $(this).attr("data-select2-id");
console.log(key);
var parent_id = $(this).val();
// check if this attr is exist to variation attribute
// if not exist add if exist ignore
var hasAttribute = false;
if (variation_list.length) {
$.each(variation_list[0].attributes, function (k, j) {
if (parent_id == j.parent_id) {
hasAttribute = true;
return false;
}
});
}
// check if attribte exist if not push to selected attributes obj
if (!hasAttribute) {
var attr_obj = {
parent_id: parseInt(parent_id),
parent_name: $("option:selected", this).text(),
child_id: "",
child_name: "",
};
$.each(variation_list, function (i, v) {
variation_list[i].attributes.push(attr_obj);
});
selected_attributes[key] = $(this).val();
}
//remove list
// 1. get variation attributes
// 2. compare 2 selected attributes
// 3. remnove variation attribute if not exist in selected variations
var filtered_selected_attributes =
filterArray(selected_attributes);
$.each(variation_list, function (i, v) {
$.each(v.attributes, function (k, j) {
if (
!filtered_selected_attributes.includes(
parseInt(j.parent_id)
)
) {
// delete variation_list[i].attributes[k];
variation_list[i].attributes = filterArray(
variation_list[i].attributes
);
}
});
});
//hide non varaitons elements
if (filtered_selected_attributes.length) {
$(".variations").show();
$(".non-variations").hide();
}
variationList();
// }
disableSelected();
});
//enable all option in repeater select2
//disable array option in repeater
disableSelected();
var repeatCount = $(".repeater").find("select").length;
var optionCount = $(this).find("select").children("option").length;
if (repeatCount == optionCount) {
$("[data-repeater-create]").hide();
}
});
},
// defaultValues: {
//'text-input': 'foo'
//},
ready: function (setIndexes) {
// console.log('repeater ready');
},
initEmpty: true,
// isFirstItemUndeletable: true,
});
responsiveDatatablesTab();
$("#btn_add_variation").on("click", function (e) {
selected_variation_key = null;
variationForm();
});
$("#tbl_variations").on("click", "td .edit", function () {
var $el = $(this).parents("td").last();
var row = myVariationList.row($el).data();
// console.log(row);
selected_variation_key = row.key;
variationForm();
});
$("#tbl_variations").on("click", "td .remove", function () {
// var table = $('#tbl_variations').DataTable();
// var cell = myVariationList.cell($el).data(); // returns correct cell data
// console.log(cell);
var $el = $(this).parents("td").last();
var row = myVariationList.row($el).data();
// console.log(row);
Swal.fire({
title: "Are you sure, you want to remove this variation?",
text: "This action cannot be undone.",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, remove it!",
showClass: {
backdrop: "swal2-noanimation", // disable backdrop animation
popup: "", // disable popup animation
icon: "", // disable icon animation
},
hideClass: {
popup: "", // disable popup fade-out animation
},
}).then((result) => {
if (result.isConfirmed) {
if (row.default) {
//transfer default
// variation_list[row.key + 1] = 1;
// console.log(variation_list[row.key + 1]);
if (typeof variation_list[row.key + 1] != "undefined") {
variation_list[row.key + 1].default = 1;
}
}
delete variation_list[row.key];
// clean and refresh variations attributes
variation_list = variation_list.filter(function (el) {
return el != null;
});
variationList();
}
});
});
$("#tbl_variations").on("click", "td .default", function () {
var $el = $(this).parents("td").last();
var row = myVariationList.row($el).data();
$.each(variation_list, function (i, v) {
variation_list[i].default = 0;
});
variation_list[row.key].default = 1;
// console.log(variation_list)
});
// dzProductPhotos("#catalog_dz_product_photo");
dzProductPhotosMain();
//initialize datepicker
$(".datetimepicker").datetimepicker({
format: "MM/DD/YYYY hh:mm A",
// minDate: moment().subtract(1, "day"),
});
$('select[name="catalog_store"]').select2({
theme: "bootstrap4",
language: {
noResults: function () {
return "Enter store name.";
},
},
escapeMarkup: function (markup) {
return markup;
},
// placeholder: 'Please Select',
ajax: {
url: "/admin/stores/options",
dataType: "json",
delay: 250,
data: function (data) {
return {
search: data.term,
limit: 15,
};
},
processResults: function (response) {
return {
results: response,
};
},
cache: true,
},
});
var default_store = {
id: 1,
text: "Default",
};
var newOption = new Option(
default_store.text,
default_store.id,
false,
false
);
$('select[name="catalog_store"]').append(newOption).trigger("change");
//init product location warehouse,floor,rack,bin
$('select[name="catalog_warehouse"]').select2({
theme: "bootstrap4",
language: {
noResults: function () {
return "Select store and enter warehouse name.";
},
},
escapeMarkup: function (markup) {
return markup;
},
placeholder: "Please Select",
ajax: {
url: "/admin/inventory/locations",
dataType: "json",
delay: 250,
data: function (data) {
return {
search: data.term,
limit: 15,
store_id: $('select[name="catalog_store"]').val(),
};
},
processResults: function (response) {
return {
results: response,
};
},
cache: true,
},
});
$('select[name="catalog_floor"]').select2({
theme: "bootstrap4",
language: {
noResults: function () {
return "Select warehouse and enter floor name.";
},
},
escapeMarkup: function (markup) {
return markup;
},
placeholder: "Please Select",
ajax: {
url: "/admin/inventory/locations",
dataType: "json",
delay: 250,
data: function (data) {
return {
search: data.term,
limit: 15,
parent_id: $('select[name="catalog_warehouse"]').val(),
type: "floor",
};
},
processResults: function (response) {
return {
results: response,
};
},
cache: true,
},
});
$('select[name="catalog_rack"]').select2({
theme: "bootstrap4",
language: {
noResults: function () {
return "Select floor and enter rack name.";
},
},
escapeMarkup: function (markup) {
return markup;
},
placeholder: "Please Select",
ajax: {
url: "/admin/inventory/locations",
dataType: "json",
delay: 250,
data: function (data) {
return {
search: data.term,
limit: 15,
parent_id: $('select[name="catalog_floor"]').val(),
type: "rack",
};
},
processResults: function (response) {
return {
results: response,
};
},
cache: true,
},
});
$('select[name="catalog_bin"]').select2({
theme: "bootstrap4",
language: {
noResults: function () {
return "Select floor and enter bin name.";
},
},
escapeMarkup: function (markup) {
return markup;
},
placeholder: "Please Select",
ajax: {
url: "/admin/inventory/locations",
dataType: "json",
delay: 250,
data: function (data) {
return {
search: data.term,
limit: 15,
parent_id: $('select[name="catalog_rack"]').val(),
type: "bin",
};
},
processResults: function (response) {
return {
results: response,
};
},
cache: true,
},
});
$('select[name="catalog_warehouse"]').on("change", function () {
$('select[name="catalog_floor"]').val(null).trigger("change");
});
$('select[name="catalog_floor"]').on("change", function () {
$('select[name="catalog_rack"]').val(null).trigger("change");
});
$('select[name="catalog_rack"]').on("change", function () {
$('select[name="catalog_bin"]').val(null).trigger("change");
});
closeAlert("catalogForm");
//Updating of product
fetchProductInformation();
});
})();
home/autoph/public_html/projects/api/public/assets/js/order/form.js 0000644 00000106753 15025016671 0021527 0 ustar 00 /*
* Author: Clarence A Andaya
* Date: 24 Mar 2022
*/
(function () {
"use strict";
const search_product = {
offset: 0,
limit: 10,
count: 0,
list: [],
};
const search_buyer = {
offset: 0,
limit: 15,
// count: 0,
list: [],
};
const $total_amount = $(".total_amount");
const $btn_paid = $("#btn_paid");
const $btn_pay_later = $("#btn_pay_later");
const $modal_container = $(".modal_container");
var $remove_buyer = null;
var selected_products = [];
var selected_buyer = null;
var selected_payment = null;
var ajax_search_product = null;
var ajax_search_buyer = null;
var open_order_modal = false;
//Logic,Variables, Functions Here
function currencyNumberFormat(num) {
return (
CURRENCY_SYMBOL +
parseFloat(num).toLocaleString("en-US", {
minimumFractionDigits: 2,
})
);
}
function filterArray(array) {
return array.filter(function (el) {
return el != null;
});
}
function orderTotal(selected_products_index) {
// get order total
var order_total = 0;
$.each(
selected_products[selected_products_index].products,
function (sp_i, sp_v) {
order_total +=
(sp_v.price + sp_v.price * (sp_v.tax_percentage / 100)) *
sp_v.order_quantity;
}
);
return selected_products[selected_products_index].shipping
? parseInt(order_total) +
parseInt(selected_products[selected_products_index].shipping.price)
: order_total;
}
function totalAmount() {
// get order total
var total_amount = 0;
$.each(selected_products, function (i, v) {
var order_total = 0;
$.each(v.products, function (sp_i, sp_v) {
order_total +=
(sp_v.price + sp_v.price * (sp_v.tax_percentage / 100)) *
sp_v.order_quantity;
});
total_amount += v.shipping
? parseInt(order_total) + parseInt(v.shipping.price)
: order_total;
});
return total_amount;
}
function orderButton() {
if (selected_products.length && selected_buyer) {
if (!open_order_modal) {
open_order_modal = true;
$btn_paid.removeAttr("disabled");
$btn_pay_later.removeAttr("disabled");
$btn_paid.on("click", function () {
createOrder("completed");
});
$btn_pay_later.on("click", function () {
createOrder("pending");
});
}
} else {
open_order_modal = false;
$btn_paid.prop("disabled", true);
$btn_pay_later.prop("disabled", true);
$btn_paid.off("click");
$btn_pay_later.off("click");
}
}
function changeOrderQuantityEvent() {
$(".table-normal").each(function (i, v) {
const $tbody_tr = $(this).find("tbody > tr");
const $tfoot_tr = $(this).find("tfoot > tr");
const $order_total = $tfoot_tr.find(".order_total");
const $txtarea_auto_h = $tfoot_tr.find(".textarea-auto-height");
$txtarea_auto_h.textareaAutoSize();
$txtarea_auto_h.on("change", function () {
if (isEmpty($txtarea_auto_h.val())) {
selected_products[i].notes = null;
} else {
selected_products[i].notes = $txtarea_auto_h.val();
}
});
$tbody_tr.each(function (j, k) {
const $price_x_order_qty = $(this).find(".price_x_order_qty");
const $order_qty = $(this).find("input");
const $remove_selected = $(this).find(".remove_selected");
$order_qty.on("change", function () {
const product = selected_products[i].products[j];
//validate if order qty empty or zero
if (isEmpty($(this).val()) || parseInt($(this).val()) <= 0) {
$(this).val(product.order_quantity).trigger("change");
return false;
}
const order_quantity = parseInt($(this).val());
//validate if order qty is less than min order qty
if (
order_quantity < product.min_order_quantity &&
product.min_order_quantity
) {
$(this).val(product.min_order_quantity).trigger("change");
return false;
}
// validate if order qty is less than or equal to max order qty
if (
order_quantity > product.max_order_quantity &&
product.max_order_quantity
) {
$(this).val(product.max_order_quantity).trigger("change");
return false;
}
// validate if order qty is less than or equal to max order qty
if (order_quantity > product.quantity && product.quantity) {
$(this).val(product.quantity).trigger("change");
return false;
}
// validate if order qty is less than or equal to max order qty
if (
order_quantity > product.max_order_quantity &&
product.max_order_quantity
) {
$(this).val(product.max_order_quantity).trigger("change");
return false;
}
product.order_quantity = order_quantity;
$price_x_order_qty.text(
currencyNumberFormat(
(product.price + product.price * (product.tax_percentage / 100)) *
product.order_quantity
)
);
$order_total.text(currencyNumberFormat(orderTotal(i)));
$total_amount.text(currencyNumberFormat(totalAmount()));
});
// $remove_selected.on("click", function (e) {
$remove_selected.on("click", function (e) {
// Logger.d(j);
// selected_products[i].products
delete selected_products[i].products[j];
selected_products[i].products = filterArray(
selected_products[i].products
);
if (!selected_products[i].products.length) {
delete selected_products[i];
selected_products = filterArray(selected_products);
}
displaySelectedProduct();
});
});
$tfoot_tr.each(function (j, k) {
const $order_shipping = $(this).find(".order_shipping");
const $shipping_price = $(this).find(".shipping_price");
$order_shipping.on("click", function () {
$.ajax({
url: "/admin/orders/shipping",
data: {
csrf_token: $("#csrf_token").val(),
},
type: "POST",
dataType: "json",
beforeSend: function () {
$modal_container.empty();
},
success: function (result) {
$modal_container.html(result.data);
$("#modal_shipping").modal("show");
//set selected shipping to Shipping UI
if (selected_products[i].shipping) {
$("#txt_shipping_custom").prop("checked", true);
$("#shipping_price").val(selected_products[i].shipping.id);
} else {
$("#txt_shipping_free").prop("checked", true);
}
$("#modal_shipping").on("hidden.bs.modal", function () {
$(this).remove();
});
var shipping = default_shipping;
$("#txt_shipping_free").on("change", function () {
shipping = null;
});
$("#txt_shipping_custom,#shipping_price").on(
"change",
function () {
var shipping_row = result.shipping
.map(function (e) {
return e.id;
})
.indexOf($("#shipping_price").val());
shipping = result.shipping[shipping_row];
$("#txt_shipping_custom").prop("checked", true);
//for bypassing html
if (!shipping) {
shipping = null;
$("#txt_shipping_free").prop("checked", true);
}
}
);
$("#btn_save_shipping").on("click", function () {
selected_products[i].shipping = shipping;
// Logger.d(selected_products[i].shipping)
if (shipping) {
$shipping_price.text(currencyNumberFormat(shipping.price));
} else {
$shipping_price.text(currencyNumberFormat(0));
}
$order_total.text(currencyNumberFormat(orderTotal(i)));
$total_amount.text(currencyNumberFormat(totalAmount()));
$("#modal_shipping").modal("hide");
});
},
error: function () {
toast("error", "Error has occurred. Try again.");
},
});
});
});
});
}
function displaySelectedProduct() {
$(".table-wrapper").empty();
// Logger.d(JSON.stringify(selected_products));
$.each(selected_products, function (i, v) {
var html_products = ``;
$.each(v.products, function (j, k) {
// Logger.d(k)
html_products +=
`
|
` +
k.name +
`
` +
(k.variation
? `
` +
k.variation +
`
`
: ``) +
`
|
|
x |
|
` +
currencyNumberFormat(
(k.price + k.price * (k.tax_percentage / 100)) * k.order_quantity
) +
` |
|
`;
});
const shipping_total = orderTotal(i);
$(".table-wrapper").append(
`
` +
v.store_name +
`
|
` +
html_products +
`
Shipping
|
` +
currencyNumberFormat(
selected_products[i].shipping
? selected_products[i].shipping.price
: 0
) +
`
|
|
Order Total
|
` +
currencyNumberFormat(shipping_total) +
`
|
`
);
});
$total_amount.text(currencyNumberFormat(totalAmount()));
// const total_amount = totalAmount();
changeOrderQuantityEvent();
orderButton();
}
function getStoreRow(i) {
var store_id = search_product.list[i].store_id;
var store_name = search_product.list[i].store;
if (!selected_products.filter((p) => p.store_id == store_id).length) {
selected_products.push({
store_id: store_id,
store_name: store_name,
notes: null,
shipping: default_shipping,
products: [],
});
}
var store_row = selected_products
.map(function (e) {
return e.store_id;
})
.indexOf(store_id);
return store_row;
}
function productSelectionEvent() {
$(".product-row").each(function (i, v) {
if ($(this).find(".product-variant").length) {
$(this)
.find(".product-variant")
.each(function (j, k) {
$(this).on("click", function () {
// alert(j)
var store_row = getStoreRow(i);
const product = search_product.list[i].variations[j];
if (
!selected_products[store_row].products.filter(
(p) => p.id == product.id
).length
) {
var order_quantity = 1;
if (product.quantity && product.min_order_quantity) {
order_quantity = product.min_order_quantity;
} else if (!product.quantity && product.min_order_quantity) {
order_quantity = product.min_order_quantity;
}
selected_products[store_row].products.push({
parent_id: search_product.list[i].id,
id: product.id,
thumbnail: search_product.list[i].thumbnail,
name: search_product.list[i].name,
sku: product.sku,
price: product.price,
tax_id: search_product.list[i].tax_id,
tax_percentage: search_product.list[i].tax_percentage,
variation: search_product.list[i].attributes_word,
quantity: product.quantity,
min_order_quantity: product.min_order_quantity,
max_order_quantity: product.max_order_quantity,
order_quantity: order_quantity,
});
displaySelectedProduct();
}
$(".product_result").removeClass("active").addClass("hidden");
});
});
} else {
$(this).on("click", function () {
var store_row = getStoreRow(i);
const product = search_product.list[i];
if (
!selected_products[store_row].products.filter(
(p) => p.id == product.id
).length
) {
var order_quantity = 1;
if (product.quantity && product.min_order_quantity) {
order_quantity = product.min_order_quantity;
} else if (!product.quantity && product.min_order_quantity) {
order_quantity = product.min_order_quantity;
}
selected_products[store_row].products.push({
parent_id: product.id,
id: product.id,
thumbnail: product.thumbnail,
name: product.name,
sku: product.sku,
price: product.price,
variation: null,
tax_id: product.tax_id,
tax_percentage: product.tax_percentage,
variation: product.attributes_word,
quantity: product.quantity,
min_order_quantity: product.min_order_quantity,
max_order_quantity: product.max_order_quantity,
order_quantity: order_quantity,
});
displaySelectedProduct();
}
$(".product_result").removeClass("active").addClass("hidden");
});
}
});
}
function searchProducts() {
if (ajax_search_product !== null) {
ajax_search_product.abort();
}
var resetEl = function () {
if (search_product.offset <= 0) {
$("#btn_product_prev").prop("disabled", true);
} else {
$("#btn_product_prev").prop("disabled", false);
}
if (search_product.count <= 0) {
$("#btn_product_next").prop("disabled", true);
} else {
$("#btn_product_next").prop("disabled", false);
}
$(".product_result").find(".has-loading").hide();
};
ajax_search_product = $.ajax({
url: "/admin/orders/products",
data: {
csrf_token: $("#csrf_token").val(),
search: $("#txt_product").val(),
offset: search_product.offset,
limit: search_product.limit,
},
type: "POST",
dataType: "json",
beforeSend: function () {
$("#product_results_list").empty();
$(".product_result").find(".has-loading").show();
$("#btn_product_prev", "#btn_product_next").prop("disabled", true);
// $(".product_result").find("#product_results_list").show();
search_product.list = [];
},
success: function (result) {
search_product.count = result.length;
search_product.list = result;
$.each(result, function (i, v) {
// Logger.d(result);
var html_product = ``;
if (v.variations.length) {
var html_product_variations = ``;
$.each(v.variations, function (j, k) {
html_product_variations +=
`
` +
k.attributes_word +
`
` +
(k.quantity == null
? ""
: "(" + k.quantity + " product(s) available)") +
`
`;
});
html_product =
`
` +
html_product_variations +
`
`;
} else {
html_product =
`
`;
}
$("#product_results_list").append(html_product);
});
if (!result.length) {
if (search_product.offset) {
$("#product_results_list").append(
`No more results...`
);
} else {
$("#product_results_list").append(
`No results...`
);
}
} else {
productSelectionEvent();
}
resetEl();
},
error: function () {
resetEl();
toast("error", "Error has occurred. Try again.");
},
});
}
function initProductSearchElements() {
$("#txt_product").on("focusin", function () {
$(".product_result").addClass("active").removeClass("hidden");
if (!search_product.offset && !search_product.count) {
searchProducts();
} else {
$(".product_result").find(".has-loading").hide();
// Logger.d(search_product.list)
}
});
$("#txt_product").on(
"keyup",
delay(function (e) {
search_product.offset = 0;
search_product.count = 0;
searchProducts();
}, 1000)
);
$("#btn_product_next").on("click", function () {
search_product.offset = search_product.offset + search_product.limit;
searchProducts();
});
$("#btn_product_prev").on("click", function () {
search_product.offset = search_product.offset - search_product.limit;
searchProducts();
});
$(document).mouseup(function (e) {
var product_box = $(".product_box");
// if the target of the click isn't the container nor a descendant of the container
if (!product_box.is(e.target) && product_box.has(e.target).length === 0) {
$(".product_result").removeClass("active").addClass("hidden");
}
});
}
//////////////////////////////////////////////////// BUYER SECTION
function buyerSelectionEvent() {
$(".buyer-row").each(function (i, v) {
const $findcustomer = $(".findcustomer");
const $selected_customer = $(".selected_customer");
const $selected_customer_image = $(".selected_customer_image");
const $selected_customer_name = $(".selected_customer_name");
const $selected_customer_email_href = $(".selected_customer_email_href");
const $selected_customer_email = $(".selected_customer_email");
const $selected_customer_addresses = $(".selected_customer_addresses");
const $address_info = $(".address_info");
$remove_buyer = $(".remove_buyer");
//address
$(this).on("click", function () {
selected_buyer = search_buyer.list[i];
selected_buyer.address_id = null;
// Logger.d(selected_buyer);
$findcustomer.hide();
$selected_customer_image.attr(
"alt",
selected_buyer.first_name + " " + selected_buyer.last_name
);
$selected_customer_name.text(
selected_buyer.first_name + " " + selected_buyer.last_name
);
$selected_customer_email_href.attr(
"href",
"mailto:" + selected_buyer.email
);
$selected_customer_email.text(selected_buyer.email);
//fetch address
$.ajax({
url: "/admin/users/addresses/" + selected_buyer.id,
data: {
csrf_token: $("#csrf_token").val(),
},
type: "POST",
dataType: "json",
beforeSend: function () {
selected_buyer.address_id = null;
$selected_customer_addresses.empty();
$address_info.empty();
},
success: function (result) {
if (result.length) {
$.each(result, function (i, v) {
const full_address = v.line1 + " " + v.city + " " + v.province;
$selected_customer_addresses.append(
``
);
});
$selected_customer_addresses.on("change", function () {
const index = $selected_customer_addresses[0].selectedIndex;
if (!result[index]) {
return false;
}
selected_buyer.address_id = result[index].id;
// console.log(selected_buyer.address_id);
$address_info.empty();
$address_info.append(
`
` +
result[index].firstName +
" " +
result[index].lastName +
`
` +
result[index].mobile +
`
` +
result[index].line1 +
" " +
result[index].city +
" " +
result[index].province +
`
`
);
});
$selected_customer_addresses.trigger("change");
}
orderButton();
},
error: function () {
orderButton();
toast("error", "Error has occurred. Try again.");
},
});
$remove_buyer.on("click", function () {
selected_buyer = null;
search_buyer.offset = 0;
search_buyer.list = [];
$selected_customer.hide();
$findcustomer.show();
orderButton();
});
$selected_customer.show();
});
});
}
function searchBuyer() {
if (ajax_search_buyer !== null) {
ajax_search_buyer.abort();
}
var resetEl = function () {
if (search_buyer.offset <= 0) {
$("#btn_buyer_prev").prop("disabled", true);
} else {
$("#btn_buyer_prev").prop("disabled", false);
}
if (search_buyer.list.length <= 0) {
$("#btn_buyer_next").prop("disabled", true);
} else {
$("#btn_buyer_next").prop("disabled", false);
}
$(".buyer_result").find(".has-loading").hide();
};
ajax_search_buyer = $.ajax({
url: "/admin/users/list",
data: {
csrf_token: $("#csrf_token").val(),
search: $("#txt_buyer").val(),
offset: search_buyer.offset,
limit: search_buyer.limit,
},
type: "POST",
dataType: "json",
beforeSend: function () {
$("#buyer_results_list").empty();
$(".buyer_result").find(".has-loading").show();
$("#btn_product_prev", "#btn_product_next").prop("disabled", true);
search_buyer.list = [];
},
success: function (result) {
search_buyer.list = result;
$.each(result, function (i, v) {
var html_buyers = ``;
html_buyers =
`
` +
v.first_name +
" " +
v.last_name +
`
`;
$("#buyer_results_list").append(html_buyers);
});
if (!result.length) {
if (search_buyer.offset) {
$("#buyer_results_list").append(
`No more results...`
);
} else {
$("#buyer_results_list").append(
`No results...`
);
}
} else {
buyerSelectionEvent();
}
resetEl();
},
error: function () {
resetEl();
toast("error", "Error has occurred. Try again.");
},
});
}
function initBuyerSearchElements() {
$("#txt_buyer").on("focusin", function () {
$(".buyer_result").addClass("active").removeClass("hidden");
if (!search_buyer.offset && !search_buyer.list.length) {
searchBuyer();
} else {
$(".buyer_result").find(".has-loading").hide();
// Logger.d(search_product.list)
}
});
$("#txt_buyer").on(
"keyup",
delay(function (e) {
search_buyer.offset = 0;
search_buyer.list = [];
searchBuyer();
}, 1000)
);
$("#btn_buyer_next").on("click", function () {
search_buyer.offset = search_buyer.offset + search_buyer.limit;
searchBuyer();
});
$("#btn_buyer_prev").on("click", function () {
search_buyer.offset = search_buyer.offset - search_buyer.limit;
searchBuyer();
});
$(document).mouseup(function (e) {
var buyer_box = $(".buyer_box");
// if the target of the click isn't the container nor a descendant of the container
if (!buyer_box.is(e.target) && buyer_box.has(e.target).length === 0) {
$(".buyer_result").removeClass("active").addClass("hidden");
}
});
}
function createOrder(payment_type) {
$.ajax({
url: "/admin/orders/payment",
data: {
csrf_token: $("#csrf_token").val(),
},
type: "POST",
dataType: "json",
beforeSend: function () {
$modal_container.empty();
},
success: function (result) {
$modal_container.html(result.data);
var $modal_order = $("#modal_order");
$modal_order.modal("show");
// alert("debugger");
$modal_order.on("hidden.bs.modal", function () {
$(this).remove();
});
var $payment_method = $("#payment_method");
var $btn_save_order = $("#btn_save_order");
var $modal_order_amount = $(".modal_order_amount");
$payment_method.on("change", function () {
if ($.inArray($(this).val(), result.payments) != -1) {
selected_payment = $(this).val();
}
});
$payment_method.trigger("change");
$modal_order_amount.text(
(payment_type == "completed" ? "Paid amount: " : "Pending amount: ") +
currencyNumberFormat(totalAmount())
);
$btn_save_order.on("click", function () {
$.ajax({
url: "/admin/orders/store",
data: {
csrf_token: $("#csrf_token").val(),
data: {
store: selected_products,
buyer: selected_buyer,
payment: { channel: selected_payment, status: payment_type },
},
},
type: "POST",
dataType: "json",
beforeSend: function () {
$btn_save_order.prop("disabled", true);
},
success: function (result) {
if (parseInt(result.status)) {
$remove_buyer.trigger("click");
selected_products = [];
displaySelectedProduct();
window.location.replace("/admin/orders");
$modal_order.modal("hide");
return false;
} else {
toast("error", result.message);
$btn_save_order.prop("disabled", false);
}
},
error: function () {
toast("error", "Error has occurred. Try again.");
$btn_save_order.prop("disabled", false);
},
});
});
},
error: function () {
toast("error", "Error has occurred. Try again.");
},
});
}
$(function () {
initProductSearchElements();
initBuyerSearchElements();
});
})();