Subversion Repositories SmartDukaan

Rev

Rev 29907 | Rev 29915 | 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(){
29912 amit.gupta 115
	let offerId = $(this).closest('tr').data("offerid");
27875 amit.gupta 116
	doGetAjaxRequestHandler(`${context}/offer/active/${offerId}`, function (response) {
117
		alert("Offer is now active");
29907 amit.gupta 118
		$(this).closest("li").html(`<a class="mk_offer_inactive" href="javascript:void(0)">Mark Inactive</a>`);
27875 amit.gupta 119
    });
120
});
29907 amit.gupta 121
$(document).on('click', 'a.mk_offer_inactive', function(){
29912 amit.gupta 122
	let offerId = $(this).closest('tr').data("offerid");
29907 amit.gupta 123
	doGetAjaxRequestHandler(`${context}/offer/active/${offerId}?active=false`, function (response) {
124
		alert("Offer is now inactive");
125
		$(this).closest("li").html(`<a class="mk_offer_active" href="javascript:void(0)">Mark Active</a>`);
126
    });
127
});
27875 amit.gupta 128
$(document).on('click', 'button.slab-add', function(){
129
	newTargetSlab = $(targetSlabTemplate);
130
	$('div.targetslabscontainer').append(newTargetSlab);	
131
	newTargetSlab.find("select.criteria-brands").toggle().next().remove();
132
	newTargetSlab.find("select.criteria-brands").chosen({no_results_text: "Oops, nothing found!"});
133
});
134
$(document).on('click', 'button.slab-remove', function(){
135
	targetSlabContainer = $('div.targetslabscontainer');
136
	if(targetSlabContainer.find(".targetslabs").length > 1) {
137
		targetSlabContainer.find(".targetslabs:last").remove();
138
	} else {
139
		alert("Cant remove single Slab");
140
	}
141
});
142
$(document).on('click', 'button.slab-copy', function(){
143
	newTargetSlab = $('div.targetslabs').eq(0).clone().appendTo('div.targetslabscontainer');
27975 amit.gupta 144
	catalogIdSelectContainers = $('div.targetslabs').eq(0).find('select.criteria-catalogids');
145
	newCatalogIdSelectContainers = newTargetSlab.find('select.criteria-catalogids');
28950 amit.gupta 146
	for(let i=0;i<9;i++) {
27975 amit.gupta 147
		let selectContainer = catalogIdSelectContainers.get(i);
148
		let newSelectContainer = newCatalogIdSelectContainers.get(i);
149
		for(let j=0;j<selectContainer.options.length;j++) {
150
			newSelectContainer.options[j].selected = selectContainer.options[j].selected;
151
		}
152
		newSelectContainer.closest('span').replaceWith(newSelectContainer);
153
		$(newSelectContainer).multiselect({
154
			includeSelectAllOption : true,
155
			maxHeight : 400,
156
			buttonWidth : '240px',
157
			numberDisplayed : 1,
158
			nonSelectedText : 'Items',
159
			nSelectedText : ' - Items Selected',
160
			allSelectedText : 'All Items Selected',
161
			enableFiltering : true,
162
			enableCaseInsensitiveFiltering : true,
163
		});
164
	}
27875 amit.gupta 165
	selectContainers = newTargetSlab.find("select.criteria-brands");
166
	selectContainers.each((y,x)=>{
167
		$next = $(x).toggle().next();
168
		$next.find("a.search-choice-close").each(function(index,ele){
169
			x.options[$(ele).data('option-array-index') + 0].selected=true; 
170
		});
171
		$next.remove();
172
	});
173
	newTargetSlab.find("select.criteria-brands").chosen({no_results_text: "Oops, nothing found!"});
174
	//createSlabFrom(currentHtml);
175
});
176
 
