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