Subversion Repositories SmartDukaan

Rev

Rev 29915 | Rev 30599 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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