Subversion Repositories SmartDukaan

Rev

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

$(function () {
        $(document).on('click', 'button.mk_plan_select', function () {
                $itemDetails = $('div.itemdetails');
                if ($itemDetails.find('.dgram').val() === '') {
                        bootbox.alert('Mobile RAM is required');
                        return;
                }
                if ($itemDetails.find('.dgmemory').val() === '') {
                        bootbox.alert('Mobile Memory is required');
                        return;
                }
                if ($itemDetails.find('#dgmfgdate').val() === '') {
                        bootbox.alert('Mobile Mfg Date is required');
                        return;
                }
                $orderItemRow = $(planCheckedOn).closest('td');
                $orderItemRow.find('.insuranceid').val($(this).data('key'));
                $orderItemRow.find('.insuranceamount').val($(this).data('amount'));
                $orderItemRow.find('.ram').val($itemDetails.find('.dgram').val());
                $orderItemRow.find('.memory').val($itemDetails.find('.dgmemory').val());
                $orderItemRow.find('.mfgdate').val(mfgDate);
                $('#mobilePlansModal').modal('hide');
                calculateTotalAmount();
        });

        //Price change handler
        $(document).on('change', "form#cd input.unitPrice,form#cd input.discount", function () {

                var unitPrice = parseFloat($('form#cd input.unitPrice').val());
                if (isNaN(unitPrice)) {
                        unitPrice = 0;
                }
                var discount = parseFloat($('form#cd input.discount').val());
                var maxDiscount = parseFloat($('form#cd input.discount').data('maxdiscount'));
                if (isNaN(discount)) {
                        discount = 0;
                }
                if (isNaN(maxDiscount)) {
                        maxDiscount = 0;
                }
                if (discount > maxDiscount) {
                        alert("Discount cant be greater than max Discount");
                        $('form#cd input.discount').val(maxDiscount);
                        $(this).focus();
                        return;
                }
                if (unitPrice < 0 || (unitPrice > 0 && unitPrice - discount < 0)) {
                        alert("Invalid unit/discount price");
                        $(this).focus();
                        return;
                }

                calculateTotalAmount();
                $('.mk_check_plans').trigger('click', ['manual']);
        });


        $(document).on('change', "form#cd input.insuranceamount", function () {
                calculateTotalAmount();
        });

        $(document).on('click', ".create-order", function () {
                checkout("main-content");
        });

        $(document).on('click', ".mk_check_plans", function (event, source) {
                $('div.itemdetails').find('input').val('');
                var mop = parseFloat($(this).data("mop"));
                var sellingPrice = $(this).val();
                var itemId = $(this).closest(".input-group").find("input.insuranceamount").attr("itemid");
                if (!isNaN(sellingPrice) && parseFloat(sellingPrice) >= mop) {
                        price = parseFloat(sellingPrice);
                } else {
                        price = mop;
                }
                var discount = parseFloat($(this).closest("tr").find("input.discount").val());
                if (!isNaN(discount)) {
                        price = price - discount;
                }
                that = this;
                doGetAjaxRequestHandler(context + "/checkplans?price=" + price + "&itemId=" + itemId, function (response) {
                        var obj = JSON.parse(response);
                        if (obj != null) {
                                getInsurancePlansModal(obj);
                                planCheckedOn = that;
                                return;
                        } else {
                                if (typeof source === "undefined") {
                                        alert("Product is not eligible for insurance");
                                }
                        }
                });
        });

});


function getInsurancePlansModal(insurancePlans) {
        var htmlArr = [];
        for (var key in insurancePlans) {
                templateArr = [];
                var plansList = insurancePlans[key];
                for (var index in plansList) {
                        var plan = plansList[index];
                        console.log(plan);
                        var template =
                                `<div class="col-lg-3">
                                        <div class="thumbnail">
                                                <img class="card-img-top" src="${logosmapping.mobile_insurance_providers[plan.providerId]}" alt="${plan.providerName}">
                                        <div class="caption" style="padding:9px 0 0">
                                                    <div style="margin:0 0 -2px -2px">
                                                        <button class="btn btn-lg btn-default mk_plan_select" style="width:100%" 
                                                                data-key="${plan.productId}"
                                                                data-amount="${plan.premium}"
                                                        >${plan.duration}@ Rs.<span class="currency">${plan.premium}</span>
                                                        </button>
                                                    </div>
                                                </div>
                                                <div>
                                                        Your Landing(DP) - ${plan.dp}
                                                </div>
                                        </div>
                                </div>`;
                        templateArr.push(template);
                        if (index % 4 == 3 || +index + 1 == plansList.length) {
                                templateArr = [`<div class="row col-lg-12">${templateArr.join('')}</div>`];
                        }
                }
                htmlArr.push(`<div class="row"><h4>${key}</h4>${templateArr.join('')}</div>`);
        }
        $('#mobilePlansModal').find('.insurancedetails').html(htmlArr.join(''));
        $('#mobilePlansModal').modal({show: true});

}


function calculateTotalAmount() {
        var netPayableAmount = 0;
        var totaInsuranceAmount = 0;
        $("#cd").find('tr:gt(0)').each(function (index, el) {
                var rowTotal = 0;
                $tr = $(el);
                var insuranceAmount = $tr.find('input.insuranceamount').val();
                if (isNaN(insuranceAmount)) {
                        insuranceAmount = 0;
                } else {
                        insuranceAmount = parseFloat(insuranceAmount);
                }

                var quantity = parseFloat($(el).find('.unitPrice').attr('quantity'));

                var unitPrice = parseFloat($(el).find('.unitPrice').val());
                if (isNaN(unitPrice)) {
                        unitPrice = 0;
                }

                var discount = $(el).find('.discount').val();
                if (isNaN(discount)) {
                        discount = 0;
                }

                rowTotal = unitPrice - discount + insuranceAmount;
                $tr.find('.totalPrice').val(rowTotal);
                netPayableAmount += rowTotal;
                totaInsuranceAmount += insuranceAmount;
        });
        if (totaInsuranceAmount > 0) {
                $(".mk_insurance_row").show();
        } else {
                $(".mk_insurance_row").hide();
        }
        $('#cd').find('input.netPayableAmount').val(netPayableAmount);
}