Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
23343 ashik.ali 1
$(function() {
2
 
3
	$(".create-order").live('click', function() {
4
		checkout("main-content");
5
	});
6
 
7
	$("form#cd input.discountAmount").live('change paste keyup mouseup', function() {
8
		var discountAmount = parseFloat($(this).val());
9
		if (isNaN(discountAmount)){
10
			discountAmount = 0;
11
		}
12
		if(discountAmount < 0){
13
			alert("Invalid insurance Value");
14
			$(this).val(0);
15
		}
16
		calculateTotalAmount();
17
	});
18
 
19
	$("form#cd input.unitPrice").live('change paste keyup mouseup', function() {
20
 
21
		var unitPrice = parseFloat($(this).val());
22
		if (isNaN(unitPrice)){
23
			unitPrice = 0;
24
		}
25
		if(unitPrice < 0){
26
			alert("Invalid unit price");
27
			$(this).val(0);
28
		}
29
 
30
		calculateTotalAmount();
31
	});
32
 
33
 
34
	$("form#cd input.insuranceAmount").live('change paste keyup mouseup', function() {
35
		calculateTotalAmount();
36
	});
37
 
38
});
39
 
22245 ashik.ali 40
$().ready(function() {
41
	$("form#cd input").each(function(){
42
		$(this).attr('autocomplete', 'off');
43
	});
44
});
45
$().ready(function() {
46
        // validate the comment form when it is submitted
47
        $('#cd').validate({
48
		rules:{
49
			name:{
50
				required:true
51
			},
52
			email:{
53
				required:true,
54
				email:true
55
			},
56
			line1:{
57
				required:true
58
			},
59
			state:{
60
				required:true
61
			},
62
			city:{
63
				required:true
64
			},
65
			pincode:{
66
				required:true,
67
				digits:true,
68
				minlength: 6,
69
                maxlength: 6,
70
			},
71
			phone:{
72
				required:true,
73
				minlength:10,
74
				maxlength:10,
75
				digits:true
76
			},
77
		},
78
		messages:{
79
			name:{
80
				required:"Please enter the name"
81
			},
82
			line1:{
83
				required:"Please enter the address"
84
			},
85
			state:{
86
				required: "Please select a state"
87
			},
88
			city:{
89
				required: "Please enter the city"
90
			},
91
			email:{
92
				require: "Please enter a valid email address"
93
			},
94
			pincode:{
95
				required: "Please enter the pincode",
96
				digits:"Please enter a valid pincode"
97
			},
98
			phone:{
99
				required: "Please enter the phone number",
100
				digits:"Please enter a valid number",
101
				minlength:"Number should be of 10 digits"
102
			}
103
		},
104
		submitHandler: function (form, event) {
105
			event.preventDefault();
106
			var payload = orderDetailsPayload();
107
			if(!validateOrderDetails()){
108
				alert("Please fix highlighted errors");
109
				return false;
110
			}
111
             $.ajax({
112
                 type: "POST",
113
                 url: "create-order",
114
                 data: payload,
115
                 contentType:'application/json',
116
				async: false,
117
				success: function (data) {
118
					emptyBag();
119
					$('#main-content').html(data);
120
				},
121
				cache: false,
122
				processData: false
123
             });
124
             return false; // required to block normal submit since you used ajax
125
         }
126
	});
127
 
23340 ashik.ali 128
	$("input.unitPrice").blur(function() {
22245 ashik.ali 129
		console.log("unitPrice blur called");
130
		var $element = $(this);
131
		var unitPrice = $element.val();
22354 ashik.ali 132
		var mopPrice = parseFloat($(this).attr('mopPrice'));
133
		if(parseFloat(unitPrice) < mopPrice){
134
			//error = true;
135
			alert("sellingPrice must be greater than equal to mop")
136
			$element.addClass("border-highlight");
137
		}else{
138
			$element.removeClass("border-highlight")
22660 ashik.ali 139
			if(unitPrice == mopPrice){
140
				$("input.discountAmount").removeAttr("readonly");
141
			}
22354 ashik.ali 142
			if(unitPrice != ''){
143
			    $.ajax({
144
			         type: "GET",
145
			         url: "insurancePrices?price="+unitPrice,
146
			         //data: payload,
147
			         //contentType:'application/json',
148
					async: false,
149
					success: function (data) {
150
						console.log("response : "+JSON.stringify(data));
151
						var insurancePriceMap = data.response;
152
						for(key in insurancePriceMap){
153
							var dealerPrice = insurancePriceMap[key].dealerPrice;
154
							var sellingPrice = insurancePriceMap[key].sellingPrice;
155
							$element.closest('tr').find('.insuranceAmount').attr("placeholder", dealerPrice + "-" + sellingPrice);
156
							break;
157
						}
158
					},
159
					error : function() {
160
						alert("OOPS!!!Failed to do changes.Try Again.",'ERROR');
161
					},
162
					cache: false,
163
					processData: false
164
			    });
165
		    }
166
		}
22245 ashik.ali 167
	}).focus(function(){
168
		console.log("unitPrice focus called");
169
		var $element = $(this);
170
		$element.closest('tr').find('.insuranceAmount').attr("placeholder", "");
171
	});
22354 ashik.ali 172
 
173
	$( "input.phone").blur(function() {
174
		console.log("phone blur called");
175
		var mobileNumber = $(this).val();
176
		writeOldCustomerDetailsByMobileNumber(mobileNumber);
177
	});
22245 ashik.ali 178
 
23343 ashik.ali 179
});
180
 
