Subversion Repositories SmartDukaan

Rev

Rev 23343 | Rev 23353 | 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
			}
72
             $.ajax({
73
                 type: "POST",
74
                 url: "create-order",
75
                 data: payload,
76
                 contentType:'application/json',
77
				async: false,
78
				success: function (data) {
79
					emptyBag();
80
					$('#main-content').html(data);
81
				},
82
				cache: false,
83
				processData: false
84
             });
85
             return false; // required to block normal submit since you used ajax
86
         }
87
	});
88
 
23340 ashik.ali 89
	$("input.unitPrice").blur(function() {
22245 ashik.ali 90
		console.log("unitPrice blur called");
91
		var $element = $(this);
92
		var unitPrice = $element.val();
22354 ashik.ali 93
		var mopPrice = parseFloat($(this).attr('mopPrice'));
94
		if(parseFloat(unitPrice) < mopPrice){
95
			//error = true;
96
			alert("sellingPrice must be greater than equal to mop")
97
			$element.addClass("border-highlight");
98
		}else{
99
			$element.removeClass("border-highlight")
22660 ashik.ali 100
			if(unitPrice == mopPrice){
101
				$("input.discountAmount").removeAttr("readonly");
102
			}
22354 ashik.ali 103
			if(unitPrice != ''){
104
			    $.ajax({
105
			         type: "GET",
106
			         url: "insurancePrices?price="+unitPrice,
107
			         //data: payload,
108
			         //contentType:'application/json',
109
					async: false,
110
					success: function (data) {
111
						console.log("response : "+JSON.stringify(data));
112
						var insurancePriceMap = data.response;
113
						for(key in insurancePriceMap){
114
							var dealerPrice = insurancePriceMap[key].dealerPrice;
115
							var sellingPrice = insurancePriceMap[key].sellingPrice;
116
							$element.closest('tr').find('.insuranceAmount').attr("placeholder", dealerPrice + "-" + sellingPrice);
117
							break;
118
						}
119
					},
120
					error : function() {
121
						alert("OOPS!!!Failed to do changes.Try Again.",'ERROR');
122
					},
123
					cache: false,
124
					processData: false
125
			    });
126
		    }
127
		}
22245 ashik.ali 128
	}).focus(function(){
129
		console.log("unitPrice focus called");
130
		var $element = $(this);
131
		$element.closest('tr').find('.insuranceAmount').attr("placeholder", "");
132
	});
22354 ashik.ali 133
 
134
	$( "input.phone").blur(function() {
135
		console.log("phone blur called");
136
		var mobileNumber = $(this).val();
137
		writeOldCustomerDetailsByMobileNumber(mobileNumber);
138
	});
22245 ashik.ali 139
 
23343 ashik.ali 140
});
141
 
142
 
