Subversion Repositories SmartDukaan

Rev

Rev 24441 | Rev 24996 | 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;
24880 govind 8
import java.time.LocalTime;
24288 amit.gupta 9
import java.time.temporal.TemporalAdjusters;
24339 amit.gupta 10
import java.util.ArrayList;
24880 govind 11
import java.util.HashMap;
23884 amit.gupta 12
import java.util.List;
24880 govind 13
import java.util.Map;
14
import java.util.Set;
15
import java.util.function.IntFunction;
16
import java.util.function.IntPredicate;
17
import java.util.stream.Collectors;
23568 govind 18
 
22086 amit.gupta 19
import javax.servlet.http.HttpServletRequest;
20
 
23786 amit.gupta 21
import org.apache.logging.log4j.LogManager;
23568 govind 22
import org.apache.logging.log4j.Logger;
24321 amit.gupta 23
import org.json.JSONArray;
24
import org.json.JSONObject;
22481 ashik.ali 25
import org.springframework.beans.factory.annotation.Autowired;
23379 ashik.ali 26
import org.springframework.beans.factory.annotation.Value;
21615 kshitij.so 27
import org.springframework.stereotype.Controller;
22481 ashik.ali 28
import org.springframework.transaction.annotation.Transactional;
22073 ashik.ali 29
import org.springframework.ui.Model;
21615 kshitij.so 30
import org.springframework.web.bind.annotation.RequestMapping;
31
import org.springframework.web.bind.annotation.RequestMethod;
24098 tejbeer 32
import org.springframework.web.bind.annotation.RequestParam;
21615 kshitij.so 33
 
24880 govind 34
import com.spice.profitmandi.common.enumuration.RechargeStatus;
22481 ashik.ali 35
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24203 amit.gupta 36
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24880 govind 37
import com.spice.profitmandi.dao.entity.catalog.Item;
38
import com.spice.profitmandi.dao.entity.dtr.InsurancePolicy;
24098 tejbeer 39
import com.spice.profitmandi.dao.entity.dtr.NotificationData;
24880 govind 40
import com.spice.profitmandi.dao.entity.dtr.RechargeTransaction;
41
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
42
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
22654 ashik.ali 43
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
24288 amit.gupta 44
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
24880 govind 45
import com.spice.profitmandi.dao.enumuration.dtr.RechargeType;
46
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
22481 ashik.ali 47
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24880 govind 48
import com.spice.profitmandi.dao.repository.dtr.InsurancePolicyRepository;
49
import com.spice.profitmandi.dao.repository.dtr.InsuranceProviderRepository;
24098 tejbeer 50
import com.spice.profitmandi.dao.repository.dtr.NotificationPanelRepository;
24880 govind 51
import com.spice.profitmandi.dao.repository.dtr.RechargeTransactionRepository;
24203 amit.gupta 52
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
24880 govind 53
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
54
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
24288 amit.gupta 55
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
23884 amit.gupta 56
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
24336 amit.gupta 57
import com.spice.profitmandi.service.PartnerInvestmentService;
23844 amit.gupta 58
import com.spice.profitmandi.service.authentication.RoleManager;
23884 amit.gupta 59
import com.spice.profitmandi.service.inventory.InventoryService;
24880 govind 60
import com.spice.profitmandi.service.slab.TargetSlabService;
23884 amit.gupta 61
import com.spice.profitmandi.service.transaction.TransactionService;
62
import com.spice.profitmandi.service.wallet.WalletService;
22481 ashik.ali 63
import com.spice.profitmandi.web.model.LoginDetails;
64
import com.spice.profitmandi.web.util.CookiesProcessor;
24263 tejbeer 65
import com.spice.profitmandi.web.util.MVCResponseSender;
22481 ashik.ali 66
 
