Subversion Repositories SmartDukaan

Rev

Rev 22279 | Rev 22284 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22279 Rev 22280
Line 358... Line 358...
358
			fofoDetails = cookiesProcessor.getCookiesObject(request);
358
			fofoDetails = cookiesProcessor.getCookiesObject(request);
359
		} catch (ProfitMandiBusinessException e) {
359
		} catch (ProfitMandiBusinessException e) {
360
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
360
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
361
			return "response";
361
			return "response";
362
		}
362
		}
363
		try{
-
 
364
			Set<Integer> itemIds = new HashSet<>();
363
		Set<Integer> itemIds = new HashSet<>();
365
			Map<Integer, Integer> itemIdQuantity = new HashMap<>(); //this is for error
364
		Map<Integer, Integer> itemIdQuantity = new HashMap<>(); //this is for error
366
			Map<Integer, CustomFofoLineItem> customFofoLineItemMap = new HashMap<>();
365
		Map<Integer, CustomFofoLineItem> customFofoLineItemMap = new HashMap<>();
367
			Map<Integer, Float> lineItemPrice = new HashMap<>(); //this is for pricing error
366
		Map<Integer, Float> lineItemPrice = new HashMap<>(); //this is for pricing error
368
			float totalAmount = 0;
367
		float totalAmount = 0;
369
			for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
368
		for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
370
				itemIds.add(customFofoLineItem.getItemId());
369
			itemIds.add(customFofoLineItem.getItemId());
371
				if(!customFofoLineItem.getSerialNumberDetails().isEmpty() && customFofoLineItem.getQuantity() != customFofoLineItem.getSerialNumberDetails().size()){
370
			if(!customFofoLineItem.getSerialNumberDetails().isEmpty() && customFofoLineItem.getQuantity() != customFofoLineItem.getSerialNumberDetails().size()){
372
					itemIdQuantity.put(customFofoLineItem.getItemId(), customFofoLineItem.getQuantity());
371
				itemIdQuantity.put(customFofoLineItem.getItemId(), customFofoLineItem.getQuantity());
373
				}
372
			}
374
				if(!(customFofoLineItem.getSellingPrice() > 0)){
373
			if(!(customFofoLineItem.getSellingPrice() > 0)){
375
					lineItemPrice.put(customFofoLineItem.getItemId(), customFofoLineItem.getSellingPrice());
374
				lineItemPrice.put(customFofoLineItem.getItemId(), customFofoLineItem.getSellingPrice());
376
				}else{
375
			}else{
377
					totalAmount = totalAmount + customFofoLineItem.getSellingPrice() * customFofoLineItem.getQuantity();
376
				totalAmount = totalAmount + customFofoLineItem.getSellingPrice() * customFofoLineItem.getQuantity();
378
					for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){
377
				for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){
379
						if(serialNumberDetail.isInsurance() && serialNumberDetail.getAmount() > 0){
378
					if(serialNumberDetail.isInsurance() && serialNumberDetail.getAmount() > 0){
380
							totalAmount = totalAmount + serialNumberDetail.getAmount();
379
						totalAmount = totalAmount + serialNumberDetail.getAmount();
381
						}
-
 
382
					}
380
					}
383
				}
381
				}
384
				customFofoLineItemMap.put(customFofoLineItem.getItemId(), customFofoLineItem);
-
 
385
			}
-
 
386
			if(!itemIdQuantity.isEmpty()){
-
 
387
				// if item quantity does not match with given serialnumbers size
-
 
388
				LOGGER.error("itemId's quantity should be equal to given serialnumber size {} ", itemIdQuantity);
-
 
389
				throw new ProfitMandiBusinessException("itemIdQuantity", itemIdQuantity, "");
-
 
390
				//return "error";
-
 
391
			}
382
			}
-
 
383
			customFofoLineItemMap.put(customFofoLineItem.getItemId(), customFofoLineItem);
-
 
384
		}
-
 
385
		if(!itemIdQuantity.isEmpty()){
-
 
386
			// if item quantity does not match with given serialnumbers size
-
 
387
			LOGGER.error("itemId's quantity should be equal to given serialnumber size {} ", itemIdQuantity);
-
 
388
			throw new ProfitMandiBusinessException("itemIdQuantity", itemIdQuantity, "");
-
 
389
			//return "error";
-
 
390
		}
392
			try{
391
		try{
393
				this.validatePaymentOptionsAndTotalAmount(createOrderRequest.getPaymentOptions(), totalAmount);
392
			this.validatePaymentOptionsAndTotalAmount(createOrderRequest.getPaymentOptions(), totalAmount);
394
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
393
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
395
				LOGGER.error("Error occured while validating payment options : ", profitMandiBusinessException);
394
			LOGGER.error("Error occured while validating payment options : ", profitMandiBusinessException);
396
				throw profitMandiBusinessException;
395
			throw profitMandiBusinessException;
397
			}
396
		}
398
			if(!lineItemPrice.isEmpty()){
397
		if(!lineItemPrice.isEmpty()){
399
				// given fofo line item price must be greater than zero
398
			// given fofo line item price must be greater than zero
400
				LOGGER.error("requested itemId's selling price must greater than 0");
399
			LOGGER.error("requested itemId's selling price must greater than 0");
401
				throw new ProfitMandiBusinessException(ProfitMandiConstants.PRICE, lineItemPrice, "");
400
			throw new ProfitMandiBusinessException(ProfitMandiConstants.PRICE, lineItemPrice, "");
402
			}
401
		}
403
	
402
 
404
			List<CurrentInventorySnapshot> currentInventorySnapshots = currentInventorySnapshotRepository.selectByFofoItemIds(fofoDetails.getFofoId(), itemIds);
403
		List<CurrentInventorySnapshot> currentInventorySnapshots = currentInventorySnapshotRepository.selectByFofoItemIds(fofoDetails.getFofoId(), itemIds);
405
			if(itemIds.size() != currentInventorySnapshots.size()){
404
		if(itemIds.size() != currentInventorySnapshots.size()){
406
				// error
405
			// error
-
 
406
		}
-
 
407
		List<ItemIdQuantityAvailability> itemIdQuantityAvailabilities = new ArrayList<>(); //this is for error
-
 
408
		LOGGER.info("currentInventorySnapshots "+currentInventorySnapshots);
-
 
409
		for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
-
 
410
			CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(currentInventorySnapshot.getId().getItemId());
-
 
411
			LOGGER.info("customFofoLineItem "+customFofoLineItem);
-
 
412
			if(customFofoLineItem.getQuantity() > currentInventorySnapshot.getAvailability()){
-
 
413
				ItemIdQuantityAvailability itemIdQuantityAvailability = new ItemIdQuantityAvailability();
-
 
414
				itemIdQuantityAvailability.setItemId(customFofoLineItem.getItemId());
-
 
415
				Quantity quantity = new Quantity();
-
 
416
				quantity.setAvailable(currentInventorySnapshot.getAvailability());
-
 
417
				quantity.setRequested(customFofoLineItem.getQuantity());
-
 
418
				itemIdQuantityAvailability.setQuantity(quantity);
-
 
419
				itemIdQuantityAvailabilities.add(itemIdQuantityAvailability);
407
			}
420
			}
408
			List<ItemIdQuantityAvailability> itemIdQuantityAvailabilities = new ArrayList<>(); //this is for error
-
 
409
			LOGGER.info("currentInventorySnapshots "+currentInventorySnapshots);
-
 
410
			for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
-
 
411
				CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(currentInventorySnapshot.getId().getItemId());
-
 
412
				LOGGER.info("customFofoLineItem "+customFofoLineItem);
-
 
413
				if(customFofoLineItem.getQuantity() > currentInventorySnapshot.getAvailability()){
-
 
414
					ItemIdQuantityAvailability itemIdQuantityAvailability = new ItemIdQuantityAvailability();
-
 
415
					itemIdQuantityAvailability.setItemId(customFofoLineItem.getItemId());
-
 
416
					Quantity quantity = new Quantity();
-
 
417
					quantity.setAvailable(currentInventorySnapshot.getAvailability());
-
 
418
					quantity.setRequested(customFofoLineItem.getQuantity());
-
 
419
					itemIdQuantityAvailability.setQuantity(quantity);
-
 
420
					itemIdQuantityAvailabilities.add(itemIdQuantityAvailability);
-
 
421
				}
-
 
422
	
421
 
423
			}
422
		}
424
	
423
 
425
	
424
 
426
	
425
 
