Subversion Repositories SmartDukaan

Rev

Rev 30479 | Rev 31884 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
28468 tejbeer 1
package com.spice.profitmandi.service;
2
 
3
import com.google.gson.Gson;
4
import com.mongodb.DBObject;
5
import com.spice.profitmandi.common.enumuration.MessageType;
6
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
30250 amit.gupta 7
import com.spice.profitmandi.common.model.*;
28468 tejbeer 8
import com.spice.profitmandi.dao.Interface.Campaign;
9
import com.spice.profitmandi.dao.entity.dtr.Document;
10
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
11
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
12
import com.spice.profitmandi.dao.entity.fofo.PartnerTargetDetails;
13
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
14
import com.spice.profitmandi.dao.model.BrandWiseModel;
15
import com.spice.profitmandi.dao.model.SimpleCampaign;
16
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
17
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
18
import com.spice.profitmandi.dao.repository.dtr.Mongo;
30250 amit.gupta 19
import com.spice.profitmandi.dao.repository.fofo.*;
28468 tejbeer 20
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
21
import com.spice.profitmandi.service.inventory.InventoryService;
30250 amit.gupta 22
import org.apache.logging.log4j.LogManager;
23
import org.apache.logging.log4j.Logger;
24
import org.springframework.beans.factory.annotation.Autowired;
25
import org.springframework.stereotype.Component;
28468 tejbeer 26
 
30250 amit.gupta 27
import java.time.*;
28
import java.time.format.DateTimeFormatter;
29
import java.util.*;
30
import java.util.Map.Entry;
31
import java.util.stream.Collectors;
32
 
