Subversion Repositories SmartDukaan

Rev

Rev 24338 | Rev 24352 | 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
			}
24271 amit.gupta 186
			totalInvestedAmount = walletAmount + inStockAmount + unbilledStockAmount + grnPendingStockAmount + sale;
23923 amit.gupta 187
			shortPercentage = ((fofoStore.getMinimumInvestment() - totalInvestedAmount)
188
					/ fofoStore.getMinimumInvestment()) * 100;
23944 amit.gupta 189
			model.addAttribute("showAlert", shortPercentage > 10);
23936 tejbeer 190
			minimumInvestment = fofoStore.getMinimumInvestment();
24263 tejbeer 191
 
192
			// debitNoteRepository.se
193
			List<NotificationData> notificationData = null;
194
			long size = 0;
195
 
196
			notificationData = notificationPanelRepository.selectAllNotificationDataByActiveFlag(offset, limit, true);
197
			size = notificationPanelRepository.selectCountByActiveFlag(true);
198
			LOGGER.info("notification_data {}", notificationData);
199
 
200
			LOGGER.info("notification_data {}", size);
201
 
202
			if (!notificationData.isEmpty()) {
203
 
204
				model.addAttribute("notificationData", notificationData);
205
				LOGGER.info("notificationdata", notificationData);
206
				model.addAttribute("start", offset + 1);
207
				model.addAttribute("size", size);
208
				model.addAttribute("url", "/getPaginatedNotificationData");
209
 
210
				LOGGER.info("start {}", offset + 1);
211
				LOGGER.info("SIZE {}", size);
212
 
213
				if (notificationData.size() < limit) {
214
					model.addAttribute("end", offset + notificationData.size());
215
					LOGGER.info("SIZE2 {}", offset + notificationData.size());
216
				} else {
217
					model.addAttribute("end", offset + limit);
218
 
219
					LOGGER.info("end {}", size);
220
 
221
				}
222
 
223
			} else {
224
 
225
				model.addAttribute("notificationData", notificationData);
226
				model.addAttribute("size", size);
227
				LOGGER.info("sizeOriginal {}", size);
228
 
229
			}
23884 amit.gupta 230
		}
24288 amit.gupta 231
		LocalDate endDate = LocalDate.now().minusDays(1);
232
		LocalDate weekStartDate = endDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
24336 amit.gupta 233
		LocalDate startDate = weekStartDate.minusWeeks(2);
24339 amit.gupta 234
		List<PartnerDailyInvestment> partnerInvestments = new ArrayList<>();
235
		partnerInvestments.add(partnerInvestmentService.getInvestment(loginDetails.getFofoId(), 0));
236
		partnerInvestments.addAll(partnerDailyInvestmentRepository.selectAll(loginDetails.getFofoId(), startDate, endDate));
24312 amit.gupta 237
 
238
		model.addAttribute("investmentChart", this.getInvestmentChartData(partnerInvestments));
24271 amit.gupta 239
		model.addAttribute("sale", sale);
23923 amit.gupta 240
		model.addAttribute("walletAmount", walletAmount);
241
		model.addAttribute("inStockAmount", inStockAmount);
242
		model.addAttribute("unbilledStockAmount", unbilledStockAmount);
243
		model.addAttribute("grnPendingStockAmount", grnPendingStockAmount);
244
		model.addAttribute("shortPercentage", shortPercentage);
245
		model.addAttribute("totalInvestedAmount", totalInvestedAmount);
23936 tejbeer 246
		model.addAttribute("minimumInvestmentAmount", minimumInvestment);
23951 amit.gupta 247
		model.addAttribute("returnedStockInTransit", returnedStockInTransit);
23923 amit.gupta 248
 
23848 ashik.ali 249
		model.addAttribute("fofoStore", fofoStore);
23918 amit.gupta 250
		model.addAttribute("walletAmount");
22086 amit.gupta 251
		model.addAttribute("appContextPath", request.getContextPath());
23796 amit.gupta 252
		model.addAttribute("isAdmin", roleManager.isAdmin(loginDetails.getRoleIds()));
23379 ashik.ali 253
		model.addAttribute("webApiHost", webApiHost);
254
		model.addAttribute("webApiPort", webApiPort);
24072 amit.gupta 255
		model.addAttribute("webApiScheme", webApiScheme);
24077 amit.gupta 256
		model.addAttribute("webApiRoot", webApiRoot);
24203 amit.gupta 257
		model.addAttribute("hasGift", hasGift(loginDetails.getFofoId()));
258
		model.addAttribute("giftItemId", ProfitMandiConstants.GIFT_ID);
24248 govind 259
		model.addAttribute("sale", sale);
