| 22653 |
ashik.ali |
1 |
package com.spice.profitmandi.service.scheme;
|
|
|
2 |
|
| 24307 |
amit.gupta |
3 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
| 29927 |
amit.gupta |
4 |
import com.spice.profitmandi.common.enumuration.MessageType;
|
| 22653 |
ashik.ali |
5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
6 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 23019 |
ashik.ali |
7 |
import com.spice.profitmandi.common.model.SchemeModel;
|
| 29927 |
amit.gupta |
8 |
import com.spice.profitmandi.common.model.SendNotificationModel;
|
| 29584 |
manish |
9 |
import com.spice.profitmandi.common.services.ReporticoService;
|
| 27395 |
amit.gupta |
10 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
| 22859 |
ashik.ali |
11 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 30121 |
amit.gupta |
12 |
import com.spice.profitmandi.common.util.Utils;
|
| 34221 |
tejus.loha |
13 |
import com.spice.profitmandi.dao.entity.catalog.*;
|
| 29927 |
amit.gupta |
14 |
import com.spice.profitmandi.dao.entity.fofo.*;
|
| 31903 |
amit.gupta |
15 |
import com.spice.profitmandi.dao.entity.inventory.PartnerAgeingModel;
|
| 34709 |
amit.gupta |
16 |
import com.spice.profitmandi.dao.entity.transaction.Loan;
|
| 26498 |
amit.gupta |
17 |
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
|
| 32060 |
amit.gupta |
18 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
| 31609 |
amit.gupta |
19 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
| 23527 |
ashik.ali |
20 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 22653 |
ashik.ali |
21 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 34317 |
amit.gupta |
22 |
import com.spice.profitmandi.dao.enumuration.catalog.StockTransactionType;
|
| 34504 |
amit.gupta |
23 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| 27377 |
amit.gupta |
24 |
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
|
| 31903 |
amit.gupta |
25 |
import com.spice.profitmandi.dao.model.AmountModel;
|
| 25503 |
amit.gupta |
26 |
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
|
| 31170 |
amit.gupta |
27 |
import com.spice.profitmandi.dao.repository.catalog.*;
|
| 32309 |
amit.gupta |
28 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 23968 |
amit.gupta |
29 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 29927 |
amit.gupta |
30 |
import com.spice.profitmandi.dao.repository.fofo.*;
|
| 34709 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
|
| 26498 |
amit.gupta |
32 |
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
|
| 31609 |
amit.gupta |
33 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
| 32060 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
| 34504 |
amit.gupta |
35 |
import com.spice.profitmandi.dao.repository.warehouse.AgeingSummaryModel;
|
| 33087 |
amit.gupta |
36 |
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
|
| 29927 |
amit.gupta |
37 |
import com.spice.profitmandi.service.NotificationService;
|
| 23798 |
amit.gupta |
38 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| 31903 |
amit.gupta |
39 |
import com.spice.profitmandi.service.inventory.AgeingService;
|
| 26722 |
amit.gupta |
40 |
import com.spice.profitmandi.service.inventory.PurchaseService;
|
| 31903 |
amit.gupta |
41 |
import com.spice.profitmandi.service.pricecircular.CatalogSummaryModel;
|
| 31921 |
amit.gupta |
42 |
import com.spice.profitmandi.service.pricecircular.PriceCircularService;
|
| 31903 |
amit.gupta |
43 |
import com.spice.profitmandi.service.pricecircular.SchemeSummaryModel;
|
| 22859 |
ashik.ali |
44 |
import com.spice.profitmandi.service.wallet.WalletService;
|
|
|
45 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| 29927 |
amit.gupta |
46 |
import org.apache.logging.log4j.LogManager;
|
|
|
47 |
import org.apache.logging.log4j.Logger;
|
|
|
48 |
import org.hibernate.Session;
|
|
|
49 |
import org.hibernate.SessionFactory;
|
|
|
50 |
import org.hibernate.query.Query;
|
|
|
51 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
52 |
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
53 |
import org.springframework.cache.annotation.Cacheable;
|
|
|
54 |
import org.springframework.stereotype.Component;
|
| 22859 |
ashik.ali |
55 |
|
| 31170 |
amit.gupta |
56 |
import javax.persistence.TypedQuery;
|
| 29927 |
amit.gupta |
57 |
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
58 |
import javax.persistence.criteria.CriteriaQuery;
|
|
|
59 |
import javax.persistence.criteria.Predicate;
|
|
|
60 |
import javax.persistence.criteria.Root;
|
|
|
61 |
import java.text.MessageFormat;
|
| 34115 |
ranu |
62 |
import java.time.LocalDate;
|
|
|
63 |
import java.time.LocalDateTime;
|
|
|
64 |
import java.time.LocalTime;
|
| 34504 |
amit.gupta |
65 |
import java.time.YearMonth;
|
| 29927 |
amit.gupta |
66 |
import java.util.*;
|
|
|
67 |
import java.util.stream.Collectors;
|
|
|
68 |
|
| 22653 |
ashik.ali |
69 |
@Component
|
|
|
70 |
public class SchemeServiceImpl implements SchemeService {
|
|
|
71 |
|
| 31410 |
amit.gupta |
72 |
private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
|
| 31916 |
amit.gupta |
73 |
private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
|
| 34709 |
amit.gupta |
74 |
private static final List<SchemeType> FULL_DAYS_CD_SCHEME_TYPES = Arrays.asList(SchemeType.CASH_DISCOUNT, SchemeType.CASH_DISCOUNT1);
|
|
|
75 |
private static final List<SchemeType> HALF_DAYS_CD_REJECT_SCHEME_TYPES = Arrays.asList(SchemeType.CASH_DISCOUNT1);
|
| 34017 |
amit.gupta |
76 |
|
| 34709 |
amit.gupta |
77 |
|
| 34317 |
amit.gupta |
78 |
private static final List<SchemeType> ACTIVATION_SCHEME_TYPES = Arrays.asList(SchemeType.SPECIAL_SUPPORT, SchemeType.SELLOUT);
|
| 33600 |
ranu |
79 |
|
| 31410 |
amit.gupta |
80 |
@Autowired
|
| 34221 |
tejus.loha |
81 |
SchemeBlockedImeiRepository schemeBlockedImeiRepository;
|
|
|
82 |
@Autowired
|
| 31410 |
amit.gupta |
83 |
StateGstRateRepository stateGstRateRepository;
|
|
|
84 |
@Autowired
|
|
|
85 |
NotificationService notificationService;
|
|
|
86 |
@Autowired
|
|
|
87 |
@Qualifier("fofoInventoryItemRepository")
|
|
|
88 |
private InventoryItemRepository inventoryItemRepository;
|
|
|
89 |
@Autowired
|
|
|
90 |
private ActivatedImeiRepository activatedImeiRepository;
|
|
|
91 |
@Autowired
|
|
|
92 |
private PartnerTypeChangeService partnerTypeChangeService;
|
|
|
93 |
@Autowired
|
|
|
94 |
private PurchaseService purchaseService;
|
|
|
95 |
@Autowired
|
|
|
96 |
private ScanRecordRepository scanRecordRepository;
|
|
|
97 |
@Autowired
|
|
|
98 |
private SessionFactory sessionFactory;
|
|
|
99 |
@Autowired
|
|
|
100 |
private SchemeRepository schemeRepository;
|
|
|
101 |
@Autowired
|
|
|
102 |
private PriceDropRepository priceDropRepository;
|
|
|
103 |
@Autowired
|
|
|
104 |
private RoleManager roleManager;
|
|
|
105 |
@Autowired
|
|
|
106 |
private RetailerRepository retailerRepository;
|
|
|
107 |
@Autowired
|
|
|
108 |
private ReporticoService reporticoService;
|
|
|
109 |
@Autowired
|
|
|
110 |
private TagListingRepository tagListingRepository;
|
|
|
111 |
@Autowired
|
|
|
112 |
private SchemeInOutRepository schemeInOutRepository;
|
|
|
113 |
@Autowired
|
|
|
114 |
@Qualifier("catalogItemRepository")
|
|
|
115 |
private ItemRepository itemRepository;
|
|
|
116 |
@Autowired
|
|
|
117 |
private SchemeItemRepository schemeItemRepository;
|
|
|
118 |
@Autowired
|
|
|
119 |
private SchemeRegionRepository schemeRegionRepository;
|
|
|
120 |
@Autowired
|
|
|
121 |
private WalletService walletService;
|
|
|
122 |
@Autowired
|
|
|
123 |
private PurchaseRepository purchaseRepository;
|
|
|
124 |
@Autowired
|
|
|
125 |
private FofoOrderRepository fofoOrderRepository;
|
| 31921 |
amit.gupta |
126 |
@Autowired
|
|
|
127 |
private PriceCircularService priceCircularService;
|
| 33600 |
ranu |
128 |
@Autowired
|
|
|
129 |
private SamsungExceptionRepository samsungExceptionRepository;
|
|
|
130 |
|
| 31410 |
amit.gupta |
131 |
@Override
|
| 34221 |
tejus.loha |
132 |
public List<String> getBlockedImeis() {
|
| 34254 |
tejus.loha |
133 |
List<SchemeBlockedImei> schemeBlockedImeis = schemeBlockedImeiRepository.selectAll();
|
| 34221 |
tejus.loha |
134 |
return schemeBlockedImeis.stream().filter(x -> x.isBlocked()).map(x -> x.getImei()).collect(Collectors.toList());
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
@Override
|
| 31410 |
amit.gupta |
138 |
public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
| 23444 |
amit.gupta |
139 |
|
| 31410 |
amit.gupta |
140 |
this.validateCreateSchemeRequest(createSchemeRequest);
|
| 27898 |
amit.gupta |
141 |
|
| 31410 |
amit.gupta |
142 |
Scheme scheme = this.toScheme(creatorId, createSchemeRequest);
|
| 25503 |
amit.gupta |
143 |
|
| 31410 |
amit.gupta |
144 |
if (scheme.getStartDateTime().isAfter(scheme.getEndDateTime())) {
|
|
|
145 |
throw new ProfitMandiBusinessException(
|
|
|
146 |
ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE,
|
|
|
147 |
scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
|
|
|
148 |
}
|
| 26722 |
amit.gupta |
149 |
|
| 31410 |
amit.gupta |
150 |
// this.validateItemIds(createSchemeRequest);
|
|
|
151 |
schemeRepository.persist(scheme);
|
|
|
152 |
for (int catalogId : createSchemeRequest.getCatalogIds()) {
|
|
|
153 |
SchemeItem schemeItem = new SchemeItem();
|
|
|
154 |
schemeItem.setSchemeId(scheme.getId());
|
|
|
155 |
schemeItem.setCatalogId(catalogId);
|
| 34317 |
amit.gupta |
156 |
schemeItem.setStartDate(createSchemeRequest.getStartDate());
|
|
|
157 |
schemeItem.setEndDate(createSchemeRequest.getEndDate());
|
| 31410 |
amit.gupta |
158 |
schemeItemRepository.persist(schemeItem);
|
|
|
159 |
}
|
| 23444 |
amit.gupta |
160 |
|
| 31410 |
amit.gupta |
161 |
for (int regionId : createSchemeRequest.getRegionIds()) {
|
|
|
162 |
SchemeRegion schemeRegion = new SchemeRegion();
|
|
|
163 |
schemeRegion.setSchemeId(scheme.getId());
|
|
|
164 |
schemeRegion.setRegionId(regionId);
|
|
|
165 |
schemeRegionRepository.persist(schemeRegion);
|
|
|
166 |
}
|
| 26332 |
amit.gupta |
167 |
|
| 31410 |
amit.gupta |
168 |
}
|
| 26722 |
amit.gupta |
169 |
|
| 31410 |
amit.gupta |
170 |
private void validateCreateSchemeRequest(CreateSchemeRequest createSchemeRequest)
|
|
|
171 |
throws ProfitMandiBusinessException {
|
|
|
172 |
if (createSchemeRequest.getName() == null || createSchemeRequest.getName().isEmpty()) {
|
|
|
173 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, createSchemeRequest.getName(),
|
|
|
174 |
"SCHM_VE_1000");
|
|
|
175 |
}
|
|
|
176 |
if (createSchemeRequest.getAmount() <= 0) {
|
|
|
177 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
178 |
"SCHM_VE_1001");
|
|
|
179 |
}
|
| 26684 |
amit.gupta |
180 |
|
| 31410 |
amit.gupta |
181 |
if (createSchemeRequest.getAmountType().equals(AmountType.PERCENTAGE)
|
|
|
182 |
&& createSchemeRequest.getAmount() > 100) {
|
|
|
183 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
184 |
"SCHM_VE_1002");
|
|
|
185 |
}
|
| 23444 |
amit.gupta |
186 |
|
| 31410 |
amit.gupta |
187 |
if (createSchemeRequest.getStartDate() == null) {
|
|
|
188 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, createSchemeRequest.getStartDate(),
|
|
|
189 |
"SCHM_VE_1003");
|
|
|
190 |
}
|
|
|
191 |
if (createSchemeRequest.getEndDate() == null) {
|
|
|
192 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, createSchemeRequest.getEndDate(),
|
|
|
193 |
"SCHM_VE_1004");
|
|
|
194 |
}
|
|
|
195 |
}
|
| 23798 |
amit.gupta |
196 |
|
| 31410 |
amit.gupta |
197 |
private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
|
|
198 |
if (createSchemeRequest.getCatalogIds() == null || createSchemeRequest.getCatalogIds().isEmpty()) {
|
|
|
199 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getCatalogIds(),
|
|
|
200 |
"SCHM_1003");
|
|
|
201 |
}
|
|
|
202 |
List<Integer> foundItemIds = itemRepository.selectIdsByIdsAndType(createSchemeRequest.getCatalogIds(),
|
|
|
203 |
ItemType.SERIALIZED);
|
|
|
204 |
if (foundItemIds.size() != createSchemeRequest.getCatalogIds().size()) {
|
|
|
205 |
createSchemeRequest.getCatalogIds().removeAll(foundItemIds);
|
|
|
206 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getCatalogIds(),
|
|
|
207 |
"SCHM_1004");
|
|
|
208 |
}
|
|
|
209 |
}
|
| 29927 |
amit.gupta |
210 |
|
| 31410 |
amit.gupta |
211 |
@Override
|
|
|
212 |
public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
213 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
|
|
214 |
List<Integer> catalogIds = schemeItemRepository.selectCatalogIdsBySchemeId(scheme.getId());
|
|
|
215 |
if (catalogIds.size() > 0) {
|
|
|
216 |
List<Item> items = itemRepository.selectAllByCatalogIds(new HashSet<>(catalogIds));
|
|
|
217 |
scheme.setCatalogStringMap(this.toCatalogStringMap(items));
|
|
|
218 |
}
|
|
|
219 |
return scheme;
|
|
|
220 |
}
|
| 24562 |
amit.gupta |
221 |
|
| 31410 |
amit.gupta |
222 |
public Map<Integer, String> toCatalogStringMap(List<Item> items) {
|
|
|
223 |
Map<Integer, String> catalogMap = new HashMap<>();
|
|
|
224 |
for (Item item : items) {
|
|
|
225 |
if (!catalogMap.containsKey(item.getCatalogItemId())) {
|
|
|
226 |
catalogMap.put(item.getCatalogItemId(), item.getItemDescriptionNoColor());
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
return catalogMap;
|
|
|
230 |
}
|
| 29927 |
amit.gupta |
231 |
|
| 31410 |
amit.gupta |
232 |
private Set<Integer> schemeItemsToCatalogIds(List<SchemeItem> schemeItems) {
|
|
|
233 |
Set<Integer> catalogId = new HashSet<>();
|
|
|
234 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
235 |
catalogId.add(schemeItem.getCatalogId());
|
|
|
236 |
}
|
|
|
237 |
return catalogId;
|
|
|
238 |
}
|
| 23444 |
amit.gupta |
239 |
|
| 31410 |
amit.gupta |
240 |
@Override
|
| 33248 |
ranu |
241 |
public List<SchemeModel> getAllSchemeModels(LocalDateTime startDateTime, LocalDateTime endDateTime) throws ProfitMandiBusinessException {
|
| 31410 |
amit.gupta |
242 |
List<Scheme> schemes = schemeRepository.selectAllBetweenCreateTimestamp(startDateTime, endDateTime);
|
|
|
243 |
Map<Integer, Scheme> schemeIdSchemeMap = schemes.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
244 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
|
|
245 |
Set<Integer> catalogIds = schemeItems.stream().map(x -> x.getCatalogId()).collect(Collectors.toSet());
|
|
|
246 |
List<Item> items = itemRepository.selectAllByCatalogIds(catalogIds);
|
|
|
247 |
Map<Integer, String> catalogStringMap = this.toCatalogStringMap(items);
|
|
|
248 |
this.addCatalogIdsToSchemes(schemeItems, schemeIdSchemeMap, catalogStringMap);
|
|
|
249 |
return this.toSchemeModels(schemeIdSchemeMap);
|
|
|
250 |
}
|
| 23444 |
amit.gupta |
251 |
|
| 31410 |
amit.gupta |
252 |
private List<SchemeModel> toSchemeModels(Map<Integer, Scheme> schemeIdSchemeMap) {
|
|
|
253 |
List<SchemeModel> schemeModels = new ArrayList<>();
|
|
|
254 |
for (Map.Entry<Integer, Scheme> schemeIdSchemeEntry : schemeIdSchemeMap.entrySet()) {
|
|
|
255 |
schemeModels.add(this.toSchemeModel(schemeIdSchemeEntry.getValue()));
|
|
|
256 |
}
|
|
|
257 |
return schemeModels;
|
|
|
258 |
}
|
| 23444 |
amit.gupta |
259 |
|
| 31410 |
amit.gupta |
260 |
private SchemeModel toSchemeModel(Scheme scheme) {
|
|
|
261 |
SchemeModel schemeModel = new SchemeModel();
|
|
|
262 |
schemeModel.setSchemeId(scheme.getId());
|
|
|
263 |
schemeModel.setName(scheme.getName());
|
|
|
264 |
schemeModel.setDescription(scheme.getDescription());
|
|
|
265 |
schemeModel.setSchemeType(scheme.getType().toString());
|
|
|
266 |
schemeModel.setAmountType(scheme.getAmountType().toString());
|
|
|
267 |
schemeModel.setAmount(scheme.getAmount());
|
|
|
268 |
schemeModel.setStartDateTime(StringUtils.toString(scheme.getStartDateTime()));
|
|
|
269 |
schemeModel.setEndDateTime(StringUtils.toString(scheme.getEndDateTime()));
|
|
|
270 |
schemeModel.setCreateTimestamp(StringUtils.toString(scheme.getCreateTimestamp()));
|
|
|
271 |
schemeModel.setActiveTimestamp(StringUtils.toString(scheme.getActiveTimestamp()));
|
|
|
272 |
schemeModel.setExpireTimestamp(StringUtils.toString(scheme.getExpireTimestamp()));
|
|
|
273 |
schemeModel.setCreatedBy(scheme.getCreatedBy());
|
|
|
274 |
schemeModel.setCatalogStringMap(scheme.getCatalogStringMap());
|
|
|
275 |
schemeModel.setRetailerIds(scheme.getRetailerIds());
|
|
|
276 |
return schemeModel;
|
|
|
277 |
}
|
| 23444 |
amit.gupta |
278 |
|
| 31410 |
amit.gupta |
279 |
private void addCatalogIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
|
|
|
280 |
Map<Integer, String> catalogStringMap) {
|
|
|
281 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
282 |
Scheme scheme = schemeIdSchemeMap.get(schemeItem.getSchemeId());
|
|
|
283 |
scheme.getCatalogStringMap().put(schemeItem.getCatalogId(), catalogStringMap.get(schemeItem.getCatalogId()));
|
|
|
284 |
}
|
|
|
285 |
}
|
| 31170 |
amit.gupta |
286 |
|
| 31410 |
amit.gupta |
287 |
@Override
|
|
|
288 |
public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
289 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
|
|
290 |
if (scheme.getActiveTimestamp() != null) {
|
|
|
291 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
292 |
"SCHM_1005");
|
|
|
293 |
}
|
|
|
294 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
295 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
296 |
"SCHM_1006");
|
|
|
297 |
}
|
|
|
298 |
scheme.setActiveTimestamp(LocalDateTime.now());
|
|
|
299 |
this.sendSchemeNotification(scheme);
|
| 23444 |
amit.gupta |
300 |
|
|
|
301 |
|
| 31410 |
amit.gupta |
302 |
/*
|
|
|
303 |
* if (scheme.getType() == SchemeType.IN) {
|
|
|
304 |
* this.processPreviousPurchases(scheme); } else if (scheme.getType() ==
|
|
|
305 |
* SchemeType.OUT) { this.processPreviousSales(scheme); }
|
|
|
306 |
*/
|
|
|
307 |
}
|
| 23796 |
amit.gupta |
308 |
|
| 34568 |
vikas.jang |
309 |
@Override
|
|
|
310 |
public void activeSchemeByIds(List<Scheme> schemes) throws ProfitMandiBusinessException {
|
|
|
311 |
if (schemes.size() > 0) {
|
|
|
312 |
for (Scheme scheme : schemes) {
|
|
|
313 |
if (scheme.getActiveTimestamp() != null || scheme.getExpireTimestamp() != null) {
|
|
|
314 |
continue;
|
|
|
315 |
}
|
|
|
316 |
scheme.setActiveTimestamp(LocalDateTime.now());
|
|
|
317 |
}
|
|
|
318 |
this.sendCombinedSchemesNotification(schemes);
|
|
|
319 |
}
|
|
|
320 |
}
|
|
|
321 |
|
| 31410 |
amit.gupta |
322 |
private void sendSchemeNotification(Scheme scheme) throws ProfitMandiBusinessException {
|
|
|
323 |
if (ACTIVATION_SCHEME_TYPES.contains(scheme.getType())) {
|
| 31336 |
amit.gupta |
324 |
|
| 31410 |
amit.gupta |
325 |
SendNotificationModel sendNotificationModel = new SendNotificationModel();
|
|
|
326 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
|
|
|
327 |
Set<Integer> catalogIds = schemeItems.stream().map(x -> x.getCatalogId()).collect(Collectors.toSet());
|
|
|
328 |
List<String> itemDescriptions = itemRepository.selectAllByCatalogIds(catalogIds).stream().filter(Utils.distinctByKey(Item::getCatalogItemId))
|
|
|
329 |
.map(x -> x.getItemDescriptionNoColor()).collect(Collectors.toList());
|
|
|
330 |
String titleTemplate = "%s of Rs.%s for %s";
|
|
|
331 |
String schemeString = "Activation scheme";
|
|
|
332 |
if (scheme.getType().equals(SchemeType.SPECIAL_SUPPORT)) {
|
|
|
333 |
schemeString = "Special Support";
|
|
|
334 |
}
|
| 30957 |
amit.gupta |
335 |
|
| 31410 |
amit.gupta |
336 |
String message = "Duration from - " + FormattingUtils.formatDateMonth(scheme.getStartDateTime()) + " - " + FormattingUtils.formatDateMonth(scheme.getEndDateTime());
|
|
|
337 |
sendNotificationModel.setCampaignName("activationscheme");
|
| 34902 |
vikas |
338 |
sendNotificationModel.setUrl("https://smartdukaan.com/pages/home/notifications");
|
| 31410 |
amit.gupta |
339 |
sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
|
|
|
340 |
sendNotificationModel.setMessage(message);
|
|
|
341 |
sendNotificationModel.setTitle(String.format(titleTemplate, schemeString, FormattingUtils.formatDecimal(scheme.getAmount()), org.apache.commons.lang3.StringUtils.abbreviate(String.join(", ", itemDescriptions), 25)));
|
|
|
342 |
sendNotificationModel.setType("url");
|
|
|
343 |
sendNotificationModel.setMessageType(MessageType.scheme);
|
|
|
344 |
notificationService.sendNotificationToAll(sendNotificationModel);
|
|
|
345 |
}
|
|
|
346 |
}
|
| 23444 |
amit.gupta |
347 |
|
| 34709 |
amit.gupta |
348 |
@Autowired
|
|
|
349 |
LoanRepository loanRepository;
|
| 34708 |
ranu |
350 |
|
| 31410 |
amit.gupta |
351 |
@Override
|
|
|
352 |
public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
|
|
|
353 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
|
|
354 |
if (scheme == null || scheme.getActiveTimestamp() == null) {
|
|
|
355 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
356 |
"SCHM_1007");
|
|
|
357 |
}
|
|
|
358 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
359 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
360 |
"SCHM_1008");
|
|
|
361 |
}
|
|
|
362 |
scheme.setExpireTimestamp(LocalDateTime.now());
|
|
|
363 |
if (expiryTime.isAfter(scheme.getEndDateTime())) {
|
|
|
364 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
365 |
"End Date cant be extended during expiry");
|
|
|
366 |
}
|
|
|
367 |
scheme.setEndDateTime(expiryTime);
|
|
|
368 |
schemeRepository.persist(scheme);
|
|
|
369 |
}
|
| 23444 |
amit.gupta |
370 |
|
| 31410 |
amit.gupta |
371 |
private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes) {
|
|
|
372 |
Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
|
|
|
373 |
for (Scheme scheme : schemes) {
|
|
|
374 |
schemeIdSchemeMap.put(scheme.getId(), scheme);
|
|
|
375 |
}
|
|
|
376 |
return schemeIdSchemeMap;
|
|
|
377 |
}
|
| 23444 |
amit.gupta |
378 |
|
| 31410 |
amit.gupta |
379 |
private Map<Integer, Set<Scheme>> toCatalogIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes) {
|
|
|
380 |
Map<Integer, Scheme> schemesMap = schemes.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
381 |
Map<Integer, Set<Scheme>> catalogSchemesMap = new HashMap<>();
|
|
|
382 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
383 |
if (!catalogSchemesMap.containsKey(schemeItem.getCatalogId())) {
|
|
|
384 |
catalogSchemesMap.put(schemeItem.getCatalogId(), new HashSet<>());
|
|
|
385 |
}
|
|
|
386 |
Set<Scheme> schemesSet = catalogSchemesMap.get(schemeItem.getCatalogId());
|
|
|
387 |
schemesSet.add(schemesMap.get(schemeItem.getSchemeId()));
|
|
|
388 |
}
|
|
|
389 |
return catalogSchemesMap;
|
|
|
390 |
}
|
| 23444 |
amit.gupta |
391 |
|
| 31410 |
amit.gupta |
392 |
private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
|
| 33248 |
ranu |
393 |
List<InventoryItem> inventoryItems) throws ProfitMandiBusinessException {
|
| 31410 |
amit.gupta |
394 |
Set<Integer> schemeIds = schemes.stream().map(x -> x.getId()).collect(Collectors.toSet());
|
|
|
395 |
Set<Integer> itemIds = inventoryItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
396 |
Set<Integer> catalogIds = itemRepository.selectByIds(itemIds).stream().map(x -> x.getCatalogItemId()).collect(Collectors.toSet());
|
|
|
397 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndCatalogIds(schemeIds, catalogIds);
|
| 23444 |
amit.gupta |
398 |
|
| 31410 |
amit.gupta |
399 |
Map<Integer, Set<Scheme>> catalogIdSchemesMap = this.toCatalogIdSchemesMap(schemeItems, schemes);
|
|
|
400 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap = new HashMap<>();
|
|
|
401 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
402 |
LOGGER.info("inventoryItem {}", inventoryItem);
|
|
|
403 |
LOGGER.info("inventoryItem.getItem() {}", inventoryItem.getItem());
|
|
|
404 |
LOGGER.info("catalogIdSchemesMap {}", catalogIdSchemesMap);
|
|
|
405 |
if (catalogIdSchemesMap.containsKey(inventoryItem.getItem().getCatalogItemId())) {
|
|
|
406 |
inventoryItemSchemesMap.put(inventoryItem, catalogIdSchemesMap.get(inventoryItem.getItem().getCatalogItemId()));
|
|
|
407 |
}
|
|
|
408 |
}
|
|
|
409 |
return inventoryItemSchemesMap;
|
|
|
410 |
}
|
| 31170 |
amit.gupta |
411 |
|
| 31903 |
amit.gupta |
412 |
@Autowired
|
|
|
413 |
OfferTargetSlabRepository offerTargetSlabRepository;
|
|
|
414 |
|
| 31410 |
amit.gupta |
415 |
private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest) {
|
|
|
416 |
Scheme scheme = new Scheme();
|
|
|
417 |
scheme.setName(createSchemeRequest.getName());
|
|
|
418 |
scheme.setDescription(createSchemeRequest.getDescription());
|
|
|
419 |
scheme.setType(createSchemeRequest.getType());
|
|
|
420 |
scheme.setAmountType(createSchemeRequest.getAmountType());
|
|
|
421 |
scheme.setAmount(createSchemeRequest.getAmount());
|
|
|
422 |
scheme.setPartnerType(createSchemeRequest.getPartnerType());
|
|
|
423 |
scheme.setStartDateTime(createSchemeRequest.getStartDate());
|
|
|
424 |
scheme.setEndDateTime(createSchemeRequest.getEndDate());
|
|
|
425 |
scheme.setCreatedBy(creatorId);
|
|
|
426 |
scheme.setCashback(createSchemeRequest.isCashback());
|
|
|
427 |
return scheme;
|
|
|
428 |
}
|
| 23444 |
amit.gupta |
429 |
|
| 31903 |
amit.gupta |
430 |
@Autowired
|
|
|
431 |
OfferRepository offerRepository;
|
|
|
432 |
@Autowired
|
|
|
433 |
OfferPayoutRepository offerPayoutRepository;
|
|
|
434 |
@Autowired
|
|
|
435 |
AgeingService ageingService;
|
| 31588 |
amit.gupta |
436 |
|
| 34709 |
amit.gupta |
437 |
private void sendCombinedSchemesNotification(List<Scheme> schemes) throws ProfitMandiBusinessException {
|
|
|
438 |
if (schemes == null || schemes.isEmpty()) return;
|
|
|
439 |
|
|
|
440 |
SendNotificationModel sendNotificationModel = new SendNotificationModel();
|
|
|
441 |
StringBuilder messageBuilder = new StringBuilder();
|
|
|
442 |
Set<Integer> allCatalogIds = new HashSet<>();
|
| 34962 |
vikas |
443 |
Set<String> allBrands = new HashSet<>();
|
|
|
444 |
String schemeTypeLabel = null;
|
| 34709 |
amit.gupta |
445 |
|
| 34962 |
vikas |
446 |
Map<String, List<Scheme>> groupedByDate = schemes.stream()
|
|
|
447 |
.filter(s -> ACTIVATION_SCHEME_TYPES.contains(s.getType()))
|
|
|
448 |
.collect(Collectors.groupingBy(s ->
|
|
|
449 |
StringUtils.fromShortMonthDate(String.valueOf(s.getStartDateTime())) + " - " +
|
|
|
450 |
StringUtils.fromShortMonthDate(String.valueOf(s.getEndDateTime()))
|
|
|
451 |
));
|
| 34709 |
amit.gupta |
452 |
|
| 34962 |
vikas |
453 |
for (Map.Entry<String, List<Scheme>> entry : groupedByDate.entrySet()) {
|
|
|
454 |
String dateRange = entry.getKey();
|
|
|
455 |
List<Scheme> groupedSchemes = entry.getValue();
|
| 34709 |
amit.gupta |
456 |
|
| 34962 |
vikas |
457 |
messageBuilder.append(String.format("Duration: %s\n", dateRange));
|
| 34709 |
amit.gupta |
458 |
|
| 34962 |
vikas |
459 |
for (Scheme scheme : groupedSchemes) {
|
|
|
460 |
if (scheme.getType().equals(SchemeType.SPECIAL_SUPPORT)) {
|
|
|
461 |
schemeTypeLabel = "Special Support";
|
|
|
462 |
} else {
|
|
|
463 |
schemeTypeLabel = "Activation Scheme";
|
|
|
464 |
}
|
| 34709 |
amit.gupta |
465 |
|
| 34962 |
vikas |
466 |
List<SchemeItem> schemeItems =
|
|
|
467 |
schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
|
| 34709 |
amit.gupta |
468 |
|
| 34962 |
vikas |
469 |
Set<Integer> catalogIds = schemeItems.stream()
|
|
|
470 |
.map(SchemeItem::getCatalogId)
|
|
|
471 |
.collect(Collectors.toSet());
|
|
|
472 |
allCatalogIds.addAll(catalogIds);
|
| 34709 |
amit.gupta |
473 |
|
| 34962 |
vikas |
474 |
List<String> itemBrand = itemRepository.selectAllByCatalogIds(catalogIds).stream()
|
|
|
475 |
.map(Item::getBrand)
|
|
|
476 |
.filter(Objects::nonNull)
|
|
|
477 |
.distinct()
|
|
|
478 |
.collect(Collectors.toList());
|
|
|
479 |
allBrands.addAll(itemBrand);
|
|
|
480 |
|
|
|
481 |
List<String> itemDescriptions = itemRepository.selectAllByCatalogIds(catalogIds).stream()
|
|
|
482 |
.filter(Utils.distinctByKey(Item::getCatalogItemId))
|
|
|
483 |
.map(Item::getItemDescriptionNoColor)
|
|
|
484 |
.collect(Collectors.toList());
|
|
|
485 |
|
|
|
486 |
messageBuilder.append(String.format("%s of Rs.%s on Models: %s\n",
|
|
|
487 |
schemeTypeLabel,
|
|
|
488 |
FormattingUtils.formatDecimal(scheme.getAmount()),
|
|
|
489 |
String.join(", ", itemDescriptions)));
|
|
|
490 |
}
|
|
|
491 |
messageBuilder.append("\n");
|
| 34709 |
amit.gupta |
492 |
}
|
|
|
493 |
|
| 34962 |
vikas |
494 |
if (allBrands.isEmpty() || schemeTypeLabel == null) return;
|
| 34709 |
amit.gupta |
495 |
|
| 34962 |
vikas |
496 |
String title = String.format("%s for %s",
|
|
|
497 |
schemeTypeLabel,
|
|
|
498 |
String.join(", ", allBrands));
|
|
|
499 |
|
| 34709 |
amit.gupta |
500 |
sendNotificationModel.setCampaignName("activationscheme");
|
| 34962 |
vikas |
501 |
sendNotificationModel.setUrl("https://store.smartdukaan.com/pages/home/notifications");
|
| 34709 |
amit.gupta |
502 |
sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
|
| 34962 |
vikas |
503 |
sendNotificationModel.setTitle(title);
|
| 34709 |
amit.gupta |
504 |
sendNotificationModel.setMessage(messageBuilder.toString().trim());
|
|
|
505 |
sendNotificationModel.setType("url");
|
|
|
506 |
sendNotificationModel.setMessageType(MessageType.scheme);
|
|
|
507 |
notificationService.sendNotificationToAll(sendNotificationModel);
|
|
|
508 |
}
|
|
|
509 |
|
|
|
510 |
//CDs would be rejected based on cdFreeDays
|
| 31903 |
amit.gupta |
511 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
| 34504 |
amit.gupta |
512 |
LOGGER.info("Processing scheme in for purchaseId - {}", purchaseId);
|
| 31903 |
amit.gupta |
513 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
| 34709 |
amit.gupta |
514 |
String purchaseInvoice = purchase.getPurchaseReference();
|
|
|
515 |
|
|
|
516 |
Loan loan = loanRepository.selectLoanByInvoice(purchaseInvoice);
|
|
|
517 |
|
| 34317 |
amit.gupta |
518 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
|
|
|
519 |
YearMonth.from(purchase.getCreateTimestamp()));
|
| 34139 |
amit.gupta |
520 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
|
| 31903 |
amit.gupta |
521 |
//Remove imeis from blocked imeis list
|
| 34317 |
amit.gupta |
522 |
List<String> blockedImeis = this.getBlockedImeis();
|
|
|
523 |
inventoryItems = inventoryItems.stream().filter(inventoryItem -> !blockedImeis.contains(inventoryItem.getSerialNumber())).collect(Collectors.toList());
|
| 31903 |
amit.gupta |
524 |
if (inventoryItems.size() == 0) return;
|
| 23444 |
amit.gupta |
525 |
|
| 31903 |
amit.gupta |
526 |
Set<Integer> itemIds = inventoryItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
527 |
Map<Integer, Item> itemsMap = itemRepository.selectByIds(itemIds).stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
528 |
inventoryItems.stream().forEach(x -> x.setItem(itemsMap.get(x.getItemId())));
|
| 23444 |
amit.gupta |
529 |
|
|
|
530 |
|
| 34317 |
amit.gupta |
531 |
LocalDateTime billingDate = purchaseService.getBillingDateOfPurchase(purchaseId);
|
| 31903 |
amit.gupta |
532 |
Set<Integer> itemIdsSet = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds).stream()
|
|
|
533 |
.filter(x -> x.getEolDate() == null || x.getEolDate().isAfter(billingDate)).map(x -> x.getItemId())
|
|
|
534 |
.collect(Collectors.toSet());
|
|
|
535 |
// Only consider inventory items that were not returned and not eol
|
|
|
536 |
inventoryItems = inventoryItems.stream().filter(x -> itemIdsSet.contains(x.getItemId()))
|
|
|
537 |
.filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET_BAD))
|
|
|
538 |
.filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET)).collect(Collectors.toList());
|
|
|
539 |
|
|
|
540 |
if (inventoryItems.size() == 0) return;
|
|
|
541 |
|
|
|
542 |
Map<Integer, List<InventoryItem>> catalogInventoryItemMap = inventoryItems.stream().collect(Collectors.groupingBy(x -> x.getItem().getCatalogItemId()));
|
| 34317 |
amit.gupta |
543 |
Map<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSchemeSummaryMap = tagListingRepository.getModelSchemesByCatalogIdsAndType(retailerId,
|
| 32454 |
amit.gupta |
544 |
partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), billingDate);
|
| 31903 |
amit.gupta |
545 |
|
|
|
546 |
|
|
|
547 |
int itemsCount = 0;
|
|
|
548 |
float totalCashback = 0;
|
| 34709 |
amit.gupta |
549 |
//LOGGER.info("catalogSchemeSummaryMap - {}", catalogSchemeSummaryMap);
|
| 31903 |
amit.gupta |
550 |
for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
|
|
|
551 |
CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
|
| 34504 |
amit.gupta |
552 |
List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue().stream().filter(Objects::nonNull).collect(Collectors.toList());
|
| 34317 |
amit.gupta |
553 |
schemeSummaryModels.stream().filter(x -> x != null && x.getSchemeType().getTransactionType().equals(StockTransactionType.IN)).forEach(x -> x.setProcess(true));
|
| 31903 |
amit.gupta |
554 |
if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
|
|
|
555 |
List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
|
|
|
556 |
for (InventoryItem inventoryItem : modelInventoryItems) {
|
| 32165 |
amit.gupta |
557 |
float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
|
| 34709 |
amit.gupta |
558 |
if (loan != null && loan.getCdFreeDays() > 0) {
|
|
|
559 |
List<SchemeType> rejectedCDTypes = HALF_DAYS_CD_REJECT_SCHEME_TYPES;
|
|
|
560 |
if (loan.getCdFreeDays() == ProfitMandiConstants.LOAN_FULL_CREDIT_DAYS) {
|
|
|
561 |
rejectedCDTypes = FULL_DAYS_CD_SCHEME_TYPES;
|
|
|
562 |
}
|
|
|
563 |
for (SchemeType rejectedCDType : rejectedCDTypes) {
|
| 34800 |
amit.gupta |
564 |
SchemeInOut sio = schemeInOutRepository.selectAllByType(rejectedCDType, inventoryItem.getId()).size() > 0 ?
|
|
|
565 |
schemeInOutRepository.selectAllByType(rejectedCDType, inventoryItem.getId()).get(0) : null;
|
|
|
566 |
if (sio == null) continue;
|
| 34709 |
amit.gupta |
567 |
sio.setStatus(SchemePayoutStatus.REJECTED);
|
| 34850 |
amit |
568 |
//Noone should change this
|
| 34709 |
amit.gupta |
569 |
sio.setStatusDescription("Rejected due to free days availed");
|
|
|
570 |
sio.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
571 |
inventoryItemCashback -= sio.getAmount();
|
|
|
572 |
}
|
|
|
573 |
|
|
|
574 |
}
|
|
|
575 |
if (inventoryItemCashback > 0.01f) {
|
| 31903 |
amit.gupta |
576 |
itemsCount++;
|
|
|
577 |
totalCashback += inventoryItemCashback;
|
| 31410 |
amit.gupta |
578 |
}
|
|
|
579 |
}
|
| 31903 |
amit.gupta |
580 |
}
|
| 22653 |
ashik.ali |
581 |
|
| 29927 |
amit.gupta |
582 |
|
| 34317 |
amit.gupta |
583 |
LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
|
| 31903 |
amit.gupta |
584 |
if (itemsCount > 0) {
|
|
|
585 |
walletService.addAmountToWallet(
|
| 34317 |
amit.gupta |
586 |
retailerId, purchaseId, WalletReferenceType.SCHEME_IN, "Added for SCHEME IN against invoice "
|
| 31903 |
amit.gupta |
587 |
+ purchase.getPurchaseReference() + " (total " + itemsCount + " pcs)",
|
|
|
588 |
totalCashback, purchase.getCreateTimestamp());
|
|
|
589 |
LOGGER.info("Added Rs.{} for SCHEME IN against invoice {} total pcs({}) {}", totalCashback,
|
|
|
590 |
purchase.getPurchaseReference(), itemsCount);
|
| 34504 |
amit.gupta |
591 |
}
|
| 34317 |
amit.gupta |
592 |
|
| 31903 |
amit.gupta |
593 |
}
|
|
|
594 |
|
|
|
595 |
|
| 31410 |
amit.gupta |
596 |
// We are maintaining price drop after grn
|
|
|
597 |
private float getAmount(InventoryItem inventoryItem, Scheme scheme) throws ProfitMandiBusinessException {
|
| 34221 |
tejus.loha |
598 |
if (this.getBlockedImeis().contains(inventoryItem.getSerialNumber())) {
|
| 31410 |
amit.gupta |
599 |
return 0;
|
|
|
600 |
}
|
|
|
601 |
float amount = 0;
|
|
|
602 |
float dpForCalc = 0;
|
|
|
603 |
float taxableSellingPrice = 0;
|
| 29927 |
amit.gupta |
604 |
|
| 31410 |
amit.gupta |
605 |
//float totalTaxRate = stateGstRateRepository.getTotalTaxRate(inventoryItem.getItemId());
|
|
|
606 |
if (scheme.getAmountType().equals(AmountType.PERCENTAGE)) {
|
| 34317 |
amit.gupta |
607 |
if (scheme.getType().getTransactionType().equals(StockTransactionType.IN)) {
|
| 31410 |
amit.gupta |
608 |
dpForCalc = inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount();
|
|
|
609 |
} else {
|
|
|
610 |
try {
|
|
|
611 |
dpForCalc = Math.min(inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount(),
|
|
|
612 |
tagListingRepository.selectByItemId(inventoryItem.getItemId()).getSellingPrice());
|
|
|
613 |
} catch (Exception e) {
|
|
|
614 |
LOGGER.info("Could not find tag Listing entry in {}", inventoryItem.getItemId());
|
|
|
615 |
e.printStackTrace();
|
|
|
616 |
}
|
|
|
617 |
}
|
|
|
618 |
//TODO:Should be calculated on unit price
|
|
|
619 |
//taxableSellingPrice = dpForCalc / (1 + totalTaxRate / 100);
|
|
|
620 |
//amount = taxableSellingPrice * scheme.getAmount() / 100;
|
|
|
621 |
amount = dpForCalc * scheme.getAmount() / 100;
|
|
|
622 |
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(),
|
|
|
623 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
|
|
624 |
scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
|
|
|
625 |
scheme.getAmount(), amount));
|
| 34317 |
amit.gupta |
626 |
} else if (scheme.getType().getTransactionType().equals(StockTransactionType.IN)) {
|
| 31410 |
amit.gupta |
627 |
amount = scheme.getAmount();
|
|
|
628 |
}
|
|
|
629 |
return amount;
|
|
|
630 |
}
|
| 23444 |
amit.gupta |
631 |
|
| 31903 |
amit.gupta |
632 |
//Only in and activation margins are allowed to be rolled out more than twice
|
| 32165 |
amit.gupta |
633 |
private float createSchemeInOut(List<SchemeSummaryModel> schemeSummaryModels, InventoryItem inventoryItem) throws ProfitMandiBusinessException {
|
| 33614 |
amit.gupta |
634 |
LOGGER.info("schemeSummaryModels - {}", schemeSummaryModels);
|
| 31921 |
amit.gupta |
635 |
InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(inventoryItem);
|
| 31903 |
amit.gupta |
636 |
//Get all schemes
|
|
|
637 |
List<SchemeSummaryModel> inventoryPayoutModelToProcess = schemeSummaryModels.stream().filter(x -> x.isProcess()).collect(Collectors.toList());
|
| 34709 |
amit.gupta |
638 |
LOGGER.info("inventoryPayoutModel - {}", inventoryPayoutModel);
|
| 32165 |
amit.gupta |
639 |
List<SchemeInOut> paidSios = inventoryPayoutModel.getPaidSios();
|
|
|
640 |
List<SchemeInOut> pendingSios = inventoryPayoutModel.getPendingSios();
|
|
|
641 |
Map<SchemeType, List<SchemeInOut>> paidSchemeTypesMap = inventoryPayoutModel.getPaidSios().stream().collect(Collectors.groupingBy(x -> x.getScheme().getType()));
|
|
|
642 |
Map<Integer, SchemeInOut> paidSchemesMap = paidSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
|
|
|
643 |
Map<Integer, SchemeInOut> pendingSchemesMap = pendingSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
|
| 32155 |
amit.gupta |
644 |
Map<SchemeType, Float> schemeTypeCancelledAmountMap = new HashMap<>();
|
| 31903 |
amit.gupta |
645 |
|
|
|
646 |
double percentageToPay = 0d;
|
|
|
647 |
double fixedToPay = 0d;
|
|
|
648 |
Map<SchemeSummaryModel, AmountModel> payoutSchemeSummaryModelMap = new HashMap<>();
|
|
|
649 |
for (SchemeSummaryModel schemeSummaryModelToProcess : inventoryPayoutModelToProcess) {
|
| 32972 |
amit.gupta |
650 |
if (paidSchemesMap.containsKey(schemeSummaryModelToProcess.getSchemeId()) || pendingSchemesMap.containsKey(schemeSummaryModelToProcess.getSchemeId()))
|
|
|
651 |
continue;
|
| 34778 |
amit.gupta |
652 |
//All Valid margins are supposed to be credited invalid margins should be rejected
|
|
|
653 |
if (SchemeType.SPECIAL_SUPPORT.equals(schemeSummaryModelToProcess.getSchemeType())) {
|
|
|
654 |
//Create only if the activation date is not known
|
| 32165 |
amit.gupta |
655 |
ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(inventoryItem.getSerialNumber());
|
| 32972 |
amit.gupta |
656 |
if (activatedImei == null || activatedImei.getActivationTimestamp() == null) {
|
| 32165 |
amit.gupta |
657 |
SchemeInOut sio = new SchemeInOut();
|
|
|
658 |
sio.setAmount(0);
|
|
|
659 |
sio.setInventoryItemId(inventoryItem.getId());
|
|
|
660 |
sio.setSchemeId(schemeSummaryModelToProcess.getSchemeId());
|
|
|
661 |
sio.setStatusDescription("Activation pending for IMEI#" + inventoryItem.getSerialNumber());
|
|
|
662 |
sio.setStatus(SchemePayoutStatus.PENDING);
|
|
|
663 |
schemeInOutRepository.persist(sio);
|
|
|
664 |
}
|
| 34317 |
amit.gupta |
665 |
} else if (!StockTransactionType.IN.equals(schemeSummaryModelToProcess.getSchemeType().getTransactionType())) {
|
| 31903 |
amit.gupta |
666 |
//We have got non repeating scheme type
|
| 34709 |
amit.gupta |
667 |
|
|
|
668 |
if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
669 |
percentageToPay += schemeSummaryModelToProcess.getAmount();
|
| 31903 |
amit.gupta |
670 |
} else {
|
| 34709 |
amit.gupta |
671 |
fixedToPay += schemeSummaryModelToProcess.getAmount();
|
| 31903 |
amit.gupta |
672 |
}
|
| 34709 |
amit.gupta |
673 |
payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
|
|
|
674 |
|
| 31903 |
amit.gupta |
675 |
} else {
|
|
|
676 |
if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
|
| 34709 |
amit.gupta |
677 |
//Check for rejected CashDiscounts dont continue if its ever rejected once
|
|
|
678 |
if (SchemeType.CDS.contains(schemeSummaryModelToProcess.getSchemeType())) {
|
|
|
679 |
List<SchemeInOut> cdSchemeInOuts = schemeInOutRepository.selectAllByType(schemeSummaryModelToProcess.getSchemeType(), inventoryItem.getId());
|
|
|
680 |
LOGGER.info(cdSchemeInOuts);
|
| 34850 |
amit |
681 |
if (!cdSchemeInOuts.isEmpty() && cdSchemeInOuts.get(0).getStatusDescription().equals("Rejected due to free days availed"))
|
|
|
682 |
continue;
|
| 34709 |
amit.gupta |
683 |
}
|
| 31903 |
amit.gupta |
684 |
percentageToPay += schemeSummaryModelToProcess.getAmount();
|
|
|
685 |
} else {
|
|
|
686 |
fixedToPay += schemeSummaryModelToProcess.getAmount();
|
|
|
687 |
}
|
|
|
688 |
payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
|
|
|
689 |
}
|
|
|
690 |
}
|
|
|
691 |
double walletCredit = 0d;
|
|
|
692 |
if (fixedToPay > 0) {
|
| 31913 |
amit.gupta |
693 |
double fixedRollout = fixedToPay * (100 / (100 + inventoryPayoutModel.getPercentageAmount()));
|
| 31903 |
amit.gupta |
694 |
for (Map.Entry<SchemeSummaryModel, AmountModel> schemeSummaryModelAmountModelEntry : payoutSchemeSummaryModelMap.entrySet()) {
|
|
|
695 |
SchemeSummaryModel schemeSummaryModel = schemeSummaryModelAmountModelEntry.getKey();
|
|
|
696 |
AmountModel amountModel = schemeSummaryModelAmountModelEntry.getValue();
|
|
|
697 |
if (amountModel.getAmountType().equals(AmountType.FIXED)) {
|
|
|
698 |
SchemeInOut sio = new SchemeInOut();
|
|
|
699 |
sio.setSchemeId(schemeSummaryModel.getSchemeId());
|
|
|
700 |
sio.setInventoryItemId(inventoryItem.getId());
|
|
|
701 |
sio.setStatus(SchemePayoutStatus.CREDITED);
|
|
|
702 |
sio.setCreditTimestamp(LocalDateTime.now());
|
| 32155 |
amit.gupta |
703 |
sio.setAmount((float) (fixedRollout * amountModel.getAmount() / fixedToPay) + schemeTypeCancelledAmountMap.getOrDefault(schemeSummaryModel.getSchemeType(), 0f));
|
| 34317 |
amit.gupta |
704 |
if (schemeSummaryModel.getSchemeType().getTransactionType().equals(StockTransactionType.IN))
|
| 31903 |
amit.gupta |
705 |
sio.setStatusDescription("Credited for GRN of IMEI-" + inventoryItem.getSerialNumber());
|
|
|
706 |
else
|
|
|
707 |
sio.setStatusDescription("Credited for Sale of IMEI-" + inventoryItem.getSerialNumber());
|
|
|
708 |
schemeInOutRepository.persist(sio);
|
|
|
709 |
}
|
|
|
710 |
}
|
|
|
711 |
walletCredit += fixedRollout;
|
|
|
712 |
}
|
|
|
713 |
|
|
|
714 |
if (percentageToPay > 0) {
|
| 31987 |
amit.gupta |
715 |
LOGGER.info("inventoryPayoutModel.getFixedAmount() ----> {}", inventoryPayoutModel.getFixedAmount());
|
| 31914 |
amit.gupta |
716 |
double effectiveDP = inventoryPayoutModel.getDp() - (inventoryPayoutModel.getFixedAmount() + fixedToPay);
|
| 31903 |
amit.gupta |
717 |
double totalPercentage = inventoryPayoutModel.getPercentageAmount() + percentageToPay;
|
|
|
718 |
double percentageRollout = effectiveDP * (totalPercentage / (100 + totalPercentage) - (inventoryPayoutModel.getPercentageAmount() / (100 + inventoryPayoutModel.getPercentageAmount())));
|
|
|
719 |
for (Map.Entry<SchemeSummaryModel, AmountModel> schemeSummaryModelAmountModelEntry : payoutSchemeSummaryModelMap.entrySet()) {
|
|
|
720 |
SchemeSummaryModel schemeSummaryModel = schemeSummaryModelAmountModelEntry.getKey();
|
|
|
721 |
AmountModel amountModel = schemeSummaryModelAmountModelEntry.getValue();
|
|
|
722 |
if (amountModel.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
723 |
SchemeInOut sio = new SchemeInOut();
|
|
|
724 |
sio.setInventoryItemId(inventoryItem.getId());
|
|
|
725 |
sio.setSchemeId(schemeSummaryModel.getSchemeId());
|
|
|
726 |
sio.setStatus(SchemePayoutStatus.CREDITED);
|
|
|
727 |
sio.setCreditTimestamp(LocalDateTime.now());
|
| 32155 |
amit.gupta |
728 |
sio.setAmount((float) (percentageRollout * amountModel.getAmount() / percentageToPay) +
|
|
|
729 |
schemeTypeCancelledAmountMap.getOrDefault(schemeSummaryModel.getSchemeType(), 0f));
|
| 34317 |
amit.gupta |
730 |
if (schemeSummaryModel.getSchemeType().getTransactionType().equals(StockTransactionType.IN))
|
| 31903 |
amit.gupta |
731 |
sio.setStatusDescription("Credited for GRN of IMEI-" + inventoryItem.getSerialNumber());
|
|
|
732 |
else
|
|
|
733 |
sio.setStatusDescription("Credited for Sale of IMEI-" + inventoryItem.getSerialNumber());
|
|
|
734 |
schemeInOutRepository.persist(sio);
|
|
|
735 |
}
|
|
|
736 |
}
|
|
|
737 |
walletCredit += percentageRollout;
|
|
|
738 |
}
|
|
|
739 |
|
|
|
740 |
return (float) walletCredit;
|
|
|
741 |
}
|
|
|
742 |
|
|
|
743 |
private Set<Integer> filterImeisByAgeing(Set<Integer> inventoryItemIds, FofoOrder fofoOrder) {
|
|
|
744 |
Set<Integer> filteredInventoryIds = new HashSet<>();
|
|
|
745 |
List<PartnerAgeingModel> partnerAgeingModels = ageingService.filterAgedInventory(inventoryItemIds);
|
|
|
746 |
for (PartnerAgeingModel partnerAgeingModel : partnerAgeingModels) {
|
|
|
747 |
LOGGER.info("Serial Number - {}", partnerAgeingModel.getSerialNumber());
|
|
|
748 |
if (partnerAgeingModel.getBrand().equalsIgnoreCase("Samsung")) {
|
|
|
749 |
ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(partnerAgeingModel.getSerialNumber());
|
|
|
750 |
if (activatedImei != null && activatedImei.getActivationTimestamp().toLocalDate()
|
|
|
751 |
.isBefore(partnerAgeingModel.getBillingDate().plusDays(partnerAgeingModel.getMaxAgeingDays()))) {
|
|
|
752 |
//Lets give money if activation is there and is before the ageing limit of that brand.
|
|
|
753 |
filteredInventoryIds.add(partnerAgeingModel.getInventoryItemId());
|
|
|
754 |
} else {
|
|
|
755 |
//If billing happens before ageing expiry
|
|
|
756 |
if (fofoOrder.getCreateTimestamp().toLocalDate().isBefore(partnerAgeingModel.getBillingDate().plusDays(partnerAgeingModel.getMaxAgeingDays()))) {
|
|
|
757 |
filteredInventoryIds.add(partnerAgeingModel.getInventoryItemId());
|
|
|
758 |
}
|
|
|
759 |
}
|
|
|
760 |
} else {
|
|
|
761 |
filteredInventoryIds.add(partnerAgeingModel.getInventoryItemId());
|
|
|
762 |
}
|
|
|
763 |
}
|
|
|
764 |
return filteredInventoryIds;
|
|
|
765 |
}
|
|
|
766 |
|
| 33087 |
amit.gupta |
767 |
@Autowired
|
|
|
768 |
WarehouseInventoryItemRepository warehouseInventoryItemRepository;
|
|
|
769 |
|
| 31410 |
amit.gupta |
770 |
@Override
|
|
|
771 |
public float processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
|
| 34746 |
amit.gupta |
772 |
|
|
|
773 |
float totalCashback = 0;
|
| 31410 |
amit.gupta |
774 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(retailerId, fofoOrderId);
|
|
|
775 |
// Process only if order is not cancelled
|
|
|
776 |
if (fofoOrder.getCancelledTimestamp() == null) {
|
|
|
777 |
// PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId,
|
|
|
778 |
// fofoOrder.getCreateTimestamp().toLocalDate());
|
|
|
779 |
// TODO - SCHEME
|
|
|
780 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
|
|
|
781 |
YearMonth.from(fofoOrder.getCreateTimestamp()));
|
| 34778 |
amit.gupta |
782 |
LOGGER.info("OrderDate - {}, Partner Type - {}", fofoOrder.getCreateTimestamp(), partnerType);
|
| 29927 |
amit.gupta |
783 |
|
| 31410 |
amit.gupta |
784 |
List<ScanRecord> scanRecords = scanRecordRepository.selectAllByOrderId(fofoOrderId);
|
|
|
785 |
if (scanRecords.size() == 0) return 0;
|
|
|
786 |
Set<Integer> inventoryItemIds = scanRecords.stream().map(x -> x.getInventoryItemId())
|
|
|
787 |
.collect(Collectors.toSet());
|
| 31903 |
amit.gupta |
788 |
//Check for ageing
|
| 31912 |
amit.gupta |
789 |
//inventoryItemIds = this.filterImeisByAgeing(inventoryItemIds, fofoOrder);
|
| 31903 |
amit.gupta |
790 |
//ageingService.filterAgedInventory(inventoryItemIds);
|
| 31410 |
amit.gupta |
791 |
LOGGER.info("fofoOrderId --- {}", fofoOrderId);
|
|
|
792 |
LOGGER.info("scanRecords --- {}", scanRecords);
|
|
|
793 |
LOGGER.info("inventoryItemIds --- {}", inventoryItemIds);
|
| 31903 |
amit.gupta |
794 |
|
|
|
795 |
if (inventoryItemIds.size() == 0) return 0;
|
|
|
796 |
|
| 31410 |
amit.gupta |
797 |
Set<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIds).stream()
|
|
|
798 |
.filter(x -> x.getSerialNumber() != null && !x.getSerialNumber().equals(""))
|
|
|
799 |
.collect(Collectors.toSet());
|
| 34221 |
tejus.loha |
800 |
inventoryItems = inventoryItems.stream().filter(inventoryItem -> !this.getBlockedImeis().contains(inventoryItem.getSerialNumber())).collect(Collectors.toSet());
|
| 29927 |
amit.gupta |
801 |
|
| 33087 |
amit.gupta |
802 |
//Do not consider imei above 90 days for samsung
|
| 33432 |
amit.gupta |
803 |
List<String> samsungSerialNumbers = inventoryItems.stream().filter(x -> x.getItem().getBrand().equalsIgnoreCase("samsung")).map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
| 33600 |
ranu |
804 |
|
| 33432 |
amit.gupta |
805 |
if (samsungSerialNumbers.size() > 0) {
|
| 33087 |
amit.gupta |
806 |
List<AgeingSummaryModel> billedImeiModels = warehouseInventoryItemRepository.findStockAgeingByFofoIdSerialNumbers(retailerId, samsungSerialNumbers);
|
| 33606 |
ranu |
807 |
List<String> agedSerialNumbers = billedImeiModels.stream().filter(x -> x.isAgedAbove(365)).map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
|
|
808 |
if (agedSerialNumbers.size() > 0) {
|
|
|
809 |
List<String> samsungExceptionsSerialNumbers = samsungExceptionRepository.selectAllBySerialNumber(agedSerialNumbers).stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
|
|
810 |
agedSerialNumbers.removeAll(samsungExceptionsSerialNumbers);
|
|
|
811 |
}
|
| 33432 |
amit.gupta |
812 |
inventoryItems = inventoryItems.stream().filter(x -> !agedSerialNumbers.contains(x.getSerialNumber())).collect(Collectors.toSet());
|
| 33087 |
amit.gupta |
813 |
}
|
|
|
814 |
|
| 31903 |
amit.gupta |
815 |
if (inventoryItems.size() == 0) return 0;
|
| 29927 |
amit.gupta |
816 |
|
| 31903 |
amit.gupta |
817 |
Map<Integer, List<InventoryItem>> catalogInventoryItemMap = inventoryItems.stream().collect(Collectors.groupingBy(x -> x.getItem().getCatalogItemId()));
|
|
|
818 |
Map<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSchemeSummaryMap = tagListingRepository.getModelSchemesByCatalogIdsAndType(retailerId,
|
|
|
819 |
partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), fofoOrder.getCreateTimestamp());
|
| 23444 |
amit.gupta |
820 |
|
| 34317 |
amit.gupta |
821 |
LOGGER.info("catalogSchemeSummaryMap - {}", catalogSchemeSummaryMap);
|
| 31903 |
amit.gupta |
822 |
|
| 34317 |
amit.gupta |
823 |
|
| 31410 |
amit.gupta |
824 |
int count = 0;
|
| 31903 |
amit.gupta |
825 |
for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
|
|
|
826 |
CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
|
| 32058 |
amit.gupta |
827 |
List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue().stream().filter(x -> x != null).collect(Collectors.toList());
|
| 34317 |
amit.gupta |
828 |
|
|
|
829 |
schemeSummaryModels.stream().filter(x -> x.getSchemeType().getTransactionType().equals(StockTransactionType.OUT)).forEach(x -> x.setProcess(true));
|
| 31903 |
amit.gupta |
830 |
if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
|
| 23444 |
amit.gupta |
831 |
|
| 31903 |
amit.gupta |
832 |
List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
|
|
|
833 |
for (InventoryItem inventoryItem : modelInventoryItems) {
|
| 34698 |
amit.gupta |
834 |
Map<Integer, SchemeSummaryModel> schemeSummaryModelMap = schemeSummaryModels.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
|
| 34705 |
amit.gupta |
835 |
List<SchemeInOut> sios = schemeInOutRepository.selectAll(inventoryItem.getId());
|
| 34740 |
amit.gupta |
836 |
|
| 34705 |
amit.gupta |
837 |
List<Integer> creditedSchemeIds = sios.stream()
|
|
|
838 |
.filter(x -> x.getStatus().equals(SchemePayoutStatus.CREDITED))
|
|
|
839 |
.map(x -> x.getSchemeId()).collect(Collectors.toList());
|
| 34698 |
amit.gupta |
840 |
|
| 34742 |
amit.gupta |
841 |
double sioRejectedValue = 0;
|
| 34740 |
amit.gupta |
842 |
if (creditedSchemeIds.size() > 0) {
|
| 34778 |
amit.gupta |
843 |
//Also ignore special support
|
| 34802 |
amit.gupta |
844 |
//This code block needs to be deleted
|
| 34803 |
amit.gupta |
845 |
List<Integer> schemeIdsToReject = null;
|
| 34802 |
amit.gupta |
846 |
if (Arrays.asList(1025588, 1025589).contains(inventoryItem.getItem().getCatalogItemId())) {
|
| 34803 |
amit.gupta |
847 |
schemeIdsToReject = schemeRepository.selectBySchemeIds(creditedSchemeIds).stream()
|
| 34802 |
amit.gupta |
848 |
.filter(x -> !SchemeType.SPECIAL_SUPPORT.equals(x.getType()))
|
|
|
849 |
.map(x -> x.getId()).collect(Collectors.toList());
|
| 34803 |
amit.gupta |
850 |
//List<Integer> schemeIdsToReject = schemeRepository.selectBySchemeIds(creditedSchemeIds).stream()
|
|
|
851 |
//This code block needs to be deleted
|
|
|
852 |
} else {
|
|
|
853 |
schemeIdsToReject = schemeRepository.selectBySchemeIds(creditedSchemeIds).stream()
|
|
|
854 |
.filter(x -> SchemeType.OUT_TYPES.contains(x.getType()) && !SchemeType.SPECIAL_SUPPORT.equals(x.getType()))
|
|
|
855 |
.map(x -> x.getId()).collect(Collectors.toList());
|
|
|
856 |
//Reject invalid scheme payouts due to upgrade in Category or any change in schemes historically
|
|
|
857 |
//Lets not touchs
|
| 34802 |
amit.gupta |
858 |
}
|
| 34740 |
amit.gupta |
859 |
for (SchemeInOut sio : sios) {
|
|
|
860 |
if (schemeIdsToReject.contains(sio.getSchemeId()) && !schemeSummaryModelMap.containsKey(sio.getSchemeId())) {
|
| 34742 |
amit.gupta |
861 |
Scheme scheme = schemeRepository.selectById(sio.getSchemeId());
|
| 34778 |
amit.gupta |
862 |
sio.setStatusDescription("Rolledback due to Category upgrade/invalid scheme");
|
|
|
863 |
sio.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
864 |
sio.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
865 |
sioRejectedValue += sio.getAmount();
|
| 34740 |
amit.gupta |
866 |
}
|
| 34705 |
amit.gupta |
867 |
}
|
| 34698 |
amit.gupta |
868 |
}
|
| 32165 |
amit.gupta |
869 |
float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
|
| 34742 |
amit.gupta |
870 |
if (inventoryItemCashback > 0 || sioRejectedValue > 0) {
|
| 31903 |
amit.gupta |
871 |
count++;
|
| 34742 |
amit.gupta |
872 |
totalCashback += inventoryItemCashback - sioRejectedValue;
|
| 31903 |
amit.gupta |
873 |
}
|
| 31410 |
amit.gupta |
874 |
}
|
|
|
875 |
}
|
| 31903 |
amit.gupta |
876 |
|
| 31410 |
amit.gupta |
877 |
if (count > 0) {
|
|
|
878 |
walletService.addAmountToWallet(
|
|
|
879 |
retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT, "Sales margin for invoice number "
|
|
|
880 |
+ fofoOrder.getInvoiceNumber() + ". Total " + count + " pc(s)",
|
|
|
881 |
totalCashback, fofoOrder.getCreateTimestamp());
|
|
|
882 |
fofoOrder.setCashback(totalCashback + fofoOrder.getCashback());
|
|
|
883 |
}
|
|
|
884 |
}
|
| 34746 |
amit.gupta |
885 |
return totalCashback;
|
| 31410 |
amit.gupta |
886 |
}
|
| 22653 |
ashik.ali |
887 |
|
| 31410 |
amit.gupta |
888 |
@Override
|
|
|
889 |
//Tax rate has been passed to 0 to ensure no tax deduction
|
|
|
890 |
public float getSpecialSupportAmount(float supportAmount, PartnerType partnerType, LocalDate onDate,
|
|
|
891 |
int catalogId) throws ProfitMandiBusinessException {
|
|
|
892 |
//int itemId = itemRepository.selectAllByCatalogItemId(catalogId).stream().findAny().get().getId();
|
|
|
893 |
//float totalTaxRate = stateGstRateRepository.getTotalTaxRate(itemId);
|
|
|
894 |
return this.getSpecialSupportAmount(supportAmount, partnerType, onDate, catalogId, 0);
|
|
|
895 |
}
|
| 23444 |
amit.gupta |
896 |
|
| 31410 |
amit.gupta |
897 |
@Override
|
|
|
898 |
public float getSpecialSupportAmount(float supportAmount, PartnerType partnerType, LocalDate onDate,
|
|
|
899 |
int catalogId, float taxRate) throws ProfitMandiBusinessException {
|
|
|
900 |
float totalMargin = this.selectPercentageScheme(partnerType, onDate, catalogId, false, 0, 0).stream().collect(Collectors.summingDouble(x -> x.getAmount())).floatValue();
|
|
|
901 |
float amountToCredit = supportAmount * (1 - (totalMargin / (100 + taxRate)));
|
|
|
902 |
return amountToCredit;
|
|
|
903 |
}
|
| 23444 |
amit.gupta |
904 |
|
| 31410 |
amit.gupta |
905 |
@Override
|
| 33614 |
amit.gupta |
906 |
public void rollbackSchemes(List<Integer> inventoryItemIds, String rollbackReason)
|
| 31410 |
amit.gupta |
907 |
throws Exception {
|
| 33614 |
amit.gupta |
908 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectAllByIds(inventoryItemIds);
|
|
|
909 |
Map<Integer, InventoryItem> inventoryItemMap = inventoryItems.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
910 |
Map<Integer, Integer> purchasePartnerMap = inventoryItems.stream().collect(Collectors.toMap(x -> x.getPurchaseId(), x -> x.getFofoId(), (u, v) -> u));
|
|
|
911 |
|
|
|
912 |
LOGGER.info("inventoryItemIds - {}", inventoryItemIds);
|
|
|
913 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(new HashSet<>(inventoryItemIds));
|
|
|
914 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).distinct().collect(Collectors.toList());
|
| 34139 |
amit.gupta |
915 |
if (schemeIds.size() == 0) return;
|
| 33614 |
amit.gupta |
916 |
List<Scheme> schemes = schemeRepository.selectBySchemeIds(schemeIds);
|
|
|
917 |
Map<Integer, Float> inSchemesMap = new HashMap<>();
|
|
|
918 |
Map<Integer, Float> outSchemesMap = new HashMap<>();
|
|
|
919 |
Map<Integer, Scheme> schemesMap = schemes.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
920 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
921 |
Map<Integer, Float> schemePayoutMap;
|
|
|
922 |
int inventoryItemId = schemeInOut.getInventoryItemId();
|
| 31410 |
amit.gupta |
923 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
924 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
925 |
if (schemeInOut.getStatus() == null || schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
| 33614 |
amit.gupta |
926 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
| 34317 |
amit.gupta |
927 |
if (scheme.getType().getTransactionType().equals(StockTransactionType.IN)) {
|
| 33614 |
amit.gupta |
928 |
schemePayoutMap = inSchemesMap;
|
|
|
929 |
} else {
|
|
|
930 |
schemePayoutMap = outSchemesMap;
|
|
|
931 |
}
|
|
|
932 |
if (!schemePayoutMap.containsKey(inventoryItemId)) {
|
|
|
933 |
schemePayoutMap.put(inventoryItemId, 0f);
|
|
|
934 |
}
|
| 34139 |
amit.gupta |
935 |
schemePayoutMap.put(inventoryItemId, inSchemesMap.get(inventoryItemId) == null ? 0 : inSchemesMap.get(inventoryItemId) + schemeInOut.getAmount());
|
| 31410 |
amit.gupta |
936 |
}
|
|
|
937 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
938 |
schemeInOut.setStatusDescription(rollbackReason);
|
|
|
939 |
}
|
|
|
940 |
}
|
| 33614 |
amit.gupta |
941 |
Map<Integer, Double> purchaseRollbackAmountMap = inSchemesMap.entrySet().stream().collect(Collectors.groupingBy(x -> inventoryItemMap.get(x.getKey()).getPurchaseId(), Collectors.summingDouble(x -> x.getValue())));
|
|
|
942 |
|
|
|
943 |
for (Map.Entry<Integer, Double> purchaseRollbackAmountEntry : purchaseRollbackAmountMap.entrySet()) {
|
|
|
944 |
int purchaseId = purchaseRollbackAmountEntry.getKey();
|
|
|
945 |
Double amountToRollback = purchaseRollbackAmountEntry.getValue();
|
|
|
946 |
if (amountToRollback != null && amountToRollback > 0) {
|
|
|
947 |
walletService.rollbackAmountFromWallet(purchasePartnerMap.get(purchaseId), amountToRollback.floatValue(), purchaseId,
|
|
|
948 |
WalletReferenceType.SCHEME_IN, rollbackReason, LocalDateTime.now());
|
|
|
949 |
}
|
| 31410 |
amit.gupta |
950 |
}
|
|
|
951 |
}
|
| 23444 |
amit.gupta |
952 |
|
| 31410 |
amit.gupta |
953 |
@Override
|
|
|
954 |
public Map<String, Object> getSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
955 |
throws ProfitMandiBusinessException {
|
|
|
956 |
Map<String, Object> map = new HashMap<>();
|
|
|
957 |
List<Scheme> schemes = null;
|
|
|
958 |
long size = 0;
|
|
|
959 |
if (roleManager.isAdmin(roleIds)) {
|
|
|
960 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
961 |
size = schemeRepository.selectAllCount();
|
|
|
962 |
} else {
|
|
|
963 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
964 |
size = schemeRepository.selectAllActiveCount();
|
|
|
965 |
}
|
|
|
966 |
map.put("schemes", schemes);
|
|
|
967 |
map.put("start", offset + 1);
|
|
|
968 |
map.put("size", size);
|
|
|
969 |
if (schemes.size() < limit) {
|
|
|
970 |
map.put("end", offset + schemes.size());
|
|
|
971 |
} else {
|
|
|
972 |
map.put("end", offset + limit);
|
|
|
973 |
}
|
|
|
974 |
return map;
|
|
|
975 |
}
|
| 26722 |
amit.gupta |
976 |
|
| 31410 |
amit.gupta |
977 |
@Override
|
|
|
978 |
public List<Scheme> getPaginatedSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
979 |
throws ProfitMandiBusinessException {
|
|
|
980 |
LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
|
|
|
981 |
List<Scheme> schemes = null;
|
|
|
982 |
if (roleManager.isAdmin(roleIds)) {
|
|
|
983 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
984 |
} else {
|
|
|
985 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
986 |
}
|
|
|
987 |
return schemes;
|
|
|
988 |
}
|
| 27898 |
amit.gupta |
989 |
|
| 31410 |
amit.gupta |
990 |
@Override
|
|
|
991 |
// This is being called to reverse schemes while processing price Drop
|
|
|
992 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int priceDropId, String reversalReason)
|
|
|
993 |
throws ProfitMandiBusinessException {
|
|
|
994 |
PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
|
|
|
995 |
Map<Integer, List<InventoryItem>> purchaseInventoryListMap = inventoryItems.stream()
|
|
|
996 |
.collect(Collectors.groupingBy(InventoryItem::getPurchaseId, Collectors.toList()));
|
| 29927 |
amit.gupta |
997 |
|
| 31410 |
amit.gupta |
998 |
for (Map.Entry<Integer, List<InventoryItem>> purchaseEntry : purchaseInventoryListMap.entrySet()) {
|
|
|
999 |
float amountToCredit = 0;
|
|
|
1000 |
float amountToDebit = 0;
|
|
|
1001 |
int purchaseId = purchaseEntry.getKey();
|
|
|
1002 |
List<InventoryItem> purchaseInventoryItemList = purchaseEntry.getValue();
|
| 30454 |
amit.gupta |
1003 |
|
| 31410 |
amit.gupta |
1004 |
Map<Integer, InventoryItem> inventoryItemsMap = purchaseInventoryItemList.stream()
|
|
|
1005 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 29927 |
amit.gupta |
1006 |
|
| 31410 |
amit.gupta |
1007 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
|
|
1008 |
LOGGER.info("Scheme InOuts , {}", schemeInOuts);
|
|
|
1009 |
if (schemeInOuts.size() == 0) {
|
|
|
1010 |
continue;
|
|
|
1011 |
}
|
|
|
1012 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
1013 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
1014 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1015 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
1016 |
InventoryItem ii = inventoryItemsMap.get(schemeInOut.getInventoryItemId());
|
|
|
1017 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
1018 |
if (scheme.getAmountType().equals(AmountType.FIXED)) {
|
|
|
1019 |
continue;
|
|
|
1020 |
}
|
| 34317 |
amit.gupta |
1021 |
if (scheme.getType().getTransactionType().equals(StockTransactionType.IN) && schemeInOut.getRolledBackTimestamp() == null) {
|
| 33432 |
amit.gupta |
1022 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
1023 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
1024 |
schemeInOut.setStatusDescription("Margin reversed due to price drop");
|
|
|
1025 |
// IF not credited then dont consider any credit/debit for that sio entry
|
|
|
1026 |
if (schemeInOut.getCreditTimestamp() != null) {
|
|
|
1027 |
amountToDebit += schemeInOut.getAmount();
|
| 31410 |
amit.gupta |
1028 |
}
|
|
|
1029 |
}
|
|
|
1030 |
}
|
|
|
1031 |
int fofoId = inventoryItems.get(0).getFofoId();
|
|
|
1032 |
if (amountToDebit > 0) {
|
|
|
1033 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN,
|
|
|
1034 |
MessageFormat.format(reversalReason, purchaseInventoryItemList.size()), -amountToDebit,
|
|
|
1035 |
priceDrop.getAffectedOn());
|
|
|
1036 |
}
|
|
|
1037 |
}
|
|
|
1038 |
}
|
| 29231 |
amit.gupta |
1039 |
|
| 31609 |
amit.gupta |
1040 |
@Autowired
|
| 32060 |
amit.gupta |
1041 |
UserWalletRepository userWalletRepository;
|
|
|
1042 |
@Autowired
|
| 31609 |
amit.gupta |
1043 |
UserWalletHistoryRepository userWalletHistoryRepository;
|
|
|
1044 |
|
| 31410 |
amit.gupta |
1045 |
@Override
|
|
|
1046 |
// Always being called from cancel order/bad return means no SCHEME IN is considered
|
|
|
1047 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int reversalReference, String reversalReason,
|
|
|
1048 |
List<SchemeType> schemeTypes) throws ProfitMandiBusinessException {
|
|
|
1049 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
|
|
|
1050 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1051 |
LOGGER.info("inventoryItems" + inventoryItems);
|
| 23444 |
amit.gupta |
1052 |
|
| 31410 |
amit.gupta |
1053 |
Map<SchemeType, SchemeInOut> schemeTypeMap = new HashMap<>();
|
| 30454 |
amit.gupta |
1054 |
|
| 31410 |
amit.gupta |
1055 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
| 32060 |
amit.gupta |
1056 |
List<SchemeInOut> rolledBacks = schemeInOuts.stream().filter(x -> x.getStatusDescription().equals(reversalReason)).collect(Collectors.toList());
|
| 31410 |
amit.gupta |
1057 |
float amountToRollback = 0;
|
| 23444 |
amit.gupta |
1058 |
|
| 31410 |
amit.gupta |
1059 |
if (!schemeInOuts.isEmpty()) {
|
|
|
1060 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
1061 |
LOGGER.info("schemeIds" + schemeIds);
|
| 29231 |
amit.gupta |
1062 |
|
| 31410 |
amit.gupta |
1063 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
1064 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 32060 |
amit.gupta |
1065 |
if (rolledBacks.size() > 0) {
|
|
|
1066 |
for (SchemeInOut schemeInOut : rolledBacks) {
|
|
|
1067 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
1068 |
if (schemeTypes.contains(scheme.getType())) {
|
|
|
1069 |
schemeTypeMap.put(scheme.getType(), schemeInOut);
|
|
|
1070 |
if (schemeInOut.getCreditTimestamp() != null) {
|
| 31410 |
amit.gupta |
1071 |
amountToRollback += schemeInOut.getAmount();
|
|
|
1072 |
}
|
|
|
1073 |
}
|
|
|
1074 |
}
|
| 32060 |
amit.gupta |
1075 |
} else {
|
|
|
1076 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
1077 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
1078 |
if (schemeTypes.contains(scheme.getType())) {
|
|
|
1079 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
1080 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
1081 |
if (schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
|
|
1082 |
amountToRollback += schemeInOut.getAmount();
|
|
|
1083 |
}
|
|
|
1084 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
1085 |
schemeInOut.setStatusDescription(reversalReason);
|
|
|
1086 |
}
|
|
|
1087 |
}
|
|
|
1088 |
}
|
| 31410 |
amit.gupta |
1089 |
}
|
|
|
1090 |
}
|
| 32060 |
amit.gupta |
1091 |
|
| 31410 |
amit.gupta |
1092 |
int fofoId = inventoryItems.get(0).getFofoId();
|
| 34486 |
amit.gupta |
1093 |
WalletReferenceType walletReferenceType = schemeTypes.containsAll(SchemeType.OUT_SCHEME_TYPES) ? WalletReferenceType.SCHEME_OUT
|
| 31609 |
amit.gupta |
1094 |
: schemeTypes.contains(SchemeType.SPECIAL_SUPPORT) ? WalletReferenceType.SPECIAL_SUPPORT
|
| 34486 |
amit.gupta |
1095 |
: schemeTypes.contains(SchemeType.INVESTMENT) ? WalletReferenceType.SCHEME_OUT : null;
|
| 32060 |
amit.gupta |
1096 |
List<UserWalletHistory> userWalletHistoryList = null;
|
| 32058 |
amit.gupta |
1097 |
if (amountToRollback > 0 && walletReferenceType != null) {
|
|
|
1098 |
// Mark appropriate reference of rollback investment margin
|
| 32060 |
amit.gupta |
1099 |
if (schemeTypes.contains(SchemeType.INVESTMENT) && schemeTypeMap.containsKey(SchemeType.INVESTMENT)) {
|
|
|
1100 |
LocalDateTime creditTime = schemeTypeMap.get(SchemeType.INVESTMENT).getCreditTimestamp();
|
|
|
1101 |
if (creditTime == null) return;
|
|
|
1102 |
int investmentReversalReference = Integer.parseInt(FormattingUtils.getYearMonth(creditTime.minusMonths(1)));
|
|
|
1103 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
1104 |
userWalletHistoryList = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(userWallet.getUserId(), investmentReversalReference, WalletReferenceType.INVESTMENT_PAYOUT);
|
|
|
1105 |
if (userWalletHistoryList.size() > 0) {
|
|
|
1106 |
walletReferenceType = WalletReferenceType.INVESTMENT_PAYOUT;
|
|
|
1107 |
} else {
|
|
|
1108 |
userWalletHistoryList = null;
|
| 32058 |
amit.gupta |
1109 |
}
|
| 32060 |
amit.gupta |
1110 |
}
|
|
|
1111 |
if (userWalletHistoryList == null) {
|
|
|
1112 |
userWalletHistoryList = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(reversalReference, walletReferenceType);
|
|
|
1113 |
}
|
|
|
1114 |
if (userWalletHistoryList.size() > 0) {
|
|
|
1115 |
int maxDeductible = userWalletHistoryList.stream().collect(Collectors.summingInt(x -> x.getAmount()));
|
|
|
1116 |
if (maxDeductible > 0) {
|
| 32165 |
amit.gupta |
1117 |
LOGGER.info("----------> maxDeductible {}, amountToRollback {}, reversalReference {}, walletReferenceType {} ", maxDeductible, amountToRollback, reversalReference, walletReferenceType);
|
| 32060 |
amit.gupta |
1118 |
walletService.rollbackAmountFromWallet(fofoId, Math.min(maxDeductible, amountToRollback), reversalReference, walletReferenceType,
|
|
|
1119 |
reversalReason, LocalDateTime.now());
|
|
|
1120 |
}
|
| 31609 |
amit.gupta |
1121 |
|
| 31903 |
amit.gupta |
1122 |
}
|
| 32058 |
amit.gupta |
1123 |
}
|
| 31410 |
amit.gupta |
1124 |
}
|
| 29231 |
amit.gupta |
1125 |
|
| 32060 |
amit.gupta |
1126 |
|
| 31410 |
amit.gupta |
1127 |
@Override
|
|
|
1128 |
public double getTotalMargin(int itemId, PartnerType partnerType, LocalDateTime dateTime) {
|
|
|
1129 |
Session session = sessionFactory.getCurrentSession();
|
|
|
1130 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
1131 |
CriteriaQuery<Double> criteriaQuery = cb.createQuery(Double.class);
|
|
|
1132 |
Root<SchemeItem> schemeItem = criteriaQuery.from(SchemeItem.class);
|
|
|
1133 |
Root<Scheme> scheme = criteriaQuery.from(Scheme.class);
|
|
|
1134 |
Predicate schemePredicate = cb.equal(scheme.get(ProfitMandiConstants.AMOUNT_TYPE), AmountType.PERCENTAGE);
|
|
|
1135 |
Predicate lessThanPredicate = cb.lessThanOrEqualTo(scheme.get(ProfitMandiConstants.END_DATE_TIME), dateTime);
|
|
|
1136 |
Predicate greaterThanPredicate = cb.greaterThanOrEqualTo(scheme.get(ProfitMandiConstants.START_DATE_TIME),
|
|
|
1137 |
dateTime);
|
|
|
1138 |
Predicate joinPredicate = cb.equal(scheme.get("id"), schemeItem.get("schemeId"));
|
|
|
1139 |
Predicate schemeItemPredicate = cb.equal(schemeItem.get(ProfitMandiConstants.ITEM_ID), itemId);
|
|
|
1140 |
criteriaQuery.select(cb.sum(scheme.get(ProfitMandiConstants.AMOUNT))).where(schemePredicate, lessThanPredicate,
|
|
|
1141 |
greaterThanPredicate, schemeItemPredicate, joinPredicate);
|
| 29231 |
amit.gupta |
1142 |
|
| 31410 |
amit.gupta |
1143 |
Query<Double> query = session.createQuery(criteriaQuery);
|
|
|
1144 |
return query.getSingleResult() + ProfitMandiConstants.SCHEME_INVESTMENT_MARGIN;
|
| 29231 |
amit.gupta |
1145 |
|
| 31410 |
amit.gupta |
1146 |
}
|
| 23444 |
amit.gupta |
1147 |
|
| 32972 |
amit.gupta |
1148 |
|
| 31410 |
amit.gupta |
1149 |
@Override
|
| 33614 |
amit.gupta |
1150 |
public Map<Integer, Float> getCatalogSchemeCashBack(int fofoId, List<Integer> catalogIds) throws
|
|
|
1151 |
ProfitMandiBusinessException {
|
| 32972 |
amit.gupta |
1152 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
|
|
|
1153 |
Map<CatalogSummaryModel, List<SchemeSummaryModel>> catalogModelMap = tagListingRepository.getModelSchemesByCatalogIdsAndType(fofoId, partnerType, catalogIds, LocalDate.now().atStartOfDay());
|
|
|
1154 |
|
|
|
1155 |
Map<Integer, Float> catalogCashbackMap = new HashMap<>();
|
|
|
1156 |
for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogModelMap.entrySet()) {
|
|
|
1157 |
int catalogItemId = catalogSummaryModelListEntry.getKey().getCatalogId();
|
|
|
1158 |
List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue();
|
|
|
1159 |
|
|
|
1160 |
float totalCashback = schemeSummaryModels.stream()
|
| 34317 |
amit.gupta |
1161 |
.filter(x -> Arrays.asList(SchemeType.SPECIAL_SUPPORT, SchemeType.SELLOUT).contains(x.getSchemeType())
|
| 32972 |
amit.gupta |
1162 |
&& x.getAmountType().equals(AmountType.FIXED))
|
|
|
1163 |
.collect(Collectors.summingDouble(x -> x.getAmount())).floatValue();
|
|
|
1164 |
catalogCashbackMap.put(catalogItemId, totalCashback);
|
| 31410 |
amit.gupta |
1165 |
}
|
| 32972 |
amit.gupta |
1166 |
return catalogCashbackMap;
|
| 31410 |
amit.gupta |
1167 |
}
|
| 30572 |
amit.gupta |
1168 |
|
| 31410 |
amit.gupta |
1169 |
@Override
|
| 31903 |
amit.gupta |
1170 |
public List<Scheme> selectSchemeByPartnerTypeFofoId(PartnerType partnerType, LocalDate onDate, int catalogId,
|
|
|
1171 |
int fofoId, int offset, int limit) throws ProfitMandiBusinessException {
|
| 31410 |
amit.gupta |
1172 |
Session session = sessionFactory.getCurrentSession();
|
|
|
1173 |
final TypedQuery<Scheme> typedQuery = session.createNamedQuery(
|
|
|
1174 |
"Scheme.selectSchemeByModelsPartnerTypeFofoId", Scheme.class);
|
|
|
1175 |
typedQuery.setParameter("catalogIds", Arrays.asList(catalogId));
|
|
|
1176 |
typedQuery.setParameter("fofoIds", Arrays.asList(fofoId, 0));
|
|
|
1177 |
typedQuery.setParameter("onDate", onDate.atStartOfDay());
|
|
|
1178 |
typedQuery.setParameter("partnerTypes", Arrays.asList(partnerType, partnerType.ALL));
|
|
|
1179 |
typedQuery.setFirstResult(offset);
|
|
|
1180 |
if (limit != 0) {
|
|
|
1181 |
typedQuery.setMaxResults(limit);
|
|
|
1182 |
}
|
|
|
1183 |
return typedQuery.getResultList();
|
|
|
1184 |
}
|
| 30463 |
amit.gupta |
1185 |
|
| 33432 |
amit.gupta |
1186 |
@Override
|
|
|
1187 |
public void processSchemeIn(List<InventoryItem> inventoryItems) throws ProfitMandiBusinessException {
|
|
|
1188 |
Map<Integer, List<InventoryItem>> purchaseIdInventoryItemsMap = inventoryItems.stream().collect(Collectors.groupingBy(x -> x.getPurchaseId()));
|
|
|
1189 |
for (Map.Entry<Integer, List<InventoryItem>> purchaseIdInventoryItemEntry : purchaseIdInventoryItemsMap.entrySet()) {
|
| 34317 |
amit.gupta |
1190 |
int retailerId = purchaseIdInventoryItemEntry.getValue().get(0).getFofoId();
|
|
|
1191 |
this.processSchemeIn(purchaseIdInventoryItemEntry.getKey(), retailerId);
|
| 33432 |
amit.gupta |
1192 |
}
|
|
|
1193 |
}
|
|
|
1194 |
|
| 32309 |
amit.gupta |
1195 |
@Autowired
|
|
|
1196 |
FofoStoreRepository fofoStoreRepository;
|
|
|
1197 |
|
|
|
1198 |
|
| 31410 |
amit.gupta |
1199 |
@Override
|
| 32309 |
amit.gupta |
1200 |
@Cacheable(value = "staticscheme", cacheManager = "oneDayCacheManager")
|
|
|
1201 |
public Scheme getStaticScheme(int fofoId) throws ProfitMandiBusinessException {
|
| 33165 |
amit.gupta |
1202 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
| 32504 |
amit.gupta |
1203 |
Scheme scheme = null;
|
|
|
1204 |
if (fofoStore.getTarget() > 0) {
|
|
|
1205 |
scheme = new Scheme();
|
| 33704 |
amit.gupta |
1206 |
scheme.setName("Super Retailer - Club 3");
|
|
|
1207 |
scheme.setStartDateTime(LocalDate.of(2024, 8, 1).atStartOfDay());
|
|
|
1208 |
scheme.setEndDateTime(LocalDate.of(2024, 11, 30).atTime(LocalTime.MAX));
|
| 32504 |
amit.gupta |
1209 |
scheme.setTarget(fofoStore.getTarget());
|
| 33548 |
amit.gupta |
1210 |
|
| 33704 |
amit.gupta |
1211 |
if (scheme.getEndDateTime().plusDays(5).isBefore(LocalDateTime.now())) return null;
|
| 32504 |
amit.gupta |
1212 |
}
|
| 33165 |
amit.gupta |
1213 |
return scheme;
|
| 32309 |
amit.gupta |
1214 |
}
|
|
|
1215 |
|
|
|
1216 |
@Override
|
| 31410 |
amit.gupta |
1217 |
public List<Scheme> selectSchemeByPartnerType(PartnerType partnerType, LocalDate onDate, int catalogId,
|
|
|
1218 |
boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
|
|
|
1219 |
Session session = sessionFactory.getCurrentSession();
|
|
|
1220 |
List<Predicate> andPredicates = new ArrayList<>();
|
|
|
1221 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
1222 |
CriteriaQuery<Scheme> query = cb.createQuery(Scheme.class);
|
|
|
1223 |
Root<Scheme> scheme = query.from(Scheme.class);
|
|
|
1224 |
if (!partnerType.equals(PartnerType.ALL)) {
|
|
|
1225 |
List<PartnerType> pt = new ArrayList<>();
|
|
|
1226 |
pt.add(PartnerType.ALL);
|
|
|
1227 |
pt.add(partnerType);
|
|
|
1228 |
andPredicates.add(cb.in(scheme.get("partnerType")).value(pt));
|
|
|
1229 |
}
|
|
|
1230 |
cb.desc(cb.isNull(scheme.get("expireTimestamp")));
|
|
|
1231 |
if (catalogId > 0) {
|
| 30454 |
amit.gupta |
1232 |
|
| 31410 |
amit.gupta |
1233 |
List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByCatalogId(catalogId);
|
|
|
1234 |
LOGGER.info("schemeId" + schemeIds);
|
|
|
1235 |
if (schemeIds.isEmpty()) {
|
|
|
1236 |
return new ArrayList<>();
|
|
|
1237 |
}
|
|
|
1238 |
andPredicates.add(cb.in(scheme.get("id")).value(schemeIds));
|
|
|
1239 |
if (onDate != null) {
|
|
|
1240 |
andPredicates.add(cb.greaterThan(scheme.get("endDateTime"), onDate.atStartOfDay()));
|
|
|
1241 |
andPredicates.add(cb.lessThanOrEqualTo(scheme.get("startDateTime"), onDate.atStartOfDay()));
|
|
|
1242 |
}
|
|
|
1243 |
}
|
|
|
1244 |
if (!isAdmin) {
|
|
|
1245 |
andPredicates.add(cb.isNotNull(scheme.get("activeTimestamp")));
|
|
|
1246 |
}
|
|
|
1247 |
query.where(cb.and(andPredicates.toArray(new Predicate[0])));
|
|
|
1248 |
query.orderBy(cb.desc(cb.function("isnull", Boolean.class, scheme.get("expireTimestamp"))));
|
|
|
1249 |
if (limit == 0) {
|
|
|
1250 |
return session.createQuery(query).setFirstResult(offset).getResultList();
|
|
|
1251 |
}
|
|
|
1252 |
return session.createQuery(query).setFirstResult(offset).setMaxResults(limit).getResultList();
|
| 30454 |
amit.gupta |
1253 |
|
| 31410 |
amit.gupta |
1254 |
}
|
| 30454 |
amit.gupta |
1255 |
|
| 31410 |
amit.gupta |
1256 |
@Override
|
|
|
1257 |
public List<Scheme> selectPercentageScheme(PartnerType partnerType, LocalDate onDate, int catalogId,
|
|
|
1258 |
boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
|
|
|
1259 |
List<Scheme> schemes = this.selectSchemeByPartnerType(partnerType, onDate, catalogId, isAdmin, offset, limit);
|
|
|
1260 |
return schemes.stream().filter(x -> x.getAmountType().equals(AmountType.PERCENTAGE)).collect(Collectors.toList());
|
|
|
1261 |
}
|
| 30454 |
amit.gupta |
1262 |
|
| 31410 |
amit.gupta |
1263 |
@Override
|
|
|
1264 |
public void processActivation() throws ProfitMandiBusinessException {
|
|
|
1265 |
List<SchemeInOut> pendingPayouts = schemeInOutRepository.selectAllPending();
|
|
|
1266 |
List<Integer> schemeIds = new ArrayList<>();
|
|
|
1267 |
Set<Integer> inventoryIds = new HashSet<>();
|
|
|
1268 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
|
|
1269 |
schemeIds.add(pendingPayout.getSchemeId());
|
|
|
1270 |
}
|
|
|
1271 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, 0).stream()
|
| 34317 |
amit.gupta |
1272 |
.filter(x -> x.getType().equals(SchemeType.SPECIAL_SUPPORT))
|
| 31410 |
amit.gupta |
1273 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1274 |
pendingPayouts = pendingPayouts.stream().filter(x -> schemesMap.get(x.getSchemeId()) != null)
|
|
|
1275 |
.collect(Collectors.toList());
|
| 23884 |
amit.gupta |
1276 |
|
| 31410 |
amit.gupta |
1277 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
|
|
1278 |
inventoryIds.add(pendingPayout.getInventoryItemId());
|
|
|
1279 |
}
|
|
|
1280 |
Map<Integer, InventoryItem> inventoryItemMap = inventoryItemRepository.selectByIds(inventoryIds).stream()
|
|
|
1281 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1282 |
Map<String, InventoryItem> serialNumberMap = inventoryItemMap.values().stream()
|
|
|
1283 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x));
|
| 23796 |
amit.gupta |
1284 |
|
| 31410 |
amit.gupta |
1285 |
List<ActivatedImei> activatedImeis = activatedImeiRepository
|
|
|
1286 |
.selectBySerialNumbers(new ArrayList<>(serialNumberMap.keySet())).stream().collect(Collectors.toList());
|
| 23508 |
amit.gupta |
1287 |
|
| 31410 |
amit.gupta |
1288 |
Map<String, ActivatedImei> activatedImeiMap = activatedImeis.stream()
|
|
|
1289 |
.collect(Collectors.toMap(x -> x.getSerialNumber().toLowerCase(), x -> x));
|
|
|
1290 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
| 31914 |
amit.gupta |
1291 |
Scheme scheme = schemesMap.get(pendingPayout.getSchemeId());
|
| 31410 |
amit.gupta |
1292 |
InventoryItem ii = inventoryItemMap.get(pendingPayout.getInventoryItemId());
|
|
|
1293 |
String serialNumber = ii.getSerialNumber().toLowerCase();
|
|
|
1294 |
ActivatedImei activatedImei = activatedImeiMap.get(serialNumber);
|
|
|
1295 |
if (activatedImei == null) {
|
|
|
1296 |
continue;
|
|
|
1297 |
}
|
|
|
1298 |
if (scheme.isWithinRange(activatedImei.getActivationTimestamp())) {
|
|
|
1299 |
int fofoId = ii.getFofoId();
|
|
|
1300 |
// Get latest order Id
|
|
|
1301 |
int orderId = scanRecordRepository.selectByInventoryItemId(ii.getId()).stream()
|
|
|
1302 |
.filter(x -> x.getOrderId() > 0)
|
|
|
1303 |
.sorted(Comparator.comparing(ScanRecord::getCreateTimestamp).reversed()).findFirst().get()
|
|
|
1304 |
.getOrderId();
|
|
|
1305 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(orderId);
|
| 31916 |
amit.gupta |
1306 |
|
| 31921 |
amit.gupta |
1307 |
InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(ii);
|
| 31916 |
amit.gupta |
1308 |
AmountModel amountModel = new AmountModel();
|
|
|
1309 |
amountModel.setAmount(scheme.getAmount());
|
|
|
1310 |
amountModel.setAmountType(scheme.getAmountType());
|
| 31921 |
amit.gupta |
1311 |
double amountToRollout = inventoryPayoutModel.getRolloutAmount(amountModel);
|
| 32058 |
amit.gupta |
1312 |
pendingPayout.setAmount((float) amountToRollout);
|
| 34317 |
amit.gupta |
1313 |
walletService.addAmountToWallet(fofoId, orderId, WalletReferenceType.SPECIAL_SUPPORT,
|
|
|
1314 |
"Special Support for " + ii.getItem().getItemDescriptionNoColor() + ", Imei - " + serialNumber, (float) amountToRollout,
|
|
|
1315 |
fofoOrder.getCreateTimestamp());
|
|
|
1316 |
pendingPayout.setStatusDescription("Special support credited, activated on " + FormattingUtils.formatDate(activatedImei.getActivationTimestamp()));
|
|
|
1317 |
|
| 31410 |
amit.gupta |
1318 |
pendingPayout.setCreditTimestamp(LocalDateTime.now());
|
|
|
1319 |
pendingPayout.setStatus(SchemePayoutStatus.CREDITED);
|
|
|
1320 |
} else {
|
|
|
1321 |
pendingPayout.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
1322 |
pendingPayout.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
1323 |
pendingPayout.setStatusDescription(
|
|
|
1324 |
"Rejected, activated on " + FormattingUtils.formatDate(activatedImei.getActivationTimestamp()));
|
|
|
1325 |
}
|
|
|
1326 |
}
|
|
|
1327 |
}
|
| 24976 |
amit.gupta |
1328 |
|
| 31410 |
amit.gupta |
1329 |
@Override
|
|
|
1330 |
public void processActivatedImeisForSchemes() throws ProfitMandiBusinessException {
|
|
|
1331 |
List<SchemesImeisModel> schemesImeisModels = schemeRepository.selectSelectUnpaidSchemes();
|
|
|
1332 |
LOGGER.info("Total Size - " + schemesImeisModels.size());
|
|
|
1333 |
List<Integer> orderIds = schemesImeisModels.stream().map(x -> x.getOrderId()).collect(Collectors.toList());
|
|
|
1334 |
List<FofoOrder> fofoOrders = fofoOrderRepository.selectAllByOrderIds(orderIds);
|
|
|
1335 |
Map<Integer, FofoOrder> validOrdersMap = fofoOrders.stream().filter(x -> x.getCancelledTimestamp() == null).collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1336 |
Map<String, List<SchemesImeisModel>> validImeiSchemesModelMap = schemesImeisModels.stream().filter(x -> validOrdersMap.containsKey(x.getOrderId())).collect(Collectors.groupingBy(x -> x.getImei()));
|
|
|
1337 |
for (Map.Entry<String, List<SchemesImeisModel>> imeiListEntry : validImeiSchemesModelMap.entrySet()) {
|
|
|
1338 |
SchemesImeisModel schemesImeisModel = imeiListEntry.getValue().get(0);
|
|
|
1339 |
List<Integer> schemeIds = imeiListEntry.getValue().stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
1340 |
LOGGER.info("Serial Number - {}, Scheme IDs - {}", schemesImeisModel.getImei(), schemeIds);
|
|
|
1341 |
InventoryItem inventoryItem = inventoryItemRepository.selectById(schemesImeisModel.getInventoryItemId());
|
|
|
1342 |
List<Scheme> schemes = schemeRepository.selectBySchemeIds(schemeIds);
|
| 34317 |
amit.gupta |
1343 |
List<Scheme> supportSchemes = schemes.stream().filter(x -> Arrays.asList(SchemeType.SPECIAL_SUPPORT).contains(x.getType())).collect(Collectors.toList());
|
| 31410 |
amit.gupta |
1344 |
if (supportSchemes.size() > 0) {
|
| 32165 |
amit.gupta |
1345 |
for (Scheme scheme : supportSchemes) {
|
|
|
1346 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByScheme(scheme.getId(), inventoryItem.getId());
|
| 32972 |
amit.gupta |
1347 |
if (schemeInOuts.stream().filter(x -> Arrays.asList(SchemePayoutStatus.CREDITED, SchemePayoutStatus.PENDING).contains(x.getStatus())).count() > 0) {
|
| 32165 |
amit.gupta |
1348 |
continue;
|
|
|
1349 |
}
|
|
|
1350 |
SchemeInOut sio = new SchemeInOut();
|
|
|
1351 |
sio.setAmount(0);
|
|
|
1352 |
sio.setInventoryItemId(inventoryItem.getId());
|
|
|
1353 |
sio.setSchemeId(scheme.getId());
|
|
|
1354 |
sio.setStatusDescription("Activation pending for IMEI#" + inventoryItem.getSerialNumber());
|
|
|
1355 |
sio.setStatus(SchemePayoutStatus.PENDING);
|
|
|
1356 |
schemeInOutRepository.persist(sio);
|
|
|
1357 |
}
|
| 31410 |
amit.gupta |
1358 |
}
|
|
|
1359 |
}
|
|
|
1360 |
}
|
| 22653 |
ashik.ali |
1361 |
}
|