Subversion Repositories SmartDukaan

Rev

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

Rev 22955 Rev 23019
Line 1... Line 1...
1
package com.spice.profitmandi.service.scheme;
1
package com.spice.profitmandi.service.scheme;
2
 
2
 
3
import java.time.LocalDateTime;
3
import java.time.LocalDateTime;
-
 
4
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.HashMap;
5
import java.util.HashSet;
6
import java.util.HashSet;
6
import java.util.List;
7
import java.util.List;
7
import java.util.Map;
8
import java.util.Map;
8
import java.util.Set;
9
import java.util.Set;
Line 14... Line 15...
14
 
15
 
15
import com.spice.profitmandi.common.enumuration.DateTimePattern;
16
import com.spice.profitmandi.common.enumuration.DateTimePattern;
16
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
17
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
17
import com.spice.profitmandi.common.model.CreateSchemeRequest;
18
import com.spice.profitmandi.common.model.CreateSchemeRequest;
18
import com.spice.profitmandi.common.model.ProfitMandiConstants;
19
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
20
import com.spice.profitmandi.common.model.SchemeModel;
19
import com.spice.profitmandi.common.util.StringUtils;
21
import com.spice.profitmandi.common.util.StringUtils;
20
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
22
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
21
import com.spice.profitmandi.dao.entity.catalog.Scheme;
23
import com.spice.profitmandi.dao.entity.catalog.Scheme;
22
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
24
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
23
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
25
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
Line 28... Line 30...
28
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
30
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
29
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
31
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
30
import com.spice.profitmandi.dao.repository.catalog.RetailerSchemeRepository;
32
import com.spice.profitmandi.dao.repository.catalog.RetailerSchemeRepository;
31
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
33
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
32
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
34
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
-
 
35
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
-
 
36
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
33
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
37
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
34
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
38
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
35
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
39
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
36
import com.spice.profitmandi.service.inventory.OrderService;
40
import com.spice.profitmandi.service.inventory.OrderService;
37
import com.spice.profitmandi.service.wallet.WalletService;
41
import com.spice.profitmandi.service.wallet.WalletService;
Line 69... Line 73...
69
	private SchemeItemRepository schemeItemRepository;
73
	private SchemeItemRepository schemeItemRepository;
70
	
74
	
71
	@Autowired
75
	@Autowired
72
	private WalletService walletService;
76
	private WalletService walletService;
73
	
77
	
-
 
78
	@Autowired
-
 
79
	private FofoOrderItemRepository fofoOrderItemRepository;
-
 
80
	
-
 
81
	@Autowired
-
 
82
	private FofoLineItemRepository fofoLineItemRepository;
-
 
83
	
74
	@Override
84
	@Override
75
	public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
85
	public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
76
		
86
		
77
		if(createSchemeRequest.getName() == null || createSchemeRequest.getName().isEmpty()){
-
 
78
			throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, createSchemeRequest.getName(), "SCHM_VE_1000");
-
 
79
		}
-
 
80
		if(createSchemeRequest.getAmount() <= 0){
87
		this.validateCreateSchemeRequest(createSchemeRequest);
81
			throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(), "SCHM_VE_1001");
-
 
82
		}
-
 
83
		
-
 
84
		if(SchemeAmountType.valueOf(createSchemeRequest.getAmountType()) == SchemeAmountType.PERCENTAGE && createSchemeRequest.getAmount() > 100){
-
 
85
			throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(), "SCHM_VE_1002");
-
 
86
		}
-
 
87
		
-
 
88
		if(createSchemeRequest.getStartDateString() == null || createSchemeRequest.getStartDateString().isEmpty()){
-
 
89
			throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, createSchemeRequest.getStartDateString(), "SCHM_VE_1003");
-
 
90
		}
-
 
91
		
-
 
92
		if(createSchemeRequest.getEndDateString() == null || createSchemeRequest.getStartDateString().isEmpty()){
-
 
93
			throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, createSchemeRequest.getEndDateString(), "SCHM_VE_1004");
-
 
94
		}
-
 
95
		
88
		
96
		Scheme scheme = this.toScheme(creatorId, createSchemeRequest);
89
		Scheme scheme = this.toScheme(creatorId, createSchemeRequest);
97
		
90
		
98
		if(scheme.getStartDate().isAfter(scheme.getEndDate())){
91
		if(scheme.getStartDateTime().isAfter(scheme.getEndDateTime())){
99
			throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE, scheme.getStartDate() + ", " + scheme.getEndDate(), "SCHM_VE_1005");
92
			throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE, scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
100
		}
