| Line 15... |
Line 15... |
| 15 |
import org.springframework.beans.factory.annotation.Qualifier;
|
15 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 16 |
import org.springframework.stereotype.Component;
|
16 |
import org.springframework.stereotype.Component;
|
| 17 |
|
17 |
|
| 18 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
18 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
| 19 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
19 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 20 |
import com.spice.profitmandi.common.model.CreateSchemeRequest;
|
- |
|
| 21 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
20 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 22 |
import com.spice.profitmandi.common.model.SchemeModel;
|
21 |
import com.spice.profitmandi.common.model.SchemeModel;
|
| 23 |
import com.spice.profitmandi.common.util.StringUtils;
|
22 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 24 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
23 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 25 |
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
|
24 |
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
|
| 26 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
25 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
| 27 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
26 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 28 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
27 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| - |
|
28 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
| 29 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
29 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 30 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
30 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
| 31 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
31 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
| 32 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
32 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
| 33 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
33 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 34 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
34 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 35 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
35 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| - |
|
36 |
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
|
| 36 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
37 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 37 |
import com.spice.profitmandi.dao.repository.catalog.RetailerSchemeRepository;
|
- |
|
| 38 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
38 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 39 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
39 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 40 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
40 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 41 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
41 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
| 42 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
42 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
| 43 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
43 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| 44 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
44 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| - |
|
45 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
|
| 45 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
46 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 46 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
47 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
| 47 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
48 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
| 48 |
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
|
49 |
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
|
| 49 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
50 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| Line 59... |
Line 60... |
| 59 |
@Autowired
|
60 |
@Autowired
|
| 60 |
@Qualifier("fofoInventoryItemRepository")
|
61 |
@Qualifier("fofoInventoryItemRepository")
|
| 61 |
private InventoryItemRepository inventoryItemRepository;
|
62 |
private InventoryItemRepository inventoryItemRepository;
|
| 62 |
|
63 |
|
| 63 |
@Autowired
|
64 |
@Autowired
|
| - |
|
65 |
private PartnerTypeChangeService partnerTypeChangeService;
|
| - |
|
66 |
|
| - |
|
67 |
@Autowired
|
| 64 |
private ScanRecordRepository scanRecordRepository;
|
68 |
private ScanRecordRepository scanRecordRepository;
|
| 65 |
|
69 |
|
| 66 |
@Autowired
|
70 |
@Autowired
|
| 67 |
private SchemeRepository schemeRepository;
|
71 |
private SchemeRepository schemeRepository;
|
| 68 |
|
72 |
|
| Line 74... |
Line 78... |
| 74 |
|
78 |
|
| 75 |
@Autowired
|
79 |
@Autowired
|
| 76 |
private TagListingRepository tagListingRepository;
|
80 |
private TagListingRepository tagListingRepository;
|
| 77 |
|
81 |
|
| 78 |
@Autowired
|
82 |
@Autowired
|
| 79 |
private RetailerSchemeRepository retailerSchemeRepository;
|
- |
|
| 80 |
|
- |
|
| 81 |
@Autowired
|
- |
|
| 82 |
private SchemeInOutRepository schemeInOutRepository;
|
83 |
private SchemeInOutRepository schemeInOutRepository;
|
| 83 |
|
84 |
|
| 84 |
@Autowired
|
85 |
@Autowired
|
| 85 |
@Qualifier("catalogItemRepository")
|
86 |
@Qualifier("catalogItemRepository")
|
| 86 |
private ItemRepository itemRepository;
|
87 |
private ItemRepository itemRepository;
|
| Line 116... |
Line 117... |
| 116 |
scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
|
117 |
scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
|
| 117 |
}
|
118 |
}
|
| 118 |
|
119 |
|
| 119 |
this.validateItemIds(createSchemeRequest);
|
120 |
this.validateItemIds(createSchemeRequest);
|
| 120 |
schemeRepository.persist(scheme);
|
121 |
schemeRepository.persist(scheme);
|
| 121 |
if (!createSchemeRequest.isRetailerAll()) {
|
- |
|
| 122 |
this.validateRetailerIds(createSchemeRequest);
|
- |
|
| 123 |
for (int retailerId : createSchemeRequest.getRetailerIds()) {
|
- |
|
| 124 |
RetailerScheme retailerScheme = new RetailerScheme();
|
- |
|
| 125 |
retailerScheme.setRetailerId(retailerId);
|
- |
|
| 126 |
retailerScheme.setSchemeId(scheme.getId());
|
- |
|
| 127 |
retailerSchemeRepository.persist(retailerScheme);
|
- |
|
| 128 |
}
|
- |
|
| 129 |
}
|
- |
|
| 130 |
for (int itemId : createSchemeRequest.getItemIds()) {
|
122 |
for (int itemId : createSchemeRequest.getItemIds()) {
|
| 131 |
SchemeItem schemeItem = new SchemeItem();
|
123 |
SchemeItem schemeItem = new SchemeItem();
|
| 132 |
schemeItem.setSchemeId(scheme.getId());
|
124 |
schemeItem.setSchemeId(scheme.getId());
|
| 133 |
schemeItem.setItemId(itemId);
|
125 |
schemeItem.setItemId(itemId);
|
| 134 |
schemeItemRepository.persist(schemeItem);
|
126 |
schemeItemRepository.persist(schemeItem);
|
| Line 145... |
Line 137... |
| 145 |
if (createSchemeRequest.getAmount() <= 0) {
|
137 |
if (createSchemeRequest.getAmount() <= 0) {
|
| 146 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
138 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
| 147 |
"SCHM_VE_1001");
|
139 |
"SCHM_VE_1001");
|
| 148 |
}
|
140 |
}
|
| 149 |
|
141 |
|
| 150 |
if (AmountType.valueOf(createSchemeRequest.getAmountType()) == AmountType.PERCENTAGE
|
142 |
if (createSchemeRequest.getAmountType().equals(AmountType.PERCENTAGE)
|
| 151 |
&& createSchemeRequest.getAmount() > 100) {
|
143 |
&& createSchemeRequest.getAmount() > 100) {
|
| 152 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
144 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
| 153 |
"SCHM_VE_1002");
|
145 |
"SCHM_VE_1002");
|
| 154 |
}
|
146 |
}
|
| 155 |
|
147 |
|
| Line 165... |
Line 157... |
| 165 |
|
157 |
|
| 166 |
private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest) {
|
158 |
private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest) {
|
| 167 |
Scheme scheme = new Scheme();
|
159 |
Scheme scheme = new Scheme();
|
| 168 |
scheme.setName(createSchemeRequest.getName());
|
160 |
scheme.setName(createSchemeRequest.getName());
|
| 169 |
scheme.setDescription(createSchemeRequest.getDescription());
|
161 |
scheme.setDescription(createSchemeRequest.getDescription());
|
| 170 |
scheme.setType(SchemeType.valueOf(createSchemeRequest.getType()));
|
162 |
scheme.setType(createSchemeRequest.getType());
|
| 171 |
scheme.setAmountType(AmountType.valueOf(createSchemeRequest.getAmountType()));
|
163 |
scheme.setAmountType(createSchemeRequest.getAmountType());
|
| - |
|
164 |
scheme.setPartnerType(createSchemeRequest.getPartnerType());
|
| 172 |
scheme.setAmount(createSchemeRequest.getAmount());
|
165 |
scheme.setAmount(createSchemeRequest.getAmount());
|
| 173 |
scheme.setStartDateTime(createSchemeRequest.getStartDate());
|
166 |
scheme.setPartnerType(createSchemeRequest.getPartnerType());
|
| 174 |
scheme.setEndDateTime(createSchemeRequest.getEndDate());
|
167 |
scheme.setEndDateTime(createSchemeRequest.getEndDate());
|
| 175 |
scheme.setCreatedBy(creatorId);
|
168 |
scheme.setCreatedBy(creatorId);
|
| 176 |
scheme.setRetailerAll(createSchemeRequest.isRetailerAll());
|
- |
|
| 177 |
return scheme;
|
169 |
return scheme;
|
| 178 |
}
|
170 |
}
|
| 179 |
|
171 |
|
| 180 |
private void validateRetailerIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
- |
|
| 181 |
if (createSchemeRequest.getRetailerIds() == null || createSchemeRequest.getRetailerIds().isEmpty()) {
|
- |
|
| 182 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID,
|
- |
|
| 183 |
createSchemeRequest.getRetailerIds(), "SCHM_1001");
|
- |
|
| 184 |
}
|
- |
|
| 185 |
List<Integer> foundRetailerIds = retailerRepository.selectIdsByIds(createSchemeRequest.getRetailerIds());
|
- |
|
| 186 |
if (foundRetailerIds.size() != createSchemeRequest.getRetailerIds().size()) {
|
- |
|
| 187 |
createSchemeRequest.getRetailerIds().removeAll(foundRetailerIds);
|
- |
|
| 188 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID,
|
- |
|
| 189 |
createSchemeRequest.getRetailerIds(), "SCHM_1002");
|
- |
|
| 190 |
}
|
- |
|
| 191 |
}
|
- |
|
| 192 |
|
- |
|
| 193 |
private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
172 |
private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
| 194 |
if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
|
173 |
if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
|
| 195 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
174 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
| 196 |
"SCHM_1003");
|
175 |
"SCHM_1003");
|
| 197 |
}
|
176 |
}
|
| Line 205... |
Line 184... |
| 205 |
}
|
184 |
}
|
| 206 |
|
185 |
|
| 207 |
@Override
|
186 |
@Override
|
| 208 |
public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
187 |
public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
| 209 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
188 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 210 |
if (!scheme.isRetailerAll()) {
|
- |
|
| 211 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
- |
|
| 212 |
scheme.getRetailerIds().addAll(retailerIds);
|
- |
|
| 213 |
}
|
- |
|
| 214 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
189 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 215 |
if (itemIds.size() > 0) {
|
190 |
if (itemIds.size() > 0) {
|
| 216 |
List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
|
191 |
List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
|
| 217 |
scheme.setItemStringMap(this.toItemStringMap(items));
|
192 |
scheme.setItemStringMap(this.toItemStringMap(items));
|
| 218 |
}
|
193 |
}
|
| Line 262... |
Line 237... |
| 262 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
237 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
| 263 |
Set<Integer> itemIds = this.schemeItemsToItemIds(schemeItems);
|
238 |
Set<Integer> itemIds = this.schemeItemsToItemIds(schemeItems);
|
| 264 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
239 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
| 265 |
Map<Integer, String> itemStringMap = this.toItemStringMap(items);
|
240 |
Map<Integer, String> itemStringMap = this.toItemStringMap(items);
|
| 266 |
this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
|
241 |
this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
|
| 267 |
List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
- |
|
| 268 |
this.addRetailerIdsToSchemes(retailerSchemes, schemeIdSchemeMap);
|
- |
|
| 269 |
return this.toSchemeModels(schemeIdSchemeMap);
|
242 |
return this.toSchemeModels(schemeIdSchemeMap);
|
| 270 |
}
|
243 |
}
|
| 271 |
|
244 |
|
| 272 |
private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
|
245 |
private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
|
| 273 |
Map<Integer, String> itemStringMap) {
|
246 |
Map<Integer, String> itemStringMap) {
|
| Line 398... |
Line 371... |
| 398 |
|
371 |
|
| 399 |
@Override
|
372 |
@Override
|
| 400 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
373 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
| 401 |
LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
|
374 |
LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
|
| 402 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
375 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
| - |
|
376 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId,
|
| - |
|
377 |
purchase.getCreateTimestamp().toLocalDate());
|
| 403 |
LOGGER.info("purchase is completed = {}", (purchase.getCompleteTimestamp() != null));
|
378 |
LOGGER.info("purchase is completed = {}", (purchase.getCompleteTimestamp() != null));
|
| 404 |
if (purchase.getCompleteTimestamp() != null) {
|
379 |
if (purchase.getCompleteTimestamp() != null) {
|
| 405 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, purchase.getCompleteTimestamp());
|
380 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, purchase.getCompleteTimestamp());
|
| 406 |
float totalCashback = 0;
|
381 |
float totalCashback = 0;
|
| 407 |
if (schemes.isEmpty()) {
|
382 |
if (schemes.isEmpty()) {
|
| Line 421... |
Line 396... |
| 421 |
}
|
396 |
}
|
| 422 |
Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
|
397 |
Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
|
| 423 |
Set<Integer> itemIds = new HashSet<>();
|
398 |
Set<Integer> itemIds = new HashSet<>();
|
| 424 |
|
399 |
|
| 425 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
400 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
| 426 |
Set<Scheme> notAllSchemes = new HashSet<>();
|
- |
|
| 427 |
Set<Scheme> allSchemes = new HashSet<>();
|
401 |
Set<Scheme> allSchemes = new HashSet<>();
|
| 428 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
402 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
| 429 |
if (!scheme.isRetailerAll()) {
|
- |
|
| 430 |
notAllSchemes.add(scheme);
|
- |
|
| 431 |
} else {
|
- |
|
| 432 |
allSchemes.add(scheme);
|
403 |
allSchemes.add(scheme);
|
| 433 |
}
|
- |
|
| 434 |
}
|
404 |
}
|
| 435 |
allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
|
405 |
allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
|
| 436 |
itemIds.add(inventoryItemSchemesEntry.getKey().getItemId());
|
406 |
itemIds.add(inventoryItemSchemesEntry.getKey().getItemId());
|
| 437 |
}
|
407 |
}
|
| 438 |
|
408 |
|