Subversion Repositories SmartDukaan

Rev

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