Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
22956 ashik.ali 1
function badRequestAlert(response){
2
	console.log(response.responseText);
3
	var errorObject = JSON.parse(response.responseText);
4
	errorObject = errorObject.response;
5
	alert('Bad Request\n'+
6
			'rejectedType : '+errorObject.rejectedType+'\n'+
7
			'rejectedValue : '+errorObject.rejectedValue+'\n'+
8
			'message : '+errorObject.message);
9
}
23347 ashik.ali 10
 
22956 ashik.ali 11
function internalServerErrorAlert(response){
12
	console.log(response.responseText);
13
	var errorObject = JSON.parse(response.responseText);
14
	errorObject = errorObject.response;
15
	alert('Internal Server Error\n'+
16
		'rejectedType : '+errorObject.rejectedType+'\n'+
17
		'rejectedValue : '+errorObject.rejectedValue+'\n'+
18
		'message : '+errorObject.message);
19
}
20
 
21
$( document ).ajaxError(function(event, jqxhr, settings, thrownError) {
24271 amit.gupta 22
	if(typeof loaderDialogObj!="undefined")
23
		loaderDialogObj.modal('hide');
22956 ashik.ali 24
	if(jqxhr.status == 400){
23872 amit.gupta 25
		// $('#error-prompt-model').modal();
22956 ashik.ali 26
		badRequestAlert(jqxhr);	
27
	}else{
28
		internalServerErrorAlert(jqxhr);
29
	}
30
});
31
 
23946 amit.gupta 32
$(document).ajaxComplete(function(){
24271 amit.gupta 33
	if(typeof loaderDialogObj!="undefined")
34
		loaderDialogObj.modal('hide');
23946 amit.gupta 35
});
36
 
23918 amit.gupta 37
$( document ).ajaxStart(function() {
24271 amit.gupta 38
		if(typeof loaderDialogObj!="undefined")
24334 amit.gupta 39
			loaderDialogObj.modal('show');
24371 amit.gupta 40
		if(typeof isInvestmentOk != "undefined" && !isInvestmentOk && $('#order-details').length == 0) {
23946 amit.gupta 41
			var investmentDialog = bootbox.dialog({
24367 amit.gupta 42
				title: '<h3	class="text-danger">ATTENTION!! ORDERING THROUGH APP MAY BE BLOCKED DUE TO LOW INVESTMENTS!</h3>',
43
				message : "<h3>Dear Partner, your investments are low by " + shortPercentage + "%</h3>"
23918 amit.gupta 44
					+  "<dl>"
24356 amit.gupta 45
					+  "<dt>Investment Should be</dt><dd>- " + minimumInvestmentAmount +"</dd>"
23921 amit.gupta 46
					+ "<dt>Total Invested Amount</dt><dd>-" + totalInvestedAmount +"</dd>"
24356 amit.gupta 47
					+  '<dt>Investment Amount Short By</dt><dd class="text-danger lead">-  ' + shortAmount+'</dd></dl>'
24369 amit.gupta 48
			 		+ '<pre class="text-danger" style="font-size:18px;"><strong>To unblock your Billing, please \nadd Rs.'+ minAmountToBeAdded + ' to wallet immediately.</strong></pre>',
23918 amit.gupta 49
			 	buttons:{
50
			 			cancel: {
51
			 				label: "OK",
23919 amit.gupta 52
			 		        className: 'btn-primary'
23918 amit.gupta 53
			 			}
23921 amit.gupta 54
			 		},
23918 amit.gupta 55
			});
23946 amit.gupta 56
			investmentDialog.on('shown.bs.modal', function(){
57
				var dialogEl = investmentDialog.find('.modal-content')[0];
58
				var dialogRect = dialogEl.getBoundingClientRect();
59
				var height = window.innerHeight - dialogRect.height;
60
				var width = window.innerWidth - dialogRect.width;
61
				var left = Math.floor(Math.random()*width) - dialogRect.left
62
				var top = Math.floor(Math.random()*height) - dialogRect.top
63
				$(dialogEl).css("left",left).css("top",top);
64
			});
23918 amit.gupta 65
		}
66
	});
23026 ashik.ali 67
 
23918 amit.gupta 68
 
69
 
23193 ashik.ali 70
function doAjaxRequestWithParamsHandler(urlString, httpType, params, callback_function){
71
	$.ajax({
72
	   url: urlString,
23343 ashik.ali 73
	   async: true,
23193 ashik.ali 74
	   cache: false,
75
	   data: params,
23872 amit.gupta 76
       // dataType:'json',
23193 ashik.ali 77
	   type: httpType,
78
	   success: function(response) {
79
	      callback_function(response);
23786 amit.gupta 80
	      $('.currency').each(function(index,ele){
81
	    	  if(!isNaN(parseInt($(ele).html()))) {
82
	    		  $(ele).html(numberToComma($(ele).html()));  
83
	    	  }
84
	      });
23193 ashik.ali 85
	   }
86
	});
87
}
88
 