28468 tejbeer 33
@Component
34
public class FofoUser {
35
 
36
	@Autowired
37
	private Gson gson;
38
 
39
	@Autowired
40
	private PartnerInvestmentService partnerInvestmentService;
41
 
42
	@Autowired
43
	private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
44
 
45
	@Autowired
46
	ChartService chartService;
47
 
48
	@Autowired
49
	FofoOrderItemRepository fofoOrderItemRepository;
50
 
51
	@Autowired
52
	FofoOrderRepository fofoOrderRepository;
53
 
54
	@Autowired
55
	PartnerTypeChangeService partnerTypeChangeService;
56
 
57
	@Autowired
58
	PartnerTargetRepository partnerTargetRepository;
59
 
60
	@Autowired
61
	InventoryService inventoryService;
62
 
63
	@Autowired
64
	private Mongo mongoClient;
65
 
66
	@Autowired
67
	private OrderRepository orderRepository;
68
 
69
	@Autowired
70
	DocumentRepository documentRepository;
71
 
72
	@Autowired
73
	CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
74
 
75
	private static final Logger LOGGER = LogManager.getLogger(FofoUser.class);
76
 
31246 amit.gupta 77
	List<String> colorList = Arrays.asList("papayawhip", "mediumseagreen", "dodgerblue", "darkblue", "gold", "#eb0029", "coral",
28468 tejbeer 78
			"steelblue", "red", "deeppink", "midnightblue", "cornsilk");
79
 
31246 amit.gupta 80
	List<String> borderList = Arrays.asList("pink", "lawngreen", "lightblue", "#0000cd", "#f7e98e", "#eb0029", "lightcoral",
28468 tejbeer 81
			"#0000cd", "lightsalmon", "pink", "#0000cd", "cornsilk");
82
 
31246 amit.gupta 83
	List<String> brands = Arrays.asList("Accessories", "Oppo", "Vivo", "Samsung", "Realme", "Xiaomi", "OnePlus", "Tecno", "Itel",
28468 tejbeer 84
			"Lava", "Nokia");
85
 
86
	public String format(long value) {
87
		String finalval = null;
88
 
89
		if (value >= 100000 && value < 10000000) {
90
			long reminder = value / 100000;
91
			long quitonent = value % 100000;
29660 amit.gupta 92
			String secondval = String.format("%05d", quitonent);
93
			secondval = secondval.substring(0, 2);
94
			finalval = reminder + "." + secondval;
28468 tejbeer 95
			return String.valueOf(finalval) + " Lacs";
96
		} else if (value >= 1000 && value < 100000) {
97
			long reminder = value / 1000;
98
			long quitonent = value % 1000;
29660 amit.gupta 99
			String secondval = String.format("%03d", quitonent);
100
			secondval = secondval.substring(0, 2);
101
			finalval = reminder + "." + secondval;
28468 tejbeer 102
			return String.valueOf(finalval) + " K";
103
		} else if (value >= 10000000 && value < 1000000000) {
104
			long reminder = value / 10000000;
105
			long quitonent = value % 10000000;
106
			finalval = reminder + "." + quitonent;
29660 amit.gupta 107
			String secondval = String.format("%07d", quitonent);
108
			secondval = secondval.substring(0, 2);
109
			finalval = reminder + "." + secondval;
28468 tejbeer 110
			return String.valueOf(finalval) + " Cr";
111
		}
112
		return String.valueOf(finalval);
113
 
114
	}
115
 
116
	public List<BrandStockPrice> getBrandStockPrices(int fofoId) throws Exception {
117
		Map<String, BrandStockPrice> brandStockPricesMap = inventoryService.getBrandWiseStockValue(fofoId);
118
 
119
		List<DBObject> mobileBrands = mongoClient.getAllBrandsToDisplay(3);
120
		List<BrandStockPrice> brandStockPrices = new ArrayList<>();
121
 
122
		mobileBrands.stream().forEach(x -> {
123
			String brand = (String) x.get("name");
124
			if (brandStockPricesMap.containsKey(brand)) {
125
				BrandStockPrice brandStockPrice = brandStockPricesMap.get(brand);
126
				brandStockPrice.setBrandUrl((String) x.get("url"));
127
				brandStockPrice.setRank(((Double) x.get("rank")).intValue());
128
				brandStockPrices.add(brandStockPrice);
129
			}
130
		});
131
 
30250 amit.gupta 132
		return brandStockPrices.stream().filter(x -> x.getTotalQty() > 0).sorted(Comparator.comparingInt(BrandStockPrice::getRank))
28468 tejbeer 133
				.collect(Collectors.toList());
134
	}
135
 
136
	public Map<String, Object> getInvestments(int fofoId) throws Exception {
137
		Map<String, Object> investments = new LinkedHashMap<>();
138
		PartnerDailyInvestment investment = partnerInvestmentService.getInvestment(fofoId, 0);
139
		LocalDate currentMonthStart = LocalDate.now().withDayOfMonth(1);
140
		LocalDate yesterDate = LocalDate.now().minusDays(1);
141
		PartnerDailyInvestment yesterdayInvestment = partnerDailyInvestmentRepository.select(fofoId, yesterDate);
142
		if (yesterdayInvestment == null) {
143
			yesterdayInvestment = new PartnerDailyInvestment();
144
		}
145
 
146
		List<PartnerDailyInvestment> currentMonthInvestments = partnerDailyInvestmentRepository.selectAll(fofoId,
147
				currentMonthStart, currentMonthStart.withDayOfMonth(currentMonthStart.lengthOfMonth()));
148
 
149
		long okInvestmentDays = currentMonthInvestments.stream().filter(x -> x.getShortPercentage() <= 10)
150
				.collect(Collectors.counting());
151
		investments.put("today", investment.getTotalInvestment());
152
		investments.put("investment", investment);
153
		investments.put("inStock", investment.getInStockAmount());
154
		investments.put("minimum", investment.getMinInvestmentString());
155
		investments.put("short", investment.getShortPercentage());
156
		investments.put("activated_stock", investment.getActivatedStockAmount());
157
		investments.put("okDays", okInvestmentDays);
158
		return investments;
159
	}
160
 
161
	public Map<String, Object> getInvestmentsMonths(int fofoId, int month) throws Exception {
162
		Map<String, Object> investments = new LinkedHashMap<>();
163
		PartnerDailyInvestment investment = partnerInvestmentService.getInvestment(fofoId, 0);
164
		LocalDate currentMonthStart = LocalDate.now().withDayOfMonth(1).minusMonths(month);
165
 
166
		LocalDate yesterDate = LocalDate.now().minusDays(1);
167
		PartnerDailyInvestment yesterdayInvestment = partnerDailyInvestmentRepository.select(fofoId, yesterDate);
168
		if (yesterdayInvestment == null) {
169
			yesterdayInvestment = new PartnerDailyInvestment();
170
		}
171
 
172
		List<PartnerDailyInvestment> currentMonthInvestments = partnerDailyInvestmentRepository.selectAll(fofoId,
173
				currentMonthStart, currentMonthStart.withDayOfMonth(currentMonthStart.lengthOfMonth()));
174
 
175
		long okInvestmentDays = currentMonthInvestments.stream().filter(x -> x.getShortPercentage() <= 10)
176
				.collect(Collectors.counting());
177
		investments.put("today", investment.getTotalInvestment());
178
		investments.put("investment", investment);
179
		investments.put("inStock", investment.getInStockAmount());
180
		investments.put("minimum", investment.getMinInvestmentString());
181
		investments.put("short", investment.getShortPercentage());
182
		investments.put("activated_stock", investment.getActivatedStockAmount());
183
		investments.put("okDays", okInvestmentDays);
184
		return investments;
185
	}
186
 
187
	public ChartModel getLmsLineChart(int fofoId) throws ProfitMandiBusinessException {
188
 
189
		LocalDateTime curDate = LocalDate.now().atStartOfDay();
190
		Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
191
		Map<YearMonth, Map<String, Double>> monthValueMap = new HashMap<>();
192
 
193
		Map<String, Double> lmsBrandWiseSale = null;
194
 
28530 tejbeer 195
		for (int i = 0; i <= 6; i++) {
28468 tejbeer 196
 
197
			LocalDateTime startOfMonth = curDate.withDayOfMonth(1).minusMonths(i);
198
 
28530 tejbeer 199
			LOGGER.info("startOfMonth" + startOfMonth);
200
 
28468 tejbeer 201
			lmsBrandWiseSale = fofoOrderItemRepository.selectSumAmountGroupByBrand(
202
					curDate.withDayOfMonth(1).minusMonths(i), curDate.withDayOfMonth(1).minusMonths(i - 1), fofoId);
203
 
204
			Map<Integer, Double> accesorieslmsSale = fofoOrderRepository
205
					.selectSumSaleGroupByFofoIdsForMobileOrAccessories(fofoId, curDate.withDayOfMonth(1).minusMonths(i),
206
							curDate.withDayOfMonth(1).minusMonths(i - 1), Optional.of(false));
207
			LOGGER.info("lmsBrandWiseSale" + lmsBrandWiseSale);
208
 
209
			lmsBrandWiseSale.put("Accessories", accesorieslmsSale.get(fofoId));
210
 
211
			monthValueMap.put(YearMonth.from(startOfMonth), lmsBrandWiseSale);
212
			for (Entry<String, Double> lbw : lmsBrandWiseSale.entrySet()) {
213
				Map<YearMonth, Double> yearMonthValue = new HashMap<>();
214
				if (brandMonthValue.containsKey(lbw.getKey())) {
215
					yearMonthValue = brandMonthValue.get(lbw.getKey());
216
					yearMonthValue.put(YearMonth.from(startOfMonth), lbw.getValue());
217
				} else {
218
 
219
					yearMonthValue.put(YearMonth.from(startOfMonth), lbw.getValue());
220
 
221
				}
222
				brandMonthValue.put(lbw.getKey(), yearMonthValue);
223
 
224
			}
225
 
226
		}
227
 
28530 tejbeer 228
		LOGGER.info("brandMonthValue" + brandMonthValue);
229
 
28468 tejbeer 230
		Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
231
 
232
		for (String brand : brands) {
233
			Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
28530 tejbeer 234
			for (int i = 6; i >= 0; i--) {
28468 tejbeer 235
 
236
				LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
28530 tejbeer 237
				LOGGER.info("startMonth" + startMonth);
28468 tejbeer 238
 
239
				if (yearMonthValue != null) {
240
					if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
241
						yearMonthValue.put(YearMonth.from(startMonth), 0.0);
242
					}
243
 
244
				} else {
245
					yearMonthValue = new HashMap<>();
246
					yearMonthValue.put(YearMonth.from(startMonth), 0.0);
247
				}
248
			}
249
 
250
			Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
251
 
252
			brandMonthValue.put(brand, sortedMonthBrandValue);
253
 
254
			sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
255
 
256
		}
257
 
28530 tejbeer 258
		LOGGER.info("brandMonthValue" + brandMonthValue);
259
 
28468 tejbeer 260
		LOGGER.info("sortedBrandValue" + sortedBrandValue);
261
 
28530 tejbeer 262
		ChartModel cm = chartService.createChart(6, sortedBrandValue, colorList, borderList, "Brand Wise LMS");
28468 tejbeer 263
 
264
		return cm;
265
 
266
	}
