Subversion Repositories SmartDukaan

Rev

Rev 32267 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(document).on('change', "#selectPriceDropStatus", function () {
    var status = this.value;
    if (status == 'rejected') {
        $('.mk_rejection_reason').val('').show();
    } else {
        $('.mk_rejection_reason').val('').hide();
    }
});
$(document).on('click', "#processPDButton", function () {
    var status = $("#selectPriceDropStatus").val();
    updatePriceDropImeis(status);
});

$(document).on('click', "button.processPayout", function () {
    if ($(".mk_pending")) {

    }
});

$(document).on('click', "button.add-pricing", function () {
    if ($("#typeaheaditem1").val().trim().length == 0) {
        alert("Please choose Item ");
        return;
    }
    var mop = $("#mop").val();
    var dp = $("#dp").val();
    var mrp = $("#mrp").val();
    var tp = $("#tp").val();
    if (isNaN(dp) || isNaN(mop) || isNaN(mrp)) {
        alert("DP/MOP/MRP should be a number");
        return;
    }
        mop = parseFloat(mop),
        tp = parseFloat(tp),
        dp = parseFloat(dp),
        mrp = parseFloat(mrp)
         if  (dp < tp || mop < tp) {
           if(!confirm("DP/MOP  is less than TP, Do you want to continue?")){
            return;
           }
         }
           if( mop < dp ){
            if(!confirm("MOP  is less than DP, Do you want to continue?")){
            return;
           }
         }
    if (mrp != 0 && (mrp < mop || mrp < dp || mrp < tp )) {
        bootbox.alert("DP/MOP/TP should be lower than MRP");
        return;
    }

    getColorsForItems(0, currentItem.itemId, "Update prices for " + currentItem.itemDescription, function (itemIds) {
        console.log(itemIds);

        if (itemIds != null) {
            dataList = [];
            coloredItems.forEach(function (item) {
                console.log(item);
                console.log(itemIds.indexOf(String(item.id)));
                if (itemIds.indexOf(String(item.id)) >= 0) {
                    dataList.push(
                        {
                            id: item.id,
                            mop: mop,
                            dp: dp,
                            mrp: mrp,
                            tp: tp
                        });
                }
            });
            bootbox.confirm("Confirm Update " + currentItem.itemDescription + "?", function (result) {
                if (result) {
                    doPostAjaxRequestWithJsonHandler(
                        context + "/add-pricing", JSON.stringify(dataList),
                        function (response) {
                            if (response == 'true') {
                                bootbox.alert("Prices Successfully Updated");
                            } else {
                                bootbox.alert("Something went wrong pls try after sometime");
                            }
                        }
                    );
                }
            });
        }
    });

});
$(document).on('click', "button.addPriceDrop", function () {
    if ($("#typeaheaditem").val().trim().length == 0) {
        alert("Please choose Item");
        return;
    }
    var newDp = $("#newDp").val();
    var newTp = $("#newTp").val();
    var newMop = $("#newMop").val();
    var affectedOn = $("#affectedDate").val();
    var allColors = $("#allColors").prop('checked');

    if (newDp.length == 0 || parseFloat(newDp) == 0) {
        alert("New Tp should not be empty");
        return;
    }
    if (newTp.length == 0 || parseFloat(newTp) == 0) {
        alert("New Tp should not be empty");
        return;
    }
    if (newMop.length == 0 || parseFloat(newMop) == 0) {
        alert("New Mop should not be empty");
        return;
    }

    if (affectedOn.length == 0) {
        alert("Affected Date Should not be empty");
        return;
    }
    // as of now allColors is set to true. This means that all colors are
    // eligible for price drop
    if (confirm("Are you sure?")) {
        var obj = {
            itemId: currentItem.itemId,
            dp: parseFloat(newDp),
            tp: parseFloat(newTp),
            mop: parseFloat(newMop),
            allColors: true,
            affectedDate: getDatesFromPicker($('#affectedDate')).startDate
        }
        doPostAjaxRequestWithJsonHandler(
            context + "/priceDrop", JSON.stringify(obj),
            function (response) {
                if (response == 'true') {
                    $('#newPriceDropModal').find('button.close').trigger('click');
                    setTimeout(() => {
                        alert("PriceDrop added successfully from " + $("#typeaheaditem").val());
                        loadPriceDrop("main-content");
                    }, 500);
                } else {
                    alert("Invalid values provided could not update");
                }
            }
        );
    }
});
$(document).on('click', 'input.downloadtotalIMEI', function () {
    var priceDropId = $(this).closest('tr').data('pricedropid');
    doAjaxGetDownload(context + "/downloadtotalPriceDropIMEI/" + priceDropId,
        "totalPriceDropIMEI" + priceDropId + ".csv");
});

