| 22653 |
ashik.ali |
1 |
package com.spice.profitmandi.service.scheme;
|
|
|
2 |
|
| 26403 |
amit.gupta |
3 |
import java.text.MessageFormat;
|
| 27877 |
amit.gupta |
4 |
import java.time.LocalDate;
|
| 22859 |
ashik.ali |
5 |
import java.time.LocalDateTime;
|
| 28969 |
amit.gupta |
6 |
import java.time.YearMonth;
|
| 23019 |
ashik.ali |
7 |
import java.util.ArrayList;
|
| 26722 |
amit.gupta |
8 |
import java.util.Arrays;
|
| 27395 |
amit.gupta |
9 |
import java.util.Comparator;
|
| 22653 |
ashik.ali |
10 |
import java.util.HashMap;
|
| 22859 |
ashik.ali |
11 |
import java.util.HashSet;
|
| 22653 |
ashik.ali |
12 |
import java.util.List;
|
|
|
13 |
import java.util.Map;
|
|
|
14 |
import java.util.Set;
|
| 23968 |
amit.gupta |
15 |
import java.util.stream.Collectors;
|
| 22653 |
ashik.ali |
16 |
|
| 26332 |
amit.gupta |
17 |
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
18 |
import javax.persistence.criteria.CriteriaQuery;
|
|
|
19 |
import javax.persistence.criteria.Predicate;
|
|
|
20 |
import javax.persistence.criteria.Root;
|
|
|
21 |
|
| 23781 |
ashik.ali |
22 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
23 |
import org.apache.logging.log4j.Logger;
|
| 26332 |
amit.gupta |
24 |
import org.hibernate.Session;
|
|
|
25 |
import org.hibernate.SessionFactory;
|
|
|
26 |
import org.hibernate.query.Query;
|
| 22653 |
ashik.ali |
27 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 23781 |
ashik.ali |
28 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 26684 |
amit.gupta |
29 |
import org.springframework.cache.annotation.Cacheable;
|
| 22653 |
ashik.ali |
30 |
import org.springframework.stereotype.Component;
|
|
|
31 |
|
| 24307 |
amit.gupta |
32 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
| 22653 |
ashik.ali |
33 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
34 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 23019 |
ashik.ali |
35 |
import com.spice.profitmandi.common.model.SchemeModel;
|
| 27395 |
amit.gupta |
36 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
| 22859 |
ashik.ali |
37 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 23339 |
ashik.ali |
38 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 22653 |
ashik.ali |
39 |
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
|
|
|
40 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
| 27395 |
amit.gupta |
41 |
import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
|
| 23365 |
ashik.ali |
42 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 22653 |
ashik.ali |
43 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 25503 |
amit.gupta |
44 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
| 23339 |
ashik.ali |
45 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 25043 |
amit.gupta |
46 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
| 22859 |
ashik.ali |
47 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
|
|
48 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
| 26498 |
amit.gupta |
49 |
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
|
| 23527 |
ashik.ali |
50 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 22653 |
ashik.ali |
51 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 25111 |
amit.gupta |
52 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| 27377 |
amit.gupta |
53 |
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
|
| 25503 |
amit.gupta |
54 |
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
|
| 22859 |
ashik.ali |
55 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 22653 |
ashik.ali |
56 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 23995 |
amit.gupta |
57 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 23968 |
amit.gupta |
58 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 27395 |
amit.gupta |
59 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
| 23019 |
ashik.ali |
60 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
|
|
61 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
| 23365 |
ashik.ali |
62 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| 22653 |
ashik.ali |
63 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 25503 |
amit.gupta |
64 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
|
| 23339 |
ashik.ali |
65 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 25043 |
amit.gupta |
66 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
| 22859 |
ashik.ali |
67 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
|
|
68 |
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
|
| 26498 |
amit.gupta |
69 |
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
|
| 23798 |
amit.gupta |
70 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| 26722 |
amit.gupta |
71 |
import com.spice.profitmandi.service.inventory.PurchaseService;
|
| 22859 |
ashik.ali |
72 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 22653 |
ashik.ali |
73 |
|
| 22859 |
ashik.ali |
74 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
75 |
|
| 22653 |
ashik.ali |
76 |
@Component
|
|
|
77 |
public class SchemeServiceImpl implements SchemeService {
|
|
|
78 |
|
| 23568 |
govind |
79 |
private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
|
| 23444 |
amit.gupta |
80 |
|
| 22653 |
ashik.ali |
81 |
@Autowired
|
| 23781 |
ashik.ali |
82 |
@Qualifier("fofoInventoryItemRepository")
|
| 22653 |
ashik.ali |
83 |
private InventoryItemRepository inventoryItemRepository;
|
| 23444 |
amit.gupta |
84 |
|
| 22653 |
ashik.ali |
85 |
@Autowired
|
| 27395 |
amit.gupta |
86 |
private ActivatedImeiRepository activatedImeiRepository;
|
| 27898 |
amit.gupta |
87 |
|
| 27395 |
amit.gupta |
88 |
@Autowired
|
| 25503 |
amit.gupta |
89 |
private PartnerTypeChangeService partnerTypeChangeService;
|
|
|
90 |
|
|
|
91 |
@Autowired
|
| 26722 |
amit.gupta |
92 |
private PurchaseService purchaseService;
|
|
|
93 |
|
|
|
94 |
@Autowired
|
| 25043 |
amit.gupta |
95 |
private ScanRecordRepository scanRecordRepository;
|
| 23444 |
amit.gupta |
96 |
|
| 22653 |
ashik.ali |
97 |
@Autowired
|
| 26332 |
amit.gupta |
98 |
private SessionFactory sessionFactory;
|
|
|
99 |
|
| 26722 |
amit.gupta |
100 |
private Set<Integer> tagIds = new HashSet<Integer>(Arrays.asList(4));
|
|
|
101 |
|
| 26332 |
amit.gupta |
102 |
@Autowired
|
| 22653 |
ashik.ali |
103 |
private SchemeRepository schemeRepository;
|
| 26684 |
amit.gupta |
104 |
|
| 26498 |
amit.gupta |
105 |
@Autowired
|
|
|
106 |
private PriceDropRepository priceDropRepository;
|
| 23444 |
amit.gupta |
107 |
|
| 22653 |
ashik.ali |
108 |
@Autowired
|
| 23798 |
amit.gupta |
109 |
private RoleManager roleManager;
|
|
|
110 |
|
|
|
111 |
@Autowired
|
| 22859 |
ashik.ali |
112 |
private RetailerRepository retailerRepository;
|
| 24562 |
amit.gupta |
113 |
|
| 23995 |
amit.gupta |
114 |
@Autowired
|
|
|
115 |
private TagListingRepository tagListingRepository;
|
| 23444 |
amit.gupta |
116 |
|
| 22859 |
ashik.ali |
117 |
@Autowired
|
|
|
118 |
private SchemeInOutRepository schemeInOutRepository;
|
| 23444 |
amit.gupta |
119 |
|
| 22653 |
ashik.ali |
120 |
@Autowired
|
| 23781 |
ashik.ali |
121 |
@Qualifier("catalogItemRepository")
|
| 22859 |
ashik.ali |
122 |
private ItemRepository itemRepository;
|
| 23444 |
amit.gupta |
123 |
|
| 22859 |
ashik.ali |
124 |
@Autowired
|
|
|
125 |
private SchemeItemRepository schemeItemRepository;
|
| 23444 |
amit.gupta |
126 |
|
| 22859 |
ashik.ali |
127 |
@Autowired
|
|
|
128 |
private WalletService walletService;
|
| 23444 |
amit.gupta |
129 |
|
| 23019 |
ashik.ali |
130 |
@Autowired
|
|
|
131 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
| 23444 |
amit.gupta |
132 |
|
| 23019 |
ashik.ali |
133 |
@Autowired
|
|
|
134 |
private FofoLineItemRepository fofoLineItemRepository;
|
| 23444 |
amit.gupta |
135 |
|
| 23339 |
ashik.ali |
136 |
@Autowired
|
|
|
137 |
private PurchaseRepository purchaseRepository;
|
| 23444 |
amit.gupta |
138 |
|
| 23344 |
ashik.ali |
139 |
@Autowired
|
| 23365 |
ashik.ali |
140 |
private FofoOrderRepository fofoOrderRepository;
|
| 23796 |
amit.gupta |
141 |
|
| 22653 |
ashik.ali |
142 |
@Override
|
| 22859 |
ashik.ali |
143 |
public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
| 23444 |
amit.gupta |
144 |
|
| 23019 |
ashik.ali |
145 |
this.validateCreateSchemeRequest(createSchemeRequest);
|
| 23444 |
amit.gupta |
146 |
|
| 22859 |
ashik.ali |
147 |
Scheme scheme = this.toScheme(creatorId, createSchemeRequest);
|
| 23444 |
amit.gupta |
148 |
|
|
|
149 |
if (scheme.getStartDateTime().isAfter(scheme.getEndDateTime())) {
|
|
|
150 |
throw new ProfitMandiBusinessException(
|
|
|
151 |
ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE,
|
|
|
152 |
scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
|
| 22653 |
ashik.ali |
153 |
}
|
| 23444 |
amit.gupta |
154 |
|
| 26332 |
amit.gupta |
155 |
// this.validateItemIds(createSchemeRequest);
|
| 22859 |
ashik.ali |
156 |
schemeRepository.persist(scheme);
|
| 23444 |
amit.gupta |
157 |
for (int itemId : createSchemeRequest.getItemIds()) {
|
| 22859 |
ashik.ali |
158 |
SchemeItem schemeItem = new SchemeItem();
|
|
|
159 |
schemeItem.setSchemeId(scheme.getId());
|
|
|
160 |
schemeItem.setItemId(itemId);
|
|
|
161 |
schemeItemRepository.persist(schemeItem);
|
|
|
162 |
}
|
| 23444 |
amit.gupta |
163 |
|
| 22653 |
ashik.ali |
164 |
}
|
| 23444 |
amit.gupta |
165 |
|
|
|
166 |
private void validateCreateSchemeRequest(CreateSchemeRequest createSchemeRequest)
|
|
|
167 |
throws ProfitMandiBusinessException {
|
|
|
168 |
if (createSchemeRequest.getName() == null || createSchemeRequest.getName().isEmpty()) {
|
|
|
169 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, createSchemeRequest.getName(),
|
|
|
170 |
"SCHM_VE_1000");
|
| 23019 |
ashik.ali |
171 |
}
|
| 23444 |
amit.gupta |
172 |
if (createSchemeRequest.getAmount() <= 0) {
|
|
|
173 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
174 |
"SCHM_VE_1001");
|
| 23019 |
ashik.ali |
175 |
}
|
| 23444 |
amit.gupta |
176 |
|
| 25503 |
amit.gupta |
177 |
if (createSchemeRequest.getAmountType().equals(AmountType.PERCENTAGE)
|
| 23444 |
amit.gupta |
178 |
&& createSchemeRequest.getAmount() > 100) {
|
|
|
179 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
180 |
"SCHM_VE_1002");
|
| 23019 |
ashik.ali |
181 |
}
|
| 23444 |
amit.gupta |
182 |
|
| 23886 |
amit.gupta |
183 |
if (createSchemeRequest.getStartDate() == null) {
|
| 23983 |
amit.gupta |
184 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, createSchemeRequest.getStartDate(),
|
|
|
185 |
"SCHM_VE_1003");
|
| 23019 |
ashik.ali |
186 |
}
|
| 23886 |
amit.gupta |
187 |
if (createSchemeRequest.getEndDate() == null) {
|
| 23983 |
amit.gupta |
188 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, createSchemeRequest.getEndDate(),
|
|
|
189 |
"SCHM_VE_1004");
|
| 23019 |
ashik.ali |
190 |
}
|
|
|
191 |
}
|
| 23444 |
amit.gupta |
192 |
|
|
|
193 |
private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest) {
|
| 22859 |
ashik.ali |
194 |
Scheme scheme = new Scheme();
|
|
|
195 |
scheme.setName(createSchemeRequest.getName());
|
|
|
196 |
scheme.setDescription(createSchemeRequest.getDescription());
|
| 25503 |
amit.gupta |
197 |
scheme.setType(createSchemeRequest.getType());
|
|
|
198 |
scheme.setAmountType(createSchemeRequest.getAmountType());
|
| 22859 |
ashik.ali |
199 |
scheme.setAmount(createSchemeRequest.getAmount());
|
| 25503 |
amit.gupta |
200 |
scheme.setPartnerType(createSchemeRequest.getPartnerType());
|
| 25517 |
amit.gupta |
201 |
scheme.setStartDateTime(createSchemeRequest.getStartDate());
|
| 23886 |
amit.gupta |
202 |
scheme.setEndDateTime(createSchemeRequest.getEndDate());
|
| 22859 |
ashik.ali |
203 |
scheme.setCreatedBy(creatorId);
|
| 28464 |
amit.gupta |
204 |
scheme.setCashback(createSchemeRequest.isCashback());
|
| 22859 |
ashik.ali |
205 |
return scheme;
|
|
|
206 |
}
|
| 23444 |
amit.gupta |
207 |
|
|
|
208 |
private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
|
|
209 |
if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
|
|
|
210 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
211 |
"SCHM_1003");
|
| 22859 |
ashik.ali |
212 |
}
|
| 23444 |
amit.gupta |
213 |
List<Integer> foundItemIds = itemRepository.selectIdsByIdsAndType(createSchemeRequest.getItemIds(),
|
|
|
214 |
ItemType.SERIALIZED);
|
|
|
215 |
if (foundItemIds.size() != createSchemeRequest.getItemIds().size()) {
|
| 22859 |
ashik.ali |
216 |
createSchemeRequest.getItemIds().removeAll(foundItemIds);
|
| 23444 |
amit.gupta |
217 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
218 |
"SCHM_1004");
|
| 22859 |
ashik.ali |
219 |
}
|
|
|
220 |
}
|
| 23444 |
amit.gupta |
221 |
|
| 22859 |
ashik.ali |
222 |
@Override
|
|
|
223 |
public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
224 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
|
|
225 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23914 |
govind |
226 |
if (itemIds.size() > 0) {
|
|
|
227 |
List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
|
|
|
228 |
scheme.setItemStringMap(this.toItemStringMap(items));
|
| 23983 |
amit.gupta |
229 |
}
|
| 22859 |
ashik.ali |
230 |
return scheme;
|
|
|
231 |
}
|
| 23444 |
amit.gupta |
232 |
|
|
|
233 |
public Map<Integer, String> toItemStringMap(List<Item> items) {
|
| 23339 |
ashik.ali |
234 |
Map<Integer, String> itemMap = new HashMap<>();
|
| 23444 |
amit.gupta |
235 |
for (Item item : items) {
|
| 23339 |
ashik.ali |
236 |
itemMap.put(item.getId(), this.getItemString(item));
|
|
|
237 |
}
|
|
|
238 |
return itemMap;
|
|
|
239 |
}
|
| 23444 |
amit.gupta |
240 |
|
|
|
241 |
public String getItemString(Item item) {
|
| 23339 |
ashik.ali |
242 |
StringBuilder itemString = new StringBuilder();
|
| 23444 |
amit.gupta |
243 |
if (item.getBrand() != null && !item.getBrand().isEmpty()) {
|
| 23339 |
ashik.ali |
244 |
itemString.append(item.getBrand().trim());
|
|
|
245 |
}
|
|
|
246 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
247 |
if (item.getModelName() != null && !item.getModelName().isEmpty()) {
|
| 23339 |
ashik.ali |
248 |
itemString.append(item.getModelName().trim());
|
|
|
249 |
}
|
|
|
250 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
251 |
if (item.getModelNumber() != null && !item.getModelNumber().isEmpty()) {
|
| 23339 |
ashik.ali |
252 |
itemString.append(item.getModelNumber().trim());
|
|
|
253 |
}
|
|
|
254 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
255 |
if (item.getColor() != null && !item.getColor().isEmpty()) {
|
| 23339 |
ashik.ali |
256 |
itemString.append(item.getColor().trim());
|
|
|
257 |
}
|
|
|
258 |
return itemString.toString();
|
|
|
259 |
}
|
| 23444 |
amit.gupta |
260 |
|
|
|
261 |
private Set<Integer> schemeItemsToItemIds(List<SchemeItem> schemeItems) {
|
| 23339 |
ashik.ali |
262 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
263 |
for (SchemeItem schemeItem : schemeItems) {
|
| 23339 |
ashik.ali |
264 |
itemIds.add(schemeItem.getItemId());
|
|
|
265 |
}
|
|
|
266 |
return itemIds;
|
|
|
267 |
}
|
| 23444 |
amit.gupta |
268 |
|
| 23019 |
ashik.ali |
269 |
@Override
|
|
|
270 |
public List<SchemeModel> getAllSchemeModels(LocalDateTime startDateTime, LocalDateTime endDateTime) {
|
|
|
271 |
List<Scheme> schemes = schemeRepository.selectAllBetweenCreateTimestamp(startDateTime, endDateTime);
|
|
|
272 |
Map<Integer, Scheme> schemeIdSchemeMap = this.toSchemeIdSchemeMap(schemes);
|
|
|
273 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
| 23339 |
ashik.ali |
274 |
Set<Integer> itemIds = this.schemeItemsToItemIds(schemeItems);
|
|
|
275 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
|
|
276 |
Map<Integer, String> itemStringMap = this.toItemStringMap(items);
|
|
|
277 |
this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
|
| 23019 |
ashik.ali |
278 |
return this.toSchemeModels(schemeIdSchemeMap);
|
|
|
279 |
}
|
| 23444 |
amit.gupta |
280 |
|
|
|
281 |
private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
|
|
|
282 |
Map<Integer, String> itemStringMap) {
|
|
|
283 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
284 |
schemeIdSchemeMap.get(schemeItem.getSchemeId()).getItemStringMap().put(schemeItem.getItemId(),
|
|
|
285 |
itemStringMap.get(schemeItem.getItemId()));
|
| 23019 |
ashik.ali |
286 |
}
|
|
|
287 |
}
|
| 23444 |
amit.gupta |
288 |
|
|
|
289 |
private List<SchemeModel> toSchemeModels(Map<Integer, Scheme> schemeIdSchemeMap) {
|
| 23019 |
ashik.ali |
290 |
List<SchemeModel> schemeModels = new ArrayList<>();
|
| 23444 |
amit.gupta |
291 |
for (Map.Entry<Integer, Scheme> schemeIdSchemeEntry : schemeIdSchemeMap.entrySet()) {
|
| 23019 |
ashik.ali |
292 |
schemeModels.add(this.toSchemeModel(schemeIdSchemeEntry.getValue()));
|
|
|
293 |
}
|
|
|
294 |
return schemeModels;
|
|
|
295 |
}
|
| 23444 |
amit.gupta |
296 |
|
|
|
297 |
private SchemeModel toSchemeModel(Scheme scheme) {
|
| 23019 |
ashik.ali |
298 |
SchemeModel schemeModel = new SchemeModel();
|
|
|
299 |
schemeModel.setSchemeId(scheme.getId());
|
|
|
300 |
schemeModel.setName(scheme.getName());
|
|
|
301 |
schemeModel.setDescription(scheme.getDescription());
|
|
|
302 |
schemeModel.setSchemeType(scheme.getType().toString());
|
|
|
303 |
schemeModel.setAmountType(scheme.getAmountType().toString());
|
|
|
304 |
schemeModel.setAmount(scheme.getAmount());
|
|
|
305 |
schemeModel.setStartDateTime(StringUtils.toString(scheme.getStartDateTime()));
|
|
|
306 |
schemeModel.setEndDateTime(StringUtils.toString(scheme.getEndDateTime()));
|
|
|
307 |
schemeModel.setCreateTimestamp(StringUtils.toString(scheme.getCreateTimestamp()));
|
|
|
308 |
schemeModel.setActiveTimestamp(StringUtils.toString(scheme.getActiveTimestamp()));
|
|
|
309 |
schemeModel.setExpireTimestamp(StringUtils.toString(scheme.getExpireTimestamp()));
|
|
|
310 |
schemeModel.setCreatedBy(scheme.getCreatedBy());
|
| 23339 |
ashik.ali |
311 |
schemeModel.setItemStringMap(scheme.getItemStringMap());
|
| 23019 |
ashik.ali |
312 |
schemeModel.setRetailerIds(scheme.getRetailerIds());
|
|
|
313 |
return schemeModel;
|
|
|
314 |
}
|
| 22653 |
ashik.ali |
315 |
|
|
|
316 |
@Override
|
|
|
317 |
public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
318 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 23444 |
amit.gupta |
319 |
if (scheme.getActiveTimestamp() != null) {
|
|
|
320 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
321 |
"SCHM_1005");
|
| 22653 |
ashik.ali |
322 |
}
|
| 23444 |
amit.gupta |
323 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
324 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
325 |
"SCHM_1006");
|
| 22653 |
ashik.ali |
326 |
}
|
| 22859 |
ashik.ali |
327 |
scheme.setActiveTimestamp(LocalDateTime.now());
|
|
|
328 |
schemeRepository.persist(scheme);
|
| 25438 |
amit.gupta |
329 |
/*
|
|
|
330 |
* if (scheme.getType() == SchemeType.IN) {
|
|
|
331 |
* this.processPreviousPurchases(scheme); } else if (scheme.getType() ==
|
|
|
332 |
* SchemeType.OUT) { this.processPreviousSales(scheme); }
|
|
|
333 |
*/
|
| 22653 |
ashik.ali |
334 |
}
|
| 23444 |
amit.gupta |
335 |
|
| 22653 |
ashik.ali |
336 |
@Override
|
| 25069 |
amit.gupta |
337 |
public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
|
| 22653 |
ashik.ali |
338 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 25111 |
amit.gupta |
339 |
if (scheme == null || scheme.getActiveTimestamp() == null) {
|
| 23444 |
amit.gupta |
340 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
341 |
"SCHM_1007");
|
| 22653 |
ashik.ali |
342 |
}
|
| 23444 |
amit.gupta |
343 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
344 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
345 |
"SCHM_1008");
|
| 22653 |
ashik.ali |
346 |
}
|
| 22859 |
ashik.ali |
347 |
scheme.setExpireTimestamp(LocalDateTime.now());
|
| 28749 |
amit.gupta |
348 |
if(expiryTime.isAfter(scheme.getEndDateTime())) {
|
|
|
349 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
350 |
"End Date cant be extended during expiry");
|
|
|
351 |
}
|
| 25069 |
amit.gupta |
352 |
scheme.setEndDateTime(expiryTime);
|
| 22859 |
ashik.ali |
353 |
schemeRepository.persist(scheme);
|
| 22653 |
ashik.ali |
354 |
}
|
| 23444 |
amit.gupta |
355 |
|
|
|
356 |
private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
357 |
Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
|
| 23444 |
amit.gupta |
358 |
for (Scheme scheme : schemes) {
|
| 23019 |
ashik.ali |
359 |
schemeIdSchemeMap.put(scheme.getId(), scheme);
|
| 22859 |
ashik.ali |
360 |
}
|
| 23019 |
ashik.ali |
361 |
return schemeIdSchemeMap;
|
| 22859 |
ashik.ali |
362 |
}
|
| 23444 |
amit.gupta |
363 |
|
|
|
364 |
private Set<Integer> inventoryItemsToItemIds(List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
365 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
366 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 22859 |
ashik.ali |
367 |
itemIds.add(inventoryItem.getItemId());
|
|
|
368 |
}
|
|
|
369 |
return itemIds;
|
|
|
370 |
}
|
| 23444 |
amit.gupta |
371 |
|
|
|
372 |
private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
373 |
Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemeMap(schemes);
|
| 22859 |
ashik.ali |
374 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
|
| 23444 |
amit.gupta |
375 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
376 |
if (!itemIdSchemesMap.containsKey(schemeItem.getItemId())) {
|
| 22859 |
ashik.ali |
377 |
Set<Scheme> schemesSet = new HashSet<>();
|
|
|
378 |
schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
379 |
itemIdSchemesMap.put(schemeItem.getItemId(), schemesSet);
|
| 23444 |
amit.gupta |
380 |
} else {
|
| 22859 |
ashik.ali |
381 |
itemIdSchemesMap.get(schemeItem.getItemId()).add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
382 |
}
|
|
|
383 |
}
|
|
|
384 |
return itemIdSchemesMap;
|
|
|
385 |
}
|
| 22653 |
ashik.ali |
386 |
|
| 23444 |
amit.gupta |
387 |
private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
|
|
|
388 |
List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
389 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
390 |
for (Scheme scheme : schemes) {
|
| 22859 |
ashik.ali |
391 |
schemeIds.add(scheme.getId());
|
|
|
392 |
}
|
|
|
393 |
Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
|
|
|
394 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
|
| 23444 |
amit.gupta |
395 |
|
| 22859 |
ashik.ali |
396 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
|
|
|
397 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemsMap = new HashMap<>();
|
| 23444 |
amit.gupta |
398 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
399 |
if (itemIdSchemesMap.containsKey(inventoryItem.getItemId())) {
|
| 22859 |
ashik.ali |
400 |
inventoryItemSchemsMap.put(inventoryItem, itemIdSchemesMap.get(inventoryItem.getItemId()));
|
|
|
401 |
}
|
|
|
402 |
}
|
|
|
403 |
return inventoryItemSchemsMap;
|
|
|
404 |
}
|
| 23444 |
amit.gupta |
405 |
|
| 22859 |
ashik.ali |
406 |
@Override
|
| 23365 |
ashik.ali |
407 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
| 23369 |
ashik.ali |
408 |
LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
|
| 23344 |
ashik.ali |
409 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
| 28969 |
amit.gupta |
410 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId, YearMonth.from(purchase.getCreateTimestamp()));
|
| 27898 |
amit.gupta |
411 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, partnerType,
|
|
|
412 |
purchase.getCreateTimestamp(), false);
|
|
|
413 |
float totalCashback = 0;
|
|
|
414 |
if (schemes.isEmpty()) {
|
|
|
415 |
return;
|
|
|
416 |
}
|
|
|
417 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
|
| 26332 |
amit.gupta |
418 |
|
| 27898 |
amit.gupta |
419 |
Set<Integer> itemIds = inventoryItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
420 |
LocalDateTime billingDate = purchaseService.getBillingDateOfPurchase(purchaseId);
|
|
|
421 |
Set<Integer> itemIdsSet = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds).stream()
|
|
|
422 |
.filter(x -> x.getEolDate() == null || x.getEolDate().isAfter(billingDate)).map(x -> x.getItemId())
|
|
|
423 |
.collect(Collectors.toSet());
|
|
|
424 |
// Only consider inventory items that were not returned
|
|
|
425 |
inventoryItems = inventoryItems.stream().filter(x -> itemIdsSet.contains(x.getItemId()))
|
|
|
426 |
.filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET_BAD))
|
|
|
427 |
.filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET)).collect(Collectors.toList());
|
|
|
428 |
LOGGER.info(inventoryItems);
|
|
|
429 |
if (inventoryItems.size() == 0)
|
|
|
430 |
return;
|
|
|
431 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap = this.toInventoryItemSchemesMap(schemes,
|
|
|
432 |
inventoryItems);
|
| 23444 |
amit.gupta |
433 |
|
| 27898 |
amit.gupta |
434 |
if (inventoryItemSchemesMap.isEmpty()) {
|
|
|
435 |
return;
|
|
|
436 |
}
|
|
|
437 |
Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
|
| 23444 |
amit.gupta |
438 |
|
| 27898 |
amit.gupta |
439 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
|
|
440 |
Set<Scheme> allSchemes = new HashSet<>();
|
|
|
441 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
|
|
442 |
allSchemes.add(scheme);
|
| 22859 |
ashik.ali |
443 |
}
|
| 27898 |
amit.gupta |
444 |
allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
|
|
|
445 |
}
|
| 23444 |
amit.gupta |
446 |
|
| 27898 |
amit.gupta |
447 |
//
|
| 26722 |
amit.gupta |
448 |
|
| 27898 |
amit.gupta |
449 |
int itemsCount = 0;
|
|
|
450 |
for (Map.Entry<InventoryItem, Set<Scheme>> allInventoryItemSchemesEntry : allInventoryItemSchemesMap
|
|
|
451 |
.entrySet()) {
|
|
|
452 |
float inventoryItemCashback = 0;
|
|
|
453 |
for (Scheme scheme : allInventoryItemSchemesEntry.getValue()) {
|
|
|
454 |
InventoryItem inventoryItem = allInventoryItemSchemesEntry.getKey();
|
|
|
455 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
|
|
456 |
inventoryItemCashback += cashback;
|
| 22859 |
ashik.ali |
457 |
}
|
| 27898 |
amit.gupta |
458 |
if (inventoryItemCashback > 0) {
|
|
|
459 |
totalCashback += inventoryItemCashback;
|
|
|
460 |
itemsCount++;
|
| 23508 |
amit.gupta |
461 |
}
|
| 22653 |
ashik.ali |
462 |
}
|
| 27898 |
amit.gupta |
463 |
|
|
|
464 |
LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
|
|
|
465 |
if (itemsCount > 0) {
|
|
|
466 |
walletService.addAmountToWallet(
|
|
|
467 |
retailerId, purchaseId, WalletReferenceType.SCHEME_IN, "Added for SCHEME IN against invoice "
|
|
|
468 |
+ purchase.getPurchaseReference() + " (total " + itemsCount + " pcs)",
|
|
|
469 |
totalCashback, purchase.getCreateTimestamp());
|
|
|
470 |
LOGGER.info("Added Rs.{} for SCHEME IN against invoice {} total pcs({}) {}", totalCashback,
|
|
|
471 |
purchase.getPurchaseReference(), itemsCount);
|
|
|
472 |
purchase.setCashback(purchase.getCashback() + totalCashback);
|
|
|
473 |
purchaseRepository.persist(purchase);
|
|
|
474 |
}
|
| 22653 |
ashik.ali |
475 |
}
|
| 23444 |
amit.gupta |
476 |
|
|
|
477 |
private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem) {
|
| 28969 |
amit.gupta |
478 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectAll(inventoryItem.getId());
|
| 27377 |
amit.gupta |
479 |
float amountToCredit = 0;
|
| 24581 |
amit.gupta |
480 |
if (schemeInOuts.stream().filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList())
|
| 24562 |
amit.gupta |
481 |
.size() == 0) {
|
|
|
482 |
SchemeInOut schemeInOut = new SchemeInOut();
|
| 27377 |
amit.gupta |
483 |
amountToCredit = this.getAmount(inventoryItem, scheme);
|
| 23443 |
amit.gupta |
484 |
schemeInOut.setSchemeId(scheme.getId());
|
|
|
485 |
schemeInOut.setInventoryItemId(inventoryItem.getId());
|
| 27377 |
amit.gupta |
486 |
schemeInOut.setAmount(amountToCredit);
|
| 27433 |
amit.gupta |
487 |
schemeInOutRepository.persist(schemeInOut);
|
| 27898 |
amit.gupta |
488 |
if (scheme.getType().equals(SchemeType.ACTIVATION)) {
|
| 27377 |
amit.gupta |
489 |
schemeInOut.setStatus(SchemePayoutStatus.PENDING);
|
|
|
490 |
schemeInOut.setStatusDescription("Activation pending for IMEI#" + inventoryItem.getSerialNumber());
|
|
|
491 |
return 0;
|
| 27898 |
amit.gupta |
492 |
} else if (scheme.getType().equals(SchemeType.INVESTMENT)) {
|
|
|
493 |
schemeInOut.setStatus(SchemePayoutStatus.PENDING);
|
|
|
494 |
schemeInOut.setStatusDescription("Subject to investment days maintained");
|
|
|
495 |
return 0;
|
| 27377 |
amit.gupta |
496 |
} else {
|
|
|
497 |
schemeInOut.setStatus(SchemePayoutStatus.CREDITED);
|
| 27898 |
amit.gupta |
498 |
if (scheme.getType().equals(SchemeType.IN)) {
|
| 27713 |
amit.gupta |
499 |
schemeInOut.setStatusDescription("Credited for GRN of IMEI#" + inventoryItem.getSerialNumber());
|
| 28749 |
amit.gupta |
500 |
} else if(scheme.getType().equals(SchemeType.OUT) || scheme.getType().equals(SchemeType.CATEGORY)){
|
| 28737 |
amit.gupta |
501 |
schemeInOut.setStatusDescription("Credited for sale of IMEI#" + inventoryItem.getSerialNumber());
|
| 28969 |
amit.gupta |
502 |
if(scheme.getType().equals(SchemeType.CATEGORY)) {
|
|
|
503 |
|
|
|
504 |
List<SchemeInOut> sios = schemeInOutRepository.selectAllByType(SchemeType.CATEGORY, inventoryItem.getId());
|
|
|
505 |
SchemeInOut sio = sios.stream().filter(x->x.getId()!=schemeInOut.getId()).findFirst().orElse(null);
|
|
|
506 |
if(sio != null) {
|
|
|
507 |
sio.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
508 |
sio.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
509 |
sio.setStatusDescription("Category upgraded, new entry added");
|
|
|
510 |
amountToCredit -= sio.getAmount();
|
|
|
511 |
schemeInOut.setStatusDescription("Category upgraded, higher margins credited for sale of IMEI#" + inventoryItem.getSerialNumber());
|
|
|
512 |
}
|
|
|
513 |
}
|
|
|
514 |
|
| 27713 |
amit.gupta |
515 |
}
|
| 27377 |
amit.gupta |
516 |
schemeInOut.setCreditTimestamp(LocalDateTime.now());
|
|
|
517 |
}
|
| 23443 |
amit.gupta |
518 |
}
|
| 27377 |
amit.gupta |
519 |
return amountToCredit;
|
| 22859 |
ashik.ali |
520 |
}
|
| 23444 |
amit.gupta |
521 |
|
| 25049 |
amit.gupta |
522 |
// We are maintaining price drop after grn
|
| 23444 |
amit.gupta |
523 |
private float getAmount(InventoryItem inventoryItem, Scheme scheme) {
|
| 22653 |
ashik.ali |
524 |
float amount = 0;
|
| 25049 |
amit.gupta |
525 |
float dpForCalc = 0;
|
|
|
526 |
float taxableSellingPrice = 0;
|
| 26684 |
amit.gupta |
527 |
// float totalTaxRate = inventoryItem.getIgstRate() +
|
|
|
528 |
// inventoryItem.getSgstRate() + inventoryItem.getCgstRate();
|
|
|
529 |
// Hardcoding it to 18%
|
| 26568 |
amit.gupta |
530 |
float totalTaxRate = 18f;
|
| 26684 |
amit.gupta |
531 |
// float totalTaxRate = inventoryItem.getIgstRate() +
|
|
|
532 |
// inventoryItem.getSgstRate() + inventoryItem.getCgstRate();
|
| 23527 |
ashik.ali |
533 |
if (scheme.getAmountType() == AmountType.PERCENTAGE) {
|
| 24562 |
amit.gupta |
534 |
if (scheme.getType().equals(SchemeType.IN)) {
|
| 25049 |
amit.gupta |
535 |
dpForCalc = inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount();
|
| 23995 |
amit.gupta |
536 |
} else {
|
|
|
537 |
try {
|
| 26684 |
amit.gupta |
538 |
dpForCalc = Math.min(inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount(),
|
| 26550 |
amit.gupta |
539 |
tagListingRepository.selectByItemId(inventoryItem.getItemId()).getSellingPrice());
|
| 24562 |
amit.gupta |
540 |
} catch (Exception e) {
|
| 23995 |
amit.gupta |
541 |
LOGGER.info("Could not find tag Listing entry in {}", inventoryItem.getItemId());
|
|
|
542 |
e.printStackTrace();
|
|
|
543 |
}
|
|
|
544 |
}
|
| 25049 |
amit.gupta |
545 |
taxableSellingPrice = dpForCalc / (1 + totalTaxRate / 100);
|
| 22859 |
ashik.ali |
546 |
amount = taxableSellingPrice * scheme.getAmount() / 100;
|
| 25517 |
amit.gupta |
547 |
System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%f\t%f\t%f\t%f", inventoryItem.getId(),
|
| 25050 |
amit.gupta |
548 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
| 26332 |
amit.gupta |
549 |
scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
|
|
|
550 |
scheme.getAmount(), amount));
|
| 23444 |
amit.gupta |
551 |
} else {
|
| 22653 |
ashik.ali |
552 |
amount = scheme.getAmount();
|
| 25517 |
amit.gupta |
553 |
System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%f\t%f\t%d\t%f", inventoryItem.getId(),
|
| 25050 |
amit.gupta |
554 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
| 26332 |
amit.gupta |
555 |
scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
|
|
|
556 |
0, amount));
|
| 22653 |
ashik.ali |
557 |
}
|
| 25049 |
amit.gupta |
558 |
|
| 22859 |
ashik.ali |
559 |
return amount;
|
| 22653 |
ashik.ali |
560 |
}
|
| 23444 |
amit.gupta |
561 |
|
| 22653 |
ashik.ali |
562 |
@Override
|
| 23365 |
ashik.ali |
563 |
public void processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
|
|
|
564 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(retailerId, fofoOrderId);
|
| 28969 |
amit.gupta |
565 |
//Process only if order is not cancelled
|
|
|
566 |
if(fofoOrder.getCancelledTimestamp()==null) {
|
|
|
567 |
//PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId,
|
|
|
568 |
// fofoOrder.getCreateTimestamp().toLocalDate());
|
|
|
569 |
|
|
|
570 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId, YearMonth.from(fofoOrder.getCreateTimestamp()));
|
|
|
571 |
|
|
|
572 |
List<ScanRecord> scanRecords = scanRecordRepository.selectAllByOrderId(fofoOrderId);
|
|
|
573 |
Set<Integer> inventoryItemIds = scanRecords.stream().map(x -> x.getInventoryItemId())
|
|
|
574 |
.collect(Collectors.toSet());
|
|
|
575 |
Set<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIds).stream()
|
|
|
576 |
.filter(x -> x.getSerialNumber() != null && !x.getSerialNumber().equals(""))
|
|
|
577 |
.collect(Collectors.toSet());
|
|
|
578 |
if (inventoryItems.size() == 0) {
|
|
|
579 |
return;
|
| 22653 |
ashik.ali |
580 |
}
|
| 28969 |
amit.gupta |
581 |
Set<Integer> itemIds = inventoryItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
582 |
|
|
|
583 |
//Remove Items that are eol now.
|
|
|
584 |
Set<Integer> itemIdsSet = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds).stream()
|
|
|
585 |
.filter(x -> x.getEolDate() == null || x.getEolDate().isAfter(fofoOrder.getCreateTimestamp()))
|
|
|
586 |
.map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
587 |
// Only consider inventory items that were not returned
|
|
|
588 |
inventoryItems = inventoryItems.stream().filter(x -> itemIdsSet.contains(x.getItemId()))
|
|
|
589 |
.collect(Collectors.toSet());
|
|
|
590 |
|
|
|
591 |
if (inventoryItems.size() == 0) {
|
|
|
592 |
return;
|
| 22859 |
ashik.ali |
593 |
}
|
| 28969 |
amit.gupta |
594 |
|
|
|
595 |
float totalCashback = 0;
|
|
|
596 |
int count = 0;
|
|
|
597 |
|
|
|
598 |
List<Scheme> allActiveSchemes = schemeRepository.selectActiveAll(SchemeType.OUT, partnerType,
|
|
|
599 |
fofoOrder.getCreateTimestamp(), false);
|
|
|
600 |
allActiveSchemes.addAll(schemeRepository.selectActiveAll(SchemeType.ACTIVATION, partnerType,
|
|
|
601 |
fofoOrder.getCreateTimestamp(), false));
|
|
|
602 |
allActiveSchemes.addAll(schemeRepository.selectActiveAll(SchemeType.INVESTMENT, partnerType,
|
|
|
603 |
fofoOrder.getCreateTimestamp(), false));
|
|
|
604 |
allActiveSchemes.addAll(schemeRepository.selectActiveAll(SchemeType.CATEGORY, partnerType,
|
|
|
605 |
fofoOrder.getCreateTimestamp(), false));
|
|
|
606 |
|
|
|
607 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
608 |
float itemCashback = 0;
|
|
|
609 |
Set<Integer> schemeIds = new HashSet<>(
|
|
|
610 |
schemeItemRepository.selectSchemeIdByItemId(inventoryItem.getItemId()));
|
|
|
611 |
allActiveSchemes = allActiveSchemes.stream().filter(x -> schemeIds.contains(x.getId()))
|
|
|
612 |
.collect(Collectors.toList());
|
|
|
613 |
for (Scheme scheme : allActiveSchemes) {
|
|
|
614 |
itemCashback += this.createSchemeInOut(scheme, inventoryItem);
|
|
|
615 |
}
|
|
|
616 |
if (itemCashback > 0) {
|
|
|
617 |
count++;
|
|
|
618 |
totalCashback += itemCashback;
|
|
|
619 |
}
|
|
|
620 |
}
|
|
|
621 |
if (count > 0) {
|
|
|
622 |
walletService.addAmountToWallet(retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT,
|
|
|
623 |
"Sales margin for invoice number " + fofoOrder.getInvoiceNumber() + ". Total " + count + " pc(s)",
|
|
|
624 |
totalCashback, fofoOrder.getCreateTimestamp());
|
|
|
625 |
fofoOrder.setCashback(totalCashback);
|
|
|
626 |
fofoOrderRepository.persist(fofoOrder);
|
|
|
627 |
}
|
| 22653 |
ashik.ali |
628 |
}
|
|
|
629 |
}
|
| 23444 |
amit.gupta |
630 |
|
| 23508 |
amit.gupta |
631 |
@Override
|
| 23796 |
amit.gupta |
632 |
public void rollbackSchemes(List<Integer> inventoryItemIds, int rollbackReference, String rollbackReason)
|
|
|
633 |
throws Exception {
|
| 23638 |
amit.gupta |
634 |
Set<Integer> inventoryItemIdSet = new HashSet<>(inventoryItemIds);
|
| 23508 |
amit.gupta |
635 |
float amountToRollback = 0;
|
|
|
636 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIdSet);
|
| 23796 |
amit.gupta |
637 |
for (SchemeInOut schemeInOut : schemes) {
|
| 23983 |
amit.gupta |
638 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
639 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
| 28464 |
amit.gupta |
640 |
if (schemeInOut.getStatus()==null || schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
| 27377 |
amit.gupta |
641 |
amountToRollback += schemeInOut.getAmount();
|
|
|
642 |
}
|
|
|
643 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
644 |
schemeInOut.setStatusDescription(rollbackReason);
|
| 23983 |
amit.gupta |
645 |
}
|
| 23508 |
amit.gupta |
646 |
}
|
| 23983 |
amit.gupta |
647 |
if (amountToRollback > 0) {
|
| 23638 |
amit.gupta |
648 |
int inventoryItemId = inventoryItemIds.get(0);
|
| 26498 |
amit.gupta |
649 |
InventoryItem ii = inventoryItemRepository.selectById(inventoryItemId);
|
|
|
650 |
Integer fofoId = ii.getFofoId();
|
| 27898 |
amit.gupta |
651 |
// Purchase p = purchaseRepository.selectById(ii.getPurchaseId());
|
| 26684 |
amit.gupta |
652 |
// TODO//
|
| 26693 |
amit.gupta |
653 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, ii.getPurchaseId(),
|
| 26498 |
amit.gupta |
654 |
WalletReferenceType.SCHEME_IN, rollbackReason, LocalDateTime.now());
|
| 23638 |
amit.gupta |
655 |
}
|
| 23508 |
amit.gupta |
656 |
}
|
| 23884 |
amit.gupta |
657 |
|
| 23781 |
ashik.ali |
658 |
@Override
|
| 23796 |
amit.gupta |
659 |
public Map<String, Object> getSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
660 |
throws ProfitMandiBusinessException {
|
| 23781 |
ashik.ali |
661 |
Map<String, Object> map = new HashMap<>();
|
|
|
662 |
List<Scheme> schemes = null;
|
|
|
663 |
long size = 0;
|
| 23798 |
amit.gupta |
664 |
if (roleManager.isAdmin(roleIds)) {
|
| 23781 |
ashik.ali |
665 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
666 |
size = schemeRepository.selectAllCount();
|
| 23796 |
amit.gupta |
667 |
} else {
|
| 23781 |
ashik.ali |
668 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
669 |
size = schemeRepository.selectAllActiveCount();
|
|
|
670 |
}
|
|
|
671 |
map.put("schemes", schemes);
|
|
|
672 |
map.put("start", offset + 1);
|
|
|
673 |
map.put("size", size);
|
| 23796 |
amit.gupta |
674 |
if (schemes.size() < limit) {
|
| 23781 |
ashik.ali |
675 |
map.put("end", offset + schemes.size());
|
| 23796 |
amit.gupta |
676 |
} else {
|
| 23781 |
ashik.ali |
677 |
map.put("end", offset + limit);
|
|
|
678 |
}
|
|
|
679 |
return map;
|
|
|
680 |
}
|
| 23796 |
amit.gupta |
681 |
|
| 23781 |
ashik.ali |
682 |
@Override
|
| 23796 |
amit.gupta |
683 |
public List<Scheme> getPaginatedSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
684 |
throws ProfitMandiBusinessException {
|
| 23781 |
ashik.ali |
685 |
LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
|
|
|
686 |
List<Scheme> schemes = null;
|
| 23798 |
amit.gupta |
687 |
if (roleManager.isAdmin(roleIds)) {
|
| 23781 |
ashik.ali |
688 |
schemes = schemeRepository.selectAll(offset, limit);
|
| 23796 |
amit.gupta |
689 |
} else {
|
| 23781 |
ashik.ali |
690 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
691 |
}
|
|
|
692 |
return schemes;
|
|
|
693 |
}
|
| 23508 |
amit.gupta |
694 |
|
| 23968 |
amit.gupta |
695 |
@Override
|
| 27898 |
amit.gupta |
696 |
// This is being called to reverse schemes while processing price Drop
|
| 24976 |
amit.gupta |
697 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int priceDropId, String reversalReason)
|
| 26332 |
amit.gupta |
698 |
throws ProfitMandiBusinessException {
|
| 26498 |
amit.gupta |
699 |
PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
|
| 24976 |
amit.gupta |
700 |
Map<Integer, List<InventoryItem>> purchaseInventoryListMap = inventoryItems.stream()
|
|
|
701 |
.collect(Collectors.groupingBy(InventoryItem::getPurchaseId, Collectors.toList()));
|
|
|
702 |
|
|
|
703 |
for (Map.Entry<Integer, List<InventoryItem>> purchaseEntry : purchaseInventoryListMap.entrySet()) {
|
|
|
704 |
float amountToCredit = 0;
|
|
|
705 |
float amountToDebit = 0;
|
|
|
706 |
int purchaseId = purchaseEntry.getKey();
|
|
|
707 |
List<InventoryItem> purchaseInventoryItemList = purchaseEntry.getValue();
|
|
|
708 |
|
|
|
709 |
Map<Integer, InventoryItem> inventoryItemsMap = purchaseInventoryItemList.stream()
|
|
|
710 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
711 |
|
|
|
712 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
| 27399 |
amit.gupta |
713 |
LOGGER.info("Scheme InOuts , {}", schemeInOuts);
|
| 27898 |
amit.gupta |
714 |
if (schemeInOuts.size() == 0) {
|
| 27400 |
amit.gupta |
715 |
continue;
|
|
|
716 |
}
|
| 24976 |
amit.gupta |
717 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
718 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
719 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
720 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
721 |
InventoryItem ii = inventoryItemsMap.get(schemeInOut.getInventoryItemId());
|
|
|
722 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
723 |
if (scheme.getAmountType().equals(AmountType.FIXED)) {
|
|
|
724 |
continue;
|
| 23995 |
amit.gupta |
725 |
}
|
| 27713 |
amit.gupta |
726 |
if (scheme.getType().equals(SchemeType.IN) && schemeInOut.getRolledBackTimestamp() == null) {
|
| 24976 |
amit.gupta |
727 |
float newAmount = getAmount(ii, scheme);
|
|
|
728 |
if (schemeInOut.getAmount() - newAmount >= 0.01f) {
|
|
|
729 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
| 27898 |
amit.gupta |
730 |
|
| 24976 |
amit.gupta |
731 |
SchemeInOut sioNew = new SchemeInOut();
|
|
|
732 |
sioNew.setAmount(newAmount);
|
| 27713 |
amit.gupta |
733 |
sioNew.setStatus(schemeInOut.getStatus());
|
|
|
734 |
sioNew.setStatusDescription(schemeInOut.getStatusDescription());
|
| 24976 |
amit.gupta |
735 |
sioNew.setInventoryItemId(schemeInOut.getInventoryItemId());
|
|
|
736 |
sioNew.setSchemeId(schemeInOut.getSchemeId());
|
|
|
737 |
schemeInOutRepository.persist(sioNew);
|
| 27713 |
amit.gupta |
738 |
|
|
|
739 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
740 |
schemeInOut.setStatusDescription("Pricedrop processed, new entries added");
|
| 27898 |
amit.gupta |
741 |
// IF not credited then dont consider any credit/debit for that sio entry
|
|
|
742 |
if (schemeInOut.getCreditTimestamp() != null) {
|
| 27713 |
amit.gupta |
743 |
amountToCredit += sioNew.getAmount();
|
|
|
744 |
amountToDebit += schemeInOut.getAmount();
|
|
|
745 |
}
|
| 24976 |
amit.gupta |
746 |
}
|
| 24562 |
amit.gupta |
747 |
|
| 24976 |
amit.gupta |
748 |
}
|
| 23986 |
amit.gupta |
749 |
}
|
| 24976 |
amit.gupta |
750 |
int fofoId = inventoryItems.get(0).getFofoId();
|
|
|
751 |
if (amountToDebit > 0) {
|
| 26684 |
amit.gupta |
752 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN,
|
|
|
753 |
MessageFormat.format(reversalReason, purchaseInventoryItemList.size()), -amountToDebit,
|
|
|
754 |
priceDrop.getAffectedOn());
|
| 24976 |
amit.gupta |
755 |
}
|
|
|
756 |
if (amountToCredit > 0) {
|
| 26403 |
amit.gupta |
757 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN,
|
| 26684 |
amit.gupta |
758 |
MessageFormat.format(reversalReason, purchaseInventoryItemList.size()), amountToCredit,
|
|
|
759 |
priceDrop.getAffectedOn());
|
| 24976 |
amit.gupta |
760 |
}
|
| 23968 |
amit.gupta |
761 |
}
|
|
|
762 |
}
|
| 24562 |
amit.gupta |
763 |
|
| 24264 |
amit.gupta |
764 |
@Override
|
| 27898 |
amit.gupta |
765 |
// Always being called from cancel order means no SCHEME IN is considered
|
| 24562 |
amit.gupta |
766 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int reversalReference, String reversalReason,
|
|
|
767 |
SchemeType schemeType) throws ProfitMandiBusinessException {
|
| 24264 |
amit.gupta |
768 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
|
| 28419 |
tejbeer |
769 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 28417 |
tejbeer |
770 |
LOGGER.info("inventoryItems" + inventoryItems);
|
|
|
771 |
|
| 24264 |
amit.gupta |
772 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
| 28416 |
tejbeer |
773 |
LOGGER.info("schemeInOuts" + schemeInOuts);
|
| 28417 |
tejbeer |
774 |
float amountToRollback = 0;
|
| 28416 |
tejbeer |
775 |
|
| 28417 |
tejbeer |
776 |
if (!schemeInOuts.isEmpty()) {
|
|
|
777 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
778 |
LOGGER.info("schemeIds" + schemeIds);
|
| 28416 |
tejbeer |
779 |
|
| 28417 |
tejbeer |
780 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
781 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
782 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
783 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
784 |
if (scheme.getType().equals(schemeType)) {
|
|
|
785 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
786 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
787 |
if (schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
|
|
788 |
amountToRollback += schemeInOut.getAmount();
|
|
|
789 |
}
|
|
|
790 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
791 |
schemeInOut.setStatusDescription(reversalReason);
|
| 27377 |
amit.gupta |
792 |
}
|
| 24264 |
amit.gupta |
793 |
}
|
|
|
794 |
}
|
| 28417 |
tejbeer |
795 |
|
| 24264 |
amit.gupta |
796 |
}
|
|
|
797 |
int fofoId = inventoryItems.get(0).getFofoId();
|
| 28749 |
amit.gupta |
798 |
WalletReferenceType walletReferenceType = schemeType.equals(SchemeType.OUT) || schemeType.equals(SchemeType.CATEGORY) ? WalletReferenceType.SCHEME_OUT
|
| 27898 |
amit.gupta |
799 |
: schemeType.equals(SchemeType.INVESTMENT) ? WalletReferenceType.INVESTMENT_PAYOUT
|
|
|
800 |
: WalletReferenceType.ACTIVATION_SCHEME;
|
| 27377 |
amit.gupta |
801 |
if (amountToRollback > 0) {
|
| 26684 |
amit.gupta |
802 |
// TODO//
|
| 27377 |
amit.gupta |
803 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, reversalReference, walletReferenceType,
|
| 26498 |
amit.gupta |
804 |
reversalReason, LocalDateTime.now());
|
| 24268 |
amit.gupta |
805 |
}
|
| 24264 |
amit.gupta |
806 |
}
|
| 23968 |
amit.gupta |
807 |
|
| 26332 |
amit.gupta |
808 |
@Override
|
|
|
809 |
public double getTotalMargin(int itemId, PartnerType partnerType, LocalDateTime dateTime) {
|
|
|
810 |
Session session = sessionFactory.getCurrentSession();
|
|
|
811 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
812 |
CriteriaQuery<Double> criteriaQuery = cb.createQuery(Double.class);
|
|
|
813 |
Root<SchemeItem> schemeItem = criteriaQuery.from(SchemeItem.class);
|
|
|
814 |
Root<Scheme> scheme = criteriaQuery.from(Scheme.class);
|
|
|
815 |
Predicate schemePredicate = cb.equal(scheme.get(ProfitMandiConstants.AMOUNT_TYPE), AmountType.PERCENTAGE);
|
|
|
816 |
Predicate lessThanPredicate = cb.lessThanOrEqualTo(scheme.get(ProfitMandiConstants.END_DATE_TIME), dateTime);
|
|
|
817 |
Predicate greaterThanPredicate = cb.greaterThanOrEqualTo(scheme.get(ProfitMandiConstants.START_DATE_TIME),
|
|
|
818 |
dateTime);
|
|
|
819 |
Predicate joinPredicate = cb.equal(scheme.get("id"), schemeItem.get("schemeId"));
|
|
|
820 |
Predicate schemeItemPredicate = cb.equal(schemeItem.get(ProfitMandiConstants.ITEM_ID), itemId);
|
|
|
821 |
criteriaQuery.select(cb.sum(scheme.get(ProfitMandiConstants.AMOUNT))).where(schemePredicate, lessThanPredicate,
|
|
|
822 |
greaterThanPredicate, schemeItemPredicate, joinPredicate);
|
|
|
823 |
|
|
|
824 |
Query<Double> query = session.createQuery(criteriaQuery);
|
|
|
825 |
return query.getSingleResult() + ProfitMandiConstants.SCHEME_INVESTMENT_MARGIN;
|
|
|
826 |
|
|
|
827 |
}
|
|
|
828 |
|
| 26684 |
amit.gupta |
829 |
@Override
|
|
|
830 |
@Cacheable(value = "itemSchemeCashback", cacheManager = "timeoutCacheManager")
|
|
|
831 |
public Map<Integer, Float> getItemSchemeCashBack() {
|
|
|
832 |
Map<Integer, Float> itemCashbackMap = new HashMap<>();
|
|
|
833 |
Map<Integer, Scheme> cashbackSchemesMap = schemeRepository
|
| 28357 |
amit.gupta |
834 |
.selectActiveAll(SchemeType.ACTIVATION, PartnerType.ALL, LocalDateTime.now(), true).stream()
|
| 26684 |
amit.gupta |
835 |
.filter(x -> x.getAmountType().equals(AmountType.FIXED))
|
|
|
836 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 26722 |
amit.gupta |
837 |
if (cashbackSchemesMap.size() > 0) {
|
| 26713 |
amit.gupta |
838 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(cashbackSchemesMap.keySet());
|
|
|
839 |
schemeItems.stream().forEach(x -> {
|
|
|
840 |
itemCashbackMap.put(x.getItemId(), cashbackSchemesMap.get(x.getSchemeId()).getAmount());
|
|
|
841 |
});
|
|
|
842 |
}
|
| 26722 |
amit.gupta |
843 |
// A107FD Model needs to removed
|
| 26703 |
amit.gupta |
844 |
itemCashbackMap.remove(30211);
|
|
|
845 |
itemCashbackMap.remove(30212);
|
|
|
846 |
itemCashbackMap.remove(30213);
|
|
|
847 |
itemCashbackMap.remove(30756);
|
| 26684 |
amit.gupta |
848 |
return itemCashbackMap;
|
|
|
849 |
}
|
|
|
850 |
|
| 24976 |
amit.gupta |
851 |
/*
|
|
|
852 |
* @Override public void updateSchmesForModel(int catalogId) throws
|
|
|
853 |
* ProfitMandiBusinessException { List<Item> items =
|
|
|
854 |
* itemRepository.selectAllByCatalogItemId(catalogId); Map<Integer, Scheme>
|
|
|
855 |
* schemes = schemeRepository .selectAllByItemIds(items.stream().map(x ->
|
|
|
856 |
* x.getId()).collect(Collectors.toList()));
|
|
|
857 |
*
|
|
|
858 |
* }
|
|
|
859 |
*/
|
| 24562 |
amit.gupta |
860 |
|
| 26802 |
tejbeer |
861 |
@Override
|
| 27898 |
amit.gupta |
862 |
public List<Scheme> selectSchemeByPartnerType(PartnerType partnerType, LocalDate onDate, int itemId,
|
|
|
863 |
boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
|
| 26802 |
tejbeer |
864 |
Session session = sessionFactory.getCurrentSession();
|
|
|
865 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
866 |
CriteriaQuery<Scheme> query = cb.createQuery(Scheme.class);
|
|
|
867 |
Root<Scheme> scheme = query.from(Scheme.class);
|
|
|
868 |
List<PartnerType> pt = new ArrayList<>();
|
|
|
869 |
pt.add(PartnerType.ALL);
|
|
|
870 |
pt.add(partnerType);
|
|
|
871 |
Predicate p1 = cb.in(scheme.get("partnerType")).value(pt);
|
|
|
872 |
Predicate p2 = cb.isNotNull(scheme.get("activeTimestamp"));
|
|
|
873 |
cb.desc(cb.isNull(scheme.get("expireTimestamp")));
|
|
|
874 |
|
|
|
875 |
Predicate p3 = null;
|
|
|
876 |
Predicate finalPredicate = null;
|
|
|
877 |
if (itemId != 0) {
|
| 27898 |
amit.gupta |
878 |
|
| 26802 |
tejbeer |
879 |
List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(itemId);
|
| 27424 |
tejbeer |
880 |
LOGGER.info("schemeId" + schemeIds);
|
| 27898 |
amit.gupta |
881 |
if (schemeIds.isEmpty()) {
|
| 27424 |
tejbeer |
882 |
return new ArrayList<>();
|
|
|
883 |
}
|
| 26802 |
tejbeer |
884 |
p3 = cb.in(scheme.get("id")).value(schemeIds);
|
|
|
885 |
if (!isAdmin) {
|
|
|
886 |
|
| 27877 |
amit.gupta |
887 |
finalPredicate = cb.and(p1, p2, p3, cb.greaterThan(scheme.get("endDateTime"), onDate.atStartOfDay()),
|
|
|
888 |
cb.lessThanOrEqualTo(scheme.get("startDateTime"), onDate.atStartOfDay()));
|
| 26802 |
tejbeer |
889 |
query.where(finalPredicate);
|
|
|
890 |
} else {
|
|
|
891 |
finalPredicate = cb.and(p1, p3);
|
|
|
892 |
query.where(finalPredicate);
|
|
|
893 |
}
|
|
|
894 |
} else {
|
|
|
895 |
|
|
|
896 |
if (!isAdmin) {
|
|
|
897 |
|
|
|
898 |
finalPredicate = cb.and(p1, p2);
|
| 26912 |
tejbeer |
899 |
query.where(finalPredicate);
|
| 26802 |
tejbeer |
900 |
} else {
|
|
|
901 |
query.where(p1);
|
|
|
902 |
}
|
|
|
903 |
}
|
|
|
904 |
query.orderBy(cb.desc(cb.function("isnull", Boolean.class, scheme.get("expireTimestamp"))));
|
|
|
905 |
|
|
|
906 |
return session.createQuery(query).setFirstResult(offset).setMaxResults(limit).getResultList();
|
|
|
907 |
|
|
|
908 |
}
|
|
|
909 |
|
|
|
910 |
@Override
|
| 27877 |
amit.gupta |
911 |
public long selectSchemeCount(PartnerType partnerType, LocalDate onDate, int itemId, boolean isAdmin) {
|
| 26802 |
tejbeer |
912 |
Session session = sessionFactory.getCurrentSession();
|
|
|
913 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
914 |
CriteriaQuery<Long> query = cb.createQuery(Long.class);
|
|
|
915 |
Root<Scheme> scheme = query.from(Scheme.class);
|
|
|
916 |
List<PartnerType> pt = new ArrayList<>();
|
|
|
917 |
pt.add(PartnerType.ALL);
|
|
|
918 |
pt.add(partnerType);
|
|
|
919 |
Predicate p1 = cb.in(scheme.get("partnerType")).value(pt);
|
|
|
920 |
Predicate p2 = cb.isNotNull(scheme.get("activeTimestamp"));
|
|
|
921 |
Predicate finalPredicate = null;
|
|
|
922 |
if (itemId != 0) {
|
|
|
923 |
List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(itemId);
|
|
|
924 |
Predicate p3 = cb.in(scheme.get("id")).value(schemeIds);
|
|
|
925 |
if (!isAdmin) {
|
|
|
926 |
|
| 27877 |
amit.gupta |
927 |
finalPredicate = cb.and(p1, p2, p3, cb.greaterThan(scheme.get("endDateTime"), onDate.atStartOfDay()),
|
|
|
928 |
cb.lessThanOrEqualTo(scheme.get("startDateTime"), onDate.atStartOfDay()));
|
| 26802 |
tejbeer |
929 |
query.select(cb.count(scheme)).where(finalPredicate);
|
|
|
930 |
} else {
|
|
|
931 |
finalPredicate = cb.and(p1, p3);
|
|
|
932 |
query.select(cb.count(scheme)).where(finalPredicate);
|
|
|
933 |
}
|
|
|
934 |
} else {
|
|
|
935 |
if (!isAdmin) {
|
|
|
936 |
finalPredicate = cb.and(p1, p2);
|
|
|
937 |
query.select(cb.count(scheme)).where(finalPredicate);
|
|
|
938 |
|
|
|
939 |
} else {
|
|
|
940 |
query.select(cb.count(scheme)).where(p1);
|
|
|
941 |
|
|
|
942 |
}
|
|
|
943 |
}
|
|
|
944 |
|
|
|
945 |
return session.createQuery(query).getSingleResult();
|
|
|
946 |
|
|
|
947 |
}
|
| 27898 |
amit.gupta |
948 |
|
| 27395 |
amit.gupta |
949 |
@Override
|
|
|
950 |
public void processActivation() throws ProfitMandiBusinessException {
|
|
|
951 |
List<SchemeInOut> pendingPayouts = schemeInOutRepository.selectAllPending();
|
|
|
952 |
List<Integer> schemeIds = new ArrayList<>();
|
|
|
953 |
Set<Integer> inventoryIds = new HashSet<>();
|
| 27898 |
amit.gupta |
954 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
| 27395 |
amit.gupta |
955 |
schemeIds.add(pendingPayout.getSchemeId());
|
| 27440 |
amit.gupta |
956 |
}
|
|
|
957 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, 0).stream()
|
| 27898 |
amit.gupta |
958 |
.filter(x -> x.getType().equals(SchemeType.ACTIVATION))
|
|
|
959 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
960 |
pendingPayouts = pendingPayouts.stream().filter(x -> schemesMap.get(x.getSchemeId()) != null)
|
|
|
961 |
.collect(Collectors.toList());
|
| 27440 |
amit.gupta |
962 |
|
| 27898 |
amit.gupta |
963 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
| 27395 |
amit.gupta |
964 |
inventoryIds.add(pendingPayout.getInventoryItemId());
|
|
|
965 |
}
|
| 27898 |
amit.gupta |
966 |
Map<Integer, InventoryItem> inventoryItemMap = inventoryItemRepository.selectByIds(inventoryIds).stream()
|
|
|
967 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
968 |
Map<String, InventoryItem> serialNumberMap = inventoryItemMap.values().stream()
|
|
|
969 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x));
|
|
|
970 |
|
|
|
971 |
List<ActivatedImei> activatedImeis = activatedImeiRepository
|
|
|
972 |
.selectBySerialNumbers(new ArrayList<>(serialNumberMap.keySet()));
|
|
|
973 |
|
|
|
974 |
Map<String, ActivatedImei> activatedImeiMap = activatedImeis.stream()
|
|
|
975 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x));
|
|
|
976 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
| 27395 |
amit.gupta |
977 |
InventoryItem ii = inventoryItemMap.get(pendingPayout.getInventoryItemId());
|
|
|
978 |
String serialNumber = ii.getSerialNumber();
|
|
|
979 |
ActivatedImei activatedImei = activatedImeiMap.get(serialNumber);
|
| 27898 |
amit.gupta |
980 |
if (activatedImei == null) {
|
| 27395 |
amit.gupta |
981 |
continue;
|
|
|
982 |
}
|
|
|
983 |
Scheme scheme = schemesMap.get(pendingPayout.getSchemeId());
|
| 27898 |
amit.gupta |
984 |
if (activatedImei.getActivationTimestamp().isBefore(scheme.getEndDateTime())
|
|
|
985 |
&& activatedImei.getActivationTimestamp().isAfter(scheme.getStartDateTime())) {
|
| 27395 |
amit.gupta |
986 |
int fofoId = ii.getFofoId();
|
| 27898 |
amit.gupta |
987 |
// Get latest order Id
|
|
|
988 |
int orderId = scanRecordRepository.selectByInventoryItemId(ii.getId()).stream()
|
|
|
989 |
.filter(x -> x.getOrderId() > 0)
|
|
|
990 |
.sorted(Comparator.comparing(ScanRecord::getCreateTimestamp).reversed()).findFirst().get()
|
|
|
991 |
.getOrderId();
|
| 27395 |
amit.gupta |
992 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(orderId);
|
| 27898 |
amit.gupta |
993 |
walletService.addAmountToWallet(fofoId, orderId, WalletReferenceType.ACTIVATION_SCHEME,
|
|
|
994 |
"Activation margin for Imei#" + serialNumber, pendingPayout.getAmount(),
|
|
|
995 |
fofoOrder.getCreateTimestamp());
|
| 27395 |
amit.gupta |
996 |
pendingPayout.setCreditTimestamp(LocalDateTime.now());
|
|
|
997 |
pendingPayout.setStatus(SchemePayoutStatus.CREDITED);
|
|
|
998 |
pendingPayout.setStatusDescription("Activation margin credited");
|
|
|
999 |
} else {
|
|
|
1000 |
pendingPayout.setStatus(SchemePayoutStatus.REJECTED);
|
| 27898 |
amit.gupta |
1001 |
pendingPayout.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
1002 |
;
|
|
|
1003 |
pendingPayout.setStatusDescription(
|
|
|
1004 |
"Rejected, activated on " + FormattingUtils.formatDate(activatedImei.getActivationTimestamp()));
|
| 27395 |
amit.gupta |
1005 |
}
|
|
|
1006 |
}
|
|
|
1007 |
}
|
| 26802 |
tejbeer |
1008 |
|
| 22653 |
ashik.ali |
1009 |
}
|