Subversion Repositories SmartDukaan

Rev

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