Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
27875 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);
11
 
12
    });
13
 
29681 amit.gupta 14
    $(document).on('click', 'a.mk_partner_view', function() {
27875 amit.gupta 15
    	let $offerTr = $(this).closest('tr');
16
        let offerId = $offerTr.data('offerid');
29789 amit.gupta 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
        }
29189 amit.gupta 22
        doGetAjaxRequestHandler(`${context}/getOfferMargins?offerId=${offerId}`, function(data) {
27875 amit.gupta 23
        	$('#offerDescription .modal-content').html(data);
24
        	$("#offerDescription").modal('show');
25
        });
26
    });
29681 amit.gupta 27
 
28
    $(document).on('click', 'a.mk_offer_detail', function() {
29
    	let $offerTr = $(this).closest('tr');
30
        let offerId = $offerTr.data('offerid');
29876 amit.gupta 31
        doGetAjaxRequestHandler(`${context}\getOfferMargins?offerId=${offerId}`, function(data) {
29681 amit.gupta 32
        	$('#offerDescription .modal-content').html(data);
33
        	$("#offerDescription").modal('show');
34
        });
35
    });
27875 amit.gupta 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
    });
44
 
45
    $(document).on('click', 'a.download_offer', function () {
46
        let $offerTr = $(this).closest('tr');
47
        let offerId = $offerTr.data('offerid');
48
        let fileName = `offer-${$offerTr.find('td:first')}.csv`;
49
        doAjaxGetDownload(`${context}\offerDownload?offerId=${offerId}`, fileName);
50
    });
51
 
52
    $(document).on('change', 'input[name=schemeType]', function () {
53
		if($('input[name=schemeType]:checked').val()=='ACTIVATION') {
54
			$("div.activation-brands").show();
55
		} else {
56
			$("div.activation-brands").hide();
57
		}
58
    });
27879 amit.gupta 59
    $(document).on('change', 'input[name^=amountType]', function () {
27875 amit.gupta 60
    	let holder1 = "Qty";
61
		let holder2 = "pc(s) onwards, ";
62
    	let holder3 = "% of value";
63
    	if($(this).val()=="FIXED") {
64
    		holder3 = "per pc";
29173 amit.gupta 65
    	} else if ($(this).val()=="SLAB_FIXED") {
29174 amit.gupta 66
    		holder3 = "";
29173 amit.gupta 67
		}    	
27875 amit.gupta 68
    	let slabHtml = `<div class="input-group slab">
69
        	<span class="input-group-addon">${holder1}</span>
70
        	<input  type="text" class="slabOnwardsAmount form-control"  value="0"/>
71
        	<span class="input-group-addon">${holder2}</span>
72
        	<input type="text" class="form-control payoutValue"  value="0"/>
73
        	<span class="input-group-addon">${holder3}</span>
74
        	</div>`;
29167 amit.gupta 75
        $(this).closest('.itemcriteriapayout').find('div.slab-container').html(slabHtml.repeat(9));
27875 amit.gupta 76
        //targetSlabTemplate = $('div.targetslabs').get(0).outerHTML;
77
    });