$(document).on('click', 'button.download-imeis', function () {
    let affectedDate = getDatesFromPicker('#affectedDate').startDate;
    doAjaxGetDownload(context + "/price-drop/imes/download/?affectedDate=" + affectedDate + "&itemId=" + currentItem.itemId, "pricedrop.csv")
});

$(document).on('click', '.download-price-drop-statement-report', function () {
    var startDate = getDatesFromPicker('input[name="priceDropStatementPeriod"]').startDate;
    var endDate = getDatesFromPicker('input[name="priceDropStatementPeriod"]').endDate;
    let endPoint = `${context}/pricedropByDate/download?startDate=${startDate}&endDate=${endDate}`;
    window.location.href = endPoint;
});

$(document).on('click', '.fetch-price-drop-statement-report', function () {

    var startDate = getDatesFromPicker('input[name="priceDropStatementPeriod"]').startDate;
    var endDate = getDatesFromPicker('input[name="priceDropStatementPeriod"]').endDate;

    if (startDate == "" || startDate == undefined) {
        alert("start date is not be empty!!");
        return;
    }
    if (endDate == "" || endDate == undefined) {

        alert("end date is not be empty!!");
        return;
    }

    doGetAjaxRequestHandler(context + "/getPricedropByDate?startDate=" + startDate + "&endDate=" + endDate,
        function (response) {
            $('#table-container').html(response);
        });

});

$(document).on('click', 'button.mk_download_pricing', function () {
    doAjaxGetDownload(context + "/tagListing/download/4", "pricing.xlsx")
});

$(document).on('click', '.mk_auto_process', function () {
    var trElement = $(this).closest('tr');
    var payout = trElement.find('input.partner-payout').val();
    if (isNaN(parseInt(payout)) || parseInt(payout) <= 0) {
        alert("Invalid payout amount");
    }
    if (confirm("Are you sure you want to Process Payout?")) {
        clickedPriceDrop = $(this);
        var priceDropId = trElement.data('pricedropid');
        autoProcess({priceDropId: priceDropId, partnerPayout: payout, activatedOnly: $(this).data('activatedonly')})
    }
});

function priceDropImeis(priceDropId) {


    doAjaxRequestWithJsonHandler(context + "/priceDropImeis/" + priceDropId, 'GET', null, function (response) {
        response = JSON.parse(response);
        holdArr = [];
        response.holdImeis.forEach(function (val, index) {
            holdArr.push('<option>' + val + '</option>')
        });
        $('select.mk_hold').html(holdArr.join('')).find('option');
        $('span.mk_hold_count').html(holdArr.length);


        pendingArr = [];
        response.pendingImeis.forEach(function (val, index) {
            pendingArr.push('<option>' + val + '</option>')
        });
        $('select.mk_pending').html(pendingArr.join('')).find('option');
        $('span.mk_pending_count').html(pendingArr.length);

        approvedArr = [];
        response.approvedImeis.forEach(function (val, index) {
            approvedArr.push('<option>' + val + '</option>')
        });
        $('select.mk_approved').html(approvedArr.join(''));
        $('span.mk_approved_count').html(approvedArr.length);

        rejectedArr = [];
        response.rejectedImeis.forEach(function (val, index) {
            rejectedArr.push('<option>' + val + '</option>')
        });
        $('span.mk_rejected_count').html(rejectedArr.length);
        $('select.mk_rejected').html(rejectedArr.join(''));
        // if(rejected) {}
    });

}

$(document).on('click', '.mk_view_imeis', function () {
    clickedPriceDrop = $(this);
    var trElement = $(this).closest('tr');
    var priceDropId = trElement.data('pricedropid');
    priceDropImeis(priceDropId);

});

