Subversion Repositories SmartDukaan

Rev

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