Subversion Repositories SmartDukaan

Rev

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