Subversion Repositories SmartDukaan

Rev

Rev 22095 | Rev 22245 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21627 kshitij.so 1
var googleProfile;
2
 
22139 amit.gupta 3
 
4
$(document).ajaxComplete(
5
	function(){
6
 
7
	}
8
);
21627 kshitij.so 9
var startApp = function() {
10
  gapi.load('auth2', function(){
11
    // Retrieve the singleton for the GoogleAuth library and set up the client.
12
    auth2 = gapi.auth2.init({
22071 ashik.ali 13
      client_id: googleApiKey,
21627 kshitij.so 14
      cookiepolicy: 'single_host_origin',
15
      // Request scopes in addition to 'profile' and 'email'
16
      //scope: 'additional_scope'
17
    });
18
    attachSignin(document.getElementById('customBtn'));
19
  });
20
};
21
 
22
function attachSignin(element) {
23
    console.log(element.id);
24
    auth2.attachClickHandler(element, {},
25
        function(googleUser) {
26
    		googleProfile = googleUser.getBasicProfile();
27
        	console.log(googleProfile.getImageUrl());
28
        	submitUser(googleUser);
29
        }, function(error) {
30
          console.log(JSON.stringify(error, undefined, 2));
31
        });
32
  }
33
 
34
function submitUser(googleUser){
35
	jQuery.ajax({
36
        type : "POST",
22090 amit.gupta 37
        url : context+"/login",
22139 amit.gupta 38
        dataType: 'json',
21627 kshitij.so 39
        data: {"token":googleUser.getAuthResponse().id_token},
40
        success: function(data, textStatus, request){
41
        	console.log(data);
42
        	addProfileInLocalDb();
22139 amit.gupta 43
        	window.location.href= data.redirectUrl;
21627 kshitij.so 44
   		}
45
    });  
46
}
47
 
48
function addProfileInLocalDb(){
49
	var profile = {'image_url':googleProfile.getImageUrl(),'name':googleProfile.getName()};
50
	localStorage.setItem("profile",JSON.stringify(profile));
51
}
52
 
53
 
54
function loadNewGrn(domId){
55
	jQuery.ajax({
56
        type : "GET",
22091 amit.gupta 57
        url : context+"/purchase",
21627 kshitij.so 58
        success : function(response) {
59
            $('#' + domId).html(response);
60
        }
61
    });  
62
}
63
 
21987 kshitij.so 64
function loadGoodInventory(domId, search_text){
65
	jQuery.ajax({
66
        type : "GET",
22091 amit.gupta 67
        url : context+"/getCurrentInventorySnapshot/?searchTerm="+search_text,
21987 kshitij.so 68
        success : function(response) {
69
            $('#' + domId).html(response);
70
        }
71
    });  
72
}
73
 
74
function loadCatalog(domId, search_text){
75
	jQuery.ajax({
76
        type : "GET",
22091 amit.gupta 77
        url : context+"/getCatalog/?searchTerm="+search_text,
21987 kshitij.so 78
        success : function(response) {
79
            $('#' + domId).html(response);
80
        }
81
    });  
82
}
83
 
84
function loadBadInventory(domId, search_text){
85
	jQuery.ajax({
86
        type : "GET",
22090 amit.gupta 87
        url : context+"/getBadInventorySnapshot/?searchTerm="+search_text,
21987 kshitij.so 88
        success : function(response) {
89
            $('#' + domId).html(response);
90
        }
91
    });  
92
}
93
 
94
function loadGrnHistory(domId,purchase_reference,searchType, startTime, endTime){
95
	jQuery.ajax({
96
        type : "GET",
22090 amit.gupta 97
        url : context+"/grnHistory/?purchaseReference="+purchase_reference+"&searchType="+searchType
21987 kshitij.so 98
        +"&startTime="+startTime
99
        +"&endTime="+endTime,
100
        success : function(response) {
101
            $('#' + domId).html(response);
102
        }
103
    });  
104
}
105
 
