| 22653 |
ashik.ali |
1 |
package com.spice.profitmandi.service.scheme;
|
|
|
2 |
|
| 25069 |
amit.gupta |
3 |
import java.time.LocalDate;
|
| 22859 |
ashik.ali |
4 |
import java.time.LocalDateTime;
|
| 25069 |
amit.gupta |
5 |
import java.time.LocalTime;
|
| 23019 |
ashik.ali |
6 |
import java.util.ArrayList;
|
| 22653 |
ashik.ali |
7 |
import java.util.HashMap;
|
| 22859 |
ashik.ali |
8 |
import java.util.HashSet;
|
| 22653 |
ashik.ali |
9 |
import java.util.List;
|
|
|
10 |
import java.util.Map;
|
|
|
11 |
import java.util.Set;
|
| 23968 |
amit.gupta |
12 |
import java.util.stream.Collectors;
|
| 22653 |
ashik.ali |
13 |
|
| 23781 |
ashik.ali |
14 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
15 |
import org.apache.logging.log4j.Logger;
|
| 22653 |
ashik.ali |
16 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 23781 |
ashik.ali |
17 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 22653 |
ashik.ali |
18 |
import org.springframework.stereotype.Component;
|
|
|
19 |
|
| 24307 |
amit.gupta |
20 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
| 22653 |
ashik.ali |
21 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 22859 |
ashik.ali |
22 |
import com.spice.profitmandi.common.model.CreateSchemeRequest;
|
| 22653 |
ashik.ali |
23 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 23019 |
ashik.ali |
24 |
import com.spice.profitmandi.common.model.SchemeModel;
|
| 22859 |
ashik.ali |
25 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 23339 |
ashik.ali |
26 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 22653 |
ashik.ali |
27 |
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
|
|
|
28 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
|
|
29 |
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
|
| 23365 |
ashik.ali |
30 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 22859 |
ashik.ali |
31 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
| 22653 |
ashik.ali |
32 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 23339 |
ashik.ali |
33 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 25043 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
| 22859 |
ashik.ali |
35 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
|
|
36 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
| 23527 |
ashik.ali |
37 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 22653 |
ashik.ali |
38 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 22859 |
ashik.ali |
39 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 22653 |
ashik.ali |
40 |
import com.spice.profitmandi.dao.repository.catalog.RetailerSchemeRepository;
|
|
|
41 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 23995 |
amit.gupta |
42 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 23968 |
amit.gupta |
43 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 23019 |
ashik.ali |
44 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
|
|
45 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
| 23365 |
ashik.ali |
46 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| 22653 |
ashik.ali |
47 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 23339 |
ashik.ali |
48 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 25043 |
amit.gupta |
49 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
| 22859 |
ashik.ali |
50 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
|
|
51 |
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
|
| 23798 |
amit.gupta |
52 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| 22859 |
ashik.ali |
53 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 22653 |
ashik.ali |
54 |
|
| 22859 |
ashik.ali |
55 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
56 |
|
| 22653 |
ashik.ali |
57 |
@Component
|
|
|
58 |
public class SchemeServiceImpl implements SchemeService {
|
|
|
59 |
|
| 23568 |
govind |
60 |
private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
|
| 23444 |
amit.gupta |
61 |
|
| 22653 |
ashik.ali |
62 |
@Autowired
|
| 23781 |
ashik.ali |
63 |
@Qualifier("fofoInventoryItemRepository")
|
| 22653 |
ashik.ali |
64 |
private InventoryItemRepository inventoryItemRepository;
|
| 23444 |
amit.gupta |
65 |
|
| 22653 |
ashik.ali |
66 |
@Autowired
|
| 25043 |
amit.gupta |
67 |
private ScanRecordRepository scanRecordRepository;
|
| 23444 |
amit.gupta |
68 |
|
| 22653 |
ashik.ali |
69 |
@Autowired
|
|
|
70 |
private SchemeRepository schemeRepository;
|
| 23444 |
amit.gupta |
71 |
|
| 22653 |
ashik.ali |
72 |
@Autowired
|
| 23798 |
amit.gupta |
73 |
private RoleManager roleManager;
|
|
|
74 |
|
|
|
75 |
@Autowired
|
| 22859 |
ashik.ali |
76 |
private RetailerRepository retailerRepository;
|
| 24562 |
amit.gupta |
77 |
|
| 23995 |
amit.gupta |
78 |
@Autowired
|
|
|
79 |
private TagListingRepository tagListingRepository;
|
| 23444 |
amit.gupta |
80 |
|
| 22859 |
ashik.ali |
81 |
@Autowired
|
| 22653 |
ashik.ali |
82 |
private RetailerSchemeRepository retailerSchemeRepository;
|
| 23444 |
amit.gupta |
83 |
|
| 22653 |
ashik.ali |
84 |
@Autowired
|
| 22859 |
ashik.ali |
85 |
private SchemeInOutRepository schemeInOutRepository;
|
| 23444 |
amit.gupta |
86 |
|
| 22653 |
ashik.ali |
87 |
@Autowired
|
| 23781 |
ashik.ali |
88 |
@Qualifier("catalogItemRepository")
|
| 22859 |
ashik.ali |
89 |
private ItemRepository itemRepository;
|
| 23444 |
amit.gupta |
90 |
|
| 22859 |
ashik.ali |
91 |
@Autowired
|
|
|
92 |
private SchemeItemRepository schemeItemRepository;
|
| 23444 |
amit.gupta |
93 |
|
| 22859 |
ashik.ali |
94 |
@Autowired
|
|
|
95 |
private WalletService walletService;
|
| 23444 |
amit.gupta |
96 |
|
| 23019 |
ashik.ali |
97 |
@Autowired
|
|
|
98 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
| 23444 |
amit.gupta |
99 |
|
| 23019 |
ashik.ali |
100 |
@Autowired
|
|
|
101 |
private FofoLineItemRepository fofoLineItemRepository;
|
| 23444 |
amit.gupta |
102 |
|
| 23339 |
ashik.ali |
103 |
@Autowired
|
|
|
104 |
private PurchaseRepository purchaseRepository;
|
| 23444 |
amit.gupta |
105 |
|
| 23344 |
ashik.ali |
106 |
@Autowired
|
| 23365 |
ashik.ali |
107 |
private FofoOrderRepository fofoOrderRepository;
|
| 23796 |
amit.gupta |
108 |
|
| 22653 |
ashik.ali |
109 |
@Override
|
| 22859 |
ashik.ali |
110 |
public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
| 23444 |
amit.gupta |
111 |
|
| 23019 |
ashik.ali |
112 |
this.validateCreateSchemeRequest(createSchemeRequest);
|
| 23444 |
amit.gupta |
113 |
|
| 22859 |
ashik.ali |
114 |
Scheme scheme = this.toScheme(creatorId, createSchemeRequest);
|
| 23444 |
amit.gupta |
115 |
|
|
|
116 |
if (scheme.getStartDateTime().isAfter(scheme.getEndDateTime())) {
|
|
|
117 |
throw new ProfitMandiBusinessException(
|
|
|
118 |
ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE,
|
|
|
119 |
scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
|
| 22653 |
ashik.ali |
120 |
}
|
| 23444 |
amit.gupta |
121 |
|
| 22859 |
ashik.ali |
122 |
this.validateItemIds(createSchemeRequest);
|
|
|
123 |
schemeRepository.persist(scheme);
|
| 23444 |
amit.gupta |
124 |
if (!createSchemeRequest.isRetailerAll()) {
|
| 22859 |
ashik.ali |
125 |
this.validateRetailerIds(createSchemeRequest);
|
| 23444 |
amit.gupta |
126 |
for (int retailerId : createSchemeRequest.getRetailerIds()) {
|
| 22859 |
ashik.ali |
127 |
RetailerScheme retailerScheme = new RetailerScheme();
|
|
|
128 |
retailerScheme.setRetailerId(retailerId);
|
|
|
129 |
retailerScheme.setSchemeId(scheme.getId());
|
|
|
130 |
retailerSchemeRepository.persist(retailerScheme);
|
|
|
131 |
}
|
| 22653 |
ashik.ali |
132 |
}
|
| 23444 |
amit.gupta |
133 |
for (int itemId : createSchemeRequest.getItemIds()) {
|
| 22859 |
ashik.ali |
134 |
SchemeItem schemeItem = new SchemeItem();
|
|
|
135 |
schemeItem.setSchemeId(scheme.getId());
|
|
|
136 |
schemeItem.setItemId(itemId);
|
|
|
137 |
schemeItemRepository.persist(schemeItem);
|
|
|
138 |
}
|
| 23444 |
amit.gupta |
139 |
|
| 22653 |
ashik.ali |
140 |
}
|
| 23444 |
amit.gupta |
141 |
|
|
|
142 |
private void validateCreateSchemeRequest(CreateSchemeRequest createSchemeRequest)
|
|
|
143 |
throws ProfitMandiBusinessException {
|
|
|
144 |
if (createSchemeRequest.getName() == null || createSchemeRequest.getName().isEmpty()) {
|
|
|
145 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, createSchemeRequest.getName(),
|
|
|
146 |
"SCHM_VE_1000");
|
| 23019 |
ashik.ali |
147 |
}
|
| 23444 |
amit.gupta |
148 |
if (createSchemeRequest.getAmount() <= 0) {
|
|
|
149 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
150 |
"SCHM_VE_1001");
|
| 23019 |
ashik.ali |
151 |
}
|
| 23444 |
amit.gupta |
152 |
|
| 23527 |
ashik.ali |
153 |
if (AmountType.valueOf(createSchemeRequest.getAmountType()) == AmountType.PERCENTAGE
|
| 23444 |
amit.gupta |
154 |
&& createSchemeRequest.getAmount() > 100) {
|
|
|
155 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
156 |
"SCHM_VE_1002");
|
| 23019 |
ashik.ali |
157 |
}
|
| 23444 |
amit.gupta |
158 |
|
| 23886 |
amit.gupta |
159 |
if (createSchemeRequest.getStartDate() == null) {
|
| 23983 |
amit.gupta |
160 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, createSchemeRequest.getStartDate(),
|
|
|
161 |
"SCHM_VE_1003");
|
| 23019 |
ashik.ali |
162 |
}
|
| 23886 |
amit.gupta |
163 |
if (createSchemeRequest.getEndDate() == null) {
|
| 23983 |
amit.gupta |
164 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, createSchemeRequest.getEndDate(),
|
|
|
165 |
"SCHM_VE_1004");
|
| 23019 |
ashik.ali |
166 |
}
|
|
|
167 |
}
|
| 23444 |
amit.gupta |
168 |
|
|
|
169 |
private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest) {
|
| 22859 |
ashik.ali |
170 |
Scheme scheme = new Scheme();
|
|
|
171 |
scheme.setName(createSchemeRequest.getName());
|
|
|
172 |
scheme.setDescription(createSchemeRequest.getDescription());
|
|
|
173 |
scheme.setType(SchemeType.valueOf(createSchemeRequest.getType()));
|
| 23527 |
ashik.ali |
174 |
scheme.setAmountType(AmountType.valueOf(createSchemeRequest.getAmountType()));
|
| 22859 |
ashik.ali |
175 |
scheme.setAmount(createSchemeRequest.getAmount());
|
| 23886 |
amit.gupta |
176 |
scheme.setStartDateTime(createSchemeRequest.getStartDate());
|
|
|
177 |
scheme.setEndDateTime(createSchemeRequest.getEndDate());
|
| 22859 |
ashik.ali |
178 |
scheme.setCreatedBy(creatorId);
|
|
|
179 |
scheme.setRetailerAll(createSchemeRequest.isRetailerAll());
|
|
|
180 |
return scheme;
|
|
|
181 |
}
|
| 23444 |
amit.gupta |
182 |
|
|
|
183 |
private void validateRetailerIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
|
|
184 |
if (createSchemeRequest.getRetailerIds() == null || createSchemeRequest.getRetailerIds().isEmpty()) {
|
|
|
185 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID,
|
|
|
186 |
createSchemeRequest.getRetailerIds(), "SCHM_1001");
|
| 22859 |
ashik.ali |
187 |
}
|
| 22870 |
ashik.ali |
188 |
List<Integer> foundRetailerIds = retailerRepository.selectIdsByIds(createSchemeRequest.getRetailerIds());
|
| 23444 |
amit.gupta |
189 |
if (foundRetailerIds.size() != createSchemeRequest.getRetailerIds().size()) {
|
| 22859 |
ashik.ali |
190 |
createSchemeRequest.getRetailerIds().removeAll(foundRetailerIds);
|
| 23444 |
amit.gupta |
191 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID,
|
|
|
192 |
createSchemeRequest.getRetailerIds(), "SCHM_1002");
|
| 22859 |
ashik.ali |
193 |
}
|
|
|
194 |
}
|
| 23444 |
amit.gupta |
195 |
|
|
|
196 |
private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
|
|
197 |
if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
|
|
|
198 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
199 |
"SCHM_1003");
|
| 22859 |
ashik.ali |
200 |
}
|
| 23444 |
amit.gupta |
201 |
List<Integer> foundItemIds = itemRepository.selectIdsByIdsAndType(createSchemeRequest.getItemIds(),
|
|
|
202 |
ItemType.SERIALIZED);
|
|
|
203 |
if (foundItemIds.size() != createSchemeRequest.getItemIds().size()) {
|
| 22859 |
ashik.ali |
204 |
createSchemeRequest.getItemIds().removeAll(foundItemIds);
|
| 23444 |
amit.gupta |
205 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
206 |
"SCHM_1004");
|
| 22859 |
ashik.ali |
207 |
}
|
|
|
208 |
}
|
| 23444 |
amit.gupta |
209 |
|
| 22859 |
ashik.ali |
210 |
@Override
|
|
|
211 |
public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
212 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 23444 |
amit.gupta |
213 |
if (!scheme.isRetailerAll()) {
|
| 23019 |
ashik.ali |
214 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
|
|
215 |
scheme.getRetailerIds().addAll(retailerIds);
|
| 22859 |
ashik.ali |
216 |
}
|
|
|
217 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23914 |
govind |
218 |
if (itemIds.size() > 0) {
|
|
|
219 |
List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
|
|
|
220 |
scheme.setItemStringMap(this.toItemStringMap(items));
|
| 23983 |
amit.gupta |
221 |
}
|
| 22859 |
ashik.ali |
222 |
return scheme;
|
|
|
223 |
}
|
| 23444 |
amit.gupta |
224 |
|
|
|
225 |
public Map<Integer, String> toItemStringMap(List<Item> items) {
|
| 23339 |
ashik.ali |
226 |
Map<Integer, String> itemMap = new HashMap<>();
|
| 23444 |
amit.gupta |
227 |
for (Item item : items) {
|
| 23339 |
ashik.ali |
228 |
itemMap.put(item.getId(), this.getItemString(item));
|
|
|
229 |
}
|
|
|
230 |
return itemMap;
|
|
|
231 |
}
|
| 23444 |
amit.gupta |
232 |
|
|
|
233 |
public String getItemString(Item item) {
|
| 23339 |
ashik.ali |
234 |
StringBuilder itemString = new StringBuilder();
|
| 23444 |
amit.gupta |
235 |
if (item.getBrand() != null && !item.getBrand().isEmpty()) {
|
| 23339 |
ashik.ali |
236 |
itemString.append(item.getBrand().trim());
|
|
|
237 |
}
|
|
|
238 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
239 |
if (item.getModelName() != null && !item.getModelName().isEmpty()) {
|
| 23339 |
ashik.ali |
240 |
itemString.append(item.getModelName().trim());
|
|
|
241 |
}
|
|
|
242 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
243 |
if (item.getModelNumber() != null && !item.getModelNumber().isEmpty()) {
|
| 23339 |
ashik.ali |
244 |
itemString.append(item.getModelNumber().trim());
|
|
|
245 |
}
|
|
|
246 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
247 |
if (item.getColor() != null && !item.getColor().isEmpty()) {
|
| 23339 |
ashik.ali |
248 |
itemString.append(item.getColor().trim());
|
|
|
249 |
}
|
|
|
250 |
return itemString.toString();
|
|
|
251 |
}
|
| 23444 |
amit.gupta |
252 |
|
|
|
253 |
private Set<Integer> schemeItemsToItemIds(List<SchemeItem> schemeItems) {
|
| 23339 |
ashik.ali |
254 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
255 |
for (SchemeItem schemeItem : schemeItems) {
|
| 23339 |
ashik.ali |
256 |
itemIds.add(schemeItem.getItemId());
|
|
|
257 |
}
|
|
|
258 |
return itemIds;
|
|
|
259 |
}
|
| 23444 |
amit.gupta |
260 |
|
| 23019 |
ashik.ali |
261 |
@Override
|
|
|
262 |
public List<SchemeModel> getAllSchemeModels(LocalDateTime startDateTime, LocalDateTime endDateTime) {
|
|
|
263 |
List<Scheme> schemes = schemeRepository.selectAllBetweenCreateTimestamp(startDateTime, endDateTime);
|
|
|
264 |
Map<Integer, Scheme> schemeIdSchemeMap = this.toSchemeIdSchemeMap(schemes);
|
|
|
265 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
| 23339 |
ashik.ali |
266 |
Set<Integer> itemIds = this.schemeItemsToItemIds(schemeItems);
|
|
|
267 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
|
|
268 |
Map<Integer, String> itemStringMap = this.toItemStringMap(items);
|
|
|
269 |
this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
|
| 23019 |
ashik.ali |
270 |
List<RetailerScheme> retailerSchemes = retailerSchemeRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
|
|
271 |
this.addRetailerIdsToSchemes(retailerSchemes, schemeIdSchemeMap);
|
|
|
272 |
return this.toSchemeModels(schemeIdSchemeMap);
|
|
|
273 |
}
|
| 23444 |
amit.gupta |
274 |
|
|
|
275 |
private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
|
|
|
276 |
Map<Integer, String> itemStringMap) {
|
|
|
277 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
278 |
schemeIdSchemeMap.get(schemeItem.getSchemeId()).getItemStringMap().put(schemeItem.getItemId(),
|
|
|
279 |
itemStringMap.get(schemeItem.getItemId()));
|
| 23019 |
ashik.ali |
280 |
}
|
|
|
281 |
}
|
| 23444 |
amit.gupta |
282 |
|
|
|
283 |
private void addRetailerIdsToSchemes(List<RetailerScheme> retailerSchemes, Map<Integer, Scheme> schemeIdSchemeMap) {
|
|
|
284 |
for (RetailerScheme retailerScheme : retailerSchemes) {
|
|
|
285 |
schemeIdSchemeMap.get(retailerScheme.getSchemeId()).getRetailerIds().add(retailerScheme.getRetailerId());
|
| 23019 |
ashik.ali |
286 |
}
|
|
|
287 |
}
|
| 23444 |
amit.gupta |
288 |
|
|
|
289 |
private List<SchemeModel> toSchemeModels(Map<Integer, Scheme> schemeIdSchemeMap) {
|
| 23019 |
ashik.ali |
290 |
List<SchemeModel> schemeModels = new ArrayList<>();
|
| 23444 |
amit.gupta |
291 |
for (Map.Entry<Integer, Scheme> schemeIdSchemeEntry : schemeIdSchemeMap.entrySet()) {
|
| 23019 |
ashik.ali |
292 |
schemeModels.add(this.toSchemeModel(schemeIdSchemeEntry.getValue()));
|
|
|
293 |
}
|
|
|
294 |
return schemeModels;
|
|
|
295 |
}
|
| 23444 |
amit.gupta |
296 |
|
|
|
297 |
private SchemeModel toSchemeModel(Scheme scheme) {
|
| 23019 |
ashik.ali |
298 |
SchemeModel schemeModel = new SchemeModel();
|
|
|
299 |
schemeModel.setSchemeId(scheme.getId());
|
|
|
300 |
schemeModel.setName(scheme.getName());
|
|
|
301 |
schemeModel.setDescription(scheme.getDescription());
|
|
|
302 |
schemeModel.setSchemeType(scheme.getType().toString());
|
|
|
303 |
schemeModel.setAmountType(scheme.getAmountType().toString());
|
|
|
304 |
schemeModel.setAmount(scheme.getAmount());
|
|
|
305 |
schemeModel.setStartDateTime(StringUtils.toString(scheme.getStartDateTime()));
|
|
|
306 |
schemeModel.setEndDateTime(StringUtils.toString(scheme.getEndDateTime()));
|
|
|
307 |
schemeModel.setCreateTimestamp(StringUtils.toString(scheme.getCreateTimestamp()));
|
|
|
308 |
schemeModel.setActiveTimestamp(StringUtils.toString(scheme.getActiveTimestamp()));
|
|
|
309 |
schemeModel.setExpireTimestamp(StringUtils.toString(scheme.getExpireTimestamp()));
|
|
|
310 |
schemeModel.setCreatedBy(scheme.getCreatedBy());
|
| 23339 |
ashik.ali |
311 |
schemeModel.setItemStringMap(scheme.getItemStringMap());
|
| 23019 |
ashik.ali |
312 |
schemeModel.setRetailerIds(scheme.getRetailerIds());
|
|
|
313 |
return schemeModel;
|
|
|
314 |
}
|
| 22653 |
ashik.ali |
315 |
|
|
|
316 |
@Override
|
|
|
317 |
public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
318 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 23444 |
amit.gupta |
319 |
if (scheme.getActiveTimestamp() != null) {
|
|
|
320 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
321 |
"SCHM_1005");
|
| 22653 |
ashik.ali |
322 |
}
|
| 23444 |
amit.gupta |
323 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
324 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
325 |
"SCHM_1006");
|
| 22653 |
ashik.ali |
326 |
}
|
| 22859 |
ashik.ali |
327 |
scheme.setActiveTimestamp(LocalDateTime.now());
|
|
|
328 |
schemeRepository.persist(scheme);
|
| 23444 |
amit.gupta |
329 |
if (scheme.getType() == SchemeType.IN) {
|
| 23019 |
ashik.ali |
330 |
this.processPreviousPurchases(scheme);
|
| 23444 |
amit.gupta |
331 |
} else if (scheme.getType() == SchemeType.OUT) {
|
| 23019 |
ashik.ali |
332 |
this.processPreviousSales(scheme);
|
|
|
333 |
}
|
| 22653 |
ashik.ali |
334 |
}
|
| 23444 |
amit.gupta |
335 |
|
|
|
336 |
private void processPreviousPurchases(Scheme scheme) throws ProfitMandiBusinessException {
|
|
|
337 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectBetweenDates(scheme.getStartDateTime(),
|
|
|
338 |
scheme.getEndDateTime());
|
|
|
339 |
if (inventoryItems.isEmpty()) {
|
| 23370 |
ashik.ali |
340 |
return;
|
|
|
341 |
}
|
| 23019 |
ashik.ali |
342 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
343 |
if (itemIds.isEmpty()) {
|
| 23370 |
ashik.ali |
344 |
return;
|
|
|
345 |
}
|
| 23339 |
ashik.ali |
346 |
List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
|
|
|
347 |
Map<Integer, String> itemStringMap = this.toItemStringMap(items);
|
| 23444 |
amit.gupta |
348 |
List<InventoryItem> completedInventoryItems = this.toFilteredPurchaseCompletedInventoryItems(inventoryItems,
|
|
|
349 |
itemIds, true);
|
|
|
350 |
if (completedInventoryItems.isEmpty()) {
|
| 23370 |
ashik.ali |
351 |
return;
|
|
|
352 |
}
|
| 23444 |
amit.gupta |
353 |
if (scheme.isRetailerAll()) {
|
|
|
354 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
| 23019 |
ashik.ali |
355 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 24562 |
amit.gupta |
356 |
if (cashback > 0) {
|
|
|
357 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
|
|
358 |
WalletReferenceType.SCHEME_IN,
|
|
|
359 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId())
|
|
|
360 |
+ " against invoiceNumber " + inventoryItem.getPurchase().getPurchaseReference(),
|
|
|
361 |
cashback);
|
|
|
362 |
}
|
| 23019 |
ashik.ali |
363 |
}
|
| 23444 |
amit.gupta |
364 |
} else {
|
| 23019 |
ashik.ali |
365 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
366 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
|
|
367 |
if (retailerIds.contains(inventoryItem.getFofoId())) {
|
| 23019 |
ashik.ali |
368 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 24562 |
amit.gupta |
369 |
if (cashback > 0) {
|
|
|
370 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
|
|
371 |
WalletReferenceType.SCHEME_IN,
|
|
|
372 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId())
|
|
|
373 |
+ " against invoiceNumber "
|
|
|
374 |
+ inventoryItem.getPurchase().getPurchaseReference(),
|
|
|
375 |
cashback);
|
|
|
376 |
}
|
| 23019 |
ashik.ali |
377 |
}
|
|
|
378 |
}
|
|
|
379 |
}
|
| 23444 |
amit.gupta |
380 |
|
| 23019 |
ashik.ali |
381 |
}
|
| 23444 |
amit.gupta |
382 |
|
|
|
383 |
private Map<Integer, Integer> toInventoryItemIdFofoOrderIdMap(List<FofoLineItem> fofoLineItems,
|
|
|
384 |
Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap) {
|
| 23019 |
ashik.ali |
385 |
Map<Integer, Integer> inventoryItemIdFofoOrderIdMap = new HashMap<>();
|
| 23444 |
amit.gupta |
386 |
|
|
|
387 |
for (FofoLineItem fofoLineItem : fofoLineItems) {
|
|
|
388 |
inventoryItemIdFofoOrderIdMap.put(fofoLineItem.getInventoryItemId(),
|
| 23019 |
ashik.ali |
389 |
fofoOrderItemIdFofoOrderItemMap.get(fofoLineItem.getFofoOrderItemId()).getOrderId());
|
|
|
390 |
}
|
|
|
391 |
return inventoryItemIdFofoOrderIdMap;
|
|
|
392 |
}
|
| 23444 |
amit.gupta |
393 |
|
|
|
394 |
private void processPreviousSales(Scheme scheme) throws ProfitMandiBusinessException {
|
|
|
395 |
List<FofoOrderItem> fofoOrderItems = fofoOrderItemRepository.selectBetweenDates(scheme.getStartDateTime(),
|
|
|
396 |
scheme.getEndDateTime());
|
|
|
397 |
|
|
|
398 |
if (fofoOrderItems.isEmpty()) {
|
| 23370 |
ashik.ali |
399 |
return;
|
|
|
400 |
}
|
| 22653 |
ashik.ali |
401 |
|
| 23444 |
amit.gupta |
402 |
Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap = this
|
|
|
403 |
.toFofoOrderItemIdFofoOrderItemMap(fofoOrderItems);
|
|
|
404 |
|
|
|
405 |
List<FofoLineItem> fofoLineItems = fofoLineItemRepository
|
|
|
406 |
.selectByFofoOrderItemIds(fofoOrderItemIdFofoOrderItemMap.keySet());
|
|
|
407 |
|
|
|
408 |
if (fofoLineItems.isEmpty()) {
|
| 23370 |
ashik.ali |
409 |
return;
|
|
|
410 |
}
|
| 23444 |
amit.gupta |
411 |
|
|
|
412 |
Map<Integer, Integer> inventoryItemIdFofoOrderIdMap = this.toInventoryItemIdFofoOrderIdMap(fofoLineItems,
|
|
|
413 |
fofoOrderItemIdFofoOrderItemMap);
|
|
|
414 |
|
|
|
415 |
List<InventoryItem> inventoryItems = inventoryItemRepository
|
|
|
416 |
.selectByIds(inventoryItemIdFofoOrderIdMap.keySet());
|
|
|
417 |
|
|
|
418 |
if (inventoryItems.isEmpty()) {
|
| 23370 |
ashik.ali |
419 |
return;
|
|
|
420 |
}
|
| 23444 |
amit.gupta |
421 |
|
| 23019 |
ashik.ali |
422 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
423 |
|
|
|
424 |
if (itemIds.isEmpty()) {
|
| 23370 |
ashik.ali |
425 |
return;
|
|
|
426 |
}
|
| 23444 |
amit.gupta |
427 |
|
|
|
428 |
List<InventoryItem> completedInventoryItems = this.toFilteredPurchaseCompletedInventoryItems(inventoryItems,
|
|
|
429 |
itemIds, false);
|
|
|
430 |
|
|
|
431 |
if (scheme.isRetailerAll()) {
|
|
|
432 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
| 23019 |
ashik.ali |
433 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 24562 |
amit.gupta |
434 |
if (cashback > 0) {
|
|
|
435 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
|
|
436 |
WalletReferenceType.SCHEME_OUT, "Added for SCHEME_OUT", cashback);
|
|
|
437 |
}
|
| 23019 |
ashik.ali |
438 |
}
|
| 23444 |
amit.gupta |
439 |
} else {
|
| 23019 |
ashik.ali |
440 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
441 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
|
|
442 |
if (retailerIds.contains(inventoryItem.getFofoId())) {
|
| 23019 |
ashik.ali |
443 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 24562 |
amit.gupta |
444 |
if (cashback > 0) {
|
|
|
445 |
walletService.addAmountToWallet(inventoryItem.getFofoId(),
|
|
|
446 |
inventoryItemIdFofoOrderIdMap.get(inventoryItem.getId()),
|
|
|
447 |
WalletReferenceType.SCHEME_OUT, "Added for SCHEME_OUT", cashback);
|
|
|
448 |
}
|
| 23019 |
ashik.ali |
449 |
}
|
|
|
450 |
}
|
|
|
451 |
}
|
|
|
452 |
}
|
| 23444 |
amit.gupta |
453 |
|
|
|
454 |
private Map<Integer, FofoOrderItem> toFofoOrderItemIdFofoOrderItemMap(List<FofoOrderItem> fofoOrderItems) {
|
| 23019 |
ashik.ali |
455 |
Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap = new HashMap<>();
|
| 23444 |
amit.gupta |
456 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
| 23019 |
ashik.ali |
457 |
fofoOrderItemIdFofoOrderItemMap.put(fofoOrderItem.getId(), fofoOrderItem);
|
|
|
458 |
}
|
|
|
459 |
return fofoOrderItemIdFofoOrderItemMap;
|
|
|
460 |
}
|
| 23444 |
amit.gupta |
461 |
|
|
|
462 |
private List<InventoryItem> toFilteredPurchaseCompletedInventoryItems(List<InventoryItem> inventoryItems,
|
|
|
463 |
List<Integer> itemIds, boolean purchase) {
|
| 23019 |
ashik.ali |
464 |
List<InventoryItem> completedInventoryItems = new ArrayList<>();
|
| 23444 |
amit.gupta |
465 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
466 |
if (purchase && inventoryItem.getPurchase().getCompleteTimestamp() != null
|
|
|
467 |
&& itemIds.contains(inventoryItem.getItemId())) {
|
| 23019 |
ashik.ali |
468 |
completedInventoryItems.add(inventoryItem);
|
| 23444 |
amit.gupta |
469 |
} else if (!purchase && itemIds.contains(inventoryItem.getItemId())) {
|
| 23297 |
ashik.ali |
470 |
completedInventoryItems.add(inventoryItem);
|
| 23019 |
ashik.ali |
471 |
}
|
|
|
472 |
}
|
|
|
473 |
return completedInventoryItems;
|
|
|
474 |
}
|
| 23444 |
amit.gupta |
475 |
|
| 22653 |
ashik.ali |
476 |
@Override
|
| 25069 |
amit.gupta |
477 |
public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
|
| 22653 |
ashik.ali |
478 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 25069 |
amit.gupta |
479 |
if (scheme==null || scheme.getActiveTimestamp() == null) {
|
| 23444 |
amit.gupta |
480 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
481 |
"SCHM_1007");
|
| 22653 |
ashik.ali |
482 |
}
|
| 23444 |
amit.gupta |
483 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
484 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
485 |
"SCHM_1008");
|
| 22653 |
ashik.ali |
486 |
}
|
| 22859 |
ashik.ali |
487 |
scheme.setExpireTimestamp(LocalDateTime.now());
|
| 25069 |
amit.gupta |
488 |
scheme.setEndDateTime(expiryTime);
|
| 22859 |
ashik.ali |
489 |
schemeRepository.persist(scheme);
|
| 22653 |
ashik.ali |
490 |
}
|
| 23444 |
amit.gupta |
491 |
|
|
|
492 |
private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
493 |
Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
|
| 23444 |
amit.gupta |
494 |
for (Scheme scheme : schemes) {
|
| 23019 |
ashik.ali |
495 |
schemeIdSchemeMap.put(scheme.getId(), scheme);
|
| 22859 |
ashik.ali |
496 |
}
|
| 23019 |
ashik.ali |
497 |
return schemeIdSchemeMap;
|
| 22859 |
ashik.ali |
498 |
}
|
| 23444 |
amit.gupta |
499 |
|
|
|
500 |
private Set<Integer> inventoryItemSchemesMapToSchemeIds(Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap) {
|
| 22859 |
ashik.ali |
501 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
502 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
|
|
503 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
| 22859 |
ashik.ali |
504 |
schemeIds.add(scheme.getId());
|
|
|
505 |
}
|
|
|
506 |
}
|
|
|
507 |
return schemeIds;
|
|
|
508 |
}
|
| 23444 |
amit.gupta |
509 |
|
|
|
510 |
private Set<Integer> inventoryItemsToItemIds(List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
511 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
512 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 22859 |
ashik.ali |
513 |
itemIds.add(inventoryItem.getItemId());
|
|
|
514 |
}
|
|
|
515 |
return itemIds;
|
|
|
516 |
}
|
| 23444 |
amit.gupta |
517 |
|
|
|
518 |
private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
519 |
Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemeMap(schemes);
|
| 22859 |
ashik.ali |
520 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
|
| 23444 |
amit.gupta |
521 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
522 |
if (!itemIdSchemesMap.containsKey(schemeItem.getItemId())) {
|
| 22859 |
ashik.ali |
523 |
Set<Scheme> schemesSet = new HashSet<>();
|
|
|
524 |
schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
525 |
itemIdSchemesMap.put(schemeItem.getItemId(), schemesSet);
|
| 23444 |
amit.gupta |
526 |
} else {
|
| 22859 |
ashik.ali |
527 |
itemIdSchemesMap.get(schemeItem.getItemId()).add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
528 |
}
|
|
|
529 |
}
|
|
|
530 |
return itemIdSchemesMap;
|
|
|
531 |
}
|
| 22653 |
ashik.ali |
532 |
|
| 23444 |
amit.gupta |
533 |
private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
|
|
|
534 |
List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
535 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
536 |
for (Scheme scheme : schemes) {
|
| 22859 |
ashik.ali |
537 |
schemeIds.add(scheme.getId());
|
|
|
538 |
}
|
|
|
539 |
Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
|
|
|
540 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
|
| 23444 |
amit.gupta |
541 |
|
| 22859 |
ashik.ali |
542 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
|
|
|
543 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemsMap = new HashMap<>();
|
| 23444 |
amit.gupta |
544 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
545 |
if (itemIdSchemesMap.containsKey(inventoryItem.getItemId())) {
|
| 22859 |
ashik.ali |
546 |
inventoryItemSchemsMap.put(inventoryItem, itemIdSchemesMap.get(inventoryItem.getItemId()));
|
|
|
547 |
}
|
|
|
548 |
}
|
|
|
549 |
return inventoryItemSchemsMap;
|
|
|
550 |
}
|
| 23444 |
amit.gupta |
551 |
|
| 22859 |
ashik.ali |
552 |
@Override
|
| 23365 |
ashik.ali |
553 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
| 23369 |
ashik.ali |
554 |
LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
|
| 23344 |
ashik.ali |
555 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
| 23369 |
ashik.ali |
556 |
LOGGER.info("purchase is completed = {}", (purchase.getCompleteTimestamp() != null));
|
| 23444 |
amit.gupta |
557 |
if (purchase.getCompleteTimestamp() != null) {
|
| 25043 |
amit.gupta |
558 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, purchase.getCompleteTimestamp());
|
| 23365 |
ashik.ali |
559 |
float totalCashback = 0;
|
| 23444 |
amit.gupta |
560 |
if (schemes.isEmpty()) {
|
| 23365 |
ashik.ali |
561 |
return;
|
|
|
562 |
}
|
|
|
563 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
|
| 23444 |
amit.gupta |
564 |
|
|
|
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 |
}
|