181
 
182
function orderDetailsPayload(){
183
	var orderObj = {};
184
	var priceQtyArray = [];
185
	var customerObj = {};
186
	var paymentOption = [];
187
	var globalInsurance = false;
188
 
189
	$("form#cd input.serialNumber").each(function(){
190
		var itemId = parseInt($(this).attr("itemId"));
191
		if (orderObj.hasOwnProperty('fofoOrderItems')){
192
			var itemDetails = orderObj['fofoOrderItems'];
193
			if (itemDetails.hasOwnProperty(itemId)){
194
				var serialNumbers = itemDetails[itemId];
195
				serialNumbers.push($(this).val());
196
				itemDetails[itemId] = serialNumbers;
197
			}else{
198
				var serialNumbers = [];
199
				serialNumbers.push($(this).val());
200
				itemDetails[itemId] = serialNumbers;
201
			}
202
		}else{
203
			var serialNumbers = [];
204
			serialNumbers.push($(this).val());
205
			var tmp ={};
206
			tmp[itemId]  = serialNumbers;
207
			orderObj['fofoOrderItems'] = tmp;
208
		}
209
	});
210
	console.log( JSON.stringify(orderObj));
211
	$("#order-details").find("tr:not(:first-child)").each(function(index, el){
212
		//console.log(el);
213
		//console.log(index);
214
		var $el = $(el);
215
		var $unitPriceElement = $el.find('.unitPrice');
216
		var $discountAmountElement = $el.find('.discountAmount');
217
 
218
		var itemId = parseInt($unitPriceElement.attr("itemId"));
219
		var unitPrice = parseFloat($unitPriceElement.val());
220
		var qty = parseInt($unitPriceElement.attr("quantity"));
221
		var discountAmount = parseFloat($discountAmountElement.val());
222
		var mop = $discountAmountElement.attr("mop");
223
		var tmpObj = {'itemId':itemId,'sellingPrice':unitPrice,'quantity':qty};
224
		tmpObj.discountAmount = discountAmount;
225
		/*tmpObj['serialNumberDetails'] = [];
226
		var found = false;
227
		for(var i = 0; i < priceQtyArray.length; i++){			
228
			if (priceQtyArray[i]["itemId"] == itemId){
229
				found = true;
230
				break;
231
			}
232
		}
233
		if(!found){
234
			priceQtyArray.push(tmpObj);
235
		}else{
236
			for(var i = 0; i < priceQtyArray.length; i++){			
237
				if (priceQtyArray[i]["itemId"] == itemId){
238
					priceQtyArray[i]["quantity"] = priceQtyArray[i]["quantity"] + 1;
239
					break;
240
				}
241
			}
242
		}
243
		var serialNumberDetails = getSerialNumbersFromOrder($el);
244
		if(serialNumberDetails != null){
245
			for(var i = 0; i < priceQtyArray.length; i++){			
246
				if (priceQtyArray[i]["itemId"] == itemId){
247
					priceQtyArray[i]["serialNumberDetails"].push(serialNumberDetails);
248
					break;
249
				}
250
			}
251
		}
252
	});*/
253
 
254
		if (orderObj['fofoOrderItems'][itemId] === undefined){
255
			tmpObj['serialNumberDetails'] =[];
256
		}
257
		else{
258
			//tmpObj['serialNumbers'] = orderObj['fofoLineItems'][itemId];
259
			tmpObj['serialNumberDetails'] = [];
260
		}
261
		var found = false;
262
		for(var i = 0; i < priceQtyArray.length; i++){			
263
			if (priceQtyArray[i]["itemId"] == itemId){
264
				priceQtyArray[i]["quantity"] = priceQtyArray[i]["quantity"] + 1;
265
				found = true;
266
				break;
267
			}
268
		}
269
		if(!found){
270
			priceQtyArray.push(tmpObj);
271
		}
272
	});
273
 
274
 
275
	$("#order-details").find("tr:not(:first-child)").each(function(index,el){
276
		//console.log(el);
277
		//console.log(index);
278
		var $serialNumberElement = $(el).find('.serialNumber');
279
		var itemId = parseInt($serialNumberElement.attr("itemId"));
280
		var insuranceAmount = parseFloat($(el).find('.insuranceAmount').val());
281
		//if (priceQtyArray['fofoLineItems'][itemId] !=undefined){
282
 
283
		//}
284
		//console.log($serialNumberElement.val());
285
		//console.log(itemId);
286
		for(var i = 0; i < priceQtyArray.length; i++){
287
			console.log(priceQtyArray[i]["itemId"]);
288
			if (priceQtyArray[i]["itemId"] == itemId){
289
				if(insuranceAmount > 0){
290
					insurance = true;
291
					globalInsurance = true;
292
				}else{
293
					insurance = false;
294
				}
295
				var serialNumberDetails = {'serialNumber':$serialNumberElement.val(),'insurance':insurance,'amount':insuranceAmount}
296
				priceQtyArray[i]['serialNumberDetails'].push(serialNumberDetails);
297
			}
298
        }
299
	});
300
 
301
	console.log("priceQtyArray : "+JSON.stringify(priceQtyArray));
302
	customerObj['firstName'] = $("form#cd input[name=firstName]").val();
303
	customerObj['lastName'] = $("form#cd input[name=lastName]").val();
304
	customerObj['mobileNumber'] = $("form#cd input[name=phone]").val(); 
305
	customerObj['emailId'] = $("form#cd input[name=email]").val();
306
	customerObj['dateOfBirth'] = $("form#cd input[name=dateOfBirth]").val();
307
	var customerAddress = {};
308
	customerAddress['name'] = $("form#cd input[name=firstName]").val() + " " + $("form#cd input[name=lastName]").val();
309
	customerAddress['line1'] = $("form#cd input[name=line1]").val();
310
	customerAddress['line2'] = $("form#cd input[name=line2]").val();
311
	customerAddress['landmark'] = $("form#cd input[name=landmark]").val();
312
	customerAddress['city'] = $("form#cd input[name=city]").val();
313
	customerAddress['state'] = $('select[name=state] option:selected').val();
314
	customerAddress['pinCode'] = $("form#cd input[name=pinCode]").val();
315
	customerAddress['phoneNumber'] = $("form#cd input[name=alternatePhone]").val();
316
	customerAddress['country'] = "India";
317
	customerObj['customerAddressId'] = parseInt($("form#cd input[name=phone]").attr("addressId"));
318
	customerObj['address'] = customerAddress; 
319
	console.log( JSON.stringify(customerObj));
320
	$("#payment-details input").each(function(){
321
	console.log($(this).attr('name'));
322
	if ($(this).attr('name') =="" ){
323
		return;
324
	}
325
	var paymentObj = {};
326
	paymentObj['type'] = $(this).attr('name');
327
	if ($(this).val() == ""){
328
		paymentObj['amount'] = 0.0;
329
	}
330
	else{
331
		paymentObj['amount'] = parseFloat($(this).val());
332
	}
333
	paymentOption.push(paymentObj);
334
	});
335
	console.log( JSON.stringify(paymentOption));
336
 
337
	var retObj = {};
338
	var dateOfBirth = $("form#cd input[name=dateOfBirth]").val();
339
	if(globalInsurance){
340
		retObj['customerDateOfBirth'] = (dateOfBirth);
341
	}
342
	retObj['fofoOrderItems'] = (priceQtyArray);
343
	retObj['customer'] = (customerObj);
344
	retObj['paymentOptions'] =  (paymentOption);
345
 
346
	console.log(retObj);
347
	return  JSON.stringify(retObj);
348
}
349
 
