Subversion Repositories SmartDukaan

Rev

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