Subversion Repositories SmartDukaan

Rev

Rev 23568 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22111 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
34861 ranu 3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.model.FranchiseeAccountModel;
5
import com.spice.profitmandi.common.util.FileUtil;
6
import com.spice.profitmandi.common.web.util.ResponseSender;
7
import com.spice.profitmandi.dao.entity.transaction.FranchiseeBankAccount;
8
import com.spice.profitmandi.dao.model.FofoForm;
9
import com.spice.profitmandi.dao.repository.FranchiseeBankAccountRepository;
10
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
11
import com.spice.profitmandi.dao.repository.dtr.Mongo;
12
import com.spice.profitmandi.dao.util.FofoDocumentsGenerator;
13
import com.spice.profitmandi.service.order.OrderService;
14
import com.spice.profitmandi.web.util.CookiesProcessor;
15
import com.spice.profitmandi.web.util.MVCResponseSender;
16
import org.apache.logging.log4j.LogManager;
23568 govind 17
import org.apache.logging.log4j.Logger;
34861 ranu 18
import org.apache.poi.ss.usermodel.Cell;
19
import org.apache.poi.ss.usermodel.Row;
20
import org.apache.poi.ss.usermodel.Sheet;
21
import org.apache.poi.ss.usermodel.Workbook;
22
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
22162 amit.gupta 23
import org.springframework.beans.factory.annotation.Autowired;
22196 amit.gupta 24
import org.springframework.http.HttpHeaders;
25
import org.springframework.http.HttpStatus;
26
import org.springframework.http.MediaType;
27
import org.springframework.http.ResponseEntity;
22111 ashik.ali 28
import org.springframework.stereotype.Controller;
22201 amit.gupta 29
import org.springframework.transaction.annotation.Transactional;
22111 ashik.ali 30
import org.springframework.ui.Model;
34861 ranu 31
import org.springframework.web.bind.annotation.*;
32
import org.springframework.web.multipart.MultipartFile;
22111 ashik.ali 33
 
34861 ranu 34
import javax.mail.MessagingException;
35
import javax.servlet.http.HttpServletRequest;
36
import java.io.IOException;
37
import java.util.ArrayList;
38
import java.util.Arrays;
39
import java.util.List;
22111 ashik.ali 40
 