21615 kshitij.so 67
@Controller
22481 ashik.ali 68
@Transactional(rollbackFor = Throwable.class)
21615 kshitij.so 69
public class DashboardController {
23923 amit.gupta 70
 
23379 ashik.ali 71
	@Value("${web.api.host}")
72
	private String webApiHost;
23923 amit.gupta 73
 
24072 amit.gupta 74
	@Value("${web.api.scheme}")
75
	private String webApiScheme;
24288 amit.gupta 76
 
24078 amit.gupta 77
	@Value("${web.api.root}")
78
	private String webApiRoot;
79
 
23379 ashik.ali 80
	@Value("${web.api.port}")
81
	private int webApiPort;
21615 kshitij.so 82
 
22481 ashik.ali 83
	@Autowired
22927 ashik.ali 84
	private CookiesProcessor cookiesProcessor;
23923 amit.gupta 85
 
23568 govind 86
	@Autowired
24880 govind 87
	private ItemRepository itemRepository;
88
 
89
	@Autowired
23786 amit.gupta 90
	private RoleManager roleManager;
23923 amit.gupta 91
 
23838 ashik.ali 92
	@Autowired
93
	private FofoStoreRepository fofoStoreRepository;
23884 amit.gupta 94
 
95
	@Autowired
24880 govind 96
	private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
23923 amit.gupta 97
 
23884 amit.gupta 98
	@Autowired
24880 govind 99
	private PartnerInvestmentService partnerInvestmentService;
24288 amit.gupta 100
 
23884 amit.gupta 101
	@Autowired
24880 govind 102
	private InsurancePolicyRepository insurancePolicyRepository;
23923 amit.gupta 103
	/*
104
	 * @Autowired private ScanRepository scanRepository;
105
	 */
24880 govind 106
	@Autowired
107
	private RechargeTransactionRepository rechargeTransactionRepository;
23923 amit.gupta 108
 
23884 amit.gupta 109
	@Autowired
110
	private TransactionService transactionService;
24263 tejbeer 111
 
24098 tejbeer 112
	@Autowired
24263 tejbeer 113
	private MVCResponseSender mvcResponseSender;
114
 
115
	@Autowired
24098 tejbeer 116
	private NotificationPanelRepository notificationPanelRepository;
23923 amit.gupta 117
 
24203 amit.gupta 118
	@Autowired
119
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
120
 
24880 govind 121
	@Autowired
122
	private FofoOrderItemRepository fofoOrderItemRepository;
123
 
124
	@Autowired
125
	private InsuranceProviderRepository insuranceProviderRepository;
126
 
23568 govind 127
	private static final Logger LOGGER = LogManager.getLogger(DashboardController.class);
23923 amit.gupta 128
 
21615 kshitij.so 129
	@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
24312 amit.gupta 130
	public String dashboard(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset,
24098 tejbeer 131
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
23923 amit.gupta 132
			throws ProfitMandiBusinessException, URISyntaxException, IOException {
133
		// LOGGER.info("scanRepository.selectScansByInventoryItemId(1)",
134
		// scanRepository.selectScansByInventoryItemId(1));
23884 amit.gupta 135
		LOGGER.info("In Dashboard");
22927 ashik.ali 136
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
23923 amit.gupta 137
 
22481 ashik.ali 138
		FofoStore fofoStore = null;
139
		try {
22927 ashik.ali 140
			fofoStore = fofoStoreRepository.selectByRetailerId(loginDetails.getFofoId());
22481 ashik.ali 141
		} catch (ProfitMandiBusinessException e) {
22927 ashik.ali 142
			LOGGER.error("FofoStore Code not found of fofoId {}", loginDetails.getFofoId());
22481 ashik.ali 143
		}
23884 amit.gupta 144
 
24312 amit.gupta 145
		float sale = 0;
23923 amit.gupta 146
		if (roleManager.isAdmin(loginDetails.getRoleIds())) {
24263 tejbeer 147
			model.addAttribute("sale", sale);
148
			List<NotificationData> notificationData = null;
149
			long size = 0;
150
 
151
			notificationData = notificationPanelRepository.selectAllNotificationData(offset, limit);
152
			size = notificationPanelRepository.selectAllCount();
153
			LOGGER.info("notification_data {}", notificationData);
154
 
155
			LOGGER.info("notification_data {}", size);
24880 govind 156
			// Map<String, Object> saleSummary=this.getAllSaleDetails(loginDetails);
157
			// model.addAllAttributes(saleSummary);
24263 tejbeer 158
			if (!notificationData.isEmpty()) {
159
 
160
				model.addAttribute("notificationData", notificationData);
161
				LOGGER.info("notificationdata", notificationData);
162
				model.addAttribute("start", offset + 1);
163
				model.addAttribute("size", size);
164
				model.addAttribute("url", "/getPaginatedNotificationData");
165
 
166
				LOGGER.info("start {}", offset + 1);
167
				LOGGER.info("SIZE {}", size);
168
 
169
				if (notificationData.size() < limit) {
170
					model.addAttribute("end", offset + notificationData.size());
171
					LOGGER.info("SIZE2 {}", offset + notificationData.size());
172
				} else {
173
					model.addAttribute("end", offset + limit);
174
 
175
					LOGGER.info("end {}", size);
176
 
177
				}
178
 
179
			} else {
180
 
181
				model.addAttribute("notificationData", notificationData);
182
				model.addAttribute("size", size);
183
				LOGGER.info("sizeOriginal {}", size);
184
 
185
			}
23923 amit.gupta 186
		} else if (roleManager.isPartner(loginDetails.getRoleIds())) {
24312 amit.gupta 187
 
24263 tejbeer 188
			List<NotificationData> notificationData = null;
189
			long size = 0;
190
 
191
			notificationData = notificationPanelRepository.selectAllNotificationDataByActiveFlag(offset, limit, true);
192
			size = notificationPanelRepository.selectCountByActiveFlag(true);
193
			LOGGER.info("notification_data {}", notificationData);
194
 
195
			LOGGER.info("notification_data {}", size);
196
 
24880 govind 197
			///// DashBoard ......................///////
198
			model.addAllAttributes(this.getAllInsuranceDetails(loginDetails.getFofoId()));
199
			model.addAllAttributes(this.getAllRechargeDetails(loginDetails.getFofoId()));
200
			model.addAllAttributes(this.getAllSaleDetails(loginDetails.getFofoId()));
201
			model.addAttribute("fofoId", loginDetails.getFofoId());
202
			///////////////////////////////
24263 tejbeer 203
			if (!notificationData.isEmpty()) {
204
 
205
				model.addAttribute("notificationData", notificationData);
206
				LOGGER.info("notificationdata", notificationData);
207
				model.addAttribute("start", offset + 1);
208
				model.addAttribute("size", size);
209
				model.addAttribute("url", "/getPaginatedNotificationData");
210
 
211
				LOGGER.info("start {}", offset + 1);
212
				LOGGER.info("SIZE {}", size);
213
 
214
				if (notificationData.size() < limit) {
215
					model.addAttribute("end", offset + notificationData.size());
216
					LOGGER.info("SIZE2 {}", offset + notificationData.size());
217
				} else {
218
					model.addAttribute("end", offset + limit);
219
 
220
					LOGGER.info("end {}", size);
221
 
222
				}
223
 
224
			} else {
225
 
226
				model.addAttribute("notificationData", notificationData);
227
				model.addAttribute("size", size);
228
				LOGGER.info("sizeOriginal {}", size);
229
 
230
			}
23884 amit.gupta 231
		}
24880 govind 232
 
24366 amit.gupta 233
		boolean isInvestmentOk = partnerInvestmentService.isInvestmentOk(loginDetails.getFofoId(), 10, 30);
24288 amit.gupta 234
		LocalDate endDate = LocalDate.now().minusDays(1);
235
		LocalDate weekStartDate = endDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
24336 amit.gupta 236
		LocalDate startDate = weekStartDate.minusWeeks(2);
24339 amit.gupta 237
		List<PartnerDailyInvestment> partnerInvestments = new ArrayList<>();
24880 govind 238
		try {
239
			partnerInvestments.add(partnerInvestmentService.getInvestment(loginDetails.getFofoId(), 1));
240
			partnerInvestments
241
					.addAll(partnerDailyInvestmentRepository.selectAll(loginDetails.getFofoId(), startDate, endDate));
242
 
243
		} catch (Exception e) {
24383 amit.gupta 244
			LOGGER.error("partner Investment can't exists");
245
		}
24366 amit.gupta 246
		model.addAttribute("isInvestmentOk", isInvestmentOk || roleManager.isAdmin(loginDetails.getRoleIds()));
24387 amit.gupta 247
		model.addAttribute("partnerInvestment", partnerInvestments.get(0));
24312 amit.gupta 248
		model.addAttribute("investmentChart", this.getInvestmentChartData(partnerInvestments));
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) {
24880 govind 267
 
24321 amit.gupta 268
		JSONObject scale = new JSONObject().put("scaleLabel", new JSONObject().put("scaleLabel", "Amount in Rs"));
24880 govind 269
		JSONObject titleObject = new JSONObject().put("display", true).put("text", "Investment Overview").put("display",
270
				true);
271
 
24321 amit.gupta 272
		JSONObject barData = new JSONObject();
24880 govind 273
 
274
		JSONObject walletStack = new JSONObject().put("backgroundColor", "rgba(255, 99, 132, 0.2)")
24323 amit.gupta 275
				.put("label", "Wallet").put("stack", "Stack1").put("data", new JSONArray());
24880 govind 276
		JSONObject inStockStack = new JSONObject().put("label", "In Stock")
277
				.put("backgroundColor", "rgba(54, 162, 235, 0.2)").put("stack", "Stack1").put("data", new JSONArray());
278
		JSONObject salesStack = new JSONObject().put("backgroundColor", "rgba(255, 206, 86, 0.2)").put("label", "Sales")
24325 amit.gupta 279
				.put("stack", "Stack1").put("data", new JSONArray());
24880 govind 280
		JSONObject grnPendingStack = new JSONObject().put("backgroundColor", "rgba(75, 192, 192, 0.2)")
24323 amit.gupta 281
				.put("label", "Grn Pending").put("stack", "Stack1").put("data", new JSONArray());
24880 govind 282
		JSONObject billingPendingStack = new JSONObject().put("backgroundColor", "rgba(153, 102, 255, 0.2)")
24323 amit.gupta 283
				.put("label", "Billing Pending").put("stack", "Stack1").put("data", new JSONArray());
24880 govind 284
		JSONObject inTransitStack = new JSONObject().put("backgroundColor", "rgba(255, 159, 64, 0.2)")
24324 amit.gupta 285
				.put("label", "Returned").put("stack", "Stack1").put("data", new JSONArray());
24880 govind 286
 
287
		JSONObject minInvestmentDataSet = new JSONObject().put("label", "Min. Ivestment").put("data", new JSONArray())
288
				.put("type", "line");
24321 amit.gupta 289
		JSONArray dateLabels = new JSONArray();
24880 govind 290
 
24312 amit.gupta 291
		for (PartnerDailyInvestment pdi : partnerInvestments) {
24321 amit.gupta 292
			dateLabels = dateLabels.put(pdi.getDate().toString());
24880 govind 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());
299
			minInvestmentDataSet.getJSONArray("data").put((int) pdi.getMinInvestment());
24312 amit.gupta 300
		}
24880 govind 301
 
302
		JSONArray barDataSetsArray = new JSONArray().put(walletStack).put(inStockStack).put(salesStack)
303
				.put(grnPendingStack).put(billingPendingStack).put(inTransitStack);
24321 amit.gupta 304
		barData.put("labels", dateLabels).put("datasets", barDataSetsArray);
24880 govind 305
		scale.put("yAxes", new JSONArray().put(new JSONObject().put("stacked", true))).put("xAxes",
306
				new JSONArray().put(new JSONObject().put("stacked", true)));
24321 amit.gupta 307
 
24880 govind 308
		JSONObject barOptions = new JSONObject().put("title", titleObject).put("responsive", true).put("scales", scale)
24321 amit.gupta 309
				.put("tooltips", new JSONObject().put("mode", "index").put("intersect", false));
24880 govind 310
 
24321 amit.gupta 311
		JSONObject chartJSOn = new JSONObject().put("type", "bar").put("data", barData).put("options", barOptions);
312
		return chartJSOn.toString();
24312 amit.gupta 313
	}
314
 
24288 amit.gupta 315
	// This method is currently hardcoded to faciliate watches sold as gift.
24203 amit.gupta 316
	private boolean hasGift(int fofoId) {
317
		try {
24288 amit.gupta 318
			return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId)
319
					.getAvailability() > 0;
24203 amit.gupta 320
		} catch (ProfitMandiBusinessException e) {
321
			return false;
322
		}
323
	}
