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