78
});
79
function changeTargetType() {
80
	let holder1 = "Rs.";
81
	let holder2 = "onwards,";
27895 amit.gupta 82
	if ($(this).val() == "QUANTITY") {
83
		holder1 = "Qty";
84
		holder2 = "pc(s) onwards, ";
85
	}
86
	let targetHtml = `<span class="input-group-addon">${holder1}</span>
87
                                    <input type="text" style="width:100px" class="form-control payoutTarget" value="0">
88
                                    <span class="input-group-addon">${holder2}</span>
89
                                    <input type="text" class="form-control targetDescription" placeholder="Description">`;
90
	$('div.targetContainer').html(targetHtml);
91
}
92
function changePayoutType() {
93
	let holder1 = "Rs.";
94
	let holder2 = "onwards,";
27875 amit.gupta 95
	let holder3 = "% of value";
96
	$allSlabContainers = $('div.slab-container');
97
	$('div.slab-payout input:radio[value="PERCENTAGE"]').prop('checked',true);
98
    if ($(this).val() == "QUANTITY") {
99
    		holder1 = "Qty";
100
    		holder2 = "pc(s) onwards, ";
29173 amit.gupta 101
        	$('div.slab-payout input:radio[value!="PERCENTAGE"]').prop('checked',false).prop('disabled',false);
27875 amit.gupta 102
    } else {
29173 amit.gupta 103
    	$('div.slab-payout input:radio[value!="PERCENTAGE"]').prop('checked',false).prop('disabled',true);
27875 amit.gupta 104
    }
105
    let slabHtml = `<div class="input-group slab">
106
    	<span class="input-group-addon">${holder1}</span>
107
    	<input  type="text" class="slabOnwardsAmount form-control"  value="0"/>
108
    	<span class="input-group-addon">${holder2}</span>
109
    	<input type="text" class="form-control payoutValue"  value="0"/>
110
    	<span class="input-group-addon">${holder3}</span>
111
    	</div>`;
29167 amit.gupta 112
    $allSlabContainers.html(slabHtml.repeat(9));
27875 amit.gupta 113
}
114
$(document).on('click', 'a.mk_offer_active', function(){
29915 amit.gupta 115
	$clicked = $(this);
116
	let offerId = $clicked.closest('tr').data("offerid");
117
	if(confirm(`Are you sure want to activate ${offerId}?`)){
118
		doGetAjaxRequestHandler(`${context}/offer/active/${offerId}`, function (response) {
119
			alert("Offer is now active");
120
			$clicked.closest("li").html(`<a class="mk_offer_inactive" href="javascript:void(0)">Mark Inactive</a>`);
121
	    });
122
    }
27875 amit.gupta 123
});
29907 amit.gupta 124
$(document).on('click', 'a.mk_offer_inactive', function(){
29915 amit.gupta 125
	$clicked = $(this);
126
	let offerId = $clicked.closest('tr').data("offerid");
127
	if(confirm(`Are you sure want to activate ${offerId}?`)){
128
		doGetAjaxRequestHandler(`${context}/offer/active/${offerId}?active=false`, function (response) {
129
			alert("Offer is now inactive");
130
			$clicked.closest("li").html(`<a class="mk_offer_active" href="javascript:void(0)">Mark Active</a>`);
131
	    });
132
    }
29907 amit.gupta 133
});
27875 amit.gupta 134
$(document).on('click', 'button.slab-add', function(){
135
	newTargetSlab = $(targetSlabTemplate);
136
	$('div.targetslabscontainer').append(newTargetSlab);	
137
	newTargetSlab.find("select.criteria-brands").toggle().next().remove();
138
	newTargetSlab.find("select.criteria-brands").chosen({no_results_text: "Oops, nothing found!"});
139
});
140
$(document).on('click', 'button.slab-remove', function(){
141
	targetSlabContainer = $('div.targetslabscontainer');
142
	if(targetSlabContainer.find(".targetslabs").length > 1) {
143
		targetSlabContainer.find(".targetslabs:last").remove();
144
	} else {
145
		alert("Cant remove single Slab");
146
	}
147
});
148
$(document).on('click', 'button.slab-copy', function(){
149
	newTargetSlab = $('div.targetslabs').eq(0).clone().appendTo('div.targetslabscontainer');
27975 amit.gupta 150
	catalogIdSelectContainers = $('div.targetslabs').eq(0).find('select.criteria-catalogids');
151
	newCatalogIdSelectContainers = newTargetSlab.find('select.criteria-catalogids');
28950 amit.gupta 152
	for(let i=0;i<9;i++) {
27975 amit.gupta 153
		let selectContainer = catalogIdSelectContainers.get(i);
154
		let newSelectContainer = newCatalogIdSelectContainers.get(i);
155
		for(let j=0;j<selectContainer.options.length;j++) {
156
			newSelectContainer.options[j].selected = selectContainer.options[j].selected;
157
		}
158
		newSelectContainer.closest('span').replaceWith(newSelectContainer);
159
		$(newSelectContainer).multiselect({
160
			includeSelectAllOption : true,
161
			maxHeight : 400,
162
			buttonWidth : '240px',
163
			numberDisplayed : 1,
164
			nonSelectedText : 'Items',
165
			nSelectedText : ' - Items Selected',
166
			allSelectedText : 'All Items Selected',
167
			enableFiltering : true,
168
			enableCaseInsensitiveFiltering : true,
169
		});
170
	}
27875 amit.gupta 171
	selectContainers = newTargetSlab.find("select.criteria-brands");
172
	selectContainers.each((y,x)=>{
173
		$next = $(x).toggle().next();
174
		$next.find("a.search-choice-close").each(function(index,ele){
175
			x.options[$(ele).data('option-array-index') + 0].selected=true; 
176
		});
177
		$next.remove();
178
	});
179
	newTargetSlab.find("select.criteria-brands").chosen({no_results_text: "Oops, nothing found!"});
180
	//createSlabFrom(currentHtml);
181
});
182
 