24098 tejbeer 260
 
23923 amit.gupta 261
		// LOGGER.info("loginDetails.getFofoId()"+loginDetails.getFofoId());
262
		// inventoryService.prebookingAvailabilitySendMessage(loginDetails.getFofoId());
21615 kshitij.so 263
		return "dashboard";
264
	}
23923 amit.gupta 265
 
24312 amit.gupta 266
	private String getInvestmentChartData(List<PartnerDailyInvestment> partnerInvestments) {
24321 amit.gupta 267
 
268
 
269
		JSONObject scale = new JSONObject().put("scaleLabel", new JSONObject().put("scaleLabel", "Amount in Rs"));
270
		JSONObject titleObject = new JSONObject()
271
				.put("display", true)
272
				.put("text", "Investment Overview").put("display", true);
273
 
274
		JSONObject barData = new JSONObject();
275
 
24325 amit.gupta 276
		JSONObject walletStack = new JSONObject().put("backgroundColor","rgba(255, 99, 132, 0.2)")
24323 amit.gupta 277
				.put("label", "Wallet").put("stack", "Stack1").put("data", new JSONArray());
24321 amit.gupta 278
		JSONObject inStockStack = new JSONObject().put("label", "In Stock").put("backgroundColor","rgba(54, 162, 235, 0.2)")
24325 amit.gupta 279
				.put("stack", "Stack1").put("data", new JSONArray());
280
		JSONObject salesStack = new JSONObject().put("backgroundColor","rgba(255, 206, 86, 0.2)")
24323 amit.gupta 281
				.put("label", "Sales").put("stack", "Stack1").put("data", new JSONArray());
24325 amit.gupta 282
		JSONObject grnPendingStack = new JSONObject().put("backgroundColor","rgba(75, 192, 192, 0.2)")
24323 amit.gupta 283
				.put("label", "Grn Pending").put("stack", "Stack1").put("data", new JSONArray());
24325 amit.gupta 284
		JSONObject  billingPendingStack = new JSONObject().put("backgroundColor","rgba(153, 102, 255, 0.2)")
24323 amit.gupta 285
				.put("label", "Billing Pending").put("stack", "Stack1").put("data", new JSONArray());
24325 amit.gupta 286
		JSONObject inTransitStack = new JSONObject().put("backgroundColor","rgba(255, 159, 64, 0.2)")
24324 amit.gupta 287
				.put("label", "Returned").put("stack", "Stack1").put("data", new JSONArray());
24321 amit.gupta 288
 
24327 amit.gupta 289
		JSONObject minInvestmentDataSet = new JSONObject().put("label", "Min. Ivestment").put("data", new JSONArray()).put("type", "line");
24321 amit.gupta 290
		JSONArray dateLabels = new JSONArray();
291
 
24312 amit.gupta 292
		for (PartnerDailyInvestment pdi : partnerInvestments) {
24321 amit.gupta 293
			dateLabels = dateLabels.put(pdi.getDate().toString());
294
			walletStack.getJSONArray("data").put((int)pdi.getWalletAmount());
295
			inStockStack.getJSONArray("data").put((int)pdi.getInStockAmount());
296
			salesStack.getJSONArray("data").put((int)pdi.getSalesAmount());
297
			grnPendingStack.getJSONArray("data").put((int)pdi.getUnbilledAmount());
298
			billingPendingStack.getJSONArray("data").put((int)pdi.getGrnPendingAmount());
299
			inTransitStack.getJSONArray("data").put((int)pdi.getReturnInTransitAmount());
24327 amit.gupta 300
			minInvestmentDataSet.getJSONArray("data").put((int)pdi.getMinInvestment());
24312 amit.gupta 301
		}
302
 
24321 amit.gupta 303
		JSONArray barDataSetsArray = new JSONArray().put(walletStack).put(inStockStack)
304
				.put(salesStack).put(grnPendingStack).put(billingPendingStack).put(inTransitStack);
305
		barData.put("labels", dateLabels).put("datasets", barDataSetsArray);
306
		scale.put("yAxes", new JSONArray().put(new JSONObject().put("stacked", true)))
307
			.put("xAxes", new JSONArray().put(new JSONObject().put("stacked", true)));
308
 
309
		JSONObject barOptions = new JSONObject()
24324 amit.gupta 310
				.put("title", titleObject).put("responsive", true).put("scales", scale)
24321 amit.gupta 311
				.put("tooltips", new JSONObject().put("mode", "index").put("intersect", false));
312
 
313
		JSONObject chartJSOn = new JSONObject().put("type", "bar").put("data", barData).put("options", barOptions);
314
		return chartJSOn.toString();
24312 amit.gupta 315
	}
