| 22653 |
ashik.ali |
1 |
package com.spice.profitmandi.service.scheme;
|
|
|
2 |
|
| 26403 |
amit.gupta |
3 |
import java.text.MessageFormat;
|
| 27877 |
amit.gupta |
4 |
import java.time.LocalDate;
|
| 22859 |
ashik.ali |
5 |
import java.time.LocalDateTime;
|
| 28969 |
amit.gupta |
6 |
import java.time.YearMonth;
|
| 23019 |
ashik.ali |
7 |
import java.util.ArrayList;
|
| 26722 |
amit.gupta |
8 |
import java.util.Arrays;
|
| 27395 |
amit.gupta |
9 |
import java.util.Comparator;
|
| 22653 |
ashik.ali |
10 |
import java.util.HashMap;
|
| 22859 |
ashik.ali |
11 |
import java.util.HashSet;
|
| 22653 |
ashik.ali |
12 |
import java.util.List;
|
|
|
13 |
import java.util.Map;
|
|
|
14 |
import java.util.Set;
|
| 23968 |
amit.gupta |
15 |
import java.util.stream.Collectors;
|
| 22653 |
ashik.ali |
16 |
|
| 26332 |
amit.gupta |
17 |
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
18 |
import javax.persistence.criteria.CriteriaQuery;
|
|
|
19 |
import javax.persistence.criteria.Predicate;
|
|
|
20 |
import javax.persistence.criteria.Root;
|
|
|
21 |
|
| 23781 |
ashik.ali |
22 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
23 |
import org.apache.logging.log4j.Logger;
|
| 26332 |
amit.gupta |
24 |
import org.hibernate.Session;
|
|
|
25 |
import org.hibernate.SessionFactory;
|
|
|
26 |
import org.hibernate.query.Query;
|
| 22653 |
ashik.ali |
27 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 23781 |
ashik.ali |
28 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 26684 |
amit.gupta |
29 |
import org.springframework.cache.annotation.Cacheable;
|
| 22653 |
ashik.ali |
30 |
import org.springframework.stereotype.Component;
|
|
|
31 |
|
| 24307 |
amit.gupta |
32 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
| 22653 |
ashik.ali |
33 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
34 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 23019 |
ashik.ali |
35 |
import com.spice.profitmandi.common.model.SchemeModel;
|
| 29584 |
manish |
36 |
import com.spice.profitmandi.common.services.ReporticoService;
|
| 27395 |
amit.gupta |
37 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
| 22859 |
ashik.ali |
38 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 23339 |
ashik.ali |
39 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 22653 |
ashik.ali |
40 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
| 27395 |
amit.gupta |
41 |
import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
|
| 23365 |
ashik.ali |
42 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 22653 |
ashik.ali |
43 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 25503 |
amit.gupta |
44 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
| 23339 |
ashik.ali |
45 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 25043 |
amit.gupta |
46 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
| 22859 |
ashik.ali |
47 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
|
|
48 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
| 26498 |
amit.gupta |
49 |
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
|
| 23527 |
ashik.ali |
50 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 22653 |
ashik.ali |
51 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 25111 |
amit.gupta |
52 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| 27377 |
amit.gupta |
53 |
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
|
| 25503 |
amit.gupta |
54 |
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
|
| 22859 |
ashik.ali |
55 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 22653 |
ashik.ali |
56 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 23995 |
amit.gupta |
57 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 23968 |
amit.gupta |
58 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 27395 |
amit.gupta |
59 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
| 23019 |
ashik.ali |
60 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
|
|
61 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
| 23365 |
ashik.ali |
62 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| 22653 |
ashik.ali |
63 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 29584 |
manish |
64 |
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
|
| 25503 |
amit.gupta |
65 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
|
| 23339 |
ashik.ali |
66 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 25043 |
amit.gupta |
67 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
| 22859 |
ashik.ali |
68 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
|
|
69 |
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
|
| 26498 |
amit.gupta |
70 |
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
|
| 23798 |
amit.gupta |
71 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| 26722 |
amit.gupta |
72 |
import com.spice.profitmandi.service.inventory.PurchaseService;
|
| 22859 |
ashik.ali |
73 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 22653 |
ashik.ali |
74 |
|
| 22859 |
ashik.ali |
75 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
76 |
|
| 22653 |
ashik.ali |
77 |
@Component
|
|
|
78 |
public class SchemeServiceImpl implements SchemeService {
|
|
|
79 |
|
| 23568 |
govind |
80 |
private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
|
| 23444 |
amit.gupta |
81 |
|
| 22653 |
ashik.ali |
82 |
@Autowired
|
| 23781 |
ashik.ali |
83 |
@Qualifier("fofoInventoryItemRepository")
|
| 22653 |
ashik.ali |
84 |
private InventoryItemRepository inventoryItemRepository;
|
| 23444 |
amit.gupta |
85 |
|
| 22653 |
ashik.ali |
86 |
@Autowired
|
| 27395 |
amit.gupta |
87 |
private ActivatedImeiRepository activatedImeiRepository;
|
| 27898 |
amit.gupta |
88 |
|
| 27395 |
amit.gupta |
89 |
@Autowired
|
| 25503 |
amit.gupta |
90 |
private PartnerTypeChangeService partnerTypeChangeService;
|
|
|
91 |
|
|
|
92 |
@Autowired
|
| 26722 |
amit.gupta |
93 |
private PurchaseService purchaseService;
|
|
|
94 |
|
|
|
95 |
@Autowired
|
| 25043 |
amit.gupta |
96 |
private ScanRecordRepository scanRecordRepository;
|
| 29584 |
manish |
97 |
|
|
|
98 |
@Autowired
|
|
|
99 |
private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
|
|
|
100 |
|
|
|
101 |
|
| 23444 |
amit.gupta |
102 |
|
| 22653 |
ashik.ali |
103 |
@Autowired
|
| 26332 |
amit.gupta |
104 |
private SessionFactory sessionFactory;
|
|
|
105 |
|
| 26722 |
amit.gupta |
106 |
private Set<Integer> tagIds = new HashSet<Integer>(Arrays.asList(4));
|
|
|
107 |
|
| 26332 |
amit.gupta |
108 |
@Autowired
|
| 22653 |
ashik.ali |
109 |
private SchemeRepository schemeRepository;
|
| 26684 |
amit.gupta |
110 |
|
| 26498 |
amit.gupta |
111 |
@Autowired
|
|
|
112 |
private PriceDropRepository priceDropRepository;
|
| 23444 |
amit.gupta |
113 |
|
| 22653 |
ashik.ali |
114 |
@Autowired
|
| 23798 |
amit.gupta |
115 |
private RoleManager roleManager;
|
|
|
116 |
|
|
|
117 |
@Autowired
|
| 22859 |
ashik.ali |
118 |
private RetailerRepository retailerRepository;
|
| 29584 |
manish |
119 |
|
|
|
120 |
@Autowired
|
|
|
121 |
private ReporticoService reporticoService;
|
| 24562 |
amit.gupta |
122 |
|
| 29584 |
manish |
123 |
|
| 23995 |
amit.gupta |
124 |
@Autowired
|
|
|
125 |
private TagListingRepository tagListingRepository;
|
| 23444 |
amit.gupta |
126 |
|
| 22859 |
ashik.ali |
127 |
@Autowired
|
|
|
128 |
private SchemeInOutRepository schemeInOutRepository;
|
| 23444 |
amit.gupta |
129 |
|
| 22653 |
ashik.ali |
130 |
@Autowired
|
| 23781 |
ashik.ali |
131 |
@Qualifier("catalogItemRepository")
|
| 22859 |
ashik.ali |
132 |
private ItemRepository itemRepository;
|
| 23444 |
amit.gupta |
133 |
|
| 22859 |
ashik.ali |
134 |
@Autowired
|
|
|
135 |
private SchemeItemRepository schemeItemRepository;
|
| 23444 |
amit.gupta |
136 |
|
| 22859 |
ashik.ali |
137 |
@Autowired
|
|
|
138 |
private WalletService walletService;
|
| 23444 |
amit.gupta |
139 |
|
| 23019 |
ashik.ali |
140 |
@Autowired
|
|
|
141 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
| 23444 |
amit.gupta |
142 |
|
| 23019 |
ashik.ali |
143 |
@Autowired
|
|
|
144 |
private FofoLineItemRepository fofoLineItemRepository;
|
| 23444 |
amit.gupta |
145 |
|
| 23339 |
ashik.ali |
146 |
@Autowired
|
|
|
147 |
private PurchaseRepository purchaseRepository;
|
| 23444 |
amit.gupta |
148 |
|
| 23344 |
ashik.ali |
149 |
@Autowired
|
| 23365 |
ashik.ali |
150 |
private FofoOrderRepository fofoOrderRepository;
|
| 23796 |
amit.gupta |
151 |
|
| 22653 |
ashik.ali |
152 |
@Override
|
| 22859 |
ashik.ali |
153 |
public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
| 23444 |
amit.gupta |
154 |
|
| 23019 |
ashik.ali |
155 |
this.validateCreateSchemeRequest(createSchemeRequest);
|
| 23444 |
amit.gupta |
156 |
|
| 22859 |
ashik.ali |
157 |
Scheme scheme = this.toScheme(creatorId, createSchemeRequest);
|
| 23444 |
amit.gupta |
158 |
|
|
|
159 |
if (scheme.getStartDateTime().isAfter(scheme.getEndDateTime())) {
|
|
|
160 |
throw new ProfitMandiBusinessException(
|
|
|
161 |
ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE,
|
|
|
162 |
scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
|
| 22653 |
ashik.ali |
163 |
}
|
| 23444 |
amit.gupta |
164 |
|
| 26332 |
amit.gupta |
165 |
// this.validateItemIds(createSchemeRequest);
|
| 22859 |
ashik.ali |
166 |
schemeRepository.persist(scheme);
|
| 23444 |
amit.gupta |
167 |
for (int itemId : createSchemeRequest.getItemIds()) {
|
| 22859 |
ashik.ali |
168 |
SchemeItem schemeItem = new SchemeItem();
|
|
|
169 |
schemeItem.setSchemeId(scheme.getId());
|
|
|
170 |
schemeItem.setItemId(itemId);
|
|
|
171 |
schemeItemRepository.persist(schemeItem);
|
|
|
172 |
}
|
| 23444 |
amit.gupta |
173 |
|
| 22653 |
ashik.ali |
174 |
}
|
| 23444 |
amit.gupta |
175 |
|
|
|
176 |
private void validateCreateSchemeRequest(CreateSchemeRequest createSchemeRequest)
|
|
|
177 |
throws ProfitMandiBusinessException {
|
|
|
178 |
if (createSchemeRequest.getName() == null || createSchemeRequest.getName().isEmpty()) {
|
|
|
179 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, createSchemeRequest.getName(),
|
|
|
180 |
"SCHM_VE_1000");
|
| 23019 |
ashik.ali |
181 |
}
|
| 23444 |
amit.gupta |
182 |
if (createSchemeRequest.getAmount() <= 0) {
|
|
|
183 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
184 |
"SCHM_VE_1001");
|
| 23019 |
ashik.ali |
185 |
}
|
| 23444 |
amit.gupta |
186 |
|
| 25503 |
amit.gupta |
187 |
if (createSchemeRequest.getAmountType().equals(AmountType.PERCENTAGE)
|
| 23444 |
amit.gupta |
188 |
&& createSchemeRequest.getAmount() > 100) {
|
|
|
189 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
190 |
"SCHM_VE_1002");
|
| 23019 |
ashik.ali |
191 |
}
|
| 23444 |
amit.gupta |
192 |
|
| 23886 |
amit.gupta |
193 |
if (createSchemeRequest.getStartDate() == null) {
|
| 23983 |
amit.gupta |
194 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, createSchemeRequest.getStartDate(),
|
|
|
195 |
"SCHM_VE_1003");
|
| 23019 |
ashik.ali |
196 |
}
|
| 23886 |
amit.gupta |
197 |
if (createSchemeRequest.getEndDate() == null) {
|
| 23983 |
amit.gupta |
198 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, createSchemeRequest.getEndDate(),
|
|
|
199 |
"SCHM_VE_1004");
|
| 23019 |
ashik.ali |
200 |
}
|
|
|
201 |
}
|
| 23444 |
amit.gupta |
202 |
|
|
|
203 |
private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest) {
|
| 22859 |
ashik.ali |
204 |
Scheme scheme = new Scheme();
|
|
|
205 |
scheme.setName(createSchemeRequest.getName());
|
|
|
206 |
scheme.setDescription(createSchemeRequest.getDescription());
|
| 25503 |
amit.gupta |
207 |
scheme.setType(createSchemeRequest.getType());
|
|
|
208 |
scheme.setAmountType(createSchemeRequest.getAmountType());
|
| 22859 |
ashik.ali |
209 |
scheme.setAmount(createSchemeRequest.getAmount());
|
| 25503 |
amit.gupta |
210 |
scheme.setPartnerType(createSchemeRequest.getPartnerType());
|
| 25517 |
amit.gupta |
211 |
scheme.setStartDateTime(createSchemeRequest.getStartDate());
|
| 23886 |
amit.gupta |
212 |
scheme.setEndDateTime(createSchemeRequest.getEndDate());
|
| 22859 |
ashik.ali |
213 |
scheme.setCreatedBy(creatorId);
|
| 28464 |
amit.gupta |
214 |
scheme.setCashback(createSchemeRequest.isCashback());
|
| 22859 |
ashik.ali |
215 |
return scheme;
|
|
|
216 |
}
|
| 29584 |
manish |
217 |
|
|
|
218 |
|
| 23444 |
amit.gupta |
219 |
|
|
|
220 |
private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
|
|
221 |
if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
|
|
|
222 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
223 |
"SCHM_1003");
|
| 22859 |
ashik.ali |
224 |
}
|
| 23444 |
amit.gupta |
225 |
List<Integer> foundItemIds = itemRepository.selectIdsByIdsAndType(createSchemeRequest.getItemIds(),
|
|
|
226 |
ItemType.SERIALIZED);
|
|
|
227 |
if (foundItemIds.size() != createSchemeRequest.getItemIds().size()) {
|
| 22859 |
ashik.ali |
228 |
createSchemeRequest.getItemIds().removeAll(foundItemIds);
|
| 23444 |
amit.gupta |
229 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
230 |
"SCHM_1004");
|
| 22859 |
ashik.ali |
231 |
}
|
|
|
232 |
}
|
| 23444 |
amit.gupta |
233 |
|
| 22859 |
ashik.ali |
234 |
@Override
|
|
|
235 |
public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
236 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
|
|
237 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23914 |
govind |
238 |
if (itemIds.size() > 0) {
|
|
|
239 |
List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
|
|
|
240 |
scheme.setItemStringMap(this.toItemStringMap(items));
|
| 23983 |
amit.gupta |
241 |
}
|
| 22859 |
ashik.ali |
242 |
return scheme;
|
|
|
243 |
}
|
| 23444 |
amit.gupta |
244 |
|
|
|
245 |
public Map<Integer, String> toItemStringMap(List<Item> items) {
|
| 23339 |
ashik.ali |
246 |
Map<Integer, String> itemMap = new HashMap<>();
|
| 23444 |
amit.gupta |
247 |
for (Item item : items) {
|
| 23339 |
ashik.ali |
248 |
itemMap.put(item.getId(), this.getItemString(item));
|
|
|
249 |
}
|
|
|
250 |
return itemMap;
|
|
|
251 |
}
|
| 23444 |
amit.gupta |
252 |
|
|
|
253 |
public String getItemString(Item item) {
|
| 23339 |
ashik.ali |
254 |
StringBuilder itemString = new StringBuilder();
|
| 23444 |
amit.gupta |
255 |
if (item.getBrand() != null && !item.getBrand().isEmpty()) {
|
| 23339 |
ashik.ali |
256 |
itemString.append(item.getBrand().trim());
|
|
|
257 |
}
|
|
|
258 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
259 |
if (item.getModelName() != null && !item.getModelName().isEmpty()) {
|
| 23339 |
ashik.ali |
260 |
itemString.append(item.getModelName().trim());
|
|
|
261 |
}
|
|
|
262 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
263 |
if (item.getModelNumber() != null && !item.getModelNumber().isEmpty()) {
|
| 23339 |
ashik.ali |
264 |
itemString.append(item.getModelNumber().trim());
|
|
|
265 |
}
|
|
|
266 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
267 |
if (item.getColor() != null && !item.getColor().isEmpty()) {
|
| 23339 |
ashik.ali |
268 |
itemString.append(item.getColor().trim());
|
|
|
269 |
}
|
|
|
270 |
return itemString.toString();
|
|
|
271 |
}
|
| 23444 |
amit.gupta |
272 |
|
|
|
273 |
private Set<Integer> schemeItemsToItemIds(List<SchemeItem> schemeItems) {
|
| 23339 |
ashik.ali |
274 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
275 |
for (SchemeItem schemeItem : schemeItems) {
|
| 23339 |
ashik.ali |
276 |
itemIds.add(schemeItem.getItemId());
|
|
|
277 |
}
|
|
|
278 |
return itemIds;
|
|
|
279 |
}
|
| 23444 |
amit.gupta |
280 |
|
| 23019 |
ashik.ali |
281 |
@Override
|
|
|
282 |
public List<SchemeModel> getAllSchemeModels(LocalDateTime startDateTime, LocalDateTime endDateTime) {
|
|
|
283 |
List<Scheme> schemes = schemeRepository.selectAllBetweenCreateTimestamp(startDateTime, endDateTime);
|
|
|
284 |
Map<Integer, Scheme> schemeIdSchemeMap = this.toSchemeIdSchemeMap(schemes);
|
|
|
285 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
| 23339 |
ashik.ali |
286 |
Set<Integer> itemIds = this.schemeItemsToItemIds(schemeItems);
|
|
|
287 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
|
|
288 |
Map<Integer, String> itemStringMap = this.toItemStringMap(items);
|
|
|
289 |
this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
|
| 23019 |
ashik.ali |
290 |
return this.toSchemeModels(schemeIdSchemeMap);
|
|
|
291 |
}
|
| 23444 |
amit.gupta |
292 |
|
|
|
293 |
private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
|
|
|
294 |
Map<Integer, String> itemStringMap) {
|
|
|
295 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
296 |
schemeIdSchemeMap.get(schemeItem.getSchemeId()).getItemStringMap().put(schemeItem.getItemId(),
|
|
|
297 |
itemStringMap.get(schemeItem.getItemId()));
|
| 23019 |
ashik.ali |
298 |
}
|
|
|
299 |
}
|
| 23444 |
amit.gupta |
300 |
|
|
|
301 |
private List<SchemeModel> toSchemeModels(Map<Integer, Scheme> schemeIdSchemeMap) {
|
| 23019 |
ashik.ali |
302 |
List<SchemeModel> schemeModels = new ArrayList<>();
|
| 23444 |
amit.gupta |
303 |
for (Map.Entry<Integer, Scheme> schemeIdSchemeEntry : schemeIdSchemeMap.entrySet()) {
|
| 23019 |
ashik.ali |
304 |
schemeModels.add(this.toSchemeModel(schemeIdSchemeEntry.getValue()));
|
|
|
305 |
}
|
|
|
306 |
return schemeModels;
|
|
|
307 |
}
|
| 23444 |
amit.gupta |
308 |
|
|
|
309 |
private SchemeModel toSchemeModel(Scheme scheme) {
|
| 23019 |
ashik.ali |
310 |
SchemeModel schemeModel = new SchemeModel();
|
|
|
311 |
schemeModel.setSchemeId(scheme.getId());
|
|
|
312 |
schemeModel.setName(scheme.getName());
|
|
|
313 |
schemeModel.setDescription(scheme.getDescription());
|
|
|
314 |
schemeModel.setSchemeType(scheme.getType().toString());
|
|
|
315 |
schemeModel.setAmountType(scheme.getAmountType().toString());
|
|
|
316 |
schemeModel.setAmount(scheme.getAmount());
|
|
|
317 |
schemeModel.setStartDateTime(StringUtils.toString(scheme.getStartDateTime()));
|
|
|
318 |
schemeModel.setEndDateTime(StringUtils.toString(scheme.getEndDateTime()));
|
|
|
319 |
schemeModel.setCreateTimestamp(StringUtils.toString(scheme.getCreateTimestamp()));
|
|
|
320 |
schemeModel.setActiveTimestamp(StringUtils.toString(scheme.getActiveTimestamp()));
|
|
|
321 |
schemeModel.setExpireTimestamp(StringUtils.toString(scheme.getExpireTimestamp()));
|
|
|
322 |
schemeModel.setCreatedBy(scheme.getCreatedBy());
|
| 23339 |
ashik.ali |
323 |
schemeModel.setItemStringMap(scheme.getItemStringMap());
|
| 23019 |
ashik.ali |
324 |
schemeModel.setRetailerIds(scheme.getRetailerIds());
|
|
|
325 |
return schemeModel;
|
|
|
326 |
}
|
| 22653 |
ashik.ali |
327 |
|
|
|
328 |
@Override
|
|
|
329 |
public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
330 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 23444 |
amit.gupta |
331 |
if (scheme.getActiveTimestamp() != null) {
|
|
|
332 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
333 |
"SCHM_1005");
|
| 22653 |
ashik.ali |
334 |
}
|
| 23444 |
amit.gupta |
335 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
336 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
337 |
"SCHM_1006");
|
| 22653 |
ashik.ali |
338 |
}
|
| 22859 |
ashik.ali |
339 |
scheme.setActiveTimestamp(LocalDateTime.now());
|
|
|
340 |
schemeRepository.persist(scheme);
|
| 25438 |
amit.gupta |
341 |
/*
|
|
|
342 |
* if (scheme.getType() == SchemeType.IN) {
|
|
|
343 |
* this.processPreviousPurchases(scheme); } else if (scheme.getType() ==
|
|
|
344 |
* SchemeType.OUT) { this.processPreviousSales(scheme); }
|
|
|
345 |
*/
|
| 22653 |
ashik.ali |
346 |
}
|
| 23444 |
amit.gupta |
347 |
|
| 22653 |
ashik.ali |
348 |
@Override
|
| 25069 |
amit.gupta |
349 |
public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
|
| 22653 |
ashik.ali |
350 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 25111 |
amit.gupta |
351 |
if (scheme == null || scheme.getActiveTimestamp() == null) {
|
| 23444 |
amit.gupta |
352 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
353 |
"SCHM_1007");
|
| 22653 |
ashik.ali |
354 |
}
|
| 23444 |
amit.gupta |
355 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
356 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
357 |
"SCHM_1008");
|
| 22653 |
ashik.ali |
358 |
}
|
| 22859 |
ashik.ali |
359 |
scheme.setExpireTimestamp(LocalDateTime.now());
|
| 29231 |
amit.gupta |
360 |
if (expiryTime.isAfter(scheme.getEndDateTime())) {
|
| 28749 |
amit.gupta |
361 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
362 |
"End Date cant be extended during expiry");
|
|
|
363 |
}
|
| 25069 |
amit.gupta |
364 |
scheme.setEndDateTime(expiryTime);
|
| 22859 |
ashik.ali |
365 |
schemeRepository.persist(scheme);
|
| 22653 |
ashik.ali |
366 |
}
|
| 23444 |
amit.gupta |
367 |
|
|
|
368 |
private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
369 |
Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
|
| 23444 |
amit.gupta |
370 |
for (Scheme scheme : schemes) {
|
| 23019 |
ashik.ali |
371 |
schemeIdSchemeMap.put(scheme.getId(), scheme);
|
| 22859 |
ashik.ali |
372 |
}
|
| 23019 |
ashik.ali |
373 |
return schemeIdSchemeMap;
|
| 22859 |
ashik.ali |
374 |
}
|
| 23444 |
amit.gupta |
375 |
|
|
|
376 |
private Set<Integer> inventoryItemsToItemIds(List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
377 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
378 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 22859 |
ashik.ali |
379 |
itemIds.add(inventoryItem.getItemId());
|
|
|
380 |
}
|
|
|
381 |
return itemIds;
|
|
|
382 |
}
|
| 23444 |
amit.gupta |
383 |
|
|
|
384 |
private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
385 |
Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemeMap(schemes);
|
| 22859 |
ashik.ali |
386 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
|
| 23444 |
amit.gupta |
387 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
388 |
if (!itemIdSchemesMap.containsKey(schemeItem.getItemId())) {
|
| 22859 |
ashik.ali |
389 |
Set<Scheme> schemesSet = new HashSet<>();
|
|
|
390 |
schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
391 |
itemIdSchemesMap.put(schemeItem.getItemId(), schemesSet);
|
| 23444 |
amit.gupta |
392 |
} else {
|
| 22859 |
ashik.ali |
393 |
itemIdSchemesMap.get(schemeItem.getItemId()).add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
394 |
}
|
|
|
395 |
}
|
|
|
396 |
return itemIdSchemesMap;
|
|
|
397 |
}
|
| 22653 |
ashik.ali |
398 |
|
| 23444 |
amit.gupta |
399 |
private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
|
|
|
400 |
List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
401 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
402 |
for (Scheme scheme : schemes) {
|
| 22859 |
ashik.ali |
403 |
schemeIds.add(scheme.getId());
|
|
|
404 |
}
|
|
|
405 |
Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
|
|
|
406 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
|
| 23444 |
amit.gupta |
407 |
|
| 22859 |
ashik.ali |
408 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
|
|
|
409 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemsMap = new HashMap<>();
|
| 23444 |
amit.gupta |
410 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
411 |
if (itemIdSchemesMap.containsKey(inventoryItem.getItemId())) {
|
| 22859 |
ashik.ali |
412 |
inventoryItemSchemsMap.put(inventoryItem, itemIdSchemesMap.get(inventoryItem.getItemId()));
|
|
|
413 |
}
|
|
|
414 |
}
|
|
|
415 |
return inventoryItemSchemsMap;
|
|
|
416 |
}
|
| 23444 |
amit.gupta |
417 |
|
| 22859 |
ashik.ali |
418 |
@Override
|
| 23365 |
ashik.ali |
419 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
| 23369 |
ashik.ali |
420 |
LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
|
| 23344 |
ashik.ali |
421 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
| 29231 |
amit.gupta |
422 |
// TODO - SCHEME
|
|
|
423 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
|
|
|
424 |
YearMonth.from(purchase.getCreateTimestamp()));
|
|
|
425 |
// PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId,
|
|
|
426 |
// purchase.getCreateTimestamp().toLocalDate());
|
|
|
427 |
|
|
|
428 |
List<Scheme> schemes = schemeRepository.selectActiveAll(Arrays.asList(SchemeType.IN), partnerType,
|
| 27898 |
amit.gupta |
429 |
purchase.getCreateTimestamp(), false);
|
|
|
430 |
float totalCashback = 0;
|
|
|
431 |
if (schemes.isEmpty()) {
|
|
|
432 |
return;
|
|
|
433 |
}
|
|
|
434 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
|
| 26332 |
amit.gupta |
435 |
|
| 27898 |
amit.gupta |
436 |
Set<Integer> itemIds = inventoryItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
437 |
LocalDateTime billingDate = purchaseService.getBillingDateOfPurchase(purchaseId);
|
|
|
438 |
Set<Integer> itemIdsSet = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds).stream()
|
|
|
439 |
.filter(x -> x.getEolDate() == null || x.getEolDate().isAfter(billingDate)).map(x -> x.getItemId())
|
|
|
440 |
.collect(Collectors.toSet());
|
|
|
441 |
// Only consider inventory items that were not returned
|
|
|
442 |
inventoryItems = inventoryItems.stream().filter(x -> itemIdsSet.contains(x.getItemId()))
|
|
|
443 |
.filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET_BAD))
|
|
|
444 |
.filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET)).collect(Collectors.toList());
|
|
|
445 |
LOGGER.info(inventoryItems);
|
|
|
446 |
if (inventoryItems.size() == 0)
|
|
|
447 |
return;
|
|
|
448 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap = this.toInventoryItemSchemesMap(schemes,
|
|
|
449 |
inventoryItems);
|
| 23444 |
amit.gupta |
450 |
|
| 27898 |
amit.gupta |
451 |
if (inventoryItemSchemesMap.isEmpty()) {
|
|
|
452 |
return;
|
|
|
453 |
}
|
|
|
454 |
Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
|
| 23444 |
amit.gupta |
455 |
|
| 27898 |
amit.gupta |
456 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
|
|
457 |
Set<Scheme> allSchemes = new HashSet<>();
|
|
|
458 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
|
|
459 |
allSchemes.add(scheme);
|
| 22859 |
ashik.ali |
460 |
}
|
| 27898 |
amit.gupta |
461 |
allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
|
|
|
462 |
}
|
| 23444 |
amit.gupta |
463 |
|
| 27898 |
amit.gupta |
464 |
//
|
| 26722 |
amit.gupta |
465 |
|
| 27898 |
amit.gupta |
466 |
int itemsCount = 0;
|
|
|
467 |
for (Map.Entry<InventoryItem, Set<Scheme>> allInventoryItemSchemesEntry : allInventoryItemSchemesMap
|
|
|
468 |
.entrySet()) {
|
|
|
469 |
float inventoryItemCashback = 0;
|
|
|
470 |
for (Scheme scheme : allInventoryItemSchemesEntry.getValue()) {
|
|
|
471 |
InventoryItem inventoryItem = allInventoryItemSchemesEntry.getKey();
|
|
|
472 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
|
|
473 |
inventoryItemCashback += cashback;
|
| 22859 |
ashik.ali |
474 |
}
|
| 27898 |
amit.gupta |
475 |
if (inventoryItemCashback > 0) {
|
|
|
476 |
totalCashback += inventoryItemCashback;
|
|
|
477 |
itemsCount++;
|
| 23508 |
amit.gupta |
478 |
}
|
| 22653 |
ashik.ali |
479 |
}
|
| 27898 |
amit.gupta |
480 |
|
|
|
481 |
LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
|
|
|
482 |
if (itemsCount > 0) {
|
|
|
483 |
walletService.addAmountToWallet(
|
|
|
484 |
retailerId, purchaseId, WalletReferenceType.SCHEME_IN, "Added for SCHEME IN against invoice "
|
|
|
485 |
+ purchase.getPurchaseReference() + " (total " + itemsCount + " pcs)",
|
|
|
486 |
totalCashback, purchase.getCreateTimestamp());
|
|
|
487 |
LOGGER.info("Added Rs.{} for SCHEME IN against invoice {} total pcs({}) {}", totalCashback,
|
|
|
488 |
purchase.getPurchaseReference(), itemsCount);
|
|
|
489 |
purchase.setCashback(purchase.getCashback() + totalCashback);
|
|
|
490 |
purchaseRepository.persist(purchase);
|
|
|
491 |
}
|
| 22653 |
ashik.ali |
492 |
}
|
| 23444 |
amit.gupta |
493 |
|
|
|
494 |
private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem) {
|
| 29239 |
amit.gupta |
495 |
LOGGER.info("Scheme === {}", scheme);
|
| 29231 |
amit.gupta |
496 |
List<SchemeInOut> sios = schemeInOutRepository.selectAllByType(scheme.getType(), inventoryItem.getId());
|
|
|
497 |
float actualCredit = 0;
|
|
|
498 |
if (sios.stream().filter(x -> x.getRolledBackTimestamp() == null && x.getSchemeId() == scheme.getId())
|
|
|
499 |
.collect(Collectors.toList()).size() == 0) {
|
| 29249 |
amit.gupta |
500 |
sios = sios.stream().filter(x->x.getRolledBackTimestamp()==null).collect(Collectors.toList());
|
|
|
501 |
//Rejected Scheme for types INVESTMENT and ACTIVATION are considered rolledback only if the item billing is cancelled.
|
|
|
502 |
float amountCredited = (float) sios.stream().mapToDouble(e -> e.getAmount()).sum();
|
|
|
503 |
|
| 29243 |
amit.gupta |
504 |
LOGGER.info("SIOS ===== {}", sios);
|
| 29231 |
amit.gupta |
505 |
float amountToCredit = this.getAmount(inventoryItem, scheme);
|
|
|
506 |
|
|
|
507 |
if (!scheme.getType().equals(SchemeType.IN) && sios.size() > 0) {
|
|
|
508 |
|
|
|
509 |
if(sios.size() > 1) {
|
|
|
510 |
LOGGER.info("SAMESCHEMETYPE has already been credited twice for inventoryItem - {}", inventoryItem.getId());
|
|
|
511 |
return 0;
|
|
|
512 |
}
|
|
|
513 |
if(amountToCredit > amountCredited + 1f) {
|
|
|
514 |
for (SchemeInOut sio : sios) {
|
|
|
515 |
sio.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
516 |
sio.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
517 |
sio.setStatusDescription("Partner Category upgraded, new entry added");
|
|
|
518 |
}
|
|
|
519 |
actualCredit = amountToCredit - amountCredited;
|
|
|
520 |
} else {
|
|
|
521 |
return 0;
|
|
|
522 |
}
|
|
|
523 |
} else {
|
|
|
524 |
actualCredit = amountToCredit;
|
|
|
525 |
}
|
| 29243 |
amit.gupta |
526 |
LOGGER.info("Actual Credit ==== {}", actualCredit);
|
| 29231 |
amit.gupta |
527 |
|
| 24562 |
amit.gupta |
528 |
SchemeInOut schemeInOut = new SchemeInOut();
|
| 23443 |
amit.gupta |
529 |
schemeInOut.setSchemeId(scheme.getId());
|
|
|
530 |
schemeInOut.setInventoryItemId(inventoryItem.getId());
|
| 27377 |
amit.gupta |
531 |
schemeInOut.setAmount(amountToCredit);
|
| 27433 |
amit.gupta |
532 |
schemeInOutRepository.persist(schemeInOut);
|
| 29231 |
amit.gupta |
533 |
|
| 27898 |
amit.gupta |
534 |
if (scheme.getType().equals(SchemeType.ACTIVATION)) {
|
| 27377 |
amit.gupta |
535 |
schemeInOut.setStatus(SchemePayoutStatus.PENDING);
|
|
|
536 |
schemeInOut.setStatusDescription("Activation pending for IMEI#" + inventoryItem.getSerialNumber());
|
|
|
537 |
return 0;
|
| 27898 |
amit.gupta |
538 |
} else if (scheme.getType().equals(SchemeType.INVESTMENT)) {
|
|
|
539 |
schemeInOut.setStatus(SchemePayoutStatus.PENDING);
|
|
|
540 |
schemeInOut.setStatusDescription("Subject to investment days maintained");
|
|
|
541 |
return 0;
|
| 27377 |
amit.gupta |
542 |
} else {
|
|
|
543 |
schemeInOut.setStatus(SchemePayoutStatus.CREDITED);
|
| 27898 |
amit.gupta |
544 |
if (scheme.getType().equals(SchemeType.IN)) {
|
| 27713 |
amit.gupta |
545 |
schemeInOut.setStatusDescription("Credited for GRN of IMEI#" + inventoryItem.getSerialNumber());
|
| 29231 |
amit.gupta |
546 |
} else if (SchemeService.OUT_SCHEME_TYPES.contains(scheme.getType())) {
|
| 28737 |
amit.gupta |
547 |
schemeInOut.setStatusDescription("Credited for sale of IMEI#" + inventoryItem.getSerialNumber());
|
| 27713 |
amit.gupta |
548 |
}
|
| 27377 |
amit.gupta |
549 |
schemeInOut.setCreditTimestamp(LocalDateTime.now());
|
|
|
550 |
}
|
| 23443 |
amit.gupta |
551 |
}
|
| 29231 |
amit.gupta |
552 |
return actualCredit;
|
| 22859 |
ashik.ali |
553 |
}
|
| 23444 |
amit.gupta |
554 |
|
| 25049 |
amit.gupta |
555 |
// We are maintaining price drop after grn
|
| 23444 |
amit.gupta |
556 |
private float getAmount(InventoryItem inventoryItem, Scheme scheme) {
|
| 22653 |
ashik.ali |
557 |
float amount = 0;
|
| 25049 |
amit.gupta |
558 |
float dpForCalc = 0;
|
|
|
559 |
float taxableSellingPrice = 0;
|
| 26684 |
amit.gupta |
560 |
// float totalTaxRate = inventoryItem.getIgstRate() +
|
|
|
561 |
// inventoryItem.getSgstRate() + inventoryItem.getCgstRate();
|
|
|
562 |
// Hardcoding it to 18%
|
| 26568 |
amit.gupta |
563 |
float totalTaxRate = 18f;
|
| 26684 |
amit.gupta |
564 |
// float totalTaxRate = inventoryItem.getIgstRate() +
|
|
|
565 |
// inventoryItem.getSgstRate() + inventoryItem.getCgstRate();
|
| 23527 |
ashik.ali |
566 |
if (scheme.getAmountType() == AmountType.PERCENTAGE) {
|
| 24562 |
amit.gupta |
567 |
if (scheme.getType().equals(SchemeType.IN)) {
|
| 25049 |
amit.gupta |
568 |
dpForCalc = inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount();
|
| 23995 |
amit.gupta |
569 |
} else {
|
|
|
570 |
try {
|
| 26684 |
amit.gupta |
571 |
dpForCalc = Math.min(inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount(),
|
| 26550 |
amit.gupta |
572 |
tagListingRepository.selectByItemId(inventoryItem.getItemId()).getSellingPrice());
|
| 24562 |
amit.gupta |
573 |
} catch (Exception e) {
|
| 23995 |
amit.gupta |
574 |
LOGGER.info("Could not find tag Listing entry in {}", inventoryItem.getItemId());
|
|
|
575 |
e.printStackTrace();
|
|
|
576 |
}
|
|
|
577 |
}
|
| 25049 |
amit.gupta |
578 |
taxableSellingPrice = dpForCalc / (1 + totalTaxRate / 100);
|
| 22859 |
ashik.ali |
579 |
amount = taxableSellingPrice * scheme.getAmount() / 100;
|
| 25517 |
amit.gupta |
580 |
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(),
|
| 25050 |
amit.gupta |
581 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
| 26332 |
amit.gupta |
582 |
scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
|
|
|
583 |
scheme.getAmount(), amount));
|
| 23444 |
amit.gupta |
584 |
} else {
|
| 22653 |
ashik.ali |
585 |
amount = scheme.getAmount();
|
| 25517 |
amit.gupta |
586 |
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%d\t%f", inventoryItem.getId(),
|
| 25050 |
amit.gupta |
587 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
| 26332 |
amit.gupta |
588 |
scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
|
|
|
589 |
0, amount));
|
| 22653 |
ashik.ali |
590 |
}
|
| 25049 |
amit.gupta |
591 |
|
| 22859 |
ashik.ali |
592 |
return amount;
|
| 22653 |
ashik.ali |
593 |
}
|
| 23444 |
amit.gupta |
594 |
|
| 22653 |
ashik.ali |
595 |
@Override
|
| 23365 |
ashik.ali |
596 |
public void processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
|
|
|
597 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(retailerId, fofoOrderId);
|
| 29231 |
amit.gupta |
598 |
// Process only if order is not cancelled
|
|
|
599 |
if (fofoOrder.getCancelledTimestamp() == null) {
|
|
|
600 |
// PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId,
|
|
|
601 |
// fofoOrder.getCreateTimestamp().toLocalDate());
|
|
|
602 |
// TODO - SCHEME
|
|
|
603 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnMonth(retailerId,
|
|
|
604 |
YearMonth.from(fofoOrder.getCreateTimestamp()));
|
|
|
605 |
|
| 28969 |
amit.gupta |
606 |
List<ScanRecord> scanRecords = scanRecordRepository.selectAllByOrderId(fofoOrderId);
|
|
|
607 |
Set<Integer> inventoryItemIds = scanRecords.stream().map(x -> x.getInventoryItemId())
|
|
|
608 |
.collect(Collectors.toSet());
|
|
|
609 |
Set<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIds).stream()
|
|
|
610 |
.filter(x -> x.getSerialNumber() != null && !x.getSerialNumber().equals(""))
|
|
|
611 |
.collect(Collectors.toSet());
|
|
|
612 |
if (inventoryItems.size() == 0) {
|
|
|
613 |
return;
|
| 22653 |
ashik.ali |
614 |
}
|
| 28969 |
amit.gupta |
615 |
Set<Integer> itemIds = inventoryItems.stream().map(x -> x.getItemId()).collect(Collectors.toSet());
|
| 29231 |
amit.gupta |
616 |
|
|
|
617 |
// Remove Items that are eol now.
|
| 28969 |
amit.gupta |
618 |
Set<Integer> itemIdsSet = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds).stream()
|
|
|
619 |
.filter(x -> x.getEolDate() == null || x.getEolDate().isAfter(fofoOrder.getCreateTimestamp()))
|
|
|
620 |
.map(x -> x.getItemId()).collect(Collectors.toSet());
|
|
|
621 |
// Only consider inventory items that were not returned
|
|
|
622 |
inventoryItems = inventoryItems.stream().filter(x -> itemIdsSet.contains(x.getItemId()))
|
|
|
623 |
.collect(Collectors.toSet());
|
| 29231 |
amit.gupta |
624 |
|
| 28969 |
amit.gupta |
625 |
if (inventoryItems.size() == 0) {
|
|
|
626 |
return;
|
| 22859 |
ashik.ali |
627 |
}
|
| 29231 |
amit.gupta |
628 |
|
| 28969 |
amit.gupta |
629 |
float totalCashback = 0;
|
|
|
630 |
int count = 0;
|
| 29231 |
amit.gupta |
631 |
|
|
|
632 |
List<SchemeType> allOutSchemeTypes = new ArrayList<>();
|
|
|
633 |
allOutSchemeTypes.addAll(Arrays.asList(SchemeType.ACTIVATION, SchemeType.INVESTMENT));
|
|
|
634 |
allOutSchemeTypes.addAll(OUT_SCHEME_TYPES);
|
|
|
635 |
|
|
|
636 |
List<Scheme> allActiveSchemes = schemeRepository.selectActiveAll(allOutSchemeTypes, partnerType,
|
| 28969 |
amit.gupta |
637 |
fofoOrder.getCreateTimestamp(), false);
|
| 29231 |
amit.gupta |
638 |
|
| 28969 |
amit.gupta |
639 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
640 |
float itemCashback = 0;
|
|
|
641 |
Set<Integer> schemeIds = new HashSet<>(
|
|
|
642 |
schemeItemRepository.selectSchemeIdByItemId(inventoryItem.getItemId()));
|
| 29231 |
amit.gupta |
643 |
List<Scheme> itemActiveSchemes = allActiveSchemes.stream().filter(x -> schemeIds.contains(x.getId()))
|
| 28969 |
amit.gupta |
644 |
.collect(Collectors.toList());
|
| 29231 |
amit.gupta |
645 |
for (Scheme scheme : itemActiveSchemes) {
|
| 29239 |
amit.gupta |
646 |
LOGGER.info("Scheme ==== {}", scheme);
|
| 28969 |
amit.gupta |
647 |
itemCashback += this.createSchemeInOut(scheme, inventoryItem);
|
|
|
648 |
}
|
| 29240 |
amit.gupta |
649 |
LOGGER.info("itemCashback ==== {}", itemCashback);
|
| 28969 |
amit.gupta |
650 |
if (itemCashback > 0) {
|
|
|
651 |
count++;
|
|
|
652 |
totalCashback += itemCashback;
|
|
|
653 |
}
|
|
|
654 |
}
|
|
|
655 |
if (count > 0) {
|
| 29231 |
amit.gupta |
656 |
walletService.addAmountToWallet(
|
|
|
657 |
retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT, "Sales margin for invoice number "
|
|
|
658 |
+ fofoOrder.getInvoiceNumber() + ". Total " + count + " pc(s)",
|
| 28969 |
amit.gupta |
659 |
totalCashback, fofoOrder.getCreateTimestamp());
|
|
|
660 |
fofoOrder.setCashback(totalCashback);
|
|
|
661 |
fofoOrderRepository.persist(fofoOrder);
|
|
|
662 |
}
|
| 22653 |
ashik.ali |
663 |
}
|
|
|
664 |
}
|
| 23444 |
amit.gupta |
665 |
|
| 23508 |
amit.gupta |
666 |
@Override
|
| 23796 |
amit.gupta |
667 |
public void rollbackSchemes(List<Integer> inventoryItemIds, int rollbackReference, String rollbackReason)
|
|
|
668 |
throws Exception {
|
| 23638 |
amit.gupta |
669 |
Set<Integer> inventoryItemIdSet = new HashSet<>(inventoryItemIds);
|
| 23508 |
amit.gupta |
670 |
float amountToRollback = 0;
|
|
|
671 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIdSet);
|
| 23796 |
amit.gupta |
672 |
for (SchemeInOut schemeInOut : schemes) {
|
| 23983 |
amit.gupta |
673 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
674 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
| 29231 |
amit.gupta |
675 |
if (schemeInOut.getStatus() == null || schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
| 27377 |
amit.gupta |
676 |
amountToRollback += schemeInOut.getAmount();
|
|
|
677 |
}
|
|
|
678 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
679 |
schemeInOut.setStatusDescription(rollbackReason);
|
| 23983 |
amit.gupta |
680 |
}
|
| 23508 |
amit.gupta |
681 |
}
|
| 23983 |
amit.gupta |
682 |
if (amountToRollback > 0) {
|
| 23638 |
amit.gupta |
683 |
int inventoryItemId = inventoryItemIds.get(0);
|
| 26498 |
amit.gupta |
684 |
InventoryItem ii = inventoryItemRepository.selectById(inventoryItemId);
|
|
|
685 |
Integer fofoId = ii.getFofoId();
|
| 27898 |
amit.gupta |
686 |
// Purchase p = purchaseRepository.selectById(ii.getPurchaseId());
|
| 26684 |
amit.gupta |
687 |
// TODO//
|
| 26693 |
amit.gupta |
688 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, ii.getPurchaseId(),
|
| 26498 |
amit.gupta |
689 |
WalletReferenceType.SCHEME_IN, rollbackReason, LocalDateTime.now());
|
| 23638 |
amit.gupta |
690 |
}
|
| 23508 |
amit.gupta |
691 |
}
|
| 23884 |
amit.gupta |
692 |
|
| 23781 |
ashik.ali |
693 |
@Override
|
| 23796 |
amit.gupta |
694 |
public Map<String, Object> getSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
695 |
throws ProfitMandiBusinessException {
|
| 23781 |
ashik.ali |
696 |
Map<String, Object> map = new HashMap<>();
|
|
|
697 |
List<Scheme> schemes = null;
|
|
|
698 |
long size = 0;
|
| 23798 |
amit.gupta |
699 |
if (roleManager.isAdmin(roleIds)) {
|
| 23781 |
ashik.ali |
700 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
701 |
size = schemeRepository.selectAllCount();
|
| 23796 |
amit.gupta |
702 |
} else {
|
| 23781 |
ashik.ali |
703 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
704 |
size = schemeRepository.selectAllActiveCount();
|
|
|
705 |
}
|
|
|
706 |
map.put("schemes", schemes);
|
|
|
707 |
map.put("start", offset + 1);
|
|
|
708 |
map.put("size", size);
|
| 23796 |
amit.gupta |
709 |
if (schemes.size() < limit) {
|
| 23781 |
ashik.ali |
710 |
map.put("end", offset + schemes.size());
|
| 23796 |
amit.gupta |
711 |
} else {
|
| 23781 |
ashik.ali |
712 |
map.put("end", offset + limit);
|
|
|
713 |
}
|
|
|
714 |
return map;
|
|
|
715 |
}
|
| 23796 |
amit.gupta |
716 |
|
| 23781 |
ashik.ali |
717 |
@Override
|
| 23796 |
amit.gupta |
718 |
public List<Scheme> getPaginatedSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
719 |
throws ProfitMandiBusinessException {
|
| 23781 |
ashik.ali |
720 |
LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
|
|
|
721 |
List<Scheme> schemes = null;
|
| 23798 |
amit.gupta |
722 |
if (roleManager.isAdmin(roleIds)) {
|
| 23781 |
ashik.ali |
723 |
schemes = schemeRepository.selectAll(offset, limit);
|
| 23796 |
amit.gupta |
724 |
} else {
|
| 23781 |
ashik.ali |
725 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
726 |
}
|
|
|
727 |
return schemes;
|
|
|
728 |
}
|
| 23508 |
amit.gupta |
729 |
|
| 23968 |
amit.gupta |
730 |
@Override
|
| 27898 |
amit.gupta |
731 |
// This is being called to reverse schemes while processing price Drop
|
| 24976 |
amit.gupta |
732 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int priceDropId, String reversalReason)
|
| 26332 |
amit.gupta |
733 |
throws ProfitMandiBusinessException {
|
| 26498 |
amit.gupta |
734 |
PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
|
| 24976 |
amit.gupta |
735 |
Map<Integer, List<InventoryItem>> purchaseInventoryListMap = inventoryItems.stream()
|
|
|
736 |
.collect(Collectors.groupingBy(InventoryItem::getPurchaseId, Collectors.toList()));
|
|
|
737 |
|
|
|
738 |
for (Map.Entry<Integer, List<InventoryItem>> purchaseEntry : purchaseInventoryListMap.entrySet()) {
|
|
|
739 |
float amountToCredit = 0;
|
|
|
740 |
float amountToDebit = 0;
|
|
|
741 |
int purchaseId = purchaseEntry.getKey();
|
|
|
742 |
List<InventoryItem> purchaseInventoryItemList = purchaseEntry.getValue();
|
|
|
743 |
|
|
|
744 |
Map<Integer, InventoryItem> inventoryItemsMap = purchaseInventoryItemList.stream()
|
|
|
745 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
746 |
|
|
|
747 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
| 27399 |
amit.gupta |
748 |
LOGGER.info("Scheme InOuts , {}", schemeInOuts);
|
| 27898 |
amit.gupta |
749 |
if (schemeInOuts.size() == 0) {
|
| 27400 |
amit.gupta |
750 |
continue;
|
|
|
751 |
}
|
| 24976 |
amit.gupta |
752 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
753 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
754 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
755 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
756 |
InventoryItem ii = inventoryItemsMap.get(schemeInOut.getInventoryItemId());
|
|
|
757 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
758 |
if (scheme.getAmountType().equals(AmountType.FIXED)) {
|
|
|
759 |
continue;
|
| 23995 |
amit.gupta |
760 |
}
|
| 27713 |
amit.gupta |
761 |
if (scheme.getType().equals(SchemeType.IN) && schemeInOut.getRolledBackTimestamp() == null) {
|
| 24976 |
amit.gupta |
762 |
float newAmount = getAmount(ii, scheme);
|
|
|
763 |
if (schemeInOut.getAmount() - newAmount >= 0.01f) {
|
|
|
764 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
| 27898 |
amit.gupta |
765 |
|
| 24976 |
amit.gupta |
766 |
SchemeInOut sioNew = new SchemeInOut();
|
|
|
767 |
sioNew.setAmount(newAmount);
|
| 27713 |
amit.gupta |
768 |
sioNew.setStatus(schemeInOut.getStatus());
|
|
|
769 |
sioNew.setStatusDescription(schemeInOut.getStatusDescription());
|
| 24976 |
amit.gupta |
770 |
sioNew.setInventoryItemId(schemeInOut.getInventoryItemId());
|
|
|
771 |
sioNew.setSchemeId(schemeInOut.getSchemeId());
|
|
|
772 |
schemeInOutRepository.persist(sioNew);
|
| 27713 |
amit.gupta |
773 |
|
|
|
774 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
775 |
schemeInOut.setStatusDescription("Pricedrop processed, new entries added");
|
| 27898 |
amit.gupta |
776 |
// IF not credited then dont consider any credit/debit for that sio entry
|
|
|
777 |
if (schemeInOut.getCreditTimestamp() != null) {
|
| 27713 |
amit.gupta |
778 |
amountToCredit += sioNew.getAmount();
|
|
|
779 |
amountToDebit += schemeInOut.getAmount();
|
|
|
780 |
}
|
| 24976 |
amit.gupta |
781 |
}
|
| 24562 |
amit.gupta |
782 |
|
| 24976 |
amit.gupta |
783 |
}
|
| 23986 |
amit.gupta |
784 |
}
|
| 24976 |
amit.gupta |
785 |
int fofoId = inventoryItems.get(0).getFofoId();
|
|
|
786 |
if (amountToDebit > 0) {
|
| 26684 |
amit.gupta |
787 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN,
|
|
|
788 |
MessageFormat.format(reversalReason, purchaseInventoryItemList.size()), -amountToDebit,
|
|
|
789 |
priceDrop.getAffectedOn());
|
| 24976 |
amit.gupta |
790 |
}
|
|
|
791 |
if (amountToCredit > 0) {
|
| 26403 |
amit.gupta |
792 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN,
|
| 26684 |
amit.gupta |
793 |
MessageFormat.format(reversalReason, purchaseInventoryItemList.size()), amountToCredit,
|
|
|
794 |
priceDrop.getAffectedOn());
|
| 24976 |
amit.gupta |
795 |
}
|
| 23968 |
amit.gupta |
796 |
}
|
|
|
797 |
}
|
| 24562 |
amit.gupta |
798 |
|
| 24264 |
amit.gupta |
799 |
@Override
|
| 27898 |
amit.gupta |
800 |
// Always being called from cancel order means no SCHEME IN is considered
|
| 24562 |
amit.gupta |
801 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int reversalReference, String reversalReason,
|
| 29231 |
amit.gupta |
802 |
List<SchemeType> schemeTypes) throws ProfitMandiBusinessException {
|
| 24264 |
amit.gupta |
803 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
|
| 28419 |
tejbeer |
804 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 28417 |
tejbeer |
805 |
LOGGER.info("inventoryItems" + inventoryItems);
|
|
|
806 |
|
| 24264 |
amit.gupta |
807 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
| 28416 |
tejbeer |
808 |
LOGGER.info("schemeInOuts" + schemeInOuts);
|
| 28417 |
tejbeer |
809 |
float amountToRollback = 0;
|
| 28416 |
tejbeer |
810 |
|
| 28417 |
tejbeer |
811 |
if (!schemeInOuts.isEmpty()) {
|
|
|
812 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
813 |
LOGGER.info("schemeIds" + schemeIds);
|
| 28416 |
tejbeer |
814 |
|
| 28417 |
tejbeer |
815 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
816 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
817 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
818 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
| 29231 |
amit.gupta |
819 |
if (schemeTypes.contains(scheme.getType())) {
|
| 28417 |
tejbeer |
820 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
821 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
822 |
if (schemeInOut.getStatus().equals(SchemePayoutStatus.CREDITED)) {
|
|
|
823 |
amountToRollback += schemeInOut.getAmount();
|
|
|
824 |
}
|
|
|
825 |
schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
826 |
schemeInOut.setStatusDescription(reversalReason);
|
| 27377 |
amit.gupta |
827 |
}
|
| 24264 |
amit.gupta |
828 |
}
|
|
|
829 |
}
|
| 28417 |
tejbeer |
830 |
|
| 24264 |
amit.gupta |
831 |
}
|
|
|
832 |
int fofoId = inventoryItems.get(0).getFofoId();
|
| 29231 |
amit.gupta |
833 |
WalletReferenceType walletReferenceType = schemeTypes.containsAll(SchemeService.OUT_SCHEME_TYPES)
|
|
|
834 |
? WalletReferenceType.SCHEME_OUT
|
|
|
835 |
: (schemeTypes.contains(SchemeType.ACTIVATION) ? WalletReferenceType.ACTIVATION_SCHEME
|
|
|
836 |
: WalletReferenceType.INVESTMENT_PAYOUT);
|
| 27377 |
amit.gupta |
837 |
if (amountToRollback > 0) {
|
| 29231 |
amit.gupta |
838 |
// Mark appropriate reference of rollback investment margin
|
|
|
839 |
if (schemeTypes.contains(SchemeType.INVESTMENT)) {
|
|
|
840 |
reversalReference = Integer
|
|
|
841 |
.parseInt(FormattingUtils.getYearMonth(schemeInOuts.get(0).getCreditTimestamp()));
|
|
|
842 |
}
|
| 27377 |
amit.gupta |
843 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, reversalReference, walletReferenceType,
|
| 26498 |
amit.gupta |
844 |
reversalReason, LocalDateTime.now());
|
| 24268 |
amit.gupta |
845 |
}
|
| 24264 |
amit.gupta |
846 |
}
|
| 23968 |
amit.gupta |
847 |
|
| 26332 |
amit.gupta |
848 |
@Override
|
|
|
849 |
public double getTotalMargin(int itemId, PartnerType partnerType, LocalDateTime dateTime) {
|
|
|
850 |
Session session = sessionFactory.getCurrentSession();
|
|
|
851 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
852 |
CriteriaQuery<Double> criteriaQuery = cb.createQuery(Double.class);
|
|
|
853 |
Root<SchemeItem> schemeItem = criteriaQuery.from(SchemeItem.class);
|
|
|
854 |
Root<Scheme> scheme = criteriaQuery.from(Scheme.class);
|
|
|
855 |
Predicate schemePredicate = cb.equal(scheme.get(ProfitMandiConstants.AMOUNT_TYPE), AmountType.PERCENTAGE);
|
|
|
856 |
Predicate lessThanPredicate = cb.lessThanOrEqualTo(scheme.get(ProfitMandiConstants.END_DATE_TIME), dateTime);
|
|
|
857 |
Predicate greaterThanPredicate = cb.greaterThanOrEqualTo(scheme.get(ProfitMandiConstants.START_DATE_TIME),
|
|
|
858 |
dateTime);
|
|
|
859 |
Predicate joinPredicate = cb.equal(scheme.get("id"), schemeItem.get("schemeId"));
|
|
|
860 |
Predicate schemeItemPredicate = cb.equal(schemeItem.get(ProfitMandiConstants.ITEM_ID), itemId);
|
|
|
861 |
criteriaQuery.select(cb.sum(scheme.get(ProfitMandiConstants.AMOUNT))).where(schemePredicate, lessThanPredicate,
|
|
|
862 |
greaterThanPredicate, schemeItemPredicate, joinPredicate);
|
|
|
863 |
|
|
|
864 |
Query<Double> query = session.createQuery(criteriaQuery);
|
|
|
865 |
return query.getSingleResult() + ProfitMandiConstants.SCHEME_INVESTMENT_MARGIN;
|
|
|
866 |
|
|
|
867 |
}
|
|
|
868 |
|
| 26684 |
amit.gupta |
869 |
@Override
|
|
|
870 |
@Cacheable(value = "itemSchemeCashback", cacheManager = "timeoutCacheManager")
|
|
|
871 |
public Map<Integer, Float> getItemSchemeCashBack() {
|
|
|
872 |
Map<Integer, Float> itemCashbackMap = new HashMap<>();
|
|
|
873 |
Map<Integer, Scheme> cashbackSchemesMap = schemeRepository
|
| 29231 |
amit.gupta |
874 |
.selectActiveAll(Arrays.asList(SchemeType.ACTIVATION), PartnerType.ALL, LocalDateTime.now(), true)
|
|
|
875 |
.stream().filter(x -> x.getAmountType().equals(AmountType.FIXED))
|
| 26684 |
amit.gupta |
876 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 26722 |
amit.gupta |
877 |
if (cashbackSchemesMap.size() > 0) {
|
| 26713 |
amit.gupta |
878 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(cashbackSchemesMap.keySet());
|
|
|
879 |
schemeItems.stream().forEach(x -> {
|
|
|
880 |
itemCashbackMap.put(x.getItemId(), cashbackSchemesMap.get(x.getSchemeId()).getAmount());
|
|
|
881 |
});
|
|
|
882 |
}
|
| 26722 |
amit.gupta |
883 |
// A107FD Model needs to removed
|
| 26703 |
amit.gupta |
884 |
itemCashbackMap.remove(30211);
|
|
|
885 |
itemCashbackMap.remove(30212);
|
|
|
886 |
itemCashbackMap.remove(30213);
|
|
|
887 |
itemCashbackMap.remove(30756);
|
| 26684 |
amit.gupta |
888 |
return itemCashbackMap;
|
|
|
889 |
}
|
|
|
890 |
|
| 24976 |
amit.gupta |
891 |
/*
|
|
|
892 |
* @Override public void updateSchmesForModel(int catalogId) throws
|
|
|
893 |
* ProfitMandiBusinessException { List<Item> items =
|
|
|
894 |
* itemRepository.selectAllByCatalogItemId(catalogId); Map<Integer, Scheme>
|
|
|
895 |
* schemes = schemeRepository .selectAllByItemIds(items.stream().map(x ->
|
|
|
896 |
* x.getId()).collect(Collectors.toList()));
|
|
|
897 |
*
|
|
|
898 |
* }
|
|
|
899 |
*/
|
| 24562 |
amit.gupta |
900 |
|
| 26802 |
tejbeer |
901 |
@Override
|
| 27898 |
amit.gupta |
902 |
public List<Scheme> selectSchemeByPartnerType(PartnerType partnerType, LocalDate onDate, int itemId,
|
|
|
903 |
boolean isAdmin, int offset, int limit) throws ProfitMandiBusinessException {
|
| 26802 |
tejbeer |
904 |
Session session = sessionFactory.getCurrentSession();
|
|
|
905 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
906 |
CriteriaQuery<Scheme> query = cb.createQuery(Scheme.class);
|
|
|
907 |
Root<Scheme> scheme = query.from(Scheme.class);
|
|
|
908 |
List<PartnerType> pt = new ArrayList<>();
|
|
|
909 |
pt.add(PartnerType.ALL);
|
|
|
910 |
pt.add(partnerType);
|
|
|
911 |
Predicate p1 = cb.in(scheme.get("partnerType")).value(pt);
|
|
|
912 |
Predicate p2 = cb.isNotNull(scheme.get("activeTimestamp"));
|
|
|
913 |
cb.desc(cb.isNull(scheme.get("expireTimestamp")));
|
|
|
914 |
|
|
|
915 |
Predicate p3 = null;
|
|
|
916 |
Predicate finalPredicate = null;
|
|
|
917 |
if (itemId != 0) {
|
| 27898 |
amit.gupta |
918 |
|
| 26802 |
tejbeer |
919 |
List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(itemId);
|
| 27424 |
tejbeer |
920 |
LOGGER.info("schemeId" + schemeIds);
|
| 27898 |
amit.gupta |
921 |
if (schemeIds.isEmpty()) {
|
| 27424 |
tejbeer |
922 |
return new ArrayList<>();
|
|
|
923 |
}
|
| 26802 |
tejbeer |
924 |
p3 = cb.in(scheme.get("id")).value(schemeIds);
|
|
|
925 |
if (!isAdmin) {
|
|
|
926 |
|
| 27877 |
amit.gupta |
927 |
finalPredicate = cb.and(p1, p2, p3, cb.greaterThan(scheme.get("endDateTime"), onDate.atStartOfDay()),
|
|
|
928 |
cb.lessThanOrEqualTo(scheme.get("startDateTime"), onDate.atStartOfDay()));
|
| 26802 |
tejbeer |
929 |
query.where(finalPredicate);
|
|
|
930 |
} else {
|
|
|
931 |
finalPredicate = cb.and(p1, p3);
|
|
|
932 |
query.where(finalPredicate);
|
|
|
933 |
}
|
|
|
934 |
} else {
|
|
|
935 |
|
|
|
936 |
if (!isAdmin) {
|
|
|
937 |
|
|
|
938 |
finalPredicate = cb.and(p1, p2);
|
| 26912 |
tejbeer |
939 |
query.where(finalPredicate);
|
| 26802 |
tejbeer |
940 |
} else {
|
|
|
941 |
query.where(p1);
|
|
|
942 |
}
|
|
|
943 |
}
|
|
|
944 |
query.orderBy(cb.desc(cb.function("isnull", Boolean.class, scheme.get("expireTimestamp"))));
|
|
|
945 |
|
|
|
946 |
return session.createQuery(query).setFirstResult(offset).setMaxResults(limit).getResultList();
|
|
|
947 |
|
|
|
948 |
}
|
|
|
949 |
|
|
|
950 |
@Override
|
| 27877 |
amit.gupta |
951 |
public long selectSchemeCount(PartnerType partnerType, LocalDate onDate, int itemId, boolean isAdmin) {
|
| 26802 |
tejbeer |
952 |
Session session = sessionFactory.getCurrentSession();
|
|
|
953 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
954 |
CriteriaQuery<Long> query = cb.createQuery(Long.class);
|
|
|
955 |
Root<Scheme> scheme = query.from(Scheme.class);
|
|
|
956 |
List<PartnerType> pt = new ArrayList<>();
|
|
|
957 |
pt.add(PartnerType.ALL);
|
|
|
958 |
pt.add(partnerType);
|
|
|
959 |
Predicate p1 = cb.in(scheme.get("partnerType")).value(pt);
|
|
|
960 |
Predicate p2 = cb.isNotNull(scheme.get("activeTimestamp"));
|
|
|
961 |
Predicate finalPredicate = null;
|
|
|
962 |
if (itemId != 0) {
|
|
|
963 |
List<Integer> schemeIds = schemeItemRepository.selectSchemeIdByItemId(itemId);
|
|
|
964 |
Predicate p3 = cb.in(scheme.get("id")).value(schemeIds);
|
|
|
965 |
if (!isAdmin) {
|
|
|
966 |
|
| 27877 |
amit.gupta |
967 |
finalPredicate = cb.and(p1, p2, p3, cb.greaterThan(scheme.get("endDateTime"), onDate.atStartOfDay()),
|
|
|
968 |
cb.lessThanOrEqualTo(scheme.get("startDateTime"), onDate.atStartOfDay()));
|
| 26802 |
tejbeer |
969 |
query.select(cb.count(scheme)).where(finalPredicate);
|
|
|
970 |
} else {
|
|
|
971 |
finalPredicate = cb.and(p1, p3);
|
|
|
972 |
query.select(cb.count(scheme)).where(finalPredicate);
|
|
|
973 |
}
|
|
|
974 |
} else {
|
|
|
975 |
if (!isAdmin) {
|
|
|
976 |
finalPredicate = cb.and(p1, p2);
|
|
|
977 |
query.select(cb.count(scheme)).where(finalPredicate);
|
|
|
978 |
|
|
|
979 |
} else {
|
|
|
980 |
query.select(cb.count(scheme)).where(p1);
|
|
|
981 |
|
|
|
982 |
}
|
|
|
983 |
}
|
|
|
984 |
|
|
|
985 |
return session.createQuery(query).getSingleResult();
|
|
|
986 |
|
|
|
987 |
}
|
| 27898 |
amit.gupta |
988 |
|
| 27395 |
amit.gupta |
989 |
@Override
|
|
|
990 |
public void processActivation() throws ProfitMandiBusinessException {
|
|
|
991 |
List<SchemeInOut> pendingPayouts = schemeInOutRepository.selectAllPending();
|
|
|
992 |
List<Integer> schemeIds = new ArrayList<>();
|
|
|
993 |
Set<Integer> inventoryIds = new HashSet<>();
|
| 27898 |
amit.gupta |
994 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
| 27395 |
amit.gupta |
995 |
schemeIds.add(pendingPayout.getSchemeId());
|
| 27440 |
amit.gupta |
996 |
}
|
|
|
997 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, 0).stream()
|
| 27898 |
amit.gupta |
998 |
.filter(x -> x.getType().equals(SchemeType.ACTIVATION))
|
|
|
999 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1000 |
pendingPayouts = pendingPayouts.stream().filter(x -> schemesMap.get(x.getSchemeId()) != null)
|
|
|
1001 |
.collect(Collectors.toList());
|
| 27440 |
amit.gupta |
1002 |
|
| 27898 |
amit.gupta |
1003 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
| 27395 |
amit.gupta |
1004 |
inventoryIds.add(pendingPayout.getInventoryItemId());
|
|
|
1005 |
}
|
| 27898 |
amit.gupta |
1006 |
Map<Integer, InventoryItem> inventoryItemMap = inventoryItemRepository.selectByIds(inventoryIds).stream()
|
|
|
1007 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
1008 |
Map<String, InventoryItem> serialNumberMap = inventoryItemMap.values().stream()
|
|
|
1009 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x));
|
|
|
1010 |
|
|
|
1011 |
List<ActivatedImei> activatedImeis = activatedImeiRepository
|
|
|
1012 |
.selectBySerialNumbers(new ArrayList<>(serialNumberMap.keySet()));
|
|
|
1013 |
|
|
|
1014 |
Map<String, ActivatedImei> activatedImeiMap = activatedImeis.stream()
|
|
|
1015 |
.collect(Collectors.toMap(x -> x.getSerialNumber(), x -> x));
|
|
|
1016 |
for (SchemeInOut pendingPayout : pendingPayouts) {
|
| 27395 |
amit.gupta |
1017 |
InventoryItem ii = inventoryItemMap.get(pendingPayout.getInventoryItemId());
|
|
|
1018 |
String serialNumber = ii.getSerialNumber();
|
|
|
1019 |
ActivatedImei activatedImei = activatedImeiMap.get(serialNumber);
|
| 27898 |
amit.gupta |
1020 |
if (activatedImei == null) {
|
| 27395 |
amit.gupta |
1021 |
continue;
|
|
|
1022 |
}
|
|
|
1023 |
Scheme scheme = schemesMap.get(pendingPayout.getSchemeId());
|
| 27898 |
amit.gupta |
1024 |
if (activatedImei.getActivationTimestamp().isBefore(scheme.getEndDateTime())
|
|
|
1025 |
&& activatedImei.getActivationTimestamp().isAfter(scheme.getStartDateTime())) {
|
| 27395 |
amit.gupta |
1026 |
int fofoId = ii.getFofoId();
|
| 27898 |
amit.gupta |
1027 |
// Get latest order Id
|
|
|
1028 |
int orderId = scanRecordRepository.selectByInventoryItemId(ii.getId()).stream()
|
|
|
1029 |
.filter(x -> x.getOrderId() > 0)
|
|
|
1030 |
.sorted(Comparator.comparing(ScanRecord::getCreateTimestamp).reversed()).findFirst().get()
|
|
|
1031 |
.getOrderId();
|
| 27395 |
amit.gupta |
1032 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(orderId);
|
| 27898 |
amit.gupta |
1033 |
walletService.addAmountToWallet(fofoId, orderId, WalletReferenceType.ACTIVATION_SCHEME,
|
|
|
1034 |
"Activation margin for Imei#" + serialNumber, pendingPayout.getAmount(),
|
|
|
1035 |
fofoOrder.getCreateTimestamp());
|
| 27395 |
amit.gupta |
1036 |
pendingPayout.setCreditTimestamp(LocalDateTime.now());
|
|
|
1037 |
pendingPayout.setStatus(SchemePayoutStatus.CREDITED);
|
|
|
1038 |
pendingPayout.setStatusDescription("Activation margin credited");
|
|
|
1039 |
} else {
|
|
|
1040 |
pendingPayout.setStatus(SchemePayoutStatus.REJECTED);
|
| 27898 |
amit.gupta |
1041 |
pendingPayout.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
1042 |
;
|
|
|
1043 |
pendingPayout.setStatusDescription(
|
|
|
1044 |
"Rejected, activated on " + FormattingUtils.formatDate(activatedImei.getActivationTimestamp()));
|
| 27395 |
amit.gupta |
1045 |
}
|
|
|
1046 |
}
|
|
|
1047 |
}
|
| 26802 |
tejbeer |
1048 |
|
| 22653 |
ashik.ali |
1049 |
}
|