106
function getNextItems(start, end, searchText){
107
	console.log(start);
108
	console.log(end);
109
	console.log(+end + +10);
110
	console.log(+start + +10);
111
	jQuery.ajax({
112
	        type : "GET",
22090 amit.gupta 113
	        url : context+"/getPaginatedCurrentInventorySnapshot/?offset="+end+"&searchTerm="+searchText,
21987 kshitij.so 114
			beforeSend: function(){
115
	        //$('#ajax-spinner').show();
116
	        },
117
	        complete: function(){
118
	        //$('#ajax-spinner').hide();
119
	        },
120
	        success : function(response) {
121
	        	$( "#good-inventory-paginated .end" ).text(+end + +10);
122
	        	$( "#good-inventory-paginated .start" ).text(+start + +10);
123
				var last = $( "#good-inventory-paginated .end" ).text();
124
				var temp = $( "#good-inventory-paginated .size" ).text();
125
				if (parseInt(last) >= parseInt(temp)){
126
					$("#good-inventory-paginated .next").prop('disabled', true);
127
					//$( "#good-inventory-paginated .end" ).text(temp);
128
				}
129
	            $('#good-inventory-table').html(response);
130
	            $("#good-inventory-paginated .previous").prop('disabled', false);
131
	        },
132
			error : function() {
133
				alert("Unable to fetch items");
134
			 },
135
	    });  
136
	}
137
function getPreviousItems(start,end,pre,searchText){
138
	jQuery.ajax({
139
	        type : "GET",
22090 amit.gupta 140
	        url : context+"/getPaginatedCurrentInventorySnapshot/?offset="+pre+"&searchTerm="+searchText,
21987 kshitij.so 141
			beforeSend: function(){
142
	        //$('#ajax-spinner').show();
143
	        },
144
	        complete: function(){
145
	        //$('#ajax-spinner').hide();
146
	        },
147
	        success : function(response) {
148
	        	$( "#good-inventory-paginated .end" ).text(+end - +10);
149
	        	$( "#good-inventory-paginated .start" ).text(+start - +10);
150
	        	$('#good-inventory-table').html(response);
151
	        	$("#good-inventory-paginated .next").prop('disabled', false);
152
				if (parseInt(pre)==0)
153
				{
154
					$("#good-inventory-paginated .previous").prop('disabled', true);
155
				}
156
 
157
	        },
158
			error : function() {
159
				alert("Unable to fetch items");
160
			 },
161
	    });
162
}
163
 
164
function getGrnHistoryNextItems(start, end, purchase_reference, searchType,startTime,endTime){
165
	console.log(start);
166
	console.log(end);
167
	console.log(+end + +10);
168
	console.log(+start + +10);
169
	jQuery.ajax({
170
	        type : "GET",
22090 amit.gupta 171
	        url : context+"/getPaginatedGrnHistory/?purchaseReference="+purchase_reference+"&searchType="+searchType
21987 kshitij.so 172
	        +"&startTime="+startTime
173
	        +"&endTime="+endTime+"&offset="+end,
174
			beforeSend: function(){
175
	        //$('#ajax-spinner').show();
176
	        },
177
	        complete: function(){
178
	        //$('#ajax-spinner').hide();
179
	        },
180
	        success : function(response) {
181
	        	$( "#grn-history-paginated .end" ).text(+end + +10);
182
	        	$( "#grn-history-paginated .start" ).text(+start + +10);
183
				var last = $( "#grn-history-paginated .end" ).text();
184
				var temp = $( "#grn-history-paginated .size" ).text();
185
				if (parseInt(last) >= parseInt(temp)){
186
					$("#grn-history-paginated .next").prop('disabled', true);
187
					//$( "#good-inventory-paginated .end" ).text(temp);
188
				}
189
	            $('#grn-history-table').html(response);
190
	            $("#grn-history-paginated .previous").prop('disabled', false);
191
	        },
192
			error : function() {
193
				alert("Unable to fetch items");
194
			 },
195
	    });  
196
	}
197
function getGrnHistoryPreviousItems(start,end,pre,purchase_reference, searchType,startTime,endTime){
198
	jQuery.ajax({
199
	        type : "GET",
22090 amit.gupta 200
	        url : context+"/getPaginatedGrnHistory/?purchaseReference="+purchase_reference+"&searchType="+searchType
21987 kshitij.so 201
	        +"&startTime="+startTime
202
	        +"&endTime="+endTime+"&offset="+pre,
203
			beforeSend: function(){
204
	        //$('#ajax-spinner').show();
205
	        },
206
	        complete: function(){
207
	        //$('#ajax-spinner').hide();
208
	        },
209
	        success : function(response) {
210
	        	$( "#grn-history-paginated .end" ).text(+end - +10);
211
	        	$( "#grn-history-paginated .start" ).text(+start - +10);
212
	        	$('#grn-history-table').html(response);
213
	        	$("#grn-history-paginated .next").prop('disabled', false);
214
				if (parseInt(pre)==0)
215
				{
216
					$("#grn-history-paginated .previous").prop('disabled', true);
217
				}
218
 
219
	        },
220
			error : function() {
221
				alert("Unable to fetch items");
222
			 },
223
	    });
224
}
225
 