143
function validateOrderDetails(){
144
	var sNumbers = [];
145
	var error = false;
146
	$("form#cd input.serialNumber").each(function(){
147
		var input = $(this).val().trim();
148
		var itemType = $(this).attr("itemType");
149
		$(this).removeClass("border-highlight");
150
		if (sNumbers.indexOf(input) !=-1 || (itemType ==='SERIALIZED' && !input)){
151
			error = true;
152
			$(this).addClass("border-highlight");
153
		}
154
		sNumbers.push(input);
155
	});
156
 
157
	$("form#cd input.unitPrice").each(function(){
158
		var input = $(this).val().trim();
159
		$(this).removeClass("border-highlight");
160
		if (!input || parseInt(input)<=0 || isNaN(input)){
161
			error=true;
162
			$(this).addClass("border-highlight");
163
		}
164
		var unitPrice = parseFloat(input);
165
		var mopPrice = parseFloat($(this).attr('mopPrice'));
166
		if(unitPrice < mopPrice){
167
			error = true;
168
			alert("sellingPrice must be greater than equal to mop")
169
			$(this).addClass("border-highlight");
170
		}
171
	});
172
 
173
	// checking input discountAmount is not greater than maxDiscountAmount
174
	$("form#cd input.discountAmount").each(function(){
175
		var input = $(this).val().trim();
176
		var mop = $(this).attr("mop");
177
 
178
		if(mop){
179
			var maxDiscountPriceRangeString = $(this).attr("placeholder");
180
			var maxDiscountPrice = 0;
181
			if(maxDiscountPriceRangeString != undefined && maxDiscountPriceRangeString != ''){
182
				maxDiscountPrice = maxDiscountPriceRangeString.substring(6);
183
			}
184
			$(this).removeClass("border-highlight");
185
			if (!input || parseFloat(input)<0 || isNaN(input)){
186
				alert("DiscountAmount value can not be lesser than 0 or invalid value");
187
				error=true;
188
				$(this).addClass("border-highlight");
189
			}
190
			if(parseFloat(input) > maxDiscountPrice){
191
				alert("DiscountAmount [" + parseFloat(input) + "] value can not be greater than the suggested maxDiscountPrice [" + maxDiscountPrice + "]");
192
				error=true;
193
				$(this).addClass("border-highlight");
194
			}
195
		}
196
	});
197
 
198
	var amount = 0;
199
	var netPayableAmount = parseFloat($("form#cd input.netPayableAmount").val());
200
 
201
 
202
	$("form#cd input.amount").each(function(){
203
		if ($(this).val() == ""){
204
			$(this).val(0);
205
		}
206
		var tmpAmount = parseFloat($(this).val());
207
		amount = amount + tmpAmount;
208
	});
209
 
210
	console.log(amount);
211
	console.log(netPayableAmount);
212
 
213
	if (amount != netPayableAmount){
214
		if(amount < netPayableAmount){
215
			alert("[" + (netPayableAmount - amount) + "] is more required to complete the payment");
216
		}else{
217
			alert("[" + (amount - netPayableAmount) + "] is extra amount, please reduce the amount");
218
		}
219
		$("form#cd input.netPayableAmount").each(function(){
220
			$(this).addClass("border-highlight");
221
		});
222
		error = true;
223
	}
224
 
225
	if (error){
226
		return false;
227
	}
228
	return true;
229
 
23347 ashik.ali 230
}
231
 
232
 
233
function getSerialNumbersFromOrder($el){
234
	var $serialNumberElement = $el.find('.serialNumber');
235
	if($serialNumberElement.val() == ''){
236
		return null;
237
	}
238
	var insuranceAmount = parseFloat($el.find('.insuranceAmount').val());
239
	if(insuranceAmount > 0){
240
		insurance = true;
241
		globalInsurace = true;
242
	}else{
243
		insurance = false;
244
	}
245
	return {'serialNumber':$serialNumberElement.val(),'insurance':insurance,'amount':insuranceAmount}
246
}
247
 
