Subversion Repositories SmartDukaan

Rev

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

Rev 35458 Rev 35459
Line 1... Line 1...
1
$(function() {
1
$(function() {
2
	const giftCallback = function(value) {
2
	callback = function(value) {
3
		removeItemFromLocalStorage(giftItemId);
3
		removeItemFromLocalStorage(giftItemId);
4
		if (isNaN(value)) {
4
		if (isNaN(value)) {
5
			bootbox.alert("No of watches should be a number. Set '0' specifically");
5
			bootbox("No of watches should be a number. Set '0' specifically");
6
			return;
6
			return;
7
		} else {
7
		} else {
8
			loadCart("main-content", value);
8
			loadCart("main-content", value);
9
		}
9
		}
10
	};
10
	}
11
 
-
 
12
	$(document).on('click', ".cart_icon_header", function() {
11
	$(document).on('click', ".cart_icon_header", function() {
13
		if (typeof hasGift === "undefined") {
12
		if (typeof hasGift == "undefined") {
14
			hasGift = false;
13
			hasGift = false;
15
		}
14
		}
16
		if (hasGift) {
15
		if (hasGift) {
17
			const bootBoxObj = {
16
			bootBoxObj = {
18
				size: "medium",
17
				size: "medium",
19
				title: "Add Number of Watch as Gift",
18
				title: "Add Number of Watch as Gift",
20
				callback: giftCallback,
19
				callback: callback,
21
				inputType: 'number'
20
				inputType: 'number'
22
			};
21
			};
-
 
22
 
23
			bootbox.prompt(bootBoxObj);
23
			bootbox.prompt(bootBoxObj);
24
		} else {
24
		} else {
25
			loadCart('main-content', 0);
25
			loadCart('main-content', 0);
26
		}
26
		}
27
	});
27
	});
Line 30... Line 30...
30
	$(document).on('click', ".cart-checkout", function() {
30
	$(document).on('click', ".cart-checkout", function() {
31
		checkout("main-content");
31
		checkout("main-content");
32
	});
32
	});
33
 
33
 
34
	$(document).on('click', ".add-bag-submit", function() {
34
	$(document).on('click', ".add-bag-submit", function() {
-
 
35
		added = false;
35
		$('#catalogModalBody').hide();
36
		$('#catalogModalBody').hide();
36
		var itemId = $(this).closest('tr').attr('data');
37
		var itemId = $(this).closest('tr').attr('data');
37
 
38
 
38
		var poItemId = $(this).closest('tr').find("#poItemId").val();
39
		var poItemId = $(this).closest('tr').find("#poItemId").val();
39
 
40
 
Line 80... Line 81...
80
 
81
 
81
			$('#catalogModalBody').show();
82
			$('#catalogModalBody').show();
82
		});
83
		});
83
	});
84
	});
84
 
85
 
85
	// Shared function for add to bag operations
86
	// addToBagButton
-
 
87
 
