Subversion Repositories SmartDukaan

Rev

Rev 22139 | Rev 22254 | 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
 
22245 ashik.ali 106
 
107
 
21987 kshitij.so 108
function getNextItems(start, end, searchText){
109
	console.log(start);
110
	console.log(end);
111
	console.log(+end + +10);
112
	console.log(+start + +10);
113
	jQuery.ajax({
114
	        type : "GET",
22090 amit.gupta 115
	        url : context+"/getPaginatedCurrentInventorySnapshot/?offset="+end+"&searchTerm="+searchText,
21987 kshitij.so 116
			beforeSend: function(){
117
	        //$('#ajax-spinner').show();
118
	        },
119
	        complete: function(){
120
	        //$('#ajax-spinner').hide();
121
	        },
122
	        success : function(response) {
123
	        	$( "#good-inventory-paginated .end" ).text(+end + +10);
124
	        	$( "#good-inventory-paginated .start" ).text(+start + +10);
125
				var last = $( "#good-inventory-paginated .end" ).text();
126
				var temp = $( "#good-inventory-paginated .size" ).text();
127
				if (parseInt(last) >= parseInt(temp)){
128
					$("#good-inventory-paginated .next").prop('disabled', true);
129
					//$( "#good-inventory-paginated .end" ).text(temp);
130
				}
131
	            $('#good-inventory-table').html(response);
132
	            $("#good-inventory-paginated .previous").prop('disabled', false);
133
	        },
134
			error : function() {
135
				alert("Unable to fetch items");
136
			 },
137
	    });  
138
	}
139
function getPreviousItems(start,end,pre,searchText){
140
	jQuery.ajax({
141
	        type : "GET",
22090 amit.gupta 142
	        url : context+"/getPaginatedCurrentInventorySnapshot/?offset="+pre+"&searchTerm="+searchText,
21987 kshitij.so 143
			beforeSend: function(){
144
	        //$('#ajax-spinner').show();
145
	        },
146
	        complete: function(){
147
	        //$('#ajax-spinner').hide();
148
	        },
149
	        success : function(response) {
150
	        	$( "#good-inventory-paginated .end" ).text(+end - +10);
151
	        	$( "#good-inventory-paginated .start" ).text(+start - +10);
152
	        	$('#good-inventory-table').html(response);
153
	        	$("#good-inventory-paginated .next").prop('disabled', false);
154
				if (parseInt(pre)==0)
155
				{
156
					$("#good-inventory-paginated .previous").prop('disabled', true);
157
				}
158
 
159
	        },
160
			error : function() {
161
				alert("Unable to fetch items");
162
			 },
163
	    });
164
}
165
 
166
function getGrnHistoryNextItems(start, end, purchase_reference, searchType,startTime,endTime){
167
	console.log(start);
168
	console.log(end);
169
	console.log(+end + +10);
170
	console.log(+start + +10);
171
	jQuery.ajax({
172
	        type : "GET",
22090 amit.gupta 173
	        url : context+"/getPaginatedGrnHistory/?purchaseReference="+purchase_reference+"&searchType="+searchType
21987 kshitij.so 174
	        +"&startTime="+startTime
175
	        +"&endTime="+endTime+"&offset="+end,
176
			beforeSend: function(){
177
	        //$('#ajax-spinner').show();
178
	        },
179
	        complete: function(){
180
	        //$('#ajax-spinner').hide();
181
	        },
182
	        success : function(response) {
183
	        	$( "#grn-history-paginated .end" ).text(+end + +10);
184
	        	$( "#grn-history-paginated .start" ).text(+start + +10);
185
				var last = $( "#grn-history-paginated .end" ).text();
186
				var temp = $( "#grn-history-paginated .size" ).text();
187
				if (parseInt(last) >= parseInt(temp)){
188
					$("#grn-history-paginated .next").prop('disabled', true);
189
					//$( "#good-inventory-paginated .end" ).text(temp);
190
				}
191
	            $('#grn-history-table').html(response);
192
	            $("#grn-history-paginated .previous").prop('disabled', false);
193
	        },
194
			error : function() {
195
				alert("Unable to fetch items");
196
			 },
197
	    });  
198
	}
