Subversion Repositories SmartDukaan

Rev

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