86
	function handleAddToBag(afterAddCallback) {
88
	$(document).on('click', "#addToBagButton", function() {
87
		var itemId = $("#bagModel .itemId").val();
89
		var itemId = $("#bagModel .itemId").val();
88
		var poitemId = $("#bagModel .poitemId").val();
90
		var poitemId = $("#bagModel .poitemId").val();
89
		var poId = $("#bagModel .poId").val();
91
		var poId = $("#bagModel .poId").val();
90
		var quantity = $("#bagModel .quantity").val();
-
 
91
		var imeiCount = $('h4.bagModalEarlyImeis').find('input').length;
-
 
92
 
92
 
-
 
93
		var tempAvailability = $("#bagModel .availability").val();
-
 
94
		var quantity = $("#bagModel .quantity").val();
-
 
95
		imeiCount = $('h4.bagModalEarlyImeis').find('input').length;
93
		if (imeiCount > 0) {
96
		if (imeiCount > 0) {
94
			quantity = $('h4.bagModalEarlyImeis').find('input:checked').length;
97
			quantity = $('h4.bagModalEarlyImeis').find('input:checked').length;
95
			if (quantity === 0) {
98
			if (quantity == 0) {
96
				alert("Please check one of the imei(s)");
99
				alert("Please check one of the imei(s)");
97
				return;
100
				return;
98
			}
101
			}
99
		} else if (!quantity || parseInt(quantity) <= 0 || isNaN(parseInt(quantity)) || quantity.indexOf('.') !== -1) {
102
		} else if (!quantity || parseInt(quantity) <= 0 || isNaN(parseInt(quantity)) || quantity.indexOf('.') != -1) {
100
			alert("Please enter valid quantity");
103
			alert("Please enter valid quantity");
101
			return;
104
			return;
102
		}
105
		}
103
 
106
 
104
		doGetAjaxRequestHandler(context + "/checkItemAvailability/?itemId=" + itemId, function(response) {
107
		doGetAjaxRequestHandler(context + "/checkItemAvailability/?itemId=" + itemId, function(response) {
105
			var availabilityObj = JSON.parse(response);
108
			var obj = JSON.parse(response);
-
 
109
			console.log(obj);
106
			if (availabilityObj.availability < parseInt(quantity) || availabilityObj.availability === 0) {
110
			if (obj.availability < parseInt(quantity) || obj.availability == 0) {
107
				alert("You don't have enough inventory of this item");
111
				alert("You don't have enough inventory of this item");
108
				return;
112
				return;
109
			}
113
			}
110
			var bagItem = {
-
 
111
				'itemId': availabilityObj.itemId,
-
 
112
				'displayName': availabilityObj.displayName,
114
			var obj = { 'itemId': obj.itemId, 'displayName': obj.displayName, 'quantity': parseInt(quantity), 'iconUrl': obj.iconUrl, 'poItemId': poitemId, 'poId': poId };
113
				'quantity': parseInt(quantity),
-
 
114
				'iconUrl': availabilityObj.iconUrl,
-
 
115
				'poItemId': poitemId,
-
 
116
				'poId': poId
-
 
117
			};
-
 
118
			if (imeiCount > 0) {
115
			if (imeiCount > 0) {
119
				bagItem['imeis'] = [];
116
				obj['imeis'] = [];
120
				$('h4.bagModalEarlyImeis').find('input:checked').each(function(index, ele) {
117
				$('h4.bagModalEarlyImeis').find('input:checked').each(function(index, ele) {
121
					bagItem['imeis'].push(ele.value);
118
					obj['imeis'].push(ele.value);
122
				});
119
				});
123
			}
120
			}
-
 
121
			console.log(obj);
124
			addItemInLocalStorage(bagItem);
122
			addItemInLocalStorage(obj);
-
 
123
			//alert("Item added to bag successfully");
125
			showToast("Item added to bag successfully!");
124
			showToast("Item, Item added to bag successfully!");
126
			$('#bagModel').modal('hide');
125
			$('#bagModel').modal('hide');
127
			if (afterAddCallback) {
-
 
128
				afterAddCallback();
-
 
129
			}
126
 
130
		});
127
		});
131
	}
-
 
132
 
128
 
133
	$(document).on('click', "#addToBagButton", function() {
-
 
134
		handleAddToBag();
-
 
135
	});
129
	});
136
 
130
 
-
 
131
	// add to bag with cart page
-
 
132
 
137
	$(document).on('click', "#addToBagButtonwithCart", function() {
133
	$(document).on('click', "#addToBagButtonwithCart", function () {
-
 
134
		var itemId = $("#bagModel .itemId").val();
-
 
135
		var poitemId = $("#bagModel .poitemId").val();
-
 
136
		var poId = $("#bagModel .poId").val();
-
 
137
		console.log(poitemId)
-
 
138
		console.log(poId)
-
 
139
 
-
 
140
		var tempAvailability = $("#bagModel .availability").val();
-
 
141
		var quantity = $("#bagModel .quantity").val();
-
 
142
		imeiCount = $('h4.bagModalEarlyImeis').find('input').length;
-
 
143
		if (imeiCount > 0) {
-
 
144
			quantity = $('h4.bagModalEarlyImeis').find('input:checked').length;
-
 
145
			if (quantity == 0) {
-
 
146
				alert("Please check one of the imei(s)");
-
 
147
				return;
-
 
148
			}
-
 
149
		} else if (!quantity || parseInt(quantity) <= 0 || isNaN(parseInt(quantity)) || quantity.indexOf('.') != -1) {
-
 
150
			alert("Please enter valid quantity");
-
 
151
			return;
-
 
152
		}
-
 
153
 
-
 
154
		doGetAjaxRequestHandler(context + "/checkItemAvailability/?itemId=" + itemId, function (response) {
-
 
155
			var obj = JSON.parse(response);
-
 
156
			console.log(obj);
-
 
157
			if (obj.availability < parseInt(quantity) || obj.availability == 0) {
-
 
158
				alert("You don't have enough inventory of this item");
-
 
159
				return;
-
 
160
			}
-
 
161
			var obj = {
-
 
162
				'itemId': obj.itemId,
-
 
163
				'displayName': obj.displayName,
-
 
164
				'quantity': parseInt(quantity),
-
 
165
				'iconUrl': obj.iconUrl,
-
 
166
				'poItemId': poitemId,
-
 
167
				'poId': poId
-
 
168
			};
-
 
169
			if (imeiCount > 0) {
-
 
170
				obj['imeis'] = [];
-
 
171
				$('h4.bagModalEarlyImeis').find('input:checked').each(function (index, ele) {
-
 
172
					obj['imeis'].push(ele.value);
-
 
173
				});
-
 
174
			}
-
 
175
			console.log(obj);
138
		handleAddToBag(function() {
176
			addItemInLocalStorage(obj);
-
 
177
			//alert("Item added to bag successfully");
-
 
178
			showToast("Item, Item added to bag successfully!");
-
 
179
			$('#bagModel').modal('hide');
-
 
180
			// Trigger the click event for the cart icon
139
			$(".cart_icon_header").trigger('click');
181
			$(".cart_icon_header").trigger('click');
-
 
182
 
140
		});
183
		});
-
 
184
 
141
	});
185
	});
