Subversion Repositories SmartDukaan

Rev

Rev 33437 | Rev 34168 | 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
 
33795 ranu 38
    $(document).on('click', ".upgrade-offer", function () {
39
        var itemId = $(this).data('itemid');
40
        doGetAjaxRequestHandler(`${context}/getItemWiseUpgradeOffer?itemId=` + itemId, function (response) {
41
            $('#upgradeOfferModal .modal-content').html(response);
42
        });
43
    });
30725 amit.gupta 44
 
33149 ranu 45
 
33795 ranu 46
 
31274 amit.gupta 47
    $(document).on('change', "form#cd input.insuranceamount", function () {
48
        calculateTotalAmount();
49
    });
30725 amit.gupta 50
 
31274 amit.gupta 51
    $(document).on('click', ".create-order", function () {
52
        checkout("main-content");
53
    });
30725 amit.gupta 54
 
31274 amit.gupta 55
    $(document).on('click', ".mk_check_plans", function (event, source) {
33149 ranu 56
        console.log("checkPlansssssss99999");
32434 amit.gupta 57
        let lines = $("#order-items").find('tbody>tr:gt(0)').length;
32414 amit.gupta 58
        if (lines > 3) {
59
            //No need to check plans
60
            return;
61
        }
31274 amit.gupta 62
        $('div.itemdetails').find('input').val('');
63
        let price = mop;
64
        var mop = parseFloat($(this).data("mop"));
65
        var sellingPrice = $(this).val();
66
        var itemId = $(this).closest(".input-group").find("input.insuranceamount").attr("itemid");
67
        if (!isNaN(sellingPrice) && parseFloat(sellingPrice) >= mop) {
68
            price = parseFloat(sellingPrice);
69
        } else {
70
            price = mop;
71
        }
33437 ranu 72
        var poiId = parseFloat($(this).closest("tr").find("input.poiId").val());
31274 amit.gupta 73
        var discount = parseFloat($(this).closest("tr").find("input.discount").val());
74
        if (!isNaN(discount)) {
75
            price = price - discount;
76
        }
77
        let that = this;
33437 ranu 78
        doGetAjaxRequestHandler(`${context}/checkplans?price=${price}&itemId=${itemId}&poiId=${poiId}`, function (response) {
31274 amit.gupta 79
            var obj = JSON.parse(response);
80
            if (obj != null) {
81
                getInsurancePlansModal(obj, that);
82
                return;
83
            } else {
84
                if (typeof source === "undefined") {
85
                    alert("Product is not eligible for insurance");
86
                }
87
            }
88
        });
89
    });
30725 amit.gupta 90
 
22245 ashik.ali 91
});
23343 ashik.ali 92
 
31274 amit.gupta 93
class InsuranceElement extends React.Component {
94
    planChecked;
23343 ashik.ali 95
 
31274 amit.gupta 96
    constructor(props) {
97
        super(props);
98
    }
24440 amit.gupta 99
 
31274 amit.gupta 100
    render() {
101
        console.log(this.props);
102
        return <table className="table table-sm">
103
            <thead>
104
            <tr>
105
                <th>Plan Name</th>
106
                <th>Duration</th>
107
                <th>DP</th>
108
                <th>Selling Price</th>
109
                <th>Action</th>
110
            </tr>
111
            </thead>
112
            <tbody>
113
            {Object.keys(this.props.plans).map((planName, i) =>
114
                this.props.plans[planName].map((plan, i) =>
115
                    <tr key={i}>
116
                        <td>{planName}</td>
117
                        <td>{plan.duration}</td>
118
                        <td>{plan.dp}</td>
119
                        <td>{plan.premium}</td>
120
                        <td>
121
                            <button className="btn btn-primary" data-key={plan.productId}
122
                                    data-amount={plan.premium} onClick={this.planChecked}>Add
123
                            </button>
124
                        </td>
125
                    </tr>))}
126
            </tbody>
127
        </table>;
128
    }
129
 
130
    planChecked = (e) => {
131
        debugger;
132
        let $itemDetails = $('div.itemdetails');
133
        if ($itemDetails.find('.dgram').val() === '') {
134
            bootbox.alert('Mobile RAM is required');
135
            return;
136
        }
137
        if ($itemDetails.find('.dgmemory').val() === '') {
138
            bootbox.alert('Mobile Memory is required');
139
            return;
140
        }
141
        if ($itemDetails.find('#dgmfgdate').val() === '') {
142
            bootbox.alert('Mobile Mfg Date is required');
143
            return;
144
        }
145
        let $orderItemRow = $(this.props.elePlanCheckedOn).closest('td');
146
        $orderItemRow.find('.insuranceid').val($(e.target).data('key'));
147
        $orderItemRow.find('.insuranceamount').val($(e.target).data('amount'));
148
        $orderItemRow.find('.ram').val($itemDetails.find('.dgram').val());
149
        $orderItemRow.find('.memory').val($itemDetails.find('.dgmemory').val());
150
        $orderItemRow.find('.mfgdate').val(window.mfgDate);
151
        $('#mobilePlansModal').modal('hide');
152
        calculateTotalAmount();
153
    }
24440 amit.gupta 154
}
155
 
