Subversion Repositories SmartDukaan

Rev

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