Subversion Repositories SmartDukaan

Rev

Rev 29864 | Rev 29935 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 29864 Rev 29927
Line 1... Line 1...
1
package com.spice.profitmandi.service.scheme;
1
package com.spice.profitmandi.service.scheme;
2
 
2
 
3
import java.text.MessageFormat;
-
 
4
import java.time.LocalDate;
-
 
5
import java.time.LocalDateTime;
-
 
6
import java.time.YearMonth;
-
 
7
import java.util.ArrayList;
-
 
8
import java.util.Arrays;
-
 
9
import java.util.Comparator;
-
 
10
import java.util.HashMap;
-
 
11
import java.util.HashSet;
-
 
12
import java.util.List;
-
 
13
import java.util.Map;
-
 
14
import java.util.Set;
-
 
15
import java.util.stream.Collectors;
-
 
16
 
-
 
17
import javax.persistence.criteria.CriteriaBuilder;
-
 
18
import javax.persistence.criteria.CriteriaQuery;
-
 
19
import javax.persistence.criteria.Predicate;
-
 
20
import javax.persistence.criteria.Root;
-
 
21
 
-
 
22
import org.apache.logging.log4j.LogManager;
-
 
23
import org.apache.logging.log4j.Logger;
-
 
24
import org.hibernate.Session;
-
 
25
import org.hibernate.SessionFactory;
-
 
26
import org.hibernate.query.Query;
-
 
27
import org.springframework.beans.factory.annotation.Autowired;
-
 
28
import org.springframework.beans.factory.annotation.Qualifier;
-
 
29
import org.springframework.cache.annotation.Cacheable;
-
 
30
import org.springframework.stereotype.Component;
-
 
31
 
-
 
32
import com.spice.profitmandi.common.enumuration.ItemType;
3
import com.spice.profitmandi.common.enumuration.ItemType;
-
 
4
import com.spice.profitmandi.common.enumuration.MessageType;
33
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
34
import com.spice.profitmandi.common.model.ProfitMandiConstants;
6
import com.spice.profitmandi.common.model.ProfitMandiConstants;
35
import com.spice.profitmandi.common.model.SchemeModel;
7
import com.spice.profitmandi.common.model.SchemeModel;
-
 
8
import com.spice.profitmandi.common.model.SendNotificationModel;
36
import com.spice.profitmandi.common.services.ReporticoService;
9
import com.spice.profitmandi.common.services.ReporticoService;
37
import com.spice.profitmandi.common.util.FormattingUtils;
10
import com.spice.profitmandi.common.util.FormattingUtils;
38
import com.spice.profitmandi.common.util.StringUtils;
11
import com.spice.profitmandi.common.util.StringUtils;
39
import com.spice.profitmandi.dao.entity.catalog.Item;
12
import com.spice.profitmandi.dao.entity.catalog.Item;
40
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
-
 
41
import com.spice.profitmandi.dao.entity.catalog.Scheme;
13
import com.spice.profitmandi.dao.entity.catalog.Scheme;
42
import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
-
 
43
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
-
 
44
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
-
 
45
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
-
 
46
import com.spice.profitmandi.dao.entity.fofo.Purchase;
14
import com.spice.profitmandi.dao.entity.fofo.*;
47
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
-
 
48
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
-
 
49
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
-
 
50
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
15
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
51
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
16
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
52
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
17
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
53
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
18
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
54
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
19
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
55
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
20
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
56
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
21
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
57
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
22
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
58
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
23
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
59
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
24
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
60
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
-
 
61
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
-
 
62
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
-
 
63
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
-
 
64
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
-
 
65
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
-
 
66
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
25
import com.spice.profitmandi.dao.repository.fofo.*;
67
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
-
 
68
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
-
 
69
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
-
 
70
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
26
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
-
 
27
import com.spice.profitmandi.service.NotificationService;
71
import com.spice.profitmandi.service.authentication.RoleManager;
28
import com.spice.profitmandi.service.authentication.RoleManager;
72
import com.spice.profitmandi.service.inventory.PurchaseService;
29
import com.spice.profitmandi.service.inventory.PurchaseService;
73
import com.spice.profitmandi.service.wallet.WalletService;
30
import com.spice.profitmandi.service.wallet.WalletService;
74
 
