| | |
| | | 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; |