Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
22653 ashik.ali 1
package com.spice.profitmandi.service.scheme;
2
 
22859 ashik.ali 3
import java.time.LocalDateTime;
22653 ashik.ali 4
import java.util.HashMap;
22859 ashik.ali 5
import java.util.HashSet;
22653 ashik.ali 6
import java.util.List;
7
import java.util.Map;
8
import java.util.Set;
9
 
22859 ashik.ali 10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
22653 ashik.ali 12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.stereotype.Component;
14
 
22859 ashik.ali 15
import com.spice.profitmandi.common.enumuration.DateTimePattern;
22653 ashik.ali 16
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22859 ashik.ali 17
import com.spice.profitmandi.common.model.CreateSchemeRequest;
22653 ashik.ali 18
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22859 ashik.ali 19
import com.spice.profitmandi.common.util.StringUtils;
22653 ashik.ali 20
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
21
import com.spice.profitmandi.dao.entity.catalog.Scheme;
22
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
22859 ashik.ali 23
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
22653 ashik.ali 24
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
22859 ashik.ali 25
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
26
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
22653 ashik.ali 27
import com.spice.profitmandi.dao.enumuration.catalog.SchemeAmountType;
28
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
22859 ashik.ali 29
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
22653 ashik.ali 30
import com.spice.profitmandi.dao.repository.catalog.RetailerSchemeRepository;
31
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
22859 ashik.ali 32
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
22653 ashik.ali 33
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
22859 ashik.ali 34
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
35
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
22925 ashik.ali 36
import com.spice.profitmandi.service.inventory.OrderService;
22859 ashik.ali 37
import com.spice.profitmandi.service.wallet.WalletService;
22653 ashik.ali 38
 
22859 ashik.ali 39
import in.shop2020.model.v1.catalog.ItemType;
40
import in.shop2020.model.v1.order.WalletReferenceType;
41
 
22653 ashik.ali 42
@Component
43
public class SchemeServiceImpl implements SchemeService {
44
 
22859 ashik.ali 45
	private static final Logger LOGGER = LoggerFactory.getLogger(SchemeServiceImpl.class);
46
 
22653 ashik.ali 47
	@Autowired
48
	private InventoryItemRepository inventoryItemRepository;
49
 
50
	@Autowired
22925 ashik.ali 51
	private OrderService orderService;
22653 ashik.ali 52
 
53
	@Autowired
54
	private SchemeRepository schemeRepository;
55
 
56
	@Autowired
22859 ashik.ali 57
	private RetailerRepository retailerRepository;
58
 
59
	@Autowired
22653 ashik.ali 60
	private RetailerSchemeRepository retailerSchemeRepository;
61
 
62
	@Autowired
22859 ashik.ali 63
	private SchemeInOutRepository schemeInOutRepository;
22653 ashik.ali 64
 
65
	@Autowired
22859 ashik.ali 66
	private ItemRepository itemRepository;
22653 ashik.ali 67
 
22859 ashik.ali 68
	@Autowired
69
	private SchemeItemRepository schemeItemRepository;
70
 
71
	@Autowired
72
	private WalletService walletService;
73
 
22653 ashik.ali 74
	@Override
22859 ashik.ali 75
	public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
76
 
22925 ashik.ali 77
		if(createSchemeRequest.getName() == null || createSchemeRequest.getName().isEmpty()){
78
			throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, createSchemeRequest.getName(), "SCHM_VE_1000");
22653 ashik.ali 79
		}
22859 ashik.ali 80
		if(createSchemeRequest.getAmount() <= 0){
22925 ashik.ali 81
			throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(), "SCHM_VE_1001");
22653 ashik.ali 82
		}
83
 
22859 ashik.ali 84
		if(SchemeAmountType.valueOf(createSchemeRequest.getAmountType()) == SchemeAmountType.PERCENTAGE && createSchemeRequest.getAmount() > 100){
22925 ashik.ali 85
			throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(), "SCHM_VE_1002");
22653 ashik.ali 86
		}
87
 
22859 ashik.ali 88
		if(createSchemeRequest.getStartDateString() == null || createSchemeRequest.getStartDateString().isEmpty()){
22925 ashik.ali 89
			throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, createSchemeRequest.getStartDateString(), "SCHM_VE_1003");
22653 ashik.ali 90
		}
91
 
