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