Subversion Repositories SmartDukaan

Rev

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