Subversion Repositories SmartDukaan

Rev

Rev 33625 | Rev 34210 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
33660 amit.gupta 1
$(document).ready(function () {
2
    $(document).on('change', 'select.criteria-brands', function () {
3
        changed = true;
4
    });
5
    $(document).on('click', ".scheme_offer", function () {
6
        loadOffer("main-content");
7
    });
8
    $(document).on('click', '#yearmonth-submit', function () {
9
        let yearMonth = $("#yearmonth").val();
10
        loadOfferHistory('#main-content', yearMonth);
27875 amit.gupta 11
 
33660 amit.gupta 12
    });
27875 amit.gupta 13
 
33660 amit.gupta 14
    $(document).on('click', 'a.mk_partner_view', function () {
15
        let $offerTr = $(this).closest('tr');
16
        let offerId = $offerTr.data('offerid');
17
        if (typeof (offerId) == "undefined") {
18
            offerId = $(this).closest('th').data('offerid');
19
            let idx = $(this).closest('th').data('idx');
20
            priceCircularTable.order([[parseInt(idx), 'desc']]).draw();
21
        }
22
        doGetAjaxRequestHandler(`${context}/getOfferMargins?offerId=${offerId}`, function (data) {
23
            $('#offerDescription .modal-content').html(data);
24
            $("#offerDescription").modal('show');
25
        });
26
    });
30485 tejbeer 27
 
33660 amit.gupta 28
    $(document).on('click', 'a.mk_offer_detail', function () {
29
        let $offerTr = $(this).closest('tr');
30
        let offerId = $offerTr.data('offerid');
31
        doGetAjaxRequestHandler(`${context}\getOfferMargins?offerId=${offerId}`, function (data) {
32
            $('#offerDescription .modal-content').html(data);
33
            $("#offerDescription").modal('show');
34
        });
35
    });
36
    $(document).on('click', 'a.offer_history', function () {
37
        let dt = new Date();
38
        loadOfferHistory('#main-content', new Date().toISOString().slice(0, 7));
39
    });
40
    $(document).on('click', 'input.create-offer', function () {
41
        console.log("Click called");
42
        createOfferNew();
43
    });
30485 tejbeer 44
 
33660 amit.gupta 45
    $(document).on('click', 'a.process_offer', function () {
46
        let $offerTr = $(this).closest('tr');
47
        let offerId = $offerTr.data('offerid');
48
        $clicked = $(this);
49
        if (confirm("Are your sure you want to process?")) {
50
            $clicked.hide();
51
            doGetAjaxRequestHandler(`${context}/offer/process/${offerId}`, function () {
52
                alert("Offer Processed successfully");
53
                $clicked.show();
54
            });
55
        }
56
    });
30485 tejbeer 57
 
33660 amit.gupta 58
    $(document).on('change', 'input[name=schemeType]', function () {
59
        if ($('input[name=schemeType]').val() == 'ACTIVATION') {
60
            $("div.activation-brands").show();
61
        } else {
62
            $("div.activation-brands").hide();
63
        }
64
    });
65
    $(document).on('change', 'input[name^=amountType]', function () {
66
        let holder1 = "Qty";
67
        let holder2 = "pc(s) onwards, ";
68
        let holder3 = "% of value";
69
        if ($(this).val() == "FIXED") {
70
            holder3 = "per pc";
71
        } else if ($(this).val() == "SLAB_FIXED") {
72
            holder3 = "";
73
        }
74
        let slabHtml = `<div class="input-group slab">
27875 amit.gupta 75
        	<span class="input-group-addon">${holder1}</span>
76
        	<input  type="text" class="slabOnwardsAmount form-control"  value="0"/>
77
        	<span class="input-group-addon">${holder2}</span>
78
        	<input type="text" class="form-control payoutValue"  value="0"/>
79
        	<span class="input-group-addon">${holder3}</span>
80
        	</div>`;
33660 amit.gupta 81
        $(this).closest('.itemcriteriapayout').find('div.slab-container').html(slabHtml.repeat(9));
82
        //targetSlabTemplate = $('div.targetslabs').get(0).outerHTML;
83
    });
27875 amit.gupta 84
});
33660 amit.gupta 85
 