93
		}
101
		
94
		
102
		this.validateItemIds(createSchemeRequest);
95
		this.validateItemIds(createSchemeRequest);
103
		schemeRepository.persist(scheme);
96
		schemeRepository.persist(scheme);
104
		if(!createSchemeRequest.isRetailerAll()){
97
		if(!createSchemeRequest.isRetailerAll()){
Line 117... Line 110...
117
			schemeItemRepository.persist(schemeItem);
110
			schemeItemRepository.persist(schemeItem);
118
		}
111
		}
119
		
112
		
120
	}
113
	}
121
	
114
	
-
 
115
	private void validateCreateSchemeRequest(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException{
-
 
116
		if(createSchemeRequest.getName() == null || createSchemeRequest.getName().isEmpty()){
-
 
117
			throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, createSchemeRequest.getName(), "SCHM_VE_1000");
-
 
118
		}
-
 
119
		if(createSchemeRequest.getAmount() <= 0){
-
 
120
			throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(), "SCHM_VE_1001");
-
 
121
		}
-
 
122
		
-
 
123
		if(SchemeAmountType.valueOf(createSchemeRequest.getAmountType()) == SchemeAmountType.PERCENTAGE && createSchemeRequest.getAmount() > 100){
-
 
124
			throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(), "SCHM_VE_1002");
-
 
125
		}
-
 
126
		
-
 
127
		if(createSchemeRequest.getStartDateTimeString() == null || createSchemeRequest.getStartDateTimeString().isEmpty()){
-
 
128
			throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, createSchemeRequest.getStartDateTimeString(), "SCHM_VE_1003");
-
 
129
		}
-
 
130
		
-
 
131
		if(createSchemeRequest.getEndDateTimeString() == null || createSchemeRequest.getStartDateTimeString().isEmpty()){
-
 
132
			throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, createSchemeRequest.getEndDateTimeString(), "SCHM_VE_1004");
-
 
133
		}
-
 
134
	}
-
 
135
	
122
	private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest){
136
	private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest){
123
		Scheme scheme = new Scheme();
137
		Scheme scheme = new Scheme();
124
		scheme.setName(createSchemeRequest.getName());
138
		scheme.setName(createSchemeRequest.getName());
125
		scheme.setDescription(createSchemeRequest.getDescription());
139
		scheme.setDescription(createSchemeRequest.getDescription());
126
		scheme.setType(SchemeType.valueOf(createSchemeRequest.getType()));
140
		scheme.setType(SchemeType.valueOf(createSchemeRequest.getType()));
127
		scheme.setAmountType(SchemeAmountType.valueOf(createSchemeRequest.getAmountType()));
141
		scheme.setAmountType(SchemeAmountType.valueOf(createSchemeRequest.getAmountType()));
128
		scheme.setAmount(createSchemeRequest.getAmount());
142
		scheme.setAmount(createSchemeRequest.getAmount());
129
		scheme.setStartDate(StringUtils.toDateTime(createSchemeRequest.getStartDateString(), DateTimePattern.YYYY_MM_DD_T_HH_MM));
143
		scheme.setStartDateTime(StringUtils.toDateTime(createSchemeRequest.getStartDateTimeString(), DateTimePattern.DD_MM_YYYY_T_HH_MM_SS));
130
		scheme.setEndDate(StringUtils.toDateTime(createSchemeRequest.getEndDateString(), DateTimePattern.YYYY_MM_DD_T_HH_MM));
144
		scheme.setEndDateTime(StringUtils.toDateTime(createSchemeRequest.getEndDateTimeString(), DateTimePattern.DD_MM_YYYY_T_HH_MM_SS));
131
		if(createSchemeRequest.isActive()){
-
 
132
			scheme.setActiveTimestamp(LocalDateTime.now());
-
 
133
		}
-
 
134
		scheme.setCreatedBy(creatorId);
145
		scheme.setCreatedBy(creatorId);
135
		scheme.setRetailerAll(createSchemeRequest.isRetailerAll());
146
		scheme.setRetailerAll(createSchemeRequest.isRetailerAll());
136
		return scheme;
147
		return scheme;
137
	}
148
	}
138
	
149
	
Line 160... Line 171...
160
	
171
	
161
	@Override
172
	@Override
162
	public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
173
	public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
163
		Scheme scheme = schemeRepository.selectById(schemeId);
174
		Scheme scheme = schemeRepository.selectById(schemeId);
