Subversion Repositories SmartDukaan

Rev

Rev 30330 | Rev 33514 | 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.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomAddress;
import com.spice.profitmandi.common.model.CustomCustomer;
import com.spice.profitmandi.common.model.CustomInsurancePolicy;
import com.spice.profitmandi.common.model.CustomOrderItem;
import com.spice.profitmandi.common.model.CustomRetailer;
import com.spice.profitmandi.common.model.GadgetCopsDocumentInsuranceModel;
import com.spice.profitmandi.common.model.InvoicePdfModel;

import com.spice.profitmandi.common.model.ProfitMandiConstants;
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.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.catalog.RangeItem;
import com.spice.profitmandi.dao.entity.catalog.RangeItemPrice;
import com.spice.profitmandi.dao.entity.dtr.Document;
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.ThirdPartyInvoiceSequence;
import com.spice.profitmandi.dao.enumuration.dtr.ThirdParty;
import com.spice.profitmandi.dao.model.UserCart;
import com.spice.profitmandi.dao.repository.catalog.RangeItemPriceRepository;
import com.spice.profitmandi.dao.repository.catalog.RangeItemRepository;
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
import com.spice.profitmandi.dao.repository.dtr.GadgetCopsInsuranceCalcRepository;
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.ThirdPartyInvoiceSequenceRepository;
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
import com.spice.profitmandi.dao.repository.fofo.InvoiceNumberGenerationSequenceRepository;
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
import com.spice.profitmandi.service.pricing.PricingService;
import com.spice.profitmandi.service.wallet.WalletService;
import com.spice.profitmandi.web.processor.OtpProcessor;

