From 3481d209ec847542b73fa16616ffe0e13c949e80 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期六, 18 四月 2026 18:11:03 +0800
Subject: [PATCH] fix: 入库与出库数量绑定
---
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java | 791 ++++++++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 650 insertions(+), 141 deletions(-)
diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
index 276e8c6..1a371f9 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -19,6 +19,9 @@
import com.ruoyi.common.enums.FileNameType;
import com.ruoyi.common.enums.SaleEnum;
import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockOutUnQualifiedRecordTypeEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
@@ -27,7 +30,6 @@
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.security.LoginUser;
-import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.other.mapper.TempFileMapper;
import com.ruoyi.other.pojo.TempFile;
import com.ruoyi.production.mapper.*;
@@ -38,6 +40,7 @@
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
+import com.ruoyi.procurementrecord.utils.StockUtils;
import com.ruoyi.purchase.dto.SimpleReturnOrderGroupDto;
import com.ruoyi.purchase.mapper.PurchaseReturnOrderProductsMapper;
import com.ruoyi.quality.mapper.QualityInspectMapper;
@@ -49,7 +52,13 @@
import com.ruoyi.sales.service.ISalesLedgerProductProcessService;
import com.ruoyi.sales.service.ISalesLedgerService;
import com.ruoyi.stock.dto.StockInventoryDto;
+import com.ruoyi.stock.mapper.StockInRecordMapper;
+import com.ruoyi.stock.mapper.StockOutRecordMapper;
+import com.ruoyi.stock.pojo.StockInRecord;
+import com.ruoyi.stock.pojo.StockOutRecord;
+import com.ruoyi.stock.service.StockInRecordService;
import com.ruoyi.stock.service.StockInventoryService;
+import com.ruoyi.stock.service.StockOutRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
@@ -130,6 +139,11 @@
private final ISalesLedgerProcessRouteService salesLedgerProcessRouteService;
private final StockInventoryService stockInventoryService;
+ private final StockInRecordMapper stockInRecordMapper;
+ private final StockOutRecordMapper stockOutRecordMapper;
+ private final StockInRecordService stockInRecordService;
+ private final StockOutRecordService stockOutRecordService;
+ private final StockUtils stockUtils;
@Autowired
private SysDeptMapper sysDeptMapper;
@@ -263,6 +277,7 @@
product.setActualTotalArea(pieceArea.multiply(quantity).setScale(2, RoundingMode.HALF_UP));
}
}
+ product.fillRemainingQuantity();
}
// 3.鏌ヨ涓婁紶鏂囦欢
@@ -408,147 +423,259 @@
@Override
@Transactional(rollbackFor = Exception.class)
- public AjaxResult importData(MultipartFile file) {
+ public void importData(MultipartFile file) {
LoginUser loginUser = SecurityUtils.getLoginUser();
- try {
- InputStream inputStream = file.getInputStream();
- ExcelUtil<SalesLedgerImportDto> salesLedgerImportDtoExcelUtil = new ExcelUtil<>(SalesLedgerImportDto.class);
- Map<String, List<SalesLedgerImportDto>> stringListMap = salesLedgerImportDtoExcelUtil.importExcelMultiSheet(Arrays.asList("閿�鍞彴璐︽暟鎹�", "閿�鍞骇鍝佹暟鎹�"), inputStream, 0);
- if (CollectionUtils.isEmpty(stringListMap)) return AjaxResult.error("閿�鍞〃鏍间负绌猴紒");
- // 涓氬姟灞傚悎骞�
- List<SalesLedgerImportDto> salesLedgerImportDtoList = stringListMap.get("閿�鍞彴璐︽暟鎹�");
- if (CollectionUtils.isEmpty(salesLedgerImportDtoList)) return AjaxResult.error("閿�鍞彴璐︽暟鎹负绌猴紒");
- List<SalesLedgerImportDto> salesLedgerProductImportDtoList = stringListMap.get("閿�鍞骇鍝佹暟鎹�");
- if (CollectionUtils.isEmpty(salesLedgerProductImportDtoList)) return AjaxResult.error("閿�鍞骇鍝佹暟鎹负绌猴紒");
- // 瀹㈡埛鏁版嵁
- List<Customer> customers = customerMapper.selectList(new LambdaQueryWrapper<Customer>().in(Customer::getCustomerName,
- salesLedgerImportDtoList.stream().map(SalesLedgerImportDto::getCustomerName).collect(Collectors.toList())));
- List<Map<String, Object>> list = productModelMapper.getProductAndModelList();
- // 褰曞叆浜烘暟鎹�
- List<SysUser> sysUsers = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getNickName,
- salesLedgerImportDtoList.stream().map(SalesLedgerImportDto::getEntryPerson).collect(Collectors.toList())));
- for (SalesLedgerImportDto salesLedgerImportDto : salesLedgerImportDtoList) {
- SalesLedger salesLedger1 = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>()
- .eq(SalesLedger::getSalesContractNo, salesLedgerImportDto.getSalesContractNo())
- .last("LIMIT 1"));
- if (salesLedger1 != null) {
- throw new ServiceException("瀵煎叆澶辫触锛氬悎鍚屽彿 [" + salesLedgerImportDto.getSalesContractNo() + "] 宸插瓨鍦紝璇锋鏌ュ悗閲嶆柊瀵煎叆");
+
+ Map<String, List<SalesLedgerImportDto>> stringListMap;
+ try (InputStream inputStream = file.getInputStream()) {
+ ExcelUtil<SalesLedgerImportDto> excelUtil = new ExcelUtil<>(SalesLedgerImportDto.class);
+ stringListMap = excelUtil.importExcelMultiSheet(Arrays.asList("閿�鍞彴璐︽暟鎹�", "閿�鍞骇鍝佹暟鎹�"), inputStream, 0);
+ } catch (IOException e) {
+ log.error("閿�鍞彴璐﹀鍏ュけ璐ワ細璇诲彇/瑙f瀽Excel寮傚父", e);
+ throw new ServiceException("瀵煎叆澶辫触锛氳鍙�/瑙f瀽Excel寮傚父");
+ } catch (Exception e) {
+ log.error("閿�鍞彴璐﹀鍏ュけ璐ワ細瑙f瀽Excel寮傚父", e);
+ throw new ServiceException("瀵煎叆澶辫触锛氳В鏋怑xcel寮傚父");
+ }
+
+ if (CollectionUtils.isEmpty(stringListMap)) {
+ throw new ServiceException("瀵煎叆澶辫触,閿�鍞〃鏍间负绌�");
+ }
+ // 涓氬姟灞傚悎骞�
+ List<SalesLedgerImportDto> salesLedgerImportDtoList = stringListMap.get("閿�鍞彴璐︽暟鎹�");
+ if (CollectionUtils.isEmpty(salesLedgerImportDtoList)) {
+ throw new ServiceException("瀵煎叆澶辫触,閿�鍞彴璐︽暟鎹负绌�");
+ }
+ List<SalesLedgerImportDto> salesLedgerProductImportDtoList = stringListMap.get("閿�鍞骇鍝佹暟鎹�");
+ if (CollectionUtils.isEmpty(salesLedgerProductImportDtoList)) {
+ throw new ServiceException("瀵煎叆澶辫触,閿�鍞骇鍝佹暟鎹负绌�");
+ }
+ // 瀹㈡埛鏁版嵁
+ List<Customer> customers = customerMapper.selectList(new LambdaQueryWrapper<Customer>().in(Customer::getCustomerName,
+ salesLedgerImportDtoList.stream().map(SalesLedgerImportDto::getCustomerName).collect(Collectors.toList())));
+ List<Map<String, Object>> list = productModelMapper.getProductAndModelList();
+ // 褰曞叆浜烘暟鎹�
+ List<SysUser> sysUsers = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getNickName,
+ salesLedgerImportDtoList.stream().map(SalesLedgerImportDto::getEntryPerson).collect(Collectors.toList())));
+ for (SalesLedgerImportDto salesLedgerImportDto : salesLedgerImportDtoList) {
+ SalesLedger salesLedger1 = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>()
+ .eq(SalesLedger::getSalesContractNo, salesLedgerImportDto.getSalesContractNo())
+ .last("LIMIT 1"));
+ if (salesLedger1 != null) {
+ throw new ServiceException("瀵煎叆澶辫触锛氬悎鍚屽彿 [" + salesLedgerImportDto.getSalesContractNo() + "] 宸插瓨鍦紝璇锋鏌ュ悗閲嶆柊瀵煎叆");
+ }
+ SalesLedger salesLedger = new SalesLedger();
+ BeanUtils.copyProperties(salesLedgerImportDto, salesLedger);
+ // 鏍¢獙锛氱璁㈡棩鏈熴�佸綍鍏ユ棩鏈熶笉鑳戒负绌�
+ if (salesLedgerImportDto.getExecutionDate() == null) {
+ throw new ServiceException("瀵煎叆澶辫触锛氬悎鍚屽彿[" + salesLedgerImportDto.getSalesContractNo() + "] 绛捐鏃ユ湡涓嶈兘涓虹┖");
+ }
+ if (salesLedgerImportDto.getEntryDate() == null) {
+ throw new ServiceException("瀵煎叆澶辫触锛氬悎鍚屽彿[" + salesLedgerImportDto.getSalesContractNo() + "] 褰曞叆鏃ユ湡涓嶈兘涓虹┖");
+ }
+ salesLedger.setExecutionDate(DateUtils.toLocalDate(salesLedgerImportDto.getExecutionDate()));
+
+ LocalDate expectedDeliveryDate = DateUtils.toLocalDate(salesLedgerImportDto.getEntryDate()).plusDays(7);
+ LocalDate importDeliveryDate = salesLedgerImportDto.getDeliveryDate() == null
+ ? null
+ : DateUtils.toLocalDate(salesLedgerImportDto.getDeliveryDate());
+ // 浜や粯鏃ユ湡涓虹┖鍒欓粯璁ゅ彇褰曞叆鏃ユ湡鍚�7澶�
+ salesLedger.setDeliveryDate(importDeliveryDate == null ? expectedDeliveryDate : importDeliveryDate);
+ // 閫氳繃瀹㈡埛鍚嶇О鏌ヨ瀹㈡埛ID锛屽鎴峰悎鍚屽彿
+ salesLedger.setCustomerId(customers.stream()
+ .filter(customer -> customer.getCustomerName().equals(salesLedger.getCustomerName()))
+ .findFirst()
+ .map(Customer::getId)
+ .orElse(null));
+ salesLedger.setCustomerContractNo(customers.stream()
+ .filter(customer -> customer.getCustomerName().equals(salesLedger.getCustomerName()))
+ .findFirst()
+ .map(Customer::getTaxpayerIdentificationNumber)
+ .orElse(null));
+ Long aLong = sysUsers.stream()
+ .filter(sysUser -> sysUser.getNickName().equals(salesLedger.getEntryPerson()))
+ .findFirst()
+ .map(SysUser::getUserId)
+ .orElse(null);
+ if (aLong == null) {
+ throw new ServiceException("褰曞叆浜�:" + salesLedger.getEntryPerson() + ",鏃犲搴旂敤鎴凤紒");
+ }
+ salesLedger.setEntryPerson(aLong.toString());
+ // 閿�鍞骇鍝佹暟鎹粦瀹氾紝閫氳繃閿�鍞崟鍙疯幏鍙栧搴旈攢鍞骇鍝佹暟鎹�
+ List<SalesLedgerProductImportDto> salesLedgerProductImportDtos = salesLedgerProductImportDtoList.stream()
+ .filter(salesLedgerProductImportDto -> salesLedgerProductImportDto.getSalesContractNo().equals(salesLedger.getSalesContractNo()))
+ .collect(Collectors.toList());
+ if (CollectionUtils.isEmpty(salesLedgerProductImportDtos)) {
+ throw new ServiceException("閿�鍞崟鍙�:" + salesLedgerImportDto.getSalesContractNo() + ",鏃犲搴斾骇鍝佹暟鎹紒");
+ }
+ // 鍙戣揣鐘舵��
+ salesLedger.setDeliveryStatus(1);
+ salesLedger.setContractAmount(BigDecimal.ZERO);
+ salesLedgerMapper.insert(salesLedger);
+ BigDecimal contractAmount = BigDecimal.ZERO;
+
+ for (SalesLedgerProductImportDto salesLedgerProductImportDto : salesLedgerProductImportDtos) {
+ SalesLedgerProduct salesLedgerProduct = new SalesLedgerProduct();
+ BeanUtils.copyProperties(salesLedgerProductImportDto, salesLedgerProduct);
+ salesLedgerProduct.setFloorCode(salesLedgerProductImportDto.getFloorNo());
+ salesLedgerProduct.setProcessRequirement(salesLedgerProductImportDto.getProcessingRequirements());
+ salesLedgerProduct.setRemark(salesLedgerProductImportDto.getRemarks());
+ salesLedgerProduct.setSalesLedgerId(salesLedger.getId());
+ salesLedgerProduct.setType(1);
+
+ BigDecimal quantity = defaultDecimal(salesLedgerProduct.getQuantity());
+ BigDecimal width = defaultDecimal(salesLedgerProduct.getWidth());
+ BigDecimal height = defaultDecimal(salesLedgerProduct.getHeight());
+ BigDecimal taxRateRaw = salesLedgerProduct.getTaxRate();
+ BigDecimal taxRate = defaultDecimal(taxRateRaw);
+ BigDecimal unitPrice = defaultDecimal(salesLedgerProduct.getTaxInclusiveUnitPrice());
+
+ // 瀵煎叆闄愬埗锛氬/楂�/鏁伴噺/閲戦(鍗曚环)/绋庣巼 涓嶈兘涓�0鎴栬礋鏁�
+ String locate = buildImportLocate(salesLedger.getSalesContractNo(), salesLedgerProductImportDto);
+ assertPositive(quantity, "鏁伴噺", locate);
+ assertPositive(width, "瀹�(mm)", locate);
+ assertPositive(height, "楂�(mm)", locate);
+ assertPositive(unitPrice, "鍚◣鍗曚环", locate);
+ assertNonNegative(taxRateRaw, "绋庣巼", locate);
+
+ BigDecimal actualPieceArea = BigDecimal.ZERO;
+ if (width.compareTo(BigDecimal.ZERO) > 0 && height.compareTo(BigDecimal.ZERO) > 0) {
+ actualPieceArea = width.multiply(height).divide(new BigDecimal("1000000"), 4, RoundingMode.HALF_UP);
}
- SalesLedger salesLedger = new SalesLedger();
- BeanUtils.copyProperties(salesLedgerImportDto, salesLedger);
- salesLedger.setExecutionDate(DateUtils.toLocalDate(salesLedgerImportDto.getExecutionDate()));
- // 閫氳繃瀹㈡埛鍚嶇О鏌ヨ瀹㈡埛ID锛屽鎴峰悎鍚屽彿
- salesLedger.setCustomerId(customers.stream()
- .filter(customer -> customer.getCustomerName().equals(salesLedger.getCustomerName()))
- .findFirst()
- .map(Customer::getId)
- .orElse(null));
- salesLedger.setCustomerContractNo(customers.stream()
- .filter(customer -> customer.getCustomerName().equals(salesLedger.getCustomerName()))
- .findFirst()
- .map(Customer::getTaxpayerIdentificationNumber)
- .orElse(null));
- Long aLong = sysUsers.stream()
- .filter(sysUser -> sysUser.getNickName().equals(salesLedger.getEntryPerson()))
- .findFirst()
- .map(SysUser::getUserId)
- .orElse(null);
- if (aLong == null)
- throw new ServiceException("褰曞叆浜�:" + salesLedger.getEntryPerson() + ",鏃犲搴旂敤鎴凤紒");
- salesLedger.setEntryPerson(aLong.toString());
- // 閿�鍞骇鍝佹暟鎹粦瀹氾紝閫氳繃閿�鍞崟鍙疯幏鍙栧搴旈攢鍞骇鍝佹暟鎹�
- List<SalesLedgerProductImportDto> salesLedgerProductImportDtos = salesLedgerProductImportDtoList.stream()
- .filter(salesLedgerProductImportDto -> salesLedgerProductImportDto.getSalesContractNo().equals(salesLedger.getSalesContractNo()))
- .collect(Collectors.toList());
- if (CollectionUtils.isEmpty(salesLedgerProductImportDtos))
- throw new RuntimeException("閿�鍞崟鍙�:" + salesLedgerImportDto.getSalesContractNo() + ",鏃犲搴斾骇鍝佹暟鎹紒");
- salesLedger.setContractAmount(salesLedgerProductImportDtos.stream()
- .map(SalesLedgerProductImportDto::getTaxInclusiveTotalPrice)
- .reduce(BigDecimal.ZERO, BigDecimal::add));
- // 鍙戣揣鐘舵��
- salesLedger.setDeliveryStatus(1);
- salesLedgerMapper.insert(salesLedger);
+ salesLedgerProduct.setActualPieceArea(actualPieceArea);
+ salesLedgerProduct.setActualTotalArea(actualPieceArea.multiply(quantity).setScale(4, RoundingMode.HALF_UP));
- for (SalesLedgerProductImportDto salesLedgerProductImportDto : salesLedgerProductImportDtos) {
- SalesLedgerProduct salesLedgerProduct = new SalesLedgerProduct();
- BeanUtils.copyProperties(salesLedgerProductImportDto, salesLedgerProduct);
- salesLedgerProduct.setFloorCode(salesLedgerProductImportDto.getFloorNo());
- salesLedgerProduct.setProcessRequirement(salesLedgerProductImportDto.getProcessingRequirements());
- salesLedgerProduct.setRemark(salesLedgerProductImportDto.getRemarks());
- salesLedgerProduct.setSalesLedgerId(salesLedger.getId());
- salesLedgerProduct.setType(1);
- // 璁$畻涓嶅惈绋庢�讳环
- salesLedgerProduct.setTaxExclusiveTotalPrice(salesLedgerProduct.getTaxInclusiveTotalPrice().divide(new BigDecimal(1).add(salesLedgerProduct.getTaxRate().divide(new BigDecimal(100))), 2, RoundingMode.HALF_UP));
- salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
- salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxExclusiveTotalPrice());
- list.stream()
- .filter(map -> map.get("productName").equals(salesLedgerProduct.getProductCategory()) && map.get("model").equals(salesLedgerProduct.getSpecificationModel()))
- .findFirst()
- .ifPresent(map -> {
- salesLedgerProduct.setProductModelId(Long.parseLong(map.get("modelId").toString()));
- salesLedgerProduct.setProductId(Long.parseLong(map.get("id").toString()));
- });
- salesLedgerProduct.setRegister(loginUser.getNickName());
- salesLedgerProduct.setRegisterDate(LocalDateTime.now());
- salesLedgerProduct.setApproveStatus(0);
- salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProductImportDto.getTaxInclusiveTotalPrice());
- salesLedgerProductMapper.insert(salesLedgerProduct);
+ BigDecimal settlePieceArea = salesLedgerProduct.getSettlePieceArea() == null
+ ? actualPieceArea
+ : salesLedgerProduct.getSettlePieceArea();
+ salesLedgerProduct.setSettlePieceArea(settlePieceArea);
+ salesLedgerProduct.setSettleTotalArea(settlePieceArea.multiply(quantity).setScale(4, RoundingMode.HALF_UP));
- // 澶勭悊棰濆鍔犲伐淇℃伅
- String extraProcessing = salesLedgerProductImportDto.getExtraProcessing();
- if (StringUtils.hasText(extraProcessing)) {
- List<SalesLedgerProductProcess> processList = new ArrayList<>();
- // 涓嫳鏂囧垎鍙�
- String[] items = extraProcessing.split("[;锛沒");
- for (String item : items) {
- if (StringUtils.hasText(item)) {
- String[] parts = item.split("[-鈥攡锝瀅");
- if (parts.length >= 2) {
- String processName = parts[0].trim();
- String qtyStr = parts[1].trim();
- try {
- BigDecimal quantity = new BigDecimal(qtyStr);
- SalesLedgerProductProcess process = salesLedgerProductProcessService.getOne(
- new LambdaQueryWrapper<SalesLedgerProductProcess>()
- .eq(SalesLedgerProductProcess::getProcessName, processName)
- .last("LIMIT 1")
+ BigDecimal perimeter = BigDecimal.ZERO;
+ if (width.compareTo(BigDecimal.ZERO) > 0 && height.compareTo(BigDecimal.ZERO) > 0) {
+ perimeter = width.add(height)
+ .multiply(new BigDecimal("2"))
+ .divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
+ }
+ salesLedgerProduct.setPerimeter(perimeter);
+
+ BigDecimal extraProcessAmountPerPiece = BigDecimal.ZERO;
+ list.stream()
+ .filter(Objects::nonNull)
+ .filter(map -> Objects.equals(Objects.toString(map.get("productName"), null), salesLedgerProduct.getProductCategory())
+ && Objects.equals(Objects.toString(map.get("model"), null), salesLedgerProduct.getSpecificationModel()))
+ .findFirst()
+ .ifPresent(map -> {
+ salesLedgerProduct.setProductModelId(Long.parseLong(map.get("modelId").toString()));
+ salesLedgerProduct.setProductId(Long.parseLong(map.get("id").toString()));
+ });
+ salesLedgerProduct.setRegister(loginUser.getNickName());
+ salesLedgerProduct.setRegisterDate(LocalDateTime.now());
+ salesLedgerProduct.setApproveStatus(0);
+ salesLedgerProduct.setProductStockStatus(0);
+
+ // 澶勭悊棰濆鍔犲伐淇℃伅
+ String extraProcessing = salesLedgerProductImportDto.getExtraProcessing();
+ List<SalesLedgerProductProcess> processList = new ArrayList<>();
+ if (StringUtils.hasText(extraProcessing)) {
+ // 涓嫳鏂囧垎鍙�
+ String[] items = extraProcessing.split("[;锛沒");
+ for (String item : items) {
+ if (StringUtils.hasText(item)) {
+ String[] parts = item.split("[-鈥攡锝瀅");
+ if (parts.length >= 2) {
+ String processName = parts[0].trim();
+ String qtyStr = parts[1].trim();
+ try {
+ BigDecimal processQty = new BigDecimal(qtyStr);
+ SalesLedgerProductProcess process = salesLedgerProductProcessService.getOne(
+ new LambdaQueryWrapper<SalesLedgerProductProcess>()
+ .eq(SalesLedgerProductProcess::getProcessName, processName)
+ .last("LIMIT 1")
+ );
+ if (process != null) {
+ SalesLedgerProductProcess p = new SalesLedgerProductProcess();
+ p.setId(process.getId());
+ p.setQuantity(processQty.intValue());
+ processList.add(p);
+ extraProcessAmountPerPiece = extraProcessAmountPerPiece.add(
+ defaultDecimal(process.getUnitPrice()).multiply(processQty)
);
- if (process != null) {
- SalesLedgerProductProcess p = new SalesLedgerProductProcess();
- p.setId(process.getId());
- p.setQuantity(quantity.intValue());
- processList.add(p);
- }
- } catch (Exception e) {
- log.error("瑙f瀽棰濆鍔犲伐鏁伴噺澶辫触: {}", qtyStr);
}
+ } catch (Exception e) {
+ log.error("瑙f瀽棰濆鍔犲伐鏁伴噺澶辫触: {}", qtyStr);
}
}
}
- if (!processList.isEmpty()) {
- salesLedgerProductProcessBindService.updateProductProcessBind(processList, salesLedgerProduct.getId());
- }
}
-
- // 娣诲姞鐢熶骇鏁版嵁
- salesLedgerProductServiceImpl.addProductionData(salesLedgerProduct);
}
- }
- return AjaxResult.success("瀵煎叆鎴愬姛");
- } catch (Exception e) {
- e.printStackTrace();
+ // 鍚◣鎬讳环 = 鍗曚环 * 缁撶畻闈㈢Н * 鏁伴噺 + 棰濆鍔犲伐閲戦 * 鏁伴噺
+ BigDecimal taxInclusiveTotalPrice = unitPrice.multiply(settlePieceArea)
+ .multiply(quantity)
+ .add(extraProcessAmountPerPiece.multiply(quantity))
+ .setScale(2, RoundingMode.HALF_UP);
+ salesLedgerProduct.setTaxInclusiveTotalPrice(taxInclusiveTotalPrice);
+ assertPositive(taxInclusiveTotalPrice, "鍚◣鎬讳环", locate);
+
+ // 绋庣巼鍏佽涓虹┖锛岀┖鍊兼寜0澶勭悊
+ BigDecimal taxDivisor = BigDecimal.ONE.add(taxRate.divide(new BigDecimal("100"), 6, RoundingMode.HALF_UP));
+ salesLedgerProduct.setTaxExclusiveTotalPrice(
+ taxInclusiveTotalPrice.divide(taxDivisor, 2, RoundingMode.HALF_UP)
+ );
+ salesLedgerProduct.setNoInvoiceNum(quantity);
+ salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxExclusiveTotalPrice());
+ salesLedgerProduct.setPendingInvoiceTotal(taxInclusiveTotalPrice);
+ salesLedgerProduct.fillRemainingQuantity();
+ salesLedgerProductMapper.insert(salesLedgerProduct);
+ if (!processList.isEmpty()) {
+ salesLedgerProductProcessBindService.updateProductProcessBind(processList, salesLedgerProduct.getId());
+ }
+ // 娣诲姞鐢熶骇鏁版嵁
+ salesLedgerProductServiceImpl.addProductionData(salesLedgerProduct);
+ contractAmount = contractAmount.add(taxInclusiveTotalPrice);
+ }
+ salesLedger.setContractAmount(contractAmount);
+ salesLedgerMapper.updateById(salesLedger);
}
- return AjaxResult.success("瀵煎叆澶辫触");
+ }
+
+ private BigDecimal defaultDecimal(BigDecimal value) {
+ return value == null ? BigDecimal.ZERO : value;
+ }
+
+ private void assertPositive(BigDecimal value, String fieldName, String locate) {
+ if (value == null || value.compareTo(BigDecimal.ZERO) <= 0) {
+ throw new ServiceException("瀵煎叆澶辫触锛�" + locate + "銆�" + fieldName + "銆戝繀椤诲ぇ浜�0");
+ }
+ }
+
+ private void assertNonNegative(BigDecimal value, String fieldName, String locate) {
+ if (value != null && value.compareTo(BigDecimal.ZERO) < 0) {
+ throw new ServiceException("瀵煎叆澶辫触锛�" + locate + "銆�" + fieldName + "銆戜笉鑳戒负璐熸暟");
+ }
+ }
+
+ private String buildImportLocate(String salesContractNo, SalesLedgerProductImportDto dto) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("閿�鍞崟鍙穂").append(salesContractNo == null ? "" : salesContractNo).append("]");
+ if (dto != null) {
+ if (StringUtils.hasText(dto.getProductCategory())) {
+ sb.append(" 浜у搧澶х被[").append(dto.getProductCategory()).append("]");
+ }
+ if (StringUtils.hasText(dto.getSpecificationModel())) {
+ sb.append(" 瑙勬牸鍨嬪彿[").append(dto.getSpecificationModel()).append("]");
+ }
+ if (StringUtils.hasText(dto.getFloorNo())) {
+ sb.append(" 妤煎眰缂栧彿[").append(dto.getFloorNo()).append("]");
+ }
+ }
+ return sb.toString();
}
@Override
public List<LossProductModelDto> getSalesLedgerWithProductsLoss(Long salesLedgerId) {
-
-
List<LossProductModelDto> lossProductModelDtos = salesLedgerProductMapper.selectProductBomStructure(salesLedgerId);
-
-
return lossProductModelDtos;
}
@@ -702,6 +829,25 @@
.in(ShippingInfo::getSalesLedgerId, idList));
if (CollectionUtils.isNotEmpty(shippingInfos)) {
shippingInfoServiceImpl.delete(shippingInfos.stream().map(ShippingInfo::getId).collect(Collectors.toList()));
+ }
+ // 鍒犻櫎鍏宠仈鐨勫叆搴�/鍑哄簱璁板綍锛堣蛋鏈嶅姟灞傚垹闄わ紝瑙﹀彂搴撳瓨鏁伴噺鍥為��锛�
+ List<Long> stockInRecordIds = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+ .in(StockInRecord::getSalesLedgerId, idList)
+ .select(StockInRecord::getId))
+ .stream()
+ .map(StockInRecord::getId)
+ .collect(Collectors.toList());
+ if (CollectionUtils.isNotEmpty(stockInRecordIds)) {
+ stockInRecordService.batchDelete(stockInRecordIds);
+ }
+ List<Long> stockOutRecordIds = stockOutRecordMapper.selectList(new LambdaQueryWrapper<StockOutRecord>()
+ .in(StockOutRecord::getSalesLedgerId, idList)
+ .select(StockOutRecord::getId))
+ .stream()
+ .map(StockOutRecord::getId)
+ .collect(Collectors.toList());
+ if (CollectionUtils.isNotEmpty(stockOutRecordIds)) {
+ stockOutRecordService.batchDelete(stockOutRecordIds);
}
// 鍒犻櫎闄勪欢琛�
commonFileService.deleteByBusinessIds(idList, FileNameType.SALE.getValue());
@@ -860,6 +1006,8 @@
if (!updateList.isEmpty()) {
for (SalesLedgerProduct product : updateList) {
product.setType(type.getCode());
+ product.setProductStockStatus(0);
+ product.fillRemainingQuantity();
salesLedgerProductMapper.updateById(product);
// 娓呯┖閿�鍞骇鍝佺粦瀹氱殑鍔犲伐
salesLedgerProductProcessBindService.updateProductProcessBind(product.getSalesProductProcessList(), product.getId());
@@ -872,6 +1020,8 @@
salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProduct.getTaxInclusiveTotalPrice());
+ salesLedgerProduct.setProductStockStatus(0);
+ salesLedgerProduct.fillRemainingQuantity();
salesLedgerProductMapper.insert(salesLedgerProduct);
// 缁戝畾浜у搧棰濆鍔犲伐
// 娓呯┖閿�鍞骇鍝佺粦瀹氱殑鍔犲伐
@@ -1042,13 +1192,19 @@
List<SalesProcessCardDto.ProcessNodeDto> nodeDtos = new ArrayList<>();
if (CollectionUtils.isEmpty(salesLedgerProcessRoutes)) {
- // 鏃犺嚜瀹氫箟璺嚎锛屽彇榛樿璺嚎
- ProcessRoute defaultRoute = processRouteMapper.selectOne(
+ // 鏃犺嚜瀹氫箟璺嚎锛氬厛鏌ヨ榛樿宸ヨ壓璺嚎锛涜嫢鏃犻粯璁わ紝鑾峰彇鍒涘缓鏃堕棿鏈�杩戠殑涓�鏉�
+ ProcessRoute fallbackRoute = processRouteMapper.selectOne(
new LambdaQueryWrapper<ProcessRoute>().eq(ProcessRoute::getIsDefault, 1).last("LIMIT 1"));
- if (defaultRoute != null) {
+ if (fallbackRoute == null) {
+ fallbackRoute = processRouteMapper.selectOne(
+ new LambdaQueryWrapper<ProcessRoute>()
+ .orderByDesc(ProcessRoute::getCreateTime)
+ .last("LIMIT 1"));
+ }
+ if (fallbackRoute != null) {
List<ProcessRouteItem> routeItems = processRouteItemMapper.selectList(
new LambdaQueryWrapper<ProcessRouteItem>()
- .eq(ProcessRouteItem::getRouteId, defaultRoute.getId())
+ .eq(ProcessRouteItem::getRouteId, fallbackRoute.getId())
.orderByAsc(ProcessRouteItem::getDragSort));
for (ProcessRouteItem i : routeItems) {
SalesProcessCardDto.ProcessNodeDto node = new SalesProcessCardDto.ProcessNodeDto();
@@ -1509,39 +1665,392 @@
@Override
@Transactional(rollbackFor = Exception.class)
- public void salesStock(SalesLedger salesLedger) {
- if (salesLedger == null || salesLedger.getId() == null) {
+ public void salesStock(SalesProductStockDto dto) {
+ if (dto == null || dto.getSalesLedgerId() == null) {
throw new NullPointerException("鍏ュ簱澶辫触,璇烽�夋嫨闇�瑕佸叆搴撶殑閿�鍞鍗�");
}
// 鏌ヨ閿�鍞鍗曟槸鍚﹀瓨鍦�
- SalesLedger ledger = baseMapper.selectById(salesLedger.getId());
+ SalesLedger ledger = baseMapper.selectById(dto.getSalesLedgerId());
if (ledger == null) {
throw new ServiceException("鍏ュ簱澶辫触,閿�鍞鍗曚笉瀛樺湪");
}
if (ledger.getStockStatus() == null) {
throw new ServiceException("鍏ュ簱澶辫触,閿�鍞鍗曞叆搴撶姸鎬佸紓甯�");
}
- if (ledger.getStockStatus() == 1) {
+ if (ledger.getStockStatus() == 2) {
throw new ServiceException("鍏ュ簱澶辫触,璇ラ攢鍞鍗曞凡鍏ュ簱,璇峰嬁閲嶅鍏ュ簱");
}
+ List<Long> products = dto.getSalesLedgerProducts();
+ if (products == null || products.isEmpty()) {
+ throw new ServiceException("鍏ュ簱澶辫触,鍏ュ簱浜у搧涓嶈兘涓虹┖");
+ }
// 鏌ヨ閿�鍞鍗曠殑浜у搧
- List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(Wrappers.<SalesLedgerProduct>lambdaQuery().eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()));
+ List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(Wrappers.<SalesLedgerProduct>lambdaQuery().in(SalesLedgerProduct::getId, products));
if (salesLedgerProducts == null || salesLedgerProducts.isEmpty()) {
throw new ServiceException("鍏ュ簱澶辫触,鏈煡璇㈠埌璇ラ攢鍞鍗曠殑閿�鍞骇鍝�");
}
for (SalesLedgerProduct product : salesLedgerProducts) {
+ if (!Objects.equals(product.getSalesLedgerId(), ledger.getId())) {
+ throw new ServiceException("鍏ュ簱澶辫触,瀛樺湪涓嶅睘浜庡綋鍓嶉攢鍞鍗曠殑浜у搧");
+ }
if (product.getProductModelId() == null) {
continue;
}
- StockInventoryDto dto = new StockInventoryDto();
- dto.setRecordId(product.getId());
- dto.setRecordType(StockInQualifiedRecordTypeEnum.SALE_STOCK_IN.getCode());
- dto.setQualitity(product.getQuantity());
- dto.setProductModelId(product.getProductModelId());
- stockInventoryService.addstockInventory(dto);
+ BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity();
+ BigDecimal oldStocked = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity();
+ BigDecimal inboundQty = orderQty.subtract(oldStocked);
+ if (inboundQty.compareTo(BigDecimal.ZERO) < 0) {
+ inboundQty = BigDecimal.ZERO;
+ }
+ if (inboundQty.compareTo(BigDecimal.ZERO) <= 0) {
+ continue;
+ }
+ StockInventoryDto stockInventoryDto = new StockInventoryDto();
+ stockInventoryDto.setRecordId(product.getId());
+ stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.SALE_STOCK_IN.getCode());
+ stockInventoryDto.setQualitity(inboundQty);
+ stockInventoryDto.setProductModelId(product.getProductModelId());
+ stockInventoryDto.setSalesLedgerId(ledger.getId());
+ stockInventoryDto.setSalesLedgerProductId(product.getId());
+ stockInventoryService.addstockInventory(stockInventoryDto);
+
+ BigDecimal newStocked = oldStocked.add(inboundQty);
+ int lineStockStatus;
+ if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
+ lineStockStatus = 0;
+ } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) {
+ lineStockStatus = 1;
+ } else {
+ lineStockStatus = 2;
+ }
+ product.setStockedQuantity(newStocked);
+ product.setProductStockStatus(lineStockStatus);
+ product.fillRemainingQuantity();
+ salesLedgerProductMapper.updateById(product);
}
- // 鏇存柊閿�鍞鍗曞叆搴撶姸鎬�
- ledger.setStockStatus(1);
+ // 鎸夐攢鍞鍗曚骇鍝佸叆搴撴儏鍐垫洿鏂颁富鍗曞叆搴撶姸鎬侊細1-閮ㄥ垎鍏ュ簱锛�2-宸插叆搴�
+ List<SalesLedgerProduct> ledgerAllProducts = salesLedgerProductMapper.selectList(Wrappers.<SalesLedgerProduct>lambdaQuery().eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()));
+ boolean hasStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().anyMatch(item -> {
+ BigDecimal sq = item.getStockedQuantity();
+ return sq != null && sq.compareTo(BigDecimal.ZERO) > 0;
+ });
+ boolean allStocked = CollectionUtils.isNotEmpty(ledgerAllProducts) && ledgerAllProducts.stream().allMatch(item -> {
+ BigDecimal orderQty = item.getQuantity() == null ? BigDecimal.ZERO : item.getQuantity();
+ BigDecimal stockedQty = item.getStockedQuantity() == null ? BigDecimal.ZERO : item.getStockedQuantity();
+ return orderQty.compareTo(BigDecimal.ZERO) <= 0 || stockedQty.compareTo(orderQty) >= 0;
+ });
+ ledger.setStockStatus(allStocked ? 2 : (hasStocked ? 1 : 0));
baseMapper.updateById(ledger);
}
+
+ @Override
+ public List<Customer> shippedCustomers() {
+ List<SalesLedger> ledgers = list(Wrappers.<SalesLedger>lambdaQuery()
+ .eq(SalesLedger::getDeliveryStatus, 5)
+ .isNotNull(SalesLedger::getCustomerId)
+ .select(SalesLedger::getCustomerId));
+ if (CollectionUtils.isEmpty(ledgers)) {
+ return Collections.emptyList();
+ }
+ Set<Long> customerIds = ledgers.stream()
+ .map(SalesLedger::getCustomerId)
+ .collect(Collectors.toCollection(LinkedHashSet::new));
+ return customerMapper.selectList(Wrappers.<Customer>lambdaQuery()
+ .in(Customer::getId, customerIds)
+ .orderByAsc(Customer::getCustomerName));
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void scanInbound(SalesScanInboundDto dto) {
+ if (dto == null || dto.getSalesLedgerId() == null) {
+ throw new ServiceException("閿�鍞鍗曞叆搴撳け璐�,鍏ュ簱鏁版嵁涓嶈兘涓虹┖");
+ }
+
+ SalesLedger salesLedger = baseMapper.selectById(dto.getSalesLedgerId());
+ if (salesLedger == null) {
+ throw new ServiceException("鍏ュ簱澶辫触,閿�鍞鍗曚笉瀛樺湪");
+ }
+ if (salesLedger.getStockStatus() == null) {
+ throw new ServiceException("鍏ュ簱澶辫触,閿�鍞鍗曠姸鎬佸紓甯�");
+ }
+ if (salesLedger.getStockStatus() == 2) {
+ throw new ServiceException("鍏ュ簱澶辫触,璇ラ攢鍞鍗曞凡鍏ㄩ儴鍏ュ簱");
+ }
+ if (salesLedger.getDeliveryStatus() == 5) {
+ throw new ServiceException("鍏ュ簱澶辫触,璇ラ攢鍞鍗曞凡鍙戣揣");
+ }
+ if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
+ throw new ServiceException("閿�鍞鍗曞叆搴撳け璐�,鍏ュ簱浜у搧涓嶈兘涓虹┖");
+ }
+ // 鏈鍏ュ簱鏁伴噺鍙互澶т簬璁㈠崟鏁伴噺,浣嗕笉鑳戒负璐�
+ Map<Long, BigDecimal> inboundQtyByLineId = new LinkedHashMap<>();
+ for (SalesLedgerProduct inbound : dto.getSalesLedgerProductList()) {
+ if (inbound == null || inbound.getId() == null) {
+ throw new ServiceException("鍏ュ簱澶辫触,閿�鍞骇鍝佷俊鎭笉瀹屾暣");
+ }
+ BigDecimal inboundQty = inbound.getStockedQuantity();
+ if (inboundQty == null) {
+ throw new ServiceException("鍏ュ簱澶辫触,鍏ュ簱鏁伴噺涓嶈兘涓虹┖");
+ }
+ if (inboundQty.compareTo(BigDecimal.ZERO) < 0) {
+ throw new ServiceException("鍏ュ簱澶辫触,鍏ュ簱鏁伴噺涓嶈兘涓鸿礋鏁�");
+ }
+ inboundQtyByLineId.merge(inbound.getId(), inboundQty, BigDecimal::add);
+ }
+ Long ledgerId = salesLedger.getId();
+ for (Map.Entry<Long, BigDecimal> entry : inboundQtyByLineId.entrySet()) {
+ Long salesLedgerProductId = entry.getKey();
+ BigDecimal inboundThisLine = entry.getValue();
+ if (inboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
+ continue;
+ }
+ SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId);
+ if (dbProduct == null) {
+ throw new ServiceException("鍏ュ簱澶辫触,閿�鍞骇鍝佷笉瀛樺湪");
+ }
+ if (!Objects.equals(dbProduct.getSalesLedgerId(), ledgerId)) {
+ throw new ServiceException("鍏ュ簱澶辫触,閿�鍞骇鍝佷笌璁㈠崟涓嶅尮閰�");
+ }
+ if (!Objects.equals(dbProduct.getType(), SaleEnum.SALE.getCode())) {
+ throw new ServiceException("鍏ュ簱澶辫触,浠呮敮鎸侀攢鍞鍗曚骇鍝佽");
+ }
+ if (dbProduct.getProductModelId() == null) {
+ throw new ServiceException("鍏ュ簱澶辫触,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍏ュ簱");
+ }
+ BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
+ BigDecimal newStocked = oldStocked.add(inboundThisLine);
+
+ StockInventoryDto stockInventoryDto = new StockInventoryDto();
+ stockInventoryDto.setRecordId(dbProduct.getId());
+ stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.SALE_SCAN_STOCK_IN.getCode());
+ stockInventoryDto.setQualitity(inboundThisLine);
+ stockInventoryDto.setProductModelId(dbProduct.getProductModelId());
+ stockInventoryDto.setSalesLedgerId(ledgerId);
+ stockInventoryDto.setSalesLedgerProductId(dbProduct.getId());
+ stockInventoryService.addstockInventory(stockInventoryDto);
+
+ BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
+ int lineStockStatus;
+ if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
+ lineStockStatus = 0;
+ } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) {
+ lineStockStatus = 1;
+ } else {
+ lineStockStatus = 2;
+ }
+ dbProduct.setStockedQuantity(newStocked);
+ dbProduct.setProductStockStatus(lineStockStatus);
+ dbProduct.fillRemainingQuantity();
+ salesLedgerProductMapper.updateById(dbProduct);
+ }
+ List<SalesLedgerProduct> ledgerAllProducts = salesLedgerProductMapper.selectList(
+ Wrappers.<SalesLedgerProduct>lambdaQuery().eq(SalesLedgerProduct::getSalesLedgerId, ledgerId));
+ boolean anyInbound = ledgerAllProducts.stream().anyMatch(p -> {
+ BigDecimal sq = p.getStockedQuantity();
+ return sq != null && sq.compareTo(BigDecimal.ZERO) > 0;
+ });
+ boolean allLinesFull = ledgerAllProducts.stream().allMatch(p -> Objects.equals(p.getProductStockStatus(), 2));
+ salesLedger.setStockStatus(allLinesFull ? 2 : (anyInbound ? 1 : 0));
+ baseMapper.updateById(salesLedger);
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void scanInboundUnqualified(SalesScanInboundDto dto) {
+ if (dto == null || dto.getSalesLedgerId() == null) {
+ throw new ServiceException("閿�鍞鍗曚笉鍚堟牸鍏ュ簱澶辫触,鍏ュ簱鏁版嵁涓嶈兘涓虹┖");
+ }
+ SalesLedger salesLedger = baseMapper.selectById(dto.getSalesLedgerId());
+ if (salesLedger == null) {
+ throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閿�鍞鍗曚笉瀛樺湪");
+ }
+ if (salesLedger.getDeliveryStatus() != null && salesLedger.getDeliveryStatus() == 5) {
+ throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,璇ラ攢鍞鍗曞凡鍙戣揣");
+ }
+ if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
+ throw new ServiceException("閿�鍞鍗曚笉鍚堟牸鍏ュ簱澶辫触,鍏ュ簱浜у搧涓嶈兘涓虹┖");
+ }
+ Map<Long, BigDecimal> inboundQtyByLineId = new LinkedHashMap<>();
+ for (SalesLedgerProduct inbound : dto.getSalesLedgerProductList()) {
+ if (inbound == null || inbound.getId() == null) {
+ throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閿�鍞骇鍝佷俊鎭笉瀹屾暣");
+ }
+ BigDecimal inboundQty = inbound.getStockedQuantity();
+ if (inboundQty == null) {
+ throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,鍏ュ簱鏁伴噺涓嶈兘涓虹┖");
+ }
+ if (inboundQty.compareTo(BigDecimal.ZERO) < 0) {
+ throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,鍏ュ簱鏁伴噺涓嶈兘涓鸿礋鏁�");
+ }
+ inboundQtyByLineId.merge(inbound.getId(), inboundQty, BigDecimal::add);
+ }
+ Long ledgerId = salesLedger.getId();
+ for (Map.Entry<Long, BigDecimal> entry : inboundQtyByLineId.entrySet()) {
+ Long salesLedgerProductId = entry.getKey();
+ BigDecimal inboundThisLine = entry.getValue();
+ if (inboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
+ continue;
+ }
+ SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId);
+ if (dbProduct == null) {
+ throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閿�鍞骇鍝佷笉瀛樺湪");
+ }
+ if (!Objects.equals(dbProduct.getSalesLedgerId(), ledgerId)) {
+ throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閿�鍞骇鍝佷笌璁㈠崟涓嶅尮閰�");
+ }
+ if (!Objects.equals(dbProduct.getType(), SaleEnum.SALE.getCode())) {
+ throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,浠呮敮鎸侀攢鍞鍗曚骇鍝佽");
+ }
+ if (dbProduct.getProductModelId() == null) {
+ throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍏ュ簱");
+ }
+ stockUtils.addUnStock(ledgerId, dbProduct.getId(), dbProduct.getProductModelId(), inboundThisLine,
+ StockInUnQualifiedRecordTypeEnum.SALES_SCAN_UNSTOCK_IN.getCode(), dbProduct.getId());
+
+ BigDecimal oldUnStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+ dbProduct.setUnqualifiedStockedQuantity(oldUnStocked.add(inboundThisLine));
+ dbProduct.fillRemainingQuantity();
+ salesLedgerProductMapper.updateById(dbProduct);
+ }
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void scanOutbound(SalesScanInboundDto dto) {
+ if (dto == null || dto.getSalesLedgerId() == null) {
+ throw new ServiceException("閿�鍞鍗曞嚭搴撳け璐�,鍑哄簱鏁版嵁涓嶈兘涓虹┖");
+ }
+ SalesLedger salesLedger = baseMapper.selectById(dto.getSalesLedgerId());
+ if (salesLedger == null) {
+ throw new ServiceException("鍑哄簱澶辫触,閿�鍞鍗曚笉瀛樺湪");
+ }
+ if (salesLedger.getDeliveryStatus() != null && salesLedger.getDeliveryStatus() == 5) {
+ throw new ServiceException("鍑哄簱澶辫触,璇ラ攢鍞鍗曞凡鍙戣揣");
+ }
+ if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
+ throw new ServiceException("閿�鍞鍗曞嚭搴撳け璐�,鍑哄簱浜у搧涓嶈兘涓虹┖");
+ }
+ int saleType = SaleEnum.SALE.getCode();
+ Map<Long, BigDecimal> outboundQtyByLineId = new LinkedHashMap<>();
+ for (SalesLedgerProduct line : dto.getSalesLedgerProductList()) {
+ if (line == null || line.getId() == null) {
+ throw new ServiceException("鍑哄簱澶辫触,閿�鍞骇鍝佷俊鎭笉瀹屾暣");
+ }
+ BigDecimal outboundQty = line.getStockedQuantity();
+ if (outboundQty == null) {
+ throw new ServiceException("鍑哄簱澶辫触,鍑哄簱鏁伴噺涓嶈兘涓虹┖");
+ }
+ if (outboundQty.compareTo(BigDecimal.ZERO) < 0) {
+ throw new ServiceException("鍑哄簱澶辫触,鍑哄簱鏁伴噺涓嶈兘涓鸿礋鏁�");
+ }
+ outboundQtyByLineId.merge(line.getId(), outboundQty, BigDecimal::add);
+ }
+ Long ledgerId = salesLedger.getId();
+ for (Map.Entry<Long, BigDecimal> entry : outboundQtyByLineId.entrySet()) {
+ Long salesLedgerProductId = entry.getKey();
+ BigDecimal outboundThisLine = entry.getValue();
+ if (outboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
+ continue;
+ }
+ SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId);
+ if (dbProduct == null) {
+ throw new ServiceException("鍑哄簱澶辫触,閿�鍞骇鍝佷笉瀛樺湪");
+ }
+ if (!Objects.equals(dbProduct.getSalesLedgerId(), ledgerId) || !Objects.equals(dbProduct.getType(), saleType)) {
+ throw new ServiceException("鍑哄簱澶辫触,閿�鍞骇鍝佷笌璁㈠崟涓嶅尮閰�");
+ }
+ if (dbProduct.getProductModelId() == null) {
+ throw new ServiceException("鍑哄簱澶辫触,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍑哄簱");
+ }
+ stockUtils.assertQualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
+
+ StockInventoryDto stockInventoryDto = new StockInventoryDto();
+ stockInventoryDto.setRecordId(dbProduct.getId());
+ stockInventoryDto.setRecordType(StockOutQualifiedRecordTypeEnum.SALE_SCAN_STOCK_OUT.getCode());
+ stockInventoryDto.setQualitity(outboundThisLine);
+ stockInventoryDto.setProductModelId(dbProduct.getProductModelId());
+ stockInventoryDto.setSalesLedgerId(ledgerId);
+ stockInventoryDto.setSalesLedgerProductId(dbProduct.getId());
+ stockInventoryService.subtractStockInventory(stockInventoryDto);
+
+ BigDecimal oldShipped = dbProduct.getShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getShippedQuantity();
+ dbProduct.setShippedQuantity(oldShipped.add(outboundThisLine));
+ dbProduct.fillRemainingQuantity();
+ salesLedgerProductMapper.updateById(dbProduct);
+ }
+ List<SalesLedgerProduct> ledgerAllProducts = salesLedgerProductMapper.selectList(
+ Wrappers.<SalesLedgerProduct>lambdaQuery().eq(SalesLedgerProduct::getSalesLedgerId, ledgerId));
+ boolean anyInbound = ledgerAllProducts.stream().anyMatch(p -> {
+ BigDecimal sq = p.getStockedQuantity();
+ return sq != null && sq.compareTo(BigDecimal.ZERO) > 0;
+ });
+ boolean allLinesFull = ledgerAllProducts.stream().allMatch(p -> Objects.equals(p.getProductStockStatus(), 2));
+ salesLedger.setStockStatus(allLinesFull ? 2 : (anyInbound ? 1 : 0));
+ baseMapper.updateById(salesLedger);
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void scanOutboundUnqualified(SalesScanInboundDto dto) {
+ if (dto == null || dto.getSalesLedgerId() == null) {
+ throw new ServiceException("閿�鍞鍗曚笉鍚堟牸鍑哄簱澶辫触,鍑哄簱鏁版嵁涓嶈兘涓虹┖");
+ }
+ SalesLedger salesLedger = baseMapper.selectById(dto.getSalesLedgerId());
+ if (salesLedger == null) {
+ throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閿�鍞鍗曚笉瀛樺湪");
+ }
+ if (salesLedger.getDeliveryStatus() != null && salesLedger.getDeliveryStatus() == 5) {
+ throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,璇ラ攢鍞鍗曞凡鍙戣揣");
+ }
+ if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
+ throw new ServiceException("閿�鍞鍗曚笉鍚堟牸鍑哄簱澶辫触,鍑哄簱浜у搧涓嶈兘涓虹┖");
+ }
+ int saleType = SaleEnum.SALE.getCode();
+ Map<Long, BigDecimal> outboundQtyByLineId = new LinkedHashMap<>();
+ for (SalesLedgerProduct line : dto.getSalesLedgerProductList()) {
+ if (line == null || line.getId() == null) {
+ throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閿�鍞骇鍝佷俊鎭笉瀹屾暣");
+ }
+ BigDecimal outboundQty = line.getStockedQuantity();
+ if (outboundQty == null) {
+ throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁伴噺涓嶈兘涓虹┖");
+ }
+ if (outboundQty.compareTo(BigDecimal.ZERO) < 0) {
+ throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁伴噺涓嶈兘涓鸿礋鏁�");
+ }
+ outboundQtyByLineId.merge(line.getId(), outboundQty, BigDecimal::add);
+ }
+ Long ledgerId = salesLedger.getId();
+ for (Map.Entry<Long, BigDecimal> entry : outboundQtyByLineId.entrySet()) {
+ Long salesLedgerProductId = entry.getKey();
+ BigDecimal outboundThisLine = entry.getValue();
+ if (outboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
+ continue;
+ }
+ SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId);
+ if (dbProduct == null) {
+ throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閿�鍞骇鍝佷笉瀛樺湪");
+ }
+ if (!Objects.equals(dbProduct.getSalesLedgerId(), ledgerId) || !Objects.equals(dbProduct.getType(), saleType)) {
+ throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閿�鍞骇鍝佷笌璁㈠崟涓嶅尮閰�");
+ }
+ if (dbProduct.getProductModelId() == null) {
+ throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍑哄簱");
+ }
+ BigDecimal unStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+ BigDecimal unShipped = dbProduct.getUnqualifiedShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedShippedQuantity();
+ BigDecimal canUnShip = unStocked.subtract(unShipped);
+ if (outboundThisLine.compareTo(canUnShip) > 0) {
+ throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁伴噺涓嶈兘澶т簬涓嶅悎鏍煎叆搴撴暟閲�");
+ }
+ stockUtils.assertUnqualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
+ stockUtils.subtractUnStock(ledgerId, dbProduct.getId(), dbProduct.getProductModelId(), outboundThisLine,
+ StockOutUnQualifiedRecordTypeEnum.SALE_SCAN_UNSTOCK_OUT.getCode(), dbProduct.getId());
+
+ dbProduct.setUnqualifiedShippedQuantity(unShipped.add(outboundThisLine));
+ dbProduct.fillRemainingQuantity();
+ salesLedgerProductMapper.updateById(dbProduct);
+ }
+ }
}
--
Gitblit v1.9.3