226
function loadGoodInventorySearchInfo(search_text){
227
	loadGoodInventory("main-content",search_text);
228
}
229
 
230
function loadGrnHistorySearchInfo(search_text,searchType,startTime,endTime){
231
	loadGrnHistory("main-content",search_text,searchType,startTime,endTime);
232
}
233
 
21627 kshitij.so 234
function findDuplicateSerialNumbers(value){
235
    var result = $("#grnImeiInformation :input[value='" + value + "']").length - 1;
236
    return result;
237
}
21987 kshitij.so 238
 
239
function loadGrnDetails(purchaseId,domId){
240
	jQuery.ajax({
241
        type : "GET",
22090 amit.gupta 242
        url : context+"/grnHistoryDetailByPurchaseId/?purchaseId="+purchaseId,
21987 kshitij.so 243
        success : function(response) {
244
            $('#' + domId).html(response);
245
            window.dispatchEvent(new Event('resize'));
246
        }
247
    });
248
}
249
 
250
function loadPendingGrnDetails(purchaseId,domId){
251
	jQuery.ajax({
22090 amit.gupta 252
		url: context+"/purchase/?airwayBillOrInvoiceNumber="+purchaseId,
21987 kshitij.so 253
		type: 'POST',
254
		async: false,
255
		success: function (data) {
256
			$('#'+domId).html(data);
257
		},
258
		error : function() {
259
			alert("OOPS!!!Failed to do changes.Try Again.",'ERROR');
260
		},
261
		cache: false,
262
		contentType: false,
263
		processData: false
264
	});
265
}
266
 
267
function getNextCatalogItems(start, end, searchText){
268
	console.log(start);
269
	console.log(end);
270
	console.log(+end + +10);
271
	console.log(+start + +10);
272
	jQuery.ajax({
273
	        type : "GET",
22090 amit.gupta 274
	        url : context+"/getPaginatedCatalog/?offset="+end+"&searchTerm="+searchText,
21987 kshitij.so 275
			beforeSend: function(){
276
	        //$('#ajax-spinner').show();
277
	        },
278
	        complete: function(){
279
	        //$('#ajax-spinner').hide();
280
	        },
281
	        success : function(response) {
282
	        	$( "#catalog-paginated .end" ).text(+end + +10);
283
	        	$( "#catalog-paginated .start" ).text(+start + +10);
284
				var last = $( "#catalog-paginated .end" ).text();
285
				var temp = $( "#catalog-paginated .size" ).text();
286
				if (parseInt(last) >= parseInt(temp)){
287
					$("#catalog-paginated .next").prop('disabled', true);
288
				}
289
	            $('#catalog-table').html(response);
290
	            $("#catalog-paginated .previous").prop('disabled', false);
291
	        },
292
			error : function() {
293
				alert("Unable to fetch items");
294
			 },
295
	    });  
296
	}
297
function getPreviousCatalogItems(start,end,pre,searchText){
298
	jQuery.ajax({
299
	        type : "GET",
22090 amit.gupta 300
	        url : context+"/getPaginatedCatalog/?offset="+pre+"&searchTerm="+searchText,
21987 kshitij.so 301
			beforeSend: function(){
302
	        //$('#ajax-spinner').show();
303
	        },
304
	        complete: function(){
305
	        //$('#ajax-spinner').hide();
306
	        },
307
	        success : function(response) {
308
	        	$( "#catalog-paginated .end" ).text(+end - +10);
309
	        	$( "#catalog-paginated .start" ).text(+start - +10);
310
	        	$('#catalog-table').html(response);
311
	        	$("#catalog-paginated .next").prop('disabled', false);
312
				if (parseInt(pre)==0)
313
				{
314
					$("#catalog-paginated .previous").prop('disabled', true);
315
				}
316
 
317
	        },
318
			error : function() {
319
				alert("Unable to fetch items");
320
			 },
321
	    });
322
}
323
 