-
 
75
import in.shop2020.model.v1.order.WalletReferenceType;
31
import in.shop2020.model.v1.order.WalletReferenceType;
-
 
32
import org.apache.logging.log4j.LogManager;
-
 
33
import org.apache.logging.log4j.Logger;
-
 
34
import org.hibernate.Session;
-
 
35
import org.hibernate.SessionFactory;
-
 
36
import org.hibernate.query.Query;
-
 
37
import org.springframework.beans.factory.annotation.Autowired;
-
 
38
import org.springframework.beans.factory.annotation.Qualifier;
-
 
39
import org.springframework.cache.annotation.Cacheable;
-
 
40
import org.springframework.stereotype.Component;
-
 
41
 
-
 
42
import javax.persistence.criteria.CriteriaBuilder;
-
 
43
import javax.persistence.criteria.CriteriaQuery;
-
 
44
import javax.persistence.criteria.Predicate;
-
 
45
import javax.persistence.criteria.Root;
-
 
46
import java.text.MessageFormat;
-
 
47
import java.time.LocalDate;
-
 
48
import java.time.LocalDateTime;
-
 
49
import java.time.YearMonth;
-
 
50
import java.util.*;
-
 
51
import java.util.stream.Collectors;
76
 
52
 
77
@Component
53
@Component
78
public class SchemeServiceImpl implements SchemeService {
54
public class SchemeServiceImpl implements SchemeService {
79
 
55
 
80
	private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
56
	private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
Line 109... Line 85...
109
	@Autowired
85
	@Autowired
110
	private RoleManager roleManager;
86
	private RoleManager roleManager;
111
 
87
 
112
	@Autowired
88
	@Autowired
113
	private RetailerRepository retailerRepository;
89
	private RetailerRepository retailerRepository;
114
	
90
 
115
	@Autowired
91
	@Autowired
116
	private ReporticoService reporticoService;
92
	private ReporticoService reporticoService;
117
 
93
 
118
	
94
 
119
	@Autowired
95
	@Autowired
120
	private TagListingRepository tagListingRepository;
96
	private TagListingRepository tagListingRepository;
121
 
97
 
122
	@Autowired
98
	@Autowired
123
	private SchemeInOutRepository schemeInOutRepository;
99
	private SchemeInOutRepository schemeInOutRepository;
Line 207... Line 183...
207
		scheme.setEndDateTime(createSchemeRequest.getEndDate());
183
		scheme.setEndDateTime(createSchemeRequest.getEndDate());
208
		scheme.setCreatedBy(creatorId);
184
		scheme.setCreatedBy(creatorId);
209
		scheme.setCashback(createSchemeRequest.isCashback());
185
		scheme.setCashback(createSchemeRequest.isCashback());
210
		return scheme;
186
		return scheme;
211
	}
187
	}
212
	
-
 
213
	
188
 
214
 
189
 
215
	private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
190
	private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
216
		if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
191
		if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
217
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
192
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
218
					"SCHM_1003");
193
					"SCHM_1003");
Line 283... Line 258...
283
		Map<Integer, String> itemStringMap = this.toItemStringMap(items);
258
		Map<Integer, String> itemStringMap = this.toItemStringMap(items);
284
		this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
259
		this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
285
		return this.toSchemeModels(schemeIdSchemeMap);
260
		return this.toSchemeModels(schemeIdSchemeMap);
286
	}
261
	}
287
 
262
 
288
	private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
-
 
289
			Map<Integer, String> itemStringMap) {
263
	@Autowired
290
		for (SchemeItem schemeItem : schemeItems) {
-
 
291
			schemeIdSchemeMap.get(schemeItem.getSchemeId()).getItemStringMap().put(schemeItem.getItemId(),
-
 
292
					itemStringMap.get(schemeItem.getItemId()));
264
	NotificationService notificationService;
293
		}
-
 
294
	}
-
 
295
 
265
 
