| | |
| | | import com.ruoyi.basic.mapper.ProductModelMapper; |
| | | import com.ruoyi.basic.pojo.Customer; |
| | | import com.ruoyi.basic.pojo.CustomerRegions; |
| | | import com.ruoyi.basic.pojo.ProductModel; |
| | | import com.ruoyi.basic.service.ICustomerRegionsService; |
| | | import com.ruoyi.common.enums.FileNameType; |
| | | import com.ruoyi.common.enums.SaleEnum; |
| | |
| | | productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedger.getId()); |
| | | productWrapper.eq(SalesLedgerProduct::getType, 1); |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper); |
| | | Map<Long, ProductModel> productModelMap = Collections.emptyMap(); |
| | | if (CollectionUtils.isNotEmpty(products)) { |
| | | List<Long> productModelIds = products.stream() |
| | | .map(SalesLedgerProduct::getProductModelId) |
| | | .filter(Objects::nonNull) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | if (CollectionUtils.isNotEmpty(productModelIds)) { |
| | | List<ProductModel> productModels = productModelMapper.selectBatchIds(productModelIds); |
| | | if (CollectionUtils.isNotEmpty(productModels)) { |
| | | productModelMap = productModels.stream().collect(Collectors.toMap(ProductModel::getId, Function.identity())); |
| | | } |
| | | } |
| | | } |
| | | for (SalesLedgerProduct product : products) { |
| | | product.setOriginalNoInvoiceNum(product.getNoInvoiceNum()); |
| | | // 提供临时未开票数,未开票金额供前段计算 |
| | |
| | | List<SalesLedgerProductProcess> processList = salesLedgerProductProcessService.listByIds(processIds); |
| | | processList.forEach(p -> p.setQuantity(processQuantityMap.get(p.getId()))); |
| | | product.setSalesProductProcessList(processList); |
| | | } |
| | | ProductModel productModel = productModelMap.get(product.getProductModelId()); |
| | | if (productModel != null) { |
| | | product.setThickness(productModel.getThickness()); |
| | | } |
| | | if (product.getWidth() != null && product.getHeight() != null) { |
| | | BigDecimal pieceArea = product.getWidth().multiply(product.getHeight()).divide(new BigDecimal(1000000), 2, RoundingMode.HALF_UP); |
| | | if (product.getActualPieceArea() == null) { |
| | | product.setActualPieceArea(pieceArea); |
| | | } |
| | | BigDecimal quantity = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity(); |
| | | if (product.getActualTotalArea() == null) { |
| | | product.setActualTotalArea(pieceArea.multiply(quantity).setScale(2, RoundingMode.HALF_UP)); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | // 客户数据 |
| | | List<Customer> customers = customerMapper.selectList(new LambdaQueryWrapper<Customer>().in(Customer::getCustomerName, |
| | | salesLedgerImportDtoList.stream().map(SalesLedgerImportDto::getCustomerName).collect(Collectors.toList()))); |
| | | // // 规格型号数据 |
| | | // List<ProductModel> productModels = productModelMapper.selectList(new LambdaQueryWrapper<ProductModel>().in(ProductModel::getModel, |
| | | // salesLedgerProductImportDtoList.stream().map(SalesLedgerImportDto::getSpecificationModel).collect(Collectors.toList()))); |
| | | // // 产品大类数据 |
| | | // List<Product> productList = productMapper.selectList(new LambdaQueryWrapper<Product>().in(Product::getProductName, |
| | | // salesLedgerProductImportDtoList.stream().map(SalesLedgerImportDto::getProductCategory).collect(Collectors.toList()))); |
| | | List<Map<String, Object>> list = productModelMapper.getProductAndModelList(); |
| | | // 录入人数据 |
| | | List<SysUser> sysUsers = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getNickName, |
| | |
| | | .eq(SalesLedger::getSalesContractNo, salesLedgerImportDto.getSalesContractNo()) |
| | | .last("LIMIT 1")); |
| | | if (salesLedger1 != null) { |
| | | continue; |
| | | throw new ServiceException("导入失败:合同号 [" + salesLedgerImportDto.getSalesContractNo() + "] 已存在,请检查后重新导入"); |
| | | } |
| | | SalesLedger salesLedger = new SalesLedger(); |
| | | BeanUtils.copyProperties(salesLedgerImportDto, salesLedger); |
| | |
| | | .map(SysUser::getUserId) |
| | | .orElse(null); |
| | | if (aLong == null) |
| | | throw new RuntimeException("录入人:" + salesLedger.getEntryPerson() + ",无对应用户!"); |
| | | throw new ServiceException("录入人:" + salesLedger.getEntryPerson() + ",无对应用户!"); |
| | | salesLedger.setEntryPerson(aLong.toString()); |
| | | // 销售产品数据绑定,通过销售单号获取对应销售产品数据 |
| | | List<SalesLedgerProductImportDto> salesLedgerProductImportDtos = salesLedgerProductImportDtoList.stream() |
| | |
| | | salesLedger.setContractAmount(salesLedgerProductImportDtos.stream() |
| | | .map(SalesLedgerProductImportDto::getTaxInclusiveTotalPrice) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | // 发货状态 |
| | | salesLedger.setDeliveryStatus(0); |
| | | salesLedgerMapper.insert(salesLedger); |
| | | |
| | | |
| | | 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.setProductModelId(Long.parseLong(map.get("modelId").toString())); |
| | | salesLedgerProduct.setProductId(Long.parseLong(map.get("id").toString())); |
| | | }); |
| | | // salesLedgerProduct.setProductId(productList.stream() |
| | | // .filter(product -> product.getProductName().equals(salesLedgerProduct.getProductCategory())) |
| | | // .findFirst() |
| | | // .map(Product::getId) |
| | | // .orElse(null)); |
| | | // salesLedgerProduct.setProductModelId(productModels.stream() |
| | | // .filter(productModel -> productModel.getModel().equals(salesLedgerProduct.getSpecificationModel())) |
| | | // .findFirst() |
| | | // .map(ProductModel::getId) |
| | | // .orElse(null)); |
| | | salesLedgerProduct.setRegister(loginUser.getNickName()); |
| | | salesLedgerProduct.setRegisterDate(LocalDateTime.now()); |
| | | salesLedgerProduct.setApproveStatus(0); |
| | | salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProductImportDto.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | |
| | | // 处理额外加工信息 |
| | | 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") |
| | | ); |
| | | if (process != null) { |
| | | SalesLedgerProductProcess p = new SalesLedgerProductProcess(); |
| | | p.setId(process.getId()); |
| | | p.setQuantity(quantity.intValue()); |
| | | processList.add(p); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("解析额外加工数量失败: {}", qtyStr); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if (!processList.isEmpty()) { |
| | | salesLedgerProductProcessBindService.updateProductProcessBind(processList, salesLedgerProduct.getId()); |
| | | } |
| | | } |
| | | |
| | | // 添加生产数据 |
| | | salesLedgerProductServiceImpl.addProductionData(salesLedgerProduct); |
| | | } |
| | |
| | | if (salesLedger.getId() == null) { |
| | | String contractNo = generateSalesContractNo(); |
| | | salesLedger.setSalesContractNo(contractNo); |
| | | salesLedger.setDeliveryStatus(0); |
| | | salesLedgerMapper.insert(salesLedger); |
| | | } else { |
| | | if (salesLedger.getDeliveryStatus() == 1) { |
| | | throw new ServiceException("订单已发货,禁止编辑"); |
| | | } |
| | | salesLedgerMapper.updateById(salesLedger); |
| | | } |
| | | |
| | |
| | | // 清空销售产品绑定的加工 |
| | | salesLedgerProductProcessBindService.updateProductProcessBind(salesLedgerProduct.getSalesProductProcessList(), salesLedgerProduct.getId()); |
| | | // 添加生产数据 |
| | | salesLedgerProductServiceImpl.addProductionData(salesLedgerProduct); |
| | | // salesLedgerProductServiceImpl.addProductionData(salesLedgerProduct); |
| | | } |
| | | } |
| | | } |
| | |
| | | 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; |
| | | } |
| | | |
| | | @Override |
| | | public List<SalesLabelDto> salesLabel(Long salesLedgerId) { |
| | | if (salesLedgerId == null) { |
| | | throw new ServiceException("打印标签失败,数据不能为空"); |
| | | } |
| | | SalesLedger salesLedger = baseMapper.selectById(salesLedgerId); |
| | | if (salesLedger == null) { |
| | | throw new ServiceException("打印失败,销售订单不存在"); |
| | | } |
| | | |
| | | // 查询产品列表 |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList( |
| | | new LambdaQueryWrapper<SalesLedgerProduct>().eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId)); |
| | | |
| | | // 查询客户地址 |
| | | String fullAddress = ""; |
| | | if (salesLedger.getCustomerId() != null) { |
| | | Customer customer = customerMapper.selectById(salesLedger.getCustomerId()); |
| | | if (customer != null) { |
| | | StringBuilder addressSb = new StringBuilder(); |
| | | if (customer.getRegionsId() != null) { |
| | | CustomerRegions regions = customerRegionsService.getById(customer.getRegionsId()); |
| | | if (regions != null) { |
| | | addressSb.append(regions.getRegionsName()); |
| | | } |
| | | } |
| | | if (StringUtils.isNotEmpty(customer.getCompanyAddress())) { |
| | | addressSb.append(customer.getCompanyAddress()); |
| | | } |
| | | fullAddress = addressSb.toString(); |
| | | } |
| | | } |
| | | |
| | | List<SalesLabelDto> list = new ArrayList<>(); |
| | | if (CollectionUtils.isNotEmpty(products)) { |
| | | for (SalesLedgerProduct p : products) { |
| | | SalesLabelDto dto = new SalesLabelDto(); |
| | | dto.setCustomerName(salesLedger.getCustomerName()); |
| | | dto.setSalesContractNo(salesLedger.getSalesContractNo()); |
| | | dto.setProductName(p.getProductCategory()); |
| | | |
| | | // 宽*高=数量 |
| | | String specification = (p.getWidth() != null ? p.getWidth().stripTrailingZeros().toPlainString() : "0") + "*" + |
| | | (p.getHeight() != null ? p.getHeight().stripTrailingZeros().toPlainString() : "0") + "=" + |
| | | (p.getQuantity() != null ? p.getQuantity().stripTrailingZeros().toPlainString() : "0"); |
| | | dto.setSpecification(specification); |
| | | |
| | | // 客户地址 + 楼层编号 |
| | | dto.setFloorCode(fullAddress + (StringUtils.isNotEmpty(p.getFloorCode()) ? " " + p.getFloorCode() : "")); |
| | | list.add(dto); |
| | | } |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | private int findFirstMissingSequence(List<Integer> sequences) { |
| | | if (sequences.isEmpty()) { |
| | | return 1; |