Subversion Repositories SmartDukaan

Rev

Rev 35458 | 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
35458 amit 42
@Transactional(rollbackFor = Throwable.class)
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
37405 amit 48
	private com.spice.profitmandi.dao.repository.inventory.StateRepository stateRepository;
49
 
50
	@Autowired
22927 ashik.ali 51
	private Mongo mongoClient;
34861 ranu 52
 
22196 amit.gupta 53
	@Autowired
34861 ranu 54
	AuthRepository authRepository;
55
	@Autowired
56
	OrderService orderService;
57
	@Autowired
58
	FranchiseeBankAccountRepository franchiseeBankAccountRepository;
59
	@Autowired
60
	private ResponseSender<?> responseSender;
61
	@Autowired
22927 ashik.ali 62
	private FofoDocumentsGenerator generator;
34861 ranu 63
	@Autowired
64
	private CookiesProcessor cookiesProcessor;
65
	@Autowired
66
	private MVCResponseSender mvcResponseSender;
22162 amit.gupta 67
 
22111 ashik.ali 68
	@RequestMapping(value = "/fofo", method = RequestMethod.GET)
22927 ashik.ali 69
	public String getAll(HttpServletRequest request, Model model) throws Throwable {
22162 amit.gupta 70
		model.addAttribute("fofoForms", mongoClient.getFofoForms(0, 50));
22111 ashik.ali 71
		return "fofo-index";
72
	}
73
 
22196 amit.gupta 74
	@RequestMapping(value = "/fofo/{fofoId}/file-display", method = RequestMethod.GET)
22927 ashik.ali 75
	public ResponseEntity<byte[]> displayDocs(HttpServletRequest request, @PathVariable(name = "fofoId") int fofoId) throws Throwable{
22196 amit.gupta 76
		HttpHeaders headers = new HttpHeaders();
77
	    headers.setContentType(MediaType.parseMediaType("application/pdf"));
78
	    String filename = "output.pdf";
79
	    headers.setContentDispositionFormData(filename, filename);
80
	    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
81
	    byte[] contents = generator.getDocumentStream(fofoId);
82
		ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
83
	    return response;
84
	}
85
 
22111 ashik.ali 86
	@RequestMapping(value = "/fofo/{fofoId}/edit", method = RequestMethod.GET)
87
	public String editFofoForm(HttpServletRequest request, @PathVariable(name = "fofoId") int fofoId, Model model) throws Exception{
22193 amit.gupta 88
		FofoForm ff = mongoClient.getFofoForm(fofoId);
89
		model.addAttribute("fofoForm", mongoClient.getFofoFormJsonStringByFofoId(fofoId));
90
		model.addAttribute("email", ff.getRegisteredEmail1());
37405 amit 91
		model.addAttribute("stateNames", this.stateNames());
22191 amit.gupta 92
		return "fofo-form";
22111 ashik.ali 93
	}
34861 ranu 94
 
95
	@RequestMapping(value = "/loadFranchiseeAccount")
96
	public String loadFranchiseeAccount(HttpServletRequest request, Model model) throws ProfitMandiBusinessException, MessagingException, IOException {
97
		List<FranchiseeBankAccount> franchiseeAccounts = franchiseeBankAccountRepository.getAllFranchiseeBankAccounts();
98
		model.addAttribute("franchiseeAccounts", franchiseeAccounts);
99
		return "franchisee-account-creation";
100
	}
101
 
102
	@RequestMapping(value = "/franchiseeAccountCreationTemplate", method = RequestMethod.GET)
103
	public ResponseEntity<?> franchiseeAccountCreationTemplate() throws Exception {
104
		List<String> headers = Arrays.asList(
105
				"Fofo ID",
106
				"Person Name",
107
				"Contact Number",
108
				"Email",
109
				"TID",
110
				"MID",
111
				"Bank Account Number",
112
				"IFSC",
113
				"Contact ID (to be filled later)",
114
				"Account ID (to be filled later)"
115
		);
116
 
117
		// Empty rows, just the header
118
		List<List<?>> rows = new ArrayList<>();
119
 
120
		// Generate CSV as ByteArrayOutputStream
121
		org.apache.commons.io.output.ByteArrayOutputStream baos =
122
				FileUtil.getCSVByteStream(headers, rows);
123
 
124
		// Reuse your CSV response generator
125
		return orderService.downloadReportInCsv(baos, rows, "Franchisee_Account_Creation_Template");
126
	}
127
 
128
 