248
function orderDetailsPayload(){
249
		var orderObj = {};
250
		var priceQtyArray = [];
251
		var customerObj = {};
252
		var paymentOption = [];
253
		var globalInsurance = false;
254
 
255
		$("form#cd input.serialNumber").each(function(){
256
			var itemId = parseInt($(this).attr("itemId"));
257
			if (orderObj.hasOwnProperty('fofoOrderItems')){
258
				var itemDetails = orderObj['fofoOrderItems'];
259
				if (itemDetails.hasOwnProperty(itemId)){
260
					var serialNumbers = itemDetails[itemId];
261
					serialNumbers.push($(this).val());
262
					itemDetails[itemId] = serialNumbers;
263
				}else{
264
					var serialNumbers = [];
265
					serialNumbers.push($(this).val());
266
					itemDetails[itemId] = serialNumbers;
267
				}
268
			}else{
269
				var serialNumbers = [];
270
				serialNumbers.push($(this).val());
271
				var tmp ={};
272
				tmp[itemId]  = serialNumbers;
273
				orderObj['fofoOrderItems'] = tmp;
274
			}
275
		});
276
		console.log( JSON.stringify(orderObj));
277
		$("#order-details").find("tr:not(:first-child)").each(function(index, el){
278
			//console.log(el);
279
			//console.log(index);
280
			var $el = $(el);
281
			var $unitPriceElement = $el.find('.unitPrice');
282
			var $discountAmountElement = $el.find('.discountAmount');
283
 
284
			var itemId = parseInt($unitPriceElement.attr("itemId"));
285
			var unitPrice = parseFloat($unitPriceElement.val());
286
			var qty = parseInt($unitPriceElement.attr("quantity"));
287
			var discountAmount = parseFloat($discountAmountElement.val());
288
			var mop = $discountAmountElement.attr("mop");
289
			var tmpObj = {'itemId':itemId,'sellingPrice':unitPrice,'quantity':qty};
290
			tmpObj.discountAmount = discountAmount;
291
			/*tmpObj['serialNumberDetails'] = [];
292
			var found = false;
293
			for(var i = 0; i < priceQtyArray.length; i++){			
294
				if (priceQtyArray[i]["itemId"] == itemId){
295
					found = true;
296
					break;
297
				}
298
			}
299
			if(!found){
300
				priceQtyArray.push(tmpObj);
301
			}else{
302
				for(var i = 0; i < priceQtyArray.length; i++){			
303
					if (priceQtyArray[i]["itemId"] == itemId){
304
						priceQtyArray[i]["quantity"] = priceQtyArray[i]["quantity"] + 1;
305
						break;
306
					}
307
				}
308
			}
309
			var serialNumberDetails = getSerialNumbersFromOrder($el);
310
			if(serialNumberDetails != null){
311
				for(var i = 0; i < priceQtyArray.length; i++){			
312
					if (priceQtyArray[i]["itemId"] == itemId){
313
						priceQtyArray[i]["serialNumberDetails"].push(serialNumberDetails);
314
						break;
315
					}
316
				}
317
			}
318
		});*/
319
 
320
			if (orderObj['fofoOrderItems'][itemId] === undefined){
321
				tmpObj['serialNumberDetails'] =[];
322
			}
323
			else{
324
				//tmpObj['serialNumbers'] = orderObj['fofoLineItems'][itemId];
325
				tmpObj['serialNumberDetails'] = [];
326
			}
327
			var found = false;
328
			for(var i = 0; i < priceQtyArray.length; i++){			
329
				if (priceQtyArray[i]["itemId"] == itemId){
330
					priceQtyArray[i]["quantity"] = priceQtyArray[i]["quantity"] + 1;
331
					found = true;
332
					break;
333
				}
334
			}
335
			if(!found){
336
				priceQtyArray.push(tmpObj);
337
			}
338
		});
339
 
340
 
341
		$("#order-details").find("tr:not(:first-child)").each(function(index,el){
342
			//console.log(el);
343
			//console.log(index);
344
			var $serialNumberElement = $(el).find('.serialNumber');
345
			var itemId = parseInt($serialNumberElement.attr("itemId"));
346
			var insuranceAmount = parseFloat($(el).find('.insuranceAmount').val());
347
			//if (priceQtyArray['fofoLineItems'][itemId] !=undefined){
348
 
349
			//}
350
			//console.log($serialNumberElement.val());
351
			//console.log(itemId);
352
			for(var i = 0; i < priceQtyArray.length; i++){
353
				console.log(priceQtyArray[i]["itemId"]);
354
				if (priceQtyArray[i]["itemId"] == itemId){
355
					if(insuranceAmount > 0){
356
						insurance = true;
357
						globalInsurance = true;
358
					}else{
359
						insurance = false;
360
					}
361
					var serialNumberDetails = {'serialNumber':$serialNumberElement.val(),'insurance':insurance,'amount':insuranceAmount}
362
					priceQtyArray[i]['serialNumberDetails'].push(serialNumberDetails);
363
				}
364
            }
365
		});
366
 
367
		console.log("priceQtyArray : "+JSON.stringify(priceQtyArray));
368
		customerObj['firstName'] = $("form#cd input[name=firstName]").val();
369
		customerObj['lastName'] = $("form#cd input[name=lastName]").val();
370
		customerObj['mobileNumber'] = $("form#cd input[name=phone]").val(); 
371
		customerObj['emailId'] = $("form#cd input[name=email]").val();
372
		customerObj['dateOfBirth'] = $("form#cd input[name=dateOfBirth]").val();
373
		var customerAddress = {};
374
		customerAddress['name'] = $("form#cd input[name=firstName]").val() + " " + $("form#cd input[name=lastName]").val();
375
		customerAddress['line1'] = $("form#cd input[name=line1]").val();
376
		customerAddress['line2'] = $("form#cd input[name=line2]").val();
377
		customerAddress['landmark'] = $("form#cd input[name=landmark]").val();
378
		customerAddress['city'] = $("form#cd input[name=city]").val();
379
		customerAddress['state'] = $('select[name=state] option:selected').val();
380
		customerAddress['pinCode'] = $("form#cd input[name=pinCode]").val();
381
		customerAddress['phoneNumber'] = $("form#cd input[name=alternatePhone]").val();
382
		customerAddress['country'] = "India";
383
		customerObj['customerAddressId'] = parseInt($("form#cd input[name=phone]").attr("addressId"));
384
		customerObj['address'] = customerAddress; 
385
		console.log( JSON.stringify(customerObj));
386
		$("#payment-details input").each(function(){
387
		console.log($(this).attr('name'));
388
		if ($(this).attr('name') =="" ){
389
			return;
390
		}
391
		var paymentObj = {};
392
		paymentObj['type'] = $(this).attr('name');
393
		if ($(this).val() == ""){
394
			paymentObj['amount'] = 0.0;
395
		}
396
		else{
397
			paymentObj['amount'] = parseFloat($(this).val());
398
		}
399
		paymentOption.push(paymentObj);
400
		});
401
		console.log( JSON.stringify(paymentOption));
402
 
403
		var retObj = {};
404
		var dateOfBirth = $("form#cd input[name=dateOfBirth]").val();
405
		if(globalInsurance){
406
			retObj['customerDateOfBirth'] = (dateOfBirth);
407
		}
408
		retObj['fofoOrderItems'] = (priceQtyArray);
409
		retObj['customer'] = (customerObj);
410
		retObj['paymentOptions'] =  (paymentOption);
411
 
412
		console.log(retObj);
413
		return  JSON.stringify(retObj);
414
}
415
 
416
 
417
function writeOldCustomerDetailsByMobileNumber(mobileNumber){
418
	var response = doGetAjaxRequest(context+"/customer/mobileNumber?mobileNumber="+mobileNumber);
419
 
420
	var customer = response.response;
421
    $('#firstName').attr('value', customer.firstName);
422
    $('#lastName').attr('value', customer.lastName);
423
    $('#email').attr('value', customer.emailId);
424
    //$('#phone').attr('value', customer.mobileNumber);
425
    $('#alternatePhone').attr('value', customer.address.phoneNumber);
426
    $('#line1').attr('value', customer.address.line1);
427
    $('#line2').attr('value', customer.address.line2);
428
    $('#landmark').attr('value', customer.address.landmark);
429
    $('#pinCode').attr('value', customer.address.pinCode);
430
    $('#city').attr('value', customer.address.city);
431
    $('#state').attr('value', customer.address.state).prop('selected',true);
432
    //$('#state').val(customer.address.state).prop('selected', true);
433
    $('#phone').attr('addressId', customer.address.id);
23343 ashik.ali 434
}