| | |
| | | import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.sale.vo.order.ErpSaleOrderPageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.sale.vo.order.ErpSaleOrderRespVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.sale.vo.order.ErpSaleOrderSaveReqVO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.sale.ErpSaleOrderDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.sale.ErpSaleOrderItemDO; |
| | | import cn.iocoder.yudao.module.erp.service.product.ErpProductService; |
| | | import cn.iocoder.yudao.module.erp.service.sale.ErpSaleOrderService; |
| | | import cn.iocoder.yudao.module.erp.service.stock.ErpStockService; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import cn.iocoder.yudao.module.mes.api.mps.MesProMpsApi; |
| | | import cn.iocoder.yudao.module.mes.api.salesnotice.MesWmSalesNoticeApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | |
| | | @Resource |
| | | private ErpSaleOrderService saleOrderService; |
| | | @Resource |
| | | private ErpStockService stockService; |
| | | @Resource |
| | | private ErpProductService productService; |
| | | @Resource |
| | | private CrmCustomerApi customerApi; |
| | | |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | | @Resource |
| | | private MesWmSalesNoticeApi salesNoticeApi; |
| | | @Resource |
| | | private MesProMpsApi mpsApi; |
| | | |
| | | @PostMapping("/create") |
| | | @Operation(summary = "创建销售订单") |
| | |
| | | return success(null); |
| | | } |
| | | List<ErpSaleOrderItemDO> saleOrderItemList = saleOrderService.getSaleOrderItemListByOrderId(id); |
| | | Map<Long, ErpProductRespVO> productMap = productService.getProductVOMap( |
| | | Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap( |
| | | convertSet(saleOrderItemList, ErpSaleOrderItemDO::getProductId)); |
| | | return success(BeanUtils.toBean(saleOrder, ErpSaleOrderRespVO.class, saleOrderVO -> |
| | | saleOrderVO.setItems(BeanUtils.toBean(saleOrderItemList, ErpSaleOrderRespVO.Item.class, item -> { |
| | | BigDecimal stockCount = stockService.getStockCount(item.getProductId()); |
| | | item.setStockCount(stockCount != null ? stockCount : BigDecimal.ZERO); |
| | | MapUtils.findAndThen(productMap, item.getProductId(), product -> item.setProductName(product.getName()) |
| | | .setProductBarCode(product.getBarCode()).setProductUnitName(product.getUnitName())); |
| | | // 库存信息暂不查询(仓储物流模块) |
| | | item.setStockCount(BigDecimal.ZERO); |
| | | MapUtils.findAndThen(itemMap, item.getProductId(), mdmItem -> item.setProductName(mdmItem.getName()) |
| | | .setProductBarCode(mdmItem.getBarCode())); |
| | | })))); |
| | | } |
| | | |
| | |
| | | List<ErpSaleOrderItemDO> saleOrderItemList = saleOrderService.getSaleOrderItemListByOrderIds( |
| | | convertSet(pageResult.getList(), ErpSaleOrderDO::getId)); |
| | | Map<Long, List<ErpSaleOrderItemDO>> saleOrderItemMap = convertMultiMap(saleOrderItemList, ErpSaleOrderItemDO::getOrderId); |
| | | // 1.2 产品信息 |
| | | Map<Long, ErpProductRespVO> productMap = productService.getProductVOMap( |
| | | // 1.2 物料信息 |
| | | Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap( |
| | | convertSet(saleOrderItemList, ErpSaleOrderItemDO::getProductId)); |
| | | // 1.3 客户信息(使用 CRM 客户 API) |
| | | Map<Long, CrmCustomerRespDTO> customerMap = customerApi.getCustomerMap( |
| | |
| | | // 1.4 管理员信息 |
| | | Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap( |
| | | convertSet(pageResult.getList(), saleOrder -> Long.parseLong(saleOrder.getCreator()))); |
| | | // 1.5 发货通知单信息 |
| | | Map<Long, Boolean> salesNoticeMap = salesNoticeApi.checkSalesNoticeBySaleOrderIds( |
| | | convertSet(pageResult.getList(), ErpSaleOrderDO::getId)); |
| | | // 1.6 检查是否可以生成发货通知单(需要生产的明细对应的 MPS 是否已完成) |
| | | // 筛选出需要生产的明细ID |
| | | List<Long> needProductionItemIds = saleOrderItemList.stream() |
| | | .filter(item -> item.getNeedProduction() != null && item.getNeedProduction() == 1) |
| | | .map(ErpSaleOrderItemDO::getId) |
| | | .toList(); |
| | | Map<Long, Boolean> canCreateSalesNoticeMap = new java.util.HashMap<>(); |
| | | if (!needProductionItemIds.isEmpty()) { |
| | | // 按订单分组明细ID |
| | | Map<Long, List<Long>> orderItemIdsMap = new java.util.HashMap<>(); |
| | | for (ErpSaleOrderItemDO item : saleOrderItemList) { |
| | | orderItemIdsMap.computeIfAbsent(item.getOrderId(), k -> new java.util.ArrayList<>()) |
| | | .add(item.getId()); |
| | | } |
| | | // 检查每个订单 |
| | | for (Map.Entry<Long, List<Long>> entry : orderItemIdsMap.entrySet()) { |
| | | List<Long> itemIds = entry.getValue().stream() |
| | | .filter(id -> { |
| | | ErpSaleOrderItemDO item = saleOrderItemList.stream() |
| | | .filter(i -> i.getId().equals(id)) |
| | | .findFirst().orElse(null); |
| | | return item != null && item.getNeedProduction() != null && item.getNeedProduction() == 1; |
| | | } |
| | | ).toList(); |
| | | if (itemIds.isEmpty()) { |
| | | canCreateSalesNoticeMap.put(entry.getKey(), true); |
| | | } else { |
| | | canCreateSalesNoticeMap.put(entry.getKey(), mpsApi.checkCanCreateSalesNotice(itemIds)); |
| | | } |
| | | } |
| | | } else { |
| | | // 没有需要生产的明细,都可以生成发货通知单 |
| | | pageResult.getList().forEach(order -> canCreateSalesNoticeMap.put(order.getId(), true)); |
| | | } |
| | | // 2. 开始拼接 |
| | | return BeanUtils.toBean(pageResult, ErpSaleOrderRespVO.class, saleOrder -> { |
| | | saleOrder.setItems(BeanUtils.toBean(saleOrderItemMap.get(saleOrder.getId()), ErpSaleOrderRespVO.Item.class, |
| | | item -> MapUtils.findAndThen(productMap, item.getProductId(), product -> item.setProductName(product.getName()) |
| | | .setProductBarCode(product.getBarCode()).setProductUnitName(product.getUnitName())))); |
| | | item -> MapUtils.findAndThen(itemMap, item.getProductId(), mdmItem -> item.setProductName(mdmItem.getName()) |
| | | .setProductBarCode(mdmItem.getBarCode())))); |
| | | saleOrder.setProductNames(CollUtil.join(saleOrder.getItems(), ",", ErpSaleOrderRespVO.Item::getProductName)); |
| | | MapUtils.findAndThen(customerMap, saleOrder.getCustomerId(), supplier -> saleOrder.setCustomerName(supplier.getName())); |
| | | MapUtils.findAndThen(userMap, Long.parseLong(saleOrder.getCreator()), user -> saleOrder.setCreatorName(user.getNickname())); |
| | | // 设置是否已生成发货通知单 |
| | | saleOrder.setHasSalesNotice(salesNoticeMap.getOrDefault(saleOrder.getId(), false)); |
| | | // 设置是否可以生成发货通知单:已生成发货通知单时不显示按钮 |
| | | saleOrder.setCanCreateSalesNotice(!saleOrder.getHasSalesNotice() && canCreateSalesNoticeMap.getOrDefault(saleOrder.getId(), true)); |
| | | // 设置是否有需要生产的明细 |
| | | boolean hasNeedProduction = saleOrder.getItems().stream() |
| | | .anyMatch(item -> item.getNeedProduction() != null && item.getNeedProduction() == 1); |
| | | saleOrder.setNeedProduction(hasNeedProduction ? 1 : 0); |
| | | }); |
| | | } |
| | | |