267
 
268
	public ChartModel getPurchaseOrderChart(int fofoId) throws ProfitMandiBusinessException {
269
 
270
		LocalDateTime curDate = LocalDate.now().atStartOfDay();
28530 tejbeer 271
 
272
		LOGGER.info("startMonth" + curDate.withDayOfMonth(1).minusMonths(6));
273
 
28468 tejbeer 274
		LocalDateTime startOfMonth = curDate.withDayOfMonth(1).minusMonths(1);
275
		List<BrandWiseModel> soblms = orderRepository.selectAllBilledOrderGroupByBrandFofoId(fofoId,
28530 tejbeer 276
				curDate.withDayOfMonth(1).minusMonths(6));
28468 tejbeer 277
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
278
		Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
279
		for (BrandWiseModel bwl : soblms) {
280
			Map<YearMonth, Double> yearMonthValue = new HashMap<>();
281
			if (brandMonthValue.containsKey(bwl.getBrand())) {
282
				yearMonthValue = brandMonthValue.get(bwl.getBrand());
283
				yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
284
			} else {
285
 
286
				yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
287
 
288
			}
289
			brandMonthValue.put(bwl.getBrand(), yearMonthValue);
290
 
291
		}
292
		LOGGER.info("soblms" + brandMonthValue);
293
 
294
		Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
295
 
296
		for (String brand : brands) {
297
			Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
28530 tejbeer 298
			for (int i = 6; i >= 0; i--) {
28468 tejbeer 299
 
300
				LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
301
 
302
				if (yearMonthValue != null) {
303
					if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
304
						yearMonthValue.put(YearMonth.from(startMonth), 0.0);
305
					}
306
 
307
				} else {
308
					yearMonthValue = new HashMap<>();
309
					yearMonthValue.put(YearMonth.from(startMonth), 0.0);
310
				}
311
			}
312
 
313
			Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
314
 
315
			brandMonthValue.put(brand, sortedMonthBrandValue);
316
 
317
			sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
318
 
319
		}