199
function getGrnHistoryPreviousItems(start,end,pre,purchase_reference, searchType,startTime,endTime){
200
	jQuery.ajax({
201
	        type : "GET",
22090 amit.gupta 202
	        url : context+"/getPaginatedGrnHistory/?purchaseReference="+purchase_reference+"&searchType="+searchType
21987 kshitij.so 203
	        +"&startTime="+startTime
204
	        +"&endTime="+endTime+"&offset="+pre,
205
			beforeSend: function(){
206
	        //$('#ajax-spinner').show();
207
	        },
208
	        complete: function(){
209
	        //$('#ajax-spinner').hide();
210
	        },
211
	        success : function(response) {
212
	        	$( "#grn-history-paginated .end" ).text(+end - +10);
213
	        	$( "#grn-history-paginated .start" ).text(+start - +10);
214
	        	$('#grn-history-table').html(response);
215
	        	$("#grn-history-paginated .next").prop('disabled', false);
216
				if (parseInt(pre)==0)
217
				{
218
					$("#grn-history-paginated .previous").prop('disabled', true);
219
				}
220
 
221
	        },
222
			error : function() {
223
				alert("Unable to fetch items");
224
			 },
225
	    });
226
}
227
 
228
function loadGoodInventorySearchInfo(search_text){
229
	loadGoodInventory("main-content",search_text);
230
}
231
 
232
function loadGrnHistorySearchInfo(search_text,searchType,startTime,endTime){
233
	loadGrnHistory("main-content",search_text,searchType,startTime,endTime);
234
}
235
 
21627 kshitij.so 236
function findDuplicateSerialNumbers(value){
237
    var result = $("#grnImeiInformation :input[value='" + value + "']").length - 1;
238
    return result;
239
}
21987 kshitij.so 240
 
241
function loadGrnDetails(purchaseId,domId){
242
	jQuery.ajax({
243
        type : "GET",
22090 amit.gupta 244
        url : context+"/grnHistoryDetailByPurchaseId/?purchaseId="+purchaseId,
21987 kshitij.so 245
        success : function(response) {
246
            $('#' + domId).html(response);
247
            window.dispatchEvent(new Event('resize'));
248
        }
249
    });
250
}
251
 
22245 ashik.ali 252
function loadSaleDetails(orderId, domId){
253
	jQuery.ajax({
254
        type : "GET",
255
        url : context+"/saleDetails?orderId="+orderId,
256
        success : function(response) {
257
            $('#' + domId).html(response);
258
            window.dispatchEvent(new Event('resize'));
259
        }
260
    });
261
}
262
 
21987 kshitij.so 263
function loadPendingGrnDetails(purchaseId,domId){
264
	jQuery.ajax({
22090 amit.gupta 265
		url: context+"/purchase/?airwayBillOrInvoiceNumber="+purchaseId,
21987 kshitij.so 266
		type: 'POST',
267
		async: false,
268
		success: function (data) {
269
			$('#'+domId).html(data);
270
		},
271
		error : function() {
272
			alert("OOPS!!!Failed to do changes.Try Again.",'ERROR');
273
		},
274
		cache: false,
275
		contentType: false,
276
		processData: false
277
	});
278
}
279
 
280
function getNextCatalogItems(start, end, searchText){
281
	console.log(start);
282
	console.log(end);
283
	console.log(+end + +10);
284
	console.log(+start + +10);
285
	jQuery.ajax({
286
	        type : "GET",
22090 amit.gupta 287
	        url : context+"/getPaginatedCatalog/?offset="+end+"&searchTerm="+searchText,
21987 kshitij.so 288
			beforeSend: function(){
289
	        //$('#ajax-spinner').show();
290
	        },
291
	        complete: function(){
292
	        //$('#ajax-spinner').hide();
293
	        },
294
	        success : function(response) {
295
	        	$( "#catalog-paginated .end" ).text(+end + +10);
296
	        	$( "#catalog-paginated .start" ).text(+start + +10);
297
				var last = $( "#catalog-paginated .end" ).text();
298
				var temp = $( "#catalog-paginated .size" ).text();
299
				if (parseInt(last) >= parseInt(temp)){
300
					$("#catalog-paginated .next").prop('disabled', true);
301
				}
302
	            $('#catalog-table').html(response);
303
	            $("#catalog-paginated .previous").prop('disabled', false);
304
	        },
305
			error : function() {
306
				alert("Unable to fetch items");
307
			 },
308
	    });  
309
	}