22859 ashik.ali 92
		if(createSchemeRequest.getEndDateString() == null || createSchemeRequest.getStartDateString().isEmpty()){
22925 ashik.ali 93
			throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, createSchemeRequest.getEndDateString(), "SCHM_VE_1004");
22653 ashik.ali 94
		}
95
 
22859 ashik.ali 96
		Scheme scheme = this.toScheme(creatorId, createSchemeRequest);
97
 
22653 ashik.ali 98
		if(scheme.getStartDate().isAfter(scheme.getEndDate())){
22925 ashik.ali 99
			throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE, scheme.getStartDate() + ", " + scheme.getEndDate(), "SCHM_VE_1005");
22653 ashik.ali 100
		}
101
 
22859 ashik.ali 102
		this.validateItemIds(createSchemeRequest);
103
		schemeRepository.persist(scheme);
104
		if(!createSchemeRequest.isRetailerAll()){
105
			this.validateRetailerIds(createSchemeRequest);
106
			for(int retailerId : createSchemeRequest.getRetailerIds()){
107
				RetailerScheme retailerScheme = new RetailerScheme();
108
				retailerScheme.setRetailerId(retailerId);
109
				retailerScheme.setSchemeId(scheme.getId());
110
				retailerSchemeRepository.persist(retailerScheme);
111
			}
22653 ashik.ali 112
		}
22859 ashik.ali 113
		for(int itemId : createSchemeRequest.getItemIds()){
114
			SchemeItem schemeItem = new SchemeItem();
115
			schemeItem.setSchemeId(scheme.getId());
116
			schemeItem.setItemId(itemId);
117
			schemeItemRepository.persist(schemeItem);
118
		}
119
 
22653 ashik.ali 120
	}
22859 ashik.ali 121
 
122
	private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest){
123
		Scheme scheme = new Scheme();
124
		scheme.setName(createSchemeRequest.getName());
125
		scheme.setDescription(createSchemeRequest.getDescription());
126
		scheme.setType(SchemeType.valueOf(createSchemeRequest.getType()));
127
		scheme.setAmountType(SchemeAmountType.valueOf(createSchemeRequest.getAmountType()));
128
		scheme.setAmount(createSchemeRequest.getAmount());
129
		scheme.setStartDate(StringUtils.toDateTime(createSchemeRequest.getStartDateString(), DateTimePattern.YYYY_MM_DD_T_HH_MM));
130
		scheme.setEndDate(StringUtils.toDateTime(createSchemeRequest.getEndDateString(), DateTimePattern.YYYY_MM_DD_T_HH_MM));
131
		if(createSchemeRequest.isActive()){
132
			scheme.setActiveTimestamp(LocalDateTime.now());
133
		}
134
		scheme.setCreatedBy(creatorId);
135
		scheme.setRetailerAll(createSchemeRequest.isRetailerAll());
136
		return scheme;
137
	}
138
 
139
	private void validateRetailerIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException{
22925 ashik.ali 140
		if(createSchemeRequest.getRetailerIds() == null || createSchemeRequest.getRetailerIds().isEmpty()){
141
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, createSchemeRequest.getRetailerIds(), "SCHM_1001");
22859 ashik.ali 142
		}
22870 ashik.ali 143
		List<Integer> foundRetailerIds = retailerRepository.selectIdsByIds(createSchemeRequest.getRetailerIds());
22859 ashik.ali 144
		if(foundRetailerIds.size() != createSchemeRequest.getRetailerIds().size()){
145
			createSchemeRequest.getRetailerIds().removeAll(foundRetailerIds);
22925 ashik.ali 146
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, createSchemeRequest.getRetailerIds(), "SCHM_1002");
22859 ashik.ali 147
		}
148
	}
149
 
150
	private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException{
151
		if(createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()){
22925 ashik.ali 152
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(), "SCHM_1003");
22859 ashik.ali 153
		}
154
		List<Integer> foundItemIds = itemRepository.selectIdsByIdsAndType(createSchemeRequest.getItemIds(), ItemType.SERIALIZED);
155
		if(foundItemIds.size() != createSchemeRequest.getItemIds().size()){
156
			createSchemeRequest.getItemIds().removeAll(foundItemIds);
22925 ashik.ali 157
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(), "SCHM_1004");
22859 ashik.ali 158
		}
159
	}