23500 ashik.ali 89
function doGetAjaxRequestWithParamsHandler(urlString, params, callback_function){
90
	doAjaxRequestWithParamsHandler(urlString, "GET", params, callback_function);
91
}
92
 
93
function doPostAjaxRequestWithParamsHandler(urlString, params, callback_function){
94
	doAjaxRequestWithParamsHandler(urlString, "POST", params, callback_function);
95
}
96
 
23032 ashik.ali 97
function doAjaxRequestWithJsonHandler(urlString, httpType, json, callback_function){
98
	$.ajax({
99
	   url: urlString,
23343 ashik.ali 100
	   async: true,
23032 ashik.ali 101
	   cache: false,
102
	   processData: false,
103
	   data: json,
104
       contentType:'application/json',
105
	   type: httpType,
106
	   success: function(response) {
23872 amit.gupta 107
	      // console.log("response"+JSON.stringify(data));
23032 ashik.ali 108
	      callback_function(response);
23786 amit.gupta 109
	      $('.currency').each(function(index,ele){
110
	    	  if(!isNaN(parseInt($(ele).html()))) {
111
	    		  $(ele).html(numberToComma($(ele).html()));  
112
	    	  }
113
	      });
23032 ashik.ali 114
	   }
115
	});
116
}
117
 
23500 ashik.ali 118
function doPostAjaxRequestWithJsonHandler(urlString, json, callback_function){
119
	doAjaxRequestWithJsonHandler(urlString, "POST", json, callback_function);
120
}
23032 ashik.ali 121
 
23500 ashik.ali 122
function doPutAjaxRequestWithJsonHandler(urlString, json, callback_function){
123
	doAjaxRequestWithJsonHandler(urlString, "PUT", json, callback_function);
124
}
125
 
23026 ashik.ali 126
function doAjaxRequestHandler(urlString, httpType, callback_function){
22982 ashik.ali 127
	$.ajax({
128
	   url: urlString,
23343 ashik.ali 129
	   async: true,
23026 ashik.ali 130
	   cache: false,
22982 ashik.ali 131
	   type: httpType,
132
	   success: function(response) {
133
	      callback_function(response);
23786 amit.gupta 134
	      $('.currency').each(function(index,ele){
135
	    	  if(!isNaN(parseInt($(ele).html()))) {
136
	    		  $(ele).html(numberToComma($(ele).html()));  
137
	    	  }
138
	      });
22982 ashik.ali 139
	   }
140
	});
141
}
142
 
23500 ashik.ali 143
function doGetAjaxRequestHandler(urlString, callback_function){
144
	doAjaxRequestHandler(urlString, "GET", callback_function);
145
}
146
 
147
function doPutAjaxRequestHandler(urlString, callback_function){
148
	doAjaxRequestHandler(urlString, "PUT", callback_function);
149
}
150
 
23629 ashik.ali 151
function doPostAjaxRequestHandler(urlString, callback_function){
152
	doAjaxRequestHandler(urlString, "POST", callback_function);
153
}
154
 
23783 ashik.ali 155
function doDeleteAjaxRequestHandler(urlString, callback_function){
156
	doAjaxRequestHandler(urlString, "DELETE", callback_function);
157
}
158
 
23347 ashik.ali 159
function doAjaxUploadRequest(urlString, httpType, file){
160
	var response;
161
	doAjaxUploadRequestHandler(urlString, httpType, file, function(ajaxResponse){
162
		response = ajaxResponse;
163
	});
164
	return response;
165
}
166
 
23026 ashik.ali 167
function doAjaxUploadRequestHandler(urlString, httpType, file, callback_function){
22982 ashik.ali 168
	var formData = new FormData();
169
	formData.append("file", file);
170
	$.ajax({
171
	   url: urlString,
172
	   type: httpType,
173
	   data: formData,
174
       dataType: 'json',
23343 ashik.ali 175
       async: true,
22982 ashik.ali 176
       cache: false,
177
       contentType: false,
178
       enctype: 'multipart/form-data',
179
       processData: false,
180
	   success: function(response) {
23872 amit.gupta 181
	      // console.log("response"+JSON.stringify(data));
22982 ashik.ali 182
	      callback_function(response);
183
	   }
184
	});
185
}
24171 govind 186
function doAjaxUploadRequestJsonHandler(urlString, httpType, file,json,callback_function){
187
	var formData = new FormData();
188
	formData.append("file", file);
189
	formData.append('json',new Blob([json]));
190
	$.ajax({
191
	   url: urlString,
192
	   type: httpType,
193
	   data: formData,
194
	   enctype: 'multipart/form-data',
195
       contentType: false,
196
       processData: false,
197
	   success: function(response) {
198
	      // console.log("response"+JSON.stringify(data));
199
	      callback_function(response);
200
	   }
201
	});
202
}
22982 ashik.ali 203
 