310
function getPreviousCatalogItems(start,end,pre,searchText){
311
	jQuery.ajax({
312
	        type : "GET",
22090 amit.gupta 313
	        url : context+"/getPaginatedCatalog/?offset="+pre+"&searchTerm="+searchText,
21987 kshitij.so 314
			beforeSend: function(){
315
	        //$('#ajax-spinner').show();
316
	        },
317
	        complete: function(){
318
	        //$('#ajax-spinner').hide();
319
	        },
320
	        success : function(response) {
321
	        	$( "#catalog-paginated .end" ).text(+end - +10);
322
	        	$( "#catalog-paginated .start" ).text(+start - +10);
323
	        	$('#catalog-table').html(response);
324
	        	$("#catalog-paginated .next").prop('disabled', false);
325
				if (parseInt(pre)==0)
326
				{
327
					$("#catalog-paginated .previous").prop('disabled', true);
328
				}
329
 
330
	        },
331
			error : function() {
332
				alert("Unable to fetch items");
333
			 },
334
	    });
335
}
336
 
337
function loadCatalogSearchInfo(search_text){
338
	loadCatalog("main-content",search_text);
339
}
340
 
341
function addItemInLocalStorage(bagObj){
342
	if (localStorage.getItem("bag")!=null){
343
		var bag = JSON.parse(localStorage.getItem("bag"));
344
		bag[bagObj.itemId] = bagObj
345
		localStorage.setItem("bag",JSON.stringify(bag));
346
		bag = localStorage.getItem("bag")
347
		$("#cart_bar").find('span').text(Object.keys(JSON.parse(bag)).length);
348
	}
349
	else{
350
		var tempObj = {};
351
		tempObj[bagObj.itemId] = bagObj
352
		localStorage.setItem("bag",JSON.stringify(tempObj));
353
		var bag = localStorage.getItem("bag")
354
		$("#cart_bar").find('span').text(Object.keys(JSON.parse(bag)).length);
355
	}
356
}
357
 
358
 
359
function changeQuantityInLocalStorage(itemId, newQuantity){
360
	var bag = JSON.parse(localStorage.getItem("bag"));
361
	bag[itemId].quantity = newQuantity;
362
	localStorage.setItem("bag",JSON.stringify(bag));
363
}
364
 
365
function removeItemFromLocalStorage(itemId){
366
	if (localStorage.getItem("bag")!=null){
367
		var bag = JSON.parse(localStorage.getItem("bag"));
368
		delete bag[itemId];
369
		localStorage.setItem("bag",JSON.stringify(bag));
370
		$("#cart_bar").find('span').text(Object.keys(bag).length);
371
	}
372
}
373
 
374
function emptyBag(){
22095 kshitij.so 375
	localStorage.setItem("bag",JSON.stringify({}));
21987 kshitij.so 376
	$("#cart_bar").find('span').text(0);
377
}
378
 
379
function loadCart(domId){
380
	jQuery.ajax({
381
        type : "POST",
382
        data: {"cartData":localStorage.getItem("bag")},
22090 amit.gupta 383
        url : context+"/cart",
21987 kshitij.so 384
        success : function(response) {
385
            $('#' + domId).html(response);
386
        }
387
    });  
388
}
389
 
390
function checkout(domId){
391
	jQuery.ajax({
22095 kshitij.so 392
        type : "GET",
21987 kshitij.so 393
        data: {"cartData":localStorage.getItem("bag")},
22090 amit.gupta 394
        url : context+"/validate-cart",
21987 kshitij.so 395
        success : function(response) {
396
        	var obj = JSON.parse(response);
397
        	redirectCart(obj.redirectUrl, obj.params, obj.method, "main-content");
22095 kshitij.so 398
        	$('#main-content').html(response);
21987 kshitij.so 399
        }
22245 ashik.ali 400
    });
21987 kshitij.so 401
}
402
 
403
function redirectCart(url, cartData, method, domId){
404
	jQuery.ajax({
405
        type : method,
406
        data: {"cartData":cartData},
407
        url : context+url,
408
        success : function(response) {
409
            $('#' + domId).html(response);
410
            window.dispatchEvent(new Event('resize'));
411
        }
412
    });  
413
}
414
 