164
		if(!scheme.isRetailerAll()){
175
		if(!scheme.isRetailerAll()){
165
			List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeId(scheme.getId());
176
			List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
166
			for(RetailerScheme retailerScheme : retailerSchemes){
-
 
167
				scheme.getRetailerIds().add(retailerScheme.getRetailerId());
177
			scheme.getRetailerIds().addAll(retailerIds);
168
			}
-
 
169
		}
178
		}
170
		List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
179
		List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
171
		scheme.setItemIds(new HashSet<>(itemIds));
180
		scheme.setItemIds(new HashSet<>(itemIds));
172
		return scheme;
181
		return scheme;
173
	}
182
	}
-
 
183
	
-
 
184
	@Override
-
 
185
	public List<SchemeModel> getAllSchemeModels(LocalDateTime startDateTime, LocalDateTime endDateTime) {
-
 
186
		List<Scheme> schemes = schemeRepository.selectAllBetweenCreateTimestamp(startDateTime, endDateTime);
-
 
187
		Map<Integer, Scheme> schemeIdSchemeMap = this.toSchemeIdSchemeMap(schemes);
-
 
188
		List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
-
 
189
		this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap);
-
 
190
		List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
-
 
191
		this.addRetailerIdsToSchemes(retailerSchemes, schemeIdSchemeMap);
-
 
192
		return this.toSchemeModels(schemeIdSchemeMap);
-
 
193
	}
-
 
194
	
-
 
195
	private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap){
-
 
196
		for(SchemeItem schemeItem : schemeItems){
-
 
197
			schemeIdSchemeMap.get(schemeItem.getSchemeId())
-
 
198
			.getItemIds()
-
 
199
			.add(schemeItem.getItemId());
-
 
200
		}
-
 
201
	}
-
 
202
	
-
 
203
	private void addRetailerIdsToSchemes(List<RetailerScheme> retailerSchemes, Map<Integer, Scheme> schemeIdSchemeMap){
-
 
204
		for(RetailerScheme retailerScheme : retailerSchemes){
-
 
205
			schemeIdSchemeMap.get(retailerScheme.getSchemeId())
-
 
206
			.getRetailerIds()
-
 
207
			.add(retailerScheme.getRetailerId());
-
 
208
		}
-
 
209
	}
-
 
210
	
-
 
211
	private List<SchemeModel> toSchemeModels(Map<Integer, Scheme> schemeIdSchemeMap){
-
 
212
		List<SchemeModel> schemeModels = new ArrayList<>();
-
 
213
		for(Map.Entry<Integer, Scheme> schemeIdSchemeEntry : schemeIdSchemeMap.entrySet()){
-
 
214
			schemeModels.add(this.toSchemeModel(schemeIdSchemeEntry.getValue()));
-
 
215
		}
-
 
216
		return schemeModels;
-
 
217
	}
-
 
218
	
-
 
219
	private SchemeModel toSchemeModel(Scheme scheme){
-
 
220
		SchemeModel schemeModel = new SchemeModel();
-
 
221
		schemeModel.setSchemeId(scheme.getId());
-
 
222
		schemeModel.setName(scheme.getName());
-
 
223
		schemeModel.setDescription(scheme.getDescription());
-
 
224
		schemeModel.setSchemeType(scheme.getType().toString());
-
 
225
		schemeModel.setAmountType(scheme.getAmountType().toString());
-
 
226
		schemeModel.setAmount(scheme.getAmount());
-
 
227
		schemeModel.setStartDateTime(StringUtils.toString(scheme.getStartDateTime()));
-
 
228
		schemeModel.setEndDateTime(StringUtils.toString(scheme.getEndDateTime()));
-
 
229
		schemeModel.setCreateTimestamp(StringUtils.toString(scheme.getCreateTimestamp()));
-
 
230
		schemeModel.setActiveTimestamp(StringUtils.toString(scheme.getActiveTimestamp()));
-
 
231
		schemeModel.setExpireTimestamp(StringUtils.toString(scheme.getExpireTimestamp()));
-
 
232
		schemeModel.setCreatedBy(scheme.getCreatedBy());
-
 
233
		schemeModel.setItemIds(scheme.getItemIds());
-
 
234
		schemeModel.setRetailerIds(scheme.getRetailerIds());
-
 
235
		return schemeModel;
-
 
236
	}
174
 
237
 
175
	@Override
238
	@Override
176
	public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
239
	public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
177
		Scheme scheme = schemeRepository.selectById(schemeId);
240
		Scheme scheme = schemeRepository.selectById(schemeId);