23347 ashik.ali 204
function uploadDocument(file){
24079 amit.gupta 205
	var url = webApiScheme + '://'+ webApiHost + ':' + webApiPort +  webApiRoot + '/document-upload';
23377 ashik.ali 206
	doAjaxUploadRequestHandler(url, 'POST', file, function(response){
207
		var documentId = response.response.document_id; 
208
		console.log("documentId : "+documentId);
209
		return documentId;
210
	});
23347 ashik.ali 211
}
212
 
23377 ashik.ali 213
function doAjaxGetDownload(urlString, fileName){
214
	console.log("fileName : "+fileName);
23343 ashik.ali 215
	doAjaxDownload(urlString, "GET", null, fileName);
22982 ashik.ali 216
}
217
 
23377 ashik.ali 218
function doAjaxPostDownload(urlString, data, fileName){
23343 ashik.ali 219
	doAjaxDownload(urlString, "POST", data, fileName);
21627 kshitij.so 220
}
221
 
23343 ashik.ali 222
function doAjaxDownload(urlString, httpType, data, fileName){
22488 ashik.ali 223
	xhttp = new XMLHttpRequest();
224
	xhttp.onreadystatechange = function() {
225
	    var a;
23494 ashik.ali 226
	    if(xhttp.readyState === 2){
227
	    	if(xhttp.status == 200){
228
	    		xhttp.responseType = "blob";
229
            } else {
230
                xhttp.responseType = "text";
231
            }
232
	    }else if (xhttp.readyState === 4 && xhttp.status === 200) {
22488 ashik.ali 233
	        // Trick for making downloadable link
234
	        a = document.createElement('a');
235
	        a.href = window.URL.createObjectURL(xhttp.response);
236
	        // Give filename you wish to download
23343 ashik.ali 237
	        a.download = fileName;
22488 ashik.ali 238
	        a.style.display = 'none';
239
	        document.body.appendChild(a);
240
	        a.click();
23494 ashik.ali 241
	    }else if(xhttp.readyState == 4 && xhttp.status === 400){
242
    		badRequestAlert(xhttp);
243
	    }else if(xhttp.readyState == 4 && xhttp.status === 500){
244
	    	internalServerErrorAlert(xhttp);
22488 ashik.ali 245
	    }
246
	};
247
	// Post data to URL which handles post request
23343 ashik.ali 248
	xhttp.open(httpType, urlString);
249
	if(httpType == "POST"){
250
		xhttp.setRequestHeader("Content-Type", "application/json");
251
	}
22488 ashik.ali 252
	// You should set responseType as blob for binary responses
23872 amit.gupta 253
	// xhttp.responseType = 'blob';
22488 ashik.ali 254
	xhttp.send(data);
23405 amit.gupta 255
}
256
 
23629 ashik.ali 257
function loadPaginatedNextItems(url, params, paginatedIdentifier, tableIdentifier, detailsContainerIdentifier){
258
	var start = $("#"+paginatedIdentifier+" .start").text();
259
	var end = $("#"+paginatedIdentifier+" .end").text();
260
	url = context + url + "?offset=" + end;
261
 
262
	if(params != null){
263
		for (var key in params) {
264
			if (params.hasOwnProperty(key)) {
23872 amit.gupta 265
				// console.log(key + " -> " + p[key]);
23629 ashik.ali 266
				url = url +"&" + key + "=" + params[key];
267
			}
268
		}
269
	}
270
 
271
	doGetAjaxRequestHandler(url, function(response){
272
		var size = $("#"+paginatedIdentifier+" .size").text();
273
		if((parseInt(end) + 10) > parseInt(size)){
274
			console.log("(end + 10) > size == true");
275
			$("#"+paginatedIdentifier+" .end").text(size);
276
		}else{
277
			console.log("(end + 10) > size == false");
278
			$("#"+paginatedIdentifier+" .end").text(+end + +10);
279
		}
280
		$("#"+paginatedIdentifier+" .start").text(+start + +10);
281
		var last = $("#"+paginatedIdentifier+" .end").text();
282
		var temp = $("#"+paginatedIdentifier+" .size").text();
283
		if (parseInt(last) >= parseInt(temp)){
284
			$("#"+paginatedIdentifier+" .next").prop('disabled', true);
23872 amit.gupta 285
			// $( "#good-inventory-paginated .end" ).text(temp);
23629 ashik.ali 286
		}
287
	    $('#'+tableIdentifier).html(response);
288
	    if(detailsContainerIdentifier != null){
289
			$('#'+detailsContainerIdentifier).html('');
290
		}
291
	    $("#"+paginatedIdentifier+" .previous").prop('disabled', false);
292
	});
293
 
294
}
23405 amit.gupta 295
 
