Subversion Repositories SmartDukaan

Rev

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