27916 amit.gupta 177
$(document).on('click', 'select.criteria-catalogids+div > button', function(){
27875 amit.gupta 178
	if(changed) {
27916 amit.gupta 179
		let brands = $(this).closest('.item-criteria').find('select').val();
180
		$brandsContainer = $(this);
27875 amit.gupta 181
		var url = `${context}/entity?brands=${brands.join(",")}&categoryId=3&limit=0`;
182
		$select = $brandsContainer.closest('.item-criteria').find('.criteria-catalogids');
183
		$select.html('');
184
		if(brands.length > 0) {
185
			doGetAjaxRequestHandler(url, function(response) {
186
				data = JSON.parse(response);
187
				for(i = 0; i < data.length; i++) {
188
					$select.append(`<option value="${data[i].catalogId_i}">${data[i].title_s}</option>`);
189
				}
190
				$select.multiselect('rebuild');
191
			});
192
		} else {
193
			$select.html('');
194
			$select.multiselect('rebuild');
195
		}
196
		changed = !changed;
197
	}
198
});
199
 
200
function createOfferNew() {
201
    let validated = true;
202
    let createOfferRequest = {};
203
    createOfferRequest['name'] = $("#offer_name").val();
204
    createOfferRequest['startDate'] = startDate;
205
    createOfferRequest['endDate'] = endDate;
206
    createOfferRequest['offerNotes'] = $("#offer_notes").val();
29795 amit.gupta 207
    createOfferRequest['baseCriteria'] = $("input[name=base_criteria]").is(":checked");
27875 amit.gupta 208
    createOfferRequest['terms'] = $("#offer_terms").val();
209
    createOfferRequest['targetType'] = $("input[name=targetType]:checked").val();
27895 amit.gupta 210
    createOfferRequest['payoutType'] = $("input[name=payoutType]:checked").val();
27875 amit.gupta 211
    createOfferRequest['schemeType'] = $("input[name=schemeType]:checked").val();
212
    if(createOfferRequest['schemeType']=='ACTIVATION') {
213
    	createOfferRequest['activationBrands'] = $("div.activation-brands select").val().join(",") || null;
214
    } else {
215
    	createOfferRequest['activationBrands'] = null;
216
 
217
    }
218
    createOfferRequest['sellinPercentage'] = $("#sellinPercentage").val();
219
    createOfferRequest['brandShareTerms'] = $('#brandShare').val();
220
    createOfferRequest['partnerCriteria'] = {};
221
    createOfferRequest['partnerCriteria']['fofoIds'] = ($("#partners").val() || []).map(Number);
222
    createOfferRequest['partnerCriteria']['partnerTypes'] = ($("select.criteria-partner-type").val() || []);
223
    createOfferRequest['partnerCriteria']['regionIds'] = ($("select.criteria-warehouseregion").val() || []).map(Number);
224
 
225
    createOfferRequest['itemCriteria'] = itemCriteria($('div.itemCriteria'));
226
    let targetSlabs = [];
227
    $('div.targetslabs').each(function (index, ele) {
228
        let $targetSlab = $(ele);
229
        let targetOnwardsValue = $targetSlab.find('.payoutTarget').val();
230
        let targetDescription = $targetSlab.find('.targetDescription').val();
231
        let itemCriteriaPayouts = [];
232
        if (isNaN(targetOnwardsValue) || targetOnwardsValue < 0) {
233
            validated = false;
234
            alert(`Invalid Number - ${targetOnwardsValue}`);
235
        } else {
236
            if (index == 0 || targetOnwardsValue > 0) {
237
                let $itemCriteriaPayoutContainers = $targetSlab.find('.itemcriteriapayout');
238
                $itemCriteriaPayoutContainers.each(function (index, itemCriteriaPayoutContainer) {
239
                    let $itemCriteriaPayoutContainer = $(itemCriteriaPayoutContainer);
240
                    let amountType = $itemCriteriaPayoutContainer.find('input[name^=amountType]:checked').val();
241
                    let payoutSlabs = [];
242
                    let itemCriteria1 = itemCriteria($itemCriteriaPayoutContainer);
243
                    let $payoutSlabContainers = $itemCriteriaPayoutContainer.find('.slab');
28018 amit.gupta 244
                    let itemCriteriaChanged = itemCriteria1['smartPhone'] || itemCriteria1['featuredPhone']
27951 amit.gupta 245
                        || itemCriteria1['catalogIds'].length > 0 || itemCriteria1['brands'].length > 0
27952 amit.gupta 246
                        || itemCriteria1['excludeCatalogIds'].length > 0 
27955 amit.gupta 247
                        || itemCriteria1['startPrice'] > 0 || itemCriteria1['endPrice'] > itemCriteria1['startPrice'] > 0;
27875 amit.gupta 248
                    if (itemCriteriaChanged || index == 0) {
249
                        $payoutSlabContainers.each(function (slabContainerIndex, slabContainer) {
250
                            let $slab = $(slabContainer);
251
 
252
                            let slabOnwardsAmount = $slab.find('.slabOnwardsAmount').val();
253
                            if (isNaN(slabOnwardsAmount) || slabOnwardsAmount < 0) {
254
                                validated = false;
255
                                alert(`Invalid Payout Slab Amount - ${slabOnwardsAmount}`);
256
                            } else {
257
                                if (slabContainerIndex == 0 || slabOnwardsAmount > 0) {
258
                                    let payoutAmount = $slab.find('.payoutValue').val();
259
                                    if (isNaN(payoutAmount) || payoutAmount <= 0) {
260
                                        validated = false;
261
                                        alert(`Invalid Payout Slab Amount - ${slabOnwardsAmount}`);
262
                                    }
263
                                    let payoutSlab = {
264
                                        onwardsAmount: Number(slabOnwardsAmount),
265
                                        payoutAmount: Number(payoutAmount)
266
                                    }
267
                                    payoutSlabs.push(payoutSlab);
268
                                }
269
                            }
270
                        });
271
                        let itemCriteriaPayout = {
272
                            itemCriteria: itemCriteria1,
273
                            payoutSlabs: payoutSlabs,
274
                            amountType: amountType
275
                        };
276
                        itemCriteriaPayouts.push(itemCriteriaPayout);
277
                    }
278
                });
279
                targetSlabs.push({
280
                    onwardsAmount: targetOnwardsValue,
281
                    targetDescription : targetDescription, 
282
                    itemCriteriaPayouts: itemCriteriaPayouts
283
                });
284
            }
285
        }
286
    });
287
    createOfferRequest['targetSlabs'] = targetSlabs;
288
    if (validated && confirm("Confirm creating scheme offer")) {
289
        console.log("Validated");
290
        console.log(createOfferRequest);
291
        doPostAjaxRequestWithJsonHandler(`${context}/createOffer`, JSON.stringify(createOfferRequest), function (response) {
292
            alert("Offer created successfully");
293
            let dt = new Date();
294
            loadOfferHistory('#main-content', new Date().toISOString().slice(0,7));
295
        });
296
    } else {
297
        console.log("Invalid Offer");
298
    }
299
}
300
 
