Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21615 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
23568 govind 3
import java.io.IOException;
4
import java.net.URISyntaxException;
24288 amit.gupta 5
import java.time.DayOfWeek;
6
import java.time.LocalDate;
24276 amit.gupta 7
import java.time.LocalDateTime;
24288 amit.gupta 8
import java.time.temporal.TemporalAdjusters;
24339 amit.gupta 9
import java.util.ArrayList;
23884 amit.gupta 10
import java.util.List;
23568 govind 11
 
22086 amit.gupta 12
import javax.servlet.http.HttpServletRequest;
13
 
23786 amit.gupta 14
import org.apache.logging.log4j.LogManager;
23568 govind 15
import org.apache.logging.log4j.Logger;
24321 amit.gupta 16
import org.json.JSONArray;
17
import org.json.JSONObject;
22481 ashik.ali 18
import org.springframework.beans.factory.annotation.Autowired;
23379 ashik.ali 19
import org.springframework.beans.factory.annotation.Value;
21615 kshitij.so 20
import org.springframework.stereotype.Controller;
22481 ashik.ali 21
import org.springframework.transaction.annotation.Transactional;
22073 ashik.ali 22
import org.springframework.ui.Model;
21615 kshitij.so 23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
24098 tejbeer 25
import org.springframework.web.bind.annotation.RequestParam;
21615 kshitij.so 26
 
22481 ashik.ali 27
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24203 amit.gupta 28
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24098 tejbeer 29
import com.spice.profitmandi.dao.entity.dtr.NotificationData;
22654 ashik.ali 30
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
24288 amit.gupta 31
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
22481 ashik.ali 32
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24098 tejbeer 33
import com.spice.profitmandi.dao.repository.dtr.NotificationPanelRepository;
24203 amit.gupta 34
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
24288 amit.gupta 35
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
23884 amit.gupta 36
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
24336 amit.gupta 37
import com.spice.profitmandi.service.PartnerInvestmentService;
23844 amit.gupta 38
import com.spice.profitmandi.service.authentication.RoleManager;
23884 amit.gupta 39
import com.spice.profitmandi.service.inventory.InventoryService;
40
import com.spice.profitmandi.service.transaction.TransactionService;
41
import com.spice.profitmandi.service.wallet.WalletService;
22481 ashik.ali 42
import com.spice.profitmandi.web.model.LoginDetails;
43
import com.spice.profitmandi.web.util.CookiesProcessor;
24263 tejbeer 44
import com.spice.profitmandi.web.util.MVCResponseSender;
22481 ashik.ali 45
 