316
 
24288 amit.gupta 317
	// This method is currently hardcoded to faciliate watches sold as gift.
24203 amit.gupta 318
	private boolean hasGift(int fofoId) {
319
		try {
24288 amit.gupta 320
			return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId)
321
					.getAvailability() > 0;
24203 amit.gupta 322
		} catch (ProfitMandiBusinessException e) {
323
			return false;
324
		}
325
	}
24288 amit.gupta 326
 
24098 tejbeer 327
	@RequestMapping(value = "/getPaginatedNotificationData", method = RequestMethod.GET)
328
	public String getPaginatedNotificationData(HttpServletRequest request,
329
			@RequestParam(name = "offset", defaultValue = "0") int offset,
330
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {
24263 tejbeer 331
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
24098 tejbeer 332
 
24263 tejbeer 333
		if (roleManager.isAdmin(loginDetails.getRoleIds())) {
334
			List<NotificationData> notificationData = null;
24098 tejbeer 335
 
24263 tejbeer 336
			notificationData = notificationPanelRepository.selectAllNotificationData(offset, limit);
24098 tejbeer 337
 
24263 tejbeer 338
			LOGGER.info("notification_data {}", notificationData);
24098 tejbeer 339
 
24263 tejbeer 340
			if (!notificationData.isEmpty()) {
24098 tejbeer 341
 
24263 tejbeer 342
				model.addAttribute("notificationData", notificationData);
343
				model.addAttribute("url", "/getPaginatedNotificationData");
344
 
345
			} else {
346
				model.addAttribute("notificationData", notificationData);
347
 
348
			}
349
		} else if (roleManager.isPartner(loginDetails.getRoleIds())) {
350
 
351
			List<NotificationData> notificationData = null;
352
 
353
			notificationData = notificationPanelRepository.selectAllNotificationDataByActiveFlag(offset, limit, true);
354
			LOGGER.info("notification_data {}", notificationData);
355
 
356
			if (!notificationData.isEmpty()) {
357
 
358
				model.addAttribute("notificationData", notificationData);
359
				model.addAttribute("url", "/getPaginatedNotificationData");
360
 
361
			} else {
362
				model.addAttribute("notificationData", notificationData);
363
 
364
			}
24098 tejbeer 365
		}
366
 
367
		return "dashboard-paginated";
368
	}
24288 amit.gupta 369
 
22354 ashik.ali 370
	@RequestMapping(value = "/contactUs", method = RequestMethod.GET)
23923 amit.gupta 371
	public String contactUs(HttpServletRequest request, Model model) throws Throwable {
22354 ashik.ali 372
		model.addAttribute("appContextPath", request.getContextPath());
373
		return "contact-us";
374
	}
23923 amit.gupta 375
 
376
	/*
377
	 * private List<PaymentOption> getPaymentOptions(int fofoId){ List<Integer>
378
	 * paymentOptionIds =
24288 amit.gupta 379
	 * fofoPartnerPaymentOptionRepository.selectPaymentOptionIdsByFofoId(fofoId) ;
380
	 * if(paymentOptionIds.isEmpty()){ return new ArrayList<>(); } return
23923 amit.gupta 381
	 * paymentOptionRepository.selectByIds(new HashSet<>(paymentOptionIds)); }
382
	 */
383
 
24263 tejbeer 384
	@RequestMapping(value = "/inactiveNotificationData", method = RequestMethod.POST)
385
	public String inactiveNotificationData(HttpServletRequest request,
386
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
387
 
388
		NotificationData notificationData = notificationPanelRepository.selectById(id);
389
		if (notificationData != null) {
390
 
391
			notificationData.setActive(false);
392
			LOGGER.info("notification" + notificationData);
393
			notificationPanelRepository.persist(notificationData);
394
 
395
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
396
		} else {
397
			model.addAttribute("response", mvcResponseSender.createResponseString(false));
398
 
399
		}
400
		return "response";
401
	}
402
 
403
	@RequestMapping(value = "/activeNotificationData", method = RequestMethod.POST)
404
	public String activeNotificationData(HttpServletRequest request,
405
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
406
 
407
		NotificationData notificationData = notificationPanelRepository.selectById(id);
408
		if (notificationData != null) {
409
 
410
			notificationData.setActive(true);
411
			notificationData.setCreated_timestamp(LocalDateTime.now());
412
			LOGGER.info("notification" + notificationData);
413
			notificationPanelRepository.persist(notificationData);
414
 
415
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
416
		} else {
417
			model.addAttribute("response", mvcResponseSender.createResponseString(false));
418
 
419
		}
420
		return "response";
421
	}
422
 
21615 kshitij.so 423
}