Subversion Repositories SmartDukaan

Rev

Rev 30730 | Rev 32329 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 30730 Rev 31274
Line 1... Line 1...
1
$(function () {
1
$(function () {
2
	$(document).on('click', 'button.mk_plan_select', function () {
-
 
3
		$itemDetails = $('div.itemdetails');
-
 
4
		if ($itemDetails.find('.dgram').val() === '') {
-
 
5
			bootbox.alert('Mobile RAM is required');
-
 
6
			return;
-
 
7
		}
-
 
8
		if ($itemDetails.find('.dgmemory').val() === '') {
-
 
9
			bootbox.alert('Mobile Memory is required');
-
 
10
			return;
-
 
11
		}
-
 
12
		if ($itemDetails.find('#dgmfgdate').val() === '') {
-
 
13
			bootbox.alert('Mobile Mfg Date is required');
-
 
14
			return;
-
 
15
		}
-
 
16
		$orderItemRow = $(planCheckedOn).closest('td');
-
 
17
		$orderItemRow.find('.insuranceid').val($(this).data('key'));
-
 
18
		$orderItemRow.find('.insuranceamount').val($(this).data('amount'));
-
 
19
		$orderItemRow.find('.ram').val($itemDetails.find('.dgram').val());
-
 
20
		$orderItemRow.find('.memory').val($itemDetails.find('.dgmemory').val());
-
 
21
		$orderItemRow.find('.mfgdate').val(mfgDate);
-
 
22
		$('#mobilePlansModal').modal('hide');
-
 
23
		calculateTotalAmount();
-
 
24
	});
-
 
25
 
-
 
26
	//Price change handler
2
    //Price change handler
27
	$(document).on('change', "form#cd input.unitPrice,form#cd input.discount", function () {
3
    $(document).on('change', "form#cd input.unitPrice,form#cd input.discount", function () {
28
 
4
 
29
		var unitPrice = parseFloat($('form#cd input.unitPrice').val());
5
        var unitPrice = parseFloat($('form#cd input.unitPrice').val());
30
		if (isNaN(unitPrice)) {
6
        if (isNaN(unitPrice)) {
31
			unitPrice = 0;
7
            unitPrice = 0;
32
		}
8
        }
33
		var discount = parseFloat($('form#cd input.discount').val());
9
        var discount = parseFloat($('form#cd input.discount').val());
34
		var maxDiscount = parseFloat($('form#cd input.discount').data('maxdiscount'));
10
        var maxDiscount = parseFloat($('form#cd input.discount').data('maxdiscount'));
35
		if (isNaN(discount)) {
11
        if (isNaN(discount)) {
36
			discount = 0;
12
            discount = 0;
37
		}
13
        }
38
		if (isNaN(maxDiscount)) {
14
        if (isNaN(maxDiscount)) {
39
			maxDiscount = 0;
15
            maxDiscount = 0;
40
		}
16
        }
41
		if (discount > maxDiscount) {
17
        if (discount > maxDiscount) {
42
			alert("Discount cant be greater than max Discount");
18
            alert("Discount cant be greater than max Discount");
43
			$('form#cd input.discount').val(maxDiscount);
19
            $('form#cd input.discount').val(maxDiscount);
44
			$(this).focus();
20
            $(this).focus();
45
			return;
21
            return;
46
		}
22
        }
47
		if (unitPrice < 0 || (unitPrice > 0 && unitPrice - discount < 0)) {
23
        if (unitPrice < 0 || (unitPrice > 0 && unitPrice - discount < 0)) {
48
			alert("Invalid unit/discount price");
24
            alert("Invalid unit/discount price");
49
			$(this).focus();
25
            $(this).focus();
50
			return;
26
            return;
51
		}
27
        }
52
 
28
 
53
		calculateTotalAmount();
29
        calculateTotalAmount();
54
		$('.mk_check_plans').trigger('click', ['manual']);
30
        $('.mk_check_plans').trigger('click', ['manual']);
55
	});
31
    });
56
 
32
 
57
 
33
 
58
	$(document).on('change', "form#cd input.insuranceamount", function () {
34
    $(document).on('change', "form#cd input.insuranceamount", function () {
59
		calculateTotalAmount();
35
        calculateTotalAmount();
60
	});
36
    });
61
 
37
 
62
	$(document).on('click', ".create-order", function () {
38
    $(document).on('click', ".create-order", function () {
63
		checkout("main-content");
39
        checkout("main-content");
64
	});
40
    });
65
 
41
 
66
	$(document).on('click', ".mk_check_plans", function (event, source) {
42
    $(document).on('click', ".mk_check_plans", function (event, source) {
67
		$('div.itemdetails').find('input').val('');
43
        $('div.itemdetails').find('input').val('');
-
 
44
        let price = mop;
68
		var mop = parseFloat($(this).data("mop"));
45
        var mop = parseFloat($(this).data("mop"));
69
		var sellingPrice = $(this).val();
46
        var sellingPrice = $(this).val();
70
		var itemId = $(this).closest(".input-group").find("input.insuranceamount").attr("itemid");
47
        var itemId = $(this).closest(".input-group").find("input.insuranceamount").attr("itemid");
71
		if (!isNaN(sellingPrice) && parseFloat(sellingPrice) >= mop) {
48
        if (!isNaN(sellingPrice) && parseFloat(sellingPrice) >= mop) {
72
			price = parseFloat(sellingPrice);
49
            price = parseFloat(sellingPrice);
73
		} else {
50
        } else {
74
			price = mop;
51
            price = mop;
75
		}
52
        }
76
		var discount = parseFloat($(this).closest("tr").find("input.discount").val());
53
        var discount = parseFloat($(this).closest("tr").find("input.discount").val());
77
		if (!isNaN(discount)) {
54
        if (!isNaN(discount)) {
78
			price = price - discount;
55
            price = price - discount;
79
		}
56
        }
80
		that = this;
57
        let that = this;
81
		doGetAjaxRequestHandler(context + "/checkplans?price=" + price + "&itemId=" + itemId, function (response) {
58
        doGetAjaxRequestHandler(`${context}/checkplans?price=${price}&itemId=${itemId}`, function (response) {
82
			var obj = JSON.parse(response);
59
            var obj = JSON.parse(response);
83
			if (obj != null) {
60
            if (obj != null) {
84
				getInsurancePlansModal(obj);
61
                getInsurancePlansModal(obj, that);
85
				planCheckedOn = that;
-
 
86
				return;
62
                return;
87
			} else {
63
            } else {
88
				if (typeof source === "undefined") {
64
                if (typeof source === "undefined") {
89
					alert("Product is not eligible for insurance");
65
                    alert("Product is not eligible for insurance");
90
				}
66
                }
91
			}
67
            }
92
		});
68
        });
93
	});
69
    });
94
 
70
 
95
});
71
});
96
 