427
			if(!itemIdQuantityAvailabilities.isEmpty()){
426
		if(!itemIdQuantityAvailabilities.isEmpty()){
428
				// itemIdQuantity request is not valid
427
			// itemIdQuantity request is not valid
429
				LOGGER.error("Requested quantities should not be greater than currently available quantities {}", itemIdQuantityAvailabilities);
428
			LOGGER.error("Requested quantities should not be greater than currently available quantities {}", itemIdQuantityAvailabilities);
430
				throw new ProfitMandiBusinessException("itemIdQuantityAvailabilities", itemIdQuantityAvailabilities, "");
429
			throw new ProfitMandiBusinessException("itemIdQuantityAvailabilities", itemIdQuantityAvailabilities, "");
431
			}
430
		}
432
	
431
 
433
	
432
 
434
	
433
 
435
			//		Set<Integer> invalidItemIdSerialNumbers = new HashSet<>();
434
		//		Set<Integer> invalidItemIdSerialNumbers = new HashSet<>();
436
			//		Map<Integer, Set<String>> itemIdSerialNumbers = new HashMap<>();
435
		//		Map<Integer, Set<String>> itemIdSerialNumbers = new HashMap<>();
437
			//		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), itemIds); //change it
436
		//		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), itemIds); //change it
438
			//		
437
		//		
439
			//		Map<Integer, Float> itemIdPriceDropAmount = new HashMap<>();
438
		//		Map<Integer, Float> itemIdPriceDropAmount = new HashMap<>();
440
			//		
439
		//		
441
			//		for(InventoryItem inventoryItem : inventoryItems){
440
		//		for(InventoryItem inventoryItem : inventoryItems){
442
			//			CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(inventoryItem.getItemId());
441
		//			CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(inventoryItem.getItemId());
443
			//			if(customFofoLineItem.getSerialNumbers().isEmpty()){
442
		//			if(customFofoLineItem.getSerialNumbers().isEmpty()){
444
			//				if(!(inventoryItem.getSerialNumber() == null && inventoryItem.getSerialNumber().equals(""))){
443
		//				if(!(inventoryItem.getSerialNumber() == null && inventoryItem.getSerialNumber().equals(""))){
445
			//					invalidItemIdSerialNumbers.add(inventoryItem.getItemId());
444
		//					invalidItemIdSerialNumbers.add(inventoryItem.getItemId());
446
			//				}
445
		//				}
447
			//			}
446
		//			}
448
			//			if(!customFofoLineItem.getSerialNumbers().isEmpty()){
447
		//			if(!customFofoLineItem.getSerialNumbers().isEmpty()){
449
			//				if(!customFofoLineItem.getSerialNumbers().contains(inventoryItem.getSerialNumber())){
448
		//				if(!customFofoLineItem.getSerialNumbers().contains(inventoryItem.getSerialNumber())){
450
			//					if(!itemIdSerialNumbers.containsKey(customFofoLineItem.getItemId())){
449
		//					if(!itemIdSerialNumbers.containsKey(customFofoLineItem.getItemId())){
451
			//						Set<String> serialNumbers = new HashSet<>();
450
		//						Set<String> serialNumbers = new HashSet<>();
452
			//						serialNumbers.add(inventoryItem.getSerialNumber());
451
		//						serialNumbers.add(inventoryItem.getSerialNumber());
453
			//						itemIdSerialNumbers.put(customFofoLineItem.getItemId(), serialNumbers);
452
		//						itemIdSerialNumbers.put(customFofoLineItem.getItemId(), serialNumbers);
454
			//					}else{
453
		//					}else{
455
			//						itemIdSerialNumbers.get(customFofoLineItem.getItemId()).add(inventoryItem.getSerialNumber());
454
		//						itemIdSerialNumbers.get(customFofoLineItem.getItemId()).add(inventoryItem.getSerialNumber());
456
			//				}
455
		//				}
457
			//			}
456
		//			}
458
			//			itemIdPriceDropAmount.put(inventoryItem.getItemId(), inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount());
457
		//			itemIdPriceDropAmount.put(inventoryItem.getItemId(), inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount());
459
			//		}
458
		//		}
460
	
459
 
461
			Map<Integer, Item>  itemMap = new HashMap<Integer, Item>();
460
		Map<Integer, Item>  itemMap = new HashMap<Integer, Item>();
462
			List<Item> items = itemRepository.selectByIds(itemIds);
461
		List<Item> items = itemRepository.selectByIds(itemIds);
463
			for (Item i : items){
462
		for (Item i : items){
464
				itemMap.put(i.getId(), i);
463
			itemMap.put(i.getId(), i);
465
			}
464
		}
466
	
465
 
467
			Set<Integer> nonSerializedItemIds = new HashSet<>();
466
		Set<Integer> nonSerializedItemIds = new HashSet<>();
468
			Set<String> serialNumbers = new HashSet<>();
467
		Set<String> serialNumbers = new HashSet<>();
469
			Map<String, Float> insuranceSerialNumberItemPrice = new HashMap<>();
468
		Map<String, Float> insuranceSerialNumberItemPrice = new HashMap<>();
470
			Map<String, Float> insuranceSerialNumberSaleAmount = new HashMap<>();
469
		Map<String, Float> insuranceSerialNumberSaleAmount = new HashMap<>();
471
			Map<String, String> serialNumberModelName = new HashMap<>();
470
		Map<String, String> serialNumberModelName = new HashMap<>();
472
			Map<String, String> serialNumberBrand = new HashMap<>();
471
		Map<String, String> serialNumberBrand = new HashMap<>();
473
			Collection<CustomFofoLineItem> lineItemsValues = customFofoLineItemMap.values();
472
		Collection<CustomFofoLineItem> lineItemsValues = customFofoLineItemMap.values();
474
			for (CustomFofoLineItem cli : lineItemsValues){
473
		for (CustomFofoLineItem cli : lineItemsValues){
475
	
474
 
476
				Item item = itemMap.get(cli.getItemId());
475
			Item item = itemMap.get(cli.getItemId());
477
				if (item.getType().equals(ItemType.SERIALIZED)){
476
			if (item.getType().equals(ItemType.SERIALIZED)){
478
					for (SerialNumberDetail serialNumberDetail : cli.getSerialNumberDetails()){
477
				for (SerialNumberDetail serialNumberDetail : cli.getSerialNumberDetails()){
479
						serialNumbers.add(serialNumberDetail.getSerialNumber());
478
					serialNumbers.add(serialNumberDetail.getSerialNumber());
480
						if(serialNumberDetail.isInsurance()){
479
					if(serialNumberDetail.isInsurance()){
481
							insuranceSerialNumberItemPrice.put(serialNumberDetail.getSerialNumber(), cli.getSellingPrice());
480
						insuranceSerialNumberItemPrice.put(serialNumberDetail.getSerialNumber(), cli.getSellingPrice());
482
							insuranceSerialNumberSaleAmount.put(serialNumberDetail.getSerialNumber(), serialNumberDetail.getAmount());
481
						insuranceSerialNumberSaleAmount.put(serialNumberDetail.getSerialNumber(), serialNumberDetail.getAmount());
483
							serialNumberModelName.put(serialNumberDetail.getSerialNumber(), item.getModelName());
482
						serialNumberModelName.put(serialNumberDetail.getSerialNumber(), item.getModelName());
484
							serialNumberBrand.put(serialNumberDetail.getSerialNumber(), item.getBrand());
483
						serialNumberBrand.put(serialNumberDetail.getSerialNumber(), item.getBrand());
485
						}
-
 
486
					}
484
					}
487
				}
485
				}
488
				else{
-
 
489
					nonSerializedItemIds.add(cli.getItemId());
-
 
490
				}
-
 
491
	
-
 
492
			}
486
			}
493
	
-
 
494
	
-
 
495
			Map<Integer, List<InventoryItem>> serializedInventoryItemMap = new HashMap<Integer, List<InventoryItem>>();
-
 
496
			Map<Integer, List<InventoryItem>> nonSerializedInventoryItemMap = new HashMap<Integer, List<InventoryItem>>();
-
 
497
			Map<Integer, List<Float>> itemIdPriceDropAmount = new HashMap<>();
-
 
498
	
-
 
499
			if (!serialNumbers.isEmpty()){
-
 
500
				List<InventoryItem> serializedInventoryItems = inventoryItemRepository.selectByFofoIdSerialNumbers(fofoDetails.getFofoId(), serialNumbers);
-
 
501
				LOGGER.info("serializedInventoryItems {}", serializedInventoryItems);
-
 
502
				for (InventoryItem it : serializedInventoryItems){
-
 
503
					if (it.getGoodQuantity() == 1){
-
 
504
						if (serializedInventoryItemMap.containsKey(it.getItemId())){
-
 
505
							serializedInventoryItemMap.get(it.getItemId()).add(it);
-
 
506
							itemIdPriceDropAmount.get(it.getItemId()).
-
 
507
							add(it.getUnitPrice() - (it.getPriceDropAmount()==null?0:it.getPriceDropAmount()));
-
 
508
						}
-
 
509
						else{
487
			else{
510
							ArrayList<InventoryItem> tmp = new ArrayList<InventoryItem>();
-
 
511
							tmp.add(it);
-
 
512
							serializedInventoryItemMap.put(it.getItemId(), tmp);
488
				nonSerializedItemIds.add(cli.getItemId());
513
							ArrayList<Float> priceDropAmouts = new ArrayList<>();
-
 
514
							priceDropAmouts.add(it.getUnitPrice() - (it.getPriceDropAmount()==null?0:it.getPriceDropAmount()));
-
 
515
							itemIdPriceDropAmount.put(it.getItemId(), priceDropAmouts);
-
 
516
						}
-
 
517
					}
-
 
518
				}
-
 
519
			}
489
			}
-
 
490
 
-
 
491
		}