27875 amit.gupta 86
function changeTargetType() {
33660 amit.gupta 87
    let holder1 = "Rs.";
88
    let holder2 = "onwards,";
89
    if ($(this).val() == "QUANTITY") {
90
        holder1 = "Qty";
91
        holder2 = "pc(s) onwards, ";
92
    }
93
    let targetHtml = `<span class="input-group-addon">${holder1}</span>
27895 amit.gupta 94
                                    <input type="text" style="width:100px" class="form-control payoutTarget" value="0">
95
                                    <span class="input-group-addon">${holder2}</span>
96
                                    <input type="text" class="form-control targetDescription" placeholder="Description">`;
33660 amit.gupta 97
    $('div.targetContainer').html(targetHtml);
27895 amit.gupta 98
}
33660 amit.gupta 99
 
27895 amit.gupta 100
function changePayoutType() {
33660 amit.gupta 101
    let holder1 = "Rs.";
102
    let holder2 = "onwards,";
103
    let holder3 = "% of value";
104
    $allSlabContainers = $('div.slab-container');
105
    $('div.slab-payout input:radio[value="PERCENTAGE"]').prop('checked', true);
106
    if ($(this).val() == "QUANTITY") {
107
        holder1 = "Qty";
108
        holder2 = "pc(s) onwards, ";
109
        $('div.slab-payout input:radio[value!="PERCENTAGE"]').prop('checked', false).prop('disabled', false);
110
    } else {
111
        $('div.slab-payout input:radio[value!="PERCENTAGE"]').prop('checked', false).prop('disabled', true);
112
    }
113
    let slabHtml = `<div class="input-group slab">
27875 amit.gupta 114
    	<span class="input-group-addon">${holder1}</span>
115
    	<input  type="text" class="slabOnwardsAmount form-control"  value="0"/>
116
    	<span class="input-group-addon">${holder2}</span>
117
    	<input type="text" class="form-control payoutValue"  value="0"/>
118
    	<span class="input-group-addon">${holder3}</span>
119
    	</div>`;
33660 amit.gupta 120
    $allSlabContainers.html(slabHtml.repeat(9));
27875 amit.gupta 121
}
33660 amit.gupta 122
 
123
$(document).on('click', 'a.mk_offer_active', function () {
124
    $clicked = $(this);
125
    let offerId = $clicked.closest('tr').data("offerid");
126
    if (confirm(`Are you sure want to activate ${offerId}?`)) {
127
        doGetAjaxRequestHandler(`${context}/offer/active/${offerId}`, function (response) {
128
            alert("Offer is now active");
129
            $clicked.closest("li").html(`<a class="mk_offer_inactive" href="javascript:void(0)">Mark Inactive</a>`);
130
        });
131
    }
27875 amit.gupta 132
});
33660 amit.gupta 133
$(document).on('click', 'a.mk_offer_inactive', function () {
134
    $clicked = $(this);
135
    let offerId = $clicked.closest('tr').data("offerid");
136
    if (confirm(`Are you sure want to activate ${offerId}?`)) {
137
        doGetAjaxRequestHandler(`${context}/offer/active/${offerId}?active=false`, function (response) {
138
            alert("Offer is now inactive");
139
            $clicked.closest("li").html(`<a class="mk_offer_active" href="javascript:void(0)">Mark Active</a>`);
140
        });
141
    }
29907 amit.gupta 142
});
33660 amit.gupta 143
$(document).on('click', 'button.slab-add', function () {
144
    newTargetSlab = $(targetSlabTemplate);
145
    $('div.targetslabscontainer').append(newTargetSlab);
146
    newTargetSlab.find("select.criteria-brands").toggle().next().remove();
147
    newTargetSlab.find("select.criteria-brands").chosen({no_results_text: "Oops, nothing found!"});
27875 amit.gupta 148
});
33660 amit.gupta 149
$(document).on('click', 'button.slab-remove', function () {
150
    targetSlabContainer = $('div.targetslabscontainer');
151
    if (targetSlabContainer.find(".targetslabs").length > 1) {
152
        targetSlabContainer.find(".targetslabs:last").remove();
153
    } else {
154
        alert("Cant remove single Slab");
155
    }
27875 amit.gupta 156
});
33660 amit.gupta 157
$(document).on('click', 'button.slab-copy', function () {
158
    newTargetSlab = $('div.targetslabs').eq(0).clone().appendTo('div.targetslabscontainer');
159
    catalogIdSelectContainers = $('div.targetslabs').eq(0).find('select.criteria-catalogids');
160
    newCatalogIdSelectContainers = newTargetSlab.find('select.criteria-catalogids');
161
    for (let i = 0; i < 9; i++) {
162
        let selectContainer = catalogIdSelectContainers.get(i);
163
        let newSelectContainer = newCatalogIdSelectContainers.get(i);
164
        for (let j = 0; j < selectContainer.options.length; j++) {
165
            newSelectContainer.options[j].selected = selectContainer.options[j].selected;
166
        }
167
        newSelectContainer.closest('span').replaceWith(newSelectContainer);
168
        $(newSelectContainer).multiselect({
169
            includeSelectAllOption: true,
170
            maxHeight: 400,
171
            buttonWidth: '240px',
172
            numberDisplayed: 1,
173
            nonSelectedText: 'Items',
174
            nSelectedText: ' - Items Selected',
175
            allSelectedText: 'All Items Selected',
176
            enableFiltering: true,
177
            enableCaseInsensitiveFiltering: true,
178
        });
179
    }
180
    selectContainers = newTargetSlab.find("select.criteria-brands");
181
    selectContainers.each((y, x) => {
182
        $next = $(x).toggle().next();
183
        $next.find("a.search-choice-close").each(function (index, ele) {
184
            x.options[$(ele).data('option-array-index') + 0].selected = true;
185
        });
186
        $next.remove();
187
    });
188
    newTargetSlab.find("select.criteria-brands").chosen({no_results_text: "Oops, nothing found!"});
189
    //createSlabFrom(currentHtml);
27875 amit.gupta 190
});
191
 