28530 tejbeer 320
 
321
		LOGGER.info("brandMonthValue" + brandMonthValue);
322
 
323
		ChartModel cm = chartService.createChart(6, sortedBrandValue, colorList, borderList,
28474 tejbeer 324
				"Brand Wise Monthly Purchase");
28468 tejbeer 325
 
326
		return cm;
327
 
328
	}
329
 
330
	public Map<String, Object> getSales(int fofoId) {
331
 
332
		Map<String, Object> salesMap = new LinkedHashMap<>();
333
		LocalDateTime now = LocalDateTime.now();
334
		LocalDateTime startOfToday = LocalDate.now().atStartOfDay();
335
		int monthLength = LocalDate.now().lengthOfMonth();
336
		int daysGone = now.getDayOfMonth() - 1;
337
		int daysRemaining = monthLength - daysGone;
338
		Double todaySale = fofoOrderItemRepository.selectSumMopGroupByRetailer(startOfToday, now, fofoId, false)
339
				.get(fofoId);
340
		Double mtdSaleTillYesterDay = fofoOrderItemRepository
341
				.selectSumMopGroupByRetailer(startOfToday.withDayOfMonth(1), startOfToday, fofoId, false).get(fofoId);
342
		Double mtdSale = mtdSaleTillYesterDay + todaySale;
343
		Double lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
344
				startOfToday.withDayOfMonth(1).minusMonths(1), now.minusMonths(1), fofoId, false).get(fofoId);
345
 
346
		List<PartnerTargetDetails> partnerTargetDetails = partnerTargetRepository
347
				.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now());
348
		if (partnerTargetDetails.isEmpty()) {
349
			partnerTargetDetails = partnerTargetRepository
350
					.selectAllGeEqAndLeEqStartDateAndEndDate(LocalDateTime.now().minusMonths(3));
351
		}
352
 
353
		PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
354
 
355
		int currentRate = 0;
356
		if (mtdSaleTillYesterDay > 0) {
357
			currentRate = (int) (mtdSaleTillYesterDay / daysGone);
358
		}
359
 
360
		salesMap.put("requiredType", partnerType.next());
361
		float reqdAmount = partnerTypeChangeService.getMinimumAmount(partnerType.next());
362
		int requiredRate = (int) ((reqdAmount - mtdSaleTillYesterDay) / daysRemaining);
363
		if (partnerType.equals(PartnerType.PLATINUM) && requiredRate < currentRate) {
364
			requiredRate = currentRate;
365
		}
366
		salesMap.put("requiredRate", requiredRate);
367
		salesMap.put("requiredTypeImage", PartnerType.imageMap.get(partnerType.next()));
368
 
369
		salesMap.put("todaySale", todaySale == null ? 0 : todaySale);
370
		salesMap.put("mtdSale", mtdSale == null ? 0 : mtdSale);
