Subversion Repositories SmartDukaan

Rev

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