33660 amit.gupta 192
$(document).on('click', 'select.criteria-catalogids+div > button', function () {
193
    if (changed) {
194
        let brands = $(this).closest('.item-criteria').find('select').val();
195
        let led = $(this).closest('.item-criteria').find('input[name=led]').is(":checked");
196
        let tablet = $(this).closest('.item-criteria').find('input[name=tablet]').is(":checked");
197
        let smartWatch = $(this).closest('.item-criteria').find('input[name=smartwatch]').is(":checked");
198
        $brandsContainer = $(this);
199
        let categoryId = 3;
200
        if (led) {
201
            categoryId = 14202;
202
        }
203
        if (tablet) {
204
            categoryId = 10010;
205
        }
206
        if (smartWatch) {
207
            categoryId = 6;
208
        }
209
        var url = `${context}/entity?brands=${brands.join(",")}&categoryId=${categoryId}&limit=0`;
210
        $select = $brandsContainer.closest('.item-criteria').find('.criteria-catalogids');
211
        $select.html('');
212
        if (brands.length > 0) {
213
            doGetAjaxRequestHandler(url, function (response) {
214
                data = JSON.parse(response);
215
                for (i = 0; i < data.length; i++) {
216
                    $select.append(`<option value="${data[i].catalogId_i}">${data[i].title_s}</option>`);
217
                }
218
                $select.multiselect('rebuild');
219
            });
220
        } else {
221
            $select.html('');
222
            $select.multiselect('rebuild');
223
        }
224
        changed = !changed;
225
    }
27875 amit.gupta 226
});
227
 
30485 tejbeer 228
var ckEditor;
229
 
