Subversion Repositories SmartDukaan

Rev

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

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