-
 
492
 
520
	
493
 
-
 
494
		Map<Integer, List<InventoryItem>> serializedInventoryItemMap = new HashMap<Integer, List<InventoryItem>>();
-
 
495
		Map<Integer, List<InventoryItem>> nonSerializedInventoryItemMap = new HashMap<Integer, List<InventoryItem>>();
-
 
496
		Map<Integer, List<Float>> itemIdPriceDropAmount = new HashMap<>();
-
 
497
 
521
			if (!nonSerializedItemIds.isEmpty()){
498
		if (!serialNumbers.isEmpty()){
522
				List<InventoryItem> nonSerializedInventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), nonSerializedItemIds);
499
			List<InventoryItem> serializedInventoryItems = inventoryItemRepository.selectByFofoIdSerialNumbers(fofoDetails.getFofoId(), serialNumbers);
-
 
500
			LOGGER.info("serializedInventoryItems {}", serializedInventoryItems);
523
				for (InventoryItem it : nonSerializedInventoryItems){
501
			for (InventoryItem it : serializedInventoryItems){
524
					if (it.getGoodQuantity() > 0){
502
				if (it.getGoodQuantity() == 1){
525
						if (nonSerializedInventoryItemMap.containsKey(it.getItemId())){
503
					if (serializedInventoryItemMap.containsKey(it.getItemId())){
526
							nonSerializedInventoryItemMap.get(it.getItemId()).add(it);
504
						serializedInventoryItemMap.get(it.getItemId()).add(it);
-
 
505
						itemIdPriceDropAmount.get(it.getItemId()).
-
 
506
						add(it.getUnitPrice() - (it.getPriceDropAmount()==null?0:it.getPriceDropAmount()));
527
						}
507
					}
528
						else{
508
					else{
529
							ArrayList<InventoryItem> tmp = new ArrayList<InventoryItem>();
509
						ArrayList<InventoryItem> tmp = new ArrayList<InventoryItem>();
530
							tmp.add(it);
510
						tmp.add(it);
531
							nonSerializedInventoryItemMap.put(it.getItemId(), tmp);
511
						serializedInventoryItemMap.put(it.getItemId(), tmp);
532
						}
512
						ArrayList<Float> priceDropAmouts = new ArrayList<>();
-
 
513
						priceDropAmouts.add(it.getUnitPrice() - (it.getPriceDropAmount()==null?0:it.getPriceDropAmount()));
-
 
514
						itemIdPriceDropAmount.put(it.getItemId(), priceDropAmouts);
533
					}
515
					}
534
				}
516
				}
535
			}
517
			}
-
 
518
		}
536
	
519
 
537
			List<Integer> invalidItemIdSerialNumbers = new ArrayList<Integer>();
520
		if (!nonSerializedItemIds.isEmpty()){
-
 
521
			List<InventoryItem> nonSerializedInventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), nonSerializedItemIds);
538
			List<Integer> itemIdSerialNumbers = new ArrayList<Integer>();
522
			for (InventoryItem it : nonSerializedInventoryItems){
539
	
-
 
540
			for (Item i : items){
523
				if (it.getGoodQuantity() > 0){
541
				CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(i.getId());
524
					if (nonSerializedInventoryItemMap.containsKey(it.getItemId())){
542
				if (i.getType().equals(ItemType.SERIALIZED)){
525
						nonSerializedInventoryItemMap.get(it.getItemId()).add(it);
-
 
526
					}
-
 
527
					else{
543
					if (customFofoLineItem ==null || customFofoLineItem.getSerialNumberDetails().isEmpty()){
528
						ArrayList<InventoryItem> tmp = new ArrayList<InventoryItem>();
-
 
529
						tmp.add(it);
544
						invalidItemIdSerialNumbers.add(i.getId());
530
						nonSerializedInventoryItemMap.put(it.getItemId(), tmp);
545
					}
531
					}
546
				}
532
				}
547
				else{
533
			}
-
 
534
		}
-
 
535
 
-
 
536
		List<Integer> invalidItemIdSerialNumbers = new ArrayList<Integer>();
-
 
537
		List<Integer> itemIdSerialNumbers = new ArrayList<Integer>();
-
 
538
 
-
 
539
		for (Item i : items){
-
 
540
			CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(i.getId());
-
 
541
			if (i.getType().equals(ItemType.SERIALIZED)){
548
					if (customFofoLineItem == null || !customFofoLineItem.getSerialNumberDetails().isEmpty()){
542
				if (customFofoLineItem ==null || customFofoLineItem.getSerialNumberDetails().isEmpty()){
549
						itemIdSerialNumbers.add(i.getId());
543
					invalidItemIdSerialNumbers.add(i.getId());
550
					}
-
 
551
				}
544
				}
552
			}
545
			}
553
	
-
 
554
			if(!invalidItemIdSerialNumbers.isEmpty()){
-
 
555
				LOGGER.error("Invalid itemId's serialNumbers {}", invalidItemIdSerialNumbers);
-
 
556
				// itemId's are serialized you are saying these are not serialized
-
 
557
				throw new ProfitMandiBusinessException("invalidItemIdSerialNumbers", invalidItemIdSerialNumbers, "");
-
 
558
			}
546
			else{
559
	
-
 
560
			if(!itemIdSerialNumbers.isEmpty()){
-
 
561
				LOGGER.error("Invalid itemId's serialNumbers {}", itemIdSerialNumbers);
-
 
562
				// itemId's are non serialized you are saying these are serialized
-
 
563
				throw new ProfitMandiBusinessException("itemIdSerialNumbers", itemIdSerialNumbers, "");
-
 
564
			}
-
 
565
	
-
 
566
			if(items.size() != itemIds.size()){
-
 
567
				LOGGER.error("Requested ItemIds not found in catalog");
-
 
568
				// invalid itemIds 
-
 
569
				throw new ProfitMandiBusinessException("invalidItemIds", "", "");
-
 
570
			}
-
 
571
	
-
 
572
			Map<Integer, List<InventoryItem>> inventoryItemsToBill = new HashMap<Integer,List<InventoryItem>>();
-
 
573
			Map<Integer, Integer> inventoryItemQuantityUsed = new HashMap<>(); //to keep track of inventoryitem quanity used for scan records insertion
-
 
574
	
-
 
575
			LOGGER.info("itemMap keys {}", itemMap.keySet());
-
 
576
			//Lets reduce quantity and decide what inventory items to use.
-
 
577
			for (Item i : items){
-
 
578
				CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(i.getId());
547
				if (customFofoLineItem == null || !customFofoLineItem.getSerialNumberDetails().isEmpty()){
579
				if (i.getType().equals(ItemType.SERIALIZED)){
-
 
580
					//TODO:handle null
-
 
581
					if (serializedInventoryItemMap.get(i.getId()) == null || customFofoLineItem.getSerialNumberDetails().size() != serializedInventoryItemMap.get(i.getId()).size()){
-
 
582
						//not enough serial numbers
-
 
583
						//LOGGER.info("serialNumbers {}", serialNumbers);
-
 
584
						LOGGER.info("serializedInventoryItemMap {}", serializedInventoryItemMap);
-
 
585
						LOGGER.info("itemId {}", i.getId());
548
					itemIdSerialNumbers.add(i.getId());
586
						LOGGER.error("not enough serial numbers");
-
 
587
						throw new ProfitMandiBusinessException("notEnoughSerialNumbers", "", "");
-
 
588
					}
-
 
589
					List<InventoryItem> inventoryItemsSerializedserialized = serializedInventoryItemMap.get(i.getId());
-
 
590
					for (InventoryItem it : inventoryItemsSerializedserialized){
-
 
591
						it.setGoodQuantity(0);
-
 
592
						inventoryItemQuantityUsed.put(it.getId(), 1);
-
 
593
					}
-
 
594
					inventoryItemsToBill.put(i.getId(), inventoryItemsSerializedserialized);
-
 
595
				}
549
				}
-
 
550
			}
-
 
551
		}
