Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
23612 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.io.IOException;
30162 manish 4
import java.time.LocalDate;
5
import java.time.LocalDateTime;
31377 tejbeer 6
import java.time.format.DateTimeFormatter;
26298 tejbeer 7
import java.util.ArrayList;
30162 manish 8
import java.util.Arrays;
26164 amit.gupta 9
import java.util.HashMap;
26298 tejbeer 10
import java.util.List;
26063 amit.gupta 11
import java.util.Map;
23612 amit.gupta 12
 
13
import javax.servlet.http.HttpServletRequest;
14
 
15
import org.apache.http.HttpResponse;
23654 amit.gupta 16
import org.apache.logging.log4j.LogManager;
17
import org.apache.logging.log4j.Logger;
23637 amit.gupta 18
import org.springframework.beans.factory.annotation.Autowired;
23612 amit.gupta 19
import org.springframework.core.io.InputStreamResource;
20
import org.springframework.http.HttpHeaders;
21
import org.springframework.http.HttpStatus;
22
import org.springframework.http.ResponseEntity;
23
import org.springframework.stereotype.Controller;
23637 amit.gupta 24
import org.springframework.transaction.annotation.Transactional;
25
import org.springframework.ui.Model;
23612 amit.gupta 26
import org.springframework.web.bind.annotation.PathVariable;
26063 amit.gupta 27
import org.springframework.web.bind.annotation.RequestBody;
23612 amit.gupta 28
import org.springframework.web.bind.annotation.RequestMapping;
29
import org.springframework.web.bind.annotation.RequestMethod;
30162 manish 30
import org.springframework.web.bind.annotation.RequestParam;
23612 amit.gupta 31
 
32
import com.spice.profitmandi.common.enumuration.ReporticoProject;
33
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
28106 amit.gupta 34
import com.spice.profitmandi.common.model.ReporticoUrlInfo;
23612 amit.gupta 35
import com.spice.profitmandi.common.services.ReporticoService;
30162 manish 36
import com.spice.profitmandi.common.util.FileUtil;
26298 tejbeer 37
import com.spice.profitmandi.dao.entity.auth.AuthUser;
30162 manish 38
import com.spice.profitmandi.dao.model.CollectionSummary;
26298 tejbeer 39
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
40
import com.spice.profitmandi.dao.repository.cs.CsService;
28106 amit.gupta 41
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
23784 ashik.ali 42
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
30162 manish 43
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
23798 amit.gupta 44
import com.spice.profitmandi.service.authentication.RoleManager;
30162 manish 45
import com.spice.profitmandi.service.order.OrderService;
23612 amit.gupta 46
import com.spice.profitmandi.web.model.LoginDetails;
47
import com.spice.profitmandi.web.util.CookiesProcessor;
48
 
