Subversion Repositories SmartDukaan

Rev

Rev 34449 | Rev 34495 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function() {
        $(document).on('click', ".partner-brandwise-detail", function() {
                loadDetails("main-content");
        });

        $(document).on('change', "#activationType", function() {
                var row = $(this).closest("tr");
                var code = $(row).find("td:eq(1)").text();
                var activationType = $(row).find("td:eq(24) option:selected").val();

                if (confirm("Are you sure ") == true) {
                        doPostAjaxRequestHandler(context + "/changeActivationType?code=" + code + "&activationType=" + activationType,
                                function(response) {

                                        console.log(response);

                                        if (response == true) {
                                                loadDetails("main-content");
                                        }

                                });


                }
        });
});


function loadDetails(domId) {

        doGetAjaxRequestHandler(context + "/getPartnersBrandWiseDetail",
                function(response) {
                        $('#' + domId).html(response);
                });
}


// create suggested po cart related js start here

let cart = {};

function renderCart() {
        let cartHtml = '';
        let totalAmount = 0;

        // First loop through cart and calculate totalAmount
        for (const id in cart) {
                const item = cart[id];
                const itemTotal = item.qty * item.price;
                totalAmount += itemTotal;
        }

        // Add total at the top
        if (totalAmount > 0) {
                cartHtml += `<div style="text-align:right;font-weight:bold; margin-bottom: 10px;">Total: &#8377; ${totalAmount.toFixed(2)}</div><hr style="margin: 4px 0;border-top: 1px solid #000">`;
        }

        // Now loop again to render items
        for (const id in cart) {
                const item = cart[id];
                const itemTotal = item.qty * item.price;

                cartHtml += `
                        <li style="display: flex; align-items: center; justify-content: space-between; padding: 5px 0;">
                                <div style="flex: 1; min-width: 200px;">${item.model} <br> <span style="color: gray;">- ${item.color}</div>
                                <div style="display: flex; align-items: center; gap: 5px;">
                                        <button class="dec-btn" data-id="${id}">-</button>
                                        <span class="cart-qty">${item.qty}</span>
                                        <button class="inc-btn" data-id="${id}">+</button>
                                        <button class="remove-btn btn btn-xs btn-danger" data-id="${id}">x</button>
                                </div>
                                <span style="width: 80px; text-align: right;">&#8377; ${itemTotal.toFixed(2)}</span>
                        </li>`;
        }

        $('#custom-cart-section').html(cartHtml);
}

$(document).on('click', '.po-radio-button', function () {
        const selectedRadio = $(this).find('input[type="radio"]');

        const itemId = selectedRadio.data('itemid');
        const model = selectedRadio.data('modelnumber');
        const color = selectedRadio.data('color');
        const price = parseFloat(selectedRadio.data('price')) || 0;

        const key = itemId;

        if (cart[key]) {
                cart[key].qty += 1;
        } else {
                cart[key] = {model, color, qty: 1, price};
        }

        renderCart();
});


$(document).on('click', '.inc-btn', function () {
        const id = $(this).data('id');
        cart[id].qty += 1;
        renderCart();
});

$(document).on('click', '.dec-btn', function () {
        const id = $(this).data('id');
        if (cart[id].qty > 1) {
                cart[id].qty -= 1;
        } else {
                delete cart[id];
        }
        renderCart();
});

$(document).on('click', '.remove-btn', function () {
        const id = $(this).data('id');
        delete cart[id];
        renderCart();
});

$(document).on('click', '.add-to-cart-btn', function () {
        var price = $(this).data('price');
        var catalogId = $(this).data('catalogid');
        console.log('price ', price)

        doGetAjaxRequestHandler(context + "/getPoCatalogsItems?price=" + price + "&catalogId=" + catalogId, function (response) {

                $('#poItemsDetail .modal-content').html(response);

        });
});

$(document).on('click', '.get-stock-info', function () {
        var $clickedTd = $(this);
        var $clickedTr = $clickedTd.closest('tr');

        var catalogId = $clickedTd.data('catalogid');
        var fofoId = $clickedTd.data('fofoid');

        // If already expanded, remove it
        if ($clickedTr.next().hasClass('expanded-row')) {
                $clickedTr.next().remove();
                return;
        }

        doGetAjaxRequestHandler(context + "/getItemsByCatalog?fofoId=" + fofoId + "&catalogId=" + catalogId, function (response) {
                console.log('response', response);

                // Remove old expanded rows
                $('.expanded-row').remove();

                var expandHtml = '<tr class="expanded-row"><td colspan="10">' + buildStockInfoHtml(response) + '</td></tr>';

                $clickedTr.after(expandHtml);
        });
});

function buildStockInfoHtml(response) {
        if (!response) {
                return '<div>No stock info available.</div>';
        }

        // if it's a single object
        var html = '<div style="padding:10px; background:#f1f1f1; border:1px solid #ccc;">';
        html += '<strong>Stock Info:</strong><br>';
        html += '<ul>';
        html += '<li><strong>Item ID:</strong> ' + response.itemId + '</li>';
        html += '<li><strong>Availability:</strong> ' + response.availability + '</li>';

        if (response.itemDescription && response.itemDescription !== 0) {
                html += '<li><strong>Description:</strong> ' + response.itemDescription + '</li>';
        } else {
                html += '<li><strong>Description:</strong> Not available</li>';
        }

        html += '</ul></div>';

        return html;
}



$(document).on('click', '#save-po-btn', function () {
        console.log("Save PO button clicked!");
        const fofoId = parseInt($('#fofo-id').val());
        console.log('fofoId:', fofoId);
        saveSuggestedPo(fofoId);
});


function saveSuggestedPo(fofoId) {
        var jsonObject = {};
        jsonObject.poIds = [];

        // Select <li> directly, not from inside <ul>
        $("#custom-cart-section li").each(function () {
                const itemId = $(this).find(".dec-btn").data("id");
                const qty = parseInt($(this).find(".cart-qty").text().trim());

                if (!isNaN(itemId) && qty > 0) {
                        const item = {
                                itemId: itemId,
                                qty: qty
                        };
                        jsonObject.poIds.push(item);
                }
        });

        if (jsonObject.poIds.length === 0) {
                alert("Cart is empty!");
                return;
        }

        console.log("Sending PO Data:", jsonObject);

        if (confirm("Are you sure you want to save this Suggested PO?")) {
                doPostAjaxRequestWithJsonHandler(
                        context + "/createSuggestedPo?fofoId=" + fofoId,
                        JSON.stringify(jsonObject.poIds),
                        function (response) {
                                if (response === 'true' || response.includes("true")) {
                                        alert("Suggested PO created successfully!");
                                        cart = {};
                                        renderCart();
                                } else {
                                        alert("Failed to create Suggested PO.");
                                }
                        }
                );
        }
}