| | |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.salesnotice.vo.MesWmSalesNoticePageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.salesnotice.vo.MesWmSalesNoticeSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesLineDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.salesnotice.MesWmSalesNoticeDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.salesnotice.MesWmSalesNoticeLineDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesMapper; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesLineMapper; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.wm.salesnotice.MesWmSalesNoticeMapper; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.wm.salesnotice.MesWmSalesNoticeLineMapper; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmOutStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmProductSalesStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.service.md.autocode.MesMdAutoCodeRecordService; |
| | | import cn.iocoder.yudao.module.mes.service.md.client.MesMdClientService; |
| | | import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | |
| | | private ErpSaleOrderApi saleOrderApi; |
| | | @Resource |
| | | private MesMdAutoCodeRecordService autoCodeRecordService; |
| | | |
| | | @Resource |
| | | private MesWmProductSalesMapper productSalesMapper; |
| | | @Resource |
| | | private MesWmProductSalesLineMapper productSalesLineMapper; |
| | | @Resource |
| | | private MesWmSalesNoticeLineMapper salesNoticeLineMapper; |
| | | |
| | | @Override |
| | | public Long createSalesNotice(MesWmSalesNoticeSaveReqVO createReqVO) { |
| | |
| | | |
| | | @Override |
| | | public void finishSalesNotice(Long id) { |
| | | // 校验存在 |
| | | MesWmSalesNoticeDO notice = validateSalesNoticeExists(id); |
| | | // 更新状态为已完成 |
| | | // 已废弃,保留兼容性 |
| | | updateSalesNoticeOutStatus(id); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateSalesNoticeLineOutCount(Long noticeLineId, BigDecimal outCount) { |
| | | if (noticeLineId == null || outCount == null) { |
| | | return; |
| | | } |
| | | // 1. 获取发货通知单行 |
| | | MesWmSalesNoticeLineDO line = salesNoticeLineMapper.selectById(noticeLineId); |
| | | if (line == null) { |
| | | return; |
| | | } |
| | | // 2. 累加出库数量 |
| | | BigDecimal newOutCount = (line.getOutCount() != null ? line.getOutCount() : BigDecimal.ZERO).add(outCount); |
| | | // 3. 更新行 |
| | | salesNoticeLineMapper.updateById(new MesWmSalesNoticeLineDO() |
| | | .setId(noticeLineId) |
| | | .setOutCount(newOutCount)); |
| | | // 4. 更新主单出库状态 |
| | | updateSalesNoticeOutStatus(line.getNoticeId()); |
| | | } |
| | | |
| | | @Override |
| | | public void updateSalesNoticeOutStatus(Long noticeId) { |
| | | if (noticeId == null) { |
| | | return; |
| | | } |
| | | // 1. 获取发货通知单 |
| | | MesWmSalesNoticeDO notice = salesNoticeMapper.selectById(noticeId); |
| | | if (notice == null) { |
| | | return; |
| | | } |
| | | // 2. 获取发货通知单行 |
| | | List<MesWmSalesNoticeLineDO> lines = salesNoticeLineMapper.selectListByNoticeId(noticeId); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | return; |
| | | } |
| | | // 3. 计算出库状态 |
| | | int outStatus = calculateOutStatus(lines); |
| | | // 4. 更新出库状态 |
| | | salesNoticeMapper.updateById(new MesWmSalesNoticeDO() |
| | | .setId(id).setStatus(FINISHED.getStatus())); |
| | | .setId(noticeId) |
| | | .setOutStatus(outStatus)); |
| | | // 5. 如果全部出库,更新单据状态为已完成 |
| | | if (outStatus == MesWmOutStatusEnum.ALL_OUT.getStatus()) { |
| | | salesNoticeMapper.updateById(new MesWmSalesNoticeDO() |
| | | .setId(noticeId) |
| | | .setStatus(FINISHED.getStatus())); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 计算出库状态 |
| | | */ |
| | | private int calculateOutStatus(List<MesWmSalesNoticeLineDO> lines) { |
| | | boolean allOut = true; |
| | | boolean anyOut = false; |
| | | |
| | | for (MesWmSalesNoticeLineDO line : lines) { |
| | | BigDecimal quantity = line.getQuantity() != null ? line.getQuantity() : BigDecimal.ZERO; |
| | | BigDecimal outCount = line.getOutCount() != null ? line.getOutCount() : BigDecimal.ZERO; |
| | | |
| | | if (outCount.compareTo(BigDecimal.ZERO) > 0) { |
| | | anyOut = true; |
| | | } |
| | | if (outCount.compareTo(quantity) < 0) { |
| | | allOut = false; |
| | | } |
| | | } |
| | | |
| | | if (allOut) { |
| | | return MesWmOutStatusEnum.ALL_OUT.getStatus(); |
| | | } else if (anyOut) { |
| | | return MesWmOutStatusEnum.PART_OUT.getStatus(); |
| | | } else { |
| | | return MesWmOutStatusEnum.NOT_OUT.getStatus(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long generateProductSalesFromNotice(Long noticeId) { |
| | | // 1. 校验发货通知单存在且为待出库状态 |
| | | MesWmSalesNoticeDO notice = validateSalesNoticeExists(noticeId); |
| | | if (ObjUtil.notEqual(APPROVED.getStatus(), notice.getStatus())) { |
| | | throw exception(WM_SALES_NOTICE_STATUS_NOT_APPROVED); |
| | | } |
| | | |
| | | // 2. 获取发货通知单行 |
| | | List<MesWmSalesNoticeLineDO> noticeLines = salesNoticeLineService.getSalesNoticeLineListByNoticeId(noticeId); |
| | | if (CollUtil.isEmpty(noticeLines)) { |
| | | throw exception(WM_SALES_NOTICE_LINE_EMPTY); |
| | | } |
| | | |
| | | // 3. 生成销售出库单编码 |
| | | String code = autoCodeRecordService.generateAutoCode("WM_PRODUCT_SALES_CODE", null); |
| | | |
| | | // 4. 创建销售出库单主表 |
| | | MesWmProductSalesDO productSales = new MesWmProductSalesDO(); |
| | | productSales.setCode(code); |
| | | productSales.setName("发货通知单[" + notice.getCode() + "]出库"); |
| | | productSales.setClientId(notice.getClientId()); |
| | | productSales.setSaleOrderId(notice.getSaleOrderId()); |
| | | productSales.setSalesOrderCode(notice.getSaleOrderCode()); |
| | | productSales.setNoticeId(noticeId); |
| | | productSales.setSalesDate(notice.getSalesDate()); |
| | | // 收货信息从发货通知单带入 |
| | | productSales.setContactName(notice.getRecipientName()); |
| | | productSales.setContactTelephone(notice.getRecipientTelephone()); |
| | | productSales.setContactAddress(notice.getRecipientAddress()); |
| | | productSales.setStatus(MesWmProductSalesStatusEnum.PREPARE.getStatus()); |
| | | productSalesMapper.insert(productSales); |
| | | |
| | | // 5. 遍历发货通知单行创建销售出库单行 |
| | | for (MesWmSalesNoticeLineDO noticeLine : noticeLines) { |
| | | MesWmProductSalesLineDO salesLine = new MesWmProductSalesLineDO(); |
| | | salesLine.setSalesId(productSales.getId()); |
| | | salesLine.setSaleOrderItemId(noticeLine.getSaleOrderItemId()); |
| | | salesLine.setNoticeLineId(noticeLine.getId()); |
| | | salesLine.setItemId(noticeLine.getItemId()); |
| | | salesLine.setQuantity(noticeLine.getQuantity()); |
| | | salesLine.setBatchId(noticeLine.getBatchId()); |
| | | salesLine.setBatchCode(noticeLine.getBatchCode()); |
| | | salesLine.setOqcCheckFlag(noticeLine.getOqcCheckFlag()); |
| | | productSalesLineMapper.insert(salesLine); |
| | | } |
| | | |
| | | return productSales.getId(); |
| | | } |
| | | |
| | | } |