301
function itemCriteria($container) {
28018 amit.gupta 302
	let smartPhone = $container.find("input[name=smartphone]").is(":checked");
303
	let featuredPhone = $container.find("input[name=featuredphone]").is(":checked");
27875 amit.gupta 304
    let catalogIds = $container.find('.criteria-catalogids').val() || [];
305
    let excludeCatalogIds = [];
306
    let excludeChecked = $container.find('input.exclude').is(':checked');
307
    if(excludeChecked) {
308
    	excludeCatalogIds = catalogIds;
309
    	catalogIds = [];
310
    }
311
    let brands = $container.find('.criteria-brands').val() || [];
312
    return {
313
        startPrice: Number($container.find('.startValue').val()),
314
        endPrice: Number($container.find('.endValue').val()),
315
        catalogIds: catalogIds.map(Number),
316
        excludeCatalogIds : excludeCatalogIds.map(Number),
317
        brands: brands,
28018 amit.gupta 318
        smartPhone: smartPhone,
319
        featuredPhone: featuredPhone
27875 amit.gupta 320
    };
321
}
322
 
323
function loadOffer(domId) {
324
    console.log(domId);
325
    doGetAjaxRequestHandler(context + "/getCreateOffer", function (response) {
326
        $('#' + domId).html(response);
327
    });
328
}
329
function loadOfferHistory(domId, yearMonth) {
330
    doGetAjaxRequestHandler(`${context}/offerHistory?yearMonth=${yearMonth}`, function (response) {
331
        $(domId).html(response);
332
        $(domId).find("#yearmonth").val(yearMonth);
333
    });
334
}
335