371
		salesMap.put("lmtdSale", lmtdSale == null ? 0 : lmtdSale);
372
 
373
		PartnerType currentType = partnerTypeChangeService.getPartnerTypeByAmount(currentRate * monthLength);
374
		salesMap.put("currentRate", currentRate);
375
		salesMap.put("currentType", currentType);
376
		salesMap.put("currentTypeImage", PartnerType.imageMap.get(currentType));
377
		return salesMap;
378
	}
379
 
380
	public ChartModel getBrandChart(int fofoId) {
381
 
382
		LocalDateTime curDate = LocalDate.now().atStartOfDay();
383
 
384
		LOGGER.info("cur Date" + curDate.withDayOfMonth(1));
385
 
386
		LOGGER.info("curDateYear" + curDate.with(LocalTime.MAX));
387
 
388
		Map<String, Double> brandwisesale = fofoOrderItemRepository
389
				.selectSumAmountGroupByBrand(curDate.withDayOfMonth(1), curDate.with(LocalTime.MAX), fofoId);
390
 
391
		LOGGER.info("brandwisesale" + brandwisesale);
392
 
393
		Map<Integer, Double> accesoriesmtdsale = fofoOrderRepository.selectSumSaleGroupByFofoIdsForMobileOrAccessories(
394
				fofoId, curDate.withDayOfMonth(1), curDate.with(LocalTime.MAX), Optional.of(false));
395
		brandwisesale.put("Accessories", accesoriesmtdsale.get(fofoId));
396
 
397
		Map<String, Double> activatedImeisWithSellingPriceMTD = fofoOrderRepository
398
				.selectValueOfActivatedImeis(curDate.withDayOfMonth(1), curDate.with(LocalTime.MAX), fofoId).stream()
399
				.collect(Collectors.toMap(x -> x.getBrand(), x -> (double) x.getSellingPrice()));
400
 
401
		activatedImeisWithSellingPriceMTD.put("Lava", brandwisesale.get("Lava"));
402
 
403
		Map<String, Double> activatedImeisWithSellingPriceLMTD = fofoOrderRepository
404
				.selectValueOfActivatedImeis(curDate.withDayOfMonth(1).minusMonths(1),
405
						curDate.with(LocalTime.MAX).minusMonths(1), fofoId)
406
				.stream().collect(Collectors.toMap(x -> x.getBrand(), x -> (double) x.getSellingPrice()));
407
		Map<String, Double> lmtdBrandWiseSale = fofoOrderItemRepository.selectSumAmountGroupByBrand(
408
				curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1), fofoId);
409
		activatedImeisWithSellingPriceLMTD.put("Lava", lmtdBrandWiseSale.get("Lava"));
410
 
411
		Map<Integer, Double> accesorieslmtdsale = fofoOrderRepository.selectSumSaleGroupByFofoIdsForMobileOrAccessories(
412
				fofoId, curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1),
413
				Optional.of(false));
414
 
415
		lmtdBrandWiseSale.put("Accessories", accesorieslmtdsale.get(fofoId));
416
 
417
		ChartModel cm = new ChartModel();
418
 
419
		HashSet<String> labels = new HashSet<String>();
420
		labels.addAll(brandwisesale.keySet());
421
		labels.addAll(lmtdBrandWiseSale.keySet());
422
 
423
		List<String> labelList = new ArrayList<>(labels);
424
 
425
		List<Double> mtdActivatedImeisValues = new ArrayList<>();
426
 
427
		List<Double> mtdValues = new ArrayList<>();
428
		List<Double> lmtdUnActivatedImeisValues = new ArrayList<>();
429
		List<Double> lmtdActivatedImeisValues = new ArrayList<>();
430
		List<Double> mtdUnActivatedImeisValues = new ArrayList<>();
431
		List<Double> lmtdValues = new ArrayList<>();
