| 22653 |
ashik.ali |
1 |
package com.spice.profitmandi.service.scheme;
|
|
|
2 |
|
| 25507 |
amit.gupta |
3 |
import java.time.LocalDate;
|
| 22859 |
ashik.ali |
4 |
import java.time.LocalDateTime;
|
| 23019 |
ashik.ali |
5 |
import java.util.ArrayList;
|
| 22653 |
ashik.ali |
6 |
import java.util.HashMap;
|
| 22859 |
ashik.ali |
7 |
import java.util.HashSet;
|
| 22653 |
ashik.ali |
8 |
import java.util.List;
|
|
|
9 |
import java.util.Map;
|
|
|
10 |
import java.util.Set;
|
| 23968 |
amit.gupta |
11 |
import java.util.stream.Collectors;
|
| 22653 |
ashik.ali |
12 |
|
| 26332 |
amit.gupta |
13 |
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
14 |
import javax.persistence.criteria.CriteriaQuery;
|
|
|
15 |
import javax.persistence.criteria.Predicate;
|
|
|
16 |
import javax.persistence.criteria.Root;
|
|
|
17 |
|
| 23781 |
ashik.ali |
18 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
19 |
import org.apache.logging.log4j.Logger;
|
| 26332 |
amit.gupta |
20 |
import org.hibernate.Session;
|
|
|
21 |
import org.hibernate.SessionFactory;
|
|
|
22 |
import org.hibernate.query.Query;
|
| 22653 |
ashik.ali |
23 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 23781 |
ashik.ali |
24 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 22653 |
ashik.ali |
25 |
import org.springframework.stereotype.Component;
|
|
|
26 |
|
| 24307 |
amit.gupta |
27 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
| 22653 |
ashik.ali |
28 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
29 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 23019 |
ashik.ali |
30 |
import com.spice.profitmandi.common.model.SchemeModel;
|
| 22859 |
ashik.ali |
31 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 23339 |
ashik.ali |
32 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 22653 |
ashik.ali |
33 |
import com.spice.profitmandi.dao.entity.catalog.RetailerScheme;
|
|
|
34 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
| 23365 |
ashik.ali |
35 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 22653 |
ashik.ali |
36 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 25503 |
amit.gupta |
37 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
| 23339 |
ashik.ali |
38 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| 25043 |
amit.gupta |
39 |
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
|
| 22859 |
ashik.ali |
40 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
|
|
41 |
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
|
| 23527 |
ashik.ali |
42 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 22653 |
ashik.ali |
43 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 25111 |
amit.gupta |
44 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| 25503 |
amit.gupta |
45 |
import com.spice.profitmandi.dao.model.CreateSchemeRequest;
|
| 22859 |
ashik.ali |
46 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 22653 |
ashik.ali |
47 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 23995 |
amit.gupta |
48 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 23968 |
amit.gupta |
49 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 23019 |
ashik.ali |
50 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
|
|
51 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
| 23365 |
ashik.ali |
52 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
| 22653 |
ashik.ali |
53 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 25503 |
amit.gupta |
54 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
|
| 23339 |
ashik.ali |
55 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 25043 |
amit.gupta |
56 |
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
|
| 22859 |
ashik.ali |
57 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
|
|
58 |
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
|
| 23798 |
amit.gupta |
59 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| 22859 |
ashik.ali |
60 |
import com.spice.profitmandi.service.wallet.WalletService;
|
| 22653 |
ashik.ali |
61 |
|
| 22859 |
ashik.ali |
62 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
63 |
|
| 22653 |
ashik.ali |
64 |
@Component
|
|
|
65 |
public class SchemeServiceImpl implements SchemeService {
|
|
|
66 |
|
| 23568 |
govind |
67 |
private static final Logger LOGGER = LogManager.getLogger(SchemeServiceImpl.class);
|
| 23444 |
amit.gupta |
68 |
|
| 22653 |
ashik.ali |
69 |
@Autowired
|
| 23781 |
ashik.ali |
70 |
@Qualifier("fofoInventoryItemRepository")
|
| 22653 |
ashik.ali |
71 |
private InventoryItemRepository inventoryItemRepository;
|
| 23444 |
amit.gupta |
72 |
|
| 22653 |
ashik.ali |
73 |
@Autowired
|
| 25503 |
amit.gupta |
74 |
private PartnerTypeChangeService partnerTypeChangeService;
|
|
|
75 |
|
|
|
76 |
@Autowired
|
| 25043 |
amit.gupta |
77 |
private ScanRecordRepository scanRecordRepository;
|
| 23444 |
amit.gupta |
78 |
|
| 22653 |
ashik.ali |
79 |
@Autowired
|
| 26332 |
amit.gupta |
80 |
private SessionFactory sessionFactory;
|
|
|
81 |
|
|
|
82 |
@Autowired
|
| 22653 |
ashik.ali |
83 |
private SchemeRepository schemeRepository;
|
| 23444 |
amit.gupta |
84 |
|
| 22653 |
ashik.ali |
85 |
@Autowired
|
| 23798 |
amit.gupta |
86 |
private RoleManager roleManager;
|
|
|
87 |
|
|
|
88 |
@Autowired
|
| 22859 |
ashik.ali |
89 |
private RetailerRepository retailerRepository;
|
| 24562 |
amit.gupta |
90 |
|
| 23995 |
amit.gupta |
91 |
@Autowired
|
|
|
92 |
private TagListingRepository tagListingRepository;
|
| 23444 |
amit.gupta |
93 |
|
| 22859 |
ashik.ali |
94 |
@Autowired
|
|
|
95 |
private SchemeInOutRepository schemeInOutRepository;
|
| 23444 |
amit.gupta |
96 |
|
| 22653 |
ashik.ali |
97 |
@Autowired
|
| 23781 |
ashik.ali |
98 |
@Qualifier("catalogItemRepository")
|
| 22859 |
ashik.ali |
99 |
private ItemRepository itemRepository;
|
| 23444 |
amit.gupta |
100 |
|
| 22859 |
ashik.ali |
101 |
@Autowired
|
|
|
102 |
private SchemeItemRepository schemeItemRepository;
|
| 23444 |
amit.gupta |
103 |
|
| 22859 |
ashik.ali |
104 |
@Autowired
|
|
|
105 |
private WalletService walletService;
|
| 23444 |
amit.gupta |
106 |
|
| 23019 |
ashik.ali |
107 |
@Autowired
|
|
|
108 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
| 23444 |
amit.gupta |
109 |
|
| 23019 |
ashik.ali |
110 |
@Autowired
|
|
|
111 |
private FofoLineItemRepository fofoLineItemRepository;
|
| 23444 |
amit.gupta |
112 |
|
| 23339 |
ashik.ali |
113 |
@Autowired
|
|
|
114 |
private PurchaseRepository purchaseRepository;
|
| 23444 |
amit.gupta |
115 |
|
| 23344 |
ashik.ali |
116 |
@Autowired
|
| 23365 |
ashik.ali |
117 |
private FofoOrderRepository fofoOrderRepository;
|
| 23796 |
amit.gupta |
118 |
|
| 22653 |
ashik.ali |
119 |
@Override
|
| 22859 |
ashik.ali |
120 |
public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
| 23444 |
amit.gupta |
121 |
|
| 23019 |
ashik.ali |
122 |
this.validateCreateSchemeRequest(createSchemeRequest);
|
| 23444 |
amit.gupta |
123 |
|
| 22859 |
ashik.ali |
124 |
Scheme scheme = this.toScheme(creatorId, createSchemeRequest);
|
| 23444 |
amit.gupta |
125 |
|
|
|
126 |
if (scheme.getStartDateTime().isAfter(scheme.getEndDateTime())) {
|
|
|
127 |
throw new ProfitMandiBusinessException(
|
|
|
128 |
ProfitMandiConstants.START_DATE + ", " + ProfitMandiConstants.END_DATE,
|
|
|
129 |
scheme.getStartDateTime() + ", " + scheme.getEndDateTime(), "SCHM_VE_1005");
|
| 22653 |
ashik.ali |
130 |
}
|
| 23444 |
amit.gupta |
131 |
|
| 26332 |
amit.gupta |
132 |
// this.validateItemIds(createSchemeRequest);
|
| 22859 |
ashik.ali |
133 |
schemeRepository.persist(scheme);
|
| 23444 |
amit.gupta |
134 |
for (int itemId : createSchemeRequest.getItemIds()) {
|
| 22859 |
ashik.ali |
135 |
SchemeItem schemeItem = new SchemeItem();
|
|
|
136 |
schemeItem.setSchemeId(scheme.getId());
|
|
|
137 |
schemeItem.setItemId(itemId);
|
|
|
138 |
schemeItemRepository.persist(schemeItem);
|
|
|
139 |
}
|
| 23444 |
amit.gupta |
140 |
|
| 22653 |
ashik.ali |
141 |
}
|
| 23444 |
amit.gupta |
142 |
|
|
|
143 |
private void validateCreateSchemeRequest(CreateSchemeRequest createSchemeRequest)
|
|
|
144 |
throws ProfitMandiBusinessException {
|
|
|
145 |
if (createSchemeRequest.getName() == null || createSchemeRequest.getName().isEmpty()) {
|
|
|
146 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.NAME, createSchemeRequest.getName(),
|
|
|
147 |
"SCHM_VE_1000");
|
| 23019 |
ashik.ali |
148 |
}
|
| 23444 |
amit.gupta |
149 |
if (createSchemeRequest.getAmount() <= 0) {
|
|
|
150 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
151 |
"SCHM_VE_1001");
|
| 23019 |
ashik.ali |
152 |
}
|
| 23444 |
amit.gupta |
153 |
|
| 25503 |
amit.gupta |
154 |
if (createSchemeRequest.getAmountType().equals(AmountType.PERCENTAGE)
|
| 23444 |
amit.gupta |
155 |
&& createSchemeRequest.getAmount() > 100) {
|
|
|
156 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.AMOUNT, createSchemeRequest.getAmount(),
|
|
|
157 |
"SCHM_VE_1002");
|
| 23019 |
ashik.ali |
158 |
}
|
| 23444 |
amit.gupta |
159 |
|
| 23886 |
amit.gupta |
160 |
if (createSchemeRequest.getStartDate() == null) {
|
| 23983 |
amit.gupta |
161 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.START_DATE, createSchemeRequest.getStartDate(),
|
|
|
162 |
"SCHM_VE_1003");
|
| 23019 |
ashik.ali |
163 |
}
|
| 23886 |
amit.gupta |
164 |
if (createSchemeRequest.getEndDate() == null) {
|
| 23983 |
amit.gupta |
165 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.END_DATE, createSchemeRequest.getEndDate(),
|
|
|
166 |
"SCHM_VE_1004");
|
| 23019 |
ashik.ali |
167 |
}
|
|
|
168 |
}
|
| 23444 |
amit.gupta |
169 |
|
|
|
170 |
private Scheme toScheme(int creatorId, CreateSchemeRequest createSchemeRequest) {
|
| 22859 |
ashik.ali |
171 |
Scheme scheme = new Scheme();
|
|
|
172 |
scheme.setName(createSchemeRequest.getName());
|
|
|
173 |
scheme.setDescription(createSchemeRequest.getDescription());
|
| 25503 |
amit.gupta |
174 |
scheme.setType(createSchemeRequest.getType());
|
|
|
175 |
scheme.setAmountType(createSchemeRequest.getAmountType());
|
| 22859 |
ashik.ali |
176 |
scheme.setAmount(createSchemeRequest.getAmount());
|
| 25503 |
amit.gupta |
177 |
scheme.setPartnerType(createSchemeRequest.getPartnerType());
|
| 25517 |
amit.gupta |
178 |
scheme.setStartDateTime(createSchemeRequest.getStartDate());
|
| 23886 |
amit.gupta |
179 |
scheme.setEndDateTime(createSchemeRequest.getEndDate());
|
| 22859 |
ashik.ali |
180 |
scheme.setCreatedBy(creatorId);
|
|
|
181 |
return scheme;
|
|
|
182 |
}
|
| 23444 |
amit.gupta |
183 |
|
|
|
184 |
private void validateItemIds(CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
|
|
185 |
if (createSchemeRequest.getItemIds() == null || createSchemeRequest.getItemIds().isEmpty()) {
|
|
|
186 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
187 |
"SCHM_1003");
|
| 22859 |
ashik.ali |
188 |
}
|
| 23444 |
amit.gupta |
189 |
List<Integer> foundItemIds = itemRepository.selectIdsByIdsAndType(createSchemeRequest.getItemIds(),
|
|
|
190 |
ItemType.SERIALIZED);
|
|
|
191 |
if (foundItemIds.size() != createSchemeRequest.getItemIds().size()) {
|
| 22859 |
ashik.ali |
192 |
createSchemeRequest.getItemIds().removeAll(foundItemIds);
|
| 23444 |
amit.gupta |
193 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, createSchemeRequest.getItemIds(),
|
|
|
194 |
"SCHM_1004");
|
| 22859 |
ashik.ali |
195 |
}
|
|
|
196 |
}
|
| 23444 |
amit.gupta |
197 |
|
| 22859 |
ashik.ali |
198 |
@Override
|
|
|
199 |
public Scheme getSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
200 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
|
|
201 |
List<Integer> itemIds = schemeItemRepository.selectItemIdsBySchemeId(scheme.getId());
|
| 23914 |
govind |
202 |
if (itemIds.size() > 0) {
|
|
|
203 |
List<Item> items = itemRepository.selectByIds(new HashSet<>(itemIds));
|
|
|
204 |
scheme.setItemStringMap(this.toItemStringMap(items));
|
| 23983 |
amit.gupta |
205 |
}
|
| 22859 |
ashik.ali |
206 |
return scheme;
|
|
|
207 |
}
|
| 23444 |
amit.gupta |
208 |
|
|
|
209 |
public Map<Integer, String> toItemStringMap(List<Item> items) {
|
| 23339 |
ashik.ali |
210 |
Map<Integer, String> itemMap = new HashMap<>();
|
| 23444 |
amit.gupta |
211 |
for (Item item : items) {
|
| 23339 |
ashik.ali |
212 |
itemMap.put(item.getId(), this.getItemString(item));
|
|
|
213 |
}
|
|
|
214 |
return itemMap;
|
|
|
215 |
}
|
| 23444 |
amit.gupta |
216 |
|
|
|
217 |
public String getItemString(Item item) {
|
| 23339 |
ashik.ali |
218 |
StringBuilder itemString = new StringBuilder();
|
| 23444 |
amit.gupta |
219 |
if (item.getBrand() != null && !item.getBrand().isEmpty()) {
|
| 23339 |
ashik.ali |
220 |
itemString.append(item.getBrand().trim());
|
|
|
221 |
}
|
|
|
222 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
223 |
if (item.getModelName() != null && !item.getModelName().isEmpty()) {
|
| 23339 |
ashik.ali |
224 |
itemString.append(item.getModelName().trim());
|
|
|
225 |
}
|
|
|
226 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
227 |
if (item.getModelNumber() != null && !item.getModelNumber().isEmpty()) {
|
| 23339 |
ashik.ali |
228 |
itemString.append(item.getModelNumber().trim());
|
|
|
229 |
}
|
|
|
230 |
itemString.append(" ");
|
| 23444 |
amit.gupta |
231 |
if (item.getColor() != null && !item.getColor().isEmpty()) {
|
| 23339 |
ashik.ali |
232 |
itemString.append(item.getColor().trim());
|
|
|
233 |
}
|
|
|
234 |
return itemString.toString();
|
|
|
235 |
}
|
| 23444 |
amit.gupta |
236 |
|
|
|
237 |
private Set<Integer> schemeItemsToItemIds(List<SchemeItem> schemeItems) {
|
| 23339 |
ashik.ali |
238 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
239 |
for (SchemeItem schemeItem : schemeItems) {
|
| 23339 |
ashik.ali |
240 |
itemIds.add(schemeItem.getItemId());
|
|
|
241 |
}
|
|
|
242 |
return itemIds;
|
|
|
243 |
}
|
| 23444 |
amit.gupta |
244 |
|
| 23019 |
ashik.ali |
245 |
@Override
|
|
|
246 |
public List<SchemeModel> getAllSchemeModels(LocalDateTime startDateTime, LocalDateTime endDateTime) {
|
|
|
247 |
List<Scheme> schemes = schemeRepository.selectAllBetweenCreateTimestamp(startDateTime, endDateTime);
|
|
|
248 |
Map<Integer, Scheme> schemeIdSchemeMap = this.toSchemeIdSchemeMap(schemes);
|
|
|
249 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(schemeIdSchemeMap.keySet());
|
| 23339 |
ashik.ali |
250 |
Set<Integer> itemIds = this.schemeItemsToItemIds(schemeItems);
|
|
|
251 |
List<Item> items = itemRepository.selectByIds(itemIds);
|
|
|
252 |
Map<Integer, String> itemStringMap = this.toItemStringMap(items);
|
|
|
253 |
this.addItemIdsToSchemes(schemeItems, schemeIdSchemeMap, itemStringMap);
|
| 23019 |
ashik.ali |
254 |
return this.toSchemeModels(schemeIdSchemeMap);
|
|
|
255 |
}
|
| 23444 |
amit.gupta |
256 |
|
|
|
257 |
private void addItemIdsToSchemes(List<SchemeItem> schemeItems, Map<Integer, Scheme> schemeIdSchemeMap,
|
|
|
258 |
Map<Integer, String> itemStringMap) {
|
|
|
259 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
260 |
schemeIdSchemeMap.get(schemeItem.getSchemeId()).getItemStringMap().put(schemeItem.getItemId(),
|
|
|
261 |
itemStringMap.get(schemeItem.getItemId()));
|
| 23019 |
ashik.ali |
262 |
}
|
|
|
263 |
}
|
| 23444 |
amit.gupta |
264 |
|
|
|
265 |
private void addRetailerIdsToSchemes(List<RetailerScheme> retailerSchemes, Map<Integer, Scheme> schemeIdSchemeMap) {
|
|
|
266 |
for (RetailerScheme retailerScheme : retailerSchemes) {
|
|
|
267 |
schemeIdSchemeMap.get(retailerScheme.getSchemeId()).getRetailerIds().add(retailerScheme.getRetailerId());
|
| 23019 |
ashik.ali |
268 |
}
|
|
|
269 |
}
|
| 23444 |
amit.gupta |
270 |
|
|
|
271 |
private List<SchemeModel> toSchemeModels(Map<Integer, Scheme> schemeIdSchemeMap) {
|
| 23019 |
ashik.ali |
272 |
List<SchemeModel> schemeModels = new ArrayList<>();
|
| 23444 |
amit.gupta |
273 |
for (Map.Entry<Integer, Scheme> schemeIdSchemeEntry : schemeIdSchemeMap.entrySet()) {
|
| 23019 |
ashik.ali |
274 |
schemeModels.add(this.toSchemeModel(schemeIdSchemeEntry.getValue()));
|
|
|
275 |
}
|
|
|
276 |
return schemeModels;
|
|
|
277 |
}
|
| 23444 |
amit.gupta |
278 |
|
|
|
279 |
private SchemeModel toSchemeModel(Scheme scheme) {
|
| 23019 |
ashik.ali |
280 |
SchemeModel schemeModel = new SchemeModel();
|
|
|
281 |
schemeModel.setSchemeId(scheme.getId());
|
|
|
282 |
schemeModel.setName(scheme.getName());
|
|
|
283 |
schemeModel.setDescription(scheme.getDescription());
|
|
|
284 |
schemeModel.setSchemeType(scheme.getType().toString());
|
|
|
285 |
schemeModel.setAmountType(scheme.getAmountType().toString());
|
|
|
286 |
schemeModel.setAmount(scheme.getAmount());
|
|
|
287 |
schemeModel.setStartDateTime(StringUtils.toString(scheme.getStartDateTime()));
|
|
|
288 |
schemeModel.setEndDateTime(StringUtils.toString(scheme.getEndDateTime()));
|
|
|
289 |
schemeModel.setCreateTimestamp(StringUtils.toString(scheme.getCreateTimestamp()));
|
|
|
290 |
schemeModel.setActiveTimestamp(StringUtils.toString(scheme.getActiveTimestamp()));
|
|
|
291 |
schemeModel.setExpireTimestamp(StringUtils.toString(scheme.getExpireTimestamp()));
|
|
|
292 |
schemeModel.setCreatedBy(scheme.getCreatedBy());
|
| 23339 |
ashik.ali |
293 |
schemeModel.setItemStringMap(scheme.getItemStringMap());
|
| 23019 |
ashik.ali |
294 |
schemeModel.setRetailerIds(scheme.getRetailerIds());
|
|
|
295 |
return schemeModel;
|
|
|
296 |
}
|
| 22653 |
ashik.ali |
297 |
|
|
|
298 |
@Override
|
|
|
299 |
public void activeSchemeById(int schemeId) throws ProfitMandiBusinessException {
|
|
|
300 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 23444 |
amit.gupta |
301 |
if (scheme.getActiveTimestamp() != null) {
|
|
|
302 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
303 |
"SCHM_1005");
|
| 22653 |
ashik.ali |
304 |
}
|
| 23444 |
amit.gupta |
305 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
306 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
307 |
"SCHM_1006");
|
| 22653 |
ashik.ali |
308 |
}
|
| 22859 |
ashik.ali |
309 |
scheme.setActiveTimestamp(LocalDateTime.now());
|
|
|
310 |
schemeRepository.persist(scheme);
|
| 25438 |
amit.gupta |
311 |
/*
|
|
|
312 |
* if (scheme.getType() == SchemeType.IN) {
|
|
|
313 |
* this.processPreviousPurchases(scheme); } else if (scheme.getType() ==
|
|
|
314 |
* SchemeType.OUT) { this.processPreviousSales(scheme); }
|
|
|
315 |
*/
|
| 22653 |
ashik.ali |
316 |
}
|
| 23444 |
amit.gupta |
317 |
|
| 22653 |
ashik.ali |
318 |
@Override
|
| 25069 |
amit.gupta |
319 |
public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
|
| 22653 |
ashik.ali |
320 |
Scheme scheme = schemeRepository.selectById(schemeId);
|
| 25111 |
amit.gupta |
321 |
if (scheme == null || scheme.getActiveTimestamp() == null) {
|
| 23444 |
amit.gupta |
322 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
|
|
|
323 |
"SCHM_1007");
|
| 22653 |
ashik.ali |
324 |
}
|
| 23444 |
amit.gupta |
325 |
if (scheme.getExpireTimestamp() != null) {
|
|
|
326 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
|
|
|
327 |
"SCHM_1008");
|
| 22653 |
ashik.ali |
328 |
}
|
| 22859 |
ashik.ali |
329 |
scheme.setExpireTimestamp(LocalDateTime.now());
|
| 25069 |
amit.gupta |
330 |
scheme.setEndDateTime(expiryTime);
|
| 22859 |
ashik.ali |
331 |
schemeRepository.persist(scheme);
|
| 22653 |
ashik.ali |
332 |
}
|
| 23444 |
amit.gupta |
333 |
|
|
|
334 |
private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
335 |
Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
|
| 23444 |
amit.gupta |
336 |
for (Scheme scheme : schemes) {
|
| 23019 |
ashik.ali |
337 |
schemeIdSchemeMap.put(scheme.getId(), scheme);
|
| 22859 |
ashik.ali |
338 |
}
|
| 23019 |
ashik.ali |
339 |
return schemeIdSchemeMap;
|
| 22859 |
ashik.ali |
340 |
}
|
| 23444 |
amit.gupta |
341 |
|
|
|
342 |
private Set<Integer> inventoryItemsToItemIds(List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
343 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
344 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 22859 |
ashik.ali |
345 |
itemIds.add(inventoryItem.getItemId());
|
|
|
346 |
}
|
|
|
347 |
return itemIds;
|
|
|
348 |
}
|
| 23444 |
amit.gupta |
349 |
|
|
|
350 |
private Map<Integer, Set<Scheme>> toItemIdSchemesMap(List<SchemeItem> schemeItems, List<Scheme> schemes) {
|
| 23019 |
ashik.ali |
351 |
Map<Integer, Scheme> schemeIdSchemesMap = this.toSchemeIdSchemeMap(schemes);
|
| 22859 |
ashik.ali |
352 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = new HashMap<>();
|
| 23444 |
amit.gupta |
353 |
for (SchemeItem schemeItem : schemeItems) {
|
|
|
354 |
if (!itemIdSchemesMap.containsKey(schemeItem.getItemId())) {
|
| 22859 |
ashik.ali |
355 |
Set<Scheme> schemesSet = new HashSet<>();
|
|
|
356 |
schemesSet.add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
357 |
itemIdSchemesMap.put(schemeItem.getItemId(), schemesSet);
|
| 23444 |
amit.gupta |
358 |
} else {
|
| 22859 |
ashik.ali |
359 |
itemIdSchemesMap.get(schemeItem.getItemId()).add(schemeIdSchemesMap.get(schemeItem.getSchemeId()));
|
|
|
360 |
}
|
|
|
361 |
}
|
|
|
362 |
return itemIdSchemesMap;
|
|
|
363 |
}
|
| 22653 |
ashik.ali |
364 |
|
| 23444 |
amit.gupta |
365 |
private Map<InventoryItem, Set<Scheme>> toInventoryItemSchemesMap(List<Scheme> schemes,
|
|
|
366 |
List<InventoryItem> inventoryItems) {
|
| 22859 |
ashik.ali |
367 |
Set<Integer> schemeIds = new HashSet<>();
|
| 23444 |
amit.gupta |
368 |
for (Scheme scheme : schemes) {
|
| 22859 |
ashik.ali |
369 |
schemeIds.add(scheme.getId());
|
|
|
370 |
}
|
|
|
371 |
Set<Integer> itemIds = this.inventoryItemsToItemIds(inventoryItems);
|
|
|
372 |
List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIdsAndItemIds(schemeIds, itemIds);
|
| 23444 |
amit.gupta |
373 |
|
| 22859 |
ashik.ali |
374 |
Map<Integer, Set<Scheme>> itemIdSchemesMap = this.toItemIdSchemesMap(schemeItems, schemes);
|
|
|
375 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemsMap = new HashMap<>();
|
| 23444 |
amit.gupta |
376 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
377 |
if (itemIdSchemesMap.containsKey(inventoryItem.getItemId())) {
|
| 22859 |
ashik.ali |
378 |
inventoryItemSchemsMap.put(inventoryItem, itemIdSchemesMap.get(inventoryItem.getItemId()));
|
|
|
379 |
}
|
|
|
380 |
}
|
|
|
381 |
return inventoryItemSchemsMap;
|
|
|
382 |
}
|
| 23444 |
amit.gupta |
383 |
|
| 22859 |
ashik.ali |
384 |
@Override
|
| 23365 |
ashik.ali |
385 |
public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
|
| 23369 |
ashik.ali |
386 |
LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
|
| 23344 |
ashik.ali |
387 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
|
| 25503 |
amit.gupta |
388 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId,
|
|
|
389 |
purchase.getCreateTimestamp().toLocalDate());
|
| 23369 |
ashik.ali |
390 |
LOGGER.info("purchase is completed = {}", (purchase.getCompleteTimestamp() != null));
|
| 23444 |
amit.gupta |
391 |
if (purchase.getCompleteTimestamp() != null) {
|
| 25507 |
amit.gupta |
392 |
List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, partnerType,
|
|
|
393 |
purchase.getCompleteTimestamp());
|
| 23365 |
ashik.ali |
394 |
float totalCashback = 0;
|
| 23444 |
amit.gupta |
395 |
if (schemes.isEmpty()) {
|
| 23365 |
ashik.ali |
396 |
return;
|
|
|
397 |
}
|
|
|
398 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchaseId);
|
| 26332 |
amit.gupta |
399 |
|
|
|
400 |
// Only consider inventory items that were not returned
|
| 26296 |
amit.gupta |
401 |
inventoryItems = inventoryItems.stream().filter(x -> !x.getLastScanType().equals(ScanType.PURCHASE_RET_BAD)
|
|
|
402 |
&& !x.getLastScanType().equals(ScanType.PURCHASE_RET)).collect(Collectors.toList());
|
| 25487 |
amit.gupta |
403 |
LOGGER.info(inventoryItems);
|
| 25438 |
amit.gupta |
404 |
if (inventoryItems.size() == 0)
|
|
|
405 |
return;
|
| 23444 |
amit.gupta |
406 |
Map<InventoryItem, Set<Scheme>> inventoryItemSchemesMap = this.toInventoryItemSchemesMap(schemes,
|
|
|
407 |
inventoryItems);
|
|
|
408 |
|
|
|
409 |
if (inventoryItemSchemesMap.isEmpty()) {
|
| 23365 |
ashik.ali |
410 |
return;
|
|
|
411 |
}
|
|
|
412 |
Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
|
|
|
413 |
Set<Integer> itemIds = new HashSet<>();
|
| 23444 |
amit.gupta |
414 |
|
|
|
415 |
for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
|
| 23365 |
ashik.ali |
416 |
Set<Scheme> allSchemes = new HashSet<>();
|
| 23444 |
amit.gupta |
417 |
for (Scheme scheme : inventoryItemSchemesEntry.getValue()) {
|
| 25503 |
amit.gupta |
418 |
allSchemes.add(scheme);
|
| 22859 |
ashik.ali |
419 |
}
|
| 23365 |
ashik.ali |
420 |
allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
|
|
|
421 |
itemIds.add(inventoryItemSchemesEntry.getKey().getItemId());
|
| 22859 |
ashik.ali |
422 |
}
|
| 23444 |
amit.gupta |
423 |
|
|
|
424 |
int itemsCount = 0;
|
|
|
425 |
for (Map.Entry<InventoryItem, Set<Scheme>> allInventoryItemSchemesEntry : allInventoryItemSchemesMap
|
|
|
426 |
.entrySet()) {
|
|
|
427 |
float inventoryItemCashback = 0;
|
|
|
428 |
for (Scheme scheme : allInventoryItemSchemesEntry.getValue()) {
|
| 23365 |
ashik.ali |
429 |
InventoryItem inventoryItem = allInventoryItemSchemesEntry.getKey();
|
|
|
430 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 23444 |
amit.gupta |
431 |
inventoryItemCashback += cashback;
|
| 23339 |
ashik.ali |
432 |
}
|
| 23444 |
amit.gupta |
433 |
if (inventoryItemCashback > 0) {
|
|
|
434 |
totalCashback += inventoryItemCashback;
|
|
|
435 |
itemsCount++;
|
|
|
436 |
}
|
| 22859 |
ashik.ali |
437 |
}
|
| 23444 |
amit.gupta |
438 |
|
| 23511 |
amit.gupta |
439 |
LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
|
| 23796 |
amit.gupta |
440 |
if (itemsCount > 0) {
|
| 23508 |
amit.gupta |
441 |
walletService.addAmountToWallet(retailerId, purchaseId, WalletReferenceType.SCHEME_IN,
|
| 23796 |
amit.gupta |
442 |
"Added for SCHEME IN against invoice " + purchase.getPurchaseReference() + " (total "
|
|
|
443 |
+ itemsCount + " pcs)",
|
|
|
444 |
totalCashback);
|
|
|
445 |
LOGGER.info("Added Rs.{} for SCHEME IN against invoice {} total pcs({}) {}", totalCashback,
|
|
|
446 |
purchase.getPurchaseReference(), itemsCount);
|
| 23508 |
amit.gupta |
447 |
purchase.setCashback(purchase.getCashback() + totalCashback);
|
|
|
448 |
purchaseRepository.persist(purchase);
|
|
|
449 |
}
|
| 22653 |
ashik.ali |
450 |
}
|
|
|
451 |
}
|
| 23444 |
amit.gupta |
452 |
|
|
|
453 |
private float createSchemeInOut(Scheme scheme, InventoryItem inventoryItem) {
|
| 24562 |
amit.gupta |
454 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByScheme(scheme.getId(), inventoryItem.getId());
|
| 23443 |
amit.gupta |
455 |
float amount = 0;
|
| 24581 |
amit.gupta |
456 |
if (schemeInOuts.stream().filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList())
|
| 24562 |
amit.gupta |
457 |
.size() == 0) {
|
|
|
458 |
SchemeInOut schemeInOut = new SchemeInOut();
|
| 23443 |
amit.gupta |
459 |
amount = this.getAmount(inventoryItem, scheme);
|
|
|
460 |
schemeInOut.setSchemeId(scheme.getId());
|
|
|
461 |
schemeInOut.setInventoryItemId(inventoryItem.getId());
|
|
|
462 |
schemeInOut.setAmount(amount);
|
|
|
463 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
464 |
}
|
| 22859 |
ashik.ali |
465 |
return amount;
|
|
|
466 |
}
|
| 23444 |
amit.gupta |
467 |
|
| 25049 |
amit.gupta |
468 |
// We are maintaining price drop after grn
|
| 23444 |
amit.gupta |
469 |
private float getAmount(InventoryItem inventoryItem, Scheme scheme) {
|
| 22653 |
ashik.ali |
470 |
float amount = 0;
|
| 25049 |
amit.gupta |
471 |
float dpForCalc = 0;
|
|
|
472 |
float taxableSellingPrice = 0;
|
| 22859 |
ashik.ali |
473 |
float totalTaxRate = inventoryItem.getIgstRate() + inventoryItem.getSgstRate() + inventoryItem.getCgstRate();
|
| 23527 |
ashik.ali |
474 |
if (scheme.getAmountType() == AmountType.PERCENTAGE) {
|
| 24562 |
amit.gupta |
475 |
if (scheme.getType().equals(SchemeType.IN)) {
|
| 25049 |
amit.gupta |
476 |
dpForCalc = inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount();
|
| 23995 |
amit.gupta |
477 |
} else {
|
|
|
478 |
try {
|
| 25049 |
amit.gupta |
479 |
dpForCalc = tagListingRepository.selectByItemId(inventoryItem.getItemId()).getSellingPrice();
|
| 24562 |
amit.gupta |
480 |
} catch (Exception e) {
|
| 23995 |
amit.gupta |
481 |
LOGGER.info("Could not find tag Listing entry in {}", inventoryItem.getItemId());
|
|
|
482 |
e.printStackTrace();
|
|
|
483 |
}
|
|
|
484 |
}
|
| 25049 |
amit.gupta |
485 |
taxableSellingPrice = dpForCalc / (1 + totalTaxRate / 100);
|
| 22859 |
ashik.ali |
486 |
amount = taxableSellingPrice * scheme.getAmount() / 100;
|
| 25517 |
amit.gupta |
487 |
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 |
488 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
| 26332 |
amit.gupta |
489 |
scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
|
|
|
490 |
scheme.getAmount(), amount));
|
| 23444 |
amit.gupta |
491 |
} else {
|
| 22653 |
ashik.ali |
492 |
amount = scheme.getAmount();
|
| 25517 |
amit.gupta |
493 |
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 |
494 |
inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
|
| 26332 |
amit.gupta |
495 |
scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
|
|
|
496 |
0, amount));
|
| 22653 |
ashik.ali |
497 |
}
|
| 25049 |
amit.gupta |
498 |
|
| 22859 |
ashik.ali |
499 |
return amount;
|
| 22653 |
ashik.ali |
500 |
}
|
| 23444 |
amit.gupta |
501 |
|
| 22653 |
ashik.ali |
502 |
@Override
|
| 23365 |
ashik.ali |
503 |
public void processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
|
|
|
504 |
FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(retailerId, fofoOrderId);
|
| 23444 |
amit.gupta |
505 |
|
| 25507 |
amit.gupta |
506 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId,
|
|
|
507 |
fofoOrder.getCreateTimestamp().toLocalDate());
|
|
|
508 |
|
| 25043 |
amit.gupta |
509 |
List<ScanRecord> scanRecords = scanRecordRepository.selectAllByOrderId(fofoOrderId);
|
|
|
510 |
Set<Integer> inventoryItemIds = scanRecords.stream().map(x -> x.getInventoryItemId())
|
|
|
511 |
.collect(Collectors.toSet());
|
|
|
512 |
Set<InventoryItem> inventoryItems = inventoryItemRepository.selectByIds(inventoryItemIds).stream()
|
|
|
513 |
.filter(x -> x.getSerialNumber() != null && !x.getSerialNumber().equals(""))
|
|
|
514 |
.collect(Collectors.toSet());
|
| 23444 |
amit.gupta |
515 |
|
| 25043 |
amit.gupta |
516 |
if (inventoryItems.size() == 0) {
|
| 23365 |
ashik.ali |
517 |
return;
|
| 22859 |
ashik.ali |
518 |
}
|
| 23444 |
amit.gupta |
519 |
|
| 25043 |
amit.gupta |
520 |
float totalCashback = 0;
|
|
|
521 |
int count = 0;
|
| 23444 |
amit.gupta |
522 |
|
| 25507 |
amit.gupta |
523 |
Set<Scheme> allFixedSchemes = schemeRepository
|
|
|
524 |
.selectActiveAll(SchemeType.OUT, partnerType, fofoOrder.getCreateTimestamp()).stream()
|
|
|
525 |
.filter(scheme -> scheme.getAmountType().equals(AmountType.FIXED)).collect(Collectors.toSet());
|
| 25050 |
amit.gupta |
526 |
|
| 25043 |
amit.gupta |
527 |
for (InventoryItem inventoryItem : inventoryItems) {
|
| 25710 |
amit.gupta |
528 |
float itemCashback = 0;
|
| 25043 |
amit.gupta |
529 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(inventoryItem.getPurchaseId(), retailerId);
|
|
|
530 |
Set<Integer> schemeIds = new HashSet<>(
|
|
|
531 |
schemeItemRepository.selectSchemeIdByItemId(inventoryItem.getItemId()));
|
| 25507 |
amit.gupta |
532 |
List<Scheme> schemes = null;
|
| 26332 |
amit.gupta |
533 |
if (purchase.getCompleteTimestamp() == null) {
|
| 25579 |
amit.gupta |
534 |
continue;
|
|
|
535 |
}
|
| 25507 |
amit.gupta |
536 |
if (purchase.getCompleteTimestamp().isAfter(LocalDate.of(2019, 9, 1).atStartOfDay())) {
|
|
|
537 |
schemes = schemeRepository.selectActiveAll(SchemeType.OUT, partnerType, fofoOrder.getCreateTimestamp());
|
|
|
538 |
} else {
|
|
|
539 |
schemes = schemeRepository.selectActiveAll(SchemeType.OUT, partnerType,
|
|
|
540 |
purchase.getCompleteTimestamp());
|
|
|
541 |
}
|
|
|
542 |
Set<Scheme> schemesSet = schemes.stream().filter(x -> x.getAmountType().equals(AmountType.PERCENTAGE))
|
|
|
543 |
.collect(Collectors.toSet());
|
|
|
544 |
schemesSet.addAll(allFixedSchemes);
|
|
|
545 |
schemesSet = schemesSet.stream().filter(x -> schemeIds.contains(x.getId())).collect(Collectors.toSet());
|
|
|
546 |
for (Scheme scheme : schemesSet) {
|
| 25043 |
amit.gupta |
547 |
itemCashback += this.createSchemeInOut(scheme, inventoryItem);
|
| 22653 |
ashik.ali |
548 |
}
|
| 25043 |
amit.gupta |
549 |
if (itemCashback > 0) {
|
|
|
550 |
count++;
|
|
|
551 |
totalCashback += itemCashback;
|
| 22859 |
ashik.ali |
552 |
}
|
| 22653 |
ashik.ali |
553 |
}
|
| 25043 |
amit.gupta |
554 |
if (count > 0) {
|
| 23444 |
amit.gupta |
555 |
walletService.addAmountToWallet(retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT,
|
| 23796 |
amit.gupta |
556 |
"Sales margin for invoice number " + fofoOrder.getInvoiceNumber() + ". Total " + count + " pc(s)",
|
|
|
557 |
totalCashback);
|
| 23365 |
ashik.ali |
558 |
fofoOrder.setCashback(totalCashback);
|
|
|
559 |
fofoOrderRepository.persist(fofoOrder);
|
| 22859 |
ashik.ali |
560 |
}
|
| 22653 |
ashik.ali |
561 |
}
|
| 23444 |
amit.gupta |
562 |
|
| 23508 |
amit.gupta |
563 |
@Override
|
| 23796 |
amit.gupta |
564 |
public void rollbackSchemes(List<Integer> inventoryItemIds, int rollbackReference, String rollbackReason)
|
|
|
565 |
throws Exception {
|
| 23638 |
amit.gupta |
566 |
Set<Integer> inventoryItemIdSet = new HashSet<>(inventoryItemIds);
|
| 23508 |
amit.gupta |
567 |
float amountToRollback = 0;
|
|
|
568 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIdSet);
|
| 23796 |
amit.gupta |
569 |
for (SchemeInOut schemeInOut : schemes) {
|
| 23983 |
amit.gupta |
570 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
571 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
572 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
573 |
amountToRollback += schemeInOut.getAmount();
|
|
|
574 |
}
|
| 23508 |
amit.gupta |
575 |
}
|
| 23983 |
amit.gupta |
576 |
if (amountToRollback > 0) {
|
| 23638 |
amit.gupta |
577 |
int inventoryItemId = inventoryItemIds.get(0);
|
|
|
578 |
Integer fofoId = inventoryItemRepository.selectById(inventoryItemId).getFofoId();
|
| 23796 |
amit.gupta |
579 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, rollbackReference,
|
|
|
580 |
WalletReferenceType.SCHEME_IN, rollbackReason);
|
| 23638 |
amit.gupta |
581 |
}
|
| 23508 |
amit.gupta |
582 |
}
|
| 23884 |
amit.gupta |
583 |
|
| 23781 |
ashik.ali |
584 |
@Override
|
| 23796 |
amit.gupta |
585 |
public Map<String, Object> getSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
586 |
throws ProfitMandiBusinessException {
|
| 23781 |
ashik.ali |
587 |
Map<String, Object> map = new HashMap<>();
|
|
|
588 |
List<Scheme> schemes = null;
|
|
|
589 |
long size = 0;
|
| 23798 |
amit.gupta |
590 |
if (roleManager.isAdmin(roleIds)) {
|
| 23781 |
ashik.ali |
591 |
schemes = schemeRepository.selectAll(offset, limit);
|
|
|
592 |
size = schemeRepository.selectAllCount();
|
| 23796 |
amit.gupta |
593 |
} else {
|
| 23781 |
ashik.ali |
594 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
595 |
size = schemeRepository.selectAllActiveCount();
|
|
|
596 |
}
|
|
|
597 |
map.put("schemes", schemes);
|
|
|
598 |
map.put("start", offset + 1);
|
|
|
599 |
map.put("size", size);
|
| 23796 |
amit.gupta |
600 |
if (schemes.size() < limit) {
|
| 23781 |
ashik.ali |
601 |
map.put("end", offset + schemes.size());
|
| 23796 |
amit.gupta |
602 |
} else {
|
| 23781 |
ashik.ali |
603 |
map.put("end", offset + limit);
|
|
|
604 |
}
|
|
|
605 |
return map;
|
|
|
606 |
}
|
| 23796 |
amit.gupta |
607 |
|
| 23781 |
ashik.ali |
608 |
@Override
|
| 23796 |
amit.gupta |
609 |
public List<Scheme> getPaginatedSchemes(Set<Integer> roleIds, int offset, int limit)
|
|
|
610 |
throws ProfitMandiBusinessException {
|
| 23781 |
ashik.ali |
611 |
LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
|
|
|
612 |
List<Scheme> schemes = null;
|
| 23798 |
amit.gupta |
613 |
if (roleManager.isAdmin(roleIds)) {
|
| 23781 |
ashik.ali |
614 |
schemes = schemeRepository.selectAll(offset, limit);
|
| 23796 |
amit.gupta |
615 |
} else {
|
| 23781 |
ashik.ali |
616 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
|
|
617 |
}
|
|
|
618 |
return schemes;
|
|
|
619 |
}
|
| 23508 |
amit.gupta |
620 |
|
| 23968 |
amit.gupta |
621 |
@Override
|
| 24976 |
amit.gupta |
622 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int priceDropId, String reversalReason)
|
| 26332 |
amit.gupta |
623 |
throws ProfitMandiBusinessException {
|
| 24976 |
amit.gupta |
624 |
Map<Integer, List<InventoryItem>> purchaseInventoryListMap = inventoryItems.stream()
|
|
|
625 |
.collect(Collectors.groupingBy(InventoryItem::getPurchaseId, Collectors.toList()));
|
|
|
626 |
|
|
|
627 |
for (Map.Entry<Integer, List<InventoryItem>> purchaseEntry : purchaseInventoryListMap.entrySet()) {
|
|
|
628 |
float amountToCredit = 0;
|
|
|
629 |
float amountToDebit = 0;
|
|
|
630 |
int purchaseId = purchaseEntry.getKey();
|
|
|
631 |
List<InventoryItem> purchaseInventoryItemList = purchaseEntry.getValue();
|
|
|
632 |
|
|
|
633 |
Map<Integer, InventoryItem> inventoryItemsMap = purchaseInventoryItemList.stream()
|
|
|
634 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
635 |
|
|
|
636 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
|
|
637 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
638 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size())
|
|
|
639 |
.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
640 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
641 |
InventoryItem ii = inventoryItemsMap.get(schemeInOut.getInventoryItemId());
|
|
|
642 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
|
|
643 |
if (scheme.getType().equals(SchemeType.OUT))
|
|
|
644 |
continue;
|
|
|
645 |
if (scheme.getAmountType().equals(AmountType.FIXED)) {
|
|
|
646 |
continue;
|
| 23995 |
amit.gupta |
647 |
}
|
| 24976 |
amit.gupta |
648 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
|
|
649 |
float newAmount = getAmount(ii, scheme);
|
|
|
650 |
if (schemeInOut.getAmount() - newAmount >= 0.01f) {
|
|
|
651 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
652 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
653 |
SchemeInOut sioNew = new SchemeInOut();
|
|
|
654 |
sioNew.setAmount(newAmount);
|
|
|
655 |
sioNew.setInventoryItemId(schemeInOut.getInventoryItemId());
|
|
|
656 |
sioNew.setSchemeId(schemeInOut.getSchemeId());
|
|
|
657 |
schemeInOutRepository.persist(sioNew);
|
|
|
658 |
amountToCredit += sioNew.getAmount();
|
|
|
659 |
amountToDebit += schemeInOut.getAmount();
|
|
|
660 |
}
|
| 24562 |
amit.gupta |
661 |
|
| 24976 |
amit.gupta |
662 |
}
|
| 23986 |
amit.gupta |
663 |
}
|
| 24976 |
amit.gupta |
664 |
int fofoId = inventoryItems.get(0).getFofoId();
|
|
|
665 |
if (amountToDebit > 0) {
|
|
|
666 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN, reversalReason,
|
|
|
667 |
-amountToDebit);
|
|
|
668 |
}
|
|
|
669 |
if (amountToCredit > 0) {
|
|
|
670 |
walletService.addAmountToWallet(fofoId, purchaseId, WalletReferenceType.SCHEME_IN, reversalReason,
|
|
|
671 |
amountToCredit);
|
|
|
672 |
}
|
| 23968 |
amit.gupta |
673 |
}
|
|
|
674 |
}
|
| 24562 |
amit.gupta |
675 |
|
| 24264 |
amit.gupta |
676 |
@Override
|
| 24562 |
amit.gupta |
677 |
public void reverseSchemes(List<InventoryItem> inventoryItems, int reversalReference, String reversalReason,
|
|
|
678 |
SchemeType schemeType) throws ProfitMandiBusinessException {
|
| 24264 |
amit.gupta |
679 |
Map<Integer, InventoryItem> inventoryItemsMap = inventoryItems.stream()
|
|
|
680 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
681 |
List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(inventoryItemsMap.keySet());
|
|
|
682 |
List<Integer> schemeIds = schemeInOuts.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
|
|
|
683 |
float amount = 0;
|
|
|
684 |
Map<Integer, Scheme> schemesMap = schemeRepository.selectBySchemeIds(schemeIds, 0, schemeIds.size()).stream()
|
|
|
685 |
.collect(Collectors.toMap(x -> x.getId(), x -> x));
|
|
|
686 |
for (SchemeInOut schemeInOut : schemeInOuts) {
|
|
|
687 |
Scheme scheme = schemesMap.get(schemeInOut.getSchemeId());
|
| 24562 |
amit.gupta |
688 |
if (scheme.getType().equals(schemeType)) {
|
|
|
689 |
if (schemeInOut.getRolledBackTimestamp() == null) {
|
| 24264 |
amit.gupta |
690 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
|
|
691 |
schemeInOutRepository.persist(schemeInOut);
|
|
|
692 |
amount += schemeInOut.getAmount();
|
|
|
693 |
}
|
|
|
694 |
}
|
|
|
695 |
}
|
|
|
696 |
int fofoId = inventoryItems.get(0).getFofoId();
|
| 24562 |
amit.gupta |
697 |
WalletReferenceType walletReferenceType = schemeType.equals(SchemeType.OUT) ? WalletReferenceType.SCHEME_OUT
|
|
|
698 |
: WalletReferenceType.SCHEME_IN;
|
|
|
699 |
if (amount > 0) {
|
|
|
700 |
walletService.rollbackAmountFromWallet(fofoId, amount, reversalReference, walletReferenceType,
|
|
|
701 |
reversalReason);
|
| 24268 |
amit.gupta |
702 |
}
|
| 24264 |
amit.gupta |
703 |
}
|
| 23968 |
amit.gupta |
704 |
|
| 26332 |
amit.gupta |
705 |
@Override
|
|
|
706 |
public double getTotalMargin(int itemId, PartnerType partnerType, LocalDateTime dateTime) {
|
|
|
707 |
Session session = sessionFactory.getCurrentSession();
|
|
|
708 |
CriteriaBuilder cb = session.getCriteriaBuilder();
|
|
|
709 |
CriteriaQuery<Double> criteriaQuery = cb.createQuery(Double.class);
|
|
|
710 |
Root<SchemeItem> schemeItem = criteriaQuery.from(SchemeItem.class);
|
|
|
711 |
Root<Scheme> scheme = criteriaQuery.from(Scheme.class);
|
|
|
712 |
Predicate schemePredicate = cb.equal(scheme.get(ProfitMandiConstants.AMOUNT_TYPE), AmountType.PERCENTAGE);
|
|
|
713 |
Predicate lessThanPredicate = cb.lessThanOrEqualTo(scheme.get(ProfitMandiConstants.END_DATE_TIME), dateTime);
|
|
|
714 |
Predicate greaterThanPredicate = cb.greaterThanOrEqualTo(scheme.get(ProfitMandiConstants.START_DATE_TIME),
|
|
|
715 |
dateTime);
|
|
|
716 |
Predicate joinPredicate = cb.equal(scheme.get("id"), schemeItem.get("schemeId"));
|
|
|
717 |
Predicate schemeItemPredicate = cb.equal(schemeItem.get(ProfitMandiConstants.ITEM_ID), itemId);
|
|
|
718 |
criteriaQuery.select(cb.sum(scheme.get(ProfitMandiConstants.AMOUNT))).where(schemePredicate, lessThanPredicate,
|
|
|
719 |
greaterThanPredicate, schemeItemPredicate, joinPredicate);
|
|
|
720 |
|
|
|
721 |
Query<Double> query = session.createQuery(criteriaQuery);
|
|
|
722 |
return query.getSingleResult() + ProfitMandiConstants.SCHEME_INVESTMENT_MARGIN;
|
|
|
723 |
|
|
|
724 |
}
|
|
|
725 |
|
| 24976 |
amit.gupta |
726 |
/*
|
|
|
727 |
* @Override public void updateSchmesForModel(int catalogId) throws
|
|
|
728 |
* ProfitMandiBusinessException { List<Item> items =
|
|
|
729 |
* itemRepository.selectAllByCatalogItemId(catalogId); Map<Integer, Scheme>
|
|
|
730 |
* schemes = schemeRepository .selectAllByItemIds(items.stream().map(x ->
|
|
|
731 |
* x.getId()).collect(Collectors.toList()));
|
|
|
732 |
*
|
|
|
733 |
* }
|
|
|
734 |
*/
|
| 24562 |
amit.gupta |
735 |
|
| 22653 |
ashik.ali |
736 |
}
|