Subversion Repositories SmartDukaan

Rev

Rev 26817 | Rev 29487 | 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();
        });

        $(document).on('change', "form#cd input.unitPrice", function() {
                
                var unitPrice = parseFloat($(this).val());
                if (isNaN(unitPrice)){
                        unitPrice = 0;
                }
                if(unitPrice < 0){
                        alert("Invalid unit price");
                        $(this).val(0);
                }
                
                calculateTotalAmount();
        });
        
        
        $(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(){
                $('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 = paeseFloat(sellingPrice);
                } else {
                        price = mop;
                }
                that = this;
                doGetAjaxRequestHandler(context+"/checkplans?price=" + price + "&itemId=" + itemId, function(response){
                        var obj = JSON.parse(response);
                        getInsurancePlansModal(obj);
                        planCheckedOn = that;
                        return;
                });
        });

});


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>
                                </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'));
                if(isNaN(quantity)){
                        quantity = 0;
                }
                console.log("quantity : "+quantity);
                
                var unitPrice = parseFloat($(el).find('.unitPrice').val());
                if(isNaN(unitPrice)){
                        unitPrice = 0;
                }
                console.log("unitPrice : "+unitPrice);

                rowTotal = unitPrice * quantity + 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);
}