432
		for (String label : labelList) {
433
			mtdActivatedImeisValues.add(activatedImeisWithSellingPriceMTD.get(label) == null ? 0
434
					: activatedImeisWithSellingPriceMTD.get(label));
435
			lmtdActivatedImeisValues.add(activatedImeisWithSellingPriceLMTD.get(label) == null ? 0
436
					: activatedImeisWithSellingPriceLMTD.get(label));
437
 
438
			mtdValues.add(brandwisesale.get(label) == null ? 0 : brandwisesale.get(label));
439
			if (brandwisesale.get(label) != null) {
440
				mtdUnActivatedImeisValues
441
						.add(brandwisesale.get(label) - (activatedImeisWithSellingPriceMTD.get(label) == null ? 0
442
								: activatedImeisWithSellingPriceMTD.get(label)));
443
			} else {
444
				mtdUnActivatedImeisValues.add(0.0);
445
			}
446
			if (lmtdBrandWiseSale.get(label) != null) {
447
				lmtdUnActivatedImeisValues
448
						.add(lmtdBrandWiseSale.get(label) - (activatedImeisWithSellingPriceLMTD.get(label) == null ? 0
449
								: activatedImeisWithSellingPriceLMTD.get(label)));
450
			} else {
451
				lmtdUnActivatedImeisValues.add(0.0);
452
			}
453
 
454
			lmtdValues.add(lmtdBrandWiseSale.get(label) == null ? 0 : lmtdBrandWiseSale.get(label));
455
		}
456
		DatasetModel dsmtotal = new DatasetModel();
457
		dsmtotal.setLabel("MTD");
458
		dsmtotal.setBackgroundColor("blue");
459
		dsmtotal.setBorderColor("blue");
460
		dsmtotal.setData(mtdValues);
461
		dsmtotal.setStack("stack0");
462
		dsmtotal.setOrder(1);
463
 
464
		DatasetModel dsmUnactivated = new DatasetModel();
465
		dsmUnactivated.setLabel("MTD Unactivated");
466
		dsmUnactivated.setBackgroundColor("red");
467
		dsmUnactivated.setBorderColor("red");
468
		dsmUnactivated.setData(mtdUnActivatedImeisValues);
469
		dsmUnactivated.setStack("stack0");
470
		dsmUnactivated.setOrder(1);
471
 
472
		LOGGER.info("mtdValuesMoney" + mtdValues);
473
 
474
		DatasetModel mtdActivatedImeis = new DatasetModel();
475
		mtdActivatedImeis.setLabel("MTD Activation");
476
		mtdActivatedImeis.setBorderColor("#00008b");
477
		mtdActivatedImeis.setData(mtdActivatedImeisValues);
478
		mtdActivatedImeis.setBackgroundColor("#00008b");
479
		mtdActivatedImeis.setStack("stack0");
480
		mtdActivatedImeis.setOrder(1);
481
 
482
		DatasetModel lmtddsmtotal = new DatasetModel();
483
		lmtddsmtotal.setLabel("LMTD");
484
		lmtddsmtotal.setBackgroundColor("blue");
485
		lmtddsmtotal.setData(lmtdValues);
486
		lmtddsmtotal.setBorderColor("blue");
487
		lmtddsmtotal.setStack("stack1");
488
		lmtddsmtotal.setOrder(1);
489
 
490
		DatasetModel lmtdUnActivated = new DatasetModel();
491
		lmtdUnActivated.setLabel("LMTD Unactivation");
492
		lmtdUnActivated.setBackgroundColor("red");
493
		lmtdUnActivated.setBorderColor("red");
494
		lmtdUnActivated.setData(lmtdUnActivatedImeisValues);
495
		lmtdUnActivated.setStack("stack1");
496
		lmtdUnActivated.setOrder(1);
497
 
498
		DatasetModel LmtdActivatedImeis = new DatasetModel();
499
		LmtdActivatedImeis.setLabel("LMTD Activation");
500
		LmtdActivatedImeis.setBackgroundColor("#87ceeb");
501
		LmtdActivatedImeis.setBorderColor("#87ceeb");
502
		LmtdActivatedImeis.setData(lmtdActivatedImeisValues);
503
		LmtdActivatedImeis.setStack("stack1");
504
		LmtdActivatedImeis.setOrder(1);
505
 
506
		DatasetModel linemtdChart = new DatasetModel();
507
		linemtdChart.setLabel("MTD");
508
		linemtdChart.setBackgroundColor("#006400");
509
		linemtdChart.setBorderColor("#006400");
510
		linemtdChart.setData(mtdValues);
511
		linemtdChart.setType("line");
512
		linemtdChart.setOrder(2);
513
		linemtdChart.setFill("false");
514
 
515
		DatasetModel lineLmtdChart = new DatasetModel();
516
		lineLmtdChart.setLabel("LMTD");
517
		lineLmtdChart.setBackgroundColor("hotpink");
518
		lineLmtdChart.setBorderColor("hotpink");
519
		lineLmtdChart.setData(lmtdValues);
520
		lineLmtdChart.setType("line");
521
		lineLmtdChart.setOrder(3);