27875 amit.gupta 230
function createOfferNew() {
33660 amit.gupta 231
    let validated = true;
232
    let rangePicker = getDatesFromPicker('input[name="dateRange"]');
233
    let createOfferRequest = {};
234
    createOfferRequest['name'] = $("#offer_name").val();
235
    createOfferRequest['startDate'] = rangePicker.startDate;
236
    createOfferRequest['endDate'] = rangePicker.endDate;
237
    createOfferRequest['offerNotes'] = $("#offer_notes").val();
238
    createOfferRequest['baseCriteria'] = $("input[name=base_criteria]").is(":checked");
239
    createOfferRequest['terms'] = ckEditor.getData() //$("#offer_terms").val();
240
    createOfferRequest['targetType'] = $("input[name=targetType]:checked").val();
241
    createOfferRequest['payoutType'] = $("input[name=payoutType]:checked").val();
242
    createOfferRequest['schemeType'] = $("select[name=schemeType]").val();
243
    createOfferRequest['booster'] = false;
244
    createOfferRequest['activationBrands'] = null;
32065 amit.gupta 245
    createOfferRequest['discount'] = $("input[name=discount]").is(':checked');
33660 amit.gupta 246
    if (createOfferRequest['schemeType'] == 'ACTIVATION') {
247
        createOfferRequest['activationBrands'] = $("div.activation-brands select").val().join(",") || null;
248
    } else if (createOfferRequest['schemeType'] == 'SELLINBOOSTER') {
249
        createOfferRequest['schemeType'] = 'SELLIN';
250
        createOfferRequest['booster'] = true;
251
    }
252
    createOfferRequest['sellinPercentage'] = $("#sellinPercentage").val();
253
    createOfferRequest['brandShareTerms'] = $('#brandShare').val();
254
    createOfferRequest['partnerCriteria'] = {};
255
    createOfferRequest['partnerCriteria']['fofoIds'] = ($("#partners").val() || []).map(Number);
256
    createOfferRequest['partnerCriteria']['excludeFofoIds'] = [];
257
    let excludeFofoIdChecked = $('input.excludeFofoId').is(':checked');
258
    if (excludeFofoIdChecked) {
259
        createOfferRequest['partnerCriteria']['excludeFofoIds'] = createOfferRequest['partnerCriteria']['fofoIds'];
260
        createOfferRequest['partnerCriteria']['fofoIds'] = [];
261
    }
262
    createOfferRequest['partnerCriteria']['partnerTypes'] = ($("select.criteria-partner-type").val() || []);
263
    createOfferRequest['partnerCriteria']['regionIds'] = ($("select.criteria-warehouseregion").val() || []).map(Number);
27875 amit.gupta 264
 
33660 amit.gupta 265
    createOfferRequest['itemCriteria'] = itemCriteria($('div.itemCriteria'));
266
    let targetSlabs = [];
267
    $('div.targetslabs').each(function (index, ele) {
268
        let $targetSlab = $(ele);
269
        let targetOnwardsValue = $targetSlab.find('.payoutTarget').val();
270
        let targetDescription = $targetSlab.find('.targetDescription').val();
271
        let itemCriteriaPayouts = [];
272
        if (isNaN(targetOnwardsValue) || targetOnwardsValue < 0) {
273
            validated = false;
274
            alert(`Invalid Number - ${targetOnwardsValue}`);
275
        } else {
276
            if (index == 0 || targetOnwardsValue > 0) {
277
                let $itemCriteriaPayoutContainers = $targetSlab.find('.itemcriteriapayout');
278
                $itemCriteriaPayoutContainers.each(function (index, itemCriteriaPayoutContainer) {
279
                    let $itemCriteriaPayoutContainer = $(itemCriteriaPayoutContainer);
280
                    let amountType = $itemCriteriaPayoutContainer.find('input[name^=amountType]:checked').val();
281
                    let startDate = $itemCriteriaPayoutContainer.find('input.startDate').val();
282
                    let endDate = $itemCriteriaPayoutContainer.find('input.endDate').val();
283
                    if ((startDate == "" && endDate != "") && (startDate != "" && endDate == "")) {
284
                        alert("Criteria specific date is required");
285
                        return false;
286
                    }
287
                    let payoutSlabs = [];
288
                    let itemCriteria1 = itemCriteria($itemCriteriaPayoutContainer);
289
                    let $payoutSlabContainers = $itemCriteriaPayoutContainer.find('.slab');
290
                    let itemCriteriaChanged = itemCriteria1['smartPhone'] || itemCriteria1['featuredPhone']
291
                        || itemCriteria1['catalogIds'].length > 0 || itemCriteria1['brands'].length > 0
292
                        || itemCriteria1['excludeCatalogIds'].length > 0
293
                        || itemCriteria1['startPrice'] > 0 || itemCriteria1['endPrice'] > itemCriteria1['startPrice'] > 0;
294
                    if (itemCriteriaChanged || index == 0) {
295
                        $payoutSlabContainers.each(function (slabContainerIndex, slabContainer) {
296
                            let $slab = $(slabContainer);
30485 tejbeer 297
 
33660 amit.gupta 298
                            let slabOnwardsAmount = $slab.find('.slabOnwardsAmount').val();
299
                            if (isNaN(slabOnwardsAmount) || slabOnwardsAmount < 0) {
300
                                validated = false;
301
                                alert(`Invalid Payout Slab Amount - ${slabOnwardsAmount}`);
302
                            } else {
303
                                if (slabContainerIndex == 0 || slabOnwardsAmount > 0) {
304
                                    let payoutAmount = $slab.find('.payoutValue').val();
305
                                    if (isNaN(payoutAmount) || payoutAmount <= 0) {
306
                                        validated = false;
307
                                        alert(`Invalid Payout Slab Amount - ${slabOnwardsAmount}`);
308
                                    }
309
                                    let payoutSlab = {
310
                                        onwardsAmount: Number(slabOnwardsAmount),
311
                                        payoutAmount: Number(payoutAmount)
312
                                    }
313
                                    payoutSlabs.push(payoutSlab);
314
                                }
315
                            }
316
                        });
317
                        let itemCriteriaPayout = {
318
                            itemCriteria: itemCriteria1,
319
                            payoutSlabs: payoutSlabs,
320
                            amountType: amountType,
321
                            startDate: jsonStartDate(startDate),
322
                            endDate: jsonEndDate(endDate)
323
                        };
324
                        itemCriteriaPayouts.push(itemCriteriaPayout);
325
                    }
326
                });
327
                targetSlabs.push({
328
                    onwardsAmount: targetOnwardsValue,
329
                    targetDescription: targetDescription,
330
                    itemCriteriaPayouts: itemCriteriaPayouts
331
                });
332
            }
333
        }
334
    });
335
    createOfferRequest['targetSlabs'] = targetSlabs;
336
    if (validated && confirm("Confirm creating scheme offer")) {
337
        console.log("Validated");
338
        console.log(createOfferRequest);
339
        doPostAjaxRequestWithJsonHandler(`${context}/createOffer`, JSON.stringify(createOfferRequest), function (response) {
340
            alert("Offer created successfully");
341
            let dt = new Date();
342
            loadOfferHistory('#main-content', new Date().toISOString().slice(0, 7));
343
        });
344
    } else {
345
        console.log("Invalid Offer");
346
    }
27875 amit.gupta 347
}
348
 
