| 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;
|
| 23527 |
ashik.ali |
33 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 22653 |
ashik.ali |
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 |
|
| 23527 |
ashik.ali |
144 |
if (AmountType.valueOf(createSchemeRequest.getAmountType()) == AmountType.PERCENTAGE
|
| 23444 |
amit.gupta |
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()));
|
| 23527 |
ashik.ali |
168 |
scheme.setAmountType(AmountType.valueOf(createSchemeRequest.getAmountType()));
|
| 22859 |
ashik.ali |
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,
|
| 23508 |
amit.gupta |
352 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId()) + " against invoiceNumber " + inventoryItem.getPurchase().getPurchaseReference(),
|
| 23444 |
amit.gupta |
353 |
cashback);
|
| 23019 |
ashik.ali |
354 |
}
|
| 23444 |
amit.gupta |
355 |
} else {
|
| 23019 |
ashik.ali |
356 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
357 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
|
|
358 |
if (retailerIds.contains(inventoryItem.getFofoId())) {
|
| 23019 |
ashik.ali |
359 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 23444 |
amit.gupta |
360 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
|
|
361 |
WalletReferenceType.SCHEME_IN,
|
| 23508 |
amit.gupta |
362 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId()) + " against invoiceNumber " + inventoryItem.getPurchase().getPurchaseReference(), cashback);
|
| 23019 |
ashik.ali |
363 |
}
|
|
|
364 |
}
|
|
|
365 |
}
|
| 23444 |
amit.gupta |
366 |
|
| 23019 |
ashik.ali |
367 |
}
|
| 23444 |
amit.gupta |
368 |
|
|
|
369 |
private Map<Integer, Integer> toInventoryItemIdFofoOrderIdMap(List<FofoLineItem> fofoLineItems,
|
|
|
370 |
Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap) {
|
| 23019 |
ashik.ali |
371 |
Map<Integer, Integer> inventoryItemIdFofoOrderIdMap = new HashMap<>();
|
| 23444 |
amit.gupta |
372 |
|
|
|
373 |
for (FofoLineItem fofoLineItem : fofoLineItems) {
|
|
|
374 |
inventoryItemIdFofoOrderIdMap.put(fofoLineItem.getInventoryItemId(),
|
| 23019 |
ashik.ali |
375 |
fofoOrderItemIdFofoOrderItemMap.get(fofoLineItem.getFofoOrderItemId()).getOrderId());
|
|
|
376 |
}
|
|
|
377 |
return inventoryItemIdFofoOrderIdMap;
|
|
|
378 |
}
|
| 23444 |
amit.gupta |
379 |
|
|
|
380 |
private void processPreviousSales(Scheme scheme) throws ProfitMandiBusinessException {
|
|
|
381 |
List<FofoOrderItem> fofoOrderItems = fofoOrderItemRepository.selectBetweenDates(scheme.getStartDateTime(),
|
|
|
382 |
scheme.getEndDateTime());
|
|
|
383 |
|
|
|
384 |
if (fofoOrderItems.isEmpty()) {
|
| 23370 |
ashik.ali |
385 |
return;
|
|
|
386 |
}
|
| 22653 |
ashik.ali |
387 |
|
| 23444 |
amit.gupta |
388 |
Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap = this
|
|
|
389 |
.toFofoOrderItemIdFofoOrderItemMap(fofoOrderItems);
|
|
|
390 |
|
|
|
391 |
List<FofoLineItem> fofoLineItems = fofoLineItemRepository
|
|
|
392 |
.selectByFofoOrderItemIds(fofoOrderItemIdFofoOrderItemMap.keySet());
|
|
|
393 |
|
|
|
394 |
if (fofoLineItems.isEmpty()) {
|
| 23370 |
ashik.ali |
395 |
return;
|
|
|
396 |
}
|
| 23444 |
amit.gupta |
397 |
|
|
|
398 |
Map<Integer, Integer> inventoryItemIdFofoOrderIdMap = this.toInventoryItemIdFofoOrderIdMap(fofoLineItems,
|
|
|
399 |
fofoOrderItemIdFofoOrderItemMap);
|
|
|
400 |
|
|
|
401 |
List<InventoryItem> inventoryItems = inventoryItemRepository
|
|
|
402 |
.selectByIds(inventoryItemIdFofoOrderIdMap.keySet());
|
|
|
403 |
|
|
|
404 |
if (inventoryItems.isEmpty()) {
|
| 23370 |
ashik.ali |
405 |
return;
|
|
|
406 |
}
|
| 23444 |
amit.gupta |
407 |
|
| 23019 |
ashik.ali |
408 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
409 |
|
|
|
410 |
if (itemIds.isEmpty()) {
|
| 23370 |
ashik.ali |
411 |
return;
|
|
|
412 |
}
|
| 23444 |
amit.gupta |
413 |
|
|
|
414 |
List<InventoryItem> completedInventoryItems = this.toFilteredPurchaseCompletedInventoryItems(inventoryItems,
|
|
|
415 |
itemIds, false);
|
|
|
416 |
|
|
|
417 |
if (scheme.isRetailerAll()) {
|
|
|
418 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
| 23019 |
ashik.ali |
419 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 23444 |
amit.gupta |
420 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
|
|
421 |
WalletReferenceType.SCHEME_OUT, "Added for SCHEME_OUT", cashback);
|
| 23019 |
ashik.ali |
422 |
}
|
| 23444 |
amit.gupta |
423 |
} else {
|
| 23019 |
ashik.ali |
424 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
| 23444 |
amit.gupta |
425 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
|
|
426 |
if (retailerIds.contains(inventoryItem.getFofoId())) {
|
| 23019 |
ashik.ali |
427 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 23444 |
amit.gupta |
428 |
walletService.addAmountToWallet(inventoryItem.getFofoId(),
|
|
|
429 |
inventoryItemIdFofoOrderIdMap.get(inventoryItem.getId()), WalletReferenceType.SCHEME_OUT,
|
|
|
430 |
"Added for SCHEME_OUT", cashback);
|
| 23019 |
ashik.ali |
431 |
}
|
|
|
432 |
}
|
|
|
433 |
}
|
|
|
434 |
}
|
| 23444 |
amit.gupta |
435 |
|
|
|
436 |
private Map<Integer, FofoOrderItem> toFofoOrderItemIdFofoOrderItemMap(List<FofoOrderItem> fofoOrderItems) {
|
| 23019 |
ashik.ali |
437 |
Map<Integer, FofoOrderItem> fofoOrderItemIdFofoOrderItemMap = new HashMap<>();
|
| 23444 |
amit.gupta |
438 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
| 23019 |
ashik.ali |
439 |
fofoOrderItemIdFofoOrderItemMap.put(fofoOrderItem.getId(), fofoOrderItem);
|
|
|
440 |
}
|
|
|
441 |
return fofoOrderItemIdFofoOrderItemMap;
|
|
|
442 |
}
|
| 23444 |
amit.gupta |
443 |
|
|
|
444 |
private List<InventoryItem> toFilteredPurchaseCompletedInventoryItems(List<InventoryItem> inventoryItems,
|
|
|
445 |
List<Integer> itemIds, boolean purchase) {
|
| 23019 |
ashik.ali |
446 |
List<InventoryItem> completedInventoryItems = new ArrayList<>();
|
| 23444 |
amit.gupta |
447 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
448 |
if (purchase && inventoryItem.getPurchase().getCompleteTimestamp() != null
|
|
|
449 |
&& itemIds.contains(inventoryItem.getItemId())) {
|
| 23019 |
ashik.ali |
450 |
completedInventoryItems.add(inventoryItem);
|
| 23444 |
amit.gupta |
451 |
} else if (!purchase && itemIds.contains(inventoryItem.getItemId())) {
|
| 23297 |
ashik.ali |
452 |
completedInventoryItems.add(inventoryItem);
|
| 23019 |
ashik.ali |
453 |
}
|
|
|
454 |
}
|
|
|
455 |
return completedInventoryItems;
|
|
|
456 |
}
|
| 23444 |
amit.gupta |
457 |
|
| 22653 |
ashik.ali |
458 |
@Override
|
|
|
459 |
public void expireSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
460 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 23444 |
amit.gupta |
461 |
if (scheme.getActiveTimestamp() == null) {
|
|
|
462 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
463 |
"SCHM_1007");
|
| 22653 |
ashik.ali |
464 |
}
|
| 23444 |
amit.gupta |
465 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
466 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
467 |
"SCHM_1008");
|
| 22653 |
ashik.ali |
468 |
}
|
| 22859 |
ashik.ali |
469 |
scheme.setExpireTimestamp(LocalDateTime.now());
|
|
|
470 |
schemeRepository.persist(scheme);
|
| 22653 |
ashik.ali |
471 |
}
|
| 23444 |
amit.gupta |
472 |
|
|
|
473 |
private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
474 |
Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
|
| 23444 |
amit.gupta |
475 |
for (Scheme scheme : schemes) {
|
| 23019 |
ashik.ali |
476 |
schemeIdSchemeMap.put(scheme.getId(), scheme);
|
| 22859 |
ashik.ali |
477 |
}
|
| 23019 |
ashik.ali |
478 |
return schemeIdSchemeMap;
|
| 22859 |
ashik.ali |
479 |
}
|
| 23444 |
amit.gupta |
480 |
|
|
|
481 |
private Set<Integer> inventoryItemSchemesMapToSchemeIds(Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap) {
|
| 22859 |
ashik.ali |
482 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
483 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
|
|
484 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
| 22859 |
ashik.ali |
485 |
schemeIds.add(scheme.getId());
|
|
|
486 |
}
|
|
|
487 |
}
|
|
|
488 |
return schemeIds;
|
|
|
489 |
}
|
| 23444 |
amit.gupta |
490 |
|
|
|
491 |
private Set<Integer> fofoLineItemSchemesMapToSchemeIds(Map<FofoOrderItem, Set<Scheme>> fofoLineItemSchemesMap) {
|
| 22859 |
ashik.ali |
492 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
493 |
for (Map.Entry<FofoOrderItem, Set<Scheme>> inventoryItemSchemesEntry : fofoLineItemSchemesMap.entrySet()) {
|
|
|
494 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
| 22859 |
ashik.ali |
495 |
schemeIds.add(scheme.getId());
|
|
|
496 |
}
|
|
|
497 |
}
|
|
|
498 |
return schemeIds;
|
|
|
499 |
}
|
| 23444 |
amit.gupta |
500 |
|
|
|
501 |
private Set<Integer> inventoryItemsToItemIds(List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
502 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
503 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 22859 |
ashik.ali |
504 |
itemIds.add(inventoryItem.getItemId());
|
|
|
505 |
}
|
|
|
506 |
return itemIds;
|
|
|
507 |
}
|
| 23444 |
amit.gupta |
508 |
|
|
|
509 |
private Set<Integer> fofoOrderItemsToItemIds(List<FofoOrderItem> fofoOrderItems) {
|
| 22859 |
ashik.ali |
510 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
511 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
| 22859 |
ashik.ali |
512 |
itemIds.add(fofoOrderItem.getItemId());
|
|
|
513 |
}
|
|
|
514 |
return itemIds;
|
|
|
515 |
}
|
| 23444 |
amit.gupta |
516 |
|
|
|
517 |
private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
518 |
Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemeMap(schemes);
|
| 22859 |
ashik.ali |
519 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
|
| 23444 |
amit.gupta |
520 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
521 |
if (!itemIdSchemesMap.containsKey(schemeItem.getItemId())) {
|
| 22859 |
ashik.ali |
522 |
Set<Scheme> schemesSet = new HashSet<>();
|
|
|
523 |
schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
524 |
itemIdSchemesMap.put(schemeItem.getItemId(), schemesSet);
|
| 23444 |
amit.gupta |
525 |
} else {
|
| 22859 |
ashik.ali |
526 |
itemIdSchemesMap.get(schemeItem.getItemId()).add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
527 |
}
|
|
|
528 |
}
|
|
|
529 |
return itemIdSchemesMap;
|
|
|
530 |
}
|
| 22653 |
ashik.ali |
531 |
|
| 23444 |
amit.gupta |
532 |
private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
|
|
|
533 |
List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
534 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
535 |
for (Scheme scheme : schemes) {
|
| 22859 |
ashik.ali |
536 |
schemeIds.add(scheme.getId());
|
|
|
537 |
}
|
|
|
538 |
Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
|
|
|
539 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
|
| 23444 |
amit.gupta |
540 |
|
| 22859 |
ashik.ali |
541 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
|
|
|
542 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemsMap = new HashMap<>();
|
| 23444 |
amit.gupta |
543 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
544 |
if (itemIdSchemesMap.containsKey(inventoryItem.getItemId())) {
|
| 22859 |
ashik.ali |
545 |
inventoryItemSchemsMap.put(inventoryItem, itemIdSchemesMap.get(inventoryItem.getItemId()));
|
|
|
546 |
}
|
|
|
547 |
}
|
|
|
548 |
return inventoryItemSchemsMap;
|
|
|
549 |
}
|
| 23444 |
amit.gupta |
550 |
|
|
|
551 |
private Map<FofoOrderItem, Set<Scheme>> toFofoOrderItemSchemesMap(List<Scheme> schemes,
|
|
|
552 |
List<FofoOrderItem> fofoOrderItems) {
|
| 22859 |
ashik.ali |
553 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
554 |
for (Scheme scheme : schemes) {
|
| 22859 |
ashik.ali |
555 |
schemeIds.add(scheme.getId());
|
|
|
556 |
}
|
|
|
557 |
Set<Integer> itemIds = this.fofoOrderItemsToItemIds(fofoOrderItems);
|
|
|
558 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
|
| 23444 |
amit.gupta |
559 |
|
| 22859 |
ashik.ali |
560 |
Map<FofoOrderItem, Set<Scheme>> fofoOrderItemSchemsMap = new HashMap<>();
|
| 23444 |
amit.gupta |
561 |
|
|
|
562 |
if (!schemeItems.isEmpty()) {
|
| 22859 |
ashik.ali |
563 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
|
| 23444 |
amit.gupta |
564 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
565 |
if (itemIdSchemesMap.containsKey(fofoOrderItem.getItemId())) {
|
| 22859 |
ashik.ali |
566 |
fofoOrderItemSchemsMap.put(fofoOrderItem, itemIdSchemesMap.get(fofoOrderItem.getItemId()));
|
|
|
567 |
}
|
| 22653 |
ashik.ali |
568 |
}
|
|
|
569 |
}
|
| 22859 |
ashik.ali |
570 |
return fofoOrderItemSchemsMap;
|
|
|
571 |
}
|
| 23444 |
amit.gupta |
572 |
|
| 22859 |
ashik.ali |
573 |
@Override
|
| 23365 |
ashik.ali |
574 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
| 23369 |
ashik.ali |
575 |
LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
|
| 23344 |
ashik.ali |
576 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
| 23369 |
ashik.ali |
577 |
LOGGER.info("purchase is completed = {}", (purchase.getCompleteTimestamp() != null));
|
| 23444 |
amit.gupta |
578 |
if (purchase.getCompleteTimestamp() != null) {
|
|
|
579 |
List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(purchase.getPurchaseReference(),
|
|
|
580 |
retailerId);
|
| 23378 |
ashik.ali |
581 |
LocalDateTime billingTimestamp = purchase.getCreateTimestamp();
|
| 23444 |
amit.gupta |
582 |
if (!orders.isEmpty()) {
|
| 23365 |
ashik.ali |
583 |
billingTimestamp = orders.get(0).getBillingTimestamp();
|
|
|
584 |
}
|
|
|
585 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, billingTimestamp);
|
|
|
586 |
float totalCashback = 0;
|
| 23444 |
amit.gupta |
587 |
if (schemes.isEmpty()) {
|
| 23365 |
ashik.ali |
588 |
return;
|
|
|
589 |
}
|
|
|
590 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
|
| 23444 |
amit.gupta |
591 |
|
|
|
592 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap = this.toInventoryItemSchemesMap(schemes,
|
|
|
593 |
inventoryItems);
|
|
|
594 |
|
|
|
595 |
if (inventoryItemSchemesMap.isEmpty()) {
|
| 23365 |
ashik.ali |
596 |
return;
|
|
|
597 |
}
|
|
|
598 |
Map<InventoryItem, Set<Scheme>> notAllInventoryItemSchemesMap = new HashMap<>();
|
|
|
599 |
Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
|
|
|
600 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
601 |
|
|
|
602 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
| 23365 |
ashik.ali |
603 |
Set<Scheme> notAllSchemes = new HashSet<>();
|
|
|
604 |
Set<Scheme> allSchemes = new HashSet<>();
|
| 23444 |
amit.gupta |
605 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
|
|
606 |
if (!scheme.isRetailerAll()) {
|
| 23365 |
ashik.ali |
607 |
notAllSchemes.add(scheme);
|
| 23444 |
amit.gupta |
608 |
} else {
|
| 23365 |
ashik.ali |
609 |
allSchemes.add(scheme);
|
|
|
610 |
}
|
| 22859 |
ashik.ali |
611 |
}
|
| 23365 |
ashik.ali |
612 |
notAllInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), notAllSchemes);
|
|
|
613 |
allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
|
|
|
614 |
itemIds.add(inventoryItemSchemesEntry.getKey().getItemId());
|
| 22859 |
ashik.ali |
615 |
}
|
| 23444 |
amit.gupta |
616 |
|
|
|
617 |
int itemsCount = 0;
|
|
|
618 |
for (Map.Entry<InventoryItem, Set<Scheme>> allInventoryItemSchemesEntry : allInventoryItemSchemesMap
|
|
|
619 |
.entrySet()) {
|
|
|
620 |
float inventoryItemCashback = 0;
|
|
|
621 |
for (Scheme scheme : allInventoryItemSchemesEntry.getValue()) {
|
| 23365 |
ashik.ali |
622 |
InventoryItem inventoryItem = allInventoryItemSchemesEntry.getKey();
|
|
|
623 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 23444 |
amit.gupta |
624 |
inventoryItemCashback += cashback;
|
| 23339 |
ashik.ali |
625 |
}
|
| 23444 |
amit.gupta |
626 |
if (inventoryItemCashback > 0) {
|
|
|
627 |
totalCashback += inventoryItemCashback;
|
|
|
628 |
itemsCount++;
|
|
|
629 |
}
|
| 22859 |
ashik.ali |
630 |
}
|
| 23444 |
amit.gupta |
631 |
|
| 23365 |
ashik.ali |
632 |
Set<Integer> schemeIds = this.inventoryItemSchemesMapToSchemeIds(notAllInventoryItemSchemesMap);
|
| 23444 |
amit.gupta |
633 |
if (!schemeIds.isEmpty()) {
|
| 23365 |
ashik.ali |
634 |
List<Integer> foundSchemeIds = retailerSchemeRepository.selectSchemeIds(schemeIds, retailerId);
|
| 23444 |
amit.gupta |
635 |
for (Map.Entry<InventoryItem, Set<Scheme>> notAllInventoryItemSchemesEntry : notAllInventoryItemSchemesMap
|
|
|
636 |
.entrySet()) {
|
|
|
637 |
float inventoryItemCashback = 0;
|
|
|
638 |
for (Scheme scheme : notAllInventoryItemSchemesEntry.getValue()) {
|
|
|
639 |
if (foundSchemeIds.contains(scheme.getId())) {
|
| 23365 |
ashik.ali |
640 |
InventoryItem inventoryItem = notAllInventoryItemSchemesEntry.getKey();
|
|
|
641 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 23444 |
amit.gupta |
642 |
inventoryItemCashback += cashback;
|
| 23339 |
ashik.ali |
643 |
}
|
| 22653 |
ashik.ali |
644 |
}
|
| 23444 |
amit.gupta |
645 |
if (inventoryItemCashback > 0) {
|
|
|
646 |
totalCashback += inventoryItemCashback;
|
|
|
647 |
itemsCount++;
|
|
|
648 |
}
|
| 22653 |
ashik.ali |
649 |
}
|
|
|
650 |
}
|
| 23511 |
amit.gupta |
651 |
LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
|
| 23508 |
amit.gupta |
652 |
if(itemsCount > 0) {
|
|
|
653 |
walletService.addAmountToWallet(retailerId, purchaseId, WalletReferenceType.SCHEME_IN,
|
|
|
654 |
"Added for SCHEME IN against invoice " + purchase.getPurchaseReference() + " (total " + itemsCount + " pcs)", totalCashback);
|
|
|
655 |
LOGGER.info("Added Rs.{} for SCHEME IN against invoice {} total pcs({}) {}", totalCashback, purchase.getPurchaseReference(), itemsCount);
|
|
|
656 |
purchase.setCashback(purchase.getCashback() + totalCashback);
|
|
|
657 |
purchaseRepository.persist(purchase);
|
|
|
658 |
}
|
| 22653 |
ashik.ali |
659 |
}
|
|
|
660 |
}
|
| 23444 |
amit.gupta |
661 |
|
|
|
662 |
private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem) {
|
| 23443 |
amit.gupta |
663 |
SchemeInOut schemeInOut = schemeInOutRepository.selectByScheme(scheme.getId(), inventoryItem.getId());
|
|
|
664 |
float amount = 0;
|
| 23444 |
amit.gupta |
665 |
if (schemeInOut == null) {
|
| 23443 |
amit.gupta |
666 |
schemeInOut = new SchemeInOut();
|
|
|
667 |
amount = this.getAmount(inventoryItem, scheme);
|
|
|
668 |
schemeInOut.setSchemeId(scheme.getId());
|
|
|
669 |
schemeInOut.setInventoryItemId(inventoryItem.getId());
|
|
|
670 |
schemeInOut.setAmount(amount);
|
|
|
671 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
672 |
}
|
| 22859 |
ashik.ali |
673 |
return amount;
|
|
|
674 |
}
|
| 23444 |
amit.gupta |
675 |
|
|
|
676 |
private float getAmount(InventoryItem inventoryItem, Scheme scheme) {
|
| 22653 |
ashik.ali |
677 |
float amount = 0;
|
| 22859 |
ashik.ali |
678 |
float totalTaxRate = inventoryItem.getIgstRate() + inventoryItem.getSgstRate() + inventoryItem.getCgstRate();
|
|
|
679 |
float taxableSellingPrice = inventoryItem.getUnitPrice() / (1 + totalTaxRate / 100);
|
| 23444 |
amit.gupta |
680 |
|
| 23527 |
ashik.ali |
681 |
if (scheme.getAmountType() == AmountType.PERCENTAGE) {
|
| 22859 |
ashik.ali |
682 |
amount = taxableSellingPrice * scheme.getAmount() / 100;
|
| 23444 |
amit.gupta |
683 |
} else {
|
| 22653 |
ashik.ali |
684 |
amount = scheme.getAmount();
|
|
|
685 |
}
|
| 22859 |
ashik.ali |
686 |
return amount;
|
| 22653 |
ashik.ali |
687 |
}
|
| 23444 |
amit.gupta |
688 |
|
|
|
689 |
private Map<Integer, InventoryItem> toInventoryItemIdInventoryItemMap(List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
690 |
Map<Integer, InventoryItem> itemIdInventoryItemMap = new HashMap<>();
|
| 23444 |
amit.gupta |
691 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 22859 |
ashik.ali |
692 |
itemIdInventoryItemMap.put(inventoryItem.getId(), inventoryItem);
|
| 22653 |
ashik.ali |
693 |
}
|
| 22859 |
ashik.ali |
694 |
return itemIdInventoryItemMap;
|
| 22653 |
ashik.ali |
695 |
}
|
| 23444 |
amit.gupta |
696 |
|
|
|
697 |
private Set<Integer> toInventoryItemIds(Set<FofoOrderItem> fofoOrderItems) {
|
| 22859 |
ashik.ali |
698 |
Set<Integer> inventoryItemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
699 |
// Map<Integer, Set<FofoLineItem>> fofoOrderItemIdFofoLineItemsMap =
|
|
|
700 |
// this.toFofoOrderItemIdFofoLineItems(fofoOrderItems);
|
|
|
701 |
for (FofoOrderItem fofoOrderItem : fofoOrderItems) {
|
|
|
702 |
for (FofoLineItem fofoLineItem : fofoOrderItem.getFofoLineItems()) {
|
| 22859 |
ashik.ali |
703 |
inventoryItemIds.add(fofoLineItem.getInventoryItemId());
|
|
|
704 |
}
|
|
|
705 |
}
|
|
|
706 |
return inventoryItemIds;
|
|
|
707 |
}
|
| 22653 |
ashik.ali |
708 |
|
|
|
709 |
@Override
|
| 23365 |
ashik.ali |
710 |
public void processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
|
|
|
711 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(retailerId, fofoOrderId);
|
| 22859 |
ashik.ali |
712 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.OUT, LocalDateTime.now());
|
|
|
713 |
LOGGER.info("Active Schemes {}", schemes);
|
|
|
714 |
float totalCashback = 0;
|
| 23512 |
amit.gupta |
715 |
int count = 0;
|
| 23444 |
amit.gupta |
716 |
if (schemes.isEmpty()) {
|
| 23365 |
ashik.ali |
717 |
return;
|
| 22859 |
ashik.ali |
718 |
}
|
| 23444 |
amit.gupta |
719 |
|
| 22925 |
ashik.ali |
720 |
List<FofoOrderItem> fofoOrderItems = orderService.getByOrderId(fofoOrderId);
|
| 23444 |
amit.gupta |
721 |
// LOGGER.info("fofoOrderItems {}", fofoOrderItems);
|
|
|
722 |
|
|
|
723 |
Map<FofoOrderItem, Set<Scheme>> fofoOrderItemSchemesMap = this.toFofoOrderItemSchemesMap(schemes,
|
|
|
724 |
fofoOrderItems);
|
|
|
725 |
|
| 22859 |
ashik.ali |
726 |
LOGGER.info("fofoOrderItemSchemesMap {}", fofoOrderItemSchemesMap);
|
| 23444 |
amit.gupta |
727 |
|
|
|
728 |
if (fofoOrderItemSchemesMap.isEmpty()) {
|
| 23365 |
ashik.ali |
729 |
return;
|
| 22859 |
ashik.ali |
730 |
}
|
| 23444 |
amit.gupta |
731 |
|
| 22859 |
ashik.ali |
732 |
Set<Integer> inventoryItemIds = this.toInventoryItemIds(fofoOrderItemSchemesMap.keySet());
|
| 23444 |
amit.gupta |
733 |
|
| 22859 |
ashik.ali |
734 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIds);
|
| 23444 |
amit.gupta |
735 |
|
|
|
736 |
Map<Integer, InventoryItem> inventoryItemIdInventoryItemMap = this
|
|
|
737 |
.toInventoryItemIdInventoryItemMap(inventoryItems);
|
|
|
738 |
|
| 22859 |
ashik.ali |
739 |
Map<FofoOrderItem, Set<Scheme>> notAllFofoOrderItemSchemesMap = new HashMap<>();
|
|
|
740 |
Map<FofoOrderItem, Set<Scheme>> allFofoOrderItemSchemesMap = new HashMap<>();
|
| 23444 |
amit.gupta |
741 |
|
|
|
742 |
for (Map.Entry<FofoOrderItem, Set<Scheme>> fofoOrderItemSchemesEntry : fofoOrderItemSchemesMap.entrySet()) {
|
| 22859 |
ashik.ali |
743 |
Set<Scheme> notAllSchemes = new HashSet<>();
|
|
|
744 |
Set<Scheme> allSchemes = new HashSet<>();
|
| 23444 |
amit.gupta |
745 |
for (Scheme scheme : fofoOrderItemSchemesEntry.getValue()) {
|
|
|
746 |
if (!scheme.isRetailerAll()) {
|
| 22859 |
ashik.ali |
747 |
notAllSchemes.add(scheme);
|
| 23444 |
amit.gupta |
748 |
} else {
|
| 22859 |
ashik.ali |
749 |
allSchemes.add(scheme);
|
|
|
750 |
}
|
| 22653 |
ashik.ali |
751 |
}
|
| 22859 |
ashik.ali |
752 |
notAllFofoOrderItemSchemesMap.put(fofoOrderItemSchemesEntry.getKey(), notAllSchemes);
|
|
|
753 |
allFofoOrderItemSchemesMap.put(fofoOrderItemSchemesEntry.getKey(), allSchemes);
|
| 22653 |
ashik.ali |
754 |
}
|
| 23444 |
amit.gupta |
755 |
|
|
|
756 |
for (Map.Entry<FofoOrderItem, Set<Scheme>> allFofoOrderItemSchemesEntry : allFofoOrderItemSchemesMap
|
|
|
757 |
.entrySet()) {
|
|
|
758 |
for (FofoLineItem fofoLineItem : allFofoOrderItemSchemesEntry.getKey().getFofoLineItems()) {
|
|
|
759 |
for (Scheme scheme : allFofoOrderItemSchemesEntry.getValue()) {
|
|
|
760 |
float cashback = this.createSchemeInOut(scheme,
|
|
|
761 |
inventoryItemIdInventoryItemMap.get(fofoLineItem.getInventoryItemId()));
|
| 23512 |
amit.gupta |
762 |
if(cashback > 0) {
|
|
|
763 |
totalCashback = totalCashback + cashback;
|
|
|
764 |
count += 1;
|
|
|
765 |
}
|
| 22859 |
ashik.ali |
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;
|
| 23512 |
amit.gupta |
783 |
if(cashback > 0) {
|
|
|
784 |
totalCashback = totalCashback + cashback;
|
|
|
785 |
count += 1;
|
|
|
786 |
}
|
| 22653 |
ashik.ali |
787 |
}
|
|
|
788 |
}
|
|
|
789 |
}
|
| 23444 |
amit.gupta |
790 |
}
|
| 22653 |
ashik.ali |
791 |
}
|
| 23444 |
amit.gupta |
792 |
if (totalCashback > 0) {
|
|
|
793 |
walletService.addAmountToWallet(retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT,
|
| 23512 |
amit.gupta |
794 |
"Sales margin for invoice number " + fofoOrder.getInvoiceNumber() + ". Total " + count + " pc(s)", totalCashback);
|
| 23365 |
ashik.ali |
795 |
fofoOrder.setCashback(totalCashback);
|
|
|
796 |
fofoOrderRepository.persist(fofoOrder);
|
| 22859 |
ashik.ali |
797 |
}
|
| 22653 |
ashik.ali |
798 |
}
|
| 23444 |
amit.gupta |
799 |
|
| 23508 |
amit.gupta |
800 |
@Override
|
|
|
801 |
public void rollbackSchemeForInventoryItemId(int inventoryItemId) {
|
|
|
802 |
/*Set<Integer> inventoryItemIds = new HashSet<>();
|
|
|
803 |
inventoryItemIds.add(inventoryItemId);
|
|
|
804 |
float amountToRollback = 0;
|
|
|
805 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIds);
|
|
|
806 |
for(SchemeInOut schemeInOut: schemes) {
|
|
|
807 |
schemeInOut.setRolled_back_timestamp(LocalDateTime.now());
|
|
|
808 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
809 |
amountToRollback += schemeInOut.getAmount();
|
|
|
810 |
}*/
|
|
|
811 |
//TODO: Unused
|
|
|
812 |
}
|
|
|
813 |
|
|
|
814 |
@Override
|
|
|
815 |
public void rollbackSchemes(List<InventoryItem> inventoryItems, int rollbackReference, String rollbackReason) {
|
|
|
816 |
Set<Integer> inventoryItemIdSet = new HashSet<>(inventoryItems.stream().map(x->x.getId()).collect(Collectors.toList()));
|
|
|
817 |
float amountToRollback = 0;
|
|
|
818 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIdSet);
|
|
|
819 |
for(SchemeInOut schemeInOut: schemes) {
|
| 23527 |
ashik.ali |
820 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
| 23508 |
amit.gupta |
821 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
822 |
amountToRollback += schemeInOut.getAmount();
|
|
|
823 |
}
|
|
|
824 |
walletService.rollbackAmountFromWallet(inventoryItems.get(0).getFofoId(), amountToRollback, rollbackReference, WalletReferenceType.SCHEME_IN, rollbackReason);
|
|
|
825 |
}
|
|
|
826 |
|
| 22653 |
ashik.ali |
827 |
}
|