Subversion Repositories SmartDukaan

Rev

Rev 34405 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed


function initializeSelect(selector, type='catalogs') {
    if (type === 'catalogs') {
        $(selector).multiselect('destroy');
        $(selector).multiselect({
            includeSelectAllOption: false,
            multiple: false,
            maxHeight: 200,
            buttonWidth: '100%',
            numberDisplayed: 1,
            nonSelectedText: '-- Select One --',
            nSelectedText: ' - Selected',
            enableFiltering: true,
            enableCaseInsensitiveFiltering: true
        });
    } else {
        $(selector).select2({
            placeholder: "Select or add new",
            tags: true,
            createTag: params => ({ id: params.term, text: params.term, newOption: true }),
            templateResult: data => data.newOption ? $('<span class="new-option">Add: ' + data.text + '</span>') : data.text,
        });
    }
}

function validateRequiredFields(fields) {
    let isValid = true;
    fields.each(function () {
        if (!$(this).val()) {
            isValid = false;
            $(this).addClass('is-invalid');
        } else {
            $(this).removeClass('is-invalid');
        }
    });
    return isValid;
}

$(document).on('click', ".liquidation-list", function () {
    doGetAjaxRequestHandler(context + "/liquidation", (response) => {
            $('#main-content').html(response);
            initializeSelect('#warehouseId, #catalogId, #status')
            $('#liquidation-table').DataTable();

        });
});

$(document).on('click', ".save-liquidation", function () {
    const params = serializeFormToJson('form#biddingForm');
    doAjaxRequestWithJsonHandler(context + "/liquidation", "POST", JSON.stringify(params), (response) => {
        if (response) {
            $('.liquidation-list').trigger('click');
            $('#manageLiquidationModal').modal('hide');
            alert("Saved successfully");
        } else {
            alert("Something went wrong!");
        }
    });
});

$(document).on('click', ".delete-liquidation", function() {
    const id = parseInt($(this).data('id')) || 0;
    if (confirm("Are you sure you want to delete this record")) {
        if (id > 0) {
            doDeleteAjaxRequestHandler(`${context}/liquidation/${id}`, (response) => {
                if (response) {
                    $(this).parent('td').closest('tr').remove();
                    $('.liquidation-list').trigger('click');
                    alert("Deleted successfully");
                } else {
                    alert("Something went wrong!");
                }
            });
        } else {
            $(this).parent('td').closest('tr').remove();
        }
    }
});


$(document).on('change', "#warehouseId", function() {
    doAjaxRequestWithJsonHandler(`${context}/liquidation/warehouse/${$(this).val()}`, "GET", null, (response) => {
        if (response) {
            $('#catalogId').html("");
            let options = `<option value="">Select</option>`;
            if (response.products.length){
                response.products.forEach(product => {
                    options+= `<option value="${product.catalogId}">${product.brand + ' ' +product.modelNumber + ' ('+product.shaholicNetAvailability+')'}</option>`;
                });
            }
            $('#catalogId').html(options);
            initializeSelect('#catalogId');
            console.log("Warehouse response: ",response)
        } else {
            alert("Something went wrong!");
        }
    });
    loadCatalogOptions($(this).val(), 'select#super_catalog_name', 'superCatalogs');
});

function editLiquidation(id) {
    doGetAjaxRequestHandler(`${context}/liquidation/${id}`, (response) => {
        if (response){
            $('#manageLiquidationModal').modal('show');
            let names = Object.keys(response);
            if (names.length){
                names.forEach(name => {
                    console.log("map",$(`#${name}`).val(response[name]))
                    $(`#${name}`).val(response[name]);
                })
            }
        }
        //initializeSelect('#warehouseId, #catalogId, #status')
    });
}

function deleteLiquidation(id) {
 if (confirm("Are you sure want to delete this?")){
     console.log(`${id} Call delete method here`);
 }
}