324
function loadCatalogSearchInfo(search_text){
325
	loadCatalog("main-content",search_text);
326
}
327
 
328
function addItemInLocalStorage(bagObj){
329
	if (localStorage.getItem("bag")!=null){
330
		var bag = JSON.parse(localStorage.getItem("bag"));
331
		bag[bagObj.itemId] = bagObj
332
		localStorage.setItem("bag",JSON.stringify(bag));
333
		bag = localStorage.getItem("bag")
334
		$("#cart_bar").find('span').text(Object.keys(JSON.parse(bag)).length);
335
	}
336
	else{
337
		var tempObj = {};
338
		tempObj[bagObj.itemId] = bagObj
339
		localStorage.setItem("bag",JSON.stringify(tempObj));
340
		var bag = localStorage.getItem("bag")
341
		$("#cart_bar").find('span').text(Object.keys(JSON.parse(bag)).length);
342
	}
343
}
344
 
345
 
346
function changeQuantityInLocalStorage(itemId, newQuantity){
347
	var bag = JSON.parse(localStorage.getItem("bag"));
348
	bag[itemId].quantity = newQuantity;
349
	localStorage.setItem("bag",JSON.stringify(bag));
350
}
351
 
352
function removeItemFromLocalStorage(itemId){
353
	if (localStorage.getItem("bag")!=null){
354
		var bag = JSON.parse(localStorage.getItem("bag"));
355
		delete bag[itemId];
356
		localStorage.setItem("bag",JSON.stringify(bag));
357
		$("#cart_bar").find('span').text(Object.keys(bag).length);
358
	}
359
}
360
 
361
function emptyBag(){
22095 kshitij.so 362
	localStorage.setItem("bag",JSON.stringify({}));
21987 kshitij.so 363
	$("#cart_bar").find('span').text(0);
364
}
365
 
366
function loadCart(domId){
367
	jQuery.ajax({
368
        type : "POST",
369
        data: {"cartData":localStorage.getItem("bag")},
22090 amit.gupta 370
        url : context+"/cart",
21987 kshitij.so 371
        success : function(response) {
372
            $('#' + domId).html(response);
373
        }
374
    });  
375
}
376
 
377
function checkout(domId){
378
	jQuery.ajax({
22095 kshitij.so 379
        type : "GET",
21987 kshitij.so 380
        data: {"cartData":localStorage.getItem("bag")},
22090 amit.gupta 381
        url : context+"/validate-cart",
21987 kshitij.so 382
        success : function(response) {
383
        	var obj = JSON.parse(response);
384
        	redirectCart(obj.redirectUrl, obj.params, obj.method, "main-content");
22095 kshitij.so 385
        	$('#main-content').html(response);
21987 kshitij.so 386
        }
387
    });  
388
}
389
 
390
function redirectCart(url, cartData, method, domId){
391
	jQuery.ajax({
392
        type : method,
393
        data: {"cartData":cartData},
394
        url : context+url,
395
        success : function(response) {
396
            $('#' + domId).html(response);
397
            window.dispatchEvent(new Event('resize'));
398
        }
399
    });  
400
}
401
 
402
function validateOrderDetails(){
403
	var sNumbers = [];
404
	var error = false;
405
	$("form#cd input.serialNumber").each(function(){
406
		var input = $(this).val().trim();
407
		$(this).removeClass("border-highlight");
408
		if (sNumbers.indexOf(input) !=-1 || !input){
409
			error = true;
410
			$(this).addClass("border-highlight");
411
		}
412
		sNumbers.push(input);
413
	});
414
 
415
	$("form#cd input.unitPrice").each(function(){
416
		var input = $(this).val().trim();
417
		$(this).removeClass("border-highlight");
418
		if (!input || parseInt(input)<=0 || isNaN(input)){
419
			error=true;
420
			$(this).addClass("border-highlight");
421
		}
422
	});
423
 
424
	var amount = 0;
425
	var netPayableAmount = parseFloat($("form#cd input.netPayableAmount").val());
426
 
427
 
428
	$("form#cd input.amount").each(function(){
429
		if ($(this).val() ==""){
430
			$(this).val(0);
431
		}
432
		var tmpAmount = parseFloat($(this).val());
433
		amount = amount + tmpAmount;
434
	});
435
 
436
	console.log(amount);
437
	console.log(netPayableAmount);
438
 
439
	if (amount!=netPayableAmount){
440
		alert("Mismatch in amount");
441
		error = true;
442
	}
443
 
444
	if (error){
445
		return false;
446
	}
447
	return true;
448
 
449
}
450
 
