Subversion Repositories SmartDukaan

Rev

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