142
 
186
 
-
 
187
	// direct go billing page skip checkout page
143
	$(document).on('click', "#addToBagButtonwithBillingpage", function() {
188
	$(document).on('click', "#addToBagButtonwithBillingpage", function () {
-
 
189
		var itemId = $("#bagModel .itemId").val();
-
 
190
		var poitemId = $("#bagModel .poitemId").val();
-
 
191
		var poId = $("#bagModel .poId").val();
-
 
192
		console.log(poitemId)
-
 
193
		console.log(poId)
-
 
194
 
-
 
195
		var tempAvailability = $("#bagModel .availability").val();
-
 
196
		var quantity = $("#bagModel .quantity").val();
-
 
197
		imeiCount = $('h4.bagModalEarlyImeis').find('input').length;
-
 
198
		if (imeiCount > 0) {
-
 
199
			quantity = $('h4.bagModalEarlyImeis').find('input:checked').length;
-
 
200
			if (quantity == 0) {
-
 
201
				alert("Please check one of the imei(s)");
-
 
202
				return;
-
 
203
			}
-
 
204
		} else if (!quantity || parseInt(quantity) <= 0 || isNaN(parseInt(quantity)) || quantity.indexOf('.') != -1) {
-
 
205
			alert("Please enter valid quantity");
-
 
206
			return;
-
 
207
		}
-
 
208
 
-
 
209
		doGetAjaxRequestHandler(context + "/checkItemAvailability/?itemId=" + itemId, function (response) {
-
 
210
			var obj = JSON.parse(response);
-
 
211
			console.log(obj);
-
 
212
			if (obj.availability < parseInt(quantity) || obj.availability == 0) {
-
 
213
				alert("You don't have enough inventory of this item");
-
 
214
				return;
-
 
215
			}
-
 
216
			var obj = {
-
 
217
				'itemId': obj.itemId,
-
 
218
				'displayName': obj.displayName,
-
 
219
				'quantity': parseInt(quantity),
-
 
220
				'iconUrl': obj.iconUrl,
-
 
221
				'poItemId': poitemId,
-
 
222
				'poId': poId
-
 
223
			};
-
 
224
			if (imeiCount > 0) {
-
 
225
				obj['imeis'] = [];
-
 
226
				$('h4.bagModalEarlyImeis').find('input:checked').each(function (index, ele) {
-
 
227
					obj['imeis'].push(ele.value);
-
 
228
				});
-
 
229
			}
-
 
230
			console.log(obj);
144
		handleAddToBag(function() {
231
			addItemInLocalStorage(obj);
-
 
232
			//alert("Item added to bag successfully");
-
 
233
			showToast("Item, Item added to bag successfully!");
-
 
234
			$('#bagModel').modal('hide');
-
 
235
			// Trigger the click event for the cart icon
145
			$(".cart-checkout").trigger('click');
236
			$(".cart-checkout").trigger('click');
-
 
237
 
146
		});
238
		});
-
 
239
 
147
	});
240
	});
148
 
241
 
149
	$(document).on('click', ".cart-trash", function() {
242
	$(document).on('click', ".cart-trash", function() {
150
		var itemId = $(this).attr('data');
243
		var itemId = $(this).attr('data');
151
		removeItemFromLocalStorage(itemId);
244
		removeItemFromLocalStorage(itemId);
Line 201... Line 294...
201
		changeQuantityInLocalStorage(itemId, qty);
294
		changeQuantityInLocalStorage(itemId, qty);
202
	});
295
	});
203
});
296
});
204
 
297
 
205
 
298
 
-
 
299
function emptyBag() {
-
 
300
	delete localStorage.getItem("bag");
-
 
301
}
-
 