296
	private List<SchemeModel> toSchemeModels(Map<Integer, Scheme> schemeIdSchemeMap) {
266
	private List<SchemeModel> toSchemeModels(Map<Integer, Scheme> schemeIdSchemeMap) {
297
		List<SchemeModel> schemeModels = new ArrayList<>();
267
		List<SchemeModel> schemeModels = new ArrayList<>();
298
		for (Map.Entry<Integer, Scheme> schemeIdSchemeEntry : schemeIdSchemeMap.entrySet()) {
268
		for (Map.Entry<Integer, Scheme> schemeIdSchemeEntry : schemeIdSchemeMap.entrySet()) {
299
			schemeModels.add(this.toSchemeModel(schemeIdSchemeEntry.getValue()));
269
			schemeModels.add(this.toSchemeModel(schemeIdSchemeEntry.getValue()));
Line 318... Line 288...
318
		schemeModel.setItemStringMap(scheme.getItemStringMap());
288
		schemeModel.setItemStringMap(scheme.getItemStringMap());
319
		schemeModel.setRetailerIds(scheme.getRetailerIds());
289
		schemeModel.setRetailerIds(scheme.getRetailerIds());
320
		return schemeModel;
290
		return schemeModel;
321
	}
291
	}
322
 
292
 
-
 
293
	private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
-
 
294
									 Map<Integer, String> itemStringMap) {
-
 
295
		for (SchemeItem schemeItem : schemeItems) {
-
 
296
			schemeIdSchemeMap.get(schemeItem.getSchemeId()).getItemStringMap().put(schemeItem.getItemId(),
-
 
297
					itemStringMap.get(schemeItem.getItemId()));
-
 
298
		}
-
 
299
	}
-
 
300
 
323
	@Override
301
	@Override
324
	public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
302
	public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
325
		Scheme scheme = schemeRepository.selectById(schemeId);
303
		Scheme scheme = schemeRepository.selectById(schemeId);
326
		if (scheme.getActiveTimestamp() != null) {
304
		if (scheme.getActiveTimestamp() != null) {
327
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
305
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
Line 331... Line 309...
331
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
309
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
332
					"SCHM_1006");
310
					"SCHM_1006");
333
		}
311
		}
334
		scheme.setActiveTimestamp(LocalDateTime.now());
312
		scheme.setActiveTimestamp(LocalDateTime.now());
335
		schemeRepository.persist(scheme);
313
		schemeRepository.persist(scheme);
-
 
314
		this.sendSchemeNotification(scheme);
-
 
315
 
-
 
316
 
336
		/*
317
		/*
337
		 * if (scheme.getType() == SchemeType.IN) {
318
		 * if (scheme.getType() == SchemeType.IN) {
338
		 * this.processPreviousPurchases(scheme); } else if (scheme.getType() ==
319
		 * this.processPreviousPurchases(scheme); } else if (scheme.getType() ==
339
		 * SchemeType.OUT) { this.processPreviousSales(scheme); }
320
		 * SchemeType.OUT) { this.processPreviousSales(scheme); }
340
		 */
321
		 */
341
	}
322
	}
342
 
323
 
-
 
324
	private void sendSchemeNotification(Scheme scheme) throws ProfitMandiBusinessException {
-
 
325
		if (scheme.getType().equals(SchemeType.ACTIVATION)) {
-
 
326
 
-
 
327
			SendNotificationModel sendNotificationModel = new SendNotificationModel();
-
 
328
			List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(new HashSet<>(scheme.getId()));
-
 
329
			Set<Integer> itemIds = schemeItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
-
 
330
			List<String> itemDescriptions = itemRepository.selectByIds(itemIds).stream().map(x -> x.getItemDescriptionNoColor()).distinct().collect(Collectors.toList());
-
 
331
 
-
 
332
 
-
 
333
			String title = "Activation scheme of Rs." + FormattingUtils.formatDecimal(scheme.getAmount()) + " for " + String.join(", ", itemDescriptions);
-
 
334
			String message = "Duration from - " + FormattingUtils.formatDateMonth(scheme.getStartDateTime()) + " - " + FormattingUtils.formatDateMonth(scheme.getEndDateTime());
-
 
335
			sendNotificationModel.setCampaignName("activationscheme");
-
 
336
			sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/scheme/" + scheme.getId());
-
 
337
			sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
-
 
338
			sendNotificationModel.setMessage(message);
-
 
339
			sendNotificationModel.setTitle(title);
-
 
340
			sendNotificationModel.setMessageType(MessageType.scheme);
-
 
341
			notificationService.sendNotificationToAll(sendNotificationModel);
-
 
342
		}
-
 
343
	}