24288 amit.gupta 324
 
24098 tejbeer 325
	@RequestMapping(value = "/getPaginatedNotificationData", method = RequestMethod.GET)
326
	public String getPaginatedNotificationData(HttpServletRequest request,
327
			@RequestParam(name = "offset", defaultValue = "0") int offset,
328
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {
24263 tejbeer 329
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
24098 tejbeer 330
 
24263 tejbeer 331
		if (roleManager.isAdmin(loginDetails.getRoleIds())) {
332
			List<NotificationData> notificationData = null;
24098 tejbeer 333
 
24263 tejbeer 334
			notificationData = notificationPanelRepository.selectAllNotificationData(offset, limit);
24098 tejbeer 335
 
24263 tejbeer 336
			LOGGER.info("notification_data {}", notificationData);
24098 tejbeer 337
 
24263 tejbeer 338
			if (!notificationData.isEmpty()) {
24098 tejbeer 339
 
24263 tejbeer 340
				model.addAttribute("notificationData", notificationData);
341
				model.addAttribute("url", "/getPaginatedNotificationData");
342
 
343
			} else {
344
				model.addAttribute("notificationData", notificationData);
345
 
346
			}
347
		} else if (roleManager.isPartner(loginDetails.getRoleIds())) {
348
 
349
			List<NotificationData> notificationData = null;
350
 
351
			notificationData = notificationPanelRepository.selectAllNotificationDataByActiveFlag(offset, limit, true);
352
			LOGGER.info("notification_data {}", notificationData);
353
 
354
			if (!notificationData.isEmpty()) {
355
 
356
				model.addAttribute("notificationData", notificationData);
357
				model.addAttribute("url", "/getPaginatedNotificationData");
358
 
359
			} else {
360
				model.addAttribute("notificationData", notificationData);
361
 
362
			}
24098 tejbeer 363
		}
364
 
365
		return "dashboard-paginated";
366
	}
24288 amit.gupta 367
 
22354 ashik.ali 368
	@RequestMapping(value = "/contactUs", method = RequestMethod.GET)
23923 amit.gupta 369
	public String contactUs(HttpServletRequest request, Model model) throws Throwable {
22354 ashik.ali 370
		model.addAttribute("appContextPath", request.getContextPath());
371
		return "contact-us";
372
	}
23923 amit.gupta 373
 
374
	/*
375
	 * private List<PaymentOption> getPaymentOptions(int fofoId){ List<Integer>
376
	 * paymentOptionIds =
24288 amit.gupta 377
	 * fofoPartnerPaymentOptionRepository.selectPaymentOptionIdsByFofoId(fofoId) ;
378
	 * if(paymentOptionIds.isEmpty()){ return new ArrayList<>(); } return
23923 amit.gupta 379
	 * paymentOptionRepository.selectByIds(new HashSet<>(paymentOptionIds)); }
380
	 */
381
 
24263 tejbeer 382
	@RequestMapping(value = "/inactiveNotificationData", method = RequestMethod.POST)
383
	public String inactiveNotificationData(HttpServletRequest request,
384
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
385
 
386
		NotificationData notificationData = notificationPanelRepository.selectById(id);
387
		if (notificationData != null) {
388
 
389
			notificationData.setActive(false);
390
			LOGGER.info("notification" + notificationData);
391
			notificationPanelRepository.persist(notificationData);
392
 
393
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
394
		} else {
395
			model.addAttribute("response", mvcResponseSender.createResponseString(false));
396
 
397
		}
398
		return "response";
399
	}
400
 
401
	@RequestMapping(value = "/activeNotificationData", method = RequestMethod.POST)
402
	public String activeNotificationData(HttpServletRequest request,
403
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
404
 
405
		NotificationData notificationData = notificationPanelRepository.selectById(id);
406
		if (notificationData != null) {
407
 
408
			notificationData.setActive(true);
409
			notificationData.setCreated_timestamp(LocalDateTime.now());
410
			LOGGER.info("notification" + notificationData);
411
			notificationPanelRepository.persist(notificationData);
412
 
413
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
414
		} else {
415
			model.addAttribute("response", mvcResponseSender.createResponseString(false));
416
 
417
		}
418
		return "response";
419
	}
420
 
24880 govind 421
	private Map<String, Object> getAllInsuranceDetails(int fofoId) {
422
 
423
		LocalDateTime currentDayStart = LocalDateTime.of(LocalDate.now(), LocalTime.of(00, 00));
424
		LocalDateTime previousDayStart = currentDayStart.minusDays(1);
425
		LocalDateTime currentDayEnd = LocalDateTime.now();
426
		LocalDateTime currentMonthDayStart = LocalDateTime
427
				.of(LocalDate.of(currentDayStart.getYear(), currentDayStart.getMonth(), 01), LocalTime.of(00, 00));
428
		LocalDateTime previousDayEnd = currentDayStart.minusMinutes(1);
429
		LocalDateTime previousMonthFirstDay = currentMonthDayStart.minusMonths(1);
430
		LocalDateTime previousMonthLastDay = currentMonthDayStart.minusMinutes(1);
431
 
432
		List<String> insuranceProviders = insuranceProviderRepository.selectAll().stream().map(x -> x.getName())
433
				.collect(Collectors.toList());
434
 
435
		Map<String, Object> map = new HashMap<>();
436
		List<InsurancePolicy> insurancePolicies = insurancePolicyRepository
437
				.selectByRetailerIdAndBetweenCreatedTimeStamp(fofoId, previousMonthFirstDay, LocalDateTime.now());
438
 
439
		Map<Integer, Double> currentMonthTotalInsurance = insurancePolicies.stream()
440
				.filter(x -> x.getCreateTimestamp().isAfter(currentMonthDayStart)
441
						&& x.getCreateTimestamp().isBefore(currentDayEnd))
442
				.collect(Collectors.groupingBy(InsurancePolicy::getRetailerId,
443
						Collectors.summingDouble(InsurancePolicy::getSellingPrice)));
444
 
445
		Map<Integer, Double> previousMonthTotalInsurance = insurancePolicies.stream()
446
				.filter(x -> x.getCreateTimestamp().isAfter(previousMonthFirstDay)
447
						&& x.getCreateTimestamp().isBefore(previousMonthLastDay))
448
				.collect(Collectors.groupingBy(InsurancePolicy::getRetailerId,
449
						Collectors.summingDouble(InsurancePolicy::getSellingPrice)));
450
 
451
		Map<Integer, Double> currentDayTotalInsurance = insurancePolicies.stream().filter(
452
				x -> x.getCreateTimestamp().isAfter(currentDayStart) && x.getCreateTimestamp().isBefore(currentDayEnd))
453
				.collect(Collectors.groupingBy(InsurancePolicy::getRetailerId,
454
						Collectors.summingDouble(InsurancePolicy::getSellingPrice)));
455
 
456
		Map<Integer, Double> previousDayTotalInsurance = insurancePolicies.stream()
457
				.filter(x -> x.getCreateTimestamp().isAfter(previousDayStart)
458
						&& x.getCreateTimestamp().isBefore(previousDayEnd))
459
				.collect(Collectors.groupingBy(InsurancePolicy::getRetailerId,
460
						Collectors.summingDouble(InsurancePolicy::getSellingPrice)));
461
 
462
		IntFunction<String> intFunction = (providerId) -> {
463
			try {
464
				return insuranceProviderRepository.selectById(providerId).getName();
465
			} catch (ProfitMandiBusinessException e1) {
466
				// TODO Auto-generated catch block
467
				return "";
468
			}
469
		};
470
		Map<String, Double> currentMonthTotalInsuranceByPolicyName = insurancePolicies.stream()
471
				.filter(x -> x.getCreateTimestamp().isAfter(currentMonthDayStart)
472
						&& x.getCreateTimestamp().isBefore(currentDayEnd))
473
				.collect(Collectors.groupingBy(x -> {
474
					return intFunction.apply(x.getProviderId());
475
				}, Collectors.summingDouble(InsurancePolicy::getSellingPrice)));
476
 
477
		Map<String, Double> previousMonthTotalInsuranceByPolicyName = insurancePolicies.stream()
478
				.filter(x -> x.getCreateTimestamp().isAfter(previousMonthFirstDay)
479
						&& x.getCreateTimestamp().isBefore(previousMonthLastDay))
480
				.collect(Collectors.groupingBy(x -> {
481
					return intFunction.apply(x.getProviderId());
482
				}, Collectors.summingDouble(InsurancePolicy::getSellingPrice)));
483
 
484
		Map<String, Double> previousDayTotalInsuranceByPolicyName = insurancePolicies.stream()
485
				.filter(x -> x.getCreateTimestamp().isAfter(previousDayStart)
486
						&& x.getCreateTimestamp().isBefore(previousDayEnd))
487
				.collect(Collectors.groupingBy(x -> {
488
					return intFunction.apply(x.getProviderId());
489
				}, Collectors.summingDouble(InsurancePolicy::getSellingPrice)));
490
 
491
		Map<String, Double> currentDayTotalInsuranceByPolicyName = insurancePolicies.stream().filter(
492
				x -> x.getCreateTimestamp().isAfter(currentDayStart) && x.getCreateTimestamp().isBefore(currentDayEnd))
493
				.collect(Collectors.groupingBy(x -> {
494
					return intFunction.apply(x.getProviderId());
495
				}, Collectors.summingDouble(InsurancePolicy::getSellingPrice)));
496
 
497
		map.put("currentMonthTotalInsurance", currentMonthTotalInsurance);
498
		map.put("previousMonthTotalInsurance", previousMonthTotalInsurance);
499
		map.put("currentDayTotalInsurance", currentDayTotalInsurance);
500
		map.put("previousDayTotalInsurance", previousDayTotalInsurance);
501
		map.put("currentMonthTotalInsuranceByPolicyName", currentMonthTotalInsuranceByPolicyName);
502
		map.put("previousMonthTotalInsuranceByPolicyName", previousMonthTotalInsuranceByPolicyName);
503
		map.put("previousDayTotalInsuranceByPolicyName", previousDayTotalInsuranceByPolicyName);
504
		map.put("currentDayTotalInsuranceByPolicyName", currentDayTotalInsuranceByPolicyName);
505
		map.put("insuranceProviders", insuranceProviders);
506
		return map;
507
	}
508
 
509
	private Map<String, Object> getAllRechargeDetails(int fofoId) {
510
 
511
		LocalDateTime currentDayStart = LocalDateTime.of(LocalDate.now(), LocalTime.of(00, 00));
512
		LocalDateTime previousDayStart = currentDayStart.minusDays(1);
513
		LocalDateTime currentDayEnd = LocalDateTime.now();
514
		LocalDateTime currentMonthDayStart = LocalDateTime
515
				.of(LocalDate.of(currentDayStart.getYear(), currentDayStart.getMonth(), 01), LocalTime.of(00, 00));
516
		LocalDateTime previousDayEnd = currentDayStart.minusMinutes(1);
517
		LocalDateTime previousMonthFirstDay = currentMonthDayStart.minusMonths(1);
518
		LocalDateTime previousMonthLastDay = currentMonthDayStart.minusMinutes(1);
519
 
520
		List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository
521
				.getAllRechargeTransactionByCreatedTimeStamp(fofoId, previousMonthFirstDay, currentDayEnd).stream()
522
				.filter(x -> x.getStatus().equals(RechargeStatus.SUCCESS)).collect(Collectors.toList());
523
 
524
		LOGGER.info(rechargeTransactions);
525
		Map<Integer, Double> currentMonthTotalRecharge = rechargeTransactions.stream()
526
				.filter(x -> x.getCreateTimestamp().isAfter(currentMonthDayStart)
527
						&& x.getCreateTimestamp().isBefore(currentDayEnd))
528
				.collect(Collectors.groupingBy(RechargeTransaction::getRetailerId,
529
						Collectors.summingDouble(RechargeTransaction::getAmount)));
530
		LOGGER.info(currentMonthTotalRecharge);
531
		Map<Integer, Double> previousMonthTotalRecharge = rechargeTransactions.stream()
532
				.filter(x -> x.getCreateTimestamp().isAfter(previousMonthFirstDay)
533
						&& x.getCreateTimestamp().isBefore(previousMonthLastDay))
534
				.collect(Collectors.groupingBy(RechargeTransaction::getRetailerId,
535
						Collectors.summingDouble(RechargeTransaction::getAmount)));
536
 
537
		LOGGER.info(previousMonthTotalRecharge);
538
		Map<Integer, Double> currentDayTotalRecharge = rechargeTransactions.stream().filter(
539
				x -> x.getCreateTimestamp().isAfter(currentDayStart) && x.getCreateTimestamp().isBefore(currentDayEnd))
540
				.collect(Collectors.groupingBy(RechargeTransaction::getRetailerId,
541
						Collectors.summingDouble(RechargeTransaction::getAmount)));
542
 
543
		Map<Integer, Double> previousDayTotalRecharge = rechargeTransactions.stream()
544
				.filter(x -> x.getCreateTimestamp().isAfter(previousDayStart)
545
						&& x.getCreateTimestamp().isBefore(previousDayEnd))
546
				.collect(Collectors.groupingBy(RechargeTransaction::getRetailerId,
547
						Collectors.summingDouble(RechargeTransaction::getAmount)));
548
 
549
		Map<Object, Double> currentMonthTotalRechargeByRechargeType = rechargeTransactions.stream()
550
				.filter(x -> x.getCreateTimestamp().isAfter(currentMonthDayStart)
551
						&& x.getCreateTimestamp().isBefore(currentDayEnd))
552
				.collect(Collectors.groupingBy(x -> {
553
					return x.getType();
554
				}, Collectors.summingDouble(RechargeTransaction::getAmount)));
555
 
556
		Map<Object, Double> previosMonthTotalRechargeByRechargeType = rechargeTransactions.stream()
557
				.filter(x -> x.getCreateTimestamp().isAfter(previousMonthFirstDay)
558
						&& x.getCreateTimestamp().isBefore(previousMonthLastDay))
559
				.collect(Collectors.groupingBy(x -> {
560
					return x.getType();
561
				}, Collectors.summingDouble(RechargeTransaction::getAmount)));
562
 
563
		Map<Object, Double> currentDayTotalRechargeByRechargeType = rechargeTransactions.stream().filter(
564
				x -> x.getCreateTimestamp().isAfter(currentDayStart) && x.getCreateTimestamp().isBefore(currentDayEnd))
565
				.collect(Collectors.groupingBy(x -> {
566
					return x.getType();
567
				}, Collectors.summingDouble(RechargeTransaction::getAmount)));
568
 
569
		Map<Object, Double> previousDayTotalRechargeByRechargeType = rechargeTransactions.stream()
570
				.filter(x -> x.getCreateTimestamp().isAfter(previousDayStart)
571
						&& x.getCreateTimestamp().isBefore(previousDayEnd))
572
				.collect(Collectors.groupingBy(x -> {
573
					return x.getType();
574
				}, Collectors.summingDouble(RechargeTransaction::getAmount)));
575
 
576
		LOGGER.info("previosMonthTotalRechargeByRechargeType" + previosMonthTotalRechargeByRechargeType);
577
		Map<String, Object> map = new HashMap<>();
578
		map.put("currentMonthTotalRecharge", currentMonthTotalRecharge);
579
		map.put("previousMonthTotalRecharge", previousMonthTotalRecharge);
580
		map.put("currentDayTotalRecharge", currentDayTotalRecharge);
581
		map.put("previousDayTotalRecharge", previousDayTotalRecharge);
582
		map.put("currentMonthTotalRechargeByRechargeType", currentMonthTotalRechargeByRechargeType);
583
		map.put("previosMonthTotalRechargeByRechargeType", previosMonthTotalRechargeByRechargeType);
584
		map.put("currentDayTotalRechargeByRechargeType", currentDayTotalRechargeByRechargeType);
585
		map.put("previousDayTotalRechargeByRechargeType", previousDayTotalRechargeByRechargeType);
586
		map.put("rechargeTypes", RechargeType.values());
587
 
588
		return map;
589
 
590
	}
591
 
592
	private Map<String, Object> getAllSaleDetails(int fofoId) {
593
 
594
		LocalDateTime currentDayStart = LocalDateTime.of(LocalDate.now(), LocalTime.of(00, 00));
595
		LocalDateTime previousDayStart = currentDayStart.minusDays(1);
596
		LocalDateTime currentDayEnd = LocalDateTime.now();
597
		LocalDateTime currentMonthDayStart = LocalDateTime
598
				.of(LocalDate.of(currentDayStart.getYear(), currentDayStart.getMonth(), 01), LocalTime.of(00, 00));
599
		LocalDateTime previousDayEnd = currentDayStart.minusMinutes(1);
600
		LocalDateTime previousMonthFirstDay = currentMonthDayStart.minusMonths(1);
601
		LocalDateTime previousMonthLastDay = currentMonthDayStart.minusMinutes(1);
602
 
603
		Map<String, Object> map = new HashMap<>();
604
 
605
		List<FofoOrderItem> fofoOrderItems = fofoOrderItemRepository.selectBetweenCreatedTime(fofoId,
606
				previousMonthFirstDay, currentDayEnd);
607
 
608
		Double currentMonthTotalSale = fofoOrderItems.stream()
609
				.filter(x -> x.getCreateTimestamp().isAfter(currentMonthDayStart)
610
						&& x.getCreateTimestamp().isBefore(currentDayEnd))
611
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
612
 
613
		LOGGER.info("currentMonthTotalSale" + currentMonthTotalSale);
614
		Double previousMonthTotalSale = fofoOrderItems.stream()
615
				.filter(x -> x.getCreateTimestamp().isAfter(previousMonthFirstDay)
616
						&& x.getCreateTimestamp().isBefore(previousMonthLastDay))
617
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
618
 
619
		Double currentDayTotalSale = fofoOrderItems.stream().filter(
620
				x -> x.getCreateTimestamp().isAfter(currentDayStart) && x.getCreateTimestamp().isBefore(currentDayEnd))
621
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
622
 
623
		LOGGER.info(currentDayTotalSale);
624
 
625
		Double previousDayTotalSale = fofoOrderItems.stream()
626
				.filter(x -> x.getCreateTimestamp().isAfter(previousDayStart)
627
						&& x.getCreateTimestamp().isBefore(previousDayEnd))
628
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
629
		LOGGER.info(previousDayTotalSale);
630
		IntPredicate checkHandset = (itemId) -> {
631
			try {
632
				Item item = itemRepository.selectById(itemId);
633
				return item.getCategoryId() == 10006;
634
			} catch (ProfitMandiBusinessException e) {
635
				// TODO Auto-generated catch block
636
				e.printStackTrace();
637
				return false;
638
			}
639
		};
640
 
641
		Set<String> brands = fofoOrderItemRepository
642
				.selectBetweenDates(previousMonthFirstDay.minusMonths(1), currentDayEnd).stream()
643
				.filter(x -> checkHandset.test(x.getItemId())).map(x -> x.getBrand()).collect(Collectors.toSet());
644
		Set<String> accessories = fofoOrderItemRepository
645
				.selectBetweenDates(previousMonthFirstDay.minusMonths(1), currentDayEnd).stream()
646
				.filter(x -> !checkHandset.test(x.getItemId())).map(x -> x.getBrand()).collect(Collectors.toSet());
647
 
648
		Double currentMonthTotalMobileSale = fofoOrderItems.stream()
649
				.filter(x -> x.getCreateTimestamp().isAfter(currentMonthDayStart)
650
						&& x.getCreateTimestamp().isBefore(currentDayEnd) && checkHandset.test(x.getItemId()))
651
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
652
 
653
		Double previousMonthTotalMobileSale = fofoOrderItems.stream()
654
				.filter(x -> x.getCreateTimestamp().isAfter(previousMonthFirstDay)
655
						&& x.getCreateTimestamp().isBefore(previousMonthLastDay) && checkHandset.test(x.getItemId()))
656
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
657
 
658
		Double previousDayTotalMobileSale = fofoOrderItems.stream()
659
				.filter(x -> x.getCreateTimestamp().isAfter(previousDayStart)
660
						&& x.getCreateTimestamp().isBefore(previousDayEnd) && checkHandset.test(x.getItemId()))
661
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
662
 
663
		Double currentDayTotalMobileSale = fofoOrderItems.stream()
664
				.filter(x -> x.getCreateTimestamp().isAfter(currentDayStart)
665
						&& x.getCreateTimestamp().isBefore(currentDayEnd) && checkHandset.test(x.getItemId()))
666
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
667
 
668
		Double currentMonthTotalAccessoriesSale = fofoOrderItems.stream()
669
				.filter(x -> x.getCreateTimestamp().isAfter(currentMonthDayStart)
670
						&& x.getCreateTimestamp().isBefore(currentDayEnd) && !checkHandset.test(x.getItemId()))
671
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
672
 
673
		Double previousMonthTotalAccessoriesSale = fofoOrderItems.stream()
674
				.filter(x -> x.getCreateTimestamp().isAfter(previousMonthFirstDay)
675
						&& x.getCreateTimestamp().isBefore(previousMonthLastDay) && !checkHandset.test(x.getItemId()))
676
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
677
 
678
		Double previousDayTotalAccessoriesSale = fofoOrderItems.stream()
679
				.filter(x -> x.getCreateTimestamp().isAfter(previousDayStart)
680
						&& x.getCreateTimestamp().isBefore(previousDayEnd) && !checkHandset.test(x.getItemId()))
681
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
682
 
683
		Double currentDayTotalAccessoriesSale = fofoOrderItems.stream()
684
				.filter(x -> x.getCreateTimestamp().isAfter(currentDayStart)
685
						&& x.getCreateTimestamp().isBefore(currentDayEnd) && !checkHandset.test(x.getItemId()))
686
				.collect(Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity()));
687
 
688
		Map<String, Double> currentMonthMobileSalegroupByBrand = fofoOrderItems.stream()
689
				.filter(x -> x.getCreateTimestamp().isAfter(currentMonthDayStart)
690
						&& x.getCreateTimestamp().isBefore(currentDayEnd) && checkHandset.test(x.getItemId()))
691
				.collect(Collectors.groupingBy(x -> x.getBrand(),
692
						Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity())));
