Subversion Repositories SmartDukaan

Rev

Rev 22071 | Rev 22090 | 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",
22073 ashik.ali 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();
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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({
22073 ashik.ali 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",
22073 ashik.ali 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",
22073 ashik.ali 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(){
355
	localStorage.removeItem("bag");
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")},
22073 ashik.ali 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({
372
        type : "POST",
373
        data: {"cartData":localStorage.getItem("bag")},
22073 ashik.ali 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");
378
        }
379
    });  
380
}
381
 
382
function redirectCart(url, cartData, method, domId){
383
	jQuery.ajax({
384
        type : method,
385
        data: {"cartData":cartData},
386
        url : context+url,
387
        success : function(response) {
388
            $('#' + domId).html(response);
389
            window.dispatchEvent(new Event('resize'));
390
        }
391
    });  
392
}
393
 
394
function validateOrderDetails(){
395
	var sNumbers = [];
396
	var error = false;
397
	$("form#cd input.serialNumber").each(function(){
398
		var input = $(this).val().trim();
399
		$(this).removeClass("border-highlight");
400
		if (sNumbers.indexOf(input) !=-1 || !input){
401
			error = true;
402
			$(this).addClass("border-highlight");
403
		}
404
		sNumbers.push(input);
405
	});
406
 
407
	$("form#cd input.unitPrice").each(function(){
408
		var input = $(this).val().trim();
409
		$(this).removeClass("border-highlight");
410
		if (!input || parseInt(input)<=0 || isNaN(input)){
411
			error=true;
412
			$(this).addClass("border-highlight");
413
		}
414
	});
415
 
416
	var amount = 0;
417
	var netPayableAmount = parseFloat($("form#cd input.netPayableAmount").val());
418
 
419
 
420
	$("form#cd input.amount").each(function(){
421
		if ($(this).val() ==""){
422
			$(this).val(0);
423
		}
424
		var tmpAmount = parseFloat($(this).val());
425
		amount = amount + tmpAmount;
426
	});
427
 
428
	console.log(amount);
429
	console.log(netPayableAmount);
430
 
431
	if (amount!=netPayableAmount){
432
		alert("Mismatch in amount");
433
		error = true;
434
	}
435
 
436
	if (error){
437
		return false;
438
	}
439
	return true;
440
 
441
}
442
 
443
function orderDetailsPayload(){
444
		var orderObj = {};
445
		var priceQtyArray = [];
446
		var customerObj = {};
447
		var paymentOption = [];
448
 
449
		$("form#cd input.serialNumber").each(function(){
450
			var itemId = parseInt($(this).attr("itemId"));
451
			if (orderObj.hasOwnProperty('fofoLineItems')){
452
				var itemDetails = orderObj['fofoLineItems'];
453
				if (itemDetails.hasOwnProperty(itemId)){
454
					var serialNumbers = itemDetails[itemId];
455
					serialNumbers.push($(this).val());
456
					itemDetails[itemId] = serialNumbers;
457
				}
458
				else{
459
					var serialNumbers = [];
460
					serialNumbers.push($(this).val());
461
					itemDetails[itemId] = serialNumbers;
462
				}
463
			}
464
			else{
465
				var serialNumbers = [];
466
				serialNumbers.push($(this).val());
467
				var tmp ={};
468
				tmp[itemId]  = serialNumbers;
469
				orderObj['fofoLineItems'] = tmp;
470
			}
471
		});
472
		console.log( JSON.stringify(orderObj));
473
		$("form#cd input.unitPrice").each(function(){
474
			var itemId = parseInt($(this).attr("itemId"));
475
			var unitPrice = parseFloat($(this).val());
476
			var qty = parseInt($(this).attr("quantity"));
477
			var tmpObj = {'itemId':itemId,'sellingPrice':unitPrice,'quantity':qty};
478
			if (orderObj['fofoLineItems'][itemId] ===undefined){
479
				tmpObj['serialNumbers'] =[]
480
			}
481
			else{
482
				tmpObj['serialNumbers'] = orderObj['fofoLineItems'][itemId];
483
			}
484
			priceQtyArray.push(tmpObj);
485
		});
486
		console.log( JSON.stringify(priceQtyArray));
487
 
488
		//customer-details
489
		/*$("#customer-details input").each(function(){
490
			console.log($(this).attr('name'));
491
			customerObj[$(this).attr('name')] = $(this).val();
492
		});*/
493
		//$("input[name=nameGoesHere]").val();
494
		customerObj['name'] = $("form#cd input[name=name]").val(); 
495
		customerObj['mobileNumber'] = $("form#cd input[name=phone]").val(); 
496
		customerObj['emailId'] = $("form#cd input[name=email]").val();
497
		var customerAddress = {};
498
		customerAddress['name'] = $("form#cd input[name=name]").val();
499
		customerAddress['line1'] = $("form#cd input[name=line1]").val();
500
		customerAddress['line2'] = $("form#cd input[name=line2]").val();
501
		customerAddress['landmark'] = $("form#cd input[name=landmark]").val();
502
		customerAddress['city'] = $("form#cd input[name=city]").val();
503
		customerAddress['state'] = $('select[name=state] option:selected').val();
504
		customerAddress['pinCode'] = $("form#cd input[name=pincode]").val();
505
		customerAddress['phoneNumber'] = $("form#cd input[name=alternatePhone]").val();
506
		customerAddress['country'] = "India";
507
		customerObj['address'] = customerAddress; 
508
		console.log( JSON.stringify(customerObj));
509
		$("#payment-details input").each(function(){
510
		console.log($(this).attr('name'));
511
		if ($(this).attr('name') =="" ){
512
			return;
513
		}
514
		var paymentObj = {};
515
		paymentObj['type'] = $(this).attr('name');
516
		if ($(this).val() == ""){
517
			paymentObj['amount'] = 0.0;
518
		}
519
		else{
520
			paymentObj['amount'] = parseFloat($(this).val());
521
		}
522
		paymentOption.push(paymentObj);
523
		});
524
		console.log( JSON.stringify(paymentOption));
525
 
526
		var retObj = {};
527
		retObj['fofoLineItems'] = (priceQtyArray);
528
		retObj['customer'] = (customerObj);
529
		retObj['paymentOptions'] =  (paymentOption);
530
		console.log(retObj);
531
		return  JSON.stringify(retObj);
532
}