522
		lineLmtdChart.setFill("false");
523
 
524
		List<DatasetModel> datasets = new ArrayList<>();
525
		datasets.add(mtdActivatedImeis);
526
		datasets.add(dsmUnactivated);
527
		datasets.add(LmtdActivatedImeis);
528
		datasets.add(lmtdUnActivated);
529
		datasets.add(linemtdChart);
530
		datasets.add(lineLmtdChart);
531
 
532
		DataModel dm = new DataModel();
533
		dm.setDatasets(datasets);
534
		dm.setLabels(labels);
535
 
536
		Tooltips tooltips = new Tooltips();
537
		tooltips.setBodyFontSize(10);
538
		tooltips.setTitleFontSize(10);
539
		tooltips.setMode("index");
540
		tooltips.setIntersect(false);
541
		tooltips.setBackgroundColor("rgba(0, 0, 0, .5)");
542
		HoverModel hover = new HoverModel();
543
		hover.setIntersect(false);
544
		hover.setMode("index");
545
 
546
		LegendModel lm = new LegendModel();
547
		lm.setPosition("top");
548
 
549
		TitleModel tm = new TitleModel();
550
		tm.setText("Brand Wise Sales");
551
		tm.setDisplay(true);
552
		tm.setFontSize(20);
553
		tm.setFontColor("#111");
554
 
555
		List<Axis> xAxes = new ArrayList<>();
556
		Axis xAxis = new Axis();
557
		xAxis.setStacked(true);
558
		xAxes.add(xAxis);
559
 
560
		List<Axis> yAxes = new ArrayList<>();
561
		Axis yAxis = new Axis();
562
		yAxis.setStacked(false);
563
		yAxes.add(yAxis);
564
 
565
		ScalesModel sm = new ScalesModel();
566
		sm.setxAxes(xAxes);
567
 
568
		OptionsModel om = new OptionsModel();
569
		om.setLegend(lm);
570
		om.setTitle(tm);
571
		om.setScales(sm);
572
		om.setHover(hover);
573
		om.setTooltips(tooltips);
574
 
575
		cm.setType("bar");
576
		cm.setData(dm);
577
		cm.setOptions(om);
578
 
579
		LOGGER.info("cm" + cm);
580
 
581
		return cm;
582
 
583
	}
584
 
585
	public ChartInvestmentModel getInvestmentChart(int fofoId) throws ProfitMandiBusinessException {
586
		PartnerDailyInvestment investment = partnerInvestmentService.getInvestment(fofoId, 0);
587
 
588
		Map<String, Float> investmentWalletAmount = new HashMap<>();
589
		investmentWalletAmount.put("Wallet", investment.getWalletAmount());
590
		investmentWalletAmount.put("InStocks", investment.getInStockAmount() - investment.getActivatedStockAmount());
591
		investmentWalletAmount.put("Unbilled Order", investment.getUnbilledAmount());
30479 amit.gupta 592
		investmentWalletAmount.put("GrnPending", investment.getGrnPendingAmount() - investment.getActivatedGrnPendingAmount());
28468 tejbeer 593
		investmentWalletAmount.put("ReturnInTransit", investment.getReturnInTransitAmount());
594
 
595
		if (investment.getShortInvestment() > 0) {
596
			investmentWalletAmount.put("Short Investment", investment.getShortInvestment());
597
		}
598
 
599
		ChartInvestmentModel cm = new ChartInvestmentModel();
600
 
601
		HashSet<String> labels = new HashSet<String>();
602
		labels.addAll(investmentWalletAmount.keySet());
603
 
604
		List<String> labelList = new ArrayList<>(labels);
605
		List<String> backgroundColor = new ArrayList<>();
606
		List<Float> values = new ArrayList<>();
607
		for (String label : labelList) {
608
			values.add(investmentWalletAmount.get(label));
609
			if (label.equals("Wallet")) {
610
				backgroundColor.add("pink");
611
			}
612
			if (label.equals("Short Investment")) {
613
				backgroundColor.add("red");
614
			}
615
			if (label.equals("InStocks")) {
616
				backgroundColor.add("#9ACD32");
617
			}
618
			if (label.equals("Unbilled Order")) {
619
				backgroundColor.add("blue");
620
			}
621
 
622
			if (label.equals("ReturnInTransit")) {
623
				backgroundColor.add("orange");
624
			}
625
			if (label.equals("GrnPending")) {
626
				backgroundColor.add("yellow");
627
			}
628
 
629
		}
630
 
631
		Data data = new Data();
632
		data.setData(values);
633
		data.setBackgroundColor(backgroundColor);
634
		data.setLabel("DataSet 1");
635
 
636
		PieLables label = new PieLables();
637
		label.setFontColor("White");
638
		label.setFontSize(15);
639
 
640
		Legend legend = new Legend();
641
		legend.setLabels(label);
642
		legend.setPosition("left");
643
 
644
		List<Data> dataList = new ArrayList<>();
645
		dataList.add(data);
646
 
647
		DataInvestmentModel datasets = new DataInvestmentModel();
648
		datasets.setDatasets(dataList);
649
		datasets.setLabels(labels);
650
 
651
		OptionModel om = new OptionModel();
652
		om.setLegend(legend);
653
		cm.setType("pie");
654
		cm.setData(datasets);
655
		cm.setOptions(om);
656
 
657
		return cm;
658
	}