178
		if(scheme.getActiveTimestamp() != null){
241
		if(scheme.getActiveTimestamp() != null){
Line 181... Line 244...
181
		if(scheme.getExpireTimestamp() != null){
244
		if(scheme.getExpireTimestamp() != null){
182
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "SCHM_1006");
245
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(), "SCHM_1006");
183
		}
246
		}
184
		scheme.setActiveTimestamp(LocalDateTime.now());
247
		scheme.setActiveTimestamp(LocalDateTime.now());
185
		schemeRepository.persist(scheme);
248
		schemeRepository.persist(scheme);
-
 
249
		if(scheme.getType() == SchemeType.IN){
-
 
250
			this.processPreviousPurchases(scheme);
-
 
251
		}else if(scheme.getType() == SchemeType.OUT){
-
 
252
			this.processPreviousSales(scheme);
-
 
253
		}
-
 
254
	}
-
 
255
	
-
 
256
	private void processPreviousPurchases(Scheme scheme) throws ProfitMandiBusinessException{
-
 
257
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectBetweenDates(scheme.getStartDateTime(), LocalDateTime.now());
-
 
258
		List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
-
 
259
		List<InventoryItem> completedInventoryItems = this.toFilteredPurchaseCompletedInventoryItems(inventoryItems, itemIds, true);
-
 
260
		
-
 
261
		if(scheme.isRetailerAll()){
-
 
262
			for(InventoryItem inventoryItem : completedInventoryItems){
-
 
263
				float cashback = this.createSchemeInOut(scheme, inventoryItem);
-
 
264
				walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(), WalletReferenceType.SCHEME_IN, "Added for SCHEME_IN", cashback);
-
 
265
			}
-
 
266
		}else{
-
 
267
			List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
-
 
268
			for(InventoryItem inventoryItem : completedInventoryItems){
-
 
269
				if(retailerIds.contains(inventoryItem.getFofoId())){
-
 
270
					float cashback = this.createSchemeInOut(scheme, inventoryItem);
-
 
271
					walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(), WalletReferenceType.SCHEME_IN, "Added for SCHEME_IN", cashback);
-
 
272
				}
-
 
273
			}
-
 
274
		}
-
 
275
		
186
	}
276
	}
-
 
277
	
-
 
278
	
-
 
279
	
-
 
280
	private Map<Integer, Integer> toInventoryItemIdFofoOrderMap(List<FofoLineItem> fofoLineItems, Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap){
-
 
281
		Map<Integer, Integer> inventoryItemIdFofoOrderIdMap = new HashMap<>();
-
 
282
		
-
 
283
		for(FofoLineItem fofoLineItem : fofoLineItems){
-
 
284
			inventoryItemIdFofoOrderIdMap.put(
-
 
285
					fofoLineItem.getInventoryItemId(),
-
 
286
					fofoOrderItemIdFofoOrderItemMap.get(fofoLineItem.getFofoOrderItemId()).getOrderId());
-
 
287
		}
-
 
288
		return inventoryItemIdFofoOrderIdMap;
-
 
289
	}
-
 
290
	
-
 
291
	private void processPreviousSales(Scheme scheme) throws ProfitMandiBusinessException{
-
 
292
		List<FofoOrderItem> fofoOrderItems = fofoOrderItemRepository.selectBetweenDates(scheme.getStartDateTime(), LocalDateTime.now());
-
 
293
		
-
 
294
		Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap = this.toFofoOrderItemIdFofoOrderItemMap(fofoOrderItems);
-
 
295
 
-
 
296
		List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByFofoOrderItemIds(fofoOrderItemIdFofoOrderItemMap.keySet());
-
 
297
		
-
 
298
		Map<Integer, Integer> inventoryItemIdFofoOrderIdMap = this.toInventoryItemIdFofoOrderMap(fofoLineItems, fofoOrderItemIdFofoOrderItemMap);
-
 
299
		
-
 
300
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIdFofoOrderIdMap.keySet());
-
 
301
		
-
 
302
		List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
-
 
303
		
-
 
304
		List<InventoryItem> completedInventoryItems = this.toFilteredPurchaseCompletedInventoryItems(inventoryItems, itemIds, false);
-
 
305
		
-
 
306
		
-
 
307
		if(scheme.isRetailerAll()){
-
 
308
			for(InventoryItem inventoryItem : completedInventoryItems){
-
 
309
				float cashback = this.createSchemeInOut(scheme, inventoryItem);
-
 
310
				walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(), WalletReferenceType.SCHEME_IN, "Added for SCHEME_IN", cashback);
-
 
311
			}
-
 