302
 
206
function addItemInLocalStorage(bagObj) {
303
function addItemInLocalStorage(bagObj) {
-
 
304
	if (localStorage.getItem("bag") != null) {
-
 
305
		var bag = JSON.parse(localStorage.getItem("bag"));
-
 
306
		bag[bagObj.itemId] = bagObj
-
 
307
		localStorage.setItem("bag", JSON.stringify(bag));
207
	var bag = localStorage.getItem("bag");
308
		bag = localStorage.getItem("bag")
208
	var bagData = bag ? JSON.parse(bag) : {};
309
		$("#cart_bar").find('span').text(Object.keys(JSON.parse(bag)).length);
-
 
310
	}
-
 
311
	else {
-
 
312
		var tempObj = {};
209
	bagData[bagObj.itemId] = bagObj;
313
		tempObj[bagObj.itemId] = bagObj
210
	localStorage.setItem("bag", JSON.stringify(bagData));
314
		localStorage.setItem("bag", JSON.stringify(tempObj));
-
 
315
		var bag = localStorage.getItem("bag")
211
	$("#cart_bar").find('span').text(Object.keys(bagData).length);
316
		$("#cart_bar").find('span').text(Object.keys(JSON.parse(bag)).length);
-
 
317
	}
212
}
318
}
213
 
319
 
214
 
320
 
215
function changeQuantityInLocalStorage(itemId, newQuantity) {
321
function changeQuantityInLocalStorage(itemId, newQuantity) {
216
	var bag = JSON.parse(localStorage.getItem("bag") || "{}");
322
	var bag = JSON.parse(localStorage.getItem("bag"));
217
	if (typeof bag[itemId] === "undefined") {
323
	if (typeof bag[itemId] == "undefined") {
218
		bag[itemId] = {
324
		bag[itemId] = {};
219
			itemId: itemId,
325
		bag[itemId].itemId = itemId;
220
			displayName: "SmartDukaan Gift Watch",
326
		bag[itemId].displayName = "SmartDukaan Gift Watch";
221
			iconUrl: ""
327
		bag[itemId].iconUrl = "";
222
		};
-
 
223
		delete bag["giftQty"];
328
		delete bag["giftQty"];
-
 
329
 
224
	}
330
	}
225
	bag[itemId].quantity = newQuantity;
331
	bag[itemId].quantity = newQuantity;
226
	localStorage.setItem("bag", JSON.stringify(bag));
332
	localStorage.setItem("bag", JSON.stringify(bag));
227
}
333
}
228
 
334
 
229
function removeItemFromLocalStorage(itemId) {
335
function removeItemFromLocalStorage(itemId) {
230
	var bagStr = localStorage.getItem("bag");
336
	if (localStorage.getItem("bag") != null) {
231
	if (bagStr) {
-
 
232
		var bag = JSON.parse(bagStr);
337
		var bag = JSON.parse(localStorage.getItem("bag"));
233
		delete bag[itemId];
338
		delete bag[itemId];
234
		localStorage.setItem("bag", JSON.stringify(bag));
339
		localStorage.setItem("bag", JSON.stringify(bag));
235
		$("#cart_bar").find('span').text(Object.keys(bag).length);
340
		$("#cart_bar").find('span').text(Object.keys(bag).length);
236
	}
341
	}
237
}
342
}
238
 
343
 
239
function addGiftQty(giftQty) {
344
function addGiftQty(giftQty) {
240
	var bagStr = localStorage.getItem("bag");
345
	if (localStorage.getItem("bag") != null) {
241
	if (bagStr) {
-
 
242
		var bag = JSON.parse(bagStr);
346
		var bag = JSON.parse(localStorage.getItem("bag"));
243
		bag["giftQty"] = giftQty;
347
		bag["giftQty"] = giftQty;
244
		localStorage.setItem("bag", JSON.stringify(bag));
348
		localStorage.setItem("bag", JSON.stringify(bag));
245
	}
349
	}
246
}
350
}
247
 
-
 
248
function removeGiftQty() {
351
function removeGiftQty() {
249
	var bagStr = localStorage.getItem("bag");
352
	if (localStorage.getItem("bag") != null) {
250
	if (bagStr) {
-
 
251
		var bag = JSON.parse(bagStr);
353
		var bag = JSON.parse(localStorage.getItem("bag"));
252
		delete bag["giftQty"];
354
		delete bag["giftQty"];
253
		localStorage.setItem("bag", JSON.stringify(bag));
355
		localStorage.setItem("bag", JSON.stringify(bag));
254
	}
356
	}
255
}
357
}
256
 
358