-
 
552
 
-
 
553
		if(!invalidItemIdSerialNumbers.isEmpty()){
-
 
554
			LOGGER.error("Invalid itemId's serialNumbers {}", invalidItemIdSerialNumbers);
-
 
555
			// itemId's are serialized you are saying these are not serialized
-
 
556
			throw new ProfitMandiBusinessException("invalidItemIdSerialNumbers", invalidItemIdSerialNumbers, "");
-
 
557
		}
-
 
558
 
-
 
559
		if(!itemIdSerialNumbers.isEmpty()){
-
 
560
			LOGGER.error("Invalid itemId's serialNumbers {}", itemIdSerialNumbers);
-
 
561
			// itemId's are non serialized you are saying these are serialized
-
 
562
			throw new ProfitMandiBusinessException("itemIdSerialNumbers", itemIdSerialNumbers, "");
-
 
563
		}
-
 
564
 
-
 
565
		if(items.size() != itemIds.size()){
-
 
566
			LOGGER.error("Requested ItemIds not found in catalog");
-
 
567
			// invalid itemIds 
-
 
568
			throw new ProfitMandiBusinessException("invalidItemIds", "", "");
-
 
569
		}
-
 
570
 
-
 
571
		Map<Integer, List<InventoryItem>> inventoryItemsToBill = new HashMap<Integer,List<InventoryItem>>();
-
 
572
		Map<Integer, Integer> inventoryItemQuantityUsed = new HashMap<>(); //to keep track of inventoryitem quanity used for scan records insertion
-
 
573
 
-
 
574
		LOGGER.info("itemMap keys {}", itemMap.keySet());
-
 
575
		//Lets reduce quantity and decide what inventory items to use.
-
 
576
		for (Item i : items){
-
 
577
			CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(i.getId());
-
 
578
			if (i.getType().equals(ItemType.SERIALIZED)){
-
 
579
				//TODO:handle null
-
 
580
				if (serializedInventoryItemMap.get(i.getId()) == null || customFofoLineItem.getSerialNumberDetails().size() != serializedInventoryItemMap.get(i.getId()).size()){
-
 
581
					//not enough serial numbers
-
 
582
					//LOGGER.info("serialNumbers {}", serialNumbers);
-
 
583
					LOGGER.info("serializedInventoryItemMap {}", serializedInventoryItemMap);
-
 
584
					LOGGER.info("itemId {}", i.getId());
-
 
585
					LOGGER.error("not enough serial numbers");
-
 
586
					throw new ProfitMandiBusinessException("notEnoughSerialNumbers", "", "");
-
 
587
				}
-
 
588
				List<InventoryItem> inventoryItemsSerializedserialized = serializedInventoryItemMap.get(i.getId());
-
 
589
				for (InventoryItem it : inventoryItemsSerializedserialized){
-
 
590
					it.setGoodQuantity(0);
-
 
591
					inventoryItemQuantityUsed.put(it.getId(), 1);
-
 
592
				}
-
 
593
				inventoryItemsToBill.put(i.getId(), inventoryItemsSerializedserialized);
-
 
594
			}
596
				else{
595
			else{
597
					List<InventoryItem> inventoryItemsNonSerialized = nonSerializedInventoryItemMap.get(i.getId());
596
				List<InventoryItem> inventoryItemsNonSerialized = nonSerializedInventoryItemMap.get(i.getId());
598
					int quantityToBill = customFofoLineItem.getQuantity();
597
				int quantityToBill = customFofoLineItem.getQuantity();
599
					int totalLeft = quantityToBill;
598
				int totalLeft = quantityToBill;
600
					List<InventoryItem> inventoryItemsNonSerializedUsed = new ArrayList<InventoryItem>();
599
				List<InventoryItem> inventoryItemsNonSerializedUsed = new ArrayList<InventoryItem>();
601
					if (inventoryItemsNonSerialized!=null){
600
				if (inventoryItemsNonSerialized!=null){
602
						for (InventoryItem it : inventoryItemsNonSerialized){
601
					for (InventoryItem it : inventoryItemsNonSerialized){
603
							if (totalLeft > 0){
602
						if (totalLeft > 0){
604
								int toUse = Math.min(totalLeft, it.getGoodQuantity());
603
							int toUse = Math.min(totalLeft, it.getGoodQuantity());
605
								inventoryItemQuantityUsed.put(it.getId(), toUse);
604
							inventoryItemQuantityUsed.put(it.getId(), toUse);
606
								it.setGoodQuantity(it.getGoodQuantity()  - toUse);
605
							it.setGoodQuantity(it.getGoodQuantity()  - toUse);
607
								totalLeft = totalLeft - toUse;
606
							totalLeft = totalLeft - toUse;
608
								inventoryItemsNonSerializedUsed.add(it);
607
							inventoryItemsNonSerializedUsed.add(it);
609
							}
-
 
610
						}
608
						}
611
					}
609
					}
612
					
-
 
613
					if (totalLeft > 0){
-
 
614
						//not enough quanity for non-serialized
-
 
615
						System.out.println("not enough quanity for non-serialized");
-
 
616
						throw new ProfitMandiBusinessException("notEnoughQuantityForNonSerialized", "", "");
-
 
617
					}
-
 
618
					inventoryItemsToBill.put(i.getId(), inventoryItemsNonSerializedUsed);
-
 
619
				}
610
				}
620
			}
611
				
621
			
-
 
622
			// mop price validation
612
				if (totalLeft > 0){
623
			Map<Integer, Float> invalidMopItemIdPriceMap = new HashMap<>();
613
					//not enough quanity for non-serialized
624
			Map<Integer, Float> itemIdMopPriceMap = pricingService.getPurchasePriceMopPriceNotFound(itemIds, fofoDetails.getFofoId());
-
 
625
			for(Map.Entry<Integer, Float> entry : itemIdMopPriceMap.entrySet()){
614
					System.out.println("not enough quanity for non-serialized");
626
				CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(entry.getKey());
-
 
627
				if(entry.getValue() < Float.MAX_VALUE && customFofoLineItem.getSellingPrice() < entry.getValue()){
-
 
628
					invalidMopItemIdPriceMap.put(entry.getKey(), customFofoLineItem.getSellingPrice());
615
					throw new ProfitMandiBusinessException("notEnoughQuantityForNonSerialized", "", "");
629
				}
616
				}
-
 
617
				inventoryItemsToBill.put(i.getId(), inventoryItemsNonSerializedUsed);
630
			}
618
			}
-
 
619
		}
-
 
620
		
-
 
621
		// mop price validation
-
 
622
		Map<Integer, Float> invalidMopItemIdPriceMap = new HashMap<>();
-
 
623
		Map<Integer, Float> itemIdMopPriceMap = pricingService.getPurchasePriceMopPriceNotFound(itemIds, fofoDetails.getFofoId());
-
 
624
		for(Map.Entry<Integer, Float> entry : itemIdMopPriceMap.entrySet()){
-
 
625
			CustomFofoLineItem customFofoLineItem = customFofoLineItemMap.get(entry.getKey());
-
 
626
			if(entry.getValue() < Float.MAX_VALUE && customFofoLineItem.getSellingPrice() < entry.getValue()){
-
 
627
				invalidMopItemIdPriceMap.put(entry.getKey(), customFofoLineItem.getSellingPrice());
631
			
628
			}
-
 
629
		}
-
 
630
		
632
			if(!invalidMopItemIdPriceMap.isEmpty()){
631
		if(!invalidMopItemIdPriceMap.isEmpty()){
633
				LOGGER.error("Invalid itemIds selling prices{} should be greater than mop prices {}", invalidMopItemIdPriceMap, itemIdMopPriceMap);
632
			LOGGER.error("Invalid itemIds selling prices{} should be greater than mop prices {}", invalidMopItemIdPriceMap, itemIdMopPriceMap);
634
				throw new ProfitMandiBusinessException("invalidMopItemIdPrice", invalidMopItemIdPriceMap, "");
633
			throw new ProfitMandiBusinessException("invalidMopItemIdPrice", invalidMopItemIdPriceMap, "");
-
 
634
		}
-
 
635
 
-
 
636
		InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = null;
-
 
637
		try{
-
 
638
			invoiceNumberGenerationSequence = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoDetails.getFofoId());
-
 
639
			invoiceNumberGenerationSequence.setSequence(invoiceNumberGenerationSequence.getSequence() + 1);
-
 
640
			invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
-
 
641
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
642
			invoiceNumberGenerationSequence = new InvoiceNumberGenerationSequence();
-
 
643
			invoiceNumberGenerationSequence.setFofoId(fofoDetails.getFofoId());
-
 
644
			invoiceNumberGenerationSequence.setPrefix("INVOICE");
-
 
645
			invoiceNumberGenerationSequence.setSequence(1);
-
 
646
			invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
-
 
647
		}