659
 
660
	public List<Notification> getNotifications(List<NotificationCampaign> nc, MessageType messageType)
661
			throws ProfitMandiBusinessException {
662
		List<Notification> notifications = new ArrayList<>();
663
		Document document = null;
664
		if (messageType != null) {
665
			for (NotificationCampaign notificationCampaign : nc) {
666
				if (notificationCampaign.getMessageType() == messageType) {
667
					Notification ns = new Notification();
668
					SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
669
							SimpleCampaignParams.class);
670
					Campaign campaign = new SimpleCampaign(scp);
671
					LocalDateTime expire = campaign.getExpireTimestamp();
672
					ns.setCid(Integer.toString(notificationCampaign.getId()));
673
					ns.setType(campaign.getType());
674
					ns.setMessage(campaign.getMessage());
675
					ns.setTitle(campaign.getTitle());
676
					if (notificationCampaign.getDocumentId() != null) {
677
						document = documentRepository.selectById(notificationCampaign.getDocumentId());
678
						ns.setDocumentName(document.getDisplayName());
679
					}
680
					ns.setUrl(campaign.getUrl());
681
					ns.setShowImage(campaign.getShowImage());
682
					ns.setImageUrl(campaign.getImageUrl());
683
					ns.setDocumentId(notificationCampaign.getDocumentId());
684
					ns.setMessageType(notificationCampaign.getMessageType());
685
					ns.setCreated(
686
							notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30))
687
									* 1000);
688
					if (LocalDateTime.now().isAfter(expire)) {
689
						ns.setExpired(true);
690
					} else {
691
						ns.setExpired(false);
692
					}
693
					notifications.add(ns);
694
				}
695
			}
696
		} else {
697
			for (NotificationCampaign notificationCampaign : nc) {
698
 
699
				Notification ns = new Notification();
700
				SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(),
701
						SimpleCampaignParams.class);
702
				Campaign campaign = new SimpleCampaign(scp);
703
				LocalDateTime expire = campaign.getExpireTimestamp();
704
				ns.setCid(Integer.toString(notificationCampaign.getId()));
705
				ns.setType(campaign.getType());
706
				ns.setMessage(campaign.getMessage());
707
				ns.setTitle(campaign.getTitle());
708
				if (notificationCampaign.getDocumentId() != null) {
709
					document = documentRepository.selectById(notificationCampaign.getDocumentId());
710
					ns.setDocumentName(document.getDisplayName());
711
				}
712
				ns.setUrl(campaign.getUrl());
713
				ns.setShowImage(campaign.getShowImage());
714
				ns.setImageUrl(campaign.getImageUrl());
715
				ns.setDocumentId(notificationCampaign.getDocumentId());
716
				ns.setMessageType(notificationCampaign.getMessageType());
717
				ns.setCreated(notificationCampaign.getCreatedTimestamp().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30))
718
						* 1000);
719
				if (LocalDateTime.now().isAfter(expire)) {
720
					ns.setExpired(true);
721
				} else {
722
					ns.setExpired(false);
723
				}
724
				notifications.add(ns);
725
			}
726
 
727
		}
728
		return notifications;
729
 
730
	}
731
 
732
	// This method is currently hardcoded to faciliate watches sold as gift.
733
	public boolean hasGift(int fofoId) {
734
		try {
735
			return currentInventorySnapshotRepository.selectByItemIdAndFofoId(ProfitMandiConstants.GIFT_ID, fofoId)
736
					.getAvailability() > 0;
737
		} catch (ProfitMandiBusinessException e) {
738
			return false;
739
		}
740
	}
741
 
742
}