Subversion Repositories SmartDukaan

Rev

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