Subversion Repositories SmartDukaan

Rev

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