Subversion Repositories SmartDukaan

Rev

Rev 34581 | 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("#activationType").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');
        var warehouseId = $(this).data('warehouseid');
        console.log('price ', price)

        doGetAjaxRequestHandler(context + "/getPoCatalogsItems?warehouseId=" + warehouseId + "&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.response) + '</td></tr>';

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

// function to build HTML when response is a LIST
function buildStockInfoHtml(responseList) {
        console.log("responseList {}", responseList);
        if (!Array.isArray(responseList) || responseList.length === 0) {
                return '<div>No stock info available.</div>';
        }

        var html = '<div style="padding:10px; background:#f9f9f9; border:1px solid #ccc;">';
        html += '<strong>Stock Details:</strong>';
        html += '<span style="margin-left:15px; color:#dc3545; font-size:12px;"><i class="fa fa-circle"></i> Activated but not billed</span>';
        html += '<table class="table table-bordered" style="margin-top:10px;">';
        html += '<thead><tr><th>Item ID</th><th>Availability</th><th>Description</th><th>Status</th></tr></thead>';
        html += '<tbody>';

        responseList.forEach(function (item) {
                var rowStyle = item.hasActivatedImei ? 'style="background-color: #f8d7da; color: #721c24;"' : '';
                html += '<tr ' + rowStyle + '>';
                html += '<td>' + item.itemId + '</td>';
                html += '<td>' + item.availability + '</td>';
                html += '<td>' + (item.itemDescription && item.itemDescription !== 0 ? item.itemDescription : 'Not Available') + '</td>';
                html += '<td>';
                if (item.hasActivatedImei && item.activatedImeis && item.activatedImeis.length > 0) {
                        html += '<span style="color:#dc3545; font-weight:bold;">Activated - Need to Bill (' + item.activatedImeis.length + ')</span>';
                        html += '<div style="margin-top:5px; padding:5px; background:#fff; border:1px solid #dc3545; border-radius:4px;">';
                        html += '<small><strong>IMEI(s):</strong></small>';
                        item.activatedImeis.forEach(function (imei, index) {
                                html += '<span style="display:inline-block; margin:2px 5px; padding:2px 6px; background:#f8d7da; border-radius:3px; font-size:11px;">' + imei + '</span>';
                        });
                        html += '</div>';
                } else {
                        html += '-';
                }
                html += '</td>';
                html += '</tr>';
        });

        html += '</tbody></table></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.");
                                }
                        }
                );
        }
}