-
 
648
 
-
 
649
		CustomCustomer customCustomer = createOrderRequest.getCustomer();
-
 
650
 
-
 
651
		if(!StringUtils.isValidEmailAddress(customCustomer.getEmailId())){
-
 
652
			LOGGER.error("invalid customer emailId {} ", customCustomer.getEmailId());
-
 
653
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, customCustomer.getEmailId(), "");
-
 
654
		}
-
 
655
 
-
 
656
		if(!StringUtils.isValidMobile(customCustomer.getMobileNumber())){
-
 
657
			LOGGER.error("invalid customer mobileNumber {} ", customCustomer.getMobileNumber());
-
 
658
			throw new ProfitMandiBusinessException(ProfitMandiConstants.MOBILE_NUMBER, customCustomer.getMobileNumber(), "");
-
 
659
		}
-
 
660
		
-
 
661
		boolean insurance = false;
-
 
662
		for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
-
 
663
			for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){
-
 
664
				if(serialNumberDetail.getAmount() > 0){
-
 
665
					insurance = true;
-
 
666
					break;
-
 
667
				}
635
			}
668
			}
-
 
669
		}
-
 
670
		LOGGER.info("insurance [{}] processing ....", insurance);
636
	
671
		
637
			InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = null;
672
		LocalDate customerDateOfBirth = null;
-
 
673
		if(insurance){
638
			try{
674
			try{
639
				invoiceNumberGenerationSequence = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoDetails.getFofoId());
-
 
640
				invoiceNumberGenerationSequence.setSequence(invoiceNumberGenerationSequence.getSequence() + 1);
-
 
641
				invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
675
				customerDateOfBirth = StringUtils.toDate(createOrderRequest.getCustomerDateOfBirth());
642
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
676
			}catch(DateTimeException dateTimeException){
643
				invoiceNumberGenerationSequence = new InvoiceNumberGenerationSequence();
-
 
644
				invoiceNumberGenerationSequence.setFofoId(fofoDetails.getFofoId());
-
 
645
				invoiceNumberGenerationSequence.setPrefix("INVOICE");
-
 
646
				invoiceNumberGenerationSequence.setSequence(1);
-
 
647
				invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);
-
 
648
			}
-
 
649
	
-
 
650
			CustomCustomer customCustomer = createOrderRequest.getCustomer();
-
 
651
	
-
 
652
			if(!StringUtils.isValidEmailAddress(customCustomer.getEmailId())){
-
 
653
				LOGGER.error("invalid customer emailId {} ", customCustomer.getEmailId());
677
				LOGGER.error("Unable to parse dateOfBirth", dateTimeException);
654
				throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, customCustomer.getEmailId(), "");
678
				throw new ProfitMandiBusinessException("dateOfBirth", createOrderRequest.getCustomerDateOfBirth(), "");
655
			}
-
 
656
	
-
 
657
			if(!StringUtils.isValidMobile(customCustomer.getMobileNumber())){
-
 
658
				LOGGER.error("invalid customer mobileNumber {} ", customCustomer.getMobileNumber());
-
 
659
				throw new ProfitMandiBusinessException(ProfitMandiConstants.MOBILE_NUMBER, customCustomer.getMobileNumber(), "");
-
 
660
			}
679
			}
661
			
680
		}
-
 
681
		
662
			boolean insurance = false;
682
		Customer customer = null;
-
 
683
		try{
-
 
684
			customer = customerRepository.selectByMobileNumber(customCustomer.getMobileNumber());
-
 
685
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
686
			LOGGER.error("Error : ", profitMandiBusinessException);
-
 
687
			customer = new Customer();
-
 
688
			customer.setFirstName(customCustomer.getFirstName());
-
 
689
			customer.setLastName(customCustomer.getLastName());
-
 
690
			customer.setEmailId(customCustomer.getEmailId());
-
 
691
			customer.setMobileNumber(customCustomer.getMobileNumber());
-
 
692
			customerRepository.persist(customer);
-
 
693
		}
-
 
694
		//TODO:Check if createOrderRequest contains addressId
-
 
695
		CustomerAddress customerAddress = this.createCustomerAddress(customCustomer.getAddress());
-
 
696
		customerAddress.setCustomerId(customer.getId());
-
 
697
		customerAddressRepository.persist(customerAddress);
-
 
698
 
-
 
699
		FofoOrder fofoOrder = new FofoOrder();
-
 
700
		fofoOrder.setCustomerId(customer.getId());
-
 
701
		fofoOrder.setFofoId(fofoDetails.getFofoId());
-
 
702
		fofoOrder.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());
-
 
703
		fofoOrder.setTotalAmount(totalAmount);
-
 
704
		fofoOrder.setCustomerAddressId(customerAddress.getId());
-
 
705
		fofoOrderRepository.persist(fofoOrder);
-
 
706
 
-
 
707
		for(CustomPaymentOption customPaymentOption : createOrderRequest.getPaymentOptions()){
-
 
708
			PaymentOption paymentOption = new PaymentOption();
-
 
709
			paymentOption.setOrderId(fofoOrder.getId());
-
 
710
			paymentOption.setAmount(customPaymentOption.getAmount());
-
 
711
			paymentOption.setType(customPaymentOption.getType());
-
 
712
			paymentOptionRepository.persist(paymentOption);
-
 
713
		}
-
 
714
 
-
 
715
		Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(fofoDetails.getFofoId()));
-
 
716
		Map<String, GstRate> gstRateMap = null;
-
 
717
		if(retailerAddress.getState().equals(customerAddress.getState())){
-
 
718
			gstRateMap = Utils.getGstRates(retailerAddress.getState());
-
 
719
		}else{
-
 
720
			LOGGER.info("inter gstRate = true");
-
 
721
			gstRateMap = Utils.getInterGstRates();
-
 
722
		}
-
 
723
		LOGGER.info("gstRateMap {}", gstRateMap);
663
			for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
724
		for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
-
 
725
			FofoLineItem fofoLineItem = new FofoLineItem();
-
 
726
			fofoLineItem.setItemId(customFofoLineItem.getItemId());
-
 
727
			fofoLineItem.setQuantity(customFofoLineItem.getQuantity());
-
 
728
			fofoLineItem.setSellingPrice(customFofoLineItem.getSellingPrice());
-
 
729
			fofoLineItem.setOrderId(fofoOrder.getId());
-
 
730
			fofoLineItem.setDp(customFofoLineItem.getSellingPrice());
-
 
731
			Item item = itemMap.get(customFofoLineItem.getItemId());
-
 
732
			GstRate gstRate = gstRateMap.get(item.getHsnCode());
-
 
733
			fofoLineItem.setIgstRate(gstRate.getIgstRate());
-
 
734
			fofoLineItem.setCgstRate(gstRate.getCgstRate());
-
 
735
			fofoLineItem.setSgstRate(gstRate.getSgstRate());
-
 
736
			fofoLineItem.setHsnCode(gstRate.getHsnCode());
-
 
737
			List<Float> priceDropAmounts = itemIdPriceDropAmount.get(customFofoLineItem.getItemId());
-
 
738
			float cost = 0;
-
 
739
			if (priceDropAmounts!=null){
-
 
740
				for (Float pda : priceDropAmounts){
-
 
741
					cost = cost + pda;
-
 
742
				}
-
 
743
			}
-
 
744
			else{
-
 
745
				cost = customFofoLineItem.getSellingPrice()* customFofoLineItem.getQuantity();
-
 
746
			}
-
 
747
			fofoLineItem.setCost(cost);
-
 
748
			fofoLineItem.setBrand(item.getBrand());
-
 
749
			fofoLineItem.setModelName(item.getModelName());
-
 
750
			fofoLineItem.setModelNumber(item.getModelNumber());
-
 
751
			fofoLineItem.setColor(item.getColor());
-
 
752
			fofoLineItemRepository.persist(fofoLineItem);
