Subversion Repositories SmartDukaan

Rev

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