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