| Line 57... |
Line 57... |
| 57 |
import org.springframework.stereotype.Service;
|
57 |
import org.springframework.stereotype.Service;
|
| 58 |
import org.springframework.web.multipart.MultipartFile;
|
58 |
import org.springframework.web.multipart.MultipartFile;
|
| 59 |
|
59 |
|
| 60 |
import java.math.BigDecimal;
|
60 |
import java.math.BigDecimal;
|
| 61 |
import java.time.LocalDateTime;
|
61 |
import java.time.LocalDateTime;
|
| 62 |
import java.util.ArrayList;
|
- |
|
| 63 |
import java.util.List;
|
- |
|
| 64 |
import java.util.Map;
|
- |
|
| 65 |
import java.util.Set;
|
62 |
import java.util.*;
|
| 66 |
import java.util.stream.Collectors;
|
63 |
import java.util.stream.Collectors;
|
| 67 |
|
64 |
|
| 68 |
@Service
|
65 |
@Service
|
| 69 |
public class BulkOrderService {
|
66 |
public class BulkOrderService {
|
| 70 |
private static final Logger LOGGER = LogManager.getLogger(BulkOrderService.class);
|
67 |
private static final Logger LOGGER = LogManager.getLogger(BulkOrderService.class);
|
| Line 298... |
Line 295... |
| 298 |
//Set here created by
|
295 |
//Set here created by
|
| 299 |
Transaction transaction = transactionRepository.selectById(transactionId);
|
296 |
Transaction transaction = transactionRepository.selectById(transactionId);
|
| 300 |
transaction.setCreatedBy(creatorId);
|
297 |
transaction.setCreatedBy(creatorId);
|
| 301 |
LOGGER.info("transaction created by {}", transaction.getCreatedBy());
|
298 |
LOGGER.info("transaction created by {}", transaction.getCreatedBy());
|
| 302 |
commonPaymentService.payThroughWallet(transactionId);
|
299 |
commonPaymentService.payThroughWallet(transactionId);
|
| - |
|
300 |
|
| - |
|
301 |
// First PO always requires approval
|
| - |
|
302 |
if (isFirstPO) {
|
| - |
|
303 |
approvalRequired = true;
|
| - |
|
304 |
}
|
| - |
|
305 |
|
| 303 |
if (approvalRequired) {
|
306 |
if (approvalRequired) {
|
| 304 |
this.createApproval(transactionId);
|
307 |
this.createApproval(transactionId, isFirstPO);
|
| 305 |
if (loanId > 0) {
|
308 |
if (loanId > 0) {
|
| 306 |
LoanTransaction loanTransaction = new LoanTransaction();
|
309 |
LoanTransaction loanTransaction = new LoanTransaction();
|
| 307 |
loanTransaction.setLoanId(loanId);
|
310 |
loanTransaction.setLoanId(loanId);
|
| 308 |
loanTransaction.setTransactionId(transactionId);
|
311 |
loanTransaction.setTransactionId(transactionId);
|
| 309 |
loanTransactionRepository.persist(loanTransaction);
|
312 |
loanTransactionRepository.persist(loanTransaction);
|
| 310 |
}
|
313 |
}
|
| 311 |
} else {
|
314 |
} else {
|
| 312 |
transactionService.processTransaction(transactionId, loanId);
|
315 |
transactionService.processTransaction(transactionId, loanId);
|
| 313 |
}
|
316 |
}
|
| 314 |
|
317 |
|
| 315 |
// Send approval email to Sales L3 only for first PO
|
318 |
// Send approval email and track timeline for first PO
|
| 316 |
if (isFirstPO) {
|
319 |
if (isFirstPO) {
|
| 317 |
try {
|
320 |
try {
|
| 318 |
sendFirstPOApprovalEmail(fofoStore, transactionId, totalPayableAmount, creatorId);
|
321 |
sendFirstPOApprovalEmail(fofoStore, transactionId, totalPayableAmount, creatorId);
|
| 319 |
} catch (Exception e) {
|
322 |
} catch (Exception e) {
|
| 320 |
LOGGER.error("Failed to send first PO approval email for fofoId: " + fofoId, e);
|
323 |
LOGGER.error("Failed to send first PO approval email for fofoId: " + fofoId, e);
|
| Line 324... |
Line 327... |
| 324 |
if (fofoStore != null && fofoStore.getCode() != null) {
|
327 |
if (fofoStore != null && fofoStore.getCode() != null) {
|
| 325 |
com.spice.profitmandi.dao.entity.fofo.PartnerOnBoardingPanel pob =
|
328 |
com.spice.profitmandi.dao.entity.fofo.PartnerOnBoardingPanel pob =
|
| 326 |
partnerOnBoardingPanelRepository.selectByCode(fofoStore.getCode());
|
329 |
partnerOnBoardingPanelRepository.selectByCode(fofoStore.getCode());
|
| 327 |
if (pob != null) {
|
330 |
if (pob != null) {
|
| 328 |
storeTimelineTatService.onPoCreationComplete(pob.getId());
|
331 |
storeTimelineTatService.onPoCreationComplete(pob.getId());
|
| 329 |
// If no approval required, PO is auto-approved
|
- |
|
| 330 |
if (!approvalRequired) {
|
- |
|
| 331 |
storeTimelineTatService.onPoApprovalComplete(pob.getId());
|
- |
|
| 332 |
}
|
- |
|
| 333 |
}
|
332 |
}
|
| 334 |
}
|
333 |
}
|
| 335 |
} catch (Exception e) {
|
334 |
} catch (Exception e) {
|
| 336 |
LOGGER.error("Failed to track PO_CREATION timeline for fofoId: " + fofoId, e);
|
335 |
LOGGER.error("Failed to track PO_CREATION timeline for fofoId: " + fofoId, e);
|
| 337 |
}
|
336 |
}
|
| Line 340... |
Line 339... |
| 340 |
|
339 |
|
| 341 |
}
|
340 |
}
|
| 342 |
return finalBidStatus;
|
341 |
return finalBidStatus;
|
| 343 |
}
|
342 |
}
|
| 344 |
|
343 |
|
| 345 |
public void createApproval(int transactionId) {
|
344 |
public void createApproval(int transactionId, boolean firstPo) {
|
| 346 |
TransactionApproval transactionApproval = new TransactionApproval();
|
345 |
TransactionApproval transactionApproval = new TransactionApproval();
|
| 347 |
transactionApproval.setId(transactionId);
|
346 |
transactionApproval.setId(transactionId);
|
| 348 |
transactionApproval.setStatus(TransactionApprovalStatus.PENDING);
|
347 |
transactionApproval.setStatus(TransactionApprovalStatus.PENDING);
|
| - |
|
348 |
transactionApproval.setFirstPo(firstPo);
|
| 349 |
transactionApprovalRepository.persist(transactionApproval);
|
349 |
transactionApprovalRepository.persist(transactionApproval);
|
| 350 |
}
|
350 |
}
|
| 351 |
|
351 |
|
| 352 |
private BulkOrderModel createBulkModel(XSSFRow row) throws ProfitMandiBusinessException {
|
352 |
private BulkOrderModel createBulkModel(XSSFRow row) throws ProfitMandiBusinessException {
|
| 353 |
BulkOrderModel bulkOrderModel = new BulkOrderModel();
|
353 |
BulkOrderModel bulkOrderModel = new BulkOrderModel();
|
| Line 384... |
Line 384... |
| 384 |
List<TransactionApprovalModel> approvalModelList = new ArrayList<>();
|
384 |
List<TransactionApprovalModel> approvalModelList = new ArrayList<>();
|
| 385 |
for (TransactionApproval transactionApproval : transactionApprovals) {
|
385 |
for (TransactionApproval transactionApproval : transactionApprovals) {
|
| 386 |
List<Order> orderList = orderRepository.selectAllByTransactionId(transactionApproval.getId());
|
386 |
List<Order> orderList = orderRepository.selectAllByTransactionId(transactionApproval.getId());
|
| 387 |
Transaction transaction = transactionRepository.selectById(transactionApproval.getId());
|
387 |
Transaction transaction = transactionRepository.selectById(transactionApproval.getId());
|
| 388 |
List<LineItemModel> lineItemModelList = new ArrayList<>();
|
388 |
List<LineItemModel> lineItemModelList = new ArrayList<>();
|
| - |
|
389 |
|
| - |
|
390 |
// Fetch stock availability for all items in this order
|
| - |
|
391 |
Map<Integer, List<com.spice.profitmandi.model.WarehouseItemQtyModel>> stockMap = Collections.emptyMap();
|
| - |
|
392 |
try {
|
| - |
|
393 |
int retailerId = transaction.getRetailerId();
|
| - |
|
394 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
|
| - |
|
395 |
if (fofoStore != null) {
|
| - |
|
396 |
List<Integer> itemIds = orderList.stream()
|
| - |
|
397 |
.map(o -> o.getLineItem().getItemId())
|
| - |
|
398 |
.collect(java.util.stream.Collectors.toList());
|
| - |
|
399 |
stockMap = orderRepository.getItemAvailability(fofoStore.getWarehouseId(), itemIds);
|
| - |
|
400 |
}
|
| - |
|
401 |
} catch (Exception e) {
|
| - |
|
402 |
LOGGER.error("Failed to fetch stock availability for transactionId: {}", transactionApproval.getId(), e);
|
| - |
|
403 |
}
|
| - |
|
404 |
|
| 389 |
for (Order order : orderList) {
|
405 |
for (Order order : orderList) {
|
| 390 |
LineItem lineItem = order.getLineItem();
|
406 |
LineItem lineItem = order.getLineItem();
|
| 391 |
LineItemModel lineItemModel = new LineItemModel();
|
407 |
LineItemModel lineItemModel = new LineItemModel();
|
| 392 |
lineItemModel.setItemId(lineItem.getItemId());
|
408 |
lineItemModel.setItemId(lineItem.getItemId());
|
| 393 |
lineItemModel.setItemName(lineItem.getItem().getItemDescription());
|
409 |
lineItemModel.setItemName(lineItem.getItem().getItemDescription());
|
| 394 |
lineItemModel.setItemQuantity(lineItem.getQuantity());
|
410 |
lineItemModel.setItemQuantity(lineItem.getQuantity());
|
| 395 |
lineItemModel.setSellingPrice(lineItem.getUnitPrice());
|
411 |
lineItemModel.setSellingPrice(lineItem.getUnitPrice());
|
| 396 |
lineItemModel.setDp(tagListingRepository.selectByItemId(lineItem.getItemId()).getSellingPrice());
|
412 |
lineItemModel.setDp(tagListingRepository.selectByItemId(lineItem.getItemId()).getSellingPrice());
|
| - |
|
413 |
// Set available stock (show 0 if negative — negative means over-committed)
|
| - |
|
414 |
List<com.spice.profitmandi.model.WarehouseItemQtyModel> itemStock = stockMap.get(lineItem.getItemId());
|
| - |
|
415 |
if (itemStock != null && !itemStock.isEmpty()) {
|
| - |
|
416 |
int totalAvailable = itemStock.stream().mapToInt(s -> s.getNetAvailability()).sum();
|
| - |
|
417 |
lineItemModel.setAvailableStock(Math.max(0, totalAvailable));
|
| - |
|
418 |
}
|
| 397 |
lineItemModelList.add(lineItemModel);
|
419 |
lineItemModelList.add(lineItemModel);
|
| 398 |
}
|
420 |
}
|
| 399 |
AuthUser authUser = authRepository.selectById(transaction.getCreatedBy());
|
421 |
AuthUser authUser = authRepository.selectById(transaction.getCreatedBy());
|
| 400 |
TransactionApprovalModel transactionApprovalModel = new TransactionApprovalModel();
|
422 |
TransactionApprovalModel transactionApprovalModel = new TransactionApprovalModel();
|
| 401 |
String retailerName = " ";
|
423 |
String retailerName = " ";
|
| Line 407... |
Line 429... |
| 407 |
transactionApprovalModel.setCreatedBy(authUser.getFullName());
|
429 |
transactionApprovalModel.setCreatedBy(authUser.getFullName());
|
| 408 |
}
|
430 |
}
|
| 409 |
transactionApprovalModel.setCreatedOn(transaction.getCreateTimestamp());
|
431 |
transactionApprovalModel.setCreatedOn(transaction.getCreateTimestamp());
|
| 410 |
transactionApprovalModel.setTransactionId(transactionApproval.getId());
|
432 |
transactionApprovalModel.setTransactionId(transactionApproval.getId());
|
| 411 |
transactionApprovalModel.setLineItemModels(lineItemModelList);
|
433 |
transactionApprovalModel.setLineItemModels(lineItemModelList);
|
| - |
|
434 |
transactionApprovalModel.setFirstPo(transactionApproval.isFirstPo());
|
| 412 |
approvalModelList.add(transactionApprovalModel);
|
435 |
approvalModelList.add(transactionApprovalModel);
|
| 413 |
|
436 |
|
| 414 |
}
|
437 |
}
|
| 415 |
return approvalModelList;
|
438 |
return approvalModelList;
|
| 416 |
}
|
439 |
}
|
| Line 447... |
Line 470... |
| 447 |
model.setLineItemModels(lineItemModelList);
|
470 |
model.setLineItemModels(lineItemModelList);
|
| 448 |
model.setStatus(transactionApproval.getStatus().name());
|
471 |
model.setStatus(transactionApproval.getStatus().name());
|
| 449 |
model.setApprovedBy(transactionApproval.getApprovedBy());
|
472 |
model.setApprovedBy(transactionApproval.getApprovedBy());
|
| 450 |
model.setApprovedOn(transactionApproval.getApprovedOn());
|
473 |
model.setApprovedOn(transactionApproval.getApprovedOn());
|
| 451 |
model.setRemark(transactionApproval.getRemark());
|
474 |
model.setRemark(transactionApproval.getRemark());
|
| - |
|
475 |
model.setFirstPo(transactionApproval.isFirstPo());
|
| 452 |
approvalModelList.add(model);
|
476 |
approvalModelList.add(model);
|
| 453 |
}
|
477 |
}
|
| 454 |
return approvalModelList;
|
478 |
return approvalModelList;
|
| 455 |
}
|
479 |
}
|
| 456 |
|
480 |
|