| | |
| | | import com.ruoyi.basic.mapper.ProductMapper; |
| | | import com.ruoyi.basic.mapper.ProductModelMapper; |
| | | import com.ruoyi.basic.pojo.Customer; |
| | | import com.ruoyi.basic.pojo.CustomerRegions; |
| | | import com.ruoyi.basic.service.ICustomerRegionsService; |
| | | import com.ruoyi.common.enums.FileNameType; |
| | | import com.ruoyi.common.enums.SaleEnum; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | |
| | | ; |
| | | @Autowired |
| | | private SysUserMapper sysUserMapper; |
| | | |
| | | private final ICustomerRegionsService customerRegionsService; |
| | | |
| | | @Override |
| | | public List<SalesLedger> selectSalesLedgerList(SalesLedgerDto salesLedgerDto) { |
| | |
| | | return dto; |
| | | } |
| | | |
| | | @Override |
| | | public SalesOrdersDto salesOrders(Long salesLedgerId) { |
| | | if (salesLedgerId == null) { |
| | | throw new ServiceException("打印销售订单失败,销售订单ID不能为空"); |
| | | } |
| | | SalesLedger salesLedger = baseMapper.selectById(salesLedgerId); |
| | | if (salesLedger == null) { |
| | | throw new ServiceException("打印销售订单失败,销售订单不存在"); |
| | | } |
| | | |
| | | SalesOrdersDto dto = new SalesOrdersDto(); |
| | | dto.setSalesContractNo(salesLedger.getSalesContractNo()); |
| | | dto.setCustomerName(salesLedger.getCustomerName()); |
| | | dto.setProjectName(salesLedger.getProjectName()); |
| | | dto.setSalesman(salesLedger.getSalesman()); |
| | | dto.setExecutionDate(salesLedger.getExecutionDate() != null ? salesLedger.getExecutionDate().atStartOfDay() : null); |
| | | dto.setDeliveryDate(salesLedger.getDeliveryDate()); |
| | | dto.setRemakes(salesLedger.getRemarks()); |
| | | dto.setCompanyName("鹤壁天沐钢化玻璃厂"); |
| | | |
| | | // 送货地址 |
| | | if (salesLedger.getCustomerId() != null) { |
| | | Customer customer = customerMapper.selectById(salesLedger.getCustomerId()); |
| | | if (customer != null) { |
| | | StringBuilder address = new StringBuilder(); |
| | | if (customer.getRegionsId() != null) { |
| | | CustomerRegions regions = customerRegionsService.getById(customer.getRegionsId()); |
| | | if (regions != null) { |
| | | address.append(regions.getRegionsName()); |
| | | } |
| | | } |
| | | if (StringUtils.isNotEmpty(customer.getCompanyAddress())) { |
| | | address.append(customer.getCompanyAddress()); |
| | | } |
| | | dto.setCompanyAddress(address.toString()); |
| | | } |
| | | } |
| | | |
| | | // 制单员 |
| | | if (StringUtils.isNotEmpty(salesLedger.getEntryPerson())) { |
| | | try { |
| | | SysUser user = sysUserMapper.selectUserById(Long.parseLong(salesLedger.getEntryPerson())); |
| | | if (user != null) { |
| | | dto.setOrderMaker(user.getNickName()); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("获取制单员信息失败: {}", e.getMessage()); |
| | | } |
| | | } |
| | | // 制单日期 (底部) |
| | | dto.setOrderMakerDate(salesLedger.getExecutionDate() != null ? salesLedger.getExecutionDate().atStartOfDay() : null); |
| | | |
| | | // 打印信息 |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | if (loginUser != null && loginUser.getUser() != null) { |
| | | dto.setPrintPeople(loginUser.getUser().getNickName()); |
| | | } |
| | | dto.setPrintTime(LocalDateTime.now()); |
| | | |
| | | // 查询产品列表 |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList( |
| | | new LambdaQueryWrapper<SalesLedgerProduct>().eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId)); |
| | | |
| | | if (CollectionUtils.isNotEmpty(products)) { |
| | | SalesLedgerProduct firstProduct = products.get(0); |
| | | dto.setProductName(firstProduct.getProductCategory() != null ? firstProduct.getProductCategory() : ""); |
| | | } |
| | | |
| | | List<SalesOrdersDto.SalesOrderItemDto> itemDtos = new ArrayList<>(); |
| | | BigDecimal subtotalQuantity = BigDecimal.ZERO; |
| | | BigDecimal subtotalArea = BigDecimal.ZERO; |
| | | BigDecimal subtotalAmount = BigDecimal.ZERO; |
| | | |
| | | for (SalesLedgerProduct p : products) { |
| | | SalesOrdersDto.SalesOrderItemDto itemDto = new SalesOrdersDto.SalesOrderItemDto(); |
| | | itemDto.setFloorCode(p.getFloorCode()); |
| | | String desc = (p.getProductCategory() != null ? p.getProductCategory() : "") + |
| | | (StringUtils.isNotBlank(p.getSpecificationModel()) ? " " + p.getSpecificationModel() : ""); |
| | | itemDto.setProductDescription(desc.trim()); |
| | | itemDto.setWidth(p.getWidth()); |
| | | itemDto.setHeight(p.getHeight()); |
| | | itemDto.setQuantity(p.getQuantity()); |
| | | |
| | | // 面积计算 |
| | | BigDecimal area = p.getSettleTotalArea() != null ? p.getSettleTotalArea() : p.getActualTotalArea(); |
| | | if (area == null && p.getWidth() != null && p.getHeight() != null && p.getQuantity() != null) { |
| | | area = p.getWidth().multiply(p.getHeight()).multiply(p.getQuantity()).divide(new BigDecimal(1000000), 2, RoundingMode.HALF_UP); |
| | | } |
| | | itemDto.setArea(area); |
| | | itemDto.setUnitPrice(p.getTaxInclusiveUnitPrice()); |
| | | itemDto.setAmount(p.getTaxInclusiveTotalPrice()); |
| | | itemDto.setRemark(p.getRemark()); |
| | | itemDto.setProcessRequirement(p.getProcessRequirement()); |
| | | |
| | | subtotalQuantity = subtotalQuantity.add(p.getQuantity() != null ? p.getQuantity() : BigDecimal.ZERO); |
| | | subtotalArea = subtotalArea.add(area != null ? area : BigDecimal.ZERO); |
| | | subtotalAmount = subtotalAmount.add(p.getTaxInclusiveTotalPrice() != null ? p.getTaxInclusiveTotalPrice() : BigDecimal.ZERO); |
| | | |
| | | itemDtos.add(itemDto); |
| | | } |
| | | dto.setItems(itemDtos); |
| | | dto.setSubtotalQuantity(subtotalQuantity); |
| | | dto.setSubtotalArea(subtotalArea.setScale(2, RoundingMode.HALF_UP)); |
| | | dto.setSubtotalAmount(subtotalAmount); |
| | | |
| | | // 处理其他费用 |
| | | List<Long> productIds = products.stream().map(SalesLedgerProduct::getId).collect(Collectors.toList()); |
| | | BigDecimal otherFeesTotal = BigDecimal.ZERO; |
| | | if (CollectionUtils.isNotEmpty(productIds)) { |
| | | List<SalesLedgerProductProcessBind> binds = salesLedgerProductProcessBindService.list( |
| | | new LambdaQueryWrapper<SalesLedgerProductProcessBind>().in(SalesLedgerProductProcessBind::getSalesLedgerProductId, productIds)); |
| | | |
| | | if (CollectionUtils.isNotEmpty(binds)) { |
| | | Map<Integer, Integer> processQuantityMap = binds.stream() |
| | | .collect(Collectors.groupingBy(SalesLedgerProductProcessBind::getSalesLedgerProductProcessId, |
| | | Collectors.summingInt(b -> b.getQuantity() != null ? b.getQuantity() : 0))); |
| | | |
| | | List<Integer> processIds = new ArrayList<>(processQuantityMap.keySet()); |
| | | List<SalesLedgerProductProcess> processes = salesLedgerProductProcessService.listByIds(processIds); |
| | | |
| | | List<SalesOrdersDto.OtherFeeDto> otherFeeDtos = new ArrayList<>(); |
| | | |
| | | for (SalesLedgerProductProcess proc : processes) { |
| | | SalesOrdersDto.OtherFeeDto feeDto = new SalesOrdersDto.OtherFeeDto(); |
| | | feeDto.setFeeName(proc.getProcessName()); |
| | | feeDto.setUnitPrice(proc.getUnitPrice()); |
| | | Integer qty = processQuantityMap.get(proc.getId()); |
| | | feeDto.setQuantity(new BigDecimal(qty != null ? qty : 0)); |
| | | BigDecimal amount = proc.getUnitPrice() != null ? proc.getUnitPrice().multiply(feeDto.getQuantity()) : BigDecimal.ZERO; |
| | | feeDto.setAmount(amount); |
| | | otherFeeDtos.add(feeDto); |
| | | otherFeesTotal = otherFeesTotal.add(amount); |
| | | } |
| | | dto.setOtherFees(otherFeeDtos); |
| | | } |
| | | } |
| | | |
| | | dto.setTotalQuantity(subtotalQuantity); |
| | | dto.setTotalArea(dto.getSubtotalArea()); |
| | | dto.setTotalAmount(subtotalAmount.add(otherFeesTotal)); |
| | | dto.setTotalAmountDisplay(dto.getTotalAmount().setScale(2, RoundingMode.HALF_UP).toString() + "元"); |
| | | |
| | | return dto; |
| | | } |
| | | |
| | | @Override |
| | | public SalesInvoicesDto salesInvoices(List<Long> salesLedgerIds) { |
| | | if (CollectionUtils.isEmpty(salesLedgerIds)) { |
| | | throw new ServiceException("销售发货单打印失败,销售订单不能为空"); |
| | | } |
| | | |
| | | List<SalesLedger> ledgers = salesLedgerMapper.selectBatchIds(salesLedgerIds); |
| | | if (CollectionUtils.isEmpty(ledgers)) { |
| | | throw new ServiceException("销售发货单打印失败,未找到对应台账记录"); |
| | | } |
| | | |
| | | Long customerId = ledgers.get(0).getCustomerId(); |
| | | for (SalesLedger ledger : ledgers) { |
| | | if (!Objects.equals(customerId, ledger.getCustomerId())) { |
| | | throw new ServiceException("销售发货单合并打印只能是同一个客户"); |
| | | } |
| | | } |
| | | |
| | | SalesInvoicesDto dto = new SalesInvoicesDto(); |
| | | |
| | | Customer customer = customerMapper.selectById(customerId); |
| | | if (customer != null) { |
| | | dto.setCustomerName(customer.getCustomerName()); |
| | | dto.setContactPerson(customer.getContactPerson()); |
| | | dto.setContactPhone(customer.getContactPhone()); |
| | | |
| | | StringBuilder address = new StringBuilder(); |
| | | if (customer.getRegionsId() != null) { |
| | | CustomerRegions regions = customerRegionsService.getById(customer.getRegionsId()); |
| | | if (regions != null) { |
| | | address.append(regions.getRegionsName()); |
| | | } |
| | | } |
| | | if (StringUtils.isNotEmpty(customer.getCompanyAddress())) { |
| | | address.append(customer.getCompanyAddress()); |
| | | } |
| | | dto.setCompanyAddress(address.toString()); |
| | | } |
| | | |
| | | // 发货单号 (XF + 日期 + 序列) |
| | | String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd")); |
| | | String redisKey = "sales:delivery:seq:" + dateStr; |
| | | Long seq = redisTemplate.opsForValue().increment(redisKey); |
| | | if (seq != null && seq == 1) { |
| | | redisTemplate.expire(redisKey, 48, TimeUnit.HOURS); |
| | | } |
| | | dto.setDeliveryNo("XF" + dateStr + String.format("%03d", seq != null ? seq : 1)); |
| | | |
| | | // 对方单号 |
| | | // dto.setExternalOrderNo(ledgers.get(0).getCustomerContractNo()); |
| | | |
| | | // 查询所有产品 |
| | | List<SalesLedgerProduct> allProducts = salesLedgerProductMapper.selectList( |
| | | new LambdaQueryWrapper<SalesLedgerProduct>().in(SalesLedgerProduct::getSalesLedgerId, salesLedgerIds)); |
| | | |
| | | if (CollectionUtils.isNotEmpty(allProducts)) { |
| | | Map<Long, SalesLedger> ledgerMap = ledgers.stream() |
| | | .collect(Collectors.toMap(SalesLedger::getId, Function.identity())); |
| | | |
| | | Map<Long, List<SalesLedgerProduct>> groupedData = new LinkedHashMap<>(); |
| | | for (SalesLedgerProduct p : allProducts) { |
| | | groupedData.computeIfAbsent(p.getSalesLedgerId(), k -> new ArrayList<>()).add(p); |
| | | } |
| | | |
| | | List<SalesInvoicesDto.InvoiceOrderGroupDto> groups = new ArrayList<>(); |
| | | BigDecimal totalQty = BigDecimal.ZERO; |
| | | BigDecimal totalArea = BigDecimal.ZERO; |
| | | |
| | | for (Map.Entry<Long, List<SalesLedgerProduct>> ledgerEntry : groupedData.entrySet()) { |
| | | SalesLedger ledger = ledgerMap.get(ledgerEntry.getKey()); |
| | | String orderNo = ledger != null ? ledger.getSalesContractNo() : ""; |
| | | List<SalesLedgerProduct> products = ledgerEntry.getValue(); |
| | | |
| | | SalesInvoicesDto.InvoiceOrderGroupDto group = new SalesInvoicesDto.InvoiceOrderGroupDto(); |
| | | group.setSalesContractNo(orderNo); |
| | | if (CollectionUtils.isNotEmpty(products)) { |
| | | group.setProductName(products.get(0).getProductCategory()); |
| | | } |
| | | |
| | | List<SalesInvoicesDto.InvoiceItemDto> itemDtos = new ArrayList<>(); |
| | | BigDecimal groupQty = BigDecimal.ZERO; |
| | | BigDecimal groupArea = BigDecimal.ZERO; |
| | | |
| | | for (SalesLedgerProduct p : products) { |
| | | SalesInvoicesDto.InvoiceItemDto item = new SalesInvoicesDto.InvoiceItemDto(); |
| | | item.setFloorCode(p.getFloorCode()); |
| | | item.setWidthHeight((p.getWidth() != null ? p.getWidth().stripTrailingZeros().toPlainString() : "0") + |
| | | " * " + (p.getHeight() != null ? p.getHeight().stripTrailingZeros().toPlainString() : "0")); |
| | | item.setQuantity(p.getQuantity()); |
| | | |
| | | // 面积 |
| | | BigDecimal area = p.getSettleTotalArea() != null ? p.getSettleTotalArea() : p.getActualTotalArea(); |
| | | if (area == null && p.getWidth() != null && p.getHeight() != null && p.getQuantity() != null) { |
| | | area = p.getWidth().multiply(p.getHeight()).multiply(p.getQuantity()).divide(new BigDecimal(1000000), 2, RoundingMode.HALF_UP); |
| | | } |
| | | item.setArea(area); |
| | | item.setRemark(p.getRemark()); |
| | | item.setProcessRequirement(p.getProcessRequirement()); |
| | | |
| | | itemDtos.add(item); |
| | | groupQty = groupQty.add(p.getQuantity() != null ? p.getQuantity() : BigDecimal.ZERO); |
| | | groupArea = groupArea.add(area != null ? area : BigDecimal.ZERO); |
| | | } |
| | | |
| | | group.setItems(itemDtos); |
| | | group.setGroupTotalQuantity(groupQty); |
| | | group.setGroupTotalArea(groupArea.setScale(2, RoundingMode.HALF_UP)); |
| | | groups.add(group); |
| | | |
| | | totalQty = totalQty.add(groupQty); |
| | | totalArea = totalArea.add(groupArea); |
| | | } |
| | | dto.setGroups(groups); |
| | | dto.setTotalQuantity(totalQty); |
| | | dto.setTotalArea(totalArea.setScale(2, RoundingMode.HALF_UP)); |
| | | } |
| | | |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | if (loginUser != null && loginUser.getUser() != null) { |
| | | dto.setOrderMaker(loginUser.getUser().getNickName()); |
| | | } |
| | | dto.setExecutionDate(LocalDateTime.now()); |
| | | |
| | | return dto; |
| | | } |
| | | |
| | | private int findFirstMissingSequence(List<Integer> sequences) { |
| | | if (sequences.isEmpty()) { |
| | | return 1; |