Subversion Repositories SmartDukaan

Rev

Rev 23568 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23568 Rev 34861
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
4
import com.spice.profitmandi.common.model.FranchiseeAccountModel;
3
import javax.servlet.http.HttpServletRequest;
5
import com.spice.profitmandi.common.util.FileUtil;
4
 
-
 
-
 
6
import com.spice.profitmandi.common.web.util.ResponseSender;
-
 
7
import com.spice.profitmandi.dao.entity.transaction.FranchiseeBankAccount;
5
import org.apache.logging.log4j.Logger;
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;
6
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.LogManager;
-
 
17
import org.apache.logging.log4j.Logger;
-
 
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;
7
import org.springframework.beans.factory.annotation.Autowired;
23
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.http.HttpHeaders;
24
import org.springframework.http.HttpHeaders;
9
import org.springframework.http.HttpStatus;
25
import org.springframework.http.HttpStatus;
10
import org.springframework.http.MediaType;
26
import org.springframework.http.MediaType;
11
import org.springframework.http.ResponseEntity;
27
import org.springframework.http.ResponseEntity;
12
import org.springframework.stereotype.Controller;
28
import org.springframework.stereotype.Controller;
13
import org.springframework.transaction.annotation.Transactional;
29
import org.springframework.transaction.annotation.Transactional;
14
import org.springframework.ui.Model;
30
import org.springframework.ui.Model;
15
import org.springframework.web.bind.annotation.PathVariable;
31
import org.springframework.web.bind.annotation.*;
16
import org.springframework.web.bind.annotation.RequestMapping;
32
import org.springframework.web.multipart.MultipartFile;
17
import org.springframework.web.bind.annotation.RequestMethod;
-
 
18
 
33
 
19
import com.spice.profitmandi.dao.model.FofoForm;
34
import javax.mail.MessagingException;
20
import com.spice.profitmandi.dao.repository.dtr.Mongo;
35
import javax.servlet.http.HttpServletRequest;
-
 
36
import java.io.IOException;
21
import com.spice.profitmandi.dao.util.FofoDocumentsGenerator;
37
import java.util.ArrayList;
-
 
38
import java.util.Arrays;
-
 
39
import java.util.List;
22
 
40
 
23
@Controller
41
@Controller
24
@Transactional
42
@Transactional
25
public class FofoController {
43
public class FofoController {
26
	
44
	
27
	private static final Logger LOGGER = LogManager.getLogger(FofoController.class);
45
	private static final Logger LOGGER = LogManager.getLogger(FofoController.class);
28
 
46
 
29
	@Autowired
47
	@Autowired
30
	private Mongo mongoClient;
48
	private Mongo mongoClient;
31
	
49
 
-
 
50
	@Autowired
-
 
51
	AuthRepository authRepository;
-
 
52
	@Autowired
-
 
53
	OrderService orderService;
-
 
54
	@Autowired
-
 
55
	FranchiseeBankAccountRepository franchiseeBankAccountRepository;
-
 
56
	@Autowired
-
 
57
	private ResponseSender<?> responseSender;
32
	@Autowired
58
	@Autowired
33
	private FofoDocumentsGenerator generator;
59
	private FofoDocumentsGenerator generator;
-
 
60
	@Autowired
-
 
61
	private CookiesProcessor cookiesProcessor;
-
 
62
	@Autowired
-
 
63
	private MVCResponseSender mvcResponseSender;
34
	
64
	
35
	@RequestMapping(value = "/fofo", method = RequestMethod.GET)
65
	@RequestMapping(value = "/fofo", method = RequestMethod.GET)
36
	public String getAll(HttpServletRequest request, Model model) throws Throwable {
66
	public String getAll(HttpServletRequest request, Model model) throws Throwable {
37
		model.addAttribute("fofoForms", mongoClient.getFofoForms(0, 50));
67
		model.addAttribute("fofoForms", mongoClient.getFofoForms(0, 50));
38
		return "fofo-index";
68
		return "fofo-index";
Line 55... Line 85...
55
		FofoForm ff = mongoClient.getFofoForm(fofoId);
85
		FofoForm ff = mongoClient.getFofoForm(fofoId);
56
		model.addAttribute("fofoForm", mongoClient.getFofoFormJsonStringByFofoId(fofoId));
86
		model.addAttribute("fofoForm", mongoClient.getFofoFormJsonStringByFofoId(fofoId));
57
		model.addAttribute("email", ff.getRegisteredEmail1());
87
		model.addAttribute("email", ff.getRegisteredEmail1());
58
		return "fofo-form";
88
		return "fofo-form";
59
	}
89
	}
-
 
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);
60
	
208
	}
-
 
209
 
-
 
210
 
-
 
211
 
61
}
212
}