| 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");
|
| 32602 |
amit.gupta |
338 |
sendNotificationModel.setUrl("https://app.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 |
StringBuilder titleBuilder = new StringBuilder();
|
|
|
443 |
Set<Integer> allCatalogIds = new HashSet<>();
|
|
|
444 |
|
|
|
445 |
for (Scheme scheme : schemes) {
|
|
|
446 |
if (!ACTIVATION_SCHEME_TYPES.contains(scheme.getType())) {
|
|
|
447 |
continue;
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
String schemeTypeLabel = "Activation scheme";
|
|
|
451 |
if (scheme.getType().equals(SchemeType.SPECIAL_SUPPORT)) {
|
|
|
452 |
schemeTypeLabel = "Special Support";
|
|
|
453 |
}
|
|
|
454 |
|
|
|
455 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
|
|
|
456 |
Set<Integer> catalogIds = schemeItems.stream().map(SchemeItem::getCatalogId).collect(Collectors.toSet());
|
|
|
457 |
allCatalogIds.addAll(catalogIds);
|
|
|
458 |
|
|
|
459 |
List<String> itemBrand = itemRepository.selectAllByCatalogIds(catalogIds).stream()
|
|
|
460 |
.filter(Utils.distinctByKey(Item::getCatalogItemId))
|
|
|
461 |
.map(Item::getBrand)
|
|
|
462 |
.collect(Collectors.toList());
|
|
|
463 |
|
|
|
464 |
List<String> itemDescriptions = itemRepository.selectAllByCatalogIds(catalogIds).stream()
|
|
|
465 |
.filter(Utils.distinctByKey(Item::getCatalogItemId))
|
|
|
466 |
.map(Item::getItemDescriptionNoColor)
|
|
|
467 |
.collect(Collectors.toList());
|
|
|
468 |
|
|
|
469 |
// Title line
|
|
|
470 |
titleBuilder.append(String.format("%s of Rs.%s for %s\n",
|
|
|
471 |
schemeTypeLabel,
|
|
|
472 |
FormattingUtils.formatDecimal(scheme.getAmount()),
|
|
|
473 |
itemBrand.get(0)));
|
|
|
474 |
|
|
|
475 |
// Message details
|
|
|
476 |
messageBuilder.append(String.format("On Selected Models: %s\n", String.join(", ", itemDescriptions)));
|
|
|
477 |
messageBuilder.append(String.format("Duration from - %s to %s\n\n",
|
|
|
478 |
FormattingUtils.formatDateMonth(scheme.getStartDateTime()),
|
|
|
479 |
FormattingUtils.formatDateMonth(scheme.getEndDateTime())));
|
|
|
480 |
}
|
|
|
481 |
|
|
|
482 |
if (titleBuilder.length() == 0) return;
|
|
|
483 |
|
|
|
484 |
sendNotificationModel.setCampaignName("activationscheme");
|
|
|
485 |
sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
|
|
|
486 |
sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
|
|
|
487 |
sendNotificationModel.setTitle(titleBuilder.toString().trim());
|
|
|
488 |
sendNotificationModel.setMessage(messageBuilder.toString().trim());
|
|
|
489 |
sendNotificationModel.setType("url");
|
|
|
490 |
sendNotificationModel.setMessageType(MessageType.scheme);
|
|
|
491 |
notificationService.sendNotificationToAll(sendNotificationModel);
|
|
|
492 |
}
|
|
|
493 |
|
|
|
494 |
//CDs would be rejected based on cdFreeDays
|
| 31903 |
amit.gupta |
495 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
| 34504 |
amit.gupta |
496 |
LOGGER.info("Processing scheme in for purchaseId - {}", purchaseId);
|
| 31903 |
amit.gupta |
497 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
| 34709 |
amit.gupta |
498 |
String purchaseInvoice = purchase.getPurchaseReference();
|
|
|
499 |
|
|
|
500 |
Loan loan = loanRepository.selectLoanByInvoice(purchaseInvoice);
|
|
|
501 |
|
| 34317 |
amit.gupta |
502 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
|
|
|
503 |
YearMonth.from(purchase.getCreateTimestamp()));
|
| 34139 |
amit.gupta |
504 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
|
| 31903 |
amit.gupta |
505 |
//Remove imeis from blocked imeis list
|
| 34317 |
amit.gupta |
506 |
List<String> blockedImeis = this.getBlockedImeis();
|
|
|
507 |
inventoryItems = inventoryItems.stream().filter(inventoryItem -> !blockedImeis.contains(inventoryItem.getSerialNumber())).collect(Collectors.toList());
|
| 31903 |
amit.gupta |
508 |
if (inventoryItems.size() == 0) return;
|
| 23444 |
amit.gupta |
509 |
|
| 31903 |
amit.gupta |
510 |
Set<Integer> itemIds = inventoryItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
511 |
Map<Integer, Item> itemsMap = itemRepository.selectByIds(itemIds).stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
512 |
inventoryItems.stream().forEach(x -> x.setItem(itemsMap.get(x.getItemId())));
|
| 23444 |
amit.gupta |
513 |
|
|
|
514 |
|
| 34317 |
amit.gupta |
515 |
LocalDateTime billingDate = purchaseService.getBillingDateOfPurchase(purchaseId);
|
| 31903 |
amit.gupta |
516 |
Set<Integer> itemIdsSet = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds).stream()
|
|
|
517 |
.filter(x -> x.getEolDate() == null || x.getEolDate().isAfter(billingDate)).map(x -> x.getItemId())
|
|
|
518 |
.collect(Collectors.toSet());
|
|
|
519 |
// Only consider inventory items that were not returned and not eol
|
|
|
520 |
inventoryItems = inventoryItems.stream().filter(x -> itemIdsSet.contains(x.getItemId()))
|
|
|
521 |
.filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET_BAD))
|
|
|
522 |
.filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET)).collect(Collectors.toList());
|
|
|
523 |
|
|
|
524 |
if (inventoryItems.size() == 0) return;
|
|
|
525 |
|
|
|
526 |
Map<Integer, List<InventoryItem>> catalogInventoryItemMap = inventoryItems.stream().collect(Collectors.groupingBy(x -> x.getItem().getCatalogItemId()));
|
| 34317 |
amit.gupta |
527 |
Map<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSchemeSummaryMap = tagListingRepository.getModelSchemesByCatalogIdsAndType(retailerId,
|
| 32454 |
amit.gupta |
528 |
partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), billingDate);
|
| 31903 |
amit.gupta |
529 |
|
|
|
530 |
|
|
|
531 |
int itemsCount = 0;
|
|
|
532 |
float totalCashback = 0;
|
| 34709 |
amit.gupta |
533 |
//LOGGER.info("catalogSchemeSummaryMap - {}", catalogSchemeSummaryMap);
|
| 31903 |
amit.gupta |
534 |
for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
|
|
|
535 |
CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
|
| 34504 |
amit.gupta |
536 |
List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue().stream().filter(Objects::nonNull).collect(Collectors.toList());
|
| 34317 |
amit.gupta |
537 |
schemeSummaryModels.stream().filter(x -> x != null && x.getSchemeType().getTransactionType().equals(StockTransactionType.IN)).forEach(x -> x.setProcess(true));
|
| 31903 |
amit.gupta |
538 |
if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
|
|
|
539 |
List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
|
|
|
540 |
for (InventoryItem inventoryItem : modelInventoryItems) {
|
| 32165 |
amit.gupta |
541 |
float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
|
| 34709 |
amit.gupta |
542 |
if (loan != null && loan.getCdFreeDays() > 0) {
|
|
|
543 |
List<SchemeType> rejectedCDTypes = HALF_DAYS_CD_REJECT_SCHEME_TYPES;
|
|
|
544 |
if (loan.getCdFreeDays() == ProfitMandiConstants.LOAN_FULL_CREDIT_DAYS) {
|
|
|
545 |
rejectedCDTypes = FULL_DAYS_CD_SCHEME_TYPES;
|
|
|
546 |
}
|
|
|
547 |
for (SchemeType rejectedCDType : rejectedCDTypes) {
|
|
|
548 |
SchemeInOut sio = schemeInOutRepository.selectAllByType(rejectedCDType, inventoryItem.getId()).get(0);
|
|
|
549 |
sio.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
550 |
sio.setStatusDescription("Rejected due to free days availed");
|
|
|
551 |
sio.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
552 |
inventoryItemCashback -= sio.getAmount();
|
|
|
553 |
}
|
|
|
554 |
|
|
|
555 |
}
|
|
|
556 |
if (inventoryItemCashback > 0.01f) {
|
| 31903 |
amit.gupta |
557 |
itemsCount++;
|
|
|
558 |
totalCashback += inventoryItemCashback;
|
| 31410 |
amit.gupta |
559 |
}
|
|
|
560 |
}
|
| 31903 |
amit.gupta |
561 |
}
|
| 22653 |
ashik.ali |
562 |
|
| 29927 |
amit.gupta |
563 |
|
| 34317 |
amit.gupta |
564 |
LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
|
| 31903 |
amit.gupta |
565 |
if (itemsCount > 0) {
|
|
|
566 |
walletService.addAmountToWallet(
|
| 34317 |
amit.gupta |
567 |
retailerId, purchaseId, WalletReferenceType.SCHEME_IN, "Added for SCHEME IN against invoice "
|
| 31903 |
amit.gupta |
568 |
+ purchase.getPurchaseReference() + " (total " + itemsCount + " pcs)",
|
|
|
569 |
totalCashback, purchase.getCreateTimestamp());
|
|
|
570 |
LOGGER.info("Added Rs.{} for SCHEME IN against invoice {} total pcs({}) {}", totalCashback,
|
|
|
571 |
purchase.getPurchaseReference(), itemsCount);
|
| 34504 |
amit.gupta |
572 |
}
|
| 34317 |
amit.gupta |
573 |
|
| 31903 |
amit.gupta |
574 |
}
|
|
|
575 |
|
|
|
576 |
|
| 31410 |
amit.gupta |
577 |
// We are maintaining price drop after grn
|
|
|
578 |
private float getAmount(InventoryItem inventoryItem, Scheme scheme) throws ProfitMandiBusinessException {
|
| 34221 |
tejus.loha |
579 |
if (this.getBlockedImeis().contains(inventoryItem.getSerialNumber())) {
|
| 31410 |
amit.gupta |
580 |
return 0;
|
|
|
581 |
}
|
|
|
582 |
float amount = 0;
|
|
|
583 |
float dpForCalc = 0;
|
|
|
584 |
float taxableSellingPrice = 0;
|
| 29927 |
amit.gupta |
585 |
|
| 31410 |
amit.gupta |
586 |
//float totalTaxRate = stateGstRateRepository.getTotalTaxRate(inventoryItem.getItemId());
|
|
|
587 |
if (scheme.getAmountType().equals(AmountType.PERCENTAGE)) {
|
| 34317 |
amit.gupta |
588 |
if (scheme.getType().getTransactionType().equals(StockTransactionType.IN)) {
|
| 31410 |
amit.gupta |
589 |
dpForCalc = inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount();
|
|
|
590 |
} else {
|
|
|
591 |
try {
|
|
|
592 |
dpForCalc = Math.min(inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount(),
|
|
|
593 |
tagListingRepository.selectByItemId(inventoryItem.getItemId()).getSellingPrice());
|
|
|
594 |
} catch (Exception e) {
|
|
|
595 |
LOGGER.info("Could not find tag Listing entry in {}", inventoryItem.getItemId());
|
|
|
596 |
e.printStackTrace();
|
|
|
597 |
}
|
|
|
598 |
}
|
|
|
599 |
//TODO:Should be calculated on unit price
|
|
|
600 |
//taxableSellingPrice = dpForCalc / (1 + totalTaxRate / 100);
|
|
|
601 |
//amount = taxableSellingPrice * scheme.getAmount() / 100;
|
|
|
602 |
amount = dpForCalc * scheme.getAmount() / 100;
|
|
|
603 |
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(),
|
|
|
604 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
|
|
605 |
scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
|
|
|
606 |
scheme.getAmount(), amount));
|
| 34317 |
amit.gupta |
607 |
} else if (scheme.getType().getTransactionType().equals(StockTransactionType.IN)) {
|
| 31410 |
amit.gupta |
608 |
amount = scheme.getAmount();
|
|
|
609 |
}
|
|
|
610 |
return amount;
|
|
|
611 |
}
|
| 23444 |
amit.gupta |
612 |
|
| 31903 |
amit.gupta |
613 |
//Only in and activation margins are allowed to be rolled out more than twice
|
| 32165 |
amit.gupta |
614 |
private float createSchemeInOut(List<SchemeSummaryModel> schemeSummaryModels, InventoryItem inventoryItem) throws ProfitMandiBusinessException {
|
| 33614 |
amit.gupta |
615 |
LOGGER.info("schemeSummaryModels - {}", schemeSummaryModels);
|
| 31921 |
amit.gupta |
616 |
InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(inventoryItem);
|
| 31903 |
amit.gupta |
617 |
//Get all schemes
|
|
|
618 |
List<SchemeSummaryModel> inventoryPayoutModelToProcess = schemeSummaryModels.stream().filter(x -> x.isProcess()).collect(Collectors.toList());
|
| 34709 |
amit.gupta |
619 |
LOGGER.info("inventoryPayoutModel - {}", inventoryPayoutModel);
|
| 32165 |
amit.gupta |
620 |
List<SchemeInOut> paidSios = inventoryPayoutModel.getPaidSios();
|
|
|
621 |
List<SchemeInOut> pendingSios = inventoryPayoutModel.getPendingSios();
|
|
|
622 |
Map<SchemeType, List<SchemeInOut>> paidSchemeTypesMap = inventoryPayoutModel.getPaidSios().stream().collect(Collectors.groupingBy(x -> x.getScheme().getType()));
|
|
|
623 |
Map<Integer, SchemeInOut> paidSchemesMap = paidSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
|
|
|
624 |
Map<Integer, SchemeInOut> pendingSchemesMap = pendingSios.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
|
| 32155 |
amit.gupta |
625 |
Map<SchemeType, Float> schemeTypeCancelledAmountMap = new HashMap<>();
|
| 31903 |
amit.gupta |
626 |
|
|
|
627 |
double percentageToPay = 0d;
|
|
|
628 |
double fixedToPay = 0d;
|
|
|
629 |
Map<SchemeSummaryModel, AmountModel> payoutSchemeSummaryModelMap = new HashMap<>();
|
|
|
630 |
for (SchemeSummaryModel schemeSummaryModelToProcess : inventoryPayoutModelToProcess) {
|
| 32972 |
amit.gupta |
631 |
if (paidSchemesMap.containsKey(schemeSummaryModelToProcess.getSchemeId()) || pendingSchemesMap.containsKey(schemeSummaryModelToProcess.getSchemeId()))
|
|
|
632 |
continue;
|
| 31903 |
amit.gupta |
633 |
//If different type but on higher side provide the higher margin
|
| 34317 |
amit.gupta |
634 |
if (Arrays.asList(SchemeType.SPECIAL_SUPPORT).contains(schemeSummaryModelToProcess.getSchemeType())) {
|
| 32165 |
amit.gupta |
635 |
Scheme scheme = schemeRepository.selectById(schemeSummaryModelToProcess.getSchemeId());
|
|
|
636 |
//Create only if the activation if the activation date is not known
|
|
|
637 |
ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(inventoryItem.getSerialNumber());
|
| 32972 |
amit.gupta |
638 |
if (activatedImei == null || activatedImei.getActivationTimestamp() == null) {
|
| 32165 |
amit.gupta |
639 |
SchemeInOut sio = new SchemeInOut();
|
|
|
640 |
sio.setAmount(0);
|
|
|
641 |
sio.setInventoryItemId(inventoryItem.getId());
|
|
|
642 |
sio.setSchemeId(schemeSummaryModelToProcess.getSchemeId());
|
|
|
643 |
sio.setStatusDescription("Activation pending for IMEI#" + inventoryItem.getSerialNumber());
|
|
|
644 |
sio.setStatus(SchemePayoutStatus.PENDING);
|
|
|
645 |
schemeInOutRepository.persist(sio);
|
|
|
646 |
}
|
| 34317 |
amit.gupta |
647 |
} else if (!StockTransactionType.IN.equals(schemeSummaryModelToProcess.getSchemeType().getTransactionType())) {
|
| 31903 |
amit.gupta |
648 |
//We have got non repeating scheme type
|
| 34709 |
amit.gupta |
649 |
|
|
|
650 |
if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
651 |
percentageToPay += schemeSummaryModelToProcess.getAmount();
|
| 31903 |
amit.gupta |
652 |
} else {
|
| 34709 |
amit.gupta |
653 |
fixedToPay += schemeSummaryModelToProcess.getAmount();
|
| 31903 |
amit.gupta |
654 |
}
|
| 34709 |
amit.gupta |
655 |
payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
|
|
|
656 |
|
| 31903 |
amit.gupta |
657 |
} else {
|
|
|
658 |
if (schemeSummaryModelToProcess.getAmountType().equals(AmountType.PERCENTAGE)) {
|
| 34709 |
amit.gupta |
659 |
//Check for rejected CashDiscounts dont continue if its ever rejected once
|
|
|
660 |
if (SchemeType.CDS.contains(schemeSummaryModelToProcess.getSchemeType())) {
|
|
|
661 |
List<SchemeInOut> cdSchemeInOuts = schemeInOutRepository.selectAllByType(schemeSummaryModelToProcess.getSchemeType(), inventoryItem.getId());
|
|
|
662 |
LOGGER.info(cdSchemeInOuts);
|
|
|
663 |
if (!cdSchemeInOuts.isEmpty()) continue;
|
|
|
664 |
}
|
| 31903 |
amit.gupta |
665 |
percentageToPay += schemeSummaryModelToProcess.getAmount();
|
|
|
666 |
} else {
|
|
|
667 |
fixedToPay += schemeSummaryModelToProcess.getAmount();
|
|
|
668 |
}
|
|
|
669 |
payoutSchemeSummaryModelMap.put(schemeSummaryModelToProcess, new AmountModel(schemeSummaryModelToProcess.getAmount(), schemeSummaryModelToProcess.getAmountType()));
|
|
|
670 |
}
|
|
|
671 |
}
|
|
|
672 |
double walletCredit = 0d;
|
|
|
673 |
if (fixedToPay > 0) {
|
| 31913 |
amit.gupta |
674 |
double fixedRollout = fixedToPay * (100 / (100 + inventoryPayoutModel.getPercentageAmount()));
|
| 31903 |
amit.gupta |
675 |
for (Map.Entry<SchemeSummaryModel, AmountModel> schemeSummaryModelAmountModelEntry : payoutSchemeSummaryModelMap.entrySet()) {
|
|
|
676 |
SchemeSummaryModel schemeSummaryModel = schemeSummaryModelAmountModelEntry.getKey();
|
|
|
677 |
AmountModel amountModel = schemeSummaryModelAmountModelEntry.getValue();
|
|
|
678 |
if (amountModel.getAmountType().equals(AmountType.FIXED)) {
|
|
|
679 |
SchemeInOut sio = new SchemeInOut();
|
|
|
680 |
sio.setSchemeId(schemeSummaryModel.getSchemeId());
|
|
|
681 |
sio.setInventoryItemId(inventoryItem.getId());
|
|
|
682 |
sio.setStatus(SchemePayoutStatus.CREDITED);
|
|
|
683 |
sio.setCreditTimestamp(LocalDateTime.now());
|
| 32155 |
amit.gupta |
684 |
sio.setAmount((float) (fixedRollout * amountModel.getAmount() / fixedToPay) + schemeTypeCancelledAmountMap.getOrDefault(schemeSummaryModel.getSchemeType(), 0f));
|
| 34317 |
amit.gupta |
685 |
if (schemeSummaryModel.getSchemeType().getTransactionType().equals(StockTransactionType.IN))
|
| 31903 |
amit.gupta |
686 |
sio.setStatusDescription("Credited for GRN of IMEI-" + inventoryItem.getSerialNumber());
|
|
|
687 |
else
|
|
|
688 |
sio.setStatusDescription("Credited for Sale of IMEI-" + inventoryItem.getSerialNumber());
|
|
|
689 |
schemeInOutRepository.persist(sio);
|
|
|
690 |
}
|
|
|
691 |
}
|
|
|
692 |
walletCredit += fixedRollout;
|
|
|
693 |
}
|
|
|
694 |
|
|
|
695 |
if (percentageToPay > 0) {
|
| 31987 |
amit.gupta |
696 |
LOGGER.info("inventoryPayoutModel.getFixedAmount() ----> {}", inventoryPayoutModel.getFixedAmount());
|
| 31914 |
amit.gupta |
697 |
double effectiveDP = inventoryPayoutModel.getDp() - (inventoryPayoutModel.getFixedAmount() + fixedToPay);
|
| 31903 |
amit.gupta |
698 |
double totalPercentage = inventoryPayoutModel.getPercentageAmount() + percentageToPay;
|
|
|
699 |
double percentageRollout = effectiveDP * (totalPercentage / (100 + totalPercentage) - (inventoryPayoutModel.getPercentageAmount() / (100 + inventoryPayoutModel.getPercentageAmount())));
|
|
|
700 |
for (Map.Entry<SchemeSummaryModel, AmountModel> schemeSummaryModelAmountModelEntry : payoutSchemeSummaryModelMap.entrySet()) {
|
|
|
701 |
SchemeSummaryModel schemeSummaryModel = schemeSummaryModelAmountModelEntry.getKey();
|
|
|
702 |
AmountModel amountModel = schemeSummaryModelAmountModelEntry.getValue();
|
|
|
703 |
if (amountModel.getAmountType().equals(AmountType.PERCENTAGE)) {
|
|
|
704 |
SchemeInOut sio = new SchemeInOut();
|
|
|
705 |
sio.setInventoryItemId(inventoryItem.getId());
|
|
|
706 |
sio.setSchemeId(schemeSummaryModel.getSchemeId());
|
|
|
707 |
sio.setStatus(SchemePayoutStatus.CREDITED);
|
|
|
708 |
sio.setCreditTimestamp(LocalDateTime.now());
|
| 32155 |
amit.gupta |
709 |
sio.setAmount((float) (percentageRollout * amountModel.getAmount() / percentageToPay) +
|
|
|
710 |
schemeTypeCancelledAmountMap.getOrDefault(schemeSummaryModel.getSchemeType(), 0f));
|
| 34317 |
amit.gupta |
711 |
if (schemeSummaryModel.getSchemeType().getTransactionType().equals(StockTransactionType.IN))
|
| 31903 |
amit.gupta |
712 |
sio.setStatusDescription("Credited for GRN of IMEI-" + inventoryItem.getSerialNumber());
|
|
|
713 |
else
|
|
|
714 |
sio.setStatusDescription("Credited for Sale of IMEI-" + inventoryItem.getSerialNumber());
|
|
|
715 |
schemeInOutRepository.persist(sio);
|
|
|
716 |
}
|
|
|
717 |
}
|
|
|
718 |
walletCredit += percentageRollout;
|
|
|
719 |
}
|
|
|
720 |
|
|
|
721 |
return (float) walletCredit;
|
|
|
722 |
}
|
|
|
723 |
|
|
|
724 |
private Set<Integer> filterImeisByAgeing(Set<Integer> inventoryItemIds, FofoOrder fofoOrder) {
|
|
|
725 |
Set<Integer> filteredInventoryIds = new HashSet<>();
|
|
|
726 |
List<PartnerAgeingModel> partnerAgeingModels = ageingService.filterAgedInventory(inventoryItemIds);
|
|
|
727 |
for (PartnerAgeingModel partnerAgeingModel : partnerAgeingModels) {
|
|
|
728 |
LOGGER.info("Serial Number - {}", partnerAgeingModel.getSerialNumber());
|
|
|
729 |
if (partnerAgeingModel.getBrand().equalsIgnoreCase("Samsung")) {
|
|
|
730 |
ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(partnerAgeingModel.getSerialNumber());
|
|
|
731 |
if (activatedImei != null && activatedImei.getActivationTimestamp().toLocalDate()
|
|
|
732 |
.isBefore(partnerAgeingModel.getBillingDate().plusDays(partnerAgeingModel.getMaxAgeingDays()))) {
|
|
|
733 |
//Lets give money if activation is there and is before the ageing limit of that brand.
|
|
|
734 |
filteredInventoryIds.add(partnerAgeingModel.getInventoryItemId());
|
|
|
735 |
} else {
|
|
|
736 |
//If billing happens before ageing expiry
|
|
|
737 |
if (fofoOrder.getCreateTimestamp().toLocalDate().isBefore(partnerAgeingModel.getBillingDate().plusDays(partnerAgeingModel.getMaxAgeingDays()))) {
|
|
|
738 |
filteredInventoryIds.add(partnerAgeingModel.getInventoryItemId());
|
|
|
739 |
}
|
|
|
740 |
}
|
|
|
741 |
} else {
|
|
|
742 |
filteredInventoryIds.add(partnerAgeingModel.getInventoryItemId());
|
|
|
743 |
}
|
|
|
744 |
}
|
|
|
745 |
return filteredInventoryIds;
|
|
|
746 |
}
|
|
|
747 |
|
| 33087 |
amit.gupta |
748 |
@Autowired
|
|
|
749 |
WarehouseInventoryItemRepository warehouseInventoryItemRepository;
|
|
|
750 |
|
| 31410 |
amit.gupta |
751 |
@Override
|
|
|
752 |
public float processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
|
| 34504 |
amit.gupta |
753 |
float totalCashback = 0;
|
| 31410 |
amit.gupta |
754 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(retailerId, fofoOrderId);
|
|
|
755 |
// Process only if order is not cancelled
|
|
|
756 |
if (fofoOrder.getCancelledTimestamp() == null) {
|
|
|
757 |
// PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId,
|
|
|
758 |
// fofoOrder.getCreateTimestamp().toLocalDate());
|
|
|
759 |
// TODO - SCHEME
|
|
|
760 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
|
|
|
761 |
YearMonth.from(fofoOrder.getCreateTimestamp()));
|
| 29927 |
amit.gupta |
762 |
|
| 31410 |
amit.gupta |
763 |
List<ScanRecord> scanRecords = scanRecordRepository.selectAllByOrderId(fofoOrderId);
|
|
|
764 |
if (scanRecords.size() == 0) return 0;
|
|
|
765 |
Set<Integer> inventoryItemIds = scanRecords.stream().map(x -> x.getInventoryItemId())
|
|
|
766 |
.collect(Collectors.toSet());
|
| 31903 |
amit.gupta |
767 |
//Check for ageing
|
| 31912 |
amit.gupta |
768 |
//inventoryItemIds = this.filterImeisByAgeing(inventoryItemIds, fofoOrder);
|
| 31903 |
amit.gupta |
769 |
//ageingService.filterAgedInventory(inventoryItemIds);
|
| 31410 |
amit.gupta |
770 |
LOGGER.info("fofoOrderId --- {}", fofoOrderId);
|
|
|
771 |
LOGGER.info("scanRecords --- {}", scanRecords);
|
|
|
772 |
LOGGER.info("inventoryItemIds --- {}", inventoryItemIds);
|
| 31903 |
amit.gupta |
773 |
|
|
|
774 |
if (inventoryItemIds.size() == 0) return 0;
|
|
|
775 |
|
| 31410 |
amit.gupta |
776 |
Set<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIds).stream()
|
|
|
777 |
.filter(x -> x.getSerialNumber() != null && !x.getSerialNumber().equals(""))
|
|
|
778 |
.collect(Collectors.toSet());
|
| 34221 |
tejus.loha |
779 |
inventoryItems = inventoryItems.stream().filter(inventoryItem -> !this.getBlockedImeis().contains(inventoryItem.getSerialNumber())).collect(Collectors.toSet());
|
| 29927 |
amit.gupta |
780 |
|
| 33087 |
amit.gupta |
781 |
//Do not consider imei above 90 days for samsung
|
| 33432 |
amit.gupta |
782 |
List<String> samsungSerialNumbers = inventoryItems.stream().filter(x -> x.getItem().getBrand().equalsIgnoreCase("samsung")).map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
| 33600 |
ranu |
783 |
|
| 33432 |
amit.gupta |
784 |
if (samsungSerialNumbers.size() > 0) {
|
| 33087 |
amit.gupta |
785 |
List<AgeingSummaryModel> billedImeiModels = warehouseInventoryItemRepository.findStockAgeingByFofoIdSerialNumbers(retailerId, samsungSerialNumbers);
|
| 33606 |
ranu |
786 |
List<String> agedSerialNumbers = billedImeiModels.stream().filter(x -> x.isAgedAbove(365)).map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
|
|
787 |
if (agedSerialNumbers.size() > 0) {
|
|
|
788 |
List<String> samsungExceptionsSerialNumbers = samsungExceptionRepository.selectAllBySerialNumber(agedSerialNumbers).stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
|
|
|
789 |
agedSerialNumbers.removeAll(samsungExceptionsSerialNumbers);
|
|
|
790 |
}
|
| 33432 |
amit.gupta |
791 |
inventoryItems = inventoryItems.stream().filter(x -> !agedSerialNumbers.contains(x.getSerialNumber())).collect(Collectors.toSet());
|
| 33087 |
amit.gupta |
792 |
}
|
|
|
793 |
|
| 31903 |
amit.gupta |
794 |
if (inventoryItems.size() == 0) return 0;
|
| 29927 |
amit.gupta |
795 |
|
| 31903 |
amit.gupta |
796 |
Map<Integer, List<InventoryItem>> catalogInventoryItemMap = inventoryItems.stream().collect(Collectors.groupingBy(x -> x.getItem().getCatalogItemId()));
|
|
|
797 |
Map<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSchemeSummaryMap = tagListingRepository.getModelSchemesByCatalogIdsAndType(retailerId,
|
|
|
798 |
partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), fofoOrder.getCreateTimestamp());
|
| 23444 |
amit.gupta |
799 |
|
| 34317 |
amit.gupta |
800 |
LOGGER.info("catalogSchemeSummaryMap - {}", catalogSchemeSummaryMap);
|
| 31903 |
amit.gupta |
801 |
|
| 34317 |
amit.gupta |
802 |
|
| 31410 |
amit.gupta |
803 |
int count = 0;
|
| 31903 |
amit.gupta |
804 |
for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
|
|
|
805 |
CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
|
| 32058 |
amit.gupta |
806 |
List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue().stream().filter(x -> x != null).collect(Collectors.toList());
|
| 34317 |
amit.gupta |
807 |
|
|
|
808 |
schemeSummaryModels.stream().filter(x -> x.getSchemeType().getTransactionType().equals(StockTransactionType.OUT)).forEach(x -> x.setProcess(true));
|
| 31903 |
amit.gupta |
809 |
if (schemeSummaryModels.stream().filter(x -> x.isProcess()).count() == 0) continue;
|
| 23444 |
amit.gupta |
810 |
|
| 31903 |
amit.gupta |
811 |
List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
|
|
|
812 |
for (InventoryItem inventoryItem : modelInventoryItems) {
|
| 34698 |
amit.gupta |
813 |
Map<Integer, SchemeSummaryModel> schemeSummaryModelMap = schemeSummaryModels.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
|
| 34705 |
amit.gupta |
814 |
List<SchemeInOut> sios = schemeInOutRepository.selectAll(inventoryItem.getId());
|
| 34740 |
amit.gupta |
815 |
|
| 34705 |
amit.gupta |
816 |
List<Integer> creditedSchemeIds = sios.stream()
|
|
|
817 |
.filter(x -> x.getStatus().equals(SchemePayoutStatus.CREDITED))
|
|
|
818 |
.map(x -> x.getSchemeId()).collect(Collectors.toList());
|
| 34698 |
amit.gupta |
819 |
|
| 34742 |
amit.gupta |
820 |
double sioRejectedValue = 0;
|
| 34740 |
amit.gupta |
821 |
if (creditedSchemeIds.size() > 0) {
|
|
|
822 |
List<Integer> schemeIdsToReject = schemeRepository.selectBySchemeIds(creditedSchemeIds).stream()
|
|
|
823 |
.filter(x -> SchemeType.OUT_TYPES.contains(x.getType())).map(x -> x.getId()).collect(Collectors.toList());
|
|
|
824 |
//Reject invalid scheme payouts due to upgrade in Category or any change in schemes historically
|
|
|
825 |
for (SchemeInOut sio : sios) {
|
|
|
826 |
if (schemeIdsToReject.contains(sio.getSchemeId()) && !schemeSummaryModelMap.containsKey(sio.getSchemeId())) {
|
|
|
827 |
sio.setStatusDescription("Rolledback due to Category upgrade/invalid scheme");
|
|
|
828 |
sio.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
829 |
sio.setRolledBackTimestamp(LocalDateTime.now());
|
| 34742 |
amit.gupta |
830 |
Scheme scheme = schemeRepository.selectById(sio.getSchemeId());
|
|
|
831 |
if(scheme.getType().equals(SchemeType.SPECIAL_SUPPORT)) {
|
|
|
832 |
ScanRecord salesRecord = scanRecordRepository.selectByInventoryItemId(sio.getInventoryItemId()).stream().sorted(Comparator.comparing(ScanRecord::getId).reversed()).findFirst().get();
|
|
|
833 |
walletService.rollbackAmountFromWallet(retailerId,sio.getAmount(), salesRecord.getOrderId(),
|
|
|
834 |
WalletReferenceType.SPECIAL_SUPPORT, "Reversed due to invalid scheme for IMEI - " + inventoryItem.getSerialNumber(), LocalDateTime.now());
|
|
|
835 |
fofoOrder.setCashback(totalCashback + fofoOrder.getCashback());
|
|
|
836 |
} else {
|
|
|
837 |
sioRejectedValue += sio.getAmount();
|
|
|
838 |
}
|
| 34740 |
amit.gupta |
839 |
}
|
| 34705 |
amit.gupta |
840 |
}
|
| 34698 |
amit.gupta |
841 |
}
|
| 32165 |
amit.gupta |
842 |
float inventoryItemCashback = this.createSchemeInOut(schemeSummaryModels, inventoryItem);
|
| 34742 |
amit.gupta |
843 |
if (inventoryItemCashback > 0 || sioRejectedValue > 0) {
|
| 31903 |
amit.gupta |
844 |
count++;
|
| 34742 |
amit.gupta |
845 |
totalCashback += inventoryItemCashback - sioRejectedValue;
|
| 31903 |
amit.gupta |
846 |
}
|
| 31410 |
amit.gupta |
847 |
}
|
|
|
848 |
}
|
| 31903 |
amit.gupta |
849 |
|
| 31410 |
amit.gupta |
850 |
if (count > 0) {
|
|
|
851 |
walletService.addAmountToWallet(
|
|
|
852 |
retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT, "Sales margin for invoice number "
|
|
|
853 |
+ fofoOrder.getInvoiceNumber() + ". Total " + count + " pc(s)",
|
|
|
854 |
totalCashback, fofoOrder.getCreateTimestamp());
|
|
|
855 |
fofoOrder.setCashback(totalCashback + fofoOrder.getCashback());
|
|
|
856 |
}
|
|
|
857 |
}
|
| 34504 |
amit.gupta |
858 |
return totalCashback;
|
| 31410 |
amit.gupta |
859 |
}
|
| 22653 |
ashik.ali |
860 |
|
| 31410 |
amit.gupta |
861 |
@Override
|
|
|
862 |
//Tax rate has been passed to 0 to ensure no tax deduction
|
|
|
863 |
public float getSpecialSupportAmount(float supportAmount, PartnerType partnerType, LocalDate onDate,
|
|
|
864 |
int catalogId) throws ProfitMandiBusinessException {
|
|
|
865 |
//int itemId = itemRepository.selectAllByCatalogItemId(catalogId).stream().findAny().get().getId();
|
|
|
866 |
//float totalTaxRate = stateGstRateRepository.getTotalTaxRate(itemId);
|
|
|
867 |
return this.getSpecialSupportAmount(supportAmount, partnerType, onDate, catalogId, 0);
|
|
|
868 |
}
|
| 23444 |
amit.gupta |
869 |
|
| 31410 |
amit.gupta |
870 |
@Override
|
|
|
871 |
public float getSpecialSupportAmount(float supportAmount, PartnerType partnerType, LocalDate onDate,
|
|
|
872 |
int catalogId, float taxRate) throws ProfitMandiBusinessException {
|
|
|
873 |
float totalMargin = this.selectPercentageScheme(partnerType, onDate, catalogId, false, 0, 0).stream().collect(Collectors.summingDouble(x -> x.getAmount())).floatValue();
|
|
|
874 |
float amountToCredit = supportAmount * (1 - (totalMargin / (100 + taxRate)));
|
|
|
875 |
return amountToCredit;
|
|
|
876 |
}
|
| 23444 |
amit.gupta |
877 |
|
| 31410 |
amit.gupta |
878 |
@Override
|
| 33614 |
amit.gupta |
879 |
public void rollbackSchemes(List<Integer> inventoryItemIds, String rollbackReason)
|
| 31410 |
amit.gupta |
880 |
throws Exception {
|
| 33614 |
amit.gupta |
881 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectAllByIds(inventoryItemIds);
|
|
|
882 |
Map<Integer, InventoryItem> inventoryItemMap = inventoryItems.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
883 |
Map<Integer, Integer> purchasePartnerMap = inventoryItems.stream().collect(Collectors.toMap(x -> x.getPurchaseId(), x -> x.getFofoId(), (u, v) -> u));
|
|
|
884 |
|
|
|
885 |
LOGGER.info("inventoryItemIds - {}", inventoryItemIds);
|
|
|
886 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(new HashSet<>(inventoryItemIds));
|
|
|
887 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).distinct().collect(Collectors.toList());
|
| 34139 |
amit.gupta |
888 |
if (schemeIds.size() == 0) return;
|
| 33614 |
amit.gupta |
889 |
List<Scheme> schemes = schemeRepository.selectBySchemeIds(schemeIds);
|
|
|
890 |
Map<Integer, Float> inSchemesMap = new HashMap<>();
|
|
|
891 |
Map<Integer, Float> outSchemesMap = new HashMap<>();
|
|
|
892 |
Map<Integer, Scheme> schemesMap = schemes.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
893 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
894 |
Map<Integer, Float> schemePayoutMap;
|
|
|
895 |
int inventoryItemId = schemeInOut.getInventoryItemId();
|
| 31410 |
amit.gupta |
896 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
897 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
898 |
if (schemeInOut.getStatus() == null || schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
| 33614 |
amit.gupta |
899 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
| 34317 |
amit.gupta |
900 |
if (scheme.getType().getTransactionType().equals(StockTransactionType.IN)) {
|
| 33614 |
amit.gupta |
901 |
schemePayoutMap = inSchemesMap;
|
|
|
902 |
} else {
|
|
|
903 |
schemePayoutMap = outSchemesMap;
|
|
|
904 |
}
|
|
|
905 |
if (!schemePayoutMap.containsKey(inventoryItemId)) {
|
|
|
906 |
schemePayoutMap.put(inventoryItemId, 0f);
|
|
|
907 |
}
|
| 34139 |
amit.gupta |
908 |
schemePayoutMap.put(inventoryItemId, inSchemesMap.get(inventoryItemId) == null ? 0 : inSchemesMap.get(inventoryItemId) + schemeInOut.getAmount());
|
| 31410 |
amit.gupta |
909 |
}
|
|
|
910 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
911 |
schemeInOut.setStatusDescription(rollbackReason);
|
|
|
912 |
}
|
|
|
913 |
}
|
| 33614 |
amit.gupta |
914 |
Map<Integer, Double> purchaseRollbackAmountMap = inSchemesMap.entrySet().stream().collect(Collectors.groupingBy(x -> inventoryItemMap.get(x.getKey()).getPurchaseId(), Collectors.summingDouble(x -> x.getValue())));
|
|
|
915 |
|
|
|
916 |
for (Map.Entry<Integer, Double> purchaseRollbackAmountEntry : purchaseRollbackAmountMap.entrySet()) {
|
|
|
917 |
int purchaseId = purchaseRollbackAmountEntry.getKey();
|
|
|
918 |
Double amountToRollback = purchaseRollbackAmountEntry.getValue();
|
|
|
919 |
if (amountToRollback != null && amountToRollback > 0) {
|
|
|
920 |
walletService.rollbackAmountFromWallet(purchasePartnerMap.get(purchaseId), amountToRollback.floatValue(), purchaseId,
|
|
|
921 |
WalletReferenceType.SCHEME_IN, rollbackReason, LocalDateTime.now());
|
|
|
922 |
}
|
| 31410 |
amit.gupta |
923 |
}
|
|
|
924 |
}
|
| 23444 |
amit.gupta |
925 |
|
| 31410 |
amit.gupta |
926 |
@Override
|
|
|
927 |
public Map<String, Object> getSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
928 |
throws ProfitMandiBusinessException {
|
|
|
929 |
Map<String, Object> map = new HashMap<>();
|
|
|
930 |
List<Scheme> schemes = null;
|
|
|
931 |
long size = 0;
|
|
|
932 |
if (roleManager.isAdmin(roleIds)) {
|
|
|
933 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
934 |
size = schemeRepository.selectAllCount();
|
|
|
935 |
} else {
|
|
|
936 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
937 |
size = schemeRepository.selectAllActiveCount();
|
|
|
938 |
}
|
|
|
939 |
map.put("schemes", schemes);
|
|
|
940 |
map.put("start", offset + 1);
|
|
|
941 |
map.put("size", size);
|
|
|
942 |
if (schemes.size() < limit) {
|
|
|
943 |
map.put("end", offset + schemes.size());
|
|
|
944 |
} else {
|
|
|
945 |
map.put("end", offset + limit);
|
|
|
946 |
}
|
|
|
947 |
return map;
|
|
|
948 |
}
|
| 26722 |
amit.gupta |
949 |
|
| 31410 |
amit.gupta |
950 |
@Override
|
|
|
951 |
public List<Scheme> getPaginatedSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
952 |
throws ProfitMandiBusinessException {
|
|
|
953 |
LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
|
|
|
954 |
List<Scheme> schemes = null;
|
|
|
955 |
if (roleManager.isAdmin(roleIds)) {
|
|
|
956 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
957 |
} else {
|
|
|
958 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
959 |
}
|
|
|
960 |
return schemes;
|
|
|
961 |
}
|
| 27898 |
amit.gupta |
962 |
|
| 31410 |
amit.gupta |
963 |
@Override
|
|
|
964 |
// This is being called to reverse schemes while processing price Drop
|
|
|
965 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int priceDropId, String reversalReason)
|
|
|
966 |
throws ProfitMandiBusinessException {
|
|
|
967 |
PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
|
|
|
968 |
Map<Integer, List<InventoryItem>> purchaseInventoryListMap = inventoryItems.stream()
|
|
|
969 |
.collect(Collectors.groupingBy(InventoryItem::getPurchaseId, Collectors.toList()));
|
| 29927 |
amit.gupta |
970 |
|
| 31410 |
amit.gupta |
971 |
for (Map.Entry<Integer, List<InventoryItem>> purchaseEntry : purchaseInventoryListMap.entrySet()) {
|
|
|
972 |
float amountToCredit = 0;
|
|
|
973 |
float amountToDebit = 0;
|
|
|
974 |
int purchaseId = purchaseEntry.getKey();
|
|
|
975 |
List<InventoryItem> purchaseInventoryItemList = purchaseEntry.getValue();
|
| 30454 |
amit.gupta |
976 |
|
| 31410 |
amit.gupta |
977 |
Map<Integer, InventoryItem> inventoryItemsMap = purchaseInventoryItemList.stream()
|
|
|
978 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 29927 |
amit.gupta |
979 |
|
| 31410 |
amit.gupta |
980 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
|
|
981 |
LOGGER.info("Scheme InOuts , {}", schemeInOuts);
|
|
|
982 |
if (schemeInOuts.size() == 0) {
|
|
|
983 |
continue;
|
|
|
984 |
}
|
|
|
985 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
986 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
987 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
988 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
989 |
InventoryItem ii = inventoryItemsMap.get(schemeInOut.getInventoryItemId());
|
|
|
990 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
991 |
if (scheme.getAmountType().equals(AmountType.FIXED)) {
|
|
|
992 |
continue;
|
|
|
993 |
}
|
| 34317 |
amit.gupta |
994 |
if (scheme.getType().getTransactionType().equals(StockTransactionType.IN) && schemeInOut.getRolledBackTimestamp() == null) {
|
| 33432 |
amit.gupta |
995 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
996 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
997 |
schemeInOut.setStatusDescription("Margin reversed due to price drop");
|
|
|
998 |
// IF not credited then dont consider any credit/debit for that sio entry
|
|
|
999 |
if (schemeInOut.getCreditTimestamp() != null) {
|
|
|
1000 |
amountToDebit += schemeInOut.getAmount();
|
| 31410 |
amit.gupta |
1001 |
}
|
|
|
1002 |
}
|
|
|
1003 |
}
|
|
|
1004 |
int fofoId = inventoryItems.get(0).getFofoId();
|
|
|
1005 |
if (amountToDebit > 0) {
|
|
|
1006 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN,
|
|
|
1007 |
MessageFormat.format(reversalReason, purchaseInventoryItemList.size()), -amountToDebit,
|
|
|
1008 |
priceDrop.getAffectedOn());
|
|
|
1009 |
}
|
|
|
1010 |
}
|
|
|
1011 |
}
|
| 29231 |
amit.gupta |
1012 |
|
| 31609 |
amit.gupta |
1013 |
@Autowired
|
| 32060 |
amit.gupta |
1014 |
UserWalletRepository userWalletRepository;
|
|
|
1015 |
@Autowired
|
| 31609 |
amit.gupta |
1016 |
UserWalletHistoryRepository userWalletHistoryRepository;
|
|
|
1017 |
|
| 31410 |
amit.gupta |
1018 |
@Override
|
|
|
1019 |
// Always being called from cancel order/bad return means no SCHEME IN is considered
|
|
|
1020 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int reversalReference, String reversalReason,
|
|
|
1021 |
List<SchemeType> schemeTypes) throws ProfitMandiBusinessException {
|
|
|
1022 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
|
|
|
1023 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1024 |
LOGGER.info("inventoryItems" + inventoryItems);
|
| 23444 |
amit.gupta |
1025 |
|
| 31410 |
amit.gupta |
1026 |
Map<SchemeType, SchemeInOut> schemeTypeMap = new HashMap<>();
|
| 30454 |
amit.gupta |
1027 |
|
| 31410 |
amit.gupta |
1028 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
| 32060 |
amit.gupta |
1029 |
List<SchemeInOut> rolledBacks = schemeInOuts.stream().filter(x -> x.getStatusDescription().equals(reversalReason)).collect(Collectors.toList());
|
| 31410 |
amit.gupta |
1030 |
float amountToRollback = 0;
|
| 23444 |
amit.gupta |
1031 |
|
| 31410 |
amit.gupta |
1032 |
if (!schemeInOuts.isEmpty()) {
|
|
|
1033 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
1034 |
LOGGER.info("schemeIds" + schemeIds);
|
| 29231 |
amit.gupta |
1035 |
|
| 31410 |
amit.gupta |
1036 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
1037 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 32060 |
amit.gupta |
1038 |
if (rolledBacks.size() > 0) {
|
|
|
1039 |
for (SchemeInOut schemeInOut : rolledBacks) {
|
|
|
1040 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
1041 |
if (schemeTypes.contains(scheme.getType())) {
|
|
|
1042 |
schemeTypeMap.put(scheme.getType(), schemeInOut);
|
|
|
1043 |
if (schemeInOut.getCreditTimestamp() != null) {
|
| 31410 |
amit.gupta |
1044 |
amountToRollback += schemeInOut.getAmount();
|
|
|
1045 |
}
|
|
|
1046 |
}
|
|
|
1047 |
}
|
| 32060 |
amit.gupta |
1048 |
} else {
|
|
|
1049 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
1050 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
1051 |
if (schemeTypes.contains(scheme.getType())) {
|
|
|
1052 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
1053 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
1054 |
if (schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
|
|
1055 |
amountToRollback += schemeInOut.getAmount();
|
|
|
1056 |
}
|
|
|
1057 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
1058 |
schemeInOut.setStatusDescription(reversalReason);
|
|
|
1059 |
}
|
|
|
1060 |
}
|
|
|
1061 |
}
|
| 31410 |
amit.gupta |
1062 |
}
|
|
|
1063 |
}
|
| 32060 |
amit.gupta |
1064 |
|
| 31410 |
amit.gupta |
1065 |
int fofoId = inventoryItems.get(0).getFofoId();
|
| 34486 |
amit.gupta |
1066 |
WalletReferenceType walletReferenceType = schemeTypes.containsAll(SchemeType.OUT_SCHEME_TYPES) ? WalletReferenceType.SCHEME_OUT
|
| 31609 |
amit.gupta |
1067 |
: schemeTypes.contains(SchemeType.SPECIAL_SUPPORT) ? WalletReferenceType.SPECIAL_SUPPORT
|
| 34486 |
amit.gupta |
1068 |
: schemeTypes.contains(SchemeType.INVESTMENT) ? WalletReferenceType.SCHEME_OUT : null;
|
| 32060 |
amit.gupta |
1069 |
List<UserWalletHistory> userWalletHistoryList = null;
|
| 32058 |
amit.gupta |
1070 |
if (amountToRollback > 0 && walletReferenceType != null) {
|
|
|
1071 |
// Mark appropriate reference of rollback investment margin
|
| 32060 |
amit.gupta |
1072 |
if (schemeTypes.contains(SchemeType.INVESTMENT) && schemeTypeMap.containsKey(SchemeType.INVESTMENT)) {
|
|
|
1073 |
LocalDateTime creditTime = schemeTypeMap.get(SchemeType.INVESTMENT).getCreditTimestamp();
|
|
|
1074 |
if (creditTime == null) return;
|
|
|
1075 |
int investmentReversalReference = Integer.parseInt(FormattingUtils.getYearMonth(creditTime.minusMonths(1)));
|
|
|
1076 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
1077 |
userWalletHistoryList = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(userWallet.getUserId(), investmentReversalReference, WalletReferenceType.INVESTMENT_PAYOUT);
|
|
|
1078 |
if (userWalletHistoryList.size() > 0) {
|
|
|
1079 |
walletReferenceType = WalletReferenceType.INVESTMENT_PAYOUT;
|
|
|
1080 |
} else {
|
|
|
1081 |
userWalletHistoryList = null;
|
| 32058 |
amit.gupta |
1082 |
}
|
| 32060 |
amit.gupta |
1083 |
}
|
|
|
1084 |
if (userWalletHistoryList == null) {
|
|
|
1085 |
userWalletHistoryList = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(reversalReference, walletReferenceType);
|
|
|
1086 |
}
|
|
|
1087 |
if (userWalletHistoryList.size() > 0) {
|
|
|
1088 |
int maxDeductible = userWalletHistoryList.stream().collect(Collectors.summingInt(x -> x.getAmount()));
|
|
|
1089 |
if (maxDeductible > 0) {
|
| 32165 |
amit.gupta |
1090 |
LOGGER.info("----------> maxDeductible {}, amountToRollback {}, reversalReference {}, walletReferenceType {} ", maxDeductible, amountToRollback, reversalReference, walletReferenceType);
|
| 32060 |
amit.gupta |
1091 |
walletService.rollbackAmountFromWallet(fofoId, Math.min(maxDeductible, amountToRollback), reversalReference, walletReferenceType,
|
|
|
1092 |
reversalReason, LocalDateTime.now());
|
|
|
1093 |
}
|
| 31609 |
amit.gupta |
1094 |
|
| 31903 |
amit.gupta |
1095 |
}
|
| 32058 |
amit.gupta |
1096 |
}
|
| 31410 |
amit.gupta |
1097 |
}
|
| 29231 |
amit.gupta |
1098 |
|
| 32060 |
amit.gupta |
1099 |
|
| 31410 |
amit.gupta |
1100 |
@Override
|
|
|
1101 |
public double getTotalMargin(int itemId, PartnerType partnerType, LocalDateTime dateTime) {
|
|
|
1102 |
Session session = sessionFactory.getCurrentSession();
|
|
|
1103 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
1104 |
CriteriaQuery<Double> criteriaQuery = cb.createQuery(Double.class);
|
|
|
1105 |
Root<SchemeItem> schemeItem = criteriaQuery.from(SchemeItem.class);
|
|
|
1106 |
Root<Scheme> scheme = criteriaQuery.from(Scheme.class);
|
|
|
1107 |
Predicate schemePredicate = cb.equal(scheme.get(ProfitMandiConstants.AMOUNT_TYPE), AmountType.PERCENTAGE);
|
|
|
1108 |
Predicate lessThanPredicate = cb.lessThanOrEqualTo(scheme.get(ProfitMandiConstants.END_DATE_TIME), dateTime);
|
|
|
1109 |
Predicate greaterThanPredicate = cb.greaterThanOrEqualTo(scheme.get(ProfitMandiConstants.START_DATE_TIME),
|
|
|
1110 |
dateTime);
|
|
|
1111 |
Predicate joinPredicate = cb.equal(scheme.get("id"), schemeItem.get("schemeId"));
|
|
|
1112 |
Predicate schemeItemPredicate = cb.equal(schemeItem.get(ProfitMandiConstants.ITEM_ID), itemId);
|
|
|
1113 |
criteriaQuery.select(cb.sum(scheme.get(ProfitMandiConstants.AMOUNT))).where(schemePredicate, lessThanPredicate,
|
|
|
1114 |
greaterThanPredicate, schemeItemPredicate, joinPredicate);
|
| 29231 |
amit.gupta |
1115 |
|
| 31410 |
amit.gupta |
1116 |
Query<Double> query = session.createQuery(criteriaQuery);
|
|
|
1117 |
return query.getSingleResult() + ProfitMandiConstants.SCHEME_INVESTMENT_MARGIN;
|
| 29231 |
amit.gupta |
1118 |
|
| 31410 |
amit.gupta |
1119 |
}
|
| 23444 |
amit.gupta |
1120 |
|
| 32972 |
amit.gupta |
1121 |
|
| 31410 |
amit.gupta |
1122 |
@Override
|
| 33614 |
amit.gupta |
1123 |
public Map<Integer, Float> getCatalogSchemeCashBack(int fofoId, List<Integer> catalogIds) throws
|
|
|
1124 |
ProfitMandiBusinessException {
|
| 32972 |
amit.gupta |
1125 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
|
|
|
1126 |
Map<CatalogSummaryModel, List<SchemeSummaryModel>> catalogModelMap = tagListingRepository.getModelSchemesByCatalogIdsAndType(fofoId, partnerType, catalogIds, LocalDate.now().atStartOfDay());
|
|
|
1127 |
|
|
|
1128 |
Map<Integer, Float> catalogCashbackMap = new HashMap<>();
|
|
|
1129 |
for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogModelMap.entrySet()) {
|
|
|
1130 |
int catalogItemId = catalogSummaryModelListEntry.getKey().getCatalogId();
|
|
|
1131 |
List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue();
|
|
|
1132 |
|
|
|
1133 |
float totalCashback = schemeSummaryModels.stream()
|
| 34317 |
amit.gupta |
1134 |
.filter(x -> Arrays.asList(SchemeType.SPECIAL_SUPPORT, SchemeType.SELLOUT).contains(x.getSchemeType())
|
| 32972 |
amit.gupta |
1135 |
&& x.getAmountType().equals(AmountType.FIXED))
|
|
|
1136 |
.collect(Collectors.summingDouble(x -> x.getAmount())).floatValue();
|
|
|
1137 |
catalogCashbackMap.put(catalogItemId, totalCashback);
|
| 31410 |
amit.gupta |
1138 |
}
|
| 32972 |
amit.gupta |
1139 |
return catalogCashbackMap;
|
| 31410 |
amit.gupta |
1140 |
}
|
| 30572 |
amit.gupta |
1141 |
|
| 31410 |
amit.gupta |
1142 |
@Override
|
| 31903 |
amit.gupta |
1143 |
public List<Scheme> selectSchemeByPartnerTypeFofoId(PartnerType partnerType, LocalDate onDate, int catalogId,
|
|
|
1144 |
int fofoId, int offset, int limit) throws ProfitMandiBusinessException {
|
| 31410 |
amit.gupta |
1145 |
Session session = sessionFactory.getCurrentSession();
|
|
|
1146 |
final TypedQuery<Scheme> typedQuery = session.createNamedQuery(
|
|
|
1147 |
"Scheme.selectSchemeByModelsPartnerTypeFofoId", Scheme.class);
|
|
|
1148 |
typedQuery.setParameter("catalogIds", Arrays.asList(catalogId));
|
|
|
1149 |
typedQuery.setParameter("fofoIds", Arrays.asList(fofoId, 0));
|
|
|
1150 |
typedQuery.setParameter("onDate", onDate.atStartOfDay());
|
|
|
1151 |
typedQuery.setParameter("partnerTypes", Arrays.asList(partnerType, partnerType.ALL));
|
|
|
1152 |
typedQuery.setFirstResult(offset);
|
|
|
1153 |
if (limit != 0) {
|
|
|
1154 |
typedQuery.setMaxResults(limit);
|
|
|
1155 |
}
|
|
|
1156 |
return typedQuery.getResultList();
|
|
|
1157 |
}
|
| 30463 |
amit.gupta |
1158 |
|
| 33432 |
amit.gupta |
1159 |
@Override
|
|
|
1160 |
public void processSchemeIn(List<InventoryItem> inventoryItems) throws ProfitMandiBusinessException {
|
|
|
1161 |
Map<Integer, List<InventoryItem>> purchaseIdInventoryItemsMap = inventoryItems.stream().collect(Collectors.groupingBy(x -> x.getPurchaseId()));
|
|
|
1162 |
for (Map.Entry<Integer, List<InventoryItem>> purchaseIdInventoryItemEntry : purchaseIdInventoryItemsMap.entrySet()) {
|
| 34317 |
amit.gupta |
1163 |
int retailerId = purchaseIdInventoryItemEntry.getValue().get(0).getFofoId();
|
|
|
1164 |
this.processSchemeIn(purchaseIdInventoryItemEntry.getKey(), retailerId);
|
| 33432 |
amit.gupta |
1165 |
}
|
|
|
1166 |
}
|
|
|
1167 |
|
| 32309 |
amit.gupta |
1168 |
@Autowired
|
|
|
1169 |
FofoStoreRepository fofoStoreRepository;
|
|
|
1170 |
|
|
|
1171 |
|
| 31410 |
amit.gupta |
1172 |
@Override
|
| 32309 |
amit.gupta |
1173 |
@Cacheable(value = "staticscheme", cacheManager = "oneDayCacheManager")
|
|
|
1174 |
public Scheme getStaticScheme(int fofoId) throws ProfitMandiBusinessException {
|
| 33165 |
amit.gupta |
1175 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
| 32504 |
amit.gupta |
1176 |
Scheme scheme = null;
|
|
|
1177 |
if (fofoStore.getTarget() > 0) {
|
|
|
1178 |
scheme = new Scheme();
|
| 33704 |
amit.gupta |
1179 |
scheme.setName("Super Retailer - Club 3");
|
|
|
1180 |
scheme.setStartDateTime(LocalDate.of(2024, 8, 1).atStartOfDay());
|
|
|
1181 |
scheme.setEndDateTime(LocalDate.of(2024, 11, 30).atTime(LocalTime.MAX));
|
| 32504 |
amit.gupta |
1182 |
scheme.setTarget(fofoStore.getTarget());
|
| 33548 |
amit.gupta |
1183 |
|
| 33704 |
amit.gupta |
1184 |
if (scheme.getEndDateTime().plusDays(5).isBefore(LocalDateTime.now())) return null;
|
| 32504 |
amit.gupta |
1185 |
}
|
| 33165 |
amit.gupta |
1186 |
return scheme;
|
| 32309 |
amit.gupta |
1187 |
}
|
|
|
1188 |
|
|
|
1189 |
@Override
|
| 31410 |
amit.gupta |
1190 |
public List<Scheme> selectSchemeByPartnerType(PartnerType partnerType, LocalDate onDate, int catalogId,
|
|
|
1191 |
boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
|
|
|
1192 |
Session session = sessionFactory.getCurrentSession();
|
|
|
1193 |
List<Predicate> andPredicates = new ArrayList<>();
|
|
|
1194 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
1195 |
CriteriaQuery<Scheme> query = cb.createQuery(Scheme.class);
|
|
|
1196 |
Root<Scheme> scheme = query.from(Scheme.class);
|
|
|
1197 |
if (!partnerType.equals(PartnerType.ALL)) {
|
|
|
1198 |
List<PartnerType> pt = new ArrayList<>();
|
|
|
1199 |
pt.add(PartnerType.ALL);
|
|
|
1200 |
pt.add(partnerType);
|
|
|
1201 |
andPredicates.add(cb.in(scheme.get("partnerType")).value(pt));
|
|
|
1202 |
}
|
|
|
1203 |
cb.desc(cb.isNull(scheme.get("expireTimestamp")));
|
|
|
1204 |
if (catalogId > 0) {
|
| 30454 |
amit.gupta |
1205 |
|
| 31410 |
amit.gupta |
1206 |
List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByCatalogId(catalogId);
|
|
|
1207 |
LOGGER.info("schemeId" + schemeIds);
|
|
|
1208 |
if (schemeIds.isEmpty()) {
|
|
|
1209 |
return new ArrayList<>();
|
|
|
1210 |
}
|
|
|
1211 |
andPredicates.add(cb.in(scheme.get("id")).value(schemeIds));
|
|
|
1212 |
if (onDate != null) {
|
|
|
1213 |
andPredicates.add(cb.greaterThan(scheme.get("endDateTime"), onDate.atStartOfDay()));
|
|
|
1214 |
andPredicates.add(cb.lessThanOrEqualTo(scheme.get("startDateTime"), onDate.atStartOfDay()));
|
|
|
1215 |
}
|
|
|
1216 |
}
|
|
|
1217 |
if (!isAdmin) {
|
|
|
1218 |
andPredicates.add(cb.isNotNull(scheme.get("activeTimestamp")));
|
|
|
1219 |
}
|
|
|
1220 |
query.where(cb.and(andPredicates.toArray(new Predicate[0])));
|
|
|
1221 |
query.orderBy(cb.desc(cb.function("isnull", Boolean.class, scheme.get("expireTimestamp"))));
|
|
|
1222 |
if (limit == 0) {
|
|
|
1223 |
return session.createQuery(query).setFirstResult(offset).getResultList();
|
|
|
1224 |
}
|
|
|
1225 |
return session.createQuery(query).setFirstResult(offset).setMaxResults(limit).getResultList();
|
| 30454 |
amit.gupta |
1226 |
|
| 31410 |
amit.gupta |
1227 |
}
|
| 30454 |
amit.gupta |
1228 |
|
| 31410 |
amit.gupta |
1229 |
@Override
|
|
|
1230 |
public List<Scheme> selectPercentageScheme(PartnerType partnerType, LocalDate onDate, int catalogId,
|
|
|
1231 |
boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
|
|
|
1232 |
List<Scheme> schemes = this.selectSchemeByPartnerType(partnerType, onDate, catalogId, isAdmin, offset, limit);
|
|
|
1233 |
return schemes.stream().filter(x -> x.getAmountType().equals(AmountType.PERCENTAGE)).collect(Collectors.toList());
|
|
|
1234 |
}
|
| 30454 |
amit.gupta |
1235 |
|
| 31410 |
amit.gupta |
1236 |
@Override
|
|
|
1237 |
public void processActivation() throws ProfitMandiBusinessException {
|
|
|
1238 |
List<SchemeInOut> pendingPayouts = schemeInOutRepository.selectAllPending();
|
|
|
1239 |
List<Integer> schemeIds = new ArrayList<>();
|
|
|
1240 |
Set<Integer> inventoryIds = new HashSet<>();
|
|
|
1241 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
|
|
1242 |
schemeIds.add(pendingPayout.getSchemeId());
|
|
|
1243 |
}
|
|
|
1244 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, 0).stream()
|
| 34317 |
amit.gupta |
1245 |
.filter(x -> x.getType().equals(SchemeType.SPECIAL_SUPPORT))
|
| 31410 |
amit.gupta |
1246 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1247 |
pendingPayouts = pendingPayouts.stream().filter(x -> schemesMap.get(x.getSchemeId()) != null)
|
|
|
1248 |
.collect(Collectors.toList());
|
| 23884 |
amit.gupta |
1249 |
|
| 31410 |
amit.gupta |
1250 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
|
|
1251 |
inventoryIds.add(pendingPayout.getInventoryItemId());
|
|
|
1252 |
}
|
|
|
1253 |
Map<Integer, InventoryItem> inventoryItemMap = inventoryItemRepository.selectByIds(inventoryIds).stream()
|
|
|
1254 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1255 |
Map<String, InventoryItem> serialNumberMap = inventoryItemMap.values().stream()
|
|
|
1256 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x));
|
| 23796 |
amit.gupta |
1257 |
|
| 31410 |
amit.gupta |
1258 |
List<ActivatedImei> activatedImeis = activatedImeiRepository
|
|
|
1259 |
.selectBySerialNumbers(new ArrayList<>(serialNumberMap.keySet())).stream().collect(Collectors.toList());
|
| 23508 |
amit.gupta |
1260 |
|
| 31410 |
amit.gupta |
1261 |
Map<String, ActivatedImei> activatedImeiMap = activatedImeis.stream()
|
|
|
1262 |
.collect(Collectors.toMap(x -> x.getSerialNumber().toLowerCase(), x -> x));
|
|
|
1263 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
| 31914 |
amit.gupta |
1264 |
Scheme scheme = schemesMap.get(pendingPayout.getSchemeId());
|
| 31410 |
amit.gupta |
1265 |
InventoryItem ii = inventoryItemMap.get(pendingPayout.getInventoryItemId());
|
|
|
1266 |
String serialNumber = ii.getSerialNumber().toLowerCase();
|
|
|
1267 |
ActivatedImei activatedImei = activatedImeiMap.get(serialNumber);
|
|
|
1268 |
if (activatedImei == null) {
|
|
|
1269 |
continue;
|
|
|
1270 |
}
|
|
|
1271 |
if (scheme.isWithinRange(activatedImei.getActivationTimestamp())) {
|
|
|
1272 |
int fofoId = ii.getFofoId();
|
|
|
1273 |
// Get latest order Id
|
|
|
1274 |
int orderId = scanRecordRepository.selectByInventoryItemId(ii.getId()).stream()
|
|
|
1275 |
.filter(x -> x.getOrderId() > 0)
|
|
|
1276 |
.sorted(Comparator.comparing(ScanRecord::getCreateTimestamp).reversed()).findFirst().get()
|
|
|
1277 |
.getOrderId();
|
|
|
1278 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(orderId);
|
| 31916 |
amit.gupta |
1279 |
|
| 31921 |
amit.gupta |
1280 |
InventoryPayoutModel inventoryPayoutModel = priceCircularService.getPayouts(ii);
|
| 31916 |
amit.gupta |
1281 |
AmountModel amountModel = new AmountModel();
|
|
|
1282 |
amountModel.setAmount(scheme.getAmount());
|
|
|
1283 |
amountModel.setAmountType(scheme.getAmountType());
|
| 31921 |
amit.gupta |
1284 |
double amountToRollout = inventoryPayoutModel.getRolloutAmount(amountModel);
|
| 32058 |
amit.gupta |
1285 |
pendingPayout.setAmount((float) amountToRollout);
|
| 34317 |
amit.gupta |
1286 |
walletService.addAmountToWallet(fofoId, orderId, WalletReferenceType.SPECIAL_SUPPORT,
|
|
|
1287 |
"Special Support for " + ii.getItem().getItemDescriptionNoColor() + ", Imei - " + serialNumber, (float) amountToRollout,
|
|
|
1288 |
fofoOrder.getCreateTimestamp());
|
|
|
1289 |
pendingPayout.setStatusDescription("Special support credited, activated on " + FormattingUtils.formatDate(activatedImei.getActivationTimestamp()));
|
|
|
1290 |
|
| 31410 |
amit.gupta |
1291 |
pendingPayout.setCreditTimestamp(LocalDateTime.now());
|
|
|
1292 |
pendingPayout.setStatus(SchemePayoutStatus.CREDITED);
|
|
|
1293 |
} else {
|
|
|
1294 |
pendingPayout.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
1295 |
pendingPayout.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
1296 |
pendingPayout.setStatusDescription(
|
|
|
1297 |
"Rejected, activated on " + FormattingUtils.formatDate(activatedImei.getActivationTimestamp()));
|
|
|
1298 |
}
|
|
|
1299 |
}
|
|
|
1300 |
}
|
| 24976 |
amit.gupta |
1301 |
|
| 31410 |
amit.gupta |
1302 |
@Override
|
|
|
1303 |
public void processActivatedImeisForSchemes() throws ProfitMandiBusinessException {
|
|
|
1304 |
List<SchemesImeisModel> schemesImeisModels = schemeRepository.selectSelectUnpaidSchemes();
|
|
|
1305 |
LOGGER.info("Total Size - " + schemesImeisModels.size());
|
|
|
1306 |
List<Integer> orderIds = schemesImeisModels.stream().map(x -> x.getOrderId()).collect(Collectors.toList());
|
|
|
1307 |
List<FofoOrder> fofoOrders = fofoOrderRepository.selectAllByOrderIds(orderIds);
|
|
|
1308 |
Map<Integer, FofoOrder> validOrdersMap = fofoOrders.stream().filter(x -> x.getCancelledTimestamp() == null).collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1309 |
Map<String, List<SchemesImeisModel>> validImeiSchemesModelMap = schemesImeisModels.stream().filter(x -> validOrdersMap.containsKey(x.getOrderId())).collect(Collectors.groupingBy(x -> x.getImei()));
|
|
|
1310 |
for (Map.Entry<String, List<SchemesImeisModel>> imeiListEntry : validImeiSchemesModelMap.entrySet()) {
|
|
|
1311 |
SchemesImeisModel schemesImeisModel = imeiListEntry.getValue().get(0);
|
|
|
1312 |
List<Integer> schemeIds = imeiListEntry.getValue().stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
1313 |
LOGGER.info("Serial Number - {}, Scheme IDs - {}", schemesImeisModel.getImei(), schemeIds);
|
|
|
1314 |
InventoryItem inventoryItem = inventoryItemRepository.selectById(schemesImeisModel.getInventoryItemId());
|
|
|
1315 |
List<Scheme> schemes = schemeRepository.selectBySchemeIds(schemeIds);
|
| 34317 |
amit.gupta |
1316 |
List<Scheme> supportSchemes = schemes.stream().filter(x -> Arrays.asList(SchemeType.SPECIAL_SUPPORT).contains(x.getType())).collect(Collectors.toList());
|
| 31410 |
amit.gupta |
1317 |
if (supportSchemes.size() > 0) {
|
| 32165 |
amit.gupta |
1318 |
for (Scheme scheme : supportSchemes) {
|
|
|
1319 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByScheme(scheme.getId(), inventoryItem.getId());
|
| 32972 |
amit.gupta |
1320 |
if (schemeInOuts.stream().filter(x -> Arrays.asList(SchemePayoutStatus.CREDITED, SchemePayoutStatus.PENDING).contains(x.getStatus())).count() > 0) {
|
| 32165 |
amit.gupta |
1321 |
continue;
|
|
|
1322 |
}
|
|
|
1323 |
SchemeInOut sio = new SchemeInOut();
|
|
|
1324 |
sio.setAmount(0);
|
|
|
1325 |
sio.setInventoryItemId(inventoryItem.getId());
|
|
|
1326 |
sio.setSchemeId(scheme.getId());
|
|
|
1327 |
sio.setStatusDescription("Activation pending for IMEI#" + inventoryItem.getSerialNumber());
|
|
|
1328 |
sio.setStatus(SchemePayoutStatus.PENDING);
|
|
|
1329 |
schemeInOutRepository.persist(sio);
|
|
|
1330 |
}
|
| 31410 |
amit.gupta |
1331 |
}
|
|
|
1332 |
}
|
|
|
1333 |
}
|
| 22653 |
ashik.ali |
1334 |
}
|