22245 ashik.ali 415
function saleHistory(domId, invoiceNumber, searchType, startTime, endTime){
416
	jQuery.ajax({
417
        type : "GET",
418
        url : context+"/saleHistory?invoiceNumber="+invoiceNumber+"&searchType="+searchType
419
        +"&startTime="+startTime
420
        +"&endTime="+endTime,
421
        success : function(response) {
422
            $('#' + domId).html(response);
423
        }
424
    });  
425
}
426
 
427
 
21987 kshitij.so 428
function validateOrderDetails(){
429
	var sNumbers = [];
430
	var error = false;
431
	$("form#cd input.serialNumber").each(function(){
432
		var input = $(this).val().trim();
22245 ashik.ali 433
		var itemType = $(this).attr("itemType");
21987 kshitij.so 434
		$(this).removeClass("border-highlight");
22245 ashik.ali 435
		if (sNumbers.indexOf(input) !=-1 || (itemType ==='SERIALIZED' && !input)){
21987 kshitij.so 436
			error = true;
437
			$(this).addClass("border-highlight");
438
		}
439
		sNumbers.push(input);
440
	});
441
 
442
	$("form#cd input.unitPrice").each(function(){
443
		var input = $(this).val().trim();
444
		$(this).removeClass("border-highlight");
445
		if (!input || parseInt(input)<=0 || isNaN(input)){
446
			error=true;
447
			$(this).addClass("border-highlight");
448
		}
449
	});
450
 
451
	var amount = 0;
452
	var netPayableAmount = parseFloat($("form#cd input.netPayableAmount").val());
453
 
454
 
455
	$("form#cd input.amount").each(function(){
456
		if ($(this).val() ==""){
457
			$(this).val(0);
458
		}
459
		var tmpAmount = parseFloat($(this).val());
460
		amount = amount + tmpAmount;
461
	});
462
 
463
	console.log(amount);
464
	console.log(netPayableAmount);
465
 
466
	if (amount!=netPayableAmount){
467
		alert("Mismatch in amount");
468
		error = true;
469
	}
470
 
471
	if (error){
472
		return false;
473
	}
474
	return true;
475
 
476
}
477
 
