| 33172 |
tejus.loha |
1 |
package com.spice.profitmandi.service.order;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
4 |
import com.spice.profitmandi.common.model.BulkOrderModel;
|
|
|
5 |
import com.spice.profitmandi.common.util.ExcelUtils;
|
|
|
6 |
import com.spice.profitmandi.dao.cart.CartService;
|
|
|
7 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 33338 |
amit.gupta |
8 |
import com.spice.profitmandi.dao.entity.transaction.SDCreditRequirement;
|
| 33213 |
tejus.loha |
9 |
import com.spice.profitmandi.dao.entity.transaction.Transaction;
|
|
|
10 |
import com.spice.profitmandi.dao.entity.transaction.TransactionApproval;
|
|
|
11 |
import com.spice.profitmandi.dao.enumuration.transaction.TransactionApprovalStatus;
|
| 33172 |
tejus.loha |
12 |
import com.spice.profitmandi.dao.model.CartItem;
|
|
|
13 |
import com.spice.profitmandi.dao.model.UserCart;
|
|
|
14 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
|
|
15 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
| 33338 |
amit.gupta |
16 |
import com.spice.profitmandi.dao.repository.transaction.SDCreditRequirementRepository;
|
| 33213 |
tejus.loha |
17 |
import com.spice.profitmandi.dao.repository.transaction.TransactionApprovalRepository;
|
|
|
18 |
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
|
|
|
19 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
| 33338 |
amit.gupta |
20 |
import com.spice.profitmandi.service.transaction.SDCreditService;
|
| 33172 |
tejus.loha |
21 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
|
|
22 |
import com.spice.profitmandi.service.wallet.CommonPaymentService;
|
|
|
23 |
import com.spice.profitmandi.service.wallet.WalletService;
|
|
|
24 |
import org.apache.logging.log4j.LogManager;
|
|
|
25 |
import org.apache.logging.log4j.Logger;
|
| 33213 |
tejus.loha |
26 |
import org.apache.poi.ss.usermodel.Cell;
|
| 33172 |
tejus.loha |
27 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
28 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
29 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
30 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
31 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 33213 |
tejus.loha |
32 |
import org.springframework.stereotype.Service;
|
|
|
33 |
import org.springframework.transaction.annotation.Transactional;
|
| 33172 |
tejus.loha |
34 |
import org.springframework.web.multipart.MultipartFile;
|
|
|
35 |
|
| 33338 |
amit.gupta |
36 |
import java.math.BigDecimal;
|
| 33172 |
tejus.loha |
37 |
import java.util.ArrayList;
|
|
|
38 |
import java.util.List;
|
|
|
39 |
import java.util.Map;
|
|
|
40 |
import java.util.stream.Collectors;
|
|
|
41 |
|
| 33213 |
tejus.loha |
42 |
@Service
|
|
|
43 |
@Transactional(rollbackFor = Throwable.class)
|
| 33172 |
tejus.loha |
44 |
public class BulkOrderService {
|
|
|
45 |
private static final Logger LOGGER = LogManager.getLogger(BulkOrderService.class);
|
|
|
46 |
@Autowired
|
|
|
47 |
CartService cartService;
|
|
|
48 |
@Autowired
|
|
|
49 |
TransactionService transactionService;
|
|
|
50 |
@Autowired
|
|
|
51 |
CommonPaymentService commonPaymentService;
|
|
|
52 |
@Autowired
|
|
|
53 |
TagListingRepository tagListingRepository;
|
|
|
54 |
@Autowired
|
|
|
55 |
WalletService walletService;
|
|
|
56 |
@Autowired
|
|
|
57 |
UserRepository userRepository;
|
| 33213 |
tejus.loha |
58 |
@Autowired
|
|
|
59 |
TransactionRepository transactionRepository;
|
|
|
60 |
@Autowired
|
|
|
61 |
TransactionApprovalRepository transactionApprovalRepository;
|
|
|
62 |
@Autowired
|
|
|
63 |
AddressRepository addressRepository;
|
|
|
64 |
//TODO:Tejus need to check
|
| 33172 |
tejus.loha |
65 |
|
| 33338 |
amit.gupta |
66 |
@Autowired
|
|
|
67 |
SDCreditService sdCreditService;
|
|
|
68 |
|
|
|
69 |
|
| 33213 |
tejus.loha |
70 |
public void parseBulkOrders(MultipartFile file, int creatorId) throws Exception {
|
| 33172 |
tejus.loha |
71 |
XSSFWorkbook myWorkBook = new XSSFWorkbook(file.getInputStream());
|
|
|
72 |
|
|
|
73 |
myWorkBook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
|
|
74 |
// Return first sheet from the XLSX workbook
|
|
|
75 |
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
|
|
|
76 |
LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
|
|
|
77 |
List<BulkOrderModel> bulkOrderModels = new ArrayList<>();
|
|
|
78 |
for (int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++) {
|
|
|
79 |
XSSFRow row = mySheet.getRow(rowNumber);
|
|
|
80 |
BulkOrderModel bulkOrderModel = this.createBulkModel(row);
|
|
|
81 |
bulkOrderModels.add(bulkOrderModel);
|
|
|
82 |
}
|
|
|
83 |
Map<Integer, List<BulkOrderModel>> fofoBulkOrdersMap = bulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getFofoId()));
|
| 33213 |
tejus.loha |
84 |
boolean approvalNotRequired = true;
|
| 33172 |
tejus.loha |
85 |
for (Map.Entry<Integer, List<BulkOrderModel>> fofoBulkOrderEntry : fofoBulkOrdersMap.entrySet()) {
|
|
|
86 |
int fofoId = fofoBulkOrderEntry.getKey();
|
|
|
87 |
List<BulkOrderModel> fofoBulkOrderModels = fofoBulkOrderEntry.getValue();
|
|
|
88 |
Map<Integer, Long> orderItemCountMap = fofoBulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getItemId(), Collectors.counting()));
|
|
|
89 |
|
|
|
90 |
if (orderItemCountMap.entrySet().stream().filter(x -> x.getValue() > 1).count() > 0) {
|
|
|
91 |
throw new ProfitMandiBusinessException("Fofo ID", fofoId, "Duplicate in items");
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
boolean hasZeroQuantity = fofoBulkOrderModels.stream().filter(x -> x.getQuantity() <= 0).count() > 0;
|
|
|
95 |
if (hasZeroQuantity) {
|
|
|
96 |
throw new ProfitMandiBusinessException("Item Quantity", "", "Should be greater than 0");
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
List<CartItem> cartItems = new ArrayList<>();
|
| 33338 |
amit.gupta |
100 |
double totalPayableAmount = 0;
|
| 33172 |
tejus.loha |
101 |
for (BulkOrderModel fofoBulkOrderModel : fofoBulkOrderModels) {
|
|
|
102 |
CartItem cartItem = new CartItem();
|
|
|
103 |
cartItem.setQuantity(fofoBulkOrderModel.getQuantity());
|
|
|
104 |
cartItem.setItemId(fofoBulkOrderModel.getItemId());
|
| 33213 |
tejus.loha |
105 |
boolean isPriceZero = fofoBulkOrderModel.getItemPrice() == 0.0;
|
| 33172 |
tejus.loha |
106 |
TagListing tagListing = tagListingRepository.selectByItemId(fofoBulkOrderModel.getItemId());
|
| 33213 |
tejus.loha |
107 |
double itemSellingPrice = tagListing.getSellingPrice();
|
|
|
108 |
double customSellingPrice = fofoBulkOrderModel.getItemPrice();
|
|
|
109 |
int itemId = cartItem.getItemId();
|
|
|
110 |
if (isPriceZero) {
|
|
|
111 |
cartItem.setSellingPrice(itemSellingPrice);
|
|
|
112 |
} else {
|
| 33216 |
tejus.loha |
113 |
if (customSellingPrice <= tagListing.getMrp() || customSellingPrice <= tagListing.getMop()) {
|
| 33213 |
tejus.loha |
114 |
cartItem.setSellingPrice(customSellingPrice);
|
|
|
115 |
approvalNotRequired = false;
|
|
|
116 |
|
|
|
117 |
} else {
|
|
|
118 |
throw new ProfitMandiBusinessException("Given price is greater than selling price for item Id - ", itemId, " it should be less or equal of DP");
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
}
|
| 33338 |
amit.gupta |
122 |
totalPayableAmount += cartItem.getSellingPrice() * cartItem.getQuantity();
|
| 33172 |
tejus.loha |
123 |
cartItems.add(cartItem);
|
|
|
124 |
}
|
| 33338 |
amit.gupta |
125 |
LOGGER.info("totalAmount of item " + totalPayableAmount);
|
| 33172 |
tejus.loha |
126 |
double walletAmount = walletService.getWalletAmount(fofoId);
|
| 33338 |
amit.gupta |
127 |
|
|
|
128 |
BigDecimal creditAvailability = sdCreditService.getAvailableAmount(fofoId);
|
|
|
129 |
|
|
|
130 |
double netAmountInHand = creditAvailability.doubleValue() + walletAmount;
|
|
|
131 |
if (totalPayableAmount > 20 && walletAmount < netAmountInHand) {
|
| 33213 |
tejus.loha |
132 |
throw new ProfitMandiBusinessException("Skippin order due to insufficent balance for id - ", fofoId, " ,Check wallet Balance ones");
|
| 33172 |
tejus.loha |
133 |
}
|
|
|
134 |
UserCart userCart = cartService.setCartItems(fofoId, cartItems);
|
| 33213 |
tejus.loha |
135 |
// createtransactionInternally set the value in transaction table
|
| 33338 |
amit.gupta |
136 |
|
|
|
137 |
double creditAmountRequired = totalPayableAmount - walletAmount - 20;
|
|
|
138 |
if (creditAmountRequired > 0d) {
|
|
|
139 |
sdCreditService.createLoan(userCart.getUserId(), creditAmountRequired, 0);
|
|
|
140 |
}
|
|
|
141 |
//return this.createLoan(userCart.getUserId(), creditAmountRequired);
|
|
|
142 |
int transactionId = transactionService.createTransactionInternally(userCart, totalPayableAmount, 0);
|
| 33213 |
tejus.loha |
143 |
//Set here created by
|
|
|
144 |
Transaction transaction = transactionRepository.selectById(transactionId);
|
|
|
145 |
transaction.setCreatedBy(creatorId);
|
|
|
146 |
LOGGER.info("transaction created by {}", transaction.getCreatedBy());
|
| 33172 |
tejus.loha |
147 |
commonPaymentService.payThroughWallet(transactionId);
|
| 33213 |
tejus.loha |
148 |
if (approvalNotRequired) {
|
|
|
149 |
transactionService.processTransaction(transactionId);
|
|
|
150 |
} else {
|
|
|
151 |
this.createApproval(transactionId);
|
|
|
152 |
}
|
|
|
153 |
|
| 33172 |
tejus.loha |
154 |
}
|
| 33213 |
tejus.loha |
155 |
|
| 33172 |
tejus.loha |
156 |
}
|
|
|
157 |
|
| 33213 |
tejus.loha |
158 |
public void createApproval(int transactionId) {
|
|
|
159 |
TransactionApproval transactionApproval = new TransactionApproval();
|
|
|
160 |
transactionApproval.setId(transactionId);
|
|
|
161 |
transactionApproval.setStatus(TransactionApprovalStatus.PENDING);
|
|
|
162 |
transactionApprovalRepository.persist(transactionApproval);
|
|
|
163 |
}
|
|
|
164 |
|
| 33172 |
tejus.loha |
165 |
private BulkOrderModel createBulkModel(XSSFRow row) throws ProfitMandiBusinessException {
|
|
|
166 |
BulkOrderModel bulkOrderModel = new BulkOrderModel();
|
|
|
167 |
int i = 0;
|
|
|
168 |
bulkOrderModel.setRowIndex(row.getRowNum());
|
|
|
169 |
try {
|
| 33213 |
tejus.loha |
170 |
Cell partneranme = row.getCell(i++);
|
|
|
171 |
if (partneranme == null)
|
|
|
172 |
bulkOrderModel.setPartnerName("");
|
|
|
173 |
else
|
|
|
174 |
bulkOrderModel.setPartnerName(partneranme.getStringCellValue().trim());
|
|
|
175 |
Cell description = row.getCell(i++);
|
|
|
176 |
if (description == null)
|
|
|
177 |
bulkOrderModel.setDescription("");
|
|
|
178 |
else
|
|
|
179 |
bulkOrderModel.setDescription(description.getStringCellValue().trim());
|
|
|
180 |
|
| 33172 |
tejus.loha |
181 |
bulkOrderModel.setFofoId((int) row.getCell(i++).getNumericCellValue());
|
|
|
182 |
bulkOrderModel.setItemId((int) row.getCell(i++).getNumericCellValue());
|
| 33213 |
tejus.loha |
183 |
bulkOrderModel.setItemPrice(row.getCell(i++).getNumericCellValue());
|
| 33172 |
tejus.loha |
184 |
bulkOrderModel.setQuantity((int) row.getCell(i++).getNumericCellValue());
|
|
|
185 |
} catch (Throwable e) {
|
|
|
186 |
LOGGER.info(e.getCause());
|
| 33213 |
tejus.loha |
187 |
throw new ProfitMandiBusinessException("Field", "Field at row - " + row.getRowNum() + ", column - " + (i - 1), "Invalid field value at - " + ExcelUtils.toAlphabet(i - 1) + (row.getRowNum() + 1) + ", " + ExcelUtils.getCellValue(row.getCell(i - 1)));
|
| 33172 |
tejus.loha |
188 |
}
|
|
|
189 |
LOGGER.info(bulkOrderModel);
|
|
|
190 |
return bulkOrderModel;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
}
|