-
 
753
			LOGGER.info("\n\n");
-
 
754
			if(!customFofoLineItem.getSerialNumberDetails().isEmpty()){
664
				for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){
755
				for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){
-
 
756
					FofoLineItemSerialNumber fofoLineItemSerialNumber = new FofoLineItemSerialNumber();
665
					if(serialNumberDetail.getAmount() > 0){
757
					fofoLineItemSerialNumber.setFofoLineItemId(fofoLineItem.getId());
666
						insurance = true;
758
					fofoLineItemSerialNumber.setSerialNumber(serialNumberDetail.getSerialNumber());
667
						break;
759
					fofoLineItemSerialNumberRepository.persist(fofoLineItemSerialNumber);
668
					}
-
 
669
				}
760
				}
670
			}
761
			}
-
 
762
 
-
 
763
			for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
-
 
764
				FofoItemId fofoItemId = new FofoItemId();
-
 
765
				fofoItemId.setFofoId(fofoDetails.getFofoId());
-
 
766
				fofoItemId.setItemId(fofoLineItem.getItemId());
-
 
767
				if(currentInventorySnapshot.getId().equals(fofoItemId)){
-
 
768
					currentInventorySnapshotRepository.updateAvailabilityByFofoItemId(fofoItemId, currentInventorySnapshot.getAvailability() - customFofoLineItem.getQuantity());
-
 
769
				}
-
 
770
			}
-
 
771
			List<InventoryItem> inventoryItems = inventoryItemsToBill.get(fofoLineItem.getItemId());
-
 
772
			for(InventoryItem inventoryItem : inventoryItems){
-
 
773
				inventoryItem.setLastScanType(ScanType.SALE);
-
 
774
				inventoryItemRepository.persist(inventoryItem);
-
 
775
				ScanRecord scanRecord = new ScanRecord();
-
 
776
				scanRecord.setInventoryItemId(inventoryItem.getId());
-
 
777
				scanRecord.setFofoId(fofoDetails.getFofoId());
-
 
778
				//correct this
-
 
779
				scanRecord.setQuantity(inventoryItemQuantityUsed.get(inventoryItem.getId()));
-
 
780
				scanRecord.setType(ScanType.SALE);
-
 
781
				scanRecordRepository.persist(scanRecord);
-
 
782
			}
-
 
783
		}
-
 
784
		
-
 
785
		// insurance calculation is insurance flag is enabled
-
 
786
		if(!insuranceSerialNumberItemPrice.isEmpty()){
671
			LOGGER.info("insurance [{}] processing ....", insurance);
787
			LOGGER.info("Processing for insurence for serialNumbers");
-
 
788
			Map<Float, GadgetCopsInsuranceCalc> insurancePricesMap = pricingService.getInsurancePrices(new HashSet<>(insuranceSerialNumberItemPrice.values()), ProfitMandiConstants.GADGET_COPS);
-
 
789
			InsuranceProvider insuranceProvider = insuranceProviderRepository.selectByName(ProfitMandiConstants.GADGET_COPS);
672
			
790
			
673
			LocalDate customerDateOfBirth = null;
-
 
674
			if(insurance){
-
 
675
				try{
791
			
676
					customerDateOfBirth = StringUtils.toDate(createOrderRequest.getCustomerDateOfBirth());
792
			Map<Float, Float> invalidInsurancePurchaseSaleAmount = new HashMap<>();
677
				}catch(DateTimeException dateTimeException){
793
			Map<Float, Float> invalidInsuranceSalePurchaseAmount = new HashMap<>();
678
					LOGGER.error("Unable to parse dateOfBirth", dateTimeException);
794
			for(Map.Entry<String, Float> entry : insuranceSerialNumberItemPrice.entrySet()){
679
					throw new ProfitMandiBusinessException("dateOfBirth", createOrderRequest.getCustomerDateOfBirth(), "");
795
				if(insuranceSerialNumberSaleAmount.get(entry.getKey()) < insurancePricesMap.get(entry.getValue()).getDealerPrice()){
-
 
796
					invalidInsurancePurchaseSaleAmount.put(insurancePricesMap.get(entry.getValue()).getDealerPrice(), insuranceSerialNumberSaleAmount.get(entry.getKey()));
-
 
797
				}
-
 
798
				
-
 
799
				if(insuranceSerialNumberSaleAmount.get(entry.getKey()) > insurancePricesMap.get(entry.getValue()).getSellingPrice()){
-
 
800
					invalidInsuranceSalePurchaseAmount.put(insuranceSerialNumberSaleAmount.get(entry.getKey()), insurancePricesMap.get(entry.getValue()).getDealerPrice());
680
				}
801
				}
681
			}
802
			}
682
			
803
			
683
			Customer customer = null;
-
 
684
			try{
-
 
685
				customer = customerRepository.selectByMobileNumber(customCustomer.getMobileNumber());
-
 
686
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
804
			// insurance sale amount can not be lesser than insurance purchase amount
687
				LOGGER.error("Error : ", profitMandiBusinessException);
-
 
688
				customer = new Customer();
-
 
689
				customer.setFirstName(customCustomer.getFirstName());
-
 
690
				customer.setLastName(customCustomer.getLastName());
-
 
691
				customer.setEmailId(customCustomer.getEmailId());
-
 
692
				customer.setMobileNumber(customCustomer.getMobileNumber());
-
 
693
				customerRepository.persist(customer);
-
 
694
			}
-
 
695
			//TODO:Check if createOrderRequest contains addressId
-
 
696
			CustomerAddress customerAddress = this.createCustomerAddress(customCustomer.getAddress());
-
 
697
			customerAddress.setCustomerId(customer.getId());
-
 
698
			customerAddressRepository.persist(customerAddress);
-
 
699
	
-
 
700
			FofoOrder fofoOrder = new FofoOrder();
-
 
701
			fofoOrder.setCustomerId(customer.getId());
-
 
702
			fofoOrder.setFofoId(fofoDetails.getFofoId());
-
 
703
			fofoOrder.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());
-
 
704
			fofoOrder.setTotalAmount(totalAmount);
-
 
705
			fofoOrder.setCustomerAddressId(customerAddress.getId());
-
 
706
			fofoOrderRepository.persist(fofoOrder);
-
 
707
	
-
 
708
			for(CustomPaymentOption customPaymentOption : createOrderRequest.getPaymentOptions()){
-
 
709
				PaymentOption paymentOption = new PaymentOption();
-
 
710
				paymentOption.setOrderId(fofoOrder.getId());
-
 
711
				paymentOption.setAmount(customPaymentOption.getAmount());
-
 
712
				paymentOption.setType(customPaymentOption.getType());
805
			if(!invalidInsurancePurchaseSaleAmount.isEmpty()){
713
				paymentOptionRepository.persist(paymentOption);
-
 
714
			}
-
 
715
	
-
 
716
			Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(fofoDetails.getFofoId()));
806
				LOGGER.error("Invalid Insurance prices [{}], insurance sale amount can not be lesser than insurance purchase amount", invalidInsurancePurchaseSaleAmount);
717
			Map<String, GstRate> gstRateMap = null;
-
 
718
			if(retailerAddress.getState().equals(customerAddress.getState())){
-
 
719
				gstRateMap = Utils.getGstRates(retailerAddress.getState());
-
 
720
			}else{
807
				return "error";
721
				LOGGER.info("inter gstRate = true");
-
 
722
				gstRateMap = Utils.getInterGstRates();
-
 
723
			}
808
			}
724
			LOGGER.info("gstRateMap {}", gstRateMap);
-
 
