Subversion Repositories SmartDukaan

Rev

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

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