41
@Controller
22201 amit.gupta 42
@Transactional
22111 ashik.ali 43
public class FofoController {
44
 
23568 govind 45
	private static final Logger LOGGER = LogManager.getLogger(FofoController.class);
22162 amit.gupta 46
 
47
	@Autowired
22927 ashik.ali 48
	private Mongo mongoClient;
34861 ranu 49
 
22196 amit.gupta 50
	@Autowired
34861 ranu 51
	AuthRepository authRepository;
52
	@Autowired
53
	OrderService orderService;
54
	@Autowired
55
	FranchiseeBankAccountRepository franchiseeBankAccountRepository;
56
	@Autowired
57
	private ResponseSender<?> responseSender;
58
	@Autowired
22927 ashik.ali 59
	private FofoDocumentsGenerator generator;
34861 ranu 60
	@Autowired
61
	private CookiesProcessor cookiesProcessor;
62
	@Autowired
63
	private MVCResponseSender mvcResponseSender;
22162 amit.gupta 64
 
22111 ashik.ali 65
	@RequestMapping(value = "/fofo", method = RequestMethod.GET)
22927 ashik.ali 66
	public String getAll(HttpServletRequest request, Model model) throws Throwable {
22162 amit.gupta 67
		model.addAttribute("fofoForms", mongoClient.getFofoForms(0, 50));
22111 ashik.ali 68
		return "fofo-index";
69
	}
70
 
22196 amit.gupta 71
	@RequestMapping(value = "/fofo/{fofoId}/file-display", method = RequestMethod.GET)
22927 ashik.ali 72
	public ResponseEntity<byte[]> displayDocs(HttpServletRequest request, @PathVariable(name = "fofoId") int fofoId) throws Throwable{
22196 amit.gupta 73
		HttpHeaders headers = new HttpHeaders();
74
	    headers.setContentType(MediaType.parseMediaType("application/pdf"));
75
	    String filename = "output.pdf";
76
	    headers.setContentDispositionFormData(filename, filename);
77
	    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
78
	    byte[] contents = generator.getDocumentStream(fofoId);
79
		ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
80
	    return response;
81
	}
82
 
22111 ashik.ali 83
	@RequestMapping(value = "/fofo/{fofoId}/edit", method = RequestMethod.GET)
84
	public String editFofoForm(HttpServletRequest request, @PathVariable(name = "fofoId") int fofoId, Model model) throws Exception{
22193 amit.gupta 85
		FofoForm ff = mongoClient.getFofoForm(fofoId);
86
		model.addAttribute("fofoForm", mongoClient.getFofoFormJsonStringByFofoId(fofoId));
87
		model.addAttribute("email", ff.getRegisteredEmail1());
22191 amit.gupta 88
		return "fofo-form";
22111 ashik.ali 89
	}
34861 ranu 90
 
91
	@RequestMapping(value = "/loadFranchiseeAccount")
92
	public String loadFranchiseeAccount(HttpServletRequest request, Model model) throws ProfitMandiBusinessException, MessagingException, IOException {
93
		List<FranchiseeBankAccount> franchiseeAccounts = franchiseeBankAccountRepository.getAllFranchiseeBankAccounts();
94
		model.addAttribute("franchiseeAccounts", franchiseeAccounts);
95
		return "franchisee-account-creation";
96
	}
97
 
98
	@RequestMapping(value = "/franchiseeAccountCreationTemplate", method = RequestMethod.GET)
99
	public ResponseEntity<?> franchiseeAccountCreationTemplate() throws Exception {
100
		List<String> headers = Arrays.asList(
101
				"Fofo ID",
102
				"Person Name",
103
				"Contact Number",
104
				"Email",
105
				"TID",
106
				"MID",
107
				"Bank Account Number",
108
				"IFSC",
109
				"Contact ID (to be filled later)",
110
				"Account ID (to be filled later)"
111
		);
112
 
113
		// Empty rows, just the header
114
		List<List<?>> rows = new ArrayList<>();
115
 
116
		// Generate CSV as ByteArrayOutputStream
117
		org.apache.commons.io.output.ByteArrayOutputStream baos =
118
				FileUtil.getCSVByteStream(headers, rows);
119
 
120
		// Reuse your CSV response generator
121
		return orderService.downloadReportInCsv(baos, rows, "Franchisee_Account_Creation_Template");
122
	}
123
 
124
 
125
	@PostMapping(value = "/franchisee/account/upload")
126
	public String uploadFranchiseeAccount(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
127
			throws Exception {
128
		List<FranchiseeBankAccount> accounts = this.parseExcel(file);
129
		for (FranchiseeBankAccount acc : accounts) {
130
			franchiseeBankAccountRepository.persist(acc);
131
		}
132
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
133
		return "response";
134
	}
135
 
136
 
137
	private List<FranchiseeBankAccount> parseExcel(MultipartFile file) throws Exception {
138
		List<FranchiseeBankAccount> accounts = new ArrayList<>();
139
		Workbook workbook = new XSSFWorkbook(file.getInputStream());
140
		Sheet sheet = workbook.getSheetAt(0);
141
 
142
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
143
			Row row = sheet.getRow(i);
144
			if (row == null) continue;
145
 
146
			FranchiseeBankAccount acc = new FranchiseeBankAccount();
147
 
148
			acc.setFofoId((int) getNumericCellValue(row.getCell(0)));
149
			acc.setPersonName(getStringCellValue(row.getCell(1)));
150
			acc.setContactNumber(getStringCellValue(row.getCell(2)));
151
			acc.setEmail(getStringCellValue(row.getCell(3)));
152
			acc.setTid(getStringCellValue(row.getCell(4)));
153
			acc.setMid(getStringCellValue(row.getCell(5)));
154
			acc.setBankAccountNumber(getStringCellValue(row.getCell(6)));
155
			acc.setIfsc(getStringCellValue(row.getCell(7)));
156
 
157
			acc.setContactId(null);
158
			acc.setAccountId(null);
159
 
160
			accounts.add(acc);
161
		}
162
		workbook.close();
163
		return accounts;
164
	}
165
 
166
	private double getNumericCellValue(Cell cell) {
167
		return cell != null ? cell.getNumericCellValue() : 0;
168
	}
169
 
170
	private String getStringCellValue(Cell cell) {
171
		if (cell == null) return null;
172
 
173
		switch (cell.getCellType()) {
174
			case Cell.CELL_TYPE_STRING:
175
				return cell.getStringCellValue();
176
			case Cell.CELL_TYPE_NUMERIC:
177
				return String.valueOf((long) cell.getNumericCellValue()); // Avoid scientific notation
178
			case Cell.CELL_TYPE_BOOLEAN:
179
				return String.valueOf(cell.getBooleanCellValue());
180
			case Cell.CELL_TYPE_FORMULA:
181
				return cell.getCellFormula();
182
			case Cell.CELL_TYPE_BLANK:
183
				return null;
184
			default:
185
				return null;
186
		}
187
	}
188
 
189
	@RequestMapping(value = "/franchisee/account/edit/{id}", method = RequestMethod.GET)
190
	@ResponseBody
191
	public FranchiseeBankAccount editFranchiseeAccount(@PathVariable("id") int id) {
192
		return franchiseeBankAccountRepository.selectById(id);
193
	}
194
 
195
	@RequestMapping(value = "/franchisee/account/update", method = RequestMethod.POST)
196
	@ResponseBody
197
	public ResponseEntity<?> updateFranchiseeAccount(HttpServletRequest request, @RequestBody FranchiseeAccountModel accountModel, Model model) {
198
		FranchiseeBankAccount franchiseeBankAccount = franchiseeBankAccountRepository.selectById(accountModel.getId());
199
 
200
		franchiseeBankAccount.setPersonName(accountModel.getPersonName());
201
		franchiseeBankAccount.setContactNumber(accountModel.getContactNumber());
202
		franchiseeBankAccount.setEmail(accountModel.getEmail());
203
		franchiseeBankAccount.setBankAccountNumber(accountModel.getBankAccountNumber());
204
		franchiseeBankAccount.setIfsc(accountModel.getIfsc());
205
		franchiseeBankAccount.setTid(accountModel.getTid());
206
		franchiseeBankAccount.setMid(accountModel.getMid());
207
		return responseSender.ok(true);
208
	}
209
 
210
 
211
 
22111 ashik.ali 212
}