23629 ashik.ali 296
 
297
function loadPaginatedPreviousItems(url, params, paginatedIdentifier, tableIdentifier, detailsContainerIdentifier){
298
	var start = $("#"+paginatedIdentifier+" .start").text();
299
	var end =  $("#"+paginatedIdentifier+" .end").text();
300
	var size = $("#"+paginatedIdentifier+" .size").text();
23819 govind 301
	if(parseInt(end) == parseInt(size) && parseInt(end)%10 !=0){
23629 ashik.ali 302
		var mod = parseInt(end) % 10;
303
		end = parseInt(end) + (10 - mod); 
304
	}
305
	var pre = end - 20;
306
 
307
	url = context + url + "?offset=" + pre;
308
 
309
	if(params != null){
310
		for (var key in params) {
311
			if (params.hasOwnProperty(key)) {
312
				url = url +"&" + key + "=" + params[key];
313
			}
314
		}
315
	}
316
 
317
	doGetAjaxRequestHandler(url, function(response){
318
		$("#"+paginatedIdentifier+" .end").text(+end - +10);
319
		$("#"+paginatedIdentifier+" .start").text(+start - +10);
320
		$('#'+tableIdentifier).html(response);
321
		if(detailsContainerIdentifier != null){
322
			$('#'+detailsContainerIdentifier).html('');
323
		}
324
		$("#"+paginatedIdentifier+" .next").prop('disabled', false);
325
		if (parseInt(pre)==0){
326
			$("#"+paginatedIdentifier+" .previous").prop('disabled', true);
327
		}
328
	});	
329
 
330
}
331
 
332
 
23405 amit.gupta 333
function numberToComma(x) {
23786 amit.gupta 334
	x=parseInt(x);
23405 amit.gupta 335
	x=x.toString();
336
	var lastThree = x.substring(x.length-3);
337
	var otherNumbers = x.substring(0,x.length-3);
338
	if(otherNumbers != '')
339
	    lastThree = ',' + lastThree;
340
	return otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
23786 amit.gupta 341
}
23870 amit.gupta 342
 
343
function dateRangeCallback(start, end) {
23886 amit.gupta 344
	startMoment = start;
345
	endMoment = end;
346
	startDate = start.format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS);
347
	endDate = end.format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS);
23872 amit.gupta 348
}
349
 
350
function getSingleDatePicker(){
351
	var singleDatePicker =  
352
	{ 
353
		"todayHighlight": true, 
23886 amit.gupta 354
		"startDate": startMoment || moment(),
23872 amit.gupta 355
		"autoclose": true,
356
		"autoUpdateInput": true,
357
		"singleDatePicker": true,
358
    "locale": {
359
		'format': 'DD/MM/YYYY'
360
	}
361
  };
362
	return singleDatePicker;
23886 amit.gupta 363
}
364
 
365
function getRangedDatePicker(showRanges){
366
	if(typeof showRanges=="undefined") {
367
		showRanges = false;
368
	}
369
	var rangedDatePicker =  
370
	{ 
371
			"todayHighlight": true,
23914 govind 372
			"opens": "right",
23886 amit.gupta 373
			"startDate": startMoment || moment(),
374
			"endDate": endMoment || moment(),
375
			"autoclose": true,
376
			"alwaysShowCalendars": false,
377
			"autoUpdateInput": true,
378
			"locale": {
379
				'format': 'DD/MM/YYYY'
380
			}
381
	};
382
	if(showRanges) {
383
		rangedDatePicker['ranges'] = 
384
		{
385
            'Today': [moment(), moment()],
386
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
387
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
388
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
389
            'This Month': [moment().startOf('month'), moment().endOf('month')],
390
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
391
         }
392
	}
393
	return rangedDatePicker;
23892 amit.gupta 394
}
23946 amit.gupta 395
function showPosition(position) {
24052 amit.gupta 396
	if(typeof latitude == "undefined") {
397
		var coords = {latitude:position.coords.latitude, longitude:position.coords.longitude}
398
		doAjaxRequestWithJsonHandler('partner/location', 'PUT', JSON.stringify(coords), function(){
24016 amit.gupta 399
			latitude = position.coords.latitude;
400
			longitude = position.coords.longitude;
24052 amit.gupta 401
		});
402
	}
24176 amit.gupta 403
	// distance = getDistance(latitude, longitude, position.coords.latitude,
404
	// position.coords.longitude);
24168 amit.gupta 405
}
406
 
