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