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