725
			for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){
-
 
726
				FofoLineItem fofoLineItem = new FofoLineItem();
-
 
727
				fofoLineItem.setItemId(customFofoLineItem.getItemId());
-
 
728
				fofoLineItem.setQuantity(customFofoLineItem.getQuantity());
-
 
729
				fofoLineItem.setSellingPrice(customFofoLineItem.getSellingPrice());
-
 
730
				fofoLineItem.setOrderId(fofoOrder.getId());
-
 
731
				fofoLineItem.setDp(customFofoLineItem.getSellingPrice());
-
 
732
				Item item = itemMap.get(customFofoLineItem.getItemId());
-
 
733
				GstRate gstRate = gstRateMap.get(item.getHsnCode());
-
 
734
				fofoLineItem.setIgstRate(gstRate.getIgstRate());
-
 
735
				fofoLineItem.setCgstRate(gstRate.getCgstRate());
-
 
736
				fofoLineItem.setSgstRate(gstRate.getSgstRate());
-
 
737
				fofoLineItem.setHsnCode(gstRate.getHsnCode());
-
 
738
				List<Float> priceDropAmounts = itemIdPriceDropAmount.get(customFofoLineItem.getItemId());
-
 
739
				float cost = 0;
-
 
740
				if (priceDropAmounts!=null){
-
 
741
					for (Float pda : priceDropAmounts){
-
 
742
						cost = cost + pda;
-
 
743
					}
-
 
744
				}
-
 
745
				else{
-
 
746
					cost = customFofoLineItem.getSellingPrice()* customFofoLineItem.getQuantity();
-
 
747
				}
809
			
748
				fofoLineItem.setCost(cost);
-
 
749
				fofoLineItem.setBrand(item.getBrand());
-
 
750
				fofoLineItem.setModelName(item.getModelName());
-
 
751
				fofoLineItem.setModelNumber(item.getModelNumber());
-
 
752
				fofoLineItem.setColor(item.getColor());
-
 
753
				fofoLineItemRepository.persist(fofoLineItem);
-
 
754
				LOGGER.info("\n\n");
-
 
755
				if(!customFofoLineItem.getSerialNumberDetails().isEmpty()){
810
			if(!invalidInsuranceSalePurchaseAmount.isEmpty()){
756
					for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){
-
 
757
						FofoLineItemSerialNumber fofoLineItemSerialNumber = new FofoLineItemSerialNumber();
-
 
758
						fofoLineItemSerialNumber.setFofoLineItemId(fofoLineItem.getId());
-
 
759
						fofoLineItemSerialNumber.setSerialNumber(serialNumberDetail.getSerialNumber());
-
 
760
						fofoLineItemSerialNumberRepository.persist(fofoLineItemSerialNumber);
-
 
761
					}
-
 
762
				}
-
 
763
	
-
 
764
				for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){
-
 
765
					FofoItemId fofoItemId = new FofoItemId();
-
 
766
					fofoItemId.setFofoId(fofoDetails.getFofoId());
-
 
767
					fofoItemId.setItemId(fofoLineItem.getItemId());
-
 
768
					if(currentInventorySnapshot.getId().equals(fofoItemId)){
-
 
769
						currentInventorySnapshotRepository.updateAvailabilityByFofoItemId(fofoItemId, currentInventorySnapshot.getAvailability() - customFofoLineItem.getQuantity());
811
				LOGGER.error("Invalid Insurance prices [{}], insurance sale amount can not be greater than than insurance max amount", invalidInsuranceSalePurchaseAmount);
770
					}
-
 
771
				}
-
 
772
				List<InventoryItem> inventoryItems = inventoryItemsToBill.get(fofoLineItem.getItemId());
-
 
773
				for(InventoryItem inventoryItem : inventoryItems){
-
 
774
					inventoryItem.setLastScanType(ScanType.SALE);
-
 
775
					inventoryItemRepository.persist(inventoryItem);
-
 
776
					ScanRecord scanRecord = new ScanRecord();
-
 
777
					scanRecord.setInventoryItemId(inventoryItem.getId());
-
 
778
					scanRecord.setFofoId(fofoDetails.getFofoId());
-
 
779
					//correct this
812
				return "error";
780
					scanRecord.setQuantity(inventoryItemQuantityUsed.get(inventoryItem.getId()));
-
 
781
					scanRecord.setType(ScanType.SALE);
-
 
782
					scanRecordRepository.persist(scanRecord);
-
 
783
				}
-
 
784
			}
813
			}
785
			
814
			
786
			// insurance calculation is insurance flag is enabled
-
 
787
			if(!insuranceSerialNumberItemPrice.isEmpty()){
-
 
788
				LOGGER.info("Processing for insurence for serialNumbers");
-
 
789
				Map<Float, GadgetCopsInsuranceCalc> insurancePricesMap = pricingService.getInsurancePrices(new HashSet<>(insuranceSerialNumberItemPrice.values()), ProfitMandiConstants.GADGET_COPS);
-
 
790
				InsuranceProvider insuranceProvider = insuranceProviderRepository.selectByName(ProfitMandiConstants.GADGET_COPS);
-
 
791
				
-
 
792
				
815
			
793
				Map<Float, Float> invalidInsurancePurchaseSaleAmount = new HashMap<>();
-
 
794
				Map<Float, Float> invalidInsuranceSalePurchaseAmount = new HashMap<>();
-
 
795
				for(Map.Entry<String, Float> entry : insuranceSerialNumberItemPrice.entrySet()){
816
			for(Map.Entry<String, Float> entry : insuranceSerialNumberItemPrice.entrySet()){
796
					if(insuranceSerialNumberSaleAmount.get(entry.getKey()) < insurancePricesMap.get(entry.getValue()).getDealerPrice()){
817
				PolicyNumberGenerationSequence policyNumberGenerationSequence = null;
797
						invalidInsurancePurchaseSaleAmount.put(insurancePricesMap.get(entry.getValue()).getDealerPrice(), insuranceSerialNumberSaleAmount.get(entry.getKey()));
-
 
798
					}
818
				try{
799
					
-
 
800
					if(insuranceSerialNumberSaleAmount.get(entry.getKey()) > insurancePricesMap.get(entry.getValue()).getSellingPrice()){
819
					policyNumberGenerationSequence = policyNumberGenerationSequenceRepository.select();
801
						invalidInsuranceSalePurchaseAmount.put(insuranceSerialNumberSaleAmount.get(entry.getKey()), insurancePricesMap.get(entry.getValue()).getDealerPrice());
820
					policyNumberGenerationSequence.setSequence(policyNumberGenerationSequence.getSequence() + 1);
802
					}
-
 
803
				}
-
 
804
				
-
 
805
				// insurance sale amount can not be lesser than insurance purchase amount
821
					policyNumberGenerationSequenceRepository.persist(policyNumberGenerationSequence);
806
				if(!invalidInsurancePurchaseSaleAmount.isEmpty()){
822
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
807
					LOGGER.error("Invalid Insurance prices [{}], insurance sale amount can not be lesser than insurance purchase amount", invalidInsurancePurchaseSaleAmount);
823
					policyNumberGenerationSequence = new PolicyNumberGenerationSequence();
808
					return "error";
824
					policyNumberGenerationSequence.setSequence(1);
-
 
825
					policyNumberGenerationSequenceRepository.persist(policyNumberGenerationSequence);
809
				}
826
				}
810
				
827
				
811
				if(!invalidInsuranceSalePurchaseAmount.isEmpty()){
828
				InsurancePolicy insurancePolicy = new InsurancePolicy();
812
					LOGGER.error("Invalid Insurance prices [{}], insurance sale amount can not be greater than than insurance max amount", invalidInsuranceSalePurchaseAmount);
829
				insurancePolicy.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());
-
 
830
				insurancePolicy.setRetailerId(fofoDetails.getFofoId());
-
 
831
				insurancePolicy.setPurchaseAmount(insurancePricesMap.get(entry.getValue()).getDealerPrice());
-
 
832
				insurancePolicy.setSaleAmount(insuranceSerialNumberSaleAmount.get(entry.getKey()));
-
 
833
				insurancePolicy.setSellingPrice(entry.getValue());
813
					return "error";
834
				insurancePolicy.setSerialNumber(entry.getKey());
814
				}
-
 
-
 
835
				insurancePolicy.setModelName(serialNumberModelName.get(entry.getKey()));
-
 
836
				insurancePolicy.setBrand(serialNumberBrand.get(entry.getKey()));
-
 
837
				insurancePolicy.setPolicyNumber(StringUtils.generatePolicyNumber(ProfitMandiConstants.POLICY_NUMBER_PREFIX, policyNumberGenerationSequence.getSequence()));
-
 
838
				insurancePolicy.setProviderId(insuranceProvider.getId());
-
 
839
				insurancePolicy.setCustomerFirstName(customer.getFirstName());
-
 
840
				insurancePolicy.setCustomerLastName(customer.getLastName());
-
 
841
				insurancePolicy.setCustomerMobileNumber(customer.getMobileNumber());
-
 
842
				insurancePolicy.setCustomerEmailId(customer.getEmailId());
-
 
843
				insurancePolicy.setCustomerDateOfBirth(customerDateOfBirth);
-
 
844
				insurancePolicy.setCustomerAddress1(customerAddress.getLine1());
-
 
845
				insurancePolicy.setCustomerAddress2(customerAddress.getLine2());
-
 
846
				insurancePolicy.setCustomerCity(customerAddress.getCity());
-
 
847
				insurancePolicy.setCustomerPinCode(customerAddress.getPinCode());
-
 
848
				insurancePolicy.setCustomerState(customerAddress.getState());
815
				
849
				
-
 
850
				GadgetCopsInsuranceModel gadgetCopsInsuranceModel = new GadgetCopsInsuranceModel();
-
 
851
				gadgetCopsInsuranceModel.setBrand(serialNumberBrand.get(entry.getKey()));
-
 
852
				gadgetCopsInsuranceModel.setModelName(serialNumberModelName.get(entry.getKey()));
-
 
853
				gadgetCopsInsuranceModel.setSerialNumber(entry.getKey());
-
 
854
				gadgetCopsInsuranceModel.setCustomerFirstName(customer.getFirstName());
-
 
855
				gadgetCopsInsuranceModel.setCustomerLastName(customer.getLastName());
-
 
856
				gadgetCopsInsuranceModel.setCustomerDateOfBirth(customerDateOfBirth);
-
 
857
				gadgetCopsInsuranceModel.setCustomerMobileNumber(customer.getMobileNumber());
-
 
858
				gadgetCopsInsuranceModel.setCustomerEmailId(customer.getEmailId());
-
 
859
				gadgetCopsInsuranceModel.setCustomerAddress1(customerAddress.getLine1());
-
 
860
				gadgetCopsInsuranceModel.setCustomerAddress2(customerAddress.getLine2());
-
 
861
				gadgetCopsInsuranceModel.setCustomerCity(customerAddress.getCity());
-
 
862
				gadgetCopsInsuranceModel.setCustomerPinCode(customerAddress.getPinCode());
-
 
863
				gadgetCopsInsuranceModel.setCustomerState(customerAddress.getState());
-
 
864
				gadgetCopsInsuranceModel.setPrice(insurancePolicy.getSellingPrice());
-
 
865
				gadgetCopsInsuranceModel.setInvoiceNumber(insurancePolicy.getInvoiceNumber());
-
 
866
				gadgetCopsInsuranceModel.setPolicyNumber(insurancePolicy.getPolicyNumber());
816
				
867
				
817
				for(Map.Entry<String, Float> entry : insuranceSerialNumberItemPrice.entrySet()){
-
 
818
					PolicyNumberGenerationSequence policyNumberGenerationSequence = null;
-
 
819
					try{
-
 
820
						policyNumberGenerationSequence = policyNumberGenerationSequenceRepository.select();
-
 
821
						policyNumberGenerationSequence.setSequence(policyNumberGenerationSequence.getSequence() + 1);
-
 
822
						policyNumberGenerationSequenceRepository.persist(policyNumberGenerationSequence);
-
 
823
					}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
824
						policyNumberGenerationSequence = new PolicyNumberGenerationSequence();
-
 
825
						policyNumberGenerationSequence.setSequence(1);
-
 
826
						policyNumberGenerationSequenceRepository.persist(policyNumberGenerationSequence);
-
 
827
					}
-
 
828
					
-
 
829
					InsurancePolicy insurancePolicy = new InsurancePolicy();
-
 
830
					insurancePolicy.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());