49
@Controller
26298 tejbeer 50
@Transactional(rollbackFor = Throwable.class)
23612 amit.gupta 51
public class ReportsController {
26298 tejbeer 52
 
23637 amit.gupta 53
	@Autowired
23784 ashik.ali 54
	private RoleRepository roleRepository;
26298 tejbeer 55
 
23764 amit.gupta 56
	@Autowired
26298 tejbeer 57
	private CookiesProcessor cookiesProcessor;
28106 amit.gupta 58
 
59
	@Autowired
60
	private FofoStoreRepository fofoStoreRepository;
26298 tejbeer 61
 
23784 ashik.ali 62
	@Autowired
23652 amit.gupta 63
	private ReporticoService reporticoService;
23764 amit.gupta 64
 
23798 amit.gupta 65
	@Autowired
30162 manish 66
	private OrderRepository orderRepository;
67
	@Autowired
23798 amit.gupta 68
	private RoleManager roleManager;
30162 manish 69
 
70
	@Autowired
71
	private OrderService  orderService;
26298 tejbeer 72
 
73
	@Autowired
74
	private CsService csService;
75
 
76
	@Autowired
77
	private AuthRepository authRepository;
78
 
30162 manish 79
	private static final Logger Logger = LogManager.getLogger(OrderController.class);
23612 amit.gupta 80
 
26063 amit.gupta 81
	@RequestMapping(value = "/reports/{projectName}/{fileName}")
23764 amit.gupta 82
	public ResponseEntity<?> fetchReport(HttpServletRequest request, @PathVariable String fileName,
26298 tejbeer 83
			@PathVariable ReporticoProject projectName, @RequestBody(required = false) Map<String, String> paramsMap)
23764 amit.gupta 84
			throws ProfitMandiBusinessException, UnsupportedOperationException, IOException {
23612 amit.gupta 85
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
86
		HttpResponse response;
26298 tejbeer 87
		if (roleManager.isAdmin(loginDetails.getRoleIds())) {
88
 
89
			if (fileName.equalsIgnoreCase("LeadsReport")) {
90
				if (paramsMap == null) {
91
					paramsMap = new HashMap<String, String>();
92
				}
26460 amit.gupta 93
				Map<Integer, List<Integer>> mapping = csService.getL2L1Mapping();
26298 tejbeer 94
 
95
				AuthUser authUser = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
96
 
97
				List<Integer> authIds = mapping.get(authUser.getId());
98
				if (authIds == null) {
99
					authIds = new ArrayList<>();
100
				}
101
				authIds.add(authUser.getId());
102
 
103
				paramsMap.put("authId", authIds + "");
104
			}
23612 amit.gupta 105
		} else {
26298 tejbeer 106
			if (paramsMap == null) {
26164 amit.gupta 107
				paramsMap = new HashMap<String, String>();
108
			}
26298 tejbeer 109
 
26158 amit.gupta 110
			paramsMap.put("MANUAL_fofoId", loginDetails.getFofoId() + "");
26298 tejbeer 111
 
23612 amit.gupta 112
		}
26298 tejbeer 113
		response = getAdminReportFile(loginDetails.getEmailId(), projectName, fileName + ".xml", paramsMap);
23612 amit.gupta 114
		HttpHeaders headers = new HttpHeaders();
23764 amit.gupta 115
		InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
23765 amit.gupta 116
		headers.set("Content-Type", "application/vnd.ms-excel");
26298 tejbeer 117
		headers.set("Content-disposition", "inline; filename=report-" + fileName + ".csv");
23766 amit.gupta 118
		headers.setContentLength(response.getEntity().getContentLength());
23764 amit.gupta 119
		return new ResponseEntity<InputStreamResource>(is, headers, HttpStatus.OK);
23612 amit.gupta 120
	}
23637 amit.gupta 121
 
26298 tejbeer 122
	private HttpResponse getAdminReportFile(String email, ReporticoProject projectName, String fileName,
123
			Map<String, String> reportParams) throws ProfitMandiBusinessException, IOException {
26063 amit.gupta 124
		return reporticoService.getReportFile(projectName, fileName, reportParams);
125
	}
30162 manish 126
 
127
	@RequestMapping(value = "/collectionSummary", method = RequestMethod.GET)
128
	public String getCollectionSummary(HttpServletRequest request,
129
			Model model) throws ProfitMandiBusinessException {
130
		LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
131
 
132
		LocalDateTime currentDate = LocalDate.now().atStartOfDay();
133
		LocalDateTime currentStartMonth = currentDate.minusDays(30).toLocalDate().atStartOfDay();
134
 
135
		List<CollectionSummary> collectionSummaryList =orderRepository.selectCollectionSummary(fofoDetails.getFofoId(),currentStartMonth, currentDate);
136
 
137
		Logger.info("CollectionSummaryList {}", collectionSummaryList);
138
 
139
		model.addAttribute("startDate",currentDate.minusDays(30).toLocalDate());
140
		model.addAttribute("endDate", LocalDate.now());
141
		model.addAttribute("collectionSummaryList", collectionSummaryList);
142
		return "partner-collection-summary";
143
	}
144
 
145
	@RequestMapping(value = "/downloadCollectionSummary", method = RequestMethod.GET)
146
	public ResponseEntity<?>  getDownloadCollectionSummary(HttpServletRequest request,
147
			@RequestParam(name = "startDate", required = true, defaultValue = "") LocalDate startDate,
148
			@RequestParam(name = "endDate", required = true, defaultValue = "") LocalDate endDate,Model model) throws Exception {
149
		LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
150
		List<List<?>> rows = new ArrayList<>();
26063 amit.gupta 151
 
30162 manish 152
 
153
		List<CollectionSummary> collectionSummaryList =orderRepository.selectCollectionSummary(fofoDetails.getFofoId(),startDate.atStartOfDay(), endDate.atStartOfDay());
154
 
155
		Logger.info("CollectionSummaryList {}", collectionSummaryList);
31377 tejbeer 156
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
30162 manish 157
 
158
		for( CollectionSummary cs : collectionSummaryList) {
159
 
31377 tejbeer 160
			rows.add(Arrays.asList(cs.getDate().format(
161
					dateTimeFormatter), cs.getReferenceType(),cs.getCash(),cs.getPinelabs(),cs.getBajajFinserv(),cs.getHomeCredit(),cs.getPaytm(),
30162 manish 162
					cs.getCapitalFirst(),cs.getZestMoney(),cs.getSamsungSure(),cs.getTotalAmount()));
163
 
164
 
165
		}
166
 
167
		org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil
168
				.getCSVByteStream(Arrays.asList("Date", "Reference Type", "Cash","Pinelabs", "Bajaj Finservice", "Home Credit", "Paymt",
169
						"Capital First", "Zest Money", "Samsung Sure", "Total Amount"), rows);
170
 
171
		ResponseEntity<?> responseEntity = orderService.downloadReportInCsv(baos, rows,"Collection Summary Report");
172
 
173
 
174
		return responseEntity;
175
	}
176
 
177
 
178
 
179
 
180
 
23822 amit.gupta 181
	@RequestMapping(value = "/reports", method = RequestMethod.GET)
28106 amit.gupta 182
	public String reports(HttpServletRequest httpServletRequest, Model model) throws ProfitMandiBusinessException, UnsupportedOperationException, IOException {
183
 
28137 amit.gupta 184
		//LoginDetails loginDetails = cookiesProcessor.getCookiesObject(httpServletRequest);
28106 amit.gupta 185
		Map<ReporticoProject, List<ReporticoUrlInfo>> returnMap = ReporticoProject.reporticoProjectMap;
28137 amit.gupta 186
		/*if(fofoStoreRepository.getWarehousePartnerMap().get(7720).stream().filter(x->x.getId()==loginDetails.getFofoId()).count() > 0) {
28106 amit.gupta 187
			Map<ReporticoProject, List<ReporticoUrlInfo>> returnMap1 = new HashMap<ReporticoProject, List<ReporticoUrlInfo>>();
188
			returnMap1.put(ReporticoProject.FOCO, returnMap.get(ReporticoProject.FOCO));
189
			returnMap1.put(ReporticoProject.FOCOR, returnMap.get(ReporticoProject.FOCOR).stream().skip(1).collect(Collectors.toList()));
28107 amit.gupta 190
			returnMap = returnMap1;
28137 amit.gupta 191
		}*/
28107 amit.gupta 192
		model.addAttribute("reporticoProjectMap", returnMap);
28106 amit.gupta 193
 
23637 amit.gupta 194
		return "reports";
195
	}
23612 amit.gupta 196
}