160
 
161
	@Override
162
	public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
163
		Scheme scheme = schemeRepository.selectById(schemeId);
164
		if(!scheme.isRetailerAll()){
165
			List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeId(scheme.getId());
166
			for(RetailerScheme retailerScheme : retailerSchemes){
167
				scheme.getRetailerIds().add(retailerScheme.getRetailerId());
168
			}
169
		}
170
		List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
171
		scheme.setItemIds(new HashSet<>(itemIds));
172
		return scheme;
173
	}
22653 ashik.ali 174
 
175
	@Override
176
	public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
177
		Scheme scheme = schemeRepository.selectById(schemeId);
178
		if(scheme.getActiveTimestamp() != null){
22925 ashik.ali 179
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(), "SCHM_1005");
22653 ashik.ali 180
		}
181
		if(scheme.getExpireTimestamp() != null){
22925 ashik.ali 182
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "SCHM_1006");
22653 ashik.ali 183
		}
22859 ashik.ali 184
		scheme.setActiveTimestamp(LocalDateTime.now());
185
		schemeRepository.persist(scheme);
22653 ashik.ali 186
	}
187
 
188
	@Override
189
	public void expireSchemeById(int schemeId) throws ProfitMandiBusinessException {
190
		Scheme scheme = schemeRepository.selectById(schemeId);
191
		if(scheme.getActiveTimestamp() == null){
22925 ashik.ali 192
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(), "SCHM_1007");
22653 ashik.ali 193
		}
194
		if(scheme.getExpireTimestamp() != null){
22925 ashik.ali 195
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "SCHM_1008");
22653 ashik.ali 196
		}
22859 ashik.ali 197
		scheme.setExpireTimestamp(LocalDateTime.now());
198
		schemeRepository.persist(scheme);
22653 ashik.ali 199
	}
22859 ashik.ali 200
 
201
	private Map<Integer, Scheme> toSchemeIdSchemesMap(List<Scheme> schemes){
202
		Map<Integer, Scheme> schemeIdSchemesMap = new HashMap<>();
203
		for(Scheme scheme : schemes){
204
			schemeIdSchemesMap.put(scheme.getId(), scheme);
205
		}
206
		return schemeIdSchemesMap;
207
	}
208
 
209
	private Set<Integer> inventoryItemSchemesMapToSchemeIds(Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap){
210
		Set<Integer> schemeIds = new HashSet<>();
211
		for(Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()){
212
			for(Scheme scheme : inventoryItemSchemesEntry.getValue()){
213
				schemeIds.add(scheme.getId());
214
			}
215
		}
216
		return schemeIds;
217
	}
218
 
219
	private Set<Integer> fofoLineItemSchemesMapToSchemeIds(Map<FofoOrderItem, Set<Scheme>> fofoLineItemSchemesMap){
220
		Set<Integer> schemeIds = new HashSet<>();
221
		for(Map.Entry<FofoOrderItem, Set<Scheme>> inventoryItemSchemesEntry : fofoLineItemSchemesMap.entrySet()){
222
			for(Scheme scheme : inventoryItemSchemesEntry.getValue()){
223
				schemeIds.add(scheme.getId());
224
			}
225
		}
226
		return schemeIds;
227
	}
228
 
229
	private Set<Integer> inventoryItemsToItemIds(List<InventoryItem> inventoryItems){
230
		Set<Integer> itemIds = new HashSet<>();
231
		for(InventoryItem inventoryItem : inventoryItems){
232
			itemIds.add(inventoryItem.getItemId());
233
		}
234
		return itemIds;
235
	}
236
 
237
	private Set<Integer> fofoOrderItemsToItemIds(List<FofoOrderItem> fofoOrderItems){
238
		Set<Integer> itemIds = new HashSet<>();
239
		for(FofoOrderItem fofoOrderItem : fofoOrderItems){
240
			itemIds.add(fofoOrderItem.getItemId());
241
		}
242
		return itemIds;
243
	}
244
 
245
	private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes){
246
		Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemesMap(schemes);
247
		Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
248
		for(SchemeItem schemeItem : schemeItems){
249
			if(!itemIdSchemesMap.containsKey(schemeItem.getItemId())){
250
				Set<Scheme> schemesSet = new HashSet<>();
251
				schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
252
				itemIdSchemesMap.put(schemeItem.getItemId(), schemesSet);
253
			}else{
254
				itemIdSchemesMap.get(schemeItem.getItemId()).add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
255
			}
256
		}
257
		return itemIdSchemesMap;
258
	}