72
 
-
 
73
class InsuranceElement extends React.Component {
-
 
74
    planChecked;
-
 
75
 
-
 
76
    constructor(props) {
-
 
77
        super(props);
-
 
78
    }
-
 
79
 
-
 
80
    render() {
-
 
81
        console.log(this.props);
-
 
82
        return <table className="table table-sm">
-
 
83
            <thead>
-
 
84
            <tr>
-
 
85
                <th>Plan Name</th>
-
 
86
                <th>Duration</th>
-
 
87
                <th>DP</th>
-
 
88
                <th>Selling Price</th>
-
 
89
                <th>Action</th>
-
 
90
            </tr>
-
 
91
            </thead>
-
 
92
            <tbody>
-
 
93
            {Object.keys(this.props.plans).map((planName, i) =>
-
 
94
                this.props.plans[planName].map((plan, i) =>
-
 
95
                    <tr key={i}>
-
 
96
                        <td>{planName}</td>
-
 
97
                        <td>{plan.duration}</td>
-
 
98
                        <td>{plan.dp}</td>
-
 
99
                        <td>{plan.premium}</td>
-
 
100
                        <td>
-
 
101
                            <button className="btn btn-primary" data-key={plan.productId}
-
 
102
                                    data-amount={plan.premium} onClick={this.planChecked}>Add
-
 
103
                            </button>
-
 
104
                        </td>
-
 
105
                    </tr>))}
-
 
106
            </tbody>
-
 
107
        </table>;
-
 
108
    }
-
 
109
 
-
 
110
    planChecked = (e) => {
-
 
111
        debugger;
-
 
112
        let $itemDetails = $('div.itemdetails');
-
 
113
        if ($itemDetails.find('.dgram').val() === '') {
-
 
114
            bootbox.alert('Mobile RAM is required');
-
 
115
            return;
-
 
116
        }
-
 
117
        if ($itemDetails.find('.dgmemory').val() === '') {
-
 
118
            bootbox.alert('Mobile Memory is required');
-
 
119
            return;
-
 
120
        }
-
 
121
        if ($itemDetails.find('#dgmfgdate').val() === '') {
-
 
122
            bootbox.alert('Mobile Mfg Date is required');
-
 
123
            return;
-
 
124
        }
-
 
125
        let $orderItemRow = $(this.props.elePlanCheckedOn).closest('td');
-
 
126
        $orderItemRow.find('.insuranceid').val($(e.target).data('key'));
-
 
127
        $orderItemRow.find('.insuranceamount').val($(e.target).data('amount'));
-
 
128
        $orderItemRow.find('.ram').val($itemDetails.find('.dgram').val());
-
 
129
        $orderItemRow.find('.memory').val($itemDetails.find('.dgmemory').val());
-
 
130
        $orderItemRow.find('.mfgdate').val(window.mfgDate);
-
 
131
        $('#mobilePlansModal').modal('hide');
-
 
132
        calculateTotalAmount();
-
 
133
    }
-
 
134
}
-
 
135
 
-
 
136
let reactRoot = null;
97
 
137
 