-
 
831
					insurancePolicy.setRetailerId(fofoDetails.getFofoId());
-
 
832
					insurancePolicy.setPurchaseAmount(insurancePricesMap.get(entry.getValue()).getDealerPrice());
-
 
833
					insurancePolicy.setSaleAmount(insuranceSerialNumberSaleAmount.get(entry.getKey()));
-
 
834
					insurancePolicy.setSellingPrice(entry.getValue());
-
 
835
					insurancePolicy.setSerialNumber(entry.getKey());
-
 
836
					insurancePolicy.setModelName(serialNumberModelName.get(entry.getKey()));
-
 
837
					insurancePolicy.setBrand(serialNumberBrand.get(entry.getKey()));
-
 
838
					insurancePolicy.setPolicyNumber(StringUtils.generatePolicyNumber(ProfitMandiConstants.POLICY_NUMBER_PREFIX, policyNumberGenerationSequence.getSequence()));
-
 
839
					insurancePolicy.setProviderId(insuranceProvider.getId());
-
 
840
					insurancePolicy.setCustomerFirstName(customer.getFirstName());
-
 
841
					insurancePolicy.setCustomerLastName(customer.getLastName());
-
 
842
					insurancePolicy.setCustomerMobileNumber(customer.getMobileNumber());
-
 
843
					insurancePolicy.setCustomerEmailId(customer.getEmailId());
-
 
844
					insurancePolicy.setCustomerDateOfBirth(customerDateOfBirth);
-
 
845
					insurancePolicy.setCustomerAddress1(customerAddress.getLine1());
-
 
846
					insurancePolicy.setCustomerAddress2(customerAddress.getLine2());
-
 
847
					insurancePolicy.setCustomerCity(customerAddress.getCity());
-
 
848
					insurancePolicy.setCustomerPinCode(customerAddress.getPinCode());
-
 
849
					insurancePolicy.setCustomerState(customerAddress.getState());
-
 
850
					
-
 
851
					GadgetCopsInsuranceModel gadgetCopsInsuranceModel = new GadgetCopsInsuranceModel();
-
 
852
					gadgetCopsInsuranceModel.setBrand(serialNumberBrand.get(entry.getKey()));
-
 
853
					gadgetCopsInsuranceModel.setModelName(serialNumberModelName.get(entry.getKey()));
-
 
854
					gadgetCopsInsuranceModel.setSerialNumber(entry.getKey());
-
 
855
					gadgetCopsInsuranceModel.setCustomerFirstName(customer.getFirstName());
-
 
856
					gadgetCopsInsuranceModel.setCustomerLastName(customer.getLastName());
-
 
857
					gadgetCopsInsuranceModel.setCustomerDateOfBirth(customerDateOfBirth);
-
 
858
					gadgetCopsInsuranceModel.setCustomerMobileNumber(customer.getMobileNumber());
-
 
859
					gadgetCopsInsuranceModel.setCustomerEmailId(customer.getEmailId());
-
 
860
					gadgetCopsInsuranceModel.setCustomerAddress1(customerAddress.getLine1());
-
 
861
					gadgetCopsInsuranceModel.setCustomerAddress2(customerAddress.getLine2());
-
 
862
					gadgetCopsInsuranceModel.setCustomerCity(customerAddress.getCity());
-
 
863
					gadgetCopsInsuranceModel.setCustomerPinCode(customerAddress.getPinCode());
-
 
864
					gadgetCopsInsuranceModel.setCustomerState(customerAddress.getState());
-
 
865
					gadgetCopsInsuranceModel.setPrice(insurancePolicy.getSellingPrice());
-
 
866
					gadgetCopsInsuranceModel.setInvoiceNumber(insurancePolicy.getInvoiceNumber());
-
 
867
					gadgetCopsInsuranceModel.setPolicyNumber(insurancePolicy.getPolicyNumber());
-
 
868
					
-
 
869
					try{
868
				try{
870
						InsuranceUtils.submitToGadgetCops(gadgetCopsInsuranceModel);
869
					InsuranceUtils.submitToGadgetCops(gadgetCopsInsuranceModel);
871
						insurancePolicy.setPosted(true);
870
					insurancePolicy.setPosted(true);
872
					}catch (ProfitMandiBusinessException profitMandiBusinessException) {
871
				}catch (ProfitMandiBusinessException profitMandiBusinessException) {
873
						LOGGER.info("Unable to submit insurance policy details to {}", insuranceProvider.getName(), profitMandiBusinessException);
872
					LOGGER.info("Unable to submit insurance policy details to {}", insuranceProvider.getName(), profitMandiBusinessException);
874
					}
-
 
875
					insurancePolicyRepository.persist(insurancePolicy);
-
 
876
				}
873
				}
-
 
874
				insurancePolicyRepository.persist(insurancePolicy);
877
			}
875
			}
878
			LOGGER.info("Order has been created successfully...");
-
 
879
			return "redirect:/get-order/?orderId="+fofoOrder.getId();
-
 
880
		}catch (Exception e) {
-
 
881
			LOGGER.error("Unable to create order : ", e);
-
 
882
			return "error";
-
 
883
		}
876
		}
-
 
877
		LOGGER.info("Order has been created successfully...");
-
 
878
		return "redirect:/get-order/?orderId="+fofoOrder.getId();
884
	}
879
	}
885
 
880
 
886
	private CustomerAddress createCustomerAddress(CustomAddress customAddress){
881
	private CustomerAddress createCustomerAddress(CustomAddress customAddress){
887
		CustomerAddress customerAddress = new CustomerAddress();
882
		CustomerAddress customerAddress = new CustomerAddress();
888
		customerAddress.setName(customAddress.getName());
883
		customerAddress.setName(customAddress.getName());