451
function orderDetailsPayload(){
452
		var orderObj = {};
453
		var priceQtyArray = [];
454
		var customerObj = {};
455
		var paymentOption = [];
456
 
457
		$("form#cd input.serialNumber").each(function(){
458
			var itemId = parseInt($(this).attr("itemId"));
459
			if (orderObj.hasOwnProperty('fofoLineItems')){
460
				var itemDetails = orderObj['fofoLineItems'];
461
				if (itemDetails.hasOwnProperty(itemId)){
462
					var serialNumbers = itemDetails[itemId];
463
					serialNumbers.push($(this).val());
464
					itemDetails[itemId] = serialNumbers;
465
				}
466
				else{
467
					var serialNumbers = [];
468
					serialNumbers.push($(this).val());
469
					itemDetails[itemId] = serialNumbers;
470
				}
471
			}
472
			else{
473
				var serialNumbers = [];
474
				serialNumbers.push($(this).val());
475
				var tmp ={};
476
				tmp[itemId]  = serialNumbers;
477
				orderObj['fofoLineItems'] = tmp;
478
			}
479
		});
480
		console.log( JSON.stringify(orderObj));
481
		$("form#cd input.unitPrice").each(function(){
482
			var itemId = parseInt($(this).attr("itemId"));
483
			var unitPrice = parseFloat($(this).val());
484
			var qty = parseInt($(this).attr("quantity"));
485
			var tmpObj = {'itemId':itemId,'sellingPrice':unitPrice,'quantity':qty};
486
			if (orderObj['fofoLineItems'][itemId] ===undefined){
487
				tmpObj['serialNumbers'] =[]
488
			}
489
			else{
490
				tmpObj['serialNumbers'] = orderObj['fofoLineItems'][itemId];
491
			}
492
			priceQtyArray.push(tmpObj);
493
		});
494
		console.log( JSON.stringify(priceQtyArray));
495
 
496
		//customer-details
497
		/*$("#customer-details input").each(function(){
498
			console.log($(this).attr('name'));
499
			customerObj[$(this).attr('name')] = $(this).val();
500
		});*/
501
		//$("input[name=nameGoesHere]").val();
502
		customerObj['name'] = $("form#cd input[name=name]").val(); 
503
		customerObj['mobileNumber'] = $("form#cd input[name=phone]").val(); 
504
		customerObj['emailId'] = $("form#cd input[name=email]").val();
505
		var customerAddress = {};
506
		customerAddress['name'] = $("form#cd input[name=name]").val();
507
		customerAddress['line1'] = $("form#cd input[name=line1]").val();
508
		customerAddress['line2'] = $("form#cd input[name=line2]").val();
509
		customerAddress['landmark'] = $("form#cd input[name=landmark]").val();
510
		customerAddress['city'] = $("form#cd input[name=city]").val();
511
		customerAddress['state'] = $('select[name=state] option:selected').val();
512
		customerAddress['pinCode'] = $("form#cd input[name=pincode]").val();
513
		customerAddress['phoneNumber'] = $("form#cd input[name=alternatePhone]").val();
514
		customerAddress['country'] = "India";
515
		customerObj['address'] = customerAddress; 
516
		console.log( JSON.stringify(customerObj));
517
		$("#payment-details input").each(function(){
518
		console.log($(this).attr('name'));
519
		if ($(this).attr('name') =="" ){
520
			return;
521
		}
522
		var paymentObj = {};
523
		paymentObj['type'] = $(this).attr('name');
524
		if ($(this).val() == ""){
525
			paymentObj['amount'] = 0.0;
526
		}
527
		else{
528
			paymentObj['amount'] = parseFloat($(this).val());
529
		}
530
		paymentOption.push(paymentObj);
531
		});
532
		console.log( JSON.stringify(paymentOption));
533
 
534
		var retObj = {};
535
		retObj['fofoLineItems'] = (priceQtyArray);
536
		retObj['customer'] = (customerObj);
537
		retObj['paymentOptions'] =  (paymentOption);
538
		console.log(retObj);
539
		return  JSON.stringify(retObj);
540
}