407
function getAuthorisedWarehouses(callback){
408
	bootBoxObj = { 
409
		size: "small",
410
		title: "Choose Warehouse", 
411
		callback: callback,
412
		inputType:'select',
413
		inputOptions : typeof inputOptions == "undefined" ? undefined : inputOptions
414
	}
415
	if(typeof inputOptions=="undefined") {
416
		doGetAjaxRequestHandler(context+"/authorisedWarehouses", function(response){
417
			response = JSON.parse(response);
418
			inputOptions = [];
419
			response.forEach(function(warehouse){
420
				inputOptions.push({
421
					text:warehouse.name,
422
					value:warehouse.id,
423
				});
424
			});
425
			bootBoxObj['inputOptions'] = inputOptions;
426
			bootbox.prompt(bootBoxObj);
427
		});
428
	} else if(inputOptions.length==1) {
429
		callback(inputOptions[0].warehouse.id);
430
	}
431
	else {
432
		bootbox.prompt(bootBoxObj);
433
	}
434
 
24176 amit.gupta 435
}
24349 amit.gupta 436
function getColorsForItems(catalogId, callback) {
437
	bootBoxObj = { 
438
			size: "small",
439
			title: "Choose Warehouse", 
440
			callback: callback,
441
			inputType:'checkbox',
442
			inputOptions : typeof inputOptions == "undefined" ? undefined : inputOptions
443
	}
444
	if(typeof inputOptions=="undefined") {
445
		doGetAjaxRequestHandler(context+"/authorisedWarehouses", function(response){
446
			response = JSON.parse(response);
447
			inputOptions = [];
448
			response.forEach(function(warehouse){
449
				inputOptions.push({
450
					text:warehouse.name,
451
					value:warehouse.id,
452
				});
453
			});
454
			bootBoxObj['inputOptions'] = inputOptions;
455
			bootbox.prompt(bootBoxObj);
456
		});
457
	} else if(inputOptions.length==1) {
458
		callback(inputOptions[0].warehouse.id);
459
	}
460
	else {
461
		bootbox.prompt(bootBoxObj);
462
	}
463
 
464
 
465
}
24176 amit.gupta 466
function getItemAheadOptions(jqElement, callback) {
467
	jqElement.typeahead('destroy').typeahead({
468
		source: function(q, process) {
24191 amit.gupta 469
			if(q.length <= 3) {
470
				if(typeof queryData != "undefined") {
471
					queryData = undefined;
472
				}
24176 amit.gupta 473
				return;
24191 amit.gupta 474
			} else if (q.length > 3 && typeof queryData == "undefined"){
24176 amit.gupta 475
				return $.ajax(context+"/item", {
476
					global:false,
477
					data: {query : q}, 
478
					success: function(data){
24191 amit.gupta 479
						queryData = JSON.parse(data);
480
						process(queryData);
24176 amit.gupta 481
					},
482
				});
24191 amit.gupta 483
			} else {
484
				return process(queryData);
24176 amit.gupta 485
			}
24191 amit.gupta 486
			return;
24176 amit.gupta 487
		},
488
		delay: 400,
489
		items: 20,
490
		displayText: function(item){return item.itemDescription;},
491
		autoSelect: true,
492
		afterSelect: callback
493
	});
494
}
24349 amit.gupta 495
function getPartnerAheadOptions(jqElement, callback) {
496
	jqElement.typeahead('destroy').typeahead({
497
		source: function(q, process) {
498
			if(q.length <= 3) {
499
				if(typeof queryData != "undefined") {
500
					queryData = undefined;
501
				}
502
				return;
503
			} else if (q.length > 3 && typeof queryData == "undefined"){
504
				return $.ajax(context+"/partners", {
505
					global:false,
506
					data: {query : q}, 
507
					success: function(data){
508
						queryData = JSON.parse(data);
509
						process(queryData);
510
					},
511
				});
512
			} else {
513
				return process(queryData);
514
			}
515
			return;
516
		},
517
		delay: 400,
518
		items: 20,
519
		displayText: function(partner){return partner.displayName;},
520
		autoSelect: true,
521
		afterSelect: callback
522
	});
523
}