21615 kshitij.so 46
@Controller
22481 ashik.ali 47
@Transactional(rollbackFor = Throwable.class)
21615 kshitij.so 48
public class DashboardController {
23923 amit.gupta 49
 
23379 ashik.ali 50
	@Value("${web.api.host}")
51
	private String webApiHost;
23923 amit.gupta 52
 
24072 amit.gupta 53
	@Value("${web.api.scheme}")
54
	private String webApiScheme;
24288 amit.gupta 55
 
24078 amit.gupta 56
	@Value("${web.api.root}")
57
	private String webApiRoot;
58
 
23379 ashik.ali 59
	@Value("${web.api.port}")
60
	private int webApiPort;
21615 kshitij.so 61
 
22481 ashik.ali 62
	@Autowired
22927 ashik.ali 63
	private CookiesProcessor cookiesProcessor;
23923 amit.gupta 64
 
23568 govind 65
	@Autowired
23786 amit.gupta 66
	private RoleManager roleManager;
23923 amit.gupta 67
 
23838 ashik.ali 68
	@Autowired
69
	private FofoStoreRepository fofoStoreRepository;
23884 amit.gupta 70
 
71
	@Autowired
72
	private WalletService walletService;
23923 amit.gupta 73
 
23884 amit.gupta 74
	@Autowired
75
	private InventoryService inventoryService;
24288 amit.gupta 76
 
23923 amit.gupta 77
 
23884 amit.gupta 78
	@Autowired
79
	private OrderRepository orderRepository;
24288 amit.gupta 80
 
81
 
82
	@Autowired
83
	private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
24336 amit.gupta 84
 
85
	@Autowired
86
	private PartnerInvestmentService partnerInvestmentService;
24288 amit.gupta 87
 
23923 amit.gupta 88
	/*
89
	 * @Autowired private ScanRepository scanRepository;
90
	 */
91
 
23884 amit.gupta 92
	@Autowired
93
	private TransactionService transactionService;
24263 tejbeer 94
 
24098 tejbeer 95
	@Autowired
24263 tejbeer 96
	private MVCResponseSender mvcResponseSender;
97
 
98
	@Autowired
24098 tejbeer 99
	private NotificationPanelRepository notificationPanelRepository;
23923 amit.gupta 100
 
24203 amit.gupta 101
	@Autowired
102
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
103
 
23568 govind 104
	private static final Logger LOGGER = LogManager.getLogger(DashboardController.class);
23923 amit.gupta 105
 
21615 kshitij.so 106
	@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
24312 amit.gupta 107
	public String dashboard(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset,
24098 tejbeer 108
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
23923 amit.gupta 109
			throws ProfitMandiBusinessException, URISyntaxException, IOException {
110
		// LOGGER.info("scanRepository.selectScansByInventoryItemId(1)",
111
		// scanRepository.selectScansByInventoryItemId(1));
23884 amit.gupta 112
		LOGGER.info("In Dashboard");
22927 ashik.ali 113
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
23923 amit.gupta 114
 
22481 ashik.ali 115
		FofoStore fofoStore = null;
116
		try {
22927 ashik.ali 117
			fofoStore = fofoStoreRepository.selectByRetailerId(loginDetails.getFofoId());
22481 ashik.ali 118
		} catch (ProfitMandiBusinessException e) {
22927 ashik.ali 119
			LOGGER.error("FofoStore Code not found of fofoId {}", loginDetails.getFofoId());
22481 ashik.ali 120
		}
23884 amit.gupta 121
 
24312 amit.gupta 122
		float sale = 0;
23923 amit.gupta 123
		if (roleManager.isAdmin(loginDetails.getRoleIds())) {
124
			model.addAttribute("showAlert", false);
24263 tejbeer 125
			model.addAttribute("sale", sale);
126
			List<NotificationData> notificationData = null;
127
			long size = 0;
128
 
129
			notificationData = notificationPanelRepository.selectAllNotificationData(offset, limit);
130
			size = notificationPanelRepository.selectAllCount();
131
			LOGGER.info("notification_data {}", notificationData);
132
 
133
			LOGGER.info("notification_data {}", size);
134
 
135
			if (!notificationData.isEmpty()) {
136
 
137
				model.addAttribute("notificationData", notificationData);
138
				LOGGER.info("notificationdata", notificationData);
139
				model.addAttribute("start", offset + 1);
140
				model.addAttribute("size", size);
141
				model.addAttribute("url", "/getPaginatedNotificationData");
142
 
143
				LOGGER.info("start {}", offset + 1);
144
				LOGGER.info("SIZE {}", size);
145
 
146
				if (notificationData.size() < limit) {
147
					model.addAttribute("end", offset + notificationData.size());
148
					LOGGER.info("SIZE2 {}", offset + notificationData.size());
149
				} else {
150
					model.addAttribute("end", offset + limit);
151
 
152
					LOGGER.info("end {}", size);
153
 
154
				}
155
 
156
			} else {
157
 
158
				model.addAttribute("notificationData", notificationData);
159
				model.addAttribute("size", size);
160
				LOGGER.info("sizeOriginal {}", size);
161
 
162
			}
23923 amit.gupta 163
		} else if (roleManager.isPartner(loginDetails.getRoleIds())) {
24312 amit.gupta 164
 
24263 tejbeer 165
			List<NotificationData> notificationData = null;
166
			long size = 0;
167
 
168
			notificationData = notificationPanelRepository.selectAllNotificationDataByActiveFlag(offset, limit, true);
169
			size = notificationPanelRepository.selectCountByActiveFlag(true);
170
			LOGGER.info("notification_data {}", notificationData);
171
 
172
			LOGGER.info("notification_data {}", size);
173
 
174
			if (!notificationData.isEmpty()) {
175
 
176
				model.addAttribute("notificationData", notificationData);
177
				LOGGER.info("notificationdata", notificationData);
178
				model.addAttribute("start", offset + 1);
179
				model.addAttribute("size", size);
180
				model.addAttribute("url", "/getPaginatedNotificationData");
181
 
182
				LOGGER.info("start {}", offset + 1);
183
				LOGGER.info("SIZE {}", size);
184
 
185
				if (notificationData.size() < limit) {
186
					model.addAttribute("end", offset + notificationData.size());
187
					LOGGER.info("SIZE2 {}", offset + notificationData.size());
188
				} else {
189
					model.addAttribute("end", offset + limit);
190
 
191
					LOGGER.info("end {}", size);
192
 
193
				}
194
 
195
			} else {
196
 
197
				model.addAttribute("notificationData", notificationData);
198
				model.addAttribute("size", size);
199
				LOGGER.info("sizeOriginal {}", size);
200
 
201
			}
24356 amit.gupta 202
			boolean isInvestmentOK = partnerInvestmentService.isInvestmentOk(loginDetails.getFofoId(), 10, 30);
203
			if(!isInvestmentOK) {
204
				model.addAttribute("showAlert", true);
205
			}
23884 amit.gupta 206
		}
24288 amit.gupta 207
		LocalDate endDate = LocalDate.now().minusDays(1);
208
		LocalDate weekStartDate = endDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
24336 amit.gupta 209
		LocalDate startDate = weekStartDate.minusWeeks(2);
24339 amit.gupta 210
		List<PartnerDailyInvestment> partnerInvestments = new ArrayList<>();
24356 amit.gupta 211
		PartnerDailyInvestment pdi = partnerInvestmentService.getInvestment(loginDetails.getFofoId(), 0);
212
		partnerInvestments.add(pdi);
24339 amit.gupta 213
		partnerInvestments.addAll(partnerDailyInvestmentRepository.selectAll(loginDetails.getFofoId(), startDate, endDate));
24312 amit.gupta 214
 
24356 amit.gupta 215
		model.addAttribute("partnerInvestment", pdi);
24312 amit.gupta 216
		model.addAttribute("investmentChart", this.getInvestmentChartData(partnerInvestments));
23848 ashik.ali 217
		model.addAttribute("fofoStore", fofoStore);
24356 amit.gupta 218
		model.addAttribute("fofoStore", fofoStore);
23918 amit.gupta 219
		model.addAttribute("walletAmount");
22086 amit.gupta 220
		model.addAttribute("appContextPath", request.getContextPath());
23796 amit.gupta 221
		model.addAttribute("isAdmin", roleManager.isAdmin(loginDetails.getRoleIds()));
23379 ashik.ali 222
		model.addAttribute("webApiHost", webApiHost);
223
		model.addAttribute("webApiPort", webApiPort);
24072 amit.gupta 224
		model.addAttribute("webApiScheme", webApiScheme);
24077 amit.gupta 225
		model.addAttribute("webApiRoot", webApiRoot);
24203 amit.gupta 226
		model.addAttribute("hasGift", hasGift(loginDetails.getFofoId()));
227
		model.addAttribute("giftItemId", ProfitMandiConstants.GIFT_ID);
24248 govind 228
		model.addAttribute("sale", sale);
24098 tejbeer 229
 
23923 amit.gupta 230
		// LOGGER.info("loginDetails.getFofoId()"+loginDetails.getFofoId());
231
		// inventoryService.prebookingAvailabilitySendMessage(loginDetails.getFofoId());
21615 kshitij.so 232
		return "dashboard";
233
	}
23923 amit.gupta 234
 
24312 amit.gupta 235
	private String getInvestmentChartData(List<PartnerDailyInvestment> partnerInvestments) {
24321 amit.gupta 236
 
237
 
238
		JSONObject scale = new JSONObject().put("scaleLabel", new JSONObject().put("scaleLabel", "Amount in Rs"));
239
		JSONObject titleObject = new JSONObject()
240
				.put("display", true)
241
				.put("text", "Investment Overview").put("display", true);
242
 
243
		JSONObject barData = new JSONObject();
244
 
24325 amit.gupta 245
		JSONObject walletStack = new JSONObject().put("backgroundColor","rgba(255, 99, 132, 0.2)")
24323 amit.gupta 246
				.put("label", "Wallet").put("stack", "Stack1").put("data", new JSONArray());
24321 amit.gupta 247
		JSONObject inStockStack = new JSONObject().put("label", "In Stock").put("backgroundColor","rgba(54, 162, 235, 0.2)")
24325 amit.gupta 248
				.put("stack", "Stack1").put("data", new JSONArray());
249
		JSONObject salesStack = new JSONObject().put("backgroundColor","rgba(255, 206, 86, 0.2)")
24323 amit.gupta 250
				.put("label", "Sales").put("stack", "Stack1").put("data", new JSONArray());
24325 amit.gupta 251
		JSONObject grnPendingStack = new JSONObject().put("backgroundColor","rgba(75, 192, 192, 0.2)")
24323 amit.gupta 252
				.put("label", "Grn Pending").put("stack", "Stack1").put("data", new JSONArray());
24325 amit.gupta 253
		JSONObject  billingPendingStack = new JSONObject().put("backgroundColor","rgba(153, 102, 255, 0.2)")
24323 amit.gupta 254
				.put("label", "Billing Pending").put("stack", "Stack1").put("data", new JSONArray());
24325 amit.gupta 255
		JSONObject inTransitStack = new JSONObject().put("backgroundColor","rgba(255, 159, 64, 0.2)")
24324 amit.gupta 256
				.put("label", "Returned").put("stack", "Stack1").put("data", new JSONArray());
24321 amit.gupta 257
 
24327 amit.gupta 258
		JSONObject minInvestmentDataSet = new JSONObject().put("label", "Min. Ivestment").put("data", new JSONArray()).put("type", "line");
24321 amit.gupta 259
		JSONArray dateLabels = new JSONArray();
260
 
24312 amit.gupta 261
		for (PartnerDailyInvestment pdi : partnerInvestments) {
24321 amit.gupta 262
			dateLabels = dateLabels.put(pdi.getDate().toString());
263
			walletStack.getJSONArray("data").put((int)pdi.getWalletAmount());
264
			inStockStack.getJSONArray("data").put((int)pdi.getInStockAmount());
265
			salesStack.getJSONArray("data").put((int)pdi.getSalesAmount());
266
			grnPendingStack.getJSONArray("data").put((int)pdi.getUnbilledAmount());
267
			billingPendingStack.getJSONArray("data").put((int)pdi.getGrnPendingAmount());
268
			inTransitStack.getJSONArray("data").put((int)pdi.getReturnInTransitAmount());
24327 amit.gupta 269
			minInvestmentDataSet.getJSONArray("data").put((int)pdi.getMinInvestment());
24312 amit.gupta 270
		}
271
 
24321 amit.gupta 272
		JSONArray barDataSetsArray = new JSONArray().put(walletStack).put(inStockStack)
273
				.put(salesStack).put(grnPendingStack).put(billingPendingStack).put(inTransitStack);
274
		barData.put("labels", dateLabels).put("datasets", barDataSetsArray);
275
		scale.put("yAxes", new JSONArray().put(new JSONObject().put("stacked", true)))
276
			.put("xAxes", new JSONArray().put(new JSONObject().put("stacked", true)));
277
 
278
		JSONObject barOptions = new JSONObject()
24324 amit.gupta 279
				.put("title", titleObject).put("responsive", true).put("scales", scale)
24321 amit.gupta 280
				.put("tooltips", new JSONObject().put("mode", "index").put("intersect", false));
281
 
282
		JSONObject chartJSOn = new JSONObject().put("type", "bar").put("data", barData).put("options", barOptions);
283
		return chartJSOn.toString();
24312 amit.gupta 284
	}
285
 
24288 amit.gupta 286
	// This method is currently hardcoded to faciliate watches sold as gift.
24203 amit.gupta 287
	private boolean hasGift(int fofoId) {
288
		try {
24288 amit.gupta 289
			return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId)
290
					.getAvailability() > 0;
24203 amit.gupta 291
		} catch (ProfitMandiBusinessException e) {
292
			return false;
293
		}
294
	}
24288 amit.gupta 295
 
24098 tejbeer 296
	@RequestMapping(value = "/getPaginatedNotificationData", method = RequestMethod.GET)
297
	public String getPaginatedNotificationData(HttpServletRequest request,
298
			@RequestParam(name = "offset", defaultValue = "0") int offset,
299
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {
24263 tejbeer 300
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
24098 tejbeer 301
 
24263 tejbeer 302
		if (roleManager.isAdmin(loginDetails.getRoleIds())) {
303
			List<NotificationData> notificationData = null;
24098 tejbeer 304
 
24263 tejbeer 305
			notificationData = notificationPanelRepository.selectAllNotificationData(offset, limit);
24098 tejbeer 306
 
24263 tejbeer 307
			LOGGER.info("notification_data {}", notificationData);
24098 tejbeer 308
 
24263 tejbeer 309
			if (!notificationData.isEmpty()) {
24098 tejbeer 310
 
24263 tejbeer 311
				model.addAttribute("notificationData", notificationData);
312
				model.addAttribute("url", "/getPaginatedNotificationData");
313
 
314
			} else {
315
				model.addAttribute("notificationData", notificationData);
316
 
317
			}
318
		} else if (roleManager.isPartner(loginDetails.getRoleIds())) {
319
 
320
			List<NotificationData> notificationData = null;
321
 
322
			notificationData = notificationPanelRepository.selectAllNotificationDataByActiveFlag(offset, limit, true);
323
			LOGGER.info("notification_data {}", notificationData);
324
 
325
			if (!notificationData.isEmpty()) {
326
 
327
				model.addAttribute("notificationData", notificationData);
328
				model.addAttribute("url", "/getPaginatedNotificationData");
329
 
330
			} else {
331
				model.addAttribute("notificationData", notificationData);
332
 
333
			}
24098 tejbeer 334
		}
335
 
336
		return "dashboard-paginated";
337
	}
24288 amit.gupta 338
 
22354 ashik.ali 339
	@RequestMapping(value = "/contactUs", method = RequestMethod.GET)
23923 amit.gupta 340
	public String contactUs(HttpServletRequest request, Model model) throws Throwable {
22354 ashik.ali 341
		model.addAttribute("appContextPath", request.getContextPath());
342
		return "contact-us";
343
	}
23923 amit.gupta 344
 
345
	/*
346
	 * private List<PaymentOption> getPaymentOptions(int fofoId){ List<Integer>
347
	 * paymentOptionIds =
24288 amit.gupta 348
	 * fofoPartnerPaymentOptionRepository.selectPaymentOptionIdsByFofoId(fofoId) ;
349
	 * if(paymentOptionIds.isEmpty()){ return new ArrayList<>(); } return
23923 amit.gupta 350
	 * paymentOptionRepository.selectByIds(new HashSet<>(paymentOptionIds)); }
351
	 */
352
 
24263 tejbeer 353
	@RequestMapping(value = "/inactiveNotificationData", method = RequestMethod.POST)
354
	public String inactiveNotificationData(HttpServletRequest request,
355
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
356
 
357
		NotificationData notificationData = notificationPanelRepository.selectById(id);
358
		if (notificationData != null) {
359
 
360
			notificationData.setActive(false);
361
			LOGGER.info("notification" + notificationData);
362
			notificationPanelRepository.persist(notificationData);
363
 
364
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
365
		} else {
366
			model.addAttribute("response", mvcResponseSender.createResponseString(false));
367
 
368
		}
369
		return "response";
370
	}
371
 
372
	@RequestMapping(value = "/activeNotificationData", method = RequestMethod.POST)
373
	public String activeNotificationData(HttpServletRequest request,
374
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
375
 
376
		NotificationData notificationData = notificationPanelRepository.selectById(id);
377
		if (notificationData != null) {
378
 
379
			notificationData.setActive(true);
380
			notificationData.setCreated_timestamp(LocalDateTime.now());
381
			LOGGER.info("notification" + notificationData);
382
			notificationPanelRepository.persist(notificationData);
383
 
384
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
385
		} else {
386
			model.addAttribute("response", mvcResponseSender.createResponseString(false));
387
 
388
		}
389
		return "response";
390
	}
391
 
21615 kshitij.so 392
}