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