import in.shop2020.model.v1.order.WalletReferenceType;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class InsuranceController {
        private static final Logger LOGGER = LogManager.getLogger(InsuranceController.class);
        private static final String gadgetCopsFilePath = "/GadgetCops";

        @Autowired
        private RangeItemPriceRepository rangeItemPriceRepository;

        @Autowired
        private RangeItemRepository rangeItemRepository;

        // This is now unused as we are not supporting multiple companies.
        @Value("${fofo.warehouseIds}")
        private int[] warehouseIds;

        @Autowired
        OtpProcessor otpProcessor;

        @RequestMapping(value = ProfitMandiConstants.URL_INSURANCE_GADGET_COPS_MAPPING, method = RequestMethod.GET)
        public ResponseEntity<?> getById(HttpServletRequest request) {
                LOGGER.info("requested url : " + request.getRequestURL().toString());
                Map<Integer, List<RangeItemPrice>> rangeItemPrices = rangeItemPriceRepository.selectAll().stream()
                                .collect(Collectors.groupingBy(RangeItemPrice::getRangeItemId, Collectors.toList()));
                List<RangeItem> rangeItems = rangeItemRepository.selectAll();
                rangeItems.stream().forEach(x -> x.setRangeItemPrices(rangeItemPrices.get(x.getId())));
                return responseSender.ok(rangeItems);
        }

        @Autowired
        ResponseSender<?> responseSender;

        @Autowired
        GadgetCopsInsuranceCalcRepository gadgetCopsInsuranceCalcRepository;

        @Autowired
        ThirdPartyInvoiceSequenceRepository thirdPartyInvoiceSequenceRepository;

        @Autowired
        DocumentRepository documentRepository;

        @Autowired
        InvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;

        @Autowired
        InsurancePolicyRepository insurancePolicyRepository;

        @Autowired
        PricingService pricingService;

        @Autowired
        JavaMailSender mailSender;

        @Autowired
        InsuranceProviderRepository insuranceProviderRepository;

        @Autowired
        UserAccountRepository userAccountRepository;

        @Autowired
        WalletService walletService;

        @Autowired
        UserWalletRepository userWalletRepositoy;

        @Autowired
        PolicyNumberGenerationSequenceRepository policyNumberGenerationSequenceRepository;

        @RequestMapping(value = ProfitMandiConstants.URL_DAMAGE_INSURANCE, method = RequestMethod.POST)
        public ResponseEntity<?> createDamageProtection(HttpServletRequest request,
                        @RequestBody GadgetCopsDocumentInsuranceModel insuranceModel) throws Throwable {
                insuranceModel
                                .setCustomerDateOfBirth(StringUtils.fromHypendatedDate(insuranceModel.getCustomerDateOfBirthString()));
                insuranceModel.setInvoiceCreationDate(LocalDate.now());
                insuranceModel.validate();
                int userId = (int) request.getAttribute("userId");
                UserCart uc = userAccountRepository.getUserCart(userId);
                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);
                }
                String policyNumber = StringUtils.generatePolicyNumber(ProfitMandiConstants.POLICY_NUMBER_PREFIX,
                                policyNumberGenerationSequence.getSequence());

                InsuranceProvider insuranceProvider = insuranceProviderRepository
                                .selectByName(ProfitMandiConstants.GADGET_COPS);
                Set<Float> devicePriceSet = new HashSet<>();
                devicePriceSet.add(insuranceModel.getPrice());
                Map<Float, GadgetCopsInsuranceCalc> insurancePricesMap = pricingService.getInsurancePrices(devicePriceSet,
                                ProfitMandiConstants.GADGET_COPS);
                InsurancePolicy insurancePolicy = new InsurancePolicy();
                insurancePolicy.setInvoiceNumber(insuranceModel.getInvoiceNumber());
                insurancePolicy.setRetailerId(uc.getUserId());
                insurancePolicy.setPurchaseAmount(insurancePricesMap.get(insuranceModel.getPrice()).getDealerPrice());
                insurancePolicy.setSaleAmount(insurancePricesMap.get(insuranceModel.getPrice()).getSellingPrice());
                insurancePolicy.setSellingPrice(insuranceModel.getPrice());
                insurancePolicy.setSerialNumber(insuranceModel.getSerialNumber());
                insurancePolicy.setModelName(insuranceModel.getModelName());
                insurancePolicy.setBrand(insuranceModel.getBrand());
                insurancePolicy.setPolicyNumber(policyNumber);
                insurancePolicy.setProviderId(insuranceProvider.getId());
                insurancePolicy.setCustomerFirstName(insuranceModel.getCustomerFirstName());
                insurancePolicy.setCustomerLastName(insuranceModel.getCustomerFirstName());
                insurancePolicy.setCustomerMobileNumber(insuranceModel.getCustomerMobileNumber());
                insurancePolicy.setCustomerEmailId(insuranceModel.getCustomerEmailId());
                insurancePolicy.setCustomerDateOfBirth(insuranceModel.getCustomerDateOfBirth());
                insurancePolicy.setCustomerAddress1(insuranceModel.getCustomerAddress1());
                insurancePolicy.setCustomerAddress2(insuranceModel.getCustomerAddress2());
                insurancePolicy.setCustomerCity(insuranceModel.getCustomerCity());
                insurancePolicy.setCustomerPinCode(insuranceModel.getCustomerPinCode());
                insurancePolicy.setCustomerState(insuranceModel.getCustomerState());
                insurancePolicyRepository.persist(insurancePolicy);
                insuranceModel.setPolicyNumber(insurancePolicy.getPolicyNumber());
                String walletDescription = "Purchased Damage Protection policy " + insurancePolicy.getPolicyNumber() + " for "
                                + insurancePolicy.getCustomerFirstName();

                walletService.consumeAmountFromWallet(uc.getUserId(), insurancePolicy.getId(),
                                WalletReferenceType.DAMAGE_PROTECTION, walletDescription, insurancePolicy.getSaleAmount(),
                                LocalDateTime.now());

                try {
                        InsuranceUtils.submitToGadgetCops(insuranceModel);
                        insurancePolicy.setPosted(true);
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.info("Unable to submit insurance policy details to {}", insuranceProvider.getName(),
                                        profitMandiBusinessException);
                        throw new ProfitMandiBusinessException("", "", "GDTCPSServerDown_1000");
                }
                insurancePolicyRepository.persist(insurancePolicy);

                InvoicePdfModel pdfModel = this.getInvoicePdfModel(insurancePolicy);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                PdfUtils.generateAndWrite(Arrays.asList(pdfModel), byteArrayOutputStream);

                Document deviceDocument = documentRepository.selectById(insuranceModel.getDeviceImageDocumentId());
                File deviceImg = new File(deviceDocument.getPath() + deviceDocument.getName());
                Document invoiceDocument = documentRepository.selectById(insuranceModel.getDeviceImageDocumentId());
                File deviceInvoiceImg = new File(invoiceDocument.getPath() + invoiceDocument.getName());

                List<File> attachments = this.savePolicyDocs(insurancePolicy.getPolicyNumber(), byteArrayOutputStream,
                                deviceImg, deviceInvoiceImg);
                String messageText = MessageFormat.format(
                                "Dear {0}, Thank you for purchasing Damage Protection Plan. Your Policy number is {1}",
                                insurancePolicy.getCustomerFirstName(), insurancePolicy.getPolicyNumber());
                // TODO: add template if needed
                otpProcessor.sendSms("", messageText, insurancePolicy.getCustomerMobileNumber());
                messageText += "\n Please find your Invoice and Documents submitted by you.";
                String subject = "Your Gadget Cop damage Protection purchase is successful";
                this.sendMailWithAttachments(insuranceModel.getCustomerEmailId(), subject, messageText, attachments);

                float taxableInsuranceMargin = (insurancePolicy.getSaleAmount() - insurancePolicy.getPurchaseAmount())
                                / (1 + ProfitMandiConstants.INSURANCE_TAX_RATE / 100);
                walletService.addAmountToWallet(uc.getUserId(), insurancePolicy.getId(), WalletReferenceType.CASHBACK,
                                "Cashback against insurance policy", taxableInsuranceMargin, LocalDateTime.now());
                LOGGER.info("requested url : " + request.getRequestURL().toString());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("policyNumber", insurancePolicy.getPolicyNumber());
                jsonObject.put("premiumCharged", insurancePolicy.getSaleAmount());
                return responseSender.ok(jsonObject.toString());
        }

        private void sendMailWithAttachments(String email, String subject, String body, List<File> attachments)
                        throws Exception {

                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setSubject(subject);
                helper.setText(body);
                String[] cc = { "backup@shop2020.in", "gadgetcops.01@gmail.com" };
                // String[] cc = {"amit.gupta@shop2020.in"};
                helper.setCc(cc);
                helper.setTo(email);
                InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "ProfitMandi Admin");
                helper.setTo("help@smartdukaan.com");
                helper.setFrom(senderAddress);
                for (File file : attachments) {
                        helper.addAttachment(file.getName(), file);
                }
                mailSender.send(message);

        }

        private List<File> savePolicyDocs(String policyNumber, ByteArrayOutputStream pdfInvoiceStream, File deviceImg,
                        File deviceInvoiceImg) throws Exception {
                // save file to gadgetCops/policyname
                String policyFolderPath = gadgetCopsFilePath + File.separator + policyNumber;
                File destDeviceInvoiceImg = new File(policyFolderPath + File.separator + "deviceInvoice.jpg");
                File destDeviceImg = new File(policyFolderPath + File.separator + "device.jpg");
                FileUtils.copyFile(deviceImg, destDeviceImg);
                FileUtils.copyFile(deviceInvoiceImg, destDeviceInvoiceImg);

                OutputStream outStream = null;
                try {
                        outStream = new FileOutputStream(policyFolderPath + File.separator + "invoice.pdf");
                        pdfInvoiceStream.writeTo(outStream);
                        pdfInvoiceStream.flush();
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        outStream.close();
                        pdfInvoiceStream.close();
                }

                File pdfInvoice = new File(policyFolderPath + File.separator + "invoice.pdf");
                return Arrays.asList(pdfInvoice, destDeviceImg, destDeviceInvoiceImg);
        }

        public String getFormattedDate(LocalDateTime localDate) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-YYYY HH:mm");
                return localDate.format(formatter);
        }

        private InvoicePdfModel getInvoicePdfModel(InsurancePolicy insurancePolicy) throws ProfitMandiBusinessException {

                InvoicePdfModel pdfModel = new InvoicePdfModel();
                pdfModel.setAuther("profitmandi");
                pdfModel.setTitle("Retailer Invoice");
                pdfModel.setInvoiceDate(getFormattedDate(insurancePolicy.getCreateTimestamp()));

                // insurance calculation
                List<CustomInsurancePolicy> customInsurancePolicies = new ArrayList<>();
                final float totalInsuranceTaxRate = 18;

                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(9);
                customInsurancePolicy.setCgstAmount(taxableInsurancePrice * 9 / 100);
                customInsurancePolicy.setSgstRate(9);
                customInsurancePolicy.setSgstAmount(taxableInsurancePrice * 9 / 100);
                customInsurancePolicy.setNetAmount(insurancePolicy.getSaleAmount());
                customInsurancePolicies.add(customInsurancePolicy);

                pdfModel.setInsurancePolicies(customInsurancePolicies);
                CustomCustomer customCustomer = new CustomCustomer();
                customCustomer.setFirstName(insurancePolicy.getCustomerFirstName());
                customCustomer.setLastName(insurancePolicy.getCustomerLastName());
                customCustomer.setEmailId(insurancePolicy.getCustomerEmailId());
                customCustomer.setMobileNumber(insurancePolicy.getCustomerMobileNumber());

                CustomAddress customAddress = new CustomAddress();
                customAddress.setName(insurancePolicy.getCustomerFirstName() + " " + insurancePolicy.getCustomerLastName());
                customAddress.setLine1(insurancePolicy.getCustomerAddress1());
                customAddress.setLine2(insurancePolicy.getCustomerAddress2());
                customAddress.setLandmark("");
                customAddress.setCity(insurancePolicy.getCustomerCity());
                customAddress.setPinCode(insurancePolicy.getCustomerPinCode());
                customAddress.setState(insurancePolicy.getCustomerState());
                customAddress.setPhoneNumber(insurancePolicy.getCustomerMobileNumber());
                customCustomer.setAddress(customAddress);
                pdfModel.setCustomer(customCustomer);

                // TODO get invoice number for damageProtection provider
                pdfModel.setInvoiceNumber(thirdPartyInvoiceSequenceRepository.getNextSequence(ThirdParty.GADGET_COP));
                pdfModel.setTotalAmount(gadgetCopsInsuranceCalcRepository.selectByPrice(insurancePolicy.getSellingPrice()));

                // Here bill is generated on behalf of GadgetCop
                ThirdPartyInvoiceSequence damageProtectionProvider = thirdPartyInvoiceSequenceRepository
                                .selectByThirdParty(ThirdParty.GADGET_COP);
                // Gadget Cop
                // Gadget cop mobile
                CustomRetailer customRetailer = new CustomRetailer();
                customRetailer.setBusinessName(damageProtectionProvider.getName());
                customRetailer.setMobileNumber(damageProtectionProvider.getMobileNumber());
                customRetailer.setGstNumber(damageProtectionProvider.getGstNumber());
                // customRetailer.setAddress(address);

                CustomAddress providerAddress = new CustomAddress();
                providerAddress.setCity(damageProtectionProvider.getCity());
                providerAddress.setLine1(damageProtectionProvider.getLine1());
                providerAddress.setLine2(damageProtectionProvider.getLine2());
                providerAddress.setPinCode(damageProtectionProvider.getPin());
                providerAddress.setPhoneNumber(damageProtectionProvider.getMobileNumber());
                providerAddress.setState(damageProtectionProvider.getState());

                customRetailer.setAddress(providerAddress);
                pdfModel.setRetailer(customRetailer);

                List<CustomOrderItem> customerFofoOrderItems = new ArrayList<>();
                pdfModel.setOrderItems(customerFofoOrderItems);
                pdfModel.setTncs(Arrays.asList("Please check out Policy Schedule for detailed terms and conditions."));
                return pdfModel;
        }

}