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