Subversion Repositories SmartDukaan

Rev

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