| 22653 |
ashik.ali |
1 |
package com.spice.profitmandi.service.scheme;
|
|
|
2 |
|
| 22859 |
ashik.ali |
3 |
import java.time.LocalDateTime;
|
| 23019 |
ashik.ali |
4 |
import java.util.ArrayList;
|
| 22653 |
ashik.ali |
5 |
import java.util.HashMap;
|
| 22859 |
ashik.ali |
6 |
import java.util.HashSet;
|
| 22653 |
ashik.ali |
7 |
import java.util.List;
|
|
|
8 |
import java.util.Map;
|
|
|
9 |
import java.util.Set;
|
| 23968 |
amit.gupta |
10 |
import java.util.stream.Collectors;
|
| 22653 |
ashik.ali |
11 |
|
| 23781 |
ashik.ali |
12 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
13 |
import org.apache.logging.log4j.Logger;
|
| 22653 |
ashik.ali |
14 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 23781 |
ashik.ali |
15 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 22653 |
ashik.ali |
16 |
import org.springframework.stereotype.Component;
|
|
|
17 |
|
| 24307 |
amit.gupta |
18 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
| 22653 |
ashik.ali |
19 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 22859 |
ashik.ali |
20 |
import com.spice.profitmandi.common.model.CreateSchemeRequest;
|
| 22653 |
ashik.ali |
21 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 23019 |
ashik.ali |
22 |
import com.spice.profitmandi.common.model.SchemeModel;
|
| 22859 |
ashik.ali |
23 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 23339 |
ashik.ali |
24 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 22653 |
ashik.ali |
25 |
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
|
|
|
26 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
|
|
27 |
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
|
| 23365 |
ashik.ali |
28 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 22859 |
ashik.ali |
29 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
| 22653 |
ashik.ali |
30 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 23339 |
ashik.ali |
31 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 25043 |
amit.gupta |
32 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
| 22859 |
ashik.ali |
33 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
|
|
34 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
| 23527 |
ashik.ali |
35 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 22653 |
ashik.ali |
36 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 25111 |
amit.gupta |
37 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| 22859 |
ashik.ali |
38 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 22653 |
ashik.ali |
39 |
import com.spice.profitmandi.dao.repository.catalog.RetailerSchemeRepository;
|
|
|
40 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 23995 |
amit.gupta |
41 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 23968 |
amit.gupta |
42 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 23019 |
ashik.ali |
43 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
|
|
44 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
| 23365 |
ashik.ali |
45 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| 22653 |
ashik.ali |
46 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 23339 |
ashik.ali |
47 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 25043 |
amit.gupta |
48 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
| 22859 |
ashik.ali |
49 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
|
|
50 |
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
|
| 23798 |
amit.gupta |
51 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| 22859 |
ashik.ali |
52 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 22653 |
ashik.ali |
53 |
|
| 22859 |
ashik.ali |
54 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
55 |
|
| 22653 |
ashik.ali |
56 |
@Component
|
|
|
57 |
public class SchemeServiceImpl implements SchemeService {
|
|
|
58 |
|
| 23568 |
govind |
59 |
private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
|
| 23444 |
amit.gupta |
60 |
|
| 22653 |
ashik.ali |
61 |
@Autowired
|
| 23781 |
ashik.ali |
62 |
@Qualifier("fofoInventoryItemRepository")
|
| 22653 |
ashik.ali |
63 |
private InventoryItemRepository inventoryItemRepository;
|
| 23444 |
amit.gupta |
64 |
|
| 22653 |
ashik.ali |
65 |
@Autowired
|
| 25043 |
amit.gupta |
66 |
private ScanRecordRepository scanRecordRepository;
|
| 23444 |
amit.gupta |
67 |
|
| 22653 |
ashik.ali |
68 |
@Autowired
|
|
|
69 |
private SchemeRepository schemeRepository;
|
| 23444 |
amit.gupta |
70 |
|
| 22653 |
ashik.ali |
71 |
@Autowired
|
| 23798 |
amit.gupta |
72 |
private RoleManager roleManager;
|
|
|
73 |
|
|
|
74 |
@Autowired
|
| 22859 |
ashik.ali |
75 |
private RetailerRepository retailerRepository;
|
| 24562 |
amit.gupta |
76 |
|
| 23995 |
amit.gupta |
77 |
@Autowired
|
|
|
78 |
private TagListingRepository tagListingRepository;
|
| 23444 |
amit.gupta |
79 |
|
| 22859 |
ashik.ali |
80 |
@Autowired
|
| 22653 |
ashik.ali |
81 |
private RetailerSchemeRepository retailerSchemeRepository;
|
| 23444 |
amit.gupta |
82 |
|
| 22653 |
ashik.ali |
83 |
@Autowired
|
| 22859 |
ashik.ali |
84 |
private SchemeInOutRepository schemeInOutRepository;
|
| 23444 |
amit.gupta |
85 |
|
| 22653 |
ashik.ali |
86 |
@Autowired
|
| 23781 |
ashik.ali |
87 |
@Qualifier("catalogItemRepository")
|
| 22859 |
ashik.ali |
88 |
private ItemRepository itemRepository;
|
| 23444 |
amit.gupta |
89 |
|
| 22859 |
ashik.ali |
90 |
@Autowired
|
|
|
91 |
private SchemeItemRepository schemeItemRepository;
|
| 23444 |
amit.gupta |
92 |
|
| 22859 |
ashik.ali |
93 |
@Autowired
|
|
|
94 |
private WalletService walletService;
|
| 23444 |
amit.gupta |
95 |
|
| 23019 |
ashik.ali |
96 |
@Autowired
|
|
|
97 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
| 23444 |
amit.gupta |
98 |
|
| 23019 |
ashik.ali |
99 |
@Autowired
|
|
|
100 |
private FofoLineItemRepository fofoLineItemRepository;
|
| 23444 |
amit.gupta |
101 |
|
| 23339 |
ashik.ali |
102 |
@Autowired
|
|
|
103 |
private PurchaseRepository purchaseRepository;
|
| 23444 |
amit.gupta |
104 |
|
| 23344 |
ashik.ali |
105 |
@Autowired
|
| 23365 |
ashik.ali |
106 |
private FofoOrderRepository fofoOrderRepository;
|
| 23796 |
amit.gupta |
107 |
|
| 22653 |
ashik.ali |
108 |
@Override
|
| 22859 |
ashik.ali |
109 |
public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
| 23444 |
amit.gupta |
110 |
|
| 23019 |
ashik.ali |
111 |
this.validateCreateSchemeRequest(createSchemeRequest);
|
| 23444 |
amit.gupta |
112 |
|
| 22859 |
ashik.ali |
113 |
Scheme scheme = this.toScheme(creatorId, createSchemeRequest);
|
| 23444 |
amit.gupta |
114 |
|
|
|
115 |
if (scheme.getStartDateTime().isAfter(scheme.getEndDateTime())) {
|
|
|
116 |
throw new ProfitMandiBusinessException(
|
|
|
117 |
ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE,
|
|
|
118 |
scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
|
| 22653 |
ashik.ali |
119 |
}
|
| 23444 |
amit.gupta |
120 |
|
| 22859 |
ashik.ali |
121 |
this.validateItemIds(createSchemeRequest);
|
|
|
122 |
schemeRepository.persist(scheme);
|
| 23444 |
amit.gupta |
123 |
if (!createSchemeRequest.isRetailerAll()) {
|
| 22859 |
ashik.ali |
124 |
this.validateRetailerIds(createSchemeRequest);
|
| 23444 |
amit.gupta |
125 |
for (int retailerId : createSchemeRequest.getRetailerIds()) {
|
| 22859 |
ashik.ali |
126 |
RetailerScheme retailerScheme = new RetailerScheme();
|
|
|
127 |
retailerScheme.setRetailerId(retailerId);
|
|
|
128 |
retailerScheme.setSchemeId(scheme.getId());
|
|
|
129 |
retailerSchemeRepository.persist(retailerScheme);
|
|
|
130 |
}
|
| 22653 |
ashik.ali |
131 |
}
|
| 23444 |
amit.gupta |
132 |
for (int itemId : createSchemeRequest.getItemIds()) {
|
| 22859 |
ashik.ali |
133 |
SchemeItem schemeItem = new SchemeItem();
|
|
|
134 |
schemeItem.setSchemeId(scheme.getId());
|
|
|
135 |
schemeItem.setItemId(itemId);
|
|
|
136 |
schemeItemRepository.persist(schemeItem);
|
|
|
137 |
}
|
| 23444 |
amit.gupta |
138 |
|
| 22653 |
ashik.ali |
139 |
}
|
| 23444 |
amit.gupta |
140 |
|
|
|
141 |
private void validateCreateSchemeRequest(CreateSchemeRequest createSchemeRequest)
|
|
|
142 |
throws ProfitMandiBusinessException {
|
|
|
143 |
if (createSchemeRequest.getName() == null || createSchemeRequest.getName().isEmpty()) {
|
|
|
144 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, createSchemeRequest.getName(),
|
|
|
145 |
"SCHM_VE_1000");
|
| 23019 |
ashik.ali |
146 |
}
|
| 23444 |
amit.gupta |
147 |
if (createSchemeRequest.getAmount() <= 0) {
|
|
|
148 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
149 |
"SCHM_VE_1001");
|
| 23019 |
ashik.ali |
150 |
}
|
| 23444 |
amit.gupta |
151 |
|
| 23527 |
ashik.ali |
152 |
if (AmountType.valueOf(createSchemeRequest.getAmountType()) == AmountType.PERCENTAGE
|
| 23444 |
amit.gupta |
153 |
&& createSchemeRequest.getAmount() > 100) {
|
|
|
154 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
155 |
"SCHM_VE_1002");
|
| 23019 |
ashik.ali |
156 |
}
|
| 23444 |
amit.gupta |
157 |
|
| 23886 |
amit.gupta |
158 |
if (createSchemeRequest.getStartDate() == null) {
|
| 23983 |
amit.gupta |
159 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, createSchemeRequest.getStartDate(),
|
|
|
160 |
"SCHM_VE_1003");
|
| 23019 |
ashik.ali |
161 |
}
|
| 23886 |
amit.gupta |
162 |
if (createSchemeRequest.getEndDate() == null) {
|
| 23983 |
amit.gupta |
163 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, createSchemeRequest.getEndDate(),
|
|
|
164 |
"SCHM_VE_1004");
|
| 23019 |
ashik.ali |
165 |
}
|
|
|
166 |
}
|
| 23444 |
amit.gupta |
167 |
|
|
|
168 |
private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest) {
|
| 22859 |
ashik.ali |
169 |
Scheme scheme = new Scheme();
|
|
|
170 |
scheme.setName(createSchemeRequest.getName());
|
|
|
171 |
scheme.setDescription(createSchemeRequest.getDescription());
|
|
|
172 |
scheme.setType(SchemeType.valueOf(createSchemeRequest.getType()));
|
| 23527 |
ashik.ali |
173 |
scheme.setAmountType(AmountType.valueOf(createSchemeRequest.getAmountType()));
|
| 22859 |
ashik.ali |
174 |
scheme.setAmount(createSchemeRequest.getAmount());
|
| 23886 |
amit.gupta |
175 |
scheme.setStartDateTime(createSchemeRequest.getStartDate());
|
|
|
176 |
scheme.setEndDateTime(createSchemeRequest.getEndDate());
|
| 22859 |
ashik.ali |
177 |
scheme.setCreatedBy(creatorId);
|
|
|
178 |
scheme.setRetailerAll(createSchemeRequest.isRetailerAll());
|
|
|
179 |
return scheme;
|
|
|
180 |
}
|
| 23444 |
amit.gupta |
181 |
|
|
|
182 |
private void validateRetailerIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
|
|
183 |
if (createSchemeRequest.getRetailerIds() == null || createSchemeRequest.getRetailerIds().isEmpty()) {
|
|
|
184 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID,
|
|
|
185 |
createSchemeRequest.getRetailerIds(), "SCHM_1001");
|
| 22859 |
ashik.ali |
186 |
}
|
| 22870 |
ashik.ali |
187 |
List<Integer> foundRetailerIds = retailerRepository.selectIdsByIds(createSchemeRequest.getRetailerIds());
|
| 23444 |
amit.gupta |
188 |
if (foundRetailerIds.size() != createSchemeRequest.getRetailerIds().size()) {
|
| 22859 |
ashik.ali |
189 |
createSchemeRequest.getRetailerIds().removeAll(foundRetailerIds);
|
| 23444 |
amit.gupta |
190 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID,
|
|
|
191 |
createSchemeRequest.getRetailerIds(), "SCHM_1002");
|
| 22859 |
ashik.ali |
192 |
}
|
|
|
193 |
}
|
| 23444 |
amit.gupta |
194 |
|
|
|
195 |
private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
|
|
196 |
if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
|
|
|
197 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
198 |
"SCHM_1003");
|
| 22859 |
ashik.ali |
199 |
}
|
| 23444 |
amit.gupta |
200 |
List<Integer> foundItemIds = itemRepository.selectIdsByIdsAndType(createSchemeRequest.getItemIds(),
|
|
|
201 |
ItemType.SERIALIZED);
|
|
|
202 |
if (foundItemIds.size() != createSchemeRequest.getItemIds().size()) {
|
| 22859 |
ashik.ali |
203 |
createSchemeRequest.getItemIds().removeAll(foundItemIds);
|
| 23444 |
amit.gupta |
204 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
205 |
"SCHM_1004");
|
| 22859 |
ashik.ali |
206 |
}
|
|
|
207 |
}
|
| 23444 |
amit.gupta |
208 |
|
| 22859 |
ashik.ali |
209 |
@Override
|
|
|
210 |
public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
211 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 23444 |
amit.gupta |
212 |
if (!scheme.isRetailerAll()) {
|
| 23019 |
ashik.ali |
213 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
|
|
214 |
scheme.getRetailerIds().addAll(retailerIds);
|
| 22859 |
ashik.ali |
215 |
}
|
|
|
216 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23914 |
govind |
217 |
if (itemIds.size() > 0) {
|
|
|
218 |
List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
|
|
|
219 |
scheme.setItemStringMap(this.toItemStringMap(items));
|
| 23983 |
amit.gupta |
220 |
}
|
| 22859 |
ashik.ali |
221 |
return scheme;
|
|
|
222 |
}
|
| 23444 |
amit.gupta |
223 |
|
|
|
224 |
public Map<Integer, String> toItemStringMap(List<Item> items) {
|
| 23339 |
ashik.ali |
225 |
Map<Integer, String> itemMap = new HashMap<>();
|
| 23444 |
amit.gupta |
226 |
for (Item item : items) {
|
| 23339 |
ashik.ali |
227 |
itemMap.put(item.getId(), this.getItemString(item));
|
|
|
228 |
}
|
|
|
229 |
return itemMap;
|
|
|
230 |
}
|
| 23444 |
amit.gupta |
231 |
|
|
|
232 |
public String getItemString(Item item) {
|
| 23339 |
ashik.ali |
233 |
StringBuilder itemString = new StringBuilder();
|
| 23444 |
amit.gupta |
234 |
if (item.getBrand() != null && !item.getBrand().isEmpty()) {
|
| 23339 |
ashik.ali |
235 |
itemString.append(item.getBrand().trim());
|
|
|
236 |
}
|
|
|
237 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
238 |
if (item.getModelName() != null && !item.getModelName().isEmpty()) {
|
| 23339 |
ashik.ali |
239 |
itemString.append(item.getModelName().trim());
|
|
|
240 |
}
|
|
|
241 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
242 |
if (item.getModelNumber() != null && !item.getModelNumber().isEmpty()) {
|
| 23339 |
ashik.ali |
243 |
itemString.append(item.getModelNumber().trim());
|
|
|
244 |
}
|
|
|
245 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
246 |
if (item.getColor() != null && !item.getColor().isEmpty()) {
|
| 23339 |
ashik.ali |
247 |
itemString.append(item.getColor().trim());
|
|
|
248 |
}
|
|
|
249 |
return itemString.toString();
|
|
|
250 |
}
|
| 23444 |
amit.gupta |
251 |
|
|
|
252 |
private Set<Integer> schemeItemsToItemIds(List<SchemeItem> schemeItems) {
|
| 23339 |
ashik.ali |
253 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
254 |
for (SchemeItem schemeItem : schemeItems) {
|
| 23339 |
ashik.ali |
255 |
itemIds.add(schemeItem.getItemId());
|
|
|
256 |
}
|
|
|
257 |
return itemIds;
|
|
|
258 |
}
|
| 23444 |
amit.gupta |
259 |
|
| 23019 |
ashik.ali |
260 |
@Override
|
|
|
261 |
public List<SchemeModel> getAllSchemeModels(LocalDateTime startDateTime, LocalDateTime endDateTime) {
|
|
|
262 |
List<Scheme> schemes = schemeRepository.selectAllBetweenCreateTimestamp(startDateTime, endDateTime);
|
|
|
263 |
Map<Integer, Scheme> schemeIdSchemeMap = this.toSchemeIdSchemeMap(schemes);
|
|
|
264 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
| 23339 |
ashik.ali |
265 |
Set<Integer> itemIds = this.schemeItemsToItemIds(schemeItems);
|
|
|
266 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
|
|
267 |
Map<Integer, String> itemStringMap = this.toItemStringMap(items);
|
|
|
268 |
this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
|
| 23019 |
ashik.ali |
269 |
List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
|
|
270 |
this.addRetailerIdsToSchemes(retailerSchemes, schemeIdSchemeMap);
|
|
|
271 |
return this.toSchemeModels(schemeIdSchemeMap);
|
|
|
272 |
}
|
| 23444 |
amit.gupta |
273 |
|
|
|
274 |
private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
|
|
|
275 |
Map<Integer, String> itemStringMap) {
|
|
|
276 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
277 |
schemeIdSchemeMap.get(schemeItem.getSchemeId()).getItemStringMap().put(schemeItem.getItemId(),
|
|
|
278 |
itemStringMap.get(schemeItem.getItemId()));
|
| 23019 |
ashik.ali |
279 |
}
|
|
|
280 |
}
|
| 23444 |
amit.gupta |
281 |
|
|
|
282 |
private void addRetailerIdsToSchemes(List<RetailerScheme> retailerSchemes, Map<Integer, Scheme> schemeIdSchemeMap) {
|
|
|
283 |
for (RetailerScheme retailerScheme : retailerSchemes) {
|
|
|
284 |
schemeIdSchemeMap.get(retailerScheme.getSchemeId()).getRetailerIds().add(retailerScheme.getRetailerId());
|
| 23019 |
ashik.ali |
285 |
}
|
|
|
286 |
}
|
| 23444 |
amit.gupta |
287 |
|
|
|
288 |
private List<SchemeModel> toSchemeModels(Map<Integer, Scheme> schemeIdSchemeMap) {
|
| 23019 |
ashik.ali |
289 |
List<SchemeModel> schemeModels = new ArrayList<>();
|
| 23444 |
amit.gupta |
290 |
for (Map.Entry<Integer, Scheme> schemeIdSchemeEntry : schemeIdSchemeMap.entrySet()) {
|
| 23019 |
ashik.ali |
291 |
schemeModels.add(this.toSchemeModel(schemeIdSchemeEntry.getValue()));
|
|
|
292 |
}
|
|
|
293 |
return schemeModels;
|
|
|
294 |
}
|
| 23444 |
amit.gupta |
295 |
|
|
|
296 |
private SchemeModel toSchemeModel(Scheme scheme) {
|
| 23019 |
ashik.ali |
297 |
SchemeModel schemeModel = new SchemeModel();
|
|
|
298 |
schemeModel.setSchemeId(scheme.getId());
|
|
|
299 |
schemeModel.setName(scheme.getName());
|
|
|
300 |
schemeModel.setDescription(scheme.getDescription());
|
|
|
301 |
schemeModel.setSchemeType(scheme.getType().toString());
|
|
|
302 |
schemeModel.setAmountType(scheme.getAmountType().toString());
|
|
|
303 |
schemeModel.setAmount(scheme.getAmount());
|
|
|
304 |
schemeModel.setStartDateTime(StringUtils.toString(scheme.getStartDateTime()));
|
|
|
305 |
schemeModel.setEndDateTime(StringUtils.toString(scheme.getEndDateTime()));
|
|
|
306 |
schemeModel.setCreateTimestamp(StringUtils.toString(scheme.getCreateTimestamp()));
|
|
|
307 |
schemeModel.setActiveTimestamp(StringUtils.toString(scheme.getActiveTimestamp()));
|
|
|
308 |
schemeModel.setExpireTimestamp(StringUtils.toString(scheme.getExpireTimestamp()));
|
|
|
309 |
schemeModel.setCreatedBy(scheme.getCreatedBy());
|
| 23339 |
ashik.ali |
310 |
schemeModel.setItemStringMap(scheme.getItemStringMap());
|
| 23019 |
ashik.ali |
311 |
schemeModel.setRetailerIds(scheme.getRetailerIds());
|
|
|
312 |
return schemeModel;
|
|
|
313 |
}
|
| 22653 |
ashik.ali |
314 |
|
|
|
315 |
@Override
|
|
|
316 |
public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
317 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 23444 |
amit.gupta |
318 |
if (scheme.getActiveTimestamp() != null) {
|
|
|
319 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
320 |
"SCHM_1005");
|
| 22653 |
ashik.ali |
321 |
}
|
| 23444 |
amit.gupta |
322 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
323 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
324 |
"SCHM_1006");
|
| 22653 |
ashik.ali |
325 |
}
|
| 22859 |
ashik.ali |
326 |
scheme.setActiveTimestamp(LocalDateTime.now());
|
|
|
327 |
schemeRepository.persist(scheme);
|
| 23444 |
amit.gupta |
328 |
if (scheme.getType() == SchemeType.IN) {
|
| 23019 |
ashik.ali |
329 |
this.processPreviousPurchases(scheme);
|
| 23444 |
amit.gupta |
330 |
} else if (scheme.getType() == SchemeType.OUT) {
|
| 23019 |
ashik.ali |
331 |
this.processPreviousSales(scheme);
|
|
|
332 |
}
|
| 22653 |
ashik.ali |
333 |
}
|
| 23444 |
amit.gupta |
334 |
|
|
|
335 |
private void processPreviousPurchases(Scheme scheme) throws ProfitMandiBusinessException {
|
|
|
336 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectBetweenDates(scheme.getStartDateTime(),
|
|
|
337 |
scheme.getEndDateTime());
|
|
|
338 |
if (inventoryItems.isEmpty()) {
|
| 23370 |
ashik.ali |
339 |
return;
|
|
|
340 |
}
|
| 23019 |
ashik.ali |
341 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
342 |
if (itemIds.isEmpty()) {
|
| 23370 |
ashik.ali |
343 |
return;
|
|
|
344 |
}
|
| 23339 |
ashik.ali |
345 |
List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
|
|
|
346 |
Map<Integer, String> itemStringMap = this.toItemStringMap(items);
|
| 23444 |
amit.gupta |
347 |
List<InventoryItem> completedInventoryItems = this.toFilteredPurchaseCompletedInventoryItems(inventoryItems,
|
|
|
348 |
itemIds, true);
|
|
|
349 |
if (completedInventoryItems.isEmpty()) {
|
| 23370 |
ashik.ali |
350 |
return;
|
|
|
351 |
}
|
| 23444 |
amit.gupta |
352 |
if (scheme.isRetailerAll()) {
|
|
|
353 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
| 23019 |
ashik.ali |
354 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 24562 |
amit.gupta |
355 |
if (cashback > 0) {
|
|
|
356 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
|
|
357 |
WalletReferenceType.SCHEME_IN,
|
|
|
358 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId())
|
|
|
359 |
+ " against invoiceNumber " + inventoryItem.getPurchase().getPurchaseReference(),
|
|
|
360 |
cashback);
|
|
|
361 |
}
|
| 23019 |
ashik.ali |
362 |
}
|
| 23444 |
amit.gupta |
363 |
} else {
|
| 23019 |
ashik.ali |
364 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
365 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
|
|
366 |
if (retailerIds.contains(inventoryItem.getFofoId())) {
|
| 23019 |
ashik.ali |
367 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 24562 |
amit.gupta |
368 |
if (cashback > 0) {
|
|
|
369 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
|
|
370 |
WalletReferenceType.SCHEME_IN,
|
|
|
371 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId())
|
|
|
372 |
+ " against invoiceNumber "
|
|
|
373 |
+ inventoryItem.getPurchase().getPurchaseReference(),
|
|
|
374 |
cashback);
|
|
|
375 |
}
|
| 23019 |
ashik.ali |
376 |
}
|
|
|
377 |
}
|
|
|
378 |
}
|
| 23444 |
amit.gupta |
379 |
|
| 23019 |
ashik.ali |
380 |
}
|
| 23444 |
amit.gupta |
381 |
|
|
|
382 |
private Map<Integer, Integer> toInventoryItemIdFofoOrderIdMap(List<FofoLineItem> fofoLineItems,
|
|
|
383 |
Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap) {
|
| 23019 |
ashik.ali |
384 |
Map<Integer, Integer> inventoryItemIdFofoOrderIdMap = new HashMap<>();
|
| 23444 |
amit.gupta |
385 |
|
|
|
386 |
for (FofoLineItem fofoLineItem : fofoLineItems) {
|
|
|
387 |
inventoryItemIdFofoOrderIdMap.put(fofoLineItem.getInventoryItemId(),
|
| 23019 |
ashik.ali |
388 |
fofoOrderItemIdFofoOrderItemMap.get(fofoLineItem.getFofoOrderItemId()).getOrderId());
|
|
|
389 |
}
|
|
|
390 |
return inventoryItemIdFofoOrderIdMap;
|
|
|
391 |
}
|
| 23444 |
amit.gupta |
392 |
|
|
|
393 |
private void processPreviousSales(Scheme scheme) throws ProfitMandiBusinessException {
|
|
|
394 |
List<FofoOrderItem> fofoOrderItems = fofoOrderItemRepository.selectBetweenDates(scheme.getStartDateTime(),
|
|
|
395 |
scheme.getEndDateTime());
|
|
|
396 |
|
|
|
397 |
if (fofoOrderItems.isEmpty()) {
|
| 23370 |
ashik.ali |
398 |
return;
|
|
|
399 |
}
|
| 22653 |
ashik.ali |
400 |
|
| 23444 |
amit.gupta |
401 |
Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap = this
|
|
|
402 |
.toFofoOrderItemIdFofoOrderItemMap(fofoOrderItems);
|
|
|
403 |
|
|
|
404 |
List<FofoLineItem> fofoLineItems = fofoLineItemRepository
|
|
|
405 |
.selectByFofoOrderItemIds(fofoOrderItemIdFofoOrderItemMap.keySet());
|
|
|
406 |
|
|
|
407 |
if (fofoLineItems.isEmpty()) {
|
| 23370 |
ashik.ali |
408 |
return;
|
|
|
409 |
}
|
| 23444 |
amit.gupta |
410 |
|
|
|
411 |
Map<Integer, Integer> inventoryItemIdFofoOrderIdMap = this.toInventoryItemIdFofoOrderIdMap(fofoLineItems,
|
|
|
412 |
fofoOrderItemIdFofoOrderItemMap);
|
|
|
413 |
|
|
|
414 |
List<InventoryItem> inventoryItems = inventoryItemRepository
|
|
|
415 |
.selectByIds(inventoryItemIdFofoOrderIdMap.keySet());
|
|
|
416 |
|
|
|
417 |
if (inventoryItems.isEmpty()) {
|
| 23370 |
ashik.ali |
418 |
return;
|
|
|
419 |
}
|
| 23444 |
amit.gupta |
420 |
|
| 23019 |
ashik.ali |
421 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
422 |
|
|
|
423 |
if (itemIds.isEmpty()) {
|
| 23370 |
ashik.ali |
424 |
return;
|
|
|
425 |
}
|
| 23444 |
amit.gupta |
426 |
|
|
|
427 |
List<InventoryItem> completedInventoryItems = this.toFilteredPurchaseCompletedInventoryItems(inventoryItems,
|
|
|
428 |
itemIds, false);
|
|
|
429 |
|
|
|
430 |
if (scheme.isRetailerAll()) {
|
|
|
431 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
| 23019 |
ashik.ali |
432 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 24562 |
amit.gupta |
433 |
if (cashback > 0) {
|
|
|
434 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
|
|
435 |
WalletReferenceType.SCHEME_OUT, "Added for SCHEME_OUT", cashback);
|
|
|
436 |
}
|
| 23019 |
ashik.ali |
437 |
}
|
| 23444 |
amit.gupta |
438 |
} else {
|
| 23019 |
ashik.ali |
439 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
440 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
|
|
441 |
if (retailerIds.contains(inventoryItem.getFofoId())) {
|
| 23019 |
ashik.ali |
442 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 24562 |
amit.gupta |
443 |
if (cashback > 0) {
|
|
|
444 |
walletService.addAmountToWallet(inventoryItem.getFofoId(),
|
|
|
445 |
inventoryItemIdFofoOrderIdMap.get(inventoryItem.getId()),
|
|
|
446 |
WalletReferenceType.SCHEME_OUT, "Added for SCHEME_OUT", cashback);
|
|
|
447 |
}
|
| 23019 |
ashik.ali |
448 |
}
|
|
|
449 |
}
|
|
|
450 |
}
|
|
|
451 |
}
|
| 23444 |
amit.gupta |
452 |
|
|
|
453 |
private Map<Integer, FofoOrderItem> toFofoOrderItemIdFofoOrderItemMap(List<FofoOrderItem> fofoOrderItems) {
|
| 23019 |
ashik.ali |
454 |
Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap = new HashMap<>();
|
| 23444 |
amit.gupta |
455 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
| 23019 |
ashik.ali |
456 |
fofoOrderItemIdFofoOrderItemMap.put(fofoOrderItem.getId(), fofoOrderItem);
|
|
|
457 |
}
|
|
|
458 |
return fofoOrderItemIdFofoOrderItemMap;
|
|
|
459 |
}
|
| 23444 |
amit.gupta |
460 |
|
|
|
461 |
private List<InventoryItem> toFilteredPurchaseCompletedInventoryItems(List<InventoryItem> inventoryItems,
|
|
|
462 |
List<Integer> itemIds, boolean purchase) {
|
| 23019 |
ashik.ali |
463 |
List<InventoryItem> completedInventoryItems = new ArrayList<>();
|
| 23444 |
amit.gupta |
464 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
465 |
if (purchase && inventoryItem.getPurchase().getCompleteTimestamp() != null
|
|
|
466 |
&& itemIds.contains(inventoryItem.getItemId())) {
|
| 23019 |
ashik.ali |
467 |
completedInventoryItems.add(inventoryItem);
|
| 23444 |
amit.gupta |
468 |
} else if (!purchase && itemIds.contains(inventoryItem.getItemId())) {
|
| 23297 |
ashik.ali |
469 |
completedInventoryItems.add(inventoryItem);
|
| 23019 |
ashik.ali |
470 |
}
|
|
|
471 |
}
|
|
|
472 |
return completedInventoryItems;
|
|
|
473 |
}
|
| 23444 |
amit.gupta |
474 |
|
| 22653 |
ashik.ali |
475 |
@Override
|
| 25069 |
amit.gupta |
476 |
public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
|
| 22653 |
ashik.ali |
477 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 25111 |
amit.gupta |
478 |
if (scheme == null || scheme.getActiveTimestamp() == null) {
|
| 23444 |
amit.gupta |
479 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
480 |
"SCHM_1007");
|
| 22653 |
ashik.ali |
481 |
}
|
| 23444 |
amit.gupta |
482 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
483 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
484 |
"SCHM_1008");
|
| 22653 |
ashik.ali |
485 |
}
|
| 22859 |
ashik.ali |
486 |
scheme.setExpireTimestamp(LocalDateTime.now());
|
| 25069 |
amit.gupta |
487 |
scheme.setEndDateTime(expiryTime);
|
| 22859 |
ashik.ali |
488 |
schemeRepository.persist(scheme);
|
| 22653 |
ashik.ali |
489 |
}
|
| 23444 |
amit.gupta |
490 |
|
|
|
491 |
private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
492 |
Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
|
| 23444 |
amit.gupta |
493 |
for (Scheme scheme : schemes) {
|
| 23019 |
ashik.ali |
494 |
schemeIdSchemeMap.put(scheme.getId(), scheme);
|
| 22859 |
ashik.ali |
495 |
}
|
| 23019 |
ashik.ali |
496 |
return schemeIdSchemeMap;
|
| 22859 |
ashik.ali |
497 |
}
|
| 23444 |
amit.gupta |
498 |
|
|
|
499 |
private Set<Integer> inventoryItemSchemesMapToSchemeIds(Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap) {
|
| 22859 |
ashik.ali |
500 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
501 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
|
|
502 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
| 22859 |
ashik.ali |
503 |
schemeIds.add(scheme.getId());
|
|
|
504 |
}
|
|
|
505 |
}
|
|
|
506 |
return schemeIds;
|
|
|
507 |
}
|
| 23444 |
amit.gupta |
508 |
|
|
|
509 |
private Set<Integer> inventoryItemsToItemIds(List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
510 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
511 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 22859 |
ashik.ali |
512 |
itemIds.add(inventoryItem.getItemId());
|
|
|
513 |
}
|
|
|
514 |
return itemIds;
|
|
|
515 |
}
|
| 23444 |
amit.gupta |
516 |
|
|
|
517 |
private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
518 |
Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemeMap(schemes);
|
| 22859 |
ashik.ali |
519 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
|
| 23444 |
amit.gupta |
520 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
521 |
if (!itemIdSchemesMap.containsKey(schemeItem.getItemId())) {
|
| 22859 |
ashik.ali |
522 |
Set<Scheme> schemesSet = new HashSet<>();
|
|
|
523 |
schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
524 |
itemIdSchemesMap.put(schemeItem.getItemId(), schemesSet);
|
| 23444 |
amit.gupta |
525 |
} else {
|
| 22859 |
ashik.ali |
526 |
itemIdSchemesMap.get(schemeItem.getItemId()).add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
527 |
}
|
|
|
528 |
}
|
|
|
529 |
return itemIdSchemesMap;
|
|
|
530 |
}
|
| 22653 |
ashik.ali |
531 |
|
| 23444 |
amit.gupta |
532 |
private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
|
|
|
533 |
List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
534 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
535 |
for (Scheme scheme : schemes) {
|
| 22859 |
ashik.ali |
536 |
schemeIds.add(scheme.getId());
|
|
|
537 |
}
|
|
|
538 |
Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
|
|
|
539 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
|
| 23444 |
amit.gupta |
540 |
|
| 22859 |
ashik.ali |
541 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
|
|
|
542 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemsMap = new HashMap<>();
|
| 23444 |
amit.gupta |
543 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
544 |
if (itemIdSchemesMap.containsKey(inventoryItem.getItemId())) {
|
| 22859 |
ashik.ali |
545 |
inventoryItemSchemsMap.put(inventoryItem, itemIdSchemesMap.get(inventoryItem.getItemId()));
|
|
|
546 |
}
|
|
|
547 |
}
|
|
|
548 |
return inventoryItemSchemsMap;
|
|
|
549 |
}
|
| 23444 |
amit.gupta |
550 |
|
| 22859 |
ashik.ali |
551 |
@Override
|
| 23365 |
ashik.ali |
552 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
| 23369 |
ashik.ali |
553 |
LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
|
| 23344 |
ashik.ali |
554 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
| 23369 |
ashik.ali |
555 |
LOGGER.info("purchase is completed = {}", (purchase.getCompleteTimestamp() != null));
|
| 23444 |
amit.gupta |
556 |
if (purchase.getCompleteTimestamp() != null) {
|
| 25043 |
amit.gupta |
557 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, purchase.getCompleteTimestamp());
|
| 23365 |
ashik.ali |
558 |
float totalCashback = 0;
|
| 23444 |
amit.gupta |
559 |
if (schemes.isEmpty()) {
|
| 23365 |
ashik.ali |
560 |
return;
|
|
|
561 |
}
|
|
|
562 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
|
| 25111 |
amit.gupta |
563 |
inventoryItems.stream().filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET_BAD)
|
|
|
564 |
&& !x.getLastScanType().equals(ScanType.PURCHASE_RET));
|
| 23444 |
amit.gupta |
565 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap = this.toInventoryItemSchemesMap(schemes,
|
|
|
566 |
inventoryItems);
|
|
|
567 |
|
|
|
568 |
if (inventoryItemSchemesMap.isEmpty()) {
|
| 23365 |
ashik.ali |
569 |
return;
|
|
|
570 |
}
|
|
|
571 |
Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
|
|
|
572 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
573 |
|
|
|
574 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
| 23365 |
ashik.ali |
575 |
Set<Scheme> notAllSchemes = new HashSet<>();
|
|
|
576 |
Set<Scheme> allSchemes = new HashSet<>();
|
| 23444 |
amit.gupta |
577 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
|
|
578 |
if (!scheme.isRetailerAll()) {
|
| 23365 |
ashik.ali |
579 |
notAllSchemes.add(scheme);
|
| 23444 |
amit.gupta |
580 |
} else {
|
| 23365 |
ashik.ali |
581 |
allSchemes.add(scheme);
|
|
|
582 |
}
|
| 22859 |
ashik.ali |
583 |
}
|
| 23365 |
ashik.ali |
584 |
allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
|
|
|
585 |
itemIds.add(inventoryItemSchemesEntry.getKey().getItemId());
|
| 22859 |
ashik.ali |
586 |
}
|
| 23444 |
amit.gupta |
587 |
|
|
|
588 |
int itemsCount = 0;
|
|
|
589 |
for (Map.Entry<InventoryItem, Set<Scheme>> allInventoryItemSchemesEntry : allInventoryItemSchemesMap
|
|
|
590 |
.entrySet()) {
|
|
|
591 |
float inventoryItemCashback = 0;
|
|
|
592 |
for (Scheme scheme : allInventoryItemSchemesEntry.getValue()) {
|
| 23365 |
ashik.ali |
593 |
InventoryItem inventoryItem = allInventoryItemSchemesEntry.getKey();
|
|
|
594 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 23444 |
amit.gupta |
595 |
inventoryItemCashback += cashback;
|
| 23339 |
ashik.ali |
596 |
}
|
| 23444 |
amit.gupta |
597 |
if (inventoryItemCashback > 0) {
|
|
|
598 |
totalCashback += inventoryItemCashback;
|
|
|
599 |
itemsCount++;
|
|
|
600 |
}
|
| 22859 |
ashik.ali |
601 |
}
|
| 23444 |
amit.gupta |
602 |
|
| 23511 |
amit.gupta |
603 |
LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
|
| 23796 |
amit.gupta |
604 |
if (itemsCount > 0) {
|
| 23508 |
amit.gupta |
605 |
walletService.addAmountToWallet(retailerId, purchaseId, WalletReferenceType.SCHEME_IN,
|
| 23796 |
amit.gupta |
606 |
"Added for SCHEME IN against invoice " + purchase.getPurchaseReference() + " (total "
|
|
|
607 |
+ itemsCount + " pcs)",
|
|
|
608 |
totalCashback);
|
|
|
609 |
LOGGER.info("Added Rs.{} for SCHEME IN against invoice {} total pcs({}) {}", totalCashback,
|
|
|
610 |
purchase.getPurchaseReference(), itemsCount);
|
| 23508 |
amit.gupta |
611 |
purchase.setCashback(purchase.getCashback() + totalCashback);
|
|
|
612 |
purchaseRepository.persist(purchase);
|
|
|
613 |
}
|
| 22653 |
ashik.ali |
614 |
}
|
|
|
615 |
}
|
| 23444 |
amit.gupta |
616 |
|
|
|
617 |
private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem) {
|
| 24562 |
amit.gupta |
618 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByScheme(scheme.getId(), inventoryItem.getId());
|
| 23443 |
amit.gupta |
619 |
float amount = 0;
|
| 24581 |
amit.gupta |
620 |
if (schemeInOuts.stream().filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList())
|
| 24562 |
amit.gupta |
621 |
.size() == 0) {
|
|
|
622 |
SchemeInOut schemeInOut = new SchemeInOut();
|
| 23443 |
amit.gupta |
623 |
amount = this.getAmount(inventoryItem, scheme);
|
|
|
624 |
schemeInOut.setSchemeId(scheme.getId());
|
|
|
625 |
schemeInOut.setInventoryItemId(inventoryItem.getId());
|
|
|
626 |
schemeInOut.setAmount(amount);
|
|
|
627 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
628 |
}
|
| 22859 |
ashik.ali |
629 |
return amount;
|
|
|
630 |
}
|
| 23444 |
amit.gupta |
631 |
|
| 25049 |
amit.gupta |
632 |
// We are maintaining price drop after grn
|
| 23444 |
amit.gupta |
633 |
private float getAmount(InventoryItem inventoryItem, Scheme scheme) {
|
| 22653 |
ashik.ali |
634 |
float amount = 0;
|
| 25049 |
amit.gupta |
635 |
float dpForCalc = 0;
|
|
|
636 |
float taxableSellingPrice = 0;
|
| 22859 |
ashik.ali |
637 |
float totalTaxRate = inventoryItem.getIgstRate() + inventoryItem.getSgstRate() + inventoryItem.getCgstRate();
|
| 23527 |
ashik.ali |
638 |
if (scheme.getAmountType() == AmountType.PERCENTAGE) {
|
| 24562 |
amit.gupta |
639 |
if (scheme.getType().equals(SchemeType.IN)) {
|
| 25049 |
amit.gupta |
640 |
dpForCalc = inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount();
|
| 23995 |
amit.gupta |
641 |
} else {
|
|
|
642 |
try {
|
| 25049 |
amit.gupta |
643 |
dpForCalc = tagListingRepository.selectByItemId(inventoryItem.getItemId()).getSellingPrice();
|
| 24562 |
amit.gupta |
644 |
} catch (Exception e) {
|
| 23995 |
amit.gupta |
645 |
LOGGER.info("Could not find tag Listing entry in {}", inventoryItem.getItemId());
|
|
|
646 |
e.printStackTrace();
|
|
|
647 |
}
|
|
|
648 |
}
|
| 25049 |
amit.gupta |
649 |
taxableSellingPrice = dpForCalc / (1 + totalTaxRate / 100);
|
| 22859 |
ashik.ali |
650 |
amount = taxableSellingPrice * scheme.getAmount() / 100;
|
| 25054 |
amit.gupta |
651 |
System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%s\t%f\t%f\t%f\t%f", inventoryItem.getId(),
|
| 25050 |
amit.gupta |
652 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
| 25054 |
amit.gupta |
653 |
scheme.getType(), scheme.getAmountType(), dpForCalc, taxableSellingPrice, scheme.getAmount(),
|
|
|
654 |
amount));
|
| 23444 |
amit.gupta |
655 |
} else {
|
| 22653 |
ashik.ali |
656 |
amount = scheme.getAmount();
|
| 25055 |
amit.gupta |
657 |
System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%s\t%f\t%f\t%d\t%f", inventoryItem.getId(),
|
| 25050 |
amit.gupta |
658 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
| 25054 |
amit.gupta |
659 |
scheme.getType(), scheme.getAmountType(), dpForCalc, taxableSellingPrice, 0, amount));
|
| 22653 |
ashik.ali |
660 |
}
|
| 25049 |
amit.gupta |
661 |
|
| 22859 |
ashik.ali |
662 |
return amount;
|
| 22653 |
ashik.ali |
663 |
}
|
| 23444 |
amit.gupta |
664 |
|
| 22653 |
ashik.ali |
665 |
@Override
|
| 23365 |
ashik.ali |
666 |
public void processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
|
|
|
667 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(retailerId, fofoOrderId);
|
| 23444 |
amit.gupta |
668 |
|
| 25043 |
amit.gupta |
669 |
List<ScanRecord> scanRecords = scanRecordRepository.selectAllByOrderId(fofoOrderId);
|
|
|
670 |
Set<Integer> inventoryItemIds = scanRecords.stream().map(x -> x.getInventoryItemId())
|
|
|
671 |
.collect(Collectors.toSet());
|
|
|
672 |
Set<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIds).stream()
|
|
|
673 |
.filter(x -> x.getSerialNumber() != null && !x.getSerialNumber().equals(""))
|
|
|
674 |
.collect(Collectors.toSet());
|
| 23444 |
amit.gupta |
675 |
|
| 25043 |
amit.gupta |
676 |
if (inventoryItems.size() == 0) {
|
| 23365 |
ashik.ali |
677 |
return;
|
| 22859 |
ashik.ali |
678 |
}
|
| 23444 |
amit.gupta |
679 |
|
| 25043 |
amit.gupta |
680 |
float totalCashback = 0;
|
|
|
681 |
int count = 0;
|
| 23444 |
amit.gupta |
682 |
|
| 25051 |
amit.gupta |
683 |
Set<Scheme> allFixedSchemes = schemeRepository.selectActiveAll(SchemeType.OUT, fofoOrder.getCreateTimestamp())
|
| 25054 |
amit.gupta |
684 |
.stream().filter(scheme -> scheme.getAmountType().equals(AmountType.FIXED)).collect(Collectors.toSet());
|
| 25050 |
amit.gupta |
685 |
|
| 25043 |
amit.gupta |
686 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
687 |
int itemCashback = 0;
|
|
|
688 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(inventoryItem.getPurchaseId(), retailerId);
|
|
|
689 |
Set<Integer> schemeIds = new HashSet<>(
|
|
|
690 |
schemeItemRepository.selectSchemeIdByItemId(inventoryItem.getItemId()));
|
| 25051 |
amit.gupta |
691 |
Set<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.OUT, purchase.getCompleteTimestamp())
|
| 25054 |
amit.gupta |
692 |
.stream().filter(x -> x.getAmountType().equals(AmountType.PERCENTAGE)).collect(Collectors.toSet());
|
| 25050 |
amit.gupta |
693 |
schemes.addAll(allFixedSchemes);
|
| 25054 |
amit.gupta |
694 |
schemes = schemes.stream().filter(x -> schemeIds.contains(x.getId())).collect(Collectors.toSet());
|
| 25043 |
amit.gupta |
695 |
for (Scheme scheme : schemes) {
|
|
|
696 |
itemCashback += this.createSchemeInOut(scheme, inventoryItem);
|
| 22653 |
ashik.ali |
697 |
}
|
| 25043 |
amit.gupta |
698 |
if (itemCashback > 0) {
|
|
|
699 |
count++;
|
|
|
700 |
totalCashback += itemCashback;
|
| 22859 |
ashik.ali |
701 |
}
|
| 22653 |
ashik.ali |
702 |
}
|
| 25043 |
amit.gupta |
703 |
if (count > 0) {
|
| 23444 |
amit.gupta |
704 |
walletService.addAmountToWallet(retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT,
|
| 23796 |
amit.gupta |
705 |
"Sales margin for invoice number " + fofoOrder.getInvoiceNumber() + ". Total " + count + " pc(s)",
|
|
|
706 |
totalCashback);
|
| 23365 |
ashik.ali |
707 |
fofoOrder.setCashback(totalCashback);
|
|
|
708 |
fofoOrderRepository.persist(fofoOrder);
|
| 22859 |
ashik.ali |
709 |
}
|
| 22653 |
ashik.ali |
710 |
}
|
| 23444 |
amit.gupta |
711 |
|
| 23508 |
amit.gupta |
712 |
@Override
|
| 23796 |
amit.gupta |
713 |
public void rollbackSchemes(List<Integer> inventoryItemIds, int rollbackReference, String rollbackReason)
|
|
|
714 |
throws Exception {
|
| 23638 |
amit.gupta |
715 |
Set<Integer> inventoryItemIdSet = new HashSet<>(inventoryItemIds);
|
| 23508 |
amit.gupta |
716 |
float amountToRollback = 0;
|
|
|
717 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIdSet);
|
| 23796 |
amit.gupta |
718 |
for (SchemeInOut schemeInOut : schemes) {
|
| 23983 |
amit.gupta |
719 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
720 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
721 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
722 |
amountToRollback += schemeInOut.getAmount();
|
|
|
723 |
}
|
| 23508 |
amit.gupta |
724 |
}
|
| 23983 |
amit.gupta |
725 |
if (amountToRollback > 0) {
|
| 23638 |
amit.gupta |
726 |
int inventoryItemId = inventoryItemIds.get(0);
|
|
|
727 |
Integer fofoId = inventoryItemRepository.selectById(inventoryItemId).getFofoId();
|
| 23796 |
amit.gupta |
728 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, rollbackReference,
|
|
|
729 |
WalletReferenceType.SCHEME_IN, rollbackReason);
|
| 23638 |
amit.gupta |
730 |
}
|
| 23508 |
amit.gupta |
731 |
}
|
| 23884 |
amit.gupta |
732 |
|
| 23781 |
ashik.ali |
733 |
@Override
|
| 23796 |
amit.gupta |
734 |
public Map<String, Object> getSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
735 |
throws ProfitMandiBusinessException {
|
| 23781 |
ashik.ali |
736 |
Map<String, Object> map = new HashMap<>();
|
|
|
737 |
List<Scheme> schemes = null;
|
|
|
738 |
long size = 0;
|
| 23798 |
amit.gupta |
739 |
if (roleManager.isAdmin(roleIds)) {
|
| 23781 |
ashik.ali |
740 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
741 |
size = schemeRepository.selectAllCount();
|
| 23796 |
amit.gupta |
742 |
} else {
|
| 23781 |
ashik.ali |
743 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
744 |
size = schemeRepository.selectAllActiveCount();
|
|
|
745 |
}
|
|
|
746 |
map.put("schemes", schemes);
|
|
|
747 |
map.put("start", offset + 1);
|
|
|
748 |
map.put("size", size);
|
| 23796 |
amit.gupta |
749 |
if (schemes.size() < limit) {
|
| 23781 |
ashik.ali |
750 |
map.put("end", offset + schemes.size());
|
| 23796 |
amit.gupta |
751 |
} else {
|
| 23781 |
ashik.ali |
752 |
map.put("end", offset + limit);
|
|
|
753 |
}
|
|
|
754 |
return map;
|
|
|
755 |
}
|
| 23796 |
amit.gupta |
756 |
|
| 23781 |
ashik.ali |
757 |
@Override
|
| 23796 |
amit.gupta |
758 |
public List<Scheme> getPaginatedSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
759 |
throws ProfitMandiBusinessException {
|
| 23781 |
ashik.ali |
760 |
LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
|
|
|
761 |
List<Scheme> schemes = null;
|
| 23798 |
amit.gupta |
762 |
if (roleManager.isAdmin(roleIds)) {
|
| 23781 |
ashik.ali |
763 |
schemes = schemeRepository.selectAll(offset, limit);
|
| 23796 |
amit.gupta |
764 |
} else {
|
| 23781 |
ashik.ali |
765 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
766 |
}
|
|
|
767 |
return schemes;
|
|
|
768 |
}
|
| 23508 |
amit.gupta |
769 |
|
| 23968 |
amit.gupta |
770 |
@Override
|
| 24976 |
amit.gupta |
771 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int priceDropId, String reversalReason)
|
| 23968 |
amit.gupta |
772 |
throws Exception {
|
| 24976 |
amit.gupta |
773 |
|
|
|
774 |
Map<Integer, List<InventoryItem>> purchaseInventoryListMap = inventoryItems.stream()
|
|
|
775 |
.collect(Collectors.groupingBy(InventoryItem::getPurchaseId, Collectors.toList()));
|
|
|
776 |
|
|
|
777 |
for (Map.Entry<Integer, List<InventoryItem>> purchaseEntry : purchaseInventoryListMap.entrySet()) {
|
|
|
778 |
float amountToCredit = 0;
|
|
|
779 |
float amountToDebit = 0;
|
|
|
780 |
int purchaseId = purchaseEntry.getKey();
|
|
|
781 |
List<InventoryItem> purchaseInventoryItemList = purchaseEntry.getValue();
|
|
|
782 |
|
|
|
783 |
Map<Integer, InventoryItem> inventoryItemsMap = purchaseInventoryItemList.stream()
|
|
|
784 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
785 |
|
|
|
786 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
|
|
787 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
788 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
789 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
790 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
791 |
InventoryItem ii = inventoryItemsMap.get(schemeInOut.getInventoryItemId());
|
|
|
792 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
793 |
if (scheme.getType().equals(SchemeType.OUT))
|
|
|
794 |
continue;
|
|
|
795 |
if (scheme.getAmountType().equals(AmountType.FIXED)) {
|
|
|
796 |
continue;
|
| 23995 |
amit.gupta |
797 |
}
|
| 24976 |
amit.gupta |
798 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
799 |
float newAmount = getAmount(ii, scheme);
|
|
|
800 |
if (schemeInOut.getAmount() - newAmount >= 0.01f) {
|
|
|
801 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
802 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
803 |
SchemeInOut sioNew = new SchemeInOut();
|
|
|
804 |
sioNew.setAmount(newAmount);
|
|
|
805 |
sioNew.setInventoryItemId(schemeInOut.getInventoryItemId());
|
|
|
806 |
sioNew.setSchemeId(schemeInOut.getSchemeId());
|
|
|
807 |
schemeInOutRepository.persist(sioNew);
|
|
|
808 |
amountToCredit += sioNew.getAmount();
|
|
|
809 |
amountToDebit += schemeInOut.getAmount();
|
|
|
810 |
}
|
| 24562 |
amit.gupta |
811 |
|
| 24976 |
amit.gupta |
812 |
}
|
| 23986 |
amit.gupta |
813 |
}
|
| 24976 |
amit.gupta |
814 |
int fofoId = inventoryItems.get(0).getFofoId();
|
|
|
815 |
if (amountToDebit > 0) {
|
|
|
816 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN, reversalReason,
|
|
|
817 |
-amountToDebit);
|
|
|
818 |
}
|
|
|
819 |
if (amountToCredit > 0) {
|
|
|
820 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN, reversalReason,
|
|
|
821 |
amountToCredit);
|
|
|
822 |
}
|
| 23968 |
amit.gupta |
823 |
}
|
|
|
824 |
}
|
| 24562 |
amit.gupta |
825 |
|
| 24264 |
amit.gupta |
826 |
@Override
|
| 24562 |
amit.gupta |
827 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int reversalReference, String reversalReason,
|
|
|
828 |
SchemeType schemeType) throws ProfitMandiBusinessException {
|
| 24264 |
amit.gupta |
829 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
|
|
|
830 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
831 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
|
|
832 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
833 |
float amount = 0;
|
|
|
834 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size()).stream()
|
|
|
835 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
836 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
837 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
| 24562 |
amit.gupta |
838 |
if (scheme.getType().equals(schemeType)) {
|
|
|
839 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
| 24264 |
amit.gupta |
840 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
841 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
842 |
amount += schemeInOut.getAmount();
|
|
|
843 |
}
|
|
|
844 |
}
|
|
|
845 |
}
|
|
|
846 |
int fofoId = inventoryItems.get(0).getFofoId();
|
| 24562 |
amit.gupta |
847 |
WalletReferenceType walletReferenceType = schemeType.equals(SchemeType.OUT) ? WalletReferenceType.SCHEME_OUT
|
|
|
848 |
: WalletReferenceType.SCHEME_IN;
|
|
|
849 |
if (amount > 0) {
|
|
|
850 |
walletService.rollbackAmountFromWallet(fofoId, amount, reversalReference, walletReferenceType,
|
|
|
851 |
reversalReason);
|
| 24268 |
amit.gupta |
852 |
}
|
| 24264 |
amit.gupta |
853 |
}
|
| 23968 |
amit.gupta |
854 |
|
| 24976 |
amit.gupta |
855 |
/*
|
|
|
856 |
* @Override public void updateSchmesForModel(int catalogId) throws
|
|
|
857 |
* ProfitMandiBusinessException { List<Item> items =
|
|
|
858 |
* itemRepository.selectAllByCatalogItemId(catalogId); Map<Integer, Scheme>
|
|
|
859 |
* schemes = schemeRepository .selectAllByItemIds(items.stream().map(x ->
|
|
|
860 |
* x.getId()).collect(Collectors.toList()));
|
|
|
861 |
*
|
|
|
862 |
* }
|
|
|
863 |
*/
|
| 24562 |
amit.gupta |
864 |
|
| 22653 |
ashik.ali |
865 |
}
|