129
	@PostMapping(value = "/franchisee/account/upload")
130
	public String uploadFranchiseeAccount(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
131
			throws Exception {
132
		List<FranchiseeBankAccount> accounts = this.parseExcel(file);
133
		for (FranchiseeBankAccount acc : accounts) {
134
			franchiseeBankAccountRepository.persist(acc);
135
		}
136
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
137
		return "response";
138
	}
139
 
140
 
141
	private List<FranchiseeBankAccount> parseExcel(MultipartFile file) throws Exception {
142
		List<FranchiseeBankAccount> accounts = new ArrayList<>();
143
		Workbook workbook = new XSSFWorkbook(file.getInputStream());
144
		Sheet sheet = workbook.getSheetAt(0);
145
 
146
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
147
			Row row = sheet.getRow(i);
148
			if (row == null) continue;
149
 
150
			FranchiseeBankAccount acc = new FranchiseeBankAccount();
151
 
152
			acc.setFofoId((int) getNumericCellValue(row.getCell(0)));
153
			acc.setPersonName(getStringCellValue(row.getCell(1)));
154
			acc.setContactNumber(getStringCellValue(row.getCell(2)));
155
			acc.setEmail(getStringCellValue(row.getCell(3)));
156
			acc.setTid(getStringCellValue(row.getCell(4)));
157
			acc.setMid(getStringCellValue(row.getCell(5)));
158
			acc.setBankAccountNumber(getStringCellValue(row.getCell(6)));
159
			acc.setIfsc(getStringCellValue(row.getCell(7)));
160
 
161
			acc.setContactId(null);
162
			acc.setAccountId(null);
163
 
164
			accounts.add(acc);
165
		}
166
		workbook.close();
167
		return accounts;
168
	}
169
 
170
	private double getNumericCellValue(Cell cell) {
171
		return cell != null ? cell.getNumericCellValue() : 0;
172
	}
173
 
174
	private String getStringCellValue(Cell cell) {
175
		if (cell == null) return null;
176
 
177
		switch (cell.getCellType()) {
178
			case Cell.CELL_TYPE_STRING:
179
				return cell.getStringCellValue();
180
			case Cell.CELL_TYPE_NUMERIC:
181
				return String.valueOf((long) cell.getNumericCellValue()); // Avoid scientific notation
182
			case Cell.CELL_TYPE_BOOLEAN:
183
				return String.valueOf(cell.getBooleanCellValue());
184
			case Cell.CELL_TYPE_FORMULA:
185
				return cell.getCellFormula();
186
			case Cell.CELL_TYPE_BLANK:
187
				return null;
188
			default:
189
				return null;
190
		}
191
	}
192
 
193
	@RequestMapping(value = "/franchisee/account/edit/{id}", method = RequestMethod.GET)
194
	@ResponseBody
195
	public FranchiseeBankAccount editFranchiseeAccount(@PathVariable("id") int id) {
196
		return franchiseeBankAccountRepository.selectById(id);
197
	}
198
 
199
	@RequestMapping(value = "/franchisee/account/update", method = RequestMethod.POST)
200
	@ResponseBody
201
	public ResponseEntity<?> updateFranchiseeAccount(HttpServletRequest request, @RequestBody FranchiseeAccountModel accountModel, Model model) {
202
		FranchiseeBankAccount franchiseeBankAccount = franchiseeBankAccountRepository.selectById(accountModel.getId());
203
 
204
		franchiseeBankAccount.setPersonName(accountModel.getPersonName());
205
		franchiseeBankAccount.setContactNumber(accountModel.getContactNumber());
206
		franchiseeBankAccount.setEmail(accountModel.getEmail());
207
		franchiseeBankAccount.setBankAccountNumber(accountModel.getBankAccountNumber());
208
		franchiseeBankAccount.setIfsc(accountModel.getIfsc());
209
		franchiseeBankAccount.setTid(accountModel.getTid());
210
		franchiseeBankAccount.setMid(accountModel.getMid());
211
		return responseSender.ok(true);
212
	}
213
 
214
 
215
 
37405 amit 216
 
217
	/** State list for the form dropdowns, from inventory.statemaster - never hardcoded in the view. */
218
	private java.util.List<String> stateNames() {
219
		return stateRepository.selectAll().stream().map(com.spice.profitmandi.dao.entity.inventory.State::getName)
220
				.sorted().collect(java.util.stream.Collectors.toList());
221
	}
22111 ashik.ali 222
}