693
 
694
		Map<String, Double> currentMonthAccesoriesSalegroupByBrand = fofoOrderItems.stream()
695
				.filter(x -> x.getCreateTimestamp().isAfter(currentMonthDayStart)
696
						&& x.getCreateTimestamp().isBefore(currentDayEnd) && !checkHandset.test(x.getItemId()))
697
				.collect(Collectors.groupingBy(x -> x.getBrand(),
698
						Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity())));
699
 
700
		Map<String, Double> previousMonthAccesoriesSalegroupByBrand = fofoOrderItems.stream()
701
				.filter(x -> x.getCreateTimestamp().isAfter(previousMonthFirstDay)
702
						&& x.getCreateTimestamp().isBefore(previousMonthLastDay) && !checkHandset.test(x.getItemId()))
703
				.collect(Collectors.groupingBy(x -> x.getBrand(),
704
						Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity())));
705
 
706
		Map<String, Double> currentDayAccesoriesSalegroupByBrand = fofoOrderItems.stream()
707
				.filter(x -> x.getCreateTimestamp().isAfter(currentDayStart)
708
						&& x.getCreateTimestamp().isBefore(currentDayEnd) && !checkHandset.test(x.getItemId()))
709
				.collect(Collectors.groupingBy(x -> x.getBrand(),
710
						Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity())));
