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