27916 amit.gupta 183
$(document).on('click', 'select.criteria-catalogids+div > button', function(){
27875 amit.gupta 184
	if(changed) {
27916 amit.gupta 185
		let brands = $(this).closest('.item-criteria').find('select').val();
186
		$brandsContainer = $(this);
27875 amit.gupta 187
		var url = `${context}/entity?brands=${brands.join(",")}&categoryId=3&limit=0`;
188
		$select = $brandsContainer.closest('.item-criteria').find('.criteria-catalogids');
189
		$select.html('');
190
		if(brands.length > 0) {
191
			doGetAjaxRequestHandler(url, function(response) {
192
				data = JSON.parse(response);
193
				for(i = 0; i < data.length; i++) {
194
					$select.append(`<option value="${data[i].catalogId_i}">${data[i].title_s}</option>`);
195
				}
196
				$select.multiselect('rebuild');
197
			});
198
		} else {
199
			$select.html('');
200
			$select.multiselect('rebuild');
201
		}
202
		changed = !changed;
203
	}
204
});
205
 
206
function createOfferNew() {
207
    let validated = true;
208
    let createOfferRequest = {};
209
    createOfferRequest['name'] = $("#offer_name").val();
210
    createOfferRequest['startDate'] = startDate;
211
    createOfferRequest['endDate'] = endDate;
212
    createOfferRequest['offerNotes'] = $("#offer_notes").val();
29795 amit.gupta 213
    createOfferRequest['baseCriteria'] = $("input[name=base_criteria]").is(":checked");
27875 amit.gupta 214
    createOfferRequest['terms'] = $("#offer_terms").val();
215
    createOfferRequest['targetType'] = $("input[name=targetType]:checked").val();
27895 amit.gupta 216
    createOfferRequest['payoutType'] = $("input[name=payoutType]:checked").val();
27875 amit.gupta 217
    createOfferRequest['schemeType'] = $("input[name=schemeType]:checked").val();
218
    if(createOfferRequest['schemeType']=='ACTIVATION') {
219
    	createOfferRequest['activationBrands'] = $("div.activation-brands select").val().join(",") || null;
220
    } else {
221
    	createOfferRequest['activationBrands'] = null;
222
 
223
    }
224
    createOfferRequest['sellinPercentage'] = $("#sellinPercentage").val();
225
    createOfferRequest['brandShareTerms'] = $('#brandShare').val();
226
    createOfferRequest['partnerCriteria'] = {};
227
    createOfferRequest['partnerCriteria']['fofoIds'] = ($("#partners").val() || []).map(Number);
228
    createOfferRequest['partnerCriteria']['partnerTypes'] = ($("select.criteria-partner-type").val() || []);
229
    createOfferRequest['partnerCriteria']['regionIds'] = ($("select.criteria-warehouseregion").val() || []).map(Number);
230
 
231
    createOfferRequest['itemCriteria'] = itemCriteria($('div.itemCriteria'));
232
    let targetSlabs = [];
233
    $('div.targetslabs').each(function (index, ele) {
234
        let $targetSlab = $(ele);
235
        let targetOnwardsValue = $targetSlab.find('.payoutTarget').val();
236
        let targetDescription = $targetSlab.find('.targetDescription').val();
237
        let itemCriteriaPayouts = [];
238
        if (isNaN(targetOnwardsValue) || targetOnwardsValue < 0) {
239
            validated = false;
240
            alert(`Invalid Number - ${targetOnwardsValue}`);
241
        } else {
242
            if (index == 0 || targetOnwardsValue > 0) {
243
                let $itemCriteriaPayoutContainers = $targetSlab.find('.itemcriteriapayout');
244
                $itemCriteriaPayoutContainers.each(function (index, itemCriteriaPayoutContainer) {
245
                    let $itemCriteriaPayoutContainer = $(itemCriteriaPayoutContainer);
246
                    let amountType = $itemCriteriaPayoutContainer.find('input[name^=amountType]:checked').val();
247
                    let payoutSlabs = [];
248
                    let itemCriteria1 = itemCriteria($itemCriteriaPayoutContainer);
249
                    let $payoutSlabContainers = $itemCriteriaPayoutContainer.find('.slab');
28018 amit.gupta 250
                    let itemCriteriaChanged = itemCriteria1['smartPhone'] || itemCriteria1['featuredPhone']
27951 amit.gupta 251
                        || itemCriteria1['catalogIds'].length > 0 || itemCriteria1['brands'].length > 0
27952 amit.gupta 252
                        || itemCriteria1['excludeCatalogIds'].length > 0 
27955 amit.gupta 253
                        || itemCriteria1['startPrice'] > 0 || itemCriteria1['endPrice'] > itemCriteria1['startPrice'] > 0;
27875 amit.gupta 254
                    if (itemCriteriaChanged || index == 0) {
255
                        $payoutSlabContainers.each(function (slabContainerIndex, slabContainer) {
256
                            let $slab = $(slabContainer);
257
 
258
                            let slabOnwardsAmount = $slab.find('.slabOnwardsAmount').val();
259
                            if (isNaN(slabOnwardsAmount) || slabOnwardsAmount < 0) {
260
                                validated = false;
261
                                alert(`Invalid Payout Slab Amount - ${slabOnwardsAmount}`);
262
                            } else {
263
                                if (slabContainerIndex == 0 || slabOnwardsAmount > 0) {
264
                                    let payoutAmount = $slab.find('.payoutValue').val();
265
                                    if (isNaN(payoutAmount) || payoutAmount <= 0) {
266
                                        validated = false;
267
                                        alert(`Invalid Payout Slab Amount - ${slabOnwardsAmount}`);
268
                                    }
269
                                    let payoutSlab = {
270
                                        onwardsAmount: Number(slabOnwardsAmount),
271
                                        payoutAmount: Number(payoutAmount)
272
                                    }
273
                                    payoutSlabs.push(payoutSlab);
274
                                }
275
                            }
276
                        });
277
                        let itemCriteriaPayout = {
278
                            itemCriteria: itemCriteria1,
279
                            payoutSlabs: payoutSlabs,
280
                            amountType: amountType
281
                        };
282
                        itemCriteriaPayouts.push(itemCriteriaPayout);
283
                    }
284
                });
285
                targetSlabs.push({
286
                    onwardsAmount: targetOnwardsValue,
287
                    targetDescription : targetDescription, 
288
                    itemCriteriaPayouts: itemCriteriaPayouts
289
                });
290
            }
291
        }
292
    });
293
    createOfferRequest['targetSlabs'] = targetSlabs;
294
    if (validated && confirm("Confirm creating scheme offer")) {
295
        console.log("Validated");
296
        console.log(createOfferRequest);
297
        doPostAjaxRequestWithJsonHandler(`${context}/createOffer`, JSON.stringify(createOfferRequest), function (response) {
298
            alert("Offer created successfully");
299
            let dt = new Date();
300
            loadOfferHistory('#main-content', new Date().toISOString().slice(0,7));
301
        });
302
    } else {
303
        console.log("Invalid Offer");
304
    }
305
}
306
 