312
		}else{
-
 
313
			List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
-
 
314
			for(InventoryItem inventoryItem : completedInventoryItems){
-
 
315
				if(retailerIds.contains(inventoryItem.getFofoId())){
-
 
316
					float cashback = this.createSchemeInOut(scheme, inventoryItem);
-
 
317
					walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItemIdFofoOrderIdMap.get(inventoryItem.getId()), WalletReferenceType.SCHEME_IN, "Added for SCHEME_IN", cashback);
-
 
318
				}
-
 
319
			}
-
 
320
		}
-
 
321
	}
-
 
322
	
-
 
323
	private Map<Integer, FofoOrderItem> toFofoOrderItemIdFofoOrderItemMap(List<FofoOrderItem> fofoOrderItems){
-
 
324
		Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap = new HashMap<>();
-
 
325
		for(FofoOrderItem fofoOrderItem : fofoOrderItems){
-
 
326
			fofoOrderItemIdFofoOrderItemMap.put(fofoOrderItem.getId(), fofoOrderItem);
-
 
327
		}
-
 
328
		return fofoOrderItemIdFofoOrderItemMap;
-
 
329
	}
-
 
330
	
-
 
331
	private List<InventoryItem> toFilteredPurchaseCompletedInventoryItems(List<InventoryItem> inventoryItems, List<Integer> itemIds, boolean purchase){
-
 
332
		List<InventoryItem> completedInventoryItems = new ArrayList<>();
-
 
333
		for(InventoryItem inventoryItem : inventoryItems){
-
 
334
			if(purchase && inventoryItem.getPurchase().getCompleteTimestamp() != null && itemIds.contains(inventoryItem.getItemId())){
-
 
335
				completedInventoryItems.add(inventoryItem);
-
 
336
			}else if(itemIds.contains(inventoryItem.getItemId())){
-
 
337
				
-
 
338
			}
-
 
339
		}
-
 
340
		return completedInventoryItems;
-
 
341
	}
-
 
342
	
-
 
343
	//private List<InventoryItem> to
187
 
344
 
188
	@Override
345
	@Override
189
	public void expireSchemeById(int schemeId) throws ProfitMandiBusinessException {
346
	public void expireSchemeById(int schemeId) throws ProfitMandiBusinessException {
190
		Scheme scheme = schemeRepository.selectById(schemeId);
347
		Scheme scheme = schemeRepository.selectById(schemeId);
191
		if(scheme.getActiveTimestamp() == null){
348
		if(scheme.getActiveTimestamp() == null){
Line 196... Line 353...
196
		}
353
		}
197
		scheme.setExpireTimestamp(LocalDateTime.now());
354
		scheme.setExpireTimestamp(LocalDateTime.now());
198
		schemeRepository.persist(scheme);
355
		schemeRepository.persist(scheme);
199
	}
356
	}
200
	
357
	
201
	private Map<Integer, Scheme> toSchemeIdSchemesMap(List<Scheme> schemes){
358
	private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes){
202
		Map<Integer, Scheme> schemeIdSchemesMap = new HashMap<>();
359
		Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
203
		for(Scheme scheme : schemes){
360
		for(Scheme scheme : schemes){
204
			schemeIdSchemesMap.put(scheme.getId(), scheme);
361
			schemeIdSchemeMap.put(scheme.getId(), scheme);
205
		}
362
		}
206
		return schemeIdSchemesMap;
363
		return schemeIdSchemeMap;
207
	}
364
	}
208
	
365
	
209
	private Set<Integer> inventoryItemSchemesMapToSchemeIds(Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap){
366
	private Set<Integer> inventoryItemSchemesMapToSchemeIds(Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap){
210
		Set<Integer> schemeIds = new HashSet<>();
367
		Set<Integer> schemeIds = new HashSet<>();
211
		for(Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()){
368
		for(Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()){
Line 241... Line 398...
241
		}
398
		}
242
		return itemIds;
399
		return itemIds;
243
	}
400
	}
244
	
401
	
245
	private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes){
402
	private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes){
246
		Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemesMap(schemes);
403
		Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemeMap(schemes);
247
		Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
404
		Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
248
		for(SchemeItem schemeItem : schemeItems){
405
		for(SchemeItem schemeItem : schemeItems){
249
			if(!itemIdSchemesMap.containsKey(schemeItem.getItemId())){
406
			if(!itemIdSchemesMap.containsKey(schemeItem.getItemId())){
250
				Set<Scheme> schemesSet = new HashSet<>();
407
				Set<Scheme> schemesSet = new HashSet<>();
251
				schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
408
				schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));