478
function orderDetailsPayload(){
479
		var orderObj = {};
480
		var priceQtyArray = [];
481
		var customerObj = {};
482
		var paymentOption = [];
483
 
484
		$("form#cd input.serialNumber").each(function(){
485
			var itemId = parseInt($(this).attr("itemId"));
486
			if (orderObj.hasOwnProperty('fofoLineItems')){
487
				var itemDetails = orderObj['fofoLineItems'];
488
				if (itemDetails.hasOwnProperty(itemId)){
489
					var serialNumbers = itemDetails[itemId];
490
					serialNumbers.push($(this).val());
491
					itemDetails[itemId] = serialNumbers;
22245 ashik.ali 492
				}else{
21987 kshitij.so 493
					var serialNumbers = [];
494
					serialNumbers.push($(this).val());
495
					itemDetails[itemId] = serialNumbers;
496
				}
22245 ashik.ali 497
			}else{
21987 kshitij.so 498
				var serialNumbers = [];
499
				serialNumbers.push($(this).val());
500
				var tmp ={};
501
				tmp[itemId]  = serialNumbers;
502
				orderObj['fofoLineItems'] = tmp;
503
			}
504
		});
505
		console.log( JSON.stringify(orderObj));
506
		$("form#cd input.unitPrice").each(function(){
507
			var itemId = parseInt($(this).attr("itemId"));
508
			var unitPrice = parseFloat($(this).val());
509
			var qty = parseInt($(this).attr("quantity"));
510
			var tmpObj = {'itemId':itemId,'sellingPrice':unitPrice,'quantity':qty};
22245 ashik.ali 511
			if (orderObj['fofoLineItems'][itemId] === undefined){
512
				tmpObj['serialNumberDetails'] =[];
21987 kshitij.so 513
			}
514
			else{
22245 ashik.ali 515
				//tmpObj['serialNumbers'] = orderObj['fofoLineItems'][itemId];
516
				tmpObj['serialNumberDetails'] = [];
21987 kshitij.so 517
			}
22245 ashik.ali 518
			var found = false;
519
			for(var i = 0; i < priceQtyArray.length; i++){			
520
				if (priceQtyArray[i]["itemId"] == itemId){
521
					found = true;
522
					break;
523
				}
524
			}
525
			if(!found){
526
				priceQtyArray.push(tmpObj);
527
			}
21987 kshitij.so 528
		});
529
 
22245 ashik.ali 530
 
531
		$("#order-details").find("tr:not(:first-child)").each(function(index,el){
532
			//console.log(el);
533
			//console.log(index);
534
			var $serialNumberElement = $(el).find('.serialNumber');
535
			var itemId = parseInt($serialNumberElement.attr("itemId"));
536
			var insuranceAmount = parseFloat($(el).find('.insuranceAmount').val());
537
			//if (priceQtyArray['fofoLineItems'][itemId] !=undefined){
538
 
539
			//}
540
			//console.log($serialNumberElement.val());
541
			//console.log(itemId);
542
			for(var i = 0; i < priceQtyArray.length; i++){
543
				console.log(priceQtyArray[i]["itemId"]);			
544
				if (priceQtyArray[i]["itemId"] == itemId){
545
					var insurance = false;
546
					if(insuranceAmount > 0){
547
						insurance = true;
548
					}
549
					var serialNumberDetails = {'serialNumber':$serialNumberElement.val(),'insurance':insurance,'amount':insuranceAmount}
550
					priceQtyArray[i]['serialNumberDetails'].push(serialNumberDetails);
551
				}
552
            }
553
 
554
		});
555
 
556
		console.log("priceQtyArray : "+JSON.stringify(priceQtyArray));
21987 kshitij.so 557
		//customer-details
558
		/*$("#customer-details input").each(function(){
559
			console.log($(this).attr('name'));
560
			customerObj[$(this).attr('name')] = $(this).val();
561
		});*/
562
		//$("input[name=nameGoesHere]").val();
22245 ashik.ali 563
		customerObj['firstName'] = $("form#cd input[name=firstName]").val();
564
		customerObj['lastName'] = $("form#cd input[name=lastName]").val();
21987 kshitij.so 565
		customerObj['mobileNumber'] = $("form#cd input[name=phone]").val(); 
566
		customerObj['emailId'] = $("form#cd input[name=email]").val();
22245 ashik.ali 567
		customerObj['dateOfBirth'] = $("form#cd input[name=dateOfBirth").val();
21987 kshitij.so 568
		var customerAddress = {};
22245 ashik.ali 569
		customerAddress['name'] = $("form#cd input[name=firstName]").val() + " " + $("form#cd input[name=lastName]").val();
21987 kshitij.so 570
		customerAddress['line1'] = $("form#cd input[name=line1]").val();
571
		customerAddress['line2'] = $("form#cd input[name=line2]").val();
572
		customerAddress['landmark'] = $("form#cd input[name=landmark]").val();
573
		customerAddress['city'] = $("form#cd input[name=city]").val();
574
		customerAddress['state'] = $('select[name=state] option:selected').val();
22245 ashik.ali 575
		customerAddress['pinCode'] = $("form#cd input[name=pinCode]").val();
21987 kshitij.so 576
		customerAddress['phoneNumber'] = $("form#cd input[name=alternatePhone]").val();
577
		customerAddress['country'] = "India";
578
		customerObj['address'] = customerAddress; 
579
		console.log( JSON.stringify(customerObj));
580
		$("#payment-details input").each(function(){
581
		console.log($(this).attr('name'));
582
		if ($(this).attr('name') =="" ){
583
			return;
584
		}
585
		var paymentObj = {};
586
		paymentObj['type'] = $(this).attr('name');
587
		if ($(this).val() == ""){
588
			paymentObj['amount'] = 0.0;
589
		}
590
		else{
591
			paymentObj['amount'] = parseFloat($(this).val());
592
		}
593
		paymentOption.push(paymentObj);
594
		});
595
		console.log( JSON.stringify(paymentOption));
596
 
597
		var retObj = {};
598
		retObj['fofoLineItems'] = (priceQtyArray);
599
		retObj['customer'] = (customerObj);
600
		retObj['paymentOptions'] =  (paymentOption);
601
		console.log(retObj);
602
		return  JSON.stringify(retObj);
603
}