-
 
344
 
343
	@Override
345
	@Override
344
	public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
346
	public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
345
		Scheme scheme = schemeRepository.selectById(schemeId);
347
		Scheme scheme = schemeRepository.selectById(schemeId);
346
		if (scheme == null || scheme.getActiveTimestamp() == null) {
348
		if (scheme == null || scheme.getActiveTimestamp() == null) {
347
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
349
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
Line 390... Line 392...
390
		}
392
		}
391
		return itemIdSchemesMap;
393
		return itemIdSchemesMap;
392
	}
394
	}
393
 
395
 
394
	private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
396
	private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
395
			List<InventoryItem> inventoryItems) {
397
																	  List<InventoryItem> inventoryItems) {
396
		Set<Integer> schemeIds = new HashSet<>();
398
		Set<Integer> schemeIds = new HashSet<>();
397
		for (Scheme scheme : schemes) {
399
		for (Scheme scheme : schemes) {
398
			schemeIds.add(scheme.getId());
400
			schemeIds.add(scheme.getId());
399
		}
401
		}
400
		Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
402
		Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
Line 483... Line 485...
483
					purchase.getPurchaseReference(), itemsCount);
485
					purchase.getPurchaseReference(), itemsCount);
484
			purchase.setCashback(purchase.getCashback() + totalCashback);
486
			purchase.setCashback(purchase.getCashback() + totalCashback);
485
			purchaseRepository.persist(purchase);
487
			purchaseRepository.persist(purchase);
486
		}
488
		}
487
	}
489
	}
-
 
490
 
488
	//Only in and activation margins are allowed to be rolled out more than twice
491
	//Only in and activation margins are allowed to be rolled out more than twice