349
function itemCriteria($container) {
33660 amit.gupta 350
    let smartPhone = $container.find("input[name=smartphone]").is(":checked");
351
    let featuredPhone = $container.find("input[name=featuredphone]").is(":checked");
352
    let led = $container.find("input[name=led]").is(":checked");
353
    let smartWatch = $container.find("input[name=smartwatch]").is(":checked");
354
    let catalogIds = $container.find('.criteria-catalogids').val() || [];
355
    let excludeCatalogIds = [];
356
    let excludeChecked = $container.find('input.exclude').is(':checked');
357
    if (excludeChecked) {
358
        excludeCatalogIds = catalogIds;
359
        catalogIds = [];
360
    }
361
    let brands = $container.find('.criteria-brands').val() || [];
362
    return {
363
        startPrice: Number($container.find('.startValue').val()),
364
        endPrice: Number($container.find('.endValue').val()),
365
        catalogIds: catalogIds.map(Number),
366
        excludeCatalogIds: excludeCatalogIds.map(Number),
367
        brands: brands,
368
        smartPhone: smartPhone,
369
        featuredPhone: featuredPhone,
370
        smartWatch: smartWatch,
371
        led: led
372
    };
27875 amit.gupta 373
}
374
 
375
function loadOffer(domId) {
33660 amit.gupta 376
    console.log(domId);
377
    doGetAjaxRequestHandler(context + "/getCreateOffer", function (response) {
378
        $('#' + domId).html(response);
30485 tejbeer 379
 
33660 amit.gupta 380
        DecoupledEditor
381
            .create(document.querySelector('#offer_terms'))
30485 tejbeer 382
 
383
 
33660 amit.gupta 384
            .then(newEditor => {
385
                const toolbarContainer = document.querySelector('#toolbar-container');
30485 tejbeer 386
 
33660 amit.gupta 387
                toolbarContainer.appendChild(newEditor.ui.view.toolbar.element);
30485 tejbeer 388
 
33660 amit.gupta 389
                ckEditor = newEditor
390
            })
391
            .catch(error => {
392
                console.error(error);
393
            });
30485 tejbeer 394
 
33660 amit.gupta 395
    });
396
}
30485 tejbeer 397
 
27875 amit.gupta 398
function loadOfferHistory(domId, yearMonth) {
33660 amit.gupta 399
    doGetAjaxRequestHandler(`${context}/offerHistory?yearMonth=${yearMonth}`, function (response) {
400
        $(domId).html(response);
401
        $(domId).find("#yearmonth").val(yearMonth);
402
    });
27875 amit.gupta 403
}
404