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