98
function getInsurancePlansModal(insurancePlans) {
138
function getInsurancePlansModal(insurancePlans, elePlanCheckedOn) {
99
	var htmlArr = [];
-
 
100
	for (var key in insurancePlans) {
139
    const domContainer = document.querySelector('.insurancedetails');
101
		templateArr = [];
-
 
102
		var plansList = insurancePlans[key];
140
    reactRoot = ReactDOM.createRoot(domContainer);
103
		for (var index in plansList) {
-
 
104
			var plan = plansList[index];
-
 
105
			console.log(plan);
-
 
106
			var template =
-
 
107
				`<div class="col-lg-3">
-
 
108
					<div class="thumbnail">
-
 
109
						<img class="card-img-top" src="${logosmapping.mobile_insurance_providers[plan.providerId]}" alt="${plan.providerName}">
141
    reactRoot.render(<InsuranceElement plans={insurancePlans} elePlanCheckedOn={elePlanCheckedOn}/>)
110
			      		<div class="caption" style="padding:9px 0 0">
-
 
111
						    <div style="margin:0 0 -2px -2px">
-
 
112
						    	<button class="btn btn-lg btn-default mk_plan_select" style="width:100%" 
-
 
113
						    		data-key="${plan.productId}"
-
 
114
						    		data-amount="${plan.premium}"
-
 
115
						    	>${plan.duration}@ Rs.<span class="currency">${plan.premium}</span>
-
 
116
						    	</button>
-
 
117
						    </div>
-
 
118
						</div>
-
 
119
						<div>
-
 
120
							Your Landing(DP) - ${plan.dp}
-
 
121
						</div>
-
 
122
					</div>
-
 
123
				</div>`;
-
 
124
			templateArr.push(template);
-
 
125
			if (index % 4 == 3 || +index + 1 == plansList.length) {
-
 
126
				templateArr = [`<div class="row col-lg-12">${templateArr.join('')}</div>`];
-
 
127
			}
-
 
128
		}
-
 
129
		htmlArr.push(`<div class="row"><h4>${key}</h4>${templateArr.join('')}</div>`);
-
 
130
	}
-
 
131
	$('#mobilePlansModal').find('.insurancedetails').html(htmlArr.join(''));
-
 
132
	$('#mobilePlansModal').modal({show: true});
142
    $('#mobilePlansModal').modal({show: true});
133
 
143
 
134
}
144
}
135
 
145
 
136
 
146
 
137
function calculateTotalAmount() {
147
function calculateTotalAmount() {
138
	var netPayableAmount = 0;
148
    var netPayableAmount = 0;
139
	var totaInsuranceAmount = 0;
149
    var totaInsuranceAmount = 0;
140
	$("#cd").find('tr:gt(0)').each(function (index, el) {
150
    $("#cd").find('tr:gt(0)').each(function (index, el) {
141
		var rowTotal = 0;
151
        var rowTotal = 0;
142
		$tr = $(el);
152
        let $tr = $(el);
143
		var insuranceAmount = $tr.find('input.insuranceamount').val();
153
        var insuranceAmount = $tr.find('input.insuranceamount').val();
144
		if (isNaN(insuranceAmount)) {
154
        if (isNaN(insuranceAmount)) {
145
			insuranceAmount = 0;
155
            insuranceAmount = 0;
146
		} else {
156
        } else {
147
			insuranceAmount = parseFloat(insuranceAmount);
157
            insuranceAmount = parseFloat(insuranceAmount);
148
		}
158
        }
149
 
159
 
150
		var quantity = parseFloat($(el).find('.unitPrice').attr('quantity'));
160
        var quantity = parseFloat($(el).find('.unitPrice').attr('quantity'));
151
 
161
 
152
		var unitPrice = parseFloat($(el).find('.unitPrice').val());
162
        var unitPrice = parseFloat($(el).find('.unitPrice').val());
153
		if (isNaN(unitPrice)) {
163
        if (isNaN(unitPrice)) {
154
			unitPrice = 0;
164
            unitPrice = 0;
155
		}
165
        }
156
 
166
 
157
		var discount = $(el).find('.discount').val();
167
        var discount = $(el).find('.discount').val();
158
		if (isNaN(discount)) {
168
        if (isNaN(discount)) {
159
			discount = 0;
169
            discount = 0;
160
		}
170
        }
161
 
171
 
162
		rowTotal = unitPrice - discount + insuranceAmount;
172
        rowTotal = unitPrice - discount + insuranceAmount;
163
		$tr.find('.totalPrice').val(rowTotal);
173
        $tr.find('.totalPrice').val(rowTotal);
164
		netPayableAmount += rowTotal;
174
        netPayableAmount += rowTotal;
165
		totaInsuranceAmount += insuranceAmount;
175
        totaInsuranceAmount += insuranceAmount;
166
	});
176
    });
167
	if (totaInsuranceAmount > 0) {
177
    if (totaInsuranceAmount > 0) {
168
		$(".mk_insurance_row").show();
178
        $(".mk_insurance_row").show();
169
	} else {
179
    } else {
170
		$(".mk_insurance_row").hide();
180
        $(".mk_insurance_row").hide();
171
	}
181
    }
172
	$('#cd').find('input.netPayableAmount').val(netPayableAmount);
182
    $('#cd').find('input.netPayableAmount').val(netPayableAmount);
173
}
183
}