$(document).on('click', '.mk_add_payout', function () {
    if (confirm("Are you sure you want to add price Drop?")) {
        var trElement = $(this).closest('tr');
        var priceDropId = trElement.data('pricedropid');
        var partnerPayout = trElement.find('.partner-payout').val();
        var priceDropIn = trElement.find('.price-drop-in').val();

        if (isNaN(partnerPayout) || partnerPayout == 0) {
            alert("Partner Payout can't be 0");
            return false;
        } else if (isNaN(priceDropIn) || priceDropIn == 0) {
            alert("Price Drop can't be 0");
            return false;
        } else {
            let priceDropObj = {
                priceDropIn: parseFloat(priceDropIn),
                partnerPayout: parseFloat(partnerPayout),
                priceDropId: parseInt(priceDropId)
            };
            addPayout(priceDropObj);
        }
    } else {
        return false;
    }
});

function addPayout(priceDropProcessObj) {
    doPostAjaxRequestWithJsonHandler(context + "/price-drop/addPayout", JSON.stringify(priceDropProcessObj), function (response) {
        if (response == 'true') {
            alert("Payout added");
            loadPriceDrop("main-content");
        } else {
            alert("Some error occurred while adding payout.");
        }
    });
}

function autoProcess(priceDropObj) {
    doPostAjaxRequestWithJsonHandler(context + "/processPriceDrop", JSON.stringify(priceDropObj), function (response) {
        if (response == 'true') {
            alert("PriceDrop sends successfully");
            loadPriceDrop("main-content");
        } else {
            alert("No IMEI is Eligible for PriceDrop");
            loadPriceDrop("main-content");
        }
    });
}


function updatePriceDropImeis(status) {
    var imeisInText = [];
    $('input.mk_imeis_text').val().trim().split(',').forEach(function (imei, i) {
        if (imei.trim() !== '') {
            imeisInText.push(imei.trim());
        }
    });

    var pendingArr = new Array();
    $('select.mk_pending option').each(function () {
        pendingArr.push($(this).val());
    });
    var rejectedArr = new Array();
    $('select.mk_rejected option').each(function () {
        rejectedArr.push($(this).val());
    });
    var approvedArr = new Array();
    $('select.mk_approved option').each(function () {
        approvedArr.push($(this).val());
    });
    var allImeisToUpdate;

    if (status === 'rejected') {
        if ($("#statusForm .mk_rejection_reason").val().trim() === "") {
            alert("Please enter rejection reason");
            return;
        }
        allImeisToUpdate = $('select.mk_pending').val();
        for (const imeiInText of imeisInText) {
            if (pendingArr.includes(imeiInText)) {
                allImeisToUpdate.push(imeiInText);
            } else {
                alert("All IMEIs Should be present in pending Status");
                return;
            }
        }
    } else if (status === 'approved') {
        allImeisToUpdate = $('select.mk_pending').val();
        for (const imeiInText of imeisInText) {
            if (pendingArr.includes(imeiInText)) {
                allImeisToUpdate.push(imeiInText);
            } else {
                alert("All IMEIs Should be present in pending Status");
                return;
            }
        }
    } else {
        allImeisToUpdate = $('select.mk_rejected').val();
        for (const imeiInText of imeisInText) {
            if (rejectedArr.includes(imeiInText)) {
                allImeisToUpdate.push(imeiInText);
            } else {
                alert("All IMEIs Should be present in Rejected Status");
                return;
            }
        }
    }

    if (allImeisToUpdate.length === 0) {
        alert('Please choose imeis');
        return;
    }

    if (!confirm("Move selected imeis to " + status + "?")) {
        return;
    }
    var trElement = clickedPriceDrop.closest('tr');
    var priceDropId = trElement.data('pricedropid');

    priceDropImeisObj = {
        priceDropId: priceDropId,
        updatedStatus: status,
        updatedImeis: allImeisToUpdate
    };
    doPostAjaxRequestWithJsonHandler(context + "/updatePriceDropImeis", JSON.stringify(priceDropImeisObj), function (response) {
        if (response == 'true') {
            alert('Imeis updated and processed successfully');
            priceDropImeis(priceDropId);
        } else {
            alert("Error occurred while updating.");
        }
    });


}