350
function getSerialNumbersFromOrder($el){
351
	var $serialNumberElement = $el.find('.serialNumber');
352
	if($serialNumberElement.val() == ''){
353
		return null;
354
	}
355
	var insuranceAmount = parseFloat($el.find('.insuranceAmount').val());
356
	if(insuranceAmount > 0){
357
		insurance = true;
358
		globalInsurace = true;
359
	}else{
360
		insurance = false;
361
	}
362
	return {'serialNumber':$serialNumberElement.val(),'insurance':insurance,'amount':insuranceAmount}
363
}
364
 
365
 
366
function calculateTotalAmount() {
367
	var netPayableAmount = 0;
368
	$("#cd").find('tr:gt(0)').each(function(index, el){
369
		var rowTotal = 0;
370
		$tr = $(el);
371
 
372
		var insuranceAmount = $tr.find('.insuranceAmount').val();
373
		if (!insuranceAmount){
374
			insuranceAmount = 0;
375
		}else{
376
			insuranceAmount = parseFloat(insuranceAmount);
377
		}
378
 
379
		var qty = parseFloat($(el).find('.unitPrice').attr('quantity'));
380
		var unitPrice = parseFloat($(el).find('.unitPrice').val());
381
 
382
		var discountAmount =  parseFloat($(el).find('.discountAmount').val());
383
		if(isNaN(discountAmount)){
384
			discountAmount = 0;
385
		}
386
 
387
		rowTotal = unitPrice*qty + insuranceAmount - discountAmount; 
388
		$tr.find('.totalPrice').val(rowTotal);
389
		netPayableAmount += rowTotal;
390
	});
391
	$('#cd').find('input.netPayableAmount').val(netPayableAmount);
392
}
393
 