31274 amit.gupta 156
let reactRoot = null;
24440 amit.gupta 157
 
31274 amit.gupta 158
function getInsurancePlansModal(insurancePlans, elePlanCheckedOn) {
159
    const domContainer = document.querySelector('.insurancedetails');
160
    reactRoot = ReactDOM.createRoot(domContainer);
161
    reactRoot.render(<InsuranceElement plans={insurancePlans} elePlanCheckedOn={elePlanCheckedOn}/>)
162
    $('#mobilePlansModal').modal({show: true});
163
 
164
}
165
 
166
 
32468 amit.gupta 167
function calculateTotalAmount(inputEle) {
168
    if (typeof inputEle !== "undefined") {
169
        let itemId = $(inputEle).attr("itemid");
170
        let unitPrice = inputEle.value;
171
        $("#order-items").find(`input.unitPrice[itemid=${itemId}]`).each(function () {
172
            this.value = unitPrice;
173
        })
174
 
175
    }
31274 amit.gupta 176
    var netPayableAmount = 0;
177
    var totaInsuranceAmount = 0;
32329 amit.gupta 178
    $("#order-items").find('tbody>tr:gt(0)').each(function (index, el) {
31274 amit.gupta 179
        var rowTotal = 0;
180
        let $tr = $(el);
181
        var insuranceAmount = $tr.find('input.insuranceamount').val();
182
        if (isNaN(insuranceAmount)) {
183
            insuranceAmount = 0;
184
        } else {
185
            insuranceAmount = parseFloat(insuranceAmount);
186
        }
30725 amit.gupta 187
 
31274 amit.gupta 188
        var quantity = parseFloat($(el).find('.unitPrice').attr('quantity'));
30725 amit.gupta 189
 
31274 amit.gupta 190
        var unitPrice = parseFloat($(el).find('.unitPrice').val());
191
        if (isNaN(unitPrice)) {
192
            unitPrice = 0;
193
        }
30725 amit.gupta 194
 
31274 amit.gupta 195
        var discount = $(el).find('.discount').val();
196
        if (isNaN(discount)) {
197
            discount = 0;
198
        }
23343 ashik.ali 199
 
32329 amit.gupta 200
        rowTotal = (unitPrice - discount + insuranceAmount) * quantity;
31274 amit.gupta 201
        $tr.find('.totalPrice').val(rowTotal);
202
        netPayableAmount += rowTotal;
203
        totaInsuranceAmount += insuranceAmount;
204
    });
205
    if (totaInsuranceAmount > 0) {
206
        $(".mk_insurance_row").show();
207
    } else {
208
        $(".mk_insurance_row").hide();
209
    }
210
    $('#cd').find('input.netPayableAmount').val(netPayableAmount);
23343 ashik.ali 211
}
33795 ranu 212
 
213
$(document).on('change', '.offerSelect', function () {
214
    calculateAndUpdatePaymentOptions();
215
});
216
 
217
function calculateAndUpdatePaymentOptions() {
218
    let totalSchemePayout = 0;
219
    let totalPartnerPayout = 0;
220
 
221
    // Loop through all selected options in each row
222
    $('.offerSelect').each(function () {
223
        let selectedOption = $(this).find('option:selected');
224
 
225
        let schemePayout = parseFloat(selectedOption.data('scheme-payout')) || 0;
226
        let partnerPayout = parseFloat(selectedOption.data('partner-payout')) || 0;
227
 
228
        // Accumulate the payouts
229
        totalSchemePayout += schemePayout;
230
        totalPartnerPayout += partnerPayout;
231
    });
232
 
233
    // Now update the payment options with the accumulated values
234
    updatePaymentOptions(totalSchemePayout, totalPartnerPayout);
235
}
236
 
237
function updatePaymentOptions(schemePayout, partnerPayout) {
238
    $("#payment-option-id-amount-container").find('.paymentOptionAmount').each(function () {
239
        let paymentOptionName = $(this).closest('.row').find('h5').text().trim();
240
 
241
        if (paymentOptionName.includes('SAMSUNG UPGRADE')) {
242
            $(this).val(schemePayout);  // Set the total scheme payout
243
        }
244
 
245
        if (paymentOptionName.includes('CASH DISCOUNT')) {
246
            $(this).val(partnerPayout);  // Set the total partner payout
247
        }
248
    });
249
}
250
 
251
 
252
 
253
 
254
 
255
 
256