Subversion Repositories SmartDukaan

Rev

Rev 32468 | Rev 33149 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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