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