489
	private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem) {
492
	private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem) {
490
		LOGGER.info("Scheme === {}", scheme);
493
		LOGGER.info("Scheme === {}", scheme);
491
		if((scheme.getId() == 411 || scheme.getId()==612) && inventoryItem.getCreateTimestamp().isAfter(LocalDate.of(2021, 12, 1).atStartOfDay())) {
494
		if ((scheme.getId() == 411 || scheme.getId() == 612) && inventoryItem.getCreateTimestamp().isAfter(LocalDate.of(2021, 12, 1).atStartOfDay())) {
492
			return 0;
495
			return 0;
493
		}
496
		}
494
		List<SchemeInOut> sios = schemeInOutRepository.selectAllByType(scheme.getType(), inventoryItem.getId());
497
		List<SchemeInOut> sios = schemeInOutRepository.selectAllByType(scheme.getType(), inventoryItem.getId());
495
		float actualCredit = 0;
498
		float actualCredit = 0;
496
		if (sios.stream().filter(x -> x.getRolledBackTimestamp() == null && x.getSchemeId() == scheme.getId())
499
		if (sios.stream().filter(x -> x.getRolledBackTimestamp() == null && x.getSchemeId() == scheme.getId())
497
				.collect(Collectors.toList()).size() == 0) {
500
				.collect(Collectors.toList()).size() == 0) {
498
			sios = sios.stream().filter(x->x.getRolledBackTimestamp()==null).collect(Collectors.toList());
501
			sios = sios.stream().filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList());
499
			//Rejected Scheme for types INVESTMENT and ACTIVATION are considered rolledback only if the item billing is cancelled.
502
			//Rejected Scheme for types INVESTMENT and ACTIVATION are considered rolledback only if the item billing is cancelled.
500
			float amountCredited = (float) sios.stream().mapToDouble(e -> e.getAmount()).sum();
503
			float amountCredited = (float) sios.stream().mapToDouble(e -> e.getAmount()).sum();
501
			
504
 
502
			LOGGER.info("SIOS ===== {}", sios);
505
			LOGGER.info("SIOS ===== {}", sios);
503
			float amountToCredit = this.getAmount(inventoryItem, scheme);
506
			float amountToCredit = this.getAmount(inventoryItem, scheme);
504
			//Activation and in scheme
507
			//Activation and in scheme
505
			if (!scheme.getType().equals(SchemeType.IN)  && !scheme.getType().equals(SchemeType.ACTIVATION) && sios.size() > 0) {
508
			if (!scheme.getType().equals(SchemeType.IN) && !scheme.getType().equals(SchemeType.ACTIVATION) && sios.size() > 0) {
506
 
509
 
507
				if(sios.size() > 1) {
510
				if (sios.size() > 1) {
508
					LOGGER.info("SAMESCHEMETYPE has already been credited twice for inventoryItem - {}", inventoryItem.getId());
511
					LOGGER.info("SAMESCHEMETYPE has already been credited twice for inventoryItem - {}", inventoryItem.getId());
509
					return 0;
512
					return 0;
510
				}
513
				}
511
				if(amountToCredit > amountCredited + 1f) {
514
				if (amountToCredit > amountCredited + 1f) {
512
					for (SchemeInOut sio : sios) {
515
					for (SchemeInOut sio : sios) {
513
						sio.setRolledBackTimestamp(LocalDateTime.now());
516
						sio.setRolledBackTimestamp(LocalDateTime.now());
514
						sio.setStatus(SchemePayoutStatus.REJECTED);
517
						sio.setStatus(SchemePayoutStatus.REJECTED);
515
						sio.setStatusDescription("Partner Category upgraded, new entry added");
518
						sio.setStatusDescription("Partner Category upgraded, new entry added");
516
					}
519
					}
Line 520... Line 523...
520
				}
523
				}
521
			} else {
524
			} else {
522
				actualCredit = amountToCredit;
525
				actualCredit = amountToCredit;
523
			}
526
			}
524
			LOGGER.info("Actual Credit ==== {}", actualCredit);
527
			LOGGER.info("Actual Credit ==== {}", actualCredit);
525
			
528
 
526
			SchemeInOut schemeInOut = new SchemeInOut();
529
			SchemeInOut schemeInOut = new SchemeInOut();
527
			schemeInOut.setSchemeId(scheme.getId());
530
			schemeInOut.setSchemeId(scheme.getId());
528
			schemeInOut.setInventoryItemId(inventoryItem.getId());
531
			schemeInOut.setInventoryItemId(inventoryItem.getId());
529
			schemeInOut.setAmount(amountToCredit);
532
			schemeInOut.setAmount(amountToCredit);
530
			schemeInOutRepository.persist(schemeInOut);
533
			schemeInOutRepository.persist(schemeInOut);
Line 795... Line 798...
795
	}
798
	}
796
 
799
 
797
	@Override
800
	@Override
798
	// Always being called from cancel order/bad return means no SCHEME IN is considered
801
	// Always being called from cancel order/bad return means no SCHEME IN is considered
799
	public void reverseSchemes(List<InventoryItem> inventoryItems, int reversalReference, String reversalReason,
802
	public void reverseSchemes(List<InventoryItem> inventoryItems, int reversalReference, String reversalReason,
800
			List<SchemeType> schemeTypes) throws ProfitMandiBusinessException {
803
							   List<SchemeType> schemeTypes) throws ProfitMandiBusinessException {
801
		Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
804
		Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
802
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
805
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
803
		LOGGER.info("inventoryItems" + inventoryItems);
806
		LOGGER.info("inventoryItems" + inventoryItems);
804
 
807
 
805
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
808
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
Line 829... Line 832...
829
		}
832
		}
830
		int fofoId = inventoryItems.get(0).getFofoId();
833
		int fofoId = inventoryItems.get(0).getFofoId();
831
		WalletReferenceType walletReferenceType = schemeTypes.containsAll(SchemeService.OUT_SCHEME_TYPES)
834
		WalletReferenceType walletReferenceType = schemeTypes.containsAll(SchemeService.OUT_SCHEME_TYPES)
832
				? WalletReferenceType.SCHEME_OUT
835
				? WalletReferenceType.SCHEME_OUT
833
				: (schemeTypes.contains(SchemeType.ACTIVATION) ? WalletReferenceType.ACTIVATION_SCHEME
836
				: (schemeTypes.contains(SchemeType.ACTIVATION) ? WalletReferenceType.ACTIVATION_SCHEME
834
						: WalletReferenceType.INVESTMENT_PAYOUT);
837
				: WalletReferenceType.INVESTMENT_PAYOUT);
