| | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.enums.StockQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.enums.StockUnQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum; |
| | | import com.ruoyi.framework.web.domain.R; |
| | | import com.ruoyi.procurementrecord.utils.StockUtils; |
| | | import com.ruoyi.production.dto.ProductStructureDto; |
| | | import com.ruoyi.production.mapper.*; |
| | | import com.ruoyi.production.pojo.*; |
| | | import com.ruoyi.production.service.impl.ProductOrderServiceImpl; |
| | | import com.ruoyi.purchase.mapper.PurchaseLedgerMapper; |
| | | import com.ruoyi.purchase.pojo.PurchaseLedger; |
| | | import com.ruoyi.quality.mapper.QualityInspectMapper; |
| | |
| | | import com.ruoyi.sales.mapper.SalesLedgerMapper; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.mapper.ShippingInfoMapper; |
| | | import com.ruoyi.sales.pojo.InvoiceRegistrationProduct; |
| | | import com.ruoyi.sales.pojo.SalesLedger; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProductFrozen; |
| | | import com.ruoyi.sales.pojo.ShippingInfo; |
| | | import com.ruoyi.sales.service.ISalesLedgerProductService; |
| | | import com.ruoyi.sales.service.ISalesLedgerProductFrozenService; |
| | | import com.ruoyi.stock.mapper.StockInventoryMapper; |
| | | import com.ruoyi.stock.pojo.StockInventory; |
| | | import lombok.AllArgsConstructor; |
| | |
| | | private ProductStructureMapper productStructureMapper; |
| | | @Autowired |
| | | private StockInventoryMapper stockInventoryMapper; |
| | | @Autowired |
| | | private ProductOrderServiceImpl productOrderServiceImpl; |
| | | @Autowired |
| | | private ISalesLedgerProductFrozenService salesLedgerProductFrozenService; |
| | | |
| | | @Override |
| | | public SalesLedgerProduct selectSalesLedgerProductById(Long id) { |
| | |
| | | return 0; |
| | | } |
| | | |
| | | checkProductionDataExist(Arrays.asList(ids)); |
| | | |
| | | // 1. 先查询要删除的子表记录,获取对应的 salesLedgerId |
| | | List<SalesLedgerProduct> deletedProducts = salesLedgerProductMapper.selectBatchIds(Arrays.asList(ids)); |
| | | if (deletedProducts.isEmpty()) { |
| | |
| | | int result = salesLedgerProductMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | //删除对应的生产订单 |
| | | deleteProductionData(Arrays.asList(ids)); |
| | | |
| | | for (Long id : ids) { |
| | | revertFrozenStock(id); |
| | | } |
| | | |
| | | // 3. 对每个主表ID进行金额更新 |
| | | for (Long salesLedgerId : mainIds) { |
| | |
| | | if (salesLedgerProduct.getId() == null) { |
| | | salesLedgerProduct.setRegisterDate(LocalDateTime.now()); |
| | | result = salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | addProductionData(salesLedgerProduct); |
| | | processStockAndProduction(salesLedgerProduct); |
| | | } else { |
| | | checkProductionDataExist(Collections.singletonList(salesLedgerProduct.getId())); |
| | | |
| | | revertFrozenStock(salesLedgerProduct.getId()); |
| | | |
| | | //查询原本的产品型号id |
| | | salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity()); |
| | | result = salesLedgerProductMapper.updateById(salesLedgerProduct); |
| | | /*删除对应的生产数据并重新新增*/ |
| | | deleteProductionData(Arrays.asList(salesLedgerProduct.getId())); |
| | | // 删除生产核算数据 |
| | | LambdaQueryWrapper<SalesLedgerProductionAccounting> reportWrapper = new LambdaQueryWrapper<>(); |
| | | reportWrapper.in(SalesLedgerProductionAccounting::getSalesLedgerId, salesLedgerId); |
| | | salesLedgerProductionAccountingMapper.delete(reportWrapper); |
| | | addProductionData(salesLedgerProduct); |
| | | |
| | | processStockAndProduction(salesLedgerProduct); |
| | | } |
| | | |
| | | // 如果插入或更新成功,并且有 salesLedgerId,才继续更新主表金额 |
| | |
| | | return result; |
| | | } |
| | | |
| | | public void processStockAndProduction(SalesLedgerProduct salesLedgerProduct) { |
| | | BigDecimal salesQty = salesLedgerProduct.getQuantity(); |
| | | if (salesQty == null) salesQty = BigDecimal.ZERO; |
| | | |
| | | BigDecimal frozenQty = BigDecimal.ZERO; |
| | | BigDecimal productionQty = salesQty; |
| | | |
| | | // 查询可用库存 |
| | | StockInventory stockInventory = stockInventoryMapper.selectOne(new LambdaQueryWrapper<StockInventory>() |
| | | .eq(StockInventory::getProductModelId, salesLedgerProduct.getProductModelId()) |
| | | .last("for update")); |
| | | if (stockInventory != null) { |
| | | BigDecimal quality = stockInventory.getQualitity() != null ? stockInventory.getQualitity() : BigDecimal.ZERO; |
| | | BigDecimal lockedQty = stockInventory.getLockedQuantity() != null ? stockInventory.getLockedQuantity() : BigDecimal.ZERO; |
| | | BigDecimal availableStock = quality.subtract(lockedQty); |
| | | |
| | | if (availableStock.compareTo(BigDecimal.ZERO) > 0) { |
| | | // 有可用库存 |
| | | if (availableStock.compareTo(salesQty) >= 0) { |
| | | frozenQty = salesQty; |
| | | productionQty = BigDecimal.ZERO; |
| | | } else { |
| | | frozenQty = availableStock; |
| | | productionQty = salesQty.subtract(frozenQty); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 冻结库存 |
| | | if (frozenQty.compareTo(BigDecimal.ZERO) > 0) { |
| | | SalesLedgerProductFrozen frozenRecord = new SalesLedgerProductFrozen(); |
| | | frozenRecord.setSalesLedgerProductId(salesLedgerProduct.getId()); |
| | | frozenRecord.setProductModelId(salesLedgerProduct.getProductModelId()); |
| | | frozenRecord.setFrozenQuantity(frozenQty); |
| | | frozenRecord.setCreateTime(LocalDateTime.now()); |
| | | salesLedgerProductFrozenService.save(frozenRecord); |
| | | |
| | | if (stockInventory != null) { |
| | | BigDecimal currentLocked = stockInventory.getLockedQuantity() != null ? stockInventory.getLockedQuantity() : BigDecimal.ZERO; |
| | | stockInventory.setLockedQuantity(currentLocked.add(frozenQty)); |
| | | stockInventoryMapper.updateById(stockInventory); |
| | | } |
| | | } |
| | | |
| | | // 如果需要生产,则生成生产数据 |
| | | if (productionQty.compareTo(BigDecimal.ZERO) > 0) { |
| | | addProductionData(salesLedgerProduct, productionQty); |
| | | } |
| | | } |
| | | |
| | | public void revertFrozenStock(Long salesLedgerProductId) { |
| | | SalesLedgerProductFrozen frozenRecord = salesLedgerProductFrozenService.getOne(new LambdaQueryWrapper<SalesLedgerProductFrozen>() |
| | | .eq(SalesLedgerProductFrozen::getSalesLedgerProductId, salesLedgerProductId) |
| | | .last("limit 1")); |
| | | if (frozenRecord != null) { |
| | | BigDecimal frozenQty = frozenRecord.getFrozenQuantity(); |
| | | if (frozenQty != null && frozenQty.compareTo(BigDecimal.ZERO) > 0) { |
| | | StockInventory stockInventory = stockInventoryMapper.selectOne(new LambdaQueryWrapper<StockInventory>() |
| | | .eq(StockInventory::getProductModelId, frozenRecord.getProductModelId()) |
| | | .last("for update")); |
| | | if (stockInventory != null) { |
| | | BigDecimal currentLocked = stockInventory.getLockedQuantity() != null ? stockInventory.getLockedQuantity() : BigDecimal.ZERO; |
| | | stockInventory.setLockedQuantity(currentLocked.subtract(frozenQty)); |
| | | stockInventoryMapper.updateById(stockInventory); |
| | | } |
| | | } |
| | | salesLedgerProductFrozenService.removeById(frozenRecord.getId()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 新增生产数据 |
| | | */ |
| | | public void addProductionData(SalesLedgerProduct salesLedgerProduct) { |
| | | public void addProductionData(SalesLedgerProduct salesLedgerProduct, BigDecimal productionQty) { |
| | | ProductOrder productOrder = new ProductOrder(); |
| | | productOrder.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId()); |
| | | productOrder.setProductModelId(salesLedgerProduct.getProductModelId()); |
| | | productOrder.setSaleLedgerProductId(salesLedgerProduct.getId()); |
| | | productOrder.setNpsNo("SC" + String.format("%08d", salesLedgerProduct.getId())); |
| | | productOrder.setQuantity(salesLedgerProduct.getQuantity());//需求数量 |
| | | String string = productOrderServiceImpl.generateNextOrderNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))); |
| | | productOrder.setNpsNo(string); |
| | | productOrder.setQuantity(productionQty);//需求数量 |
| | | productOrder.setCompleteQuantity(BigDecimal.ZERO);//完成数量 |
| | | productOrderMapper.insert(productOrder); |
| | | |
| | |
| | | int insert = productProcessRouteItemMapper.insert(productProcessRouteItem); |
| | | if (insert > 0) { |
| | | // 查询今日已存在的最大工单号 |
| | | QueryWrapper<ProductWorkOrder> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.likeRight("work_order_no", datePrefix) |
| | | .orderByDesc("work_order_no") |
| | | .last("LIMIT 1"); |
| | | |
| | | ProductWorkOrder lastWorkOrder = productWorkOrderMapper.selectOne(queryWrapper); |
| | | |
| | | ProductWorkOrder lastWorkOrder = productWorkOrderMapper.selectMax(datePrefix); |
| | | int sequenceNumber = 1; // 默认序号 |
| | | if (lastWorkOrder != null && lastWorkOrder.getWorkOrderNo() != null) { |
| | | String lastNo = lastWorkOrder.getWorkOrderNo().toString(); |
| | |
| | | ProductWorkOrder productWorkOrder = new ProductWorkOrder(); |
| | | productWorkOrder.setProductProcessRouteItemId(productProcessRouteItem.getId()); |
| | | productWorkOrder.setProductOrderId(productOrder.getId()); |
| | | productWorkOrder.setPlanQuantity(salesLedgerProduct.getQuantity()); |
| | | productWorkOrder.setPlanQuantity(productionQty); |
| | | productWorkOrder.setWorkOrderNo(workOrderNoStr); |
| | | productWorkOrder.setStatus(1); |
| | | |
| | |
| | | } |
| | | productOrder.setRouteId(processRoute.getId()); |
| | | productOrderMapper.updateById(productOrder); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查是否有报工数据 |
| | | */ |
| | | public void checkProductionDataExist(List<Long> productIds) { |
| | | if (org.springframework.util.CollectionUtils.isEmpty(productIds)) { |
| | | return; |
| | | } |
| | | // 查询productOrder |
| | | List<ProductOrder> productOrders = productOrderMapper.selectList(new LambdaQueryWrapper<ProductOrder>() |
| | | .in(ProductOrder::getSaleLedgerProductId, productIds)); |
| | | if (!CollectionUtils.isEmpty(productOrders)) { |
| | | List<Long> orderIds = productOrders.stream() |
| | | .map(ProductOrder::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 查询processRouteItems |
| | | List<ProductProcessRouteItem> allRouteItems = productProcessRouteItemMapper.selectList(new LambdaQueryWrapper<ProductProcessRouteItem>() |
| | | .in(ProductProcessRouteItem::getProductOrderId, orderIds)); |
| | | |
| | | if (!CollectionUtils.isEmpty(allRouteItems)) { |
| | | // 获取工序项ID |
| | | List<Long> routeItemIds = allRouteItems.stream() |
| | | .map(ProductProcessRouteItem::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 查询关联的工单ID |
| | | List<ProductWorkOrder> workOrders = productWorkOrderMapper.selectList(new LambdaQueryWrapper<ProductWorkOrder>() |
| | | .in(ProductWorkOrder::getProductProcessRouteItemId, routeItemIds)); |
| | | if (!CollectionUtils.isEmpty(workOrders)) { |
| | | List<Long> workOrderIds = workOrders.stream() |
| | | .map(ProductWorkOrder::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 查询关联的生产主表ID是否有数据 |
| | | Long count = productionProductMainMapper.selectCount(new LambdaQueryWrapper<ProductionProductMain>() |
| | | .in(ProductionProductMain::getWorkOrderId, workOrderIds)); |
| | | if (count != null && count > 0L) { |
| | | throw new RuntimeException("当前销售订单已有报工数据,不能删除和修改"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | //删除出库记录 |
| | | for (Long productMainId : productMainIds) { |
| | | //删除生产出库记录 |
| | | stockUtils.deleteStockOutRecord(productMainId, StockQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_OUT.getCode()); |
| | | stockUtils.deleteStockOutRecord(productMainId, StockOutQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_OUT.getCode()); |
| | | //删除报废的入库记录 |
| | | stockUtils.deleteStockInRecord(productMainId, StockUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode()); |
| | | stockUtils.deleteStockInRecord(productMainId, StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode()); |
| | | } |
| | | qualityInspects.forEach(qualityInspect -> { |
| | | //inspectState=1 已提交 不能删除 |
| | |
| | | } |
| | | }); |
| | | qualityInspectMapper.deleteByProductMainIds(productMainIds); |
| | | salesLedgerProductionAccountingMapper.delete(new LambdaQueryWrapper<SalesLedgerProductionAccounting>() |
| | | .in(SalesLedgerProductionAccounting::getProductMainId, productMainIds)); |
| | | } |
| | | |
| | | // 删除生产主表数据 |
| | |
| | | IPage<SalesLedgerProductDto> salesLedgerProductDtoIPage = salesLedgerProductMapper.listPagePurchaseLedger(page, salesLedgerProduct); |
| | | salesLedgerProductDtoIPage.getRecords().forEach(item -> { |
| | | // 判断状态 |
| | | if(item.getTaxInclusiveTotalPrice().compareTo(item.getInvoiceTotal()) == 0){ |
| | | if(item.getTaxInclusiveTotalPrice().compareTo(item.getTicketsTotal()) == 0){ |
| | | item.setStatusName("已完成付款"); |
| | | }else{ |
| | | item.setStatusName("未完成付款"); |