| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | * 将加工产生的产品记录到待入库表 |
| | | */ |
| | | private void insertPendingInventory(List<Production> list) { |
| | | LocalDate currentDate = LocalDate.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | String formattedDate = currentDate.format(formatter); |
| | | // 常量定义:保留2位小数,四舍五入模式 |
| | | final int SCALE = 2; |
| | | final RoundingMode ROUNDING_MODE = RoundingMode.HALF_UP; |
| | | // 税率13%,用字符串构造BigDecimal避免精度误差 |
| | | final BigDecimal TAX_RATE = new BigDecimal("1.13"); |
| | | |
| | | for (Production p : list) { |
| | | PendingInventory pending = new PendingInventory(); |
| | | pending.setCoalId(p.getCoalId()); |
| | | pending.setInventoryQuantity(p.getProductionQuantity()); |
| | | pending.setSupplierName("生产加工入库"); |
| | | pending.setTotalPriceIncludingTax(p.getTotalCost()); |
| | | pending.setPriceIncludingTax(p.getPurchasePrice()); |
| | | pending.setUnit("t"); |
| | | pending.setSupplierName(formattedDate + " - " + "生产加工入库"); |
| | | |
| | | // 1. 非空处理:避免null导致的运算异常 |
| | | BigDecimal totalCost = p.getTotalCost() == null ? BigDecimal.ZERO : p.getTotalCost(); |
| | | BigDecimal productionQuantity = p.getProductionQuantity() == null ? BigDecimal.ZERO : p.getProductionQuantity(); |
| | | |
| | | // 2. 含税总价 = 含税单价 * 产量 → 保留2位小数 |
| | | BigDecimal totalPriceIncludingTax = totalCost.multiply(productionQuantity) |
| | | .setScale(SCALE, ROUNDING_MODE); |
| | | pending.setTotalPriceIncludingTax(totalPriceIncludingTax); |
| | | |
| | | // 3. 含税单价 → 直接保留2位小数 |
| | | pending.setPriceIncludingTax(totalCost.setScale(SCALE, ROUNDING_MODE)); |
| | | |
| | | // 4. 不含税单价 = 含税单价 / 1.13 → 先高精度计算,再保留2位 |
| | | BigDecimal priceExcludingTax = totalCost.divide(TAX_RATE, 10, ROUNDING_MODE) // 中间保留10位防误差 |
| | | .setScale(SCALE, ROUNDING_MODE); // 最终保留2位 |
| | | pending.setPriceExcludingTax(priceExcludingTax); |
| | | |
| | | // 5. 不含税总价 = 不含税单价 * 产量 → 保留2位小数 |
| | | BigDecimal totalPriceExcludingTax = priceExcludingTax.multiply(productionQuantity) |
| | | .setScale(SCALE, ROUNDING_MODE); |
| | | pending.setTotalPriceExcludingTax(totalPriceExcludingTax); |
| | | |
| | | pending.setRegistrantId(p.getProducerId()); |
| | | pending.setRegistrationDate(LocalDate.now()); |
| | | pendingInventoryMapper.insert(pending); |