711
 
712
		Map<String, Double> previousDayAccesoriesSalegroupByBrand = fofoOrderItems.stream()
713
				.filter(x -> x.getCreateTimestamp().isAfter(previousDayStart)
714
						&& x.getCreateTimestamp().isBefore(previousDayEnd) && !checkHandset.test(x.getItemId()))
715
				.collect(Collectors.groupingBy(x -> x.getBrand(),
716
						Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity())));
717
 
718
		Map<String, Double> currentDayMobileSalegroupByBrand = fofoOrderItems.stream()
719
				.filter(x -> x.getCreateTimestamp().isAfter(currentDayStart)
720
						&& x.getCreateTimestamp().isBefore(currentDayEnd) && checkHandset.test(x.getItemId()))
721
				.collect(Collectors.groupingBy(x -> x.getBrand(),
722
						Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity())));
723
 
724
		Map<String, Double> previousDayMobileSalegroupByBrand = fofoOrderItems.stream()
725
				.filter(x -> x.getCreateTimestamp().isAfter(previousDayStart)
726
						&& x.getCreateTimestamp().isBefore(previousDayEnd) && checkHandset.test(x.getItemId()))
727
				.collect(Collectors.groupingBy(x -> x.getBrand(),
728
						Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity())));
729
		Map<String, Double> previousMonthMobileSalegroupByBrand = fofoOrderItems.stream()
