Subversion Repositories SmartDukaan

Rev

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

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