| | |
| | | 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) { |
| | |
| | | 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位 |
| | | // 不含税单价(直接保留2位小数) |
| | | BigDecimal priceExcludingTax = totalCost.divide(TAX_RATE, SCALE, ROUNDING_MODE); |
| | | pending.setPriceExcludingTax(priceExcludingTax); |
| | | |
| | | // 5. 不含税总价 = 不含税单价 * 产量 → 保留2位小数 |
| | | // 不含税总价 |
| | | BigDecimal totalPriceExcludingTax = priceExcludingTax.multiply(productionQuantity) |
| | | .setScale(SCALE, ROUNDING_MODE); |
| | | pending.setTotalPriceExcludingTax(totalPriceExcludingTax); |