730
				.filter(x -> x.getCreateTimestamp().isAfter(previousMonthFirstDay)
731
						&& x.getCreateTimestamp().isBefore(previousMonthLastDay) && checkHandset.test(x.getItemId()))
732
				.collect(Collectors.groupingBy(x -> x.getBrand(),
733
						Collectors.summingDouble(x -> x.getSellingPrice() * x.getQuantity())));
734
 
735
		map.put("brands", brands);
736
		map.put("accessories", accessories);
737
		map.put("currentMonthTotalSale", currentMonthTotalSale);
738
		map.put("previousMonthTotalSale", previousMonthTotalSale);
739
		map.put("currentDayTotalSale", currentDayTotalSale);
740
		map.put("previousDayTotalSale", previousDayTotalSale);
741
		map.put("currentMonthMobileSalegroupByBrand", currentMonthMobileSalegroupByBrand);
742
		map.put("currentMonthAccesoriesSalegroupByBrand", currentMonthAccesoriesSalegroupByBrand);
743
		map.put("previousMonthAccesoriesSalegroupByBrand", previousMonthAccesoriesSalegroupByBrand);
744
		map.put("previousDayAccesoriesSalegroupByBrand", previousDayAccesoriesSalegroupByBrand);
745
		map.put("currentDayMobileSalegroupByBrand", currentDayMobileSalegroupByBrand);
746
		map.put("previousDayMobileSalegroupByBrand", previousDayMobileSalegroupByBrand);
747
		map.put("currentDayAccesoriesSalegroupByBrand", currentDayAccesoriesSalegroupByBrand);
748
		map.put("previousMonthMobileSalegroupByBrand", previousMonthMobileSalegroupByBrand);
749
 
750
		map.put("currentMonthTotalMobileSale", currentMonthTotalMobileSale);
751
		map.put("previousMonthTotalMobileSale", previousMonthTotalMobileSale);
752
		map.put("previousDayTotalMobileSale", previousDayTotalMobileSale);
753
		map.put("currentDayTotalMobileSale", currentDayTotalMobileSale);
754
		map.put("currentMonthTotalAccessoriesSale", currentMonthTotalAccessoriesSale);
755
		map.put("previousMonthTotalAccessoriesSale", previousMonthTotalAccessoriesSale);
756
		map.put("previousDayTotalAccessoriesSale", previousDayTotalAccessoriesSale);
757
		map.put("currentDayTotalAccessoriesSale", currentDayTotalAccessoriesSale);
758
 
759
		return map;
760
	}
21615 kshitij.so 761
}