835
		if (amountToRollback > 0) {
838
		if (amountToRollback > 0) {
836
			// Mark appropriate reference of rollback investment margin
839
			// Mark appropriate reference of rollback investment margin
837
			if (schemeTypes.contains(SchemeType.INVESTMENT)) {
840
			if (schemeTypes.contains(SchemeType.INVESTMENT)) {
838
				reversalReference = Integer
841
				reversalReference = Integer
839
						.parseInt(FormattingUtils.getYearMonth(schemeInOuts.get(0).getCreditTimestamp().minusMonths(1)));
842
						.parseInt(FormattingUtils.getYearMonth(schemeInOuts.get(0).getCreditTimestamp().minusMonths(1)));
Line 874... Line 877...
874
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
877
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
875
		if (cashbackSchemesMap.size() > 0) {
878
		if (cashbackSchemesMap.size() > 0) {
876
			List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(cashbackSchemesMap.keySet());
879
			List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(cashbackSchemesMap.keySet());
877
			schemeItems.stream().forEach(x -> {
880
			schemeItems.stream().forEach(x -> {
878
				float cashbackAmount = cashbackSchemesMap.get(x.getSchemeId()).getAmount();
881
				float cashbackAmount = cashbackSchemesMap.get(x.getSchemeId()).getAmount();
879
				if(!itemCashbackMap.containsKey(x.getItemId())) {
882
				if (!itemCashbackMap.containsKey(x.getItemId())) {
880
					itemCashbackMap.put(x.getItemId(), cashbackAmount);
883
					itemCashbackMap.put(x.getItemId(), cashbackAmount);
881
				} else {
884
				} else {
882
					itemCashbackMap.put(x.getItemId(), itemCashbackMap.get(x.getItemId()) + cashbackAmount); 
885
					itemCashbackMap.put(x.getItemId(), itemCashbackMap.get(x.getItemId()) + cashbackAmount);
883
				}
886
				}
884
			});
887
			});
885
		}
888
		}
886
		// A107FD Model needs to removed
889
		// A107FD Model needs to removed
887
		itemCashbackMap.remove(30211);
890
		itemCashbackMap.remove(30211);
Line 895... Line 898...
895
	 * @Override public void updateSchmesForModel(int catalogId) throws
898
	 * @Override public void updateSchmesForModel(int catalogId) throws
896
	 * ProfitMandiBusinessException { List<Item> items =
899
	 * ProfitMandiBusinessException { List<Item> items =
897
	 * itemRepository.selectAllByCatalogItemId(catalogId); Map<Integer, Scheme>
900
	 * itemRepository.selectAllByCatalogItemId(catalogId); Map<Integer, Scheme>
898
	 * schemes = schemeRepository .selectAllByItemIds(items.stream().map(x ->
901
	 * schemes = schemeRepository .selectAllByItemIds(items.stream().map(x ->
899
	 * x.getId()).collect(Collectors.toList()));
902
	 * x.getId()).collect(Collectors.toList()));
900
	 * 
903
	 *
901
	 * }
904
	 * }
902
	 */
905
	 */
903
 
906
 
904
	@Override
907
	@Override
905
	public List<Scheme> selectSchemeByPartnerType(PartnerType partnerType, LocalDate onDate, int itemId,
908
	public List<Scheme> selectSchemeByPartnerType(PartnerType partnerType, LocalDate onDate, int itemId,
906
			boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
909
												  boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
907
		Session session = sessionFactory.getCurrentSession();
910
		Session session = sessionFactory.getCurrentSession();
908
		CriteriaBuilder cb = session.getCriteriaBuilder();
911
		CriteriaBuilder cb = session.getCriteriaBuilder();
909
		CriteriaQuery<Scheme> query = cb.createQuery(Scheme.class);
912
		CriteriaQuery<Scheme> query = cb.createQuery(Scheme.class);
910
		Root<Scheme> scheme = query.from(Scheme.class);
913
		Root<Scheme> scheme = query.from(Scheme.class);
911
		List<PartnerType> pt = new ArrayList<>();
914
		List<PartnerType> pt = new ArrayList<>();