307
function itemCriteria($container) {
28018 amit.gupta 308
	let smartPhone = $container.find("input[name=smartphone]").is(":checked");
309
	let featuredPhone = $container.find("input[name=featuredphone]").is(":checked");
27875 amit.gupta 310
    let catalogIds = $container.find('.criteria-catalogids').val() || [];
311
    let excludeCatalogIds = [];
312
    let excludeChecked = $container.find('input.exclude').is(':checked');
313
    if(excludeChecked) {
314
    	excludeCatalogIds = catalogIds;
315
    	catalogIds = [];
316
    }
317
    let brands = $container.find('.criteria-brands').val() || [];
318
    return {
319
        startPrice: Number($container.find('.startValue').val()),
320
        endPrice: Number($container.find('.endValue').val()),
321
        catalogIds: catalogIds.map(Number),
322
        excludeCatalogIds : excludeCatalogIds.map(Number),
323
        brands: brands,
28018 amit.gupta 324
        smartPhone: smartPhone,
325
        featuredPhone: featuredPhone
27875 amit.gupta 326
    };
327
}
328
 
329
function loadOffer(domId) {
330
    console.log(domId);
331
    doGetAjaxRequestHandler(context + "/getCreateOffer", function (response) {
332
        $('#' + domId).html(response);
333
    });
334
}
335
function loadOfferHistory(domId, yearMonth) {
336
    doGetAjaxRequestHandler(`${context}/offerHistory?yearMonth=${yearMonth}`, function (response) {
337
        $(domId).html(response);
338
        $(domId).find("#yearmonth").val(yearMonth);
339
    });
340
}
341