394
 
395
function writeOldCustomerDetailsByMobileNumber(mobileNumber){
396
	doAjaxRequestHandler(context+"/customer/mobileNumber?mobileNumber="+mobileNumber, "GET", function(response){
397
		var customer = response.response;
398
	    $('#firstName').attr('value', customer.firstName);
399
	    $('#lastName').attr('value', customer.lastName);
400
	    $('#email').attr('value', customer.emailId);
401
	    //$('#phone').attr('value', customer.mobileNumber);
402
	    $('#alternatePhone').attr('value', customer.address.phoneNumber);
403
	    $('#line1').attr('value', customer.address.line1);
404
	    $('#line2').attr('value', customer.address.line2);
405
	    $('#landmark').attr('value', customer.address.landmark);
406
	    $('#pinCode').attr('value', customer.address.pinCode);
407
	    $('#city').attr('value', customer.address.city);
408
	    $('#state').attr('value', customer.address.state).prop('selected',true);
409
	    //$('#state').val(customer.address.state).prop('selected', true);
410
	    $('#phone').attr('addressId', customer.address.id);
411
	});
412
 
413
}
414
 
415
 
416
function validateOrderDetails(){
417
	var sNumbers = [];
418
	var error = false;
419
	$("form#cd input.serialNumber").each(function(){
420
		var input = $(this).val().trim();
421
		var itemType = $(this).attr("itemType");
422
		$(this).removeClass("border-highlight");
423
		if (sNumbers.indexOf(input) !=-1 || (itemType ==='SERIALIZED' && !input)){
424
			error = true;
425
			$(this).addClass("border-highlight");
426
		}
427
		sNumbers.push(input);
428
	});
429
 
430
	$("form#cd input.unitPrice").each(function(){
431
		var input = $(this).val().trim();
432
		$(this).removeClass("border-highlight");
433
		if (!input || parseInt(input)<=0 || isNaN(input)){
434
			error=true;
435
			$(this).addClass("border-highlight");
436
		}
437
		var unitPrice = parseFloat(input);
438
		var mopPrice = parseFloat($(this).attr('mopPrice'));
439
		if(unitPrice < mopPrice){
440
			error = true;
441
			alert("sellingPrice must be greater than equal to mop")
442
			$(this).addClass("border-highlight");
443
		}
444
	});
445
 
446
	// checking input discountAmount is not greater than maxDiscountAmount
447
	$("form#cd input.discountAmount").each(function(){
448
		var input = $(this).val().trim();
449
		var mop = $(this).attr("mop");
450
 
451
		if(mop){
452
			var maxDiscountPriceRangeString = $(this).attr("placeholder");
453
			var maxDiscountPrice = 0;
454
			if(maxDiscountPriceRangeString != undefined && maxDiscountPriceRangeString != ''){
455
				maxDiscountPrice = maxDiscountPriceRangeString.substring(6);
456
			}
457
			$(this).removeClass("border-highlight");
458
			if (!input || parseFloat(input)<0 || isNaN(input)){
459
				alert("DiscountAmount value can not be lesser than 0 or invalid value");
460
				error=true;
461
				$(this).addClass("border-highlight");
462
			}
463
			if(parseFloat(input) > maxDiscountPrice){
464
				alert("DiscountAmount [" + parseFloat(input) + "] value can not be greater than the suggested maxDiscountPrice [" + maxDiscountPrice + "]");
465
				error=true;
466
				$(this).addClass("border-highlight");
467
			}
468
		}
469
	});
470
 
471
	var amount = 0;
472
	var netPayableAmount = parseFloat($("form#cd input.netPayableAmount").val());
473
 
474
 
475
	$("form#cd input.amount").each(function(){
476
		if ($(this).val() == ""){
477
			$(this).val(0);
478
		}
479
		var tmpAmount = parseFloat($(this).val());
480
		amount = amount + tmpAmount;
481
	});
482
 
483
	console.log(amount);
484
	console.log(netPayableAmount);
485
 
486
	if (amount != netPayableAmount){
487
		if(amount < netPayableAmount){
488
			alert("[" + (netPayableAmount - amount) + "] is more required to complete the payment");
489
		}else{
490
			alert("[" + (amount - netPayableAmount) + "] is extra amount, please reduce the amount");
491
		}
492
		$("form#cd input.netPayableAmount").each(function(){
493
			$(this).addClass("border-highlight");
494
		});
495
		error = true;
496
	}
497
 
498
	if (error){
499
		return false;
500
	}
501
	return true;
502
 
503
}