Subversion Repositories SmartDukaan

Rev

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