22653 ashik.ali 259
 
22859 ashik.ali 260
	private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes, List<InventoryItem> inventoryItems){
261
		Set<Integer> schemeIds = new HashSet<>();
262
		for(Scheme scheme : schemes){
263
			schemeIds.add(scheme.getId());
264
		}
265
		Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
266
		List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
22653 ashik.ali 267
 
22859 ashik.ali 268
		Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
269
		Map<InventoryItem, Set<Scheme>> inventoryItemSchemsMap = new HashMap<>();
270
		for(InventoryItem inventoryItem : inventoryItems){
271
			if(itemIdSchemesMap.containsKey(inventoryItem.getItemId())){
272
				inventoryItemSchemsMap.put(inventoryItem, itemIdSchemesMap.get(inventoryItem.getItemId()));
273
			}
274
		}
275
		return inventoryItemSchemsMap;
276
	}
277
 
278
	private Map<FofoOrderItem, Set<Scheme>> toFofoOrderItemSchemesMap(List<Scheme> schemes, List<FofoOrderItem> fofoOrderItems){
279
		Set<Integer> schemeIds = new HashSet<>();
22653 ashik.ali 280
		for(Scheme scheme : schemes){
22859 ashik.ali 281
			schemeIds.add(scheme.getId());
282
		}
283
		Set<Integer> itemIds = this.fofoOrderItemsToItemIds(fofoOrderItems);
284
		List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
285
 
286
		Map<FofoOrderItem, Set<Scheme>> fofoOrderItemSchemsMap = new HashMap<>();
287
 
288
		if(!schemeItems.isEmpty()){
289
			Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
290
			for(FofoOrderItem fofoOrderItem : fofoOrderItems){
291
				if(itemIdSchemesMap.containsKey(fofoOrderItem.getItemId())){
292
					fofoOrderItemSchemsMap.put(fofoOrderItem, itemIdSchemesMap.get(fofoOrderItem.getItemId()));
293
				}
22653 ashik.ali 294
			}
295
		}
22859 ashik.ali 296
		return fofoOrderItemSchemsMap;
297
	}
298
 
299
	@Override
300
	public float processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
301
		List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, LocalDateTime.now());
302
		float totalCashback = 0;
303
		if(schemes.isEmpty()){
304
			return 0;
22653 ashik.ali 305
		}
22859 ashik.ali 306
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
307
		LOGGER.info("SchemeIn filter inventoryItems {}", inventoryItems);
22653 ashik.ali 308
 
22859 ashik.ali 309
		Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap = this.toInventoryItemSchemesMap(schemes, inventoryItems);
310
 
311
		if(inventoryItemSchemesMap.isEmpty()){
312
			return 0;
313
		}
314
		Map<InventoryItem, Set<Scheme>> notAllInventoryItemSchemesMap = new HashMap<>();
315
		Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
316
 
317
		for(Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()){
318
			Set<Scheme> notAllSchemes = new HashSet<>();
319
			Set<Scheme> allSchemes = new HashSet<>();
320
			for(Scheme scheme : inventoryItemSchemesEntry.getValue()){
321
				if(!scheme.isRetailerAll()){
322
					notAllSchemes.add(scheme);
323
				}else{
324
					allSchemes.add(scheme);
325
				}
326
			}
327
			notAllInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), notAllSchemes);
328
			allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
329
		}
330
 
331
		for(Map.Entry<InventoryItem, Set<Scheme>> allInventoryItemSchemesEntry : allInventoryItemSchemesMap.entrySet()){
332
			for(Scheme scheme : allInventoryItemSchemesEntry.getValue()){
333
				float cashback = this.createSchemeInOut(scheme, allInventoryItemSchemesEntry.getKey());
334
				totalCashback = totalCashback + cashback;
335
			}
336
		}
337
 
338
		Set<Integer> schemeIds = this.inventoryItemSchemesMapToSchemeIds(notAllInventoryItemSchemesMap);
