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