Subversion Repositories SmartDukaan

Rev

Rev 34861 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.FranchiseeAccountModel;
import com.spice.profitmandi.common.util.FileUtil;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.transaction.FranchiseeBankAccount;
import com.spice.profitmandi.dao.model.FofoForm;
import com.spice.profitmandi.dao.repository.FranchiseeBankAccountRepository;
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
import com.spice.profitmandi.dao.repository.dtr.Mongo;
import com.spice.profitmandi.dao.util.FofoDocumentsGenerator;
import com.spice.profitmandi.service.order.OrderService;
import com.spice.profitmandi.web.util.CookiesProcessor;
import com.spice.profitmandi.web.util.MVCResponseSender;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
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.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class FofoController {
        
        private static final Logger LOGGER = LogManager.getLogger(FofoController.class);

        @Autowired
        private Mongo mongoClient;

        @Autowired
        AuthRepository authRepository;
        @Autowired
        OrderService orderService;
        @Autowired
        FranchiseeBankAccountRepository franchiseeBankAccountRepository;
        @Autowired
        private ResponseSender<?> responseSender;
        @Autowired
        private FofoDocumentsGenerator generator;
        @Autowired
        private CookiesProcessor cookiesProcessor;
        @Autowired
        private MVCResponseSender mvcResponseSender;
        
        @RequestMapping(value = "/fofo", method = RequestMethod.GET)
        public String getAll(HttpServletRequest request, Model model) throws Throwable {
                model.addAttribute("fofoForms", mongoClient.getFofoForms(0, 50));
                return "fofo-index";
        }
        
        @RequestMapping(value = "/fofo/{fofoId}/file-display", method = RequestMethod.GET)
        public ResponseEntity<byte[]> displayDocs(HttpServletRequest request, @PathVariable(name = "fofoId") int fofoId) throws Throwable{
                HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("application/pdf"));
            String filename = "output.pdf";
            headers.setContentDispositionFormData(filename, filename);
            headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
            byte[] contents = generator.getDocumentStream(fofoId);
                ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
            return response;
        }
        
        @RequestMapping(value = "/fofo/{fofoId}/edit", method = RequestMethod.GET)
        public String editFofoForm(HttpServletRequest request, @PathVariable(name = "fofoId") int fofoId, Model model) throws Exception{
                FofoForm ff = mongoClient.getFofoForm(fofoId);
                model.addAttribute("fofoForm", mongoClient.getFofoFormJsonStringByFofoId(fofoId));
                model.addAttribute("email", ff.getRegisteredEmail1());
                return "fofo-form";
        }

        @RequestMapping(value = "/loadFranchiseeAccount")
        public String loadFranchiseeAccount(HttpServletRequest request, Model model) throws ProfitMandiBusinessException, MessagingException, IOException {
                List<FranchiseeBankAccount> franchiseeAccounts = franchiseeBankAccountRepository.getAllFranchiseeBankAccounts();
                model.addAttribute("franchiseeAccounts", franchiseeAccounts);
                return "franchisee-account-creation";
        }

        @RequestMapping(value = "/franchiseeAccountCreationTemplate", method = RequestMethod.GET)
        public ResponseEntity<?> franchiseeAccountCreationTemplate() throws Exception {
                List<String> headers = Arrays.asList(
                                "Fofo ID",
                                "Person Name",
                                "Contact Number",
                                "Email",
                                "TID",
                                "MID",
                                "Bank Account Number",
                                "IFSC",
                                "Contact ID (to be filled later)",
                                "Account ID (to be filled later)"
                );

                // Empty rows, just the header
                List<List<?>> rows = new ArrayList<>();

                // Generate CSV as ByteArrayOutputStream
                org.apache.commons.io.output.ByteArrayOutputStream baos =
                                FileUtil.getCSVByteStream(headers, rows);

                // Reuse your CSV response generator
                return orderService.downloadReportInCsv(baos, rows, "Franchisee_Account_Creation_Template");
        }


        @PostMapping(value = "/franchisee/account/upload")
        public String uploadFranchiseeAccount(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
                        throws Exception {
                List<FranchiseeBankAccount> accounts = this.parseExcel(file);
                for (FranchiseeBankAccount acc : accounts) {
                        franchiseeBankAccountRepository.persist(acc);
                }
                model.addAttribute("response1", mvcResponseSender.createResponseString(true));
                return "response";
        }


        private List<FranchiseeBankAccount> parseExcel(MultipartFile file) throws Exception {
                List<FranchiseeBankAccount> accounts = new ArrayList<>();
                Workbook workbook = new XSSFWorkbook(file.getInputStream());
                Sheet sheet = workbook.getSheetAt(0);

                for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                        Row row = sheet.getRow(i);
                        if (row == null) continue;

                        FranchiseeBankAccount acc = new FranchiseeBankAccount();

                        acc.setFofoId((int) getNumericCellValue(row.getCell(0)));
                        acc.setPersonName(getStringCellValue(row.getCell(1)));
                        acc.setContactNumber(getStringCellValue(row.getCell(2)));
                        acc.setEmail(getStringCellValue(row.getCell(3)));
                        acc.setTid(getStringCellValue(row.getCell(4)));
                        acc.setMid(getStringCellValue(row.getCell(5)));
                        acc.setBankAccountNumber(getStringCellValue(row.getCell(6)));
                        acc.setIfsc(getStringCellValue(row.getCell(7)));

                        acc.setContactId(null);
                        acc.setAccountId(null);

                        accounts.add(acc);
                }
                workbook.close();
                return accounts;
        }

        private double getNumericCellValue(Cell cell) {
                return cell != null ? cell.getNumericCellValue() : 0;
        }

        private String getStringCellValue(Cell cell) {
                if (cell == null) return null;

                switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                                return cell.getStringCellValue();
                        case Cell.CELL_TYPE_NUMERIC:
                                return String.valueOf((long) cell.getNumericCellValue()); // Avoid scientific notation
                        case Cell.CELL_TYPE_BOOLEAN:
                                return String.valueOf(cell.getBooleanCellValue());
                        case Cell.CELL_TYPE_FORMULA:
                                return cell.getCellFormula();
                        case Cell.CELL_TYPE_BLANK:
                                return null;
                        default:
                                return null;
                }
        }

        @RequestMapping(value = "/franchisee/account/edit/{id}", method = RequestMethod.GET)
        @ResponseBody
        public FranchiseeBankAccount editFranchiseeAccount(@PathVariable("id") int id) {
                return franchiseeBankAccountRepository.selectById(id);
        }

        @RequestMapping(value = "/franchisee/account/update", method = RequestMethod.POST)
        @ResponseBody
        public ResponseEntity<?> updateFranchiseeAccount(HttpServletRequest request, @RequestBody FranchiseeAccountModel accountModel, Model model) {
                FranchiseeBankAccount franchiseeBankAccount = franchiseeBankAccountRepository.selectById(accountModel.getId());

                franchiseeBankAccount.setPersonName(accountModel.getPersonName());
                franchiseeBankAccount.setContactNumber(accountModel.getContactNumber());
                franchiseeBankAccount.setEmail(accountModel.getEmail());
                franchiseeBankAccount.setBankAccountNumber(accountModel.getBankAccountNumber());
                franchiseeBankAccount.setIfsc(accountModel.getIfsc());
                franchiseeBankAccount.setTid(accountModel.getTid());
                franchiseeBankAccount.setMid(accountModel.getMid());
                return responseSender.ok(true);
        }



}