339
		if(!schemeIds.isEmpty()){
340
			List<Integer> foundSchemeIds = retailerSchemeRepository.selectSchemeIds(schemeIds, retailerId);
341
 
342
			for(Map.Entry<InventoryItem, Set<Scheme>> notAllInventoryItemSchemesEntry : notAllInventoryItemSchemesMap.entrySet()){
343
				for(Scheme scheme : notAllInventoryItemSchemesEntry.getValue()){
344
					if(foundSchemeIds.contains(scheme.getId())){
345
						float cashback = this.createSchemeInOut(scheme, notAllInventoryItemSchemesEntry.getKey());
346
						totalCashback = totalCashback + cashback;
22653 ashik.ali 347
					}
348
				}
349
			}
350
		}
22859 ashik.ali 351
		if(totalCashback > 0){
352
			walletService.addAmountToWallet(retailerId, purchaseId, WalletReferenceType.SCHEME_IN, "Added for SCHEME_IN", totalCashback);
353
		}
354
		return totalCashback;
22653 ashik.ali 355
	}
356
 
22859 ashik.ali 357
	private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem){
358
		float amount = this.getAmount(inventoryItem, scheme);
359
		SchemeInOut schemeInOut = new SchemeInOut();
360
		schemeInOut.setSchemeId(scheme.getId());
361
		schemeInOut.setInventoryItemId(inventoryItem.getId());
362
		schemeInOut.setAmount(amount);
363
		schemeInOutRepository.persist(schemeInOut);
364
		return amount;
365
	}
366
 
367
	private float getAmount(InventoryItem inventoryItem, Scheme scheme){
22653 ashik.ali 368
		float amount = 0;
22859 ashik.ali 369
		float totalTaxRate = inventoryItem.getIgstRate() + inventoryItem.getSgstRate() + inventoryItem.getCgstRate();
370
		float taxableSellingPrice = inventoryItem.getUnitPrice() / (1 + totalTaxRate / 100);
371
 
22653 ashik.ali 372
		if(scheme.getAmountType() == SchemeAmountType.PERCENTAGE){
22859 ashik.ali 373
			amount = taxableSellingPrice * scheme.getAmount() / 100;
22653 ashik.ali 374
		}else{
375
			amount = scheme.getAmount();
376
		}
22859 ashik.ali 377
		return amount;
22653 ashik.ali 378
	}
379
 
22859 ashik.ali 380
	private Map<Integer, InventoryItem> toInventoryItemIdInventoryItemMap(List<InventoryItem> inventoryItems){
381
		Map<Integer, InventoryItem> itemIdInventoryItemMap = new HashMap<>();
382
		for(InventoryItem inventoryItem : inventoryItems){
383
			itemIdInventoryItemMap.put(inventoryItem.getId(), inventoryItem);
22653 ashik.ali 384
		}
22859 ashik.ali 385
		return itemIdInventoryItemMap;
22653 ashik.ali 386
	}
22859 ashik.ali 387
 
388
 
389
	private Set<Integer> toInventoryItemIds(Set<FofoOrderItem> fofoOrderItems){
390
		Set<Integer> inventoryItemIds = new HashSet<>();
391
		//Map<Integer, Set<FofoLineItem>> fofoOrderItemIdFofoLineItemsMap = this.toFofoOrderItemIdFofoLineItems(fofoOrderItems);
392
		for(FofoOrderItem fofoOrderItem : fofoOrderItems){
393
			for(FofoLineItem fofoLineItem : fofoOrderItem.getFofoLineItems()){
394
				inventoryItemIds.add(fofoLineItem.getInventoryItemId());
395
			}
396
		}
397
		return inventoryItemIds;
398
	}
22653 ashik.ali 399
 
400
	@Override
22859 ashik.ali 401
	public float processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
402
		List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.OUT, LocalDateTime.now());
403
		LOGGER.info("Active Schemes {}", schemes);
404
		float totalCashback = 0;
405
		if(schemes.isEmpty()){
406
			return 0;
407
		}
22653 ashik.ali 408
 
22925 ashik.ali 409
		List<FofoOrderItem> fofoOrderItems = orderService.getByOrderId(fofoOrderId);
22859 ashik.ali 410
		//LOGGER.info("fofoOrderItems {}", fofoOrderItems);
411
 
412
		Map<FofoOrderItem, Set<Scheme>> fofoOrderItemSchemesMap = this.toFofoOrderItemSchemesMap(schemes, fofoOrderItems);
413
 
414
		LOGGER.info("fofoOrderItemSchemesMap {}", fofoOrderItemSchemesMap);
415
 
416
		if(fofoOrderItemSchemesMap.isEmpty()){
417
			return 0;
418
		}
419
 
420
		Set<Integer> inventoryItemIds = this.toInventoryItemIds(fofoOrderItemSchemesMap.keySet());
421
 
422
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIds);
423
 
424
		Map<Integer, InventoryItem> inventoryItemIdInventoryItemMap = this.toInventoryItemIdInventoryItemMap(inventoryItems);
425
 
426
		Map<FofoOrderItem, Set<Scheme>> notAllFofoOrderItemSchemesMap = new HashMap<>();
427
		Map<FofoOrderItem, Set<Scheme>> allFofoOrderItemSchemesMap = new HashMap<>();
428
 
429
		for(Map.Entry<FofoOrderItem, Set<Scheme>> fofoOrderItemSchemesEntry : fofoOrderItemSchemesMap.entrySet()){
430
			Set<Scheme> notAllSchemes = new HashSet<>();
431
			Set<Scheme> allSchemes = new HashSet<>();
432
			for(Scheme scheme : fofoOrderItemSchemesEntry.getValue()){
433
				if(!scheme.isRetailerAll()){
434
					notAllSchemes.add(scheme);
435
				}else{
436
					allSchemes.add(scheme);
437
				}
22653 ashik.ali 438
			}
22859 ashik.ali 439
			notAllFofoOrderItemSchemesMap.put(fofoOrderItemSchemesEntry.getKey(), notAllSchemes);
440
			allFofoOrderItemSchemesMap.put(fofoOrderItemSchemesEntry.getKey(), allSchemes);
22653 ashik.ali 441
		}
22859 ashik.ali 442
 
443
		//Map<Integer, Set<FofoLineItem>> allFofoOrderItemIdFofoLineItemsMap = this.toFofoOrderItemIdFofoLineItems(allFofoOrderItemSchemesMap.keySet());
444
 
445
		for(Map.Entry<FofoOrderItem, Set<Scheme>> allFofoOrderItemSchemesEntry : allFofoOrderItemSchemesMap.entrySet()){
446
			for(FofoLineItem fofoLineItem : allFofoOrderItemSchemesEntry.getKey().getFofoLineItems()){
447
				for(Scheme scheme : allFofoOrderItemSchemesEntry.getValue()){
448
					float cashback = this.createSchemeInOut(scheme, inventoryItemIdInventoryItemMap.get(fofoLineItem.getInventoryItemId()));
449
					totalCashback = totalCashback + cashback;
450
				}
451
			}
22653 ashik.ali 452
		}
453
 
22859 ashik.ali 454
		Set<Integer> schemeIds = this.fofoLineItemSchemesMapToSchemeIds(notAllFofoOrderItemSchemesMap);
455
 
456
		if(!schemeIds.isEmpty()){
457
			List<Integer> foundSchemeIds = retailerSchemeRepository.selectSchemeIds(schemeIds, retailerId);
458
 
459
			//Map<Integer, Set<FofoLineItem>> notAllFofoOrderItemIdFofoLineItemsMap = this.toFofoOrderItemIdFofoLineItems(allFofoOrderItemSchemesMap.keySet());
460
 
461
			for(Map.Entry<FofoOrderItem, Set<Scheme>> notAllFofoOrderItemSchemesEntry : notAllFofoOrderItemSchemesMap.entrySet()){
462
				for(FofoLineItem fofoLineItem : notAllFofoOrderItemSchemesEntry.getKey().getFofoLineItems()){
463
					for(Scheme scheme : notAllFofoOrderItemSchemesEntry.getValue()){
464
						if(foundSchemeIds.contains(scheme.getId())){
465
							float cashback = this.createSchemeInOut(scheme, inventoryItemIdInventoryItemMap.get(fofoLineItem.getInventoryItemId()));
466
							totalCashback = totalCashback + cashback;
22653 ashik.ali 467
						}
468
					}
469
				}
22859 ashik.ali 470
			}	
22653 ashik.ali 471
		}
22859 ashik.ali 472
		if(totalCashback > 0){
473
			walletService.addAmountToWallet(retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT, "Added for SCHEME_OUT", totalCashback);
474
		}
475
		return totalCashback;
22653 ashik.ali 476
	}
477
 
478
}