Subversion Repositories SmartDukaan

Rev

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