Rev 22684 | Rev 22860 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.time.DateTimeException;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.json.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.io.InputStreamResource;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.CartFofo;import com.spice.profitmandi.common.model.CustomAddress;import com.spice.profitmandi.common.model.CustomCustomer;import com.spice.profitmandi.common.model.CustomFofoLineItem;import com.spice.profitmandi.common.model.CustomFofoOrderItem;import com.spice.profitmandi.common.model.CustomInsurancePolicy;import com.spice.profitmandi.common.model.CustomRetailer;import com.spice.profitmandi.common.model.GadgetCopsInsuranceModel;import com.spice.profitmandi.common.model.GstRate;import com.spice.profitmandi.common.model.PdfModel;import com.spice.profitmandi.common.model.PriceModel;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.model.SerialNumberDetail;import com.spice.profitmandi.common.util.InsuranceUtils;import com.spice.profitmandi.common.util.PdfUtils;import com.spice.profitmandi.common.util.StringUtils;import com.spice.profitmandi.common.util.Utils;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.catalog.Item;import com.spice.profitmandi.dao.entity.dtr.GadgetCopsInsuranceCalc;import com.spice.profitmandi.dao.entity.dtr.InsurancePolicy;import com.spice.profitmandi.dao.entity.dtr.InsuranceProvider;import com.spice.profitmandi.dao.entity.dtr.PolicyNumberGenerationSequence;import com.spice.profitmandi.dao.entity.dtr.Retailer;import com.spice.profitmandi.dao.entity.dtr.User;import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;import com.spice.profitmandi.dao.entity.fofo.Customer;import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;import com.spice.profitmandi.dao.entity.fofo.FofoItemId;import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;import com.spice.profitmandi.dao.entity.fofo.FofoLineItemSerialNumber;import com.spice.profitmandi.dao.entity.fofo.FofoOrder;import com.spice.profitmandi.dao.entity.fofo.InventoryItem;import com.spice.profitmandi.dao.entity.fofo.InvoiceNumberGenerationSequence;import com.spice.profitmandi.dao.entity.fofo.PaymentOption;import com.spice.profitmandi.dao.entity.fofo.ScanRecord;import com.spice.profitmandi.dao.entity.user.Address;import com.spice.profitmandi.dao.entity.user.Counter;import com.spice.profitmandi.dao.entity.user.PrivateDealUser;import com.spice.profitmandi.dao.enumuration.fofo.ScanType;import com.spice.profitmandi.dao.repository.catalog.ItemRepository;import com.spice.profitmandi.dao.repository.dtr.InsurancePolicyRepository;import com.spice.profitmandi.dao.repository.dtr.InsuranceProviderRepository;import com.spice.profitmandi.dao.repository.dtr.PolicyNumberGenerationSequenceRepository;import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;import com.spice.profitmandi.dao.repository.dtr.UserRepository;import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;import com.spice.profitmandi.dao.repository.fofo.CustomerAddressRepository;import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;import com.spice.profitmandi.dao.repository.fofo.FofoLineItemSerialNumberRepository;import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;import com.spice.profitmandi.dao.repository.fofo.InvoiceNumberGenerationSequenceRepository;import com.spice.profitmandi.dao.repository.fofo.PaymentOptionRepository;import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;import com.spice.profitmandi.dao.repository.transaction.OrderRepository;import com.spice.profitmandi.dao.repository.user.AddressRepository;import com.spice.profitmandi.dao.repository.user.CounterRepository;import com.spice.profitmandi.dao.repository.user.PrivateDealUserRepository;import com.spice.profitmandi.service.pricing.PricingService;import com.spice.profitmandi.web.model.LoginDetails;import com.spice.profitmandi.web.request.CreateOrderRequest;import com.spice.profitmandi.web.request.CustomPaymentOption;import com.spice.profitmandi.web.response.ItemIdQuantityAvailability;import com.spice.profitmandi.web.response.Quantity;import com.spice.profitmandi.web.util.CookiesProcessor;import com.spice.profitmandi.web.util.MVCResponseSender;import in.shop2020.model.v1.catalog.ItemType;@Controller@Transactional(rollbackFor=Throwable.class)public class OrderController {private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);@AutowiredOrderRepository orderRepository;@AutowiredInventoryItemRepository inventoryItemRepository;@AutowiredCurrentInventorySnapshotRepository currentInventorySnapshotRepository;@AutowiredInvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;@AutowiredCustomerRepository customerRepository;@AutowiredAddressRepository addressRepository;@AutowiredFofoLineItemSerialNumberRepository fofoLineItemSerialNumberRepository;@AutowiredFofoLineItemRepository fofoLineItemRepository;@AutowiredPaymentOptionRepository paymentOptionRepository;@AutowiredScanRecordRepository scanRecordRepository;@AutowiredFofoOrderRepository fofoOrderRepository;@AutowiredRetailerRepository retailerRepository;@AutowiredUserRepository userRepository;@AutowiredUserAccountRepository userAccountRepository;@AutowiredRetailerRegisteredAddressRepository retailerRegisteredAddressRepository;@AutowiredCustomerAddressRepository customerAddressRepository;@AutowiredItemRepository itemRepository;@AutowiredInsuranceProviderRepository insuranceProviderRepository;@AutowiredInsurancePolicyRepository insurancePolicyRepository;@AutowiredPolicyNumberGenerationSequenceRepository policyNumberGenerationSequenceRepository;@AutowiredMVCResponseSender mvcResponseSender;@AutowiredCookiesProcessor cookiesProcessor;@AutowiredPricingService pricingService;@AutowiredPrivateDealUserRepository privateDealUserRepository;@AutowiredCounterRepository counterRepository;@AutowiredResponseSender<?> responseSender;//private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");@RequestMapping(value = "/order")public String orderIndex(HttpServletRequest request, @RequestParam(name = "cartData") String cartData, Model model) throws Exception{LoginDetails loginDetails = null;try {loginDetails = cookiesProcessor.getCookiesObject(request);} catch (ProfitMandiBusinessException e) {model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));return "response";}try{JSONObject cartObject = new JSONObject(cartData);Iterator<?> keys = cartObject.keys();Set<Integer> itemIds = new HashSet<>();List<CartFofo> cartItems = new ArrayList<CartFofo>();while( keys.hasNext() ) {String key = (String)keys.next();if ( cartObject.get(key) instanceof JSONObject ) {System.out.println(cartObject.get(key));}CartFofo cf = new CartFofo();cf.setItemId(cartObject.getJSONObject(key).getInt("itemId"));cf.setQuantity(cartObject.getJSONObject(key).getInt("quantity"));if (cf.getQuantity() <= 0){continue;}cartItems.add(cf);itemIds.add(cartObject.getJSONObject(key).getInt("itemId"));}Map<Integer, Item> itemMap = new HashMap<Integer, Item>();if (itemIds.size() > 0){List<Item> items = itemRepository.selectByIds(itemIds);for (Item i : items){itemMap.put(i.getId(), i);}}for (CartFofo cf : cartItems){Item i = itemMap.get(cf.getItemId());if (i == null){continue;}cf.setDisplayName(getValidName(i.getBrand())+" "+getValidName(i.getModelName())+" "+getValidName(i.getModelNumber())+" "+getValidName(i.getColor()).replaceAll("\\s+", " "));cf.setItemType(i.getType());}Map<Integer, PriceModel> mopPriceMap = pricingService.getPurchasePriceMopPriceNotFound(itemIds, loginDetails.getFofoId());LOGGER.info("mopPriceMap {}", mopPriceMap);model.addAttribute("cartObj", cartItems);model.addAttribute("mopPriceMap", mopPriceMap);return "order-index";}catch (Exception e) {LOGGER.error("Unable to Prepare cart to place order...", e);return "error";}}private String getValidName(String name){return name!=null?name:"";}@RequestMapping(value = "/insurancePrices", method = RequestMethod.GET)public ResponseEntity<?> getInsurancePrices(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PRICE) float price){LOGGER.info("Request received at url : {}", request.getRequestURI());try{Set<Float> prices = new HashSet<>();prices.add(price);return responseSender.ok(pricingService.getInsurancePrices(prices, ProfitMandiConstants.GADGET_COPS));}catch(ProfitMandiBusinessException profitMandiBusinessException){return responseSender.notFound(profitMandiBusinessException);}}@RequestMapping(value = "/get-order", method = RequestMethod.GET)public String getOrder(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException, Exception{LoginDetails fofoDetails;try {fofoDetails = cookiesProcessor.getCookiesObject(request);} catch (ProfitMandiBusinessException e) {model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));return "response";}try{FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId());Customer customer = customerRepository.selectById(fofoOrder.getCustomerId());customerAddress.setPhoneNumber(customer.getMobileNumber());List<PaymentOption> paymentOptions = paymentOptionRepository.selectByOrderId(fofoOrder.getId());List<InsurancePolicy> insurancePolicies = insurancePolicyRepository.selectByRetailerInvoiceNumber(fofoOrder.getFofoId(), fofoOrder.getInvoiceNumber());model.addAttribute("fofoOrder", fofoOrder);model.addAttribute("fofoLineItems", fofoLineItems);model.addAttribute("customerBillingAddress", getBillingAddress(customerAddress));model.addAttribute("customerBillingAddressObj", customerAddress);model.addAttribute("paymentOptions", paymentOptions);model.addAttribute("insurancePolicies", insurancePolicies);return "order-details";}catch (Exception e) {LOGGER.error("Unable to get Order details...", e);return "error";}}@RequestMapping(value = "/saleDetails", method = RequestMethod.GET)public String getSaleDetails(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException, Exception{LoginDetails fofoDetails;try {fofoDetails = cookiesProcessor.getCookiesObject(request);} catch (ProfitMandiBusinessException e) {model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));return "response";}try{FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId());List<PaymentOption> paymentOptions = paymentOptionRepository.selectByOrderId(fofoOrder.getId());List<InsurancePolicy> insurancePolicies = insurancePolicyRepository.selectByRetailerInvoiceNumber(fofoOrder.getFofoId(), fofoOrder.getInvoiceNumber());model.addAttribute("fofoOrder", fofoOrder);model.addAttribute("fofoLineItems", fofoLineItems);model.addAttribute("customerBillingAddress", getBillingAddress(customerAddress));model.addAttribute("customerBillingAddressObj", customerAddress);model.addAttribute("paymentOptions", paymentOptions);model.addAttribute("insurancePolicies", insurancePolicies);return "sale-details";}catch (Exception e) {LOGGER.error("Unble to fetch sale details... ", e);return "error";}}private String getBillingAddress(CustomerAddress customerAddress) {String address = "";if ((customerAddress.getLine1() != null) && (!customerAddress.getLine1().isEmpty())) {address = address + customerAddress.getLine1();address = address + ", ";}if ((customerAddress.getLine2() != null) && (!customerAddress.getLine2().isEmpty())) {address = address + customerAddress.getLine2();address = address + ", ";}if ((customerAddress.getLandmark() != null) && (!customerAddress.getLandmark().isEmpty())) {address = address + customerAddress.getLandmark();address = address + ", ";}if ((customerAddress.getCity() != null) && (!customerAddress.getCity().isEmpty())) {address = address + customerAddress.getCity();address = address + ", ";}if ((customerAddress.getState() != null) && (!customerAddress.getState().isEmpty())) {address = address + customerAddress.getState();}if ((customerAddress.getPinCode() != null) && (!customerAddress.getPinCode().isEmpty())) {address = address + "- " + customerAddress.getPinCode();}return address;}@RequestMapping(value = "/create-order", method = RequestMethod.POST)public String createOrder(HttpServletRequest request, @RequestBody CreateOrderRequest createOrderRequest, Model model) throws Throwable{LOGGER.info("request at uri {} body {}", request.getRequestURI(), createOrderRequest);LoginDetails fofoDetails;try {fofoDetails = cookiesProcessor.getCookiesObject(request);} catch (ProfitMandiBusinessException e) {model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));return "response";}Set<Integer> itemIds = new HashSet<>();Map<Integer, Integer> itemIdQuantity = new HashMap<>(); //this is for errorMap<Integer, List<CustomFofoLineItem>> customFofoLineItemMap = new HashMap<>();Map<Integer, Float> lineItemPrice = new HashMap<>(); //this is for pricing errorMap<Integer, Set<String>> itemIdSerialNumbers = new HashMap<>(); //float totalAmount = 0;for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){itemIds.add(customFofoLineItem.getItemId());if(!customFofoLineItem.getSerialNumberDetails().isEmpty() && customFofoLineItem.getQuantity() != customFofoLineItem.getSerialNumberDetails().size()){itemIdQuantity.put(customFofoLineItem.getItemId(), customFofoLineItem.getQuantity());}if(!(customFofoLineItem.getSellingPrice() > 0)){lineItemPrice.put(customFofoLineItem.getItemId(), customFofoLineItem.getSellingPrice());}else{totalAmount = totalAmount + customFofoLineItem.getSellingPrice() * customFofoLineItem.getQuantity() - customFofoLineItem.getDiscountAmount();for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){if(serialNumberDetail.isInsurance() && serialNumberDetail.getAmount() > 0){totalAmount = totalAmount + serialNumberDetail.getAmount();}}}List<CustomFofoLineItem> customFofoLineItems;if(customFofoLineItemMap.containsKey(customFofoLineItem.getItemId())) {customFofoLineItems = customFofoLineItemMap.get(customFofoLineItem.getItemId());} else {customFofoLineItems = new ArrayList<>();}customFofoLineItemMap.put(customFofoLineItem.getItemId(), customFofoLineItems);Set<String> serialNumbers;if(!itemIdSerialNumbers.containsKey(customFofoLineItem.getItemId())){serialNumbers = new HashSet<>();} else {serialNumbers = itemIdSerialNumbers.get(customFofoLineItem.getItemId());}for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){serialNumbers.add(serialNumberDetail.getSerialNumber());}itemIdSerialNumbers.put(customFofoLineItem.getItemId(), serialNumbers);}if(!itemIdQuantity.isEmpty()){// if item quantity does not match with given serialnumbers sizeLOGGER.error("itemId's quantity should be equal to given serialnumber size {} ", itemIdQuantity);throw new ProfitMandiBusinessException("itemIdQuantity", itemIdQuantity, "");//return "error";}try{this.validatePaymentOptionsAndTotalAmount(createOrderRequest.getPaymentOptions(), totalAmount);}catch(ProfitMandiBusinessException profitMandiBusinessException){LOGGER.error("Error occured while validating payment options : ", profitMandiBusinessException);throw profitMandiBusinessException;}if(!lineItemPrice.isEmpty()){// given fofo line item price must be greater than zeroLOGGER.error("requested itemId's selling price must greater than 0");throw new ProfitMandiBusinessException(ProfitMandiConstants.PRICE, lineItemPrice, "");}List<CurrentInventorySnapshot> currentInventorySnapshots = currentInventorySnapshotRepository.selectByFofoItemIds(fofoDetails.getFofoId(), itemIds);if(itemIds.size() != currentInventorySnapshots.size()){// error}List<ItemIdQuantityAvailability> itemIdQuantityAvailabilities = new ArrayList<>(); //this is for errorLOGGER.info("currentInventorySnapshots "+currentInventorySnapshots);for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){List<CustomFofoLineItem> customFofoLineItems = customFofoLineItemMap.get(currentInventorySnapshot.getId().getItemId());LOGGER.info("customFofoLineItems "+customFofoLineItems);for (CustomFofoLineItem customFofoLineItem : customFofoLineItems) {if(customFofoLineItem.getQuantity() > currentInventorySnapshot.getAvailability()){ItemIdQuantityAvailability itemIdQuantityAvailability = new ItemIdQuantityAvailability();itemIdQuantityAvailability.setItemId(customFofoLineItem.getItemId());Quantity quantity = new Quantity();quantity.setAvailable(currentInventorySnapshot.getAvailability());quantity.setRequested(customFofoLineItem.getQuantity());itemIdQuantityAvailability.setQuantity(quantity);itemIdQuantityAvailabilities.add(itemIdQuantityAvailability);}}}if(!itemIdQuantityAvailabilities.isEmpty()){// itemIdQuantity request is not validLOGGER.error("Requested quantities should not be greater than currently available quantities {}", itemIdQuantityAvailabilities);throw new ProfitMandiBusinessException("itemIdQuantityAvailabilities", itemIdQuantityAvailabilities, "");}Map<Integer, Item> itemMap = new HashMap<Integer, Item>();List<Item> items = itemRepository.selectByIds(itemIds);for (Item i : items){itemMap.put(i.getId(), i);}Set<Integer> nonSerializedItemIds = new HashSet<>();Set<String> serialNumbers = new HashSet<>();Map<String, Float> insuranceSerialNumberItemPrice = new HashMap<>();Map<String, Float> insuranceSerialNumberSaleAmount = new HashMap<>();Map<String, String> serialNumberModelName = new HashMap<>();Map<String, String> serialNumberBrand = new HashMap<>();for (CustomFofoLineItem cli : createOrderRequest.getFofoLineItems()){Item item = itemMap.get(cli.getItemId());if (item.getType().equals(ItemType.SERIALIZED)){for (SerialNumberDetail serialNumberDetail : cli.getSerialNumberDetails()){serialNumbers.add(serialNumberDetail.getSerialNumber());if(serialNumberDetail.isInsurance()){insuranceSerialNumberItemPrice.put(serialNumberDetail.getSerialNumber(), cli.getSellingPrice());insuranceSerialNumberSaleAmount.put(serialNumberDetail.getSerialNumber(), serialNumberDetail.getAmount());serialNumberModelName.put(serialNumberDetail.getSerialNumber(), item.getModelName());serialNumberBrand.put(serialNumberDetail.getSerialNumber(), item.getBrand());}}}else{nonSerializedItemIds.add(cli.getItemId());}}Map<Integer, List<InventoryItem>> serializedInventoryItemMap = new HashMap<Integer, List<InventoryItem>>();Map<Integer, List<InventoryItem>> nonSerializedInventoryItemMap = new HashMap<Integer, List<InventoryItem>>();Map<Integer, List<Float>> itemIdPriceDropAmount = new HashMap<>();//Map<String, Float> serialNumberItemPrice = new HashMap<>();if (!serialNumbers.isEmpty()){List<InventoryItem> serializedInventoryItems = inventoryItemRepository.selectByFofoIdSerialNumbers(fofoDetails.getFofoId(), serialNumbers);LOGGER.info("serializedInventoryItems {}", serializedInventoryItems);for (InventoryItem it : serializedInventoryItems){if (it.getGoodQuantity() == 1){if (serializedInventoryItemMap.containsKey(it.getItemId())){serializedInventoryItemMap.get(it.getItemId()).add(it);itemIdPriceDropAmount.get(it.getItemId()).add(it.getUnitPrice() - (it.getPriceDropAmount()==null?0:it.getPriceDropAmount()));}else{ArrayList<InventoryItem> tmp = new ArrayList<InventoryItem>();tmp.add(it);serializedInventoryItemMap.put(it.getItemId(), tmp);ArrayList<Float> priceDropAmouts = new ArrayList<>();priceDropAmouts.add(it.getUnitPrice() - (it.getPriceDropAmount()==null?0:it.getPriceDropAmount()));itemIdPriceDropAmount.put(it.getItemId(), priceDropAmouts);}}}}if (!nonSerializedItemIds.isEmpty()){List<InventoryItem> nonSerializedInventoryItems = inventoryItemRepository.selectByFofoIdItemIds(fofoDetails.getFofoId(), nonSerializedItemIds);for (InventoryItem it : nonSerializedInventoryItems){if (it.getGoodQuantity() > 0){if (nonSerializedInventoryItemMap.containsKey(it.getItemId())){nonSerializedInventoryItemMap.get(it.getItemId()).add(it);}else{ArrayList<InventoryItem> tmp = new ArrayList<InventoryItem>();tmp.add(it);nonSerializedInventoryItemMap.put(it.getItemId(), tmp);}}}}List<Integer> invalidItemIdSerialNumbers = new ArrayList<Integer>();List<Integer> itemIdNonSerializedSerialNumbers = new ArrayList<Integer>();for (Item i : items){List<CustomFofoLineItem> customFofoLineItems = customFofoLineItemMap.get(i.getId());for(CustomFofoLineItem customFofoLineItem: customFofoLineItems) {if (i.getType().equals(ItemType.SERIALIZED)){if (customFofoLineItem ==null || customFofoLineItem.getSerialNumberDetails().isEmpty()){invalidItemIdSerialNumbers.add(i.getId());}}else{if (customFofoLineItem == null || !customFofoLineItem.getSerialNumberDetails().isEmpty()){itemIdNonSerializedSerialNumbers.add(i.getId());}}}}if(!invalidItemIdSerialNumbers.isEmpty()){LOGGER.error("Invalid itemId's serialNumbers {}", invalidItemIdSerialNumbers);// itemId's are serialized you are saying these are not serializedthrow new ProfitMandiBusinessException("invalidItemIdSerialNumbers", invalidItemIdSerialNumbers, "");}if(!itemIdNonSerializedSerialNumbers.isEmpty()){LOGGER.error("Invalid itemId's serialNumbers {}", itemIdNonSerializedSerialNumbers);// itemId's are non serialized you are saying these are serializedthrow new ProfitMandiBusinessException("itemIdNonSerializedSerialNumbers", itemIdNonSerializedSerialNumbers, "");}if(items.size() != itemIds.size()){LOGGER.error("Requested ItemIds not found in catalog");// invalid itemIdsthrow new ProfitMandiBusinessException("invalidItemIds", "", "");}Map<Integer, List<InventoryItem>> inventoryItemsToBill = new HashMap<Integer,List<InventoryItem>>();Map<Integer, Integer> inventoryItemQuantityUsed = new HashMap<>(); //to keep track of inventoryitem quanity used for scan records insertionLOGGER.info("itemMap keys {}", itemMap.keySet());//Lets reduce quantity and decide what inventory items to use.for (Item i : items){if (i.getType().equals(ItemType.SERIALIZED)){//TODO:handle nullif (serializedInventoryItemMap.get(i.getId()) == null || customFofoLineItemMap.get(i.getId()).size() != serializedInventoryItemMap.get(i.getId()).size()){//not enough serial numbers//LOGGER.info("serialNumbers {}", serialNumbers);LOGGER.info("serializedInventoryItemMap {}", serializedInventoryItemMap);LOGGER.info("itemId {}", i.getId());LOGGER.error("not enough serial numbers");throw new ProfitMandiBusinessException("notEnoughSerialNumbers", "", "");}List<InventoryItem> inventoryItemsSerializedserialized = serializedInventoryItemMap.get(i.getId());for (InventoryItem it : inventoryItemsSerializedserialized){it.setGoodQuantity(0);inventoryItemQuantityUsed.put(it.getId(), 1);}inventoryItemsToBill.put(i.getId(), inventoryItemsSerializedserialized);}else{List<InventoryItem> inventoryItemsNonSerialized = nonSerializedInventoryItemMap.get(i.getId());int quantityToBill = customFofoLineItemMap.get(i.getId()).get(0).getQuantity();int totalLeft = quantityToBill;List<InventoryItem> inventoryItemsNonSerializedUsed = new ArrayList<InventoryItem>();if (inventoryItemsNonSerialized!=null){for (InventoryItem it : inventoryItemsNonSerialized){if (totalLeft > 0){int toUse = Math.min(totalLeft, it.getGoodQuantity());inventoryItemQuantityUsed.put(it.getId(), toUse);it.setGoodQuantity(it.getGoodQuantity() - toUse);totalLeft = totalLeft - toUse;inventoryItemsNonSerializedUsed.add(it);}}}if (totalLeft > 0){//not enough quanity for non-serializedLOGGER.error("not enough quanity for non-serialized");throw new ProfitMandiBusinessException("notEnoughQuantityForNonSerialized", "", "");}inventoryItemsToBill.put(i.getId(), inventoryItemsNonSerializedUsed);}}// mop price validationMap<Integer, Float> invalidMopItemIdPriceMap = new HashMap<>();Map<Integer, Float> invalidDiscountAmountMap = new HashMap<>();Map<Integer, PriceModel> itemIdMopPriceMap = pricingService.getPurchasePriceMopPriceNotFound(itemIds, fofoDetails.getFofoId());for(Map.Entry<Integer, PriceModel> entry : itemIdMopPriceMap.entrySet()){List<CustomFofoLineItem>customFofoLineItems = customFofoLineItemMap.get(entry.getKey());for(CustomFofoLineItem customFofoLineItem : customFofoLineItems) {if(entry.getValue().getPrice() < Float.MAX_VALUE && customFofoLineItem.getSellingPrice() < entry.getValue().getPrice()){invalidMopItemIdPriceMap.put(entry.getKey(), customFofoLineItem.getSellingPrice());}if(entry.getValue().isMop() && customFofoLineItem.getDiscountAmount() > entry.getValue().getMaxDiscountAmount()){invalidDiscountAmountMap.put(entry.getKey(), customFofoLineItem.getDiscountAmount());}}}if(!invalidMopItemIdPriceMap.isEmpty()){LOGGER.error("Invalid itemIds selling prices{} should be greater than mop prices {}", invalidMopItemIdPriceMap, itemIdMopPriceMap);throw new ProfitMandiBusinessException("invalidMopItemIdPrice", invalidMopItemIdPriceMap, "");}if(!invalidMopItemIdPriceMap.isEmpty()){LOGGER.error("Invalid itemIds discount amounts {} should be less than maxDiscount prices {}", invalidDiscountAmountMap, itemIdMopPriceMap);throw new ProfitMandiBusinessException("invalidMopItemIdPrice", invalidDiscountAmountMap, "");}InvoiceNumberGenerationSequence invoiceNumberGenerationSequence = null;try{invoiceNumberGenerationSequence = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoDetails.getFofoId());invoiceNumberGenerationSequence.setSequence(invoiceNumberGenerationSequence.getSequence() + 1);invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);}catch(ProfitMandiBusinessException profitMandiBusinessException){invoiceNumberGenerationSequence = new InvoiceNumberGenerationSequence();invoiceNumberGenerationSequence.setFofoId(fofoDetails.getFofoId());invoiceNumberGenerationSequence.setPrefix("INVOICE");invoiceNumberGenerationSequence.setSequence(1);invoiceNumberGenerationSequenceRepository.persist(invoiceNumberGenerationSequence);}CustomCustomer customCustomer = createOrderRequest.getCustomer();if(!StringUtils.isValidEmailAddress(customCustomer.getEmailId())){LOGGER.error("invalid customer emailId {} ", customCustomer.getEmailId());throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, customCustomer.getEmailId(), "");}if(!StringUtils.isValidMobile(customCustomer.getMobileNumber())){LOGGER.error("invalid customer mobileNumber {} ", customCustomer.getMobileNumber());throw new ProfitMandiBusinessException(ProfitMandiConstants.MOBILE_NUMBER, customCustomer.getMobileNumber(), "");}boolean insurance = false;for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){if(serialNumberDetail.getAmount() > 0){insurance = true;break;}}}LOGGER.info("insurance [{}] processing ....", insurance);LocalDate customerDateOfBirth = null;if(insurance){try{customerDateOfBirth = StringUtils.toDate(createOrderRequest.getCustomerDateOfBirth());}catch(DateTimeException dateTimeException){LOGGER.error("Unable to parse dateOfBirth", dateTimeException);throw new ProfitMandiBusinessException("dateOfBirth", createOrderRequest.getCustomerDateOfBirth(), "");}}Customer customer = null;try{customer = customerRepository.selectByMobileNumber(customCustomer.getMobileNumber());}catch(ProfitMandiBusinessException profitMandiBusinessException){LOGGER.error("Error : ", profitMandiBusinessException);customer = new Customer();customer.setFirstName(customCustomer.getFirstName());customer.setLastName(customCustomer.getLastName());customer.setEmailId(customCustomer.getEmailId());customer.setMobileNumber(customCustomer.getMobileNumber());customerRepository.persist(customer);}//TODO:Check if createOrderRequest contains addressIdCustomerAddress customerAddress = this.createCustomerAddress(customCustomer.getAddress());customerAddress.setCustomerId(customer.getId());customerAddressRepository.persist(customerAddress);FofoOrder fofoOrder = new FofoOrder();fofoOrder.setCustomerId(customer.getId());fofoOrder.setFofoId(fofoDetails.getFofoId());fofoOrder.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());fofoOrder.setTotalAmount(totalAmount);fofoOrder.setCustomerAddressId(customerAddress.getId());fofoOrderRepository.persist(fofoOrder);for(CustomPaymentOption customPaymentOption : createOrderRequest.getPaymentOptions()){PaymentOption paymentOption = new PaymentOption();paymentOption.setOrderId(fofoOrder.getId());paymentOption.setAmount(customPaymentOption.getAmount());paymentOption.setType(customPaymentOption.getType());paymentOptionRepository.persist(paymentOption);}Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(fofoDetails.getFofoId()));Map<String, GstRate> gstRateMap = null;if(retailerAddress.getState().equals(customerAddress.getState())){gstRateMap = Utils.getGstRates(retailerAddress.getState());}else{LOGGER.info("inter gstRate = true");gstRateMap = Utils.getInterGstRates();}LOGGER.info("gstRateMap {}", gstRateMap);for(CustomFofoLineItem customFofoLineItem : createOrderRequest.getFofoLineItems()){FofoLineItem fofoLineItem = new FofoLineItem();fofoLineItem.setItemId(customFofoLineItem.getItemId());fofoLineItem.setQuantity(customFofoLineItem.getQuantity());fofoLineItem.setSellingPrice(customFofoLineItem.getSellingPrice());fofoLineItem.setOrderId(fofoOrder.getId());fofoLineItem.setDp(customFofoLineItem.getSellingPrice());fofoLineItem.setDiscount(customFofoLineItem.getDiscountAmount());Item item = itemMap.get(customFofoLineItem.getItemId());GstRate gstRate = gstRateMap.get(item.getHsnCode());fofoLineItem.setIgstRate(gstRate.getIgstRate());fofoLineItem.setCgstRate(gstRate.getCgstRate());fofoLineItem.setSgstRate(gstRate.getSgstRate());fofoLineItem.setHsnCode(gstRate.getHsnCode());List<Float> priceDropAmounts = itemIdPriceDropAmount.get(customFofoLineItem.getItemId());float cost = 0;if (priceDropAmounts!=null){for (Float pda : priceDropAmounts){cost = cost + pda;}}else{cost = customFofoLineItem.getSellingPrice()* customFofoLineItem.getQuantity();}fofoLineItem.setCost(cost);fofoLineItem.setBrand(item.getBrand());fofoLineItem.setModelName(item.getModelName());fofoLineItem.setModelNumber(item.getModelNumber());fofoLineItem.setColor(item.getColor());fofoLineItemRepository.persist(fofoLineItem);LOGGER.info("\n\n");if(!customFofoLineItem.getSerialNumberDetails().isEmpty()){for(SerialNumberDetail serialNumberDetail : customFofoLineItem.getSerialNumberDetails()){FofoLineItemSerialNumber fofoLineItemSerialNumber = new FofoLineItemSerialNumber();fofoLineItemSerialNumber.setFofoLineItemId(fofoLineItem.getId());fofoLineItemSerialNumber.setSerialNumber(serialNumberDetail.getSerialNumber());fofoLineItemSerialNumberRepository.persist(fofoLineItemSerialNumber);}}for(CurrentInventorySnapshot currentInventorySnapshot : currentInventorySnapshots){FofoItemId fofoItemId = new FofoItemId();fofoItemId.setFofoId(fofoDetails.getFofoId());fofoItemId.setItemId(fofoLineItem.getItemId());if(currentInventorySnapshot.getId().equals(fofoItemId)){currentInventorySnapshotRepository.updateAvailabilityByFofoItemId(fofoItemId, currentInventorySnapshot.getAvailability() - customFofoLineItem.getQuantity());}}List<InventoryItem> inventoryItems = inventoryItemsToBill.get(fofoLineItem.getItemId());for(InventoryItem inventoryItem : inventoryItems){inventoryItem.setLastScanType(ScanType.SALE);inventoryItemRepository.persist(inventoryItem);ScanRecord scanRecord = new ScanRecord();scanRecord.setInventoryItemId(inventoryItem.getId());scanRecord.setFofoId(fofoDetails.getFofoId());//correct thisscanRecord.setQuantity(inventoryItemQuantityUsed.get(inventoryItem.getId()));scanRecord.setType(ScanType.SALE);scanRecordRepository.persist(scanRecord);}}// insurance calculation is insurance flag is enabledif(!insuranceSerialNumberItemPrice.isEmpty()){LOGGER.info("Processing for insurence for serialNumbers");Map<Float, GadgetCopsInsuranceCalc> insurancePricesMap = pricingService.getInsurancePrices(new HashSet<>(insuranceSerialNumberItemPrice.values()), ProfitMandiConstants.GADGET_COPS);InsuranceProvider insuranceProvider = insuranceProviderRepository.selectByName(ProfitMandiConstants.GADGET_COPS);Map<Float, Float> invalidInsurancePurchaseSaleAmount = new HashMap<>();Map<Float, Float> invalidInsuranceSalePurchaseAmount = new HashMap<>();for(Map.Entry<String, Float> entry : insuranceSerialNumberItemPrice.entrySet()){if(insuranceSerialNumberSaleAmount.get(entry.getKey()) < insurancePricesMap.get(entry.getValue()).getDealerPrice()){invalidInsurancePurchaseSaleAmount.put(insurancePricesMap.get(entry.getValue()).getDealerPrice(), insuranceSerialNumberSaleAmount.get(entry.getKey()));}if(insuranceSerialNumberSaleAmount.get(entry.getKey()) > insurancePricesMap.get(entry.getValue()).getSellingPrice()){invalidInsuranceSalePurchaseAmount.put(insuranceSerialNumberSaleAmount.get(entry.getKey()), insurancePricesMap.get(entry.getValue()).getDealerPrice());}}// insurance sale amount can not be lesser than insurance purchase amountif(!invalidInsurancePurchaseSaleAmount.isEmpty()){LOGGER.error("Invalid Insurance prices [{}], insurance sale amount can not be lesser than insurance purchase amount", invalidInsurancePurchaseSaleAmount);return "error";}if(!invalidInsuranceSalePurchaseAmount.isEmpty()){LOGGER.error("Invalid Insurance prices [{}], insurance sale amount can not be greater than than insurance max amount", invalidInsuranceSalePurchaseAmount);return "error";}Map<Float, Float> invalidInsuranceMarginAmount = new HashMap<>();for(Map.Entry<String, Float> entry : insuranceSerialNumberItemPrice.entrySet()){int itemId = this.getItemIdFromSerialNumber(itemIdSerialNumbers, entry.getKey());LOGGER.info("itemId -->{}", itemId);LOGGER.info("itemIdMopPriceMap.get(itemId) -->{}", itemIdMopPriceMap.get(itemId));LOGGER.info("itemIdSerialNumbers -->{}", itemIdSerialNumbers);LOGGER.info("entry -->{}, {}", entry.getKey(), entry.getValue());float itemPurchasePrice = itemIdMopPriceMap.get(itemId).getPrice();float itemSellingPrice = entry.getValue();float itemMargin = itemSellingPrice - itemPurchasePrice;float insurancePurchasePrice = insurancePricesMap.get(entry.getValue()).getDealerPrice();float insuranceSellingPrice = insuranceSerialNumberSaleAmount.get(entry.getKey());float insuranceMargin = insuranceSellingPrice - insurancePurchasePrice;if(insuranceMargin < (itemMargin * 30 / 100)){invalidInsuranceMarginAmount.put(insuranceMargin, (itemMargin * 30 / 100));}}if(!invalidInsuranceMarginAmount.isEmpty()){LOGGER.error("insurance marging should be greater than equal to item profit margin insuranceMarginItemIdMargin {}", invalidInsuranceMarginAmount);return "error";}for(Map.Entry<String, Float> entry : insuranceSerialNumberItemPrice.entrySet()){PolicyNumberGenerationSequence policyNumberGenerationSequence = null;try{policyNumberGenerationSequence = policyNumberGenerationSequenceRepository.select();policyNumberGenerationSequence.setSequence(policyNumberGenerationSequence.getSequence() + 1);policyNumberGenerationSequenceRepository.persist(policyNumberGenerationSequence);}catch(ProfitMandiBusinessException profitMandiBusinessException){policyNumberGenerationSequence = new PolicyNumberGenerationSequence();policyNumberGenerationSequence.setSequence(1);policyNumberGenerationSequenceRepository.persist(policyNumberGenerationSequence);}InsurancePolicy insurancePolicy = new InsurancePolicy();insurancePolicy.setInvoiceNumber(invoiceNumberGenerationSequence.getPrefix() + invoiceNumberGenerationSequence.getSequence());insurancePolicy.setRetailerId(fofoDetails.getFofoId());insurancePolicy.setPurchaseAmount(insurancePricesMap.get(entry.getValue()).getDealerPrice());insurancePolicy.setSaleAmount(insuranceSerialNumberSaleAmount.get(entry.getKey()));insurancePolicy.setSellingPrice(entry.getValue());insurancePolicy.setSerialNumber(entry.getKey());insurancePolicy.setModelName(serialNumberModelName.get(entry.getKey()));insurancePolicy.setBrand(serialNumberBrand.get(entry.getKey()));insurancePolicy.setPolicyNumber(StringUtils.generatePolicyNumber(ProfitMandiConstants.POLICY_NUMBER_PREFIX, policyNumberGenerationSequence.getSequence()));insurancePolicy.setProviderId(insuranceProvider.getId());insurancePolicy.setCustomerFirstName(customer.getFirstName());insurancePolicy.setCustomerLastName(customer.getLastName());insurancePolicy.setCustomerMobileNumber(customer.getMobileNumber());insurancePolicy.setCustomerEmailId(customer.getEmailId());insurancePolicy.setCustomerDateOfBirth(customerDateOfBirth);insurancePolicy.setCustomerAddress1(customerAddress.getLine1());insurancePolicy.setCustomerAddress2(customerAddress.getLine2());insurancePolicy.setCustomerCity(customerAddress.getCity());insurancePolicy.setCustomerPinCode(customerAddress.getPinCode());insurancePolicy.setCustomerState(customerAddress.getState());GadgetCopsInsuranceModel gadgetCopsInsuranceModel = new GadgetCopsInsuranceModel();gadgetCopsInsuranceModel.setBrand(serialNumberBrand.get(entry.getKey()));gadgetCopsInsuranceModel.setModelName(serialNumberModelName.get(entry.getKey()));gadgetCopsInsuranceModel.setSerialNumber(entry.getKey());gadgetCopsInsuranceModel.setCustomerFirstName(customer.getFirstName());gadgetCopsInsuranceModel.setCustomerLastName(customer.getLastName());gadgetCopsInsuranceModel.setCustomerDateOfBirth(customerDateOfBirth);gadgetCopsInsuranceModel.setCustomerMobileNumber(customer.getMobileNumber());gadgetCopsInsuranceModel.setCustomerEmailId(customer.getEmailId());gadgetCopsInsuranceModel.setCustomerAddress1(customerAddress.getLine1());gadgetCopsInsuranceModel.setCustomerAddress2(customerAddress.getLine2());gadgetCopsInsuranceModel.setCustomerCity(customerAddress.getCity());gadgetCopsInsuranceModel.setCustomerPinCode(customerAddress.getPinCode());gadgetCopsInsuranceModel.setCustomerState(customerAddress.getState());gadgetCopsInsuranceModel.setPrice(insurancePolicy.getSellingPrice());gadgetCopsInsuranceModel.setInvoiceNumber(insurancePolicy.getInvoiceNumber());gadgetCopsInsuranceModel.setPolicyNumber(insurancePolicy.getPolicyNumber());try{InsuranceUtils.submitToGadgetCops(gadgetCopsInsuranceModel);insurancePolicy.setPosted(true);}catch (ProfitMandiBusinessException profitMandiBusinessException) {LOGGER.info("Unable to submit insurance policy details to {}", insuranceProvider.getName(), profitMandiBusinessException);}insurancePolicyRepository.persist(insurancePolicy);}}LOGGER.info("Order has been created successfully...");return "redirect:/get-order/?orderId="+fofoOrder.getId();}private int getItemIdFromSerialNumber(Map<Integer, Set<String>> itemIdSerialNumbers, String serialNumber){for(Map.Entry<Integer, Set<String>> entry : itemIdSerialNumbers.entrySet()){if(entry.getValue().contains(serialNumber)){return entry.getKey();}}return 0;}private CustomerAddress createCustomerAddress(CustomAddress customAddress){CustomerAddress customerAddress = new CustomerAddress();customerAddress.setName(customAddress.getName());customerAddress.setLine1(customAddress.getLine1());customerAddress.setLine2(customAddress.getLine2());customerAddress.setLandmark(customAddress.getLandmark());customerAddress.setCity(customAddress.getCity());customerAddress.setPinCode(customAddress.getPinCode());customerAddress.setState(customAddress.getState());customerAddress.setCountry(customAddress.getCountry());customerAddress.setPhoneNumber(customAddress.getPhoneNumber());return customerAddress;}private CustomAddress createCustomAddress(Address address){CustomAddress customAddress = new CustomAddress();customAddress.setName(address.getName());customAddress.setLine1(address.getLine1());customAddress.setLine2(address.getLine2());customAddress.setLandmark(address.getLandmark());customAddress.setCity(address.getCity());customAddress.setPinCode(address.getPinCode());customAddress.setState(address.getState());customAddress.setCountry(address.getCountry());customAddress.setPhoneNumber(address.getPhoneNumber());return customAddress;}private CustomAddress createCustomAddress(CustomerAddress customerAddress){CustomAddress customAddress = new CustomAddress();customAddress.setName(customerAddress.getName());customAddress.setLine1(customerAddress.getLine1());customAddress.setLine2(customerAddress.getLine2());customAddress.setLandmark(customerAddress.getLandmark());customAddress.setCity(customerAddress.getCity());customAddress.setPinCode(customerAddress.getPinCode());customAddress.setState(customerAddress.getState());customAddress.setCountry(customerAddress.getCountry());customAddress.setPhoneNumber(customerAddress.getPhoneNumber());return customAddress;}private void validatePaymentOptionsAndTotalAmount(Set<CustomPaymentOption> customPaymentOptions, float totalAmount) throws ProfitMandiBusinessException{float calculatedAmount = 0;Set<String> paymentOptionTypes = new HashSet<>();for(CustomPaymentOption customPaymentOption : customPaymentOptions){if(paymentOptionTypes.contains(customPaymentOption.getType().name())){throw new ProfitMandiBusinessException(ProfitMandiConstants.PAYMENT_OPTION_TYPE, customPaymentOption.getType().name(), "");}else{paymentOptionTypes.add(customPaymentOption.getType().name());calculatedAmount = calculatedAmount + customPaymentOption.getAmount();}}if(calculatedAmount != totalAmount){LOGGER.error("PaymentOptionCalculatedAmount [{}] != TotalAmount [{}]", calculatedAmount, totalAmount);throw new ProfitMandiBusinessException(ProfitMandiConstants.PAYMENT_OPTION_CALCULATED_AMOUNT, calculatedAmount, "");}}@RequestMapping(value = "/generateInvoice")public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId) throws Throwable{LOGGER.info("Request received at url {} with params [{}={}] ", request.getRequestURI(), ProfitMandiConstants.ORDER_ID, orderId);LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(fofoDetails.getFofoId(), orderId);PdfModel pdfModel = new PdfModel();pdfModel.setAuther("profitmandi");pdfModel.setTitle("Retailer Invoice");pdfModel.setInvoiceDate(fofoOrder.getFormattedDate());// insurance calculationList<InsurancePolicy> insurancePolicies = insurancePolicyRepository.selectByRetailerInvoiceNumber(fofoDetails.getFofoId(), fofoOrder.getInvoiceNumber());Set<CustomInsurancePolicy> customInsurancePolicies = new HashSet<>();final float totalInsuranceTaxRate = 18;for(InsurancePolicy insurancePolicy : insurancePolicies){float taxableInsurancePrice = insurancePolicy.getSaleAmount() / (1 + totalInsuranceTaxRate / 100);CustomInsurancePolicy customInsurancePolicy = new CustomInsurancePolicy();customInsurancePolicy.setDescription("Damage Protection Plan for device IMEI #" + insurancePolicy.getSerialNumber() + "\n Certificate No. " + insurancePolicy.getPolicyNumber());customInsurancePolicy.setHsnCode("998716");customInsurancePolicy.setRate(taxableInsurancePrice);customInsurancePolicy.setIgstRate(18);customInsurancePolicy.setIgstAmount(taxableInsurancePrice * 18 /100);customInsurancePolicy.setCgstRate(18);customInsurancePolicy.setCgstAmount(taxableInsurancePrice * 9 /100);customInsurancePolicy.setSgstRate(9);customInsurancePolicy.setSgstAmount(taxableInsurancePrice * 9 /100);customInsurancePolicy.setNetAmount(insurancePolicy.getSaleAmount());customInsurancePolicies.add(customInsurancePolicy);}pdfModel.setInsurancePolicies(customInsurancePolicies);Customer customer = customerRepository.selectById(fofoOrder.getCustomerId());CustomCustomer customCustomer = new CustomCustomer();customCustomer.setFirstName(customer.getFirstName());customCustomer.setLastName(customer.getLastName());customCustomer.setEmailId(customer.getEmailId());customCustomer.setMobileNumber(customer.getMobileNumber());CustomerAddress customerAddress = customerAddressRepository.selectById(fofoOrder.getCustomerAddressId());customCustomer.setAddress(this.createCustomAddress(customerAddress));pdfModel.setCustomer(customCustomer);pdfModel.setInvoiceNumber(fofoOrder.getInvoiceNumber());pdfModel.setTotalAmount(fofoOrder.getTotalAmount());Retailer retailer = retailerRepository.selectById(fofoDetails.getFofoId());PrivateDealUser privateDealUser = null;try{privateDealUser = privateDealUserRepository.selectById(retailer.getId());}catch(ProfitMandiBusinessException profitMandiBusinessException){LOGGER.error("Private Deal User not found : ", profitMandiBusinessException);}User user = userRepository.selectById(userAccountRepository.selectUserIdByRetailerId(retailer.getId()));CustomRetailer customRetailer = new CustomRetailer();customRetailer.setBusinessName(retailer.getName());customRetailer.setMobileNumber(user.getMobileNumber());customRetailer.setTinNumber(retailer.getNumber());if(privateDealUser == null){customRetailer.setGstNumber(null);}else{if(null != privateDealUser.getCounterId()){Counter counter = counterRepository.selectById(privateDealUser.getCounterId());customRetailer.setGstNumber(counter.getGstin());}else{customRetailer.setGstNumber(null);}}Address retailerAddress = addressRepository.selectById(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId()));customRetailer.setAddress(this.createCustomAddress(retailerAddress));pdfModel.setRetailer(customRetailer);List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByOrderId(fofoOrder.getId());Set<CustomFofoOrderItem> customerFofoOrderItems = new HashSet<>();for(FofoLineItem fofoLineItem : fofoLineItems){CustomFofoOrderItem customFofoOrderItem = new CustomFofoOrderItem();float totalTaxRate = fofoLineItem.getIgstRate() + fofoLineItem.getSgstRate() + fofoLineItem.getCgstRate();float taxableSellingPrice = fofoLineItem.getSellingPrice() / (1 + totalTaxRate / 100);float taxableDiscountPrice = fofoLineItem.getDiscount() / (1 + totalTaxRate / 100);customFofoOrderItem.setAmount(fofoLineItem.getQuantity() * (taxableSellingPrice-taxableDiscountPrice));customFofoOrderItem.setDescription(fofoLineItem.getBrand() + " " + fofoLineItem.getModelName() + " " + fofoLineItem.getModelNumber() + "-" + fofoLineItem.getColor() + "\n IMEIS - " + String.join(", ",this.toSerialNumbers(fofoLineItem.getFofoLineItemSerialNumbers())));customFofoOrderItem.setRate(taxableSellingPrice);customFofoOrderItem.setDiscount(taxableDiscountPrice);customFofoOrderItem.setQuantity(fofoLineItem.getQuantity());customFofoOrderItem.setNetAmount((fofoLineItem.getSellingPrice()-fofoLineItem.getDiscount())*fofoLineItem.getQuantity());float igstAmount = (customFofoOrderItem.getAmount() * fofoLineItem.getIgstRate()) / 100;float cgstAmount = (customFofoOrderItem.getAmount() * fofoLineItem.getCgstRate()) / 100;float sgstAmount = (customFofoOrderItem.getAmount() * fofoLineItem.getSgstRate()) / 100;customFofoOrderItem.setIgstRate(fofoLineItem.getIgstRate());customFofoOrderItem.setIgstAmount(igstAmount);customFofoOrderItem.setCgstRate(fofoLineItem.getCgstRate());customFofoOrderItem.setCgstAmount(cgstAmount);customFofoOrderItem.setSgstRate(fofoLineItem.getSgstRate());customFofoOrderItem.setSgstAmount(sgstAmount);customFofoOrderItem.setHsnCode(fofoLineItem.getHsnCode());customerFofoOrderItems.add(customFofoOrderItem);}pdfModel.setOrderItems(customerFofoOrderItems);ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();PdfUtils.generateAndWrite(pdfModel, byteArrayOutputStream);//final MediaType mediaType=MediaType.parseMediaType(profilePhotoModel.getContentType().getValue());LOGGER.info("Pdf Stream length {}", byteArrayOutputStream.toByteArray().length);final HttpHeaders headers=new HttpHeaders();headers.setContentType(MediaType.APPLICATION_PDF);headers.set("Content-disposition", "inline; filename=invoice-" + fofoOrder.getInvoiceNumber() + ".pdf");headers.setContentLength(byteArrayOutputStream.toByteArray().length);final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);}private Set<String> toSerialNumbers(Set<FofoLineItemSerialNumber> fofoLineItemSerialNumbers){Set<String> serialNumbers = new HashSet<>(fofoLineItemSerialNumbers.size());for(FofoLineItemSerialNumber fofoLineItemSerialNumber : fofoLineItemSerialNumbers){serialNumbers.add(fofoLineItemSerialNumber.getSerialNumber());}return serialNumbers;}@RequestMapping(value = "/saleHistory")public String saleHistory(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString, @RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, @RequestParam(name = ProfitMandiConstants.INVOICE_NUMBER, defaultValue = "") String invoiceNumber, @RequestParam(name = "searchType",defaultValue="") String searchType, Model model) throws Exception{LoginDetails loginDetails = null;try {loginDetails = cookiesProcessor.getCookiesObject(request);} catch (ProfitMandiBusinessException e) {model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));return "response";}LocalDateTime startDateTime = StringUtils.toDateTime(startTimeString);LocalDateTime endDateTime = StringUtils.toDateTime(endTimeString);long countItems = 0;List<FofoOrder> fofoOrders = new ArrayList<>();if(searchType.equalsIgnoreCase(ProfitMandiConstants.INVOICE_NUMBER) && !invoiceNumber.isEmpty()){try {FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndInvoiceNumber(loginDetails.getFofoId(), invoiceNumber);fofoOrders.add(fofoOrder);} catch (ProfitMandiBusinessException e) {LOGGER.info("Sale history not found : ", e);}}else{fofoOrders = fofoOrderRepository.selectByFofoId(loginDetails.getFofoId(),startDateTime, endDateTime, offset, limit);countItems = fofoOrderRepository.selectCount(loginDetails.getFofoId(), startDateTime, endDateTime, invoiceNumber, searchType);}model.addAttribute("saleHistories", fofoOrders);model.addAttribute("start", offset + 1);model.addAttribute("size", countItems);model.addAttribute("searchType", searchType);model.addAttribute(ProfitMandiConstants.START_TIME, startTimeString);model.addAttribute(ProfitMandiConstants.END_TIME, endTimeString);if (fofoOrders.size() < limit){model.addAttribute("end", offset + fofoOrders.size());}else{model.addAttribute("end", offset + limit);}return "sale-history";}@RequestMapping(value = "/getPaginatedSaleHistory")public String getSaleHistoryPaginated(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString, @RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue="10") int limit, @RequestParam(name = ProfitMandiConstants.INVOICE_NUMBER, defaultValue="") String invoiceNumber, @RequestParam(name = "searchType", defaultValue = "") String searchType, Model model) throws Exception{LoginDetails loginDetails;try {loginDetails = cookiesProcessor.getCookiesObject(request);} catch (ProfitMandiBusinessException e) {model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));return "response";}LocalDateTime startDateTime = StringUtils.toDateTime(startTimeString);LocalDateTime endDateTime = StringUtils.toDateTime(endTimeString);List<FofoOrder> saleHistories = fofoOrderRepository.selectByFofoId(loginDetails.getFofoId(), startDateTime, endDateTime, offset, limit);model.addAttribute("saleHistories", saleHistories);return "sale-history-paginated";}}