| | |
| | | import com.ruoyi.basic.pojo.ProductModel; |
| | | import com.ruoyi.basic.pojo.SupplierManage; |
| | | import com.ruoyi.common.enums.FileNameType; |
| | | import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | |
| | | import com.ruoyi.other.pojo.TempFile; |
| | | import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper; |
| | | import com.ruoyi.procurementrecord.pojo.ProcurementRecordStorage; |
| | | import com.ruoyi.procurementrecord.utils.StockUtils; |
| | | import com.ruoyi.project.system.domain.SysUser; |
| | | import com.ruoyi.project.system.mapper.SysUserMapper; |
| | | import com.ruoyi.purchase.dto.PurchaseLedgerDto; |
| | |
| | | import com.ruoyi.sales.pojo.SalesLedger; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import com.ruoyi.sales.service.impl.CommonFileServiceImpl; |
| | | import com.ruoyi.stock.pojo.StockInRecord; |
| | | import com.ruoyi.stock.service.StockInRecordService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | |
| | | private PurchaseLedgerTemplateMapper purchaseLedgerTemplateMapper; |
| | | @Autowired |
| | | private SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper; |
| | | @Autowired |
| | | private StockUtils stockUtils; |
| | | @Autowired |
| | | private StockInRecordService stockInRecordService; |
| | | @Value("${file.upload-dir}") |
| | | private String uploadDir; |
| | | |
| | |
| | | purchaseLedger.setRecorderName(sysUser.getNickName()); |
| | | purchaseLedger.setPhoneNumber(sysUser.getPhonenumber()); |
| | | purchaseLedger.setApprovalStatus(1); |
| | | |
| | | List<SalesLedgerProduct> productList = Optional.ofNullable(purchaseLedgerDto.getProductData()).orElse(Collections.emptyList()); |
| | | // 3. 新增或更新主表 |
| | | if (purchaseLedger.getId() == null) { |
| | | boolean isUpdate = purchaseLedger.getId() != null; |
| | | if (!isUpdate) { |
| | | purchaseLedgerMapper.insert(purchaseLedger); |
| | | } else { |
| | | // 删除采购审核,重新提交 |
| | | ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>() |
| | | .eq(ApproveProcess::getApproveType, 5) |
| | | .eq(ApproveProcess::getApproveReason, purchaseLedger.getPurchaseContractNumber()) |
| | | .eq(ApproveProcess::getApproveDelete, 0) |
| | | .last("limit 1")); |
| | | if (one != null) { |
| | | approveProcessService.delByIds(Collections.singletonList(one.getId())); |
| | | PurchaseLedger oldPurchase = purchaseLedgerMapper.selectById(purchaseLedger.getId()); |
| | | if (oldPurchase == null) { |
| | | throw new BaseException("采购台账不存在"); |
| | | } |
| | | if (CollectionUtils.isEmpty(productList)) { |
| | | throw new BaseException("产品信息不存在"); |
| | | } |
| | | // 编辑前先回滚旧的库存与质检副作用 |
| | | clearPurchaseSideEffectsBeforeEdit(oldPurchase.getId(), oldPurchase.getPurchaseContractNumber()); |
| | | purchaseLedgerMapper.updateById(purchaseLedger); |
| | | } |
| | | // 6.采购审核新增 |
| | | // addApproveByPurchase(loginUser, purchaseLedger); |
| | | |
| | | // 4. 处理子表数据 |
| | | List<SalesLedgerProduct> productList = purchaseLedgerDto.getProductData(); |
| | | if (productList != null && !productList.isEmpty()) { |
| | | if (!productList.isEmpty()) { |
| | | handleSalesLedgerProducts(purchaseLedger.getId(), productList, purchaseLedgerDto.getType()); |
| | | } |
| | | for (SalesLedgerProduct salesLedgerProduct : productList) { |
| | | // 质检 |
| | | if (Boolean.TRUE.equals(salesLedgerProduct.getIsChecked())) { |
| | | addQualityInspect(purchaseLedger, salesLedgerProduct); |
| | | } else { |
| | | //直接入库 |
| | | stockUtils.addStock(salesLedgerProduct.getProductModelId(), salesLedgerProduct.getQuantity(), StockInQualifiedRecordTypeEnum.PURCHASE_STOCK_IN.getCode(), purchaseLedger.getId()); |
| | | } |
| | | } |
| | | //新增原材料检验 审批之后才生成检验 |
| | | // if (productList != null) { |
| | |
| | | qualityInspectParamMapper.insert(param); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | private void clearPurchaseSideEffectsBeforeEdit(Long purchaseLedgerId, String purchaseContractNumber) { |
| | | clearPurchaseApproveProcess(purchaseContractNumber); |
| | | clearPurchaseQualityInspect(purchaseLedgerId); |
| | | clearPurchaseStockInRecord(purchaseLedgerId); |
| | | } |
| | | |
| | | private void clearPurchaseApproveProcess(String purchaseContractNumber) { |
| | | ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>() |
| | | .eq(ApproveProcess::getApproveType, 5) |
| | | .eq(ApproveProcess::getApproveReason, purchaseContractNumber) |
| | | .eq(ApproveProcess::getApproveDelete, 0) |
| | | .last("limit 1")); |
| | | if (one != null) { |
| | | approveProcessService.delByIds(Collections.singletonList(one.getId())); |
| | | } |
| | | } |
| | | |
| | | private void clearPurchaseQualityInspect(Long purchaseLedgerId) { |
| | | LambdaQueryWrapper<QualityInspect> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(QualityInspect::getPurchaseLedgerId, purchaseLedgerId) |
| | | .eq(QualityInspect::getInspectType, 0); |
| | | List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(queryWrapper); |
| | | if (CollectionUtils.isEmpty(qualityInspects)) { |
| | | return; |
| | | } |
| | | |
| | | boolean hasSubmittedInspect = qualityInspects.stream() |
| | | .anyMatch(item -> ObjectUtils.isNotEmpty(item.getInspectState()) && item.getInspectState().equals(1)); |
| | | if (hasSubmittedInspect) { |
| | | throw new BaseException("存在已提交的质检单,不允许编辑采购明细"); |
| | | } |
| | | |
| | | List<Long> inspectIds = qualityInspects.stream() |
| | | .map(QualityInspect::getId) |
| | | .collect(Collectors.toList()); |
| | | if (CollectionUtils.isNotEmpty(inspectIds)) { |
| | | qualityInspectParamMapper.delete(new LambdaQueryWrapper<QualityInspectParam>() |
| | | .in(QualityInspectParam::getInspectId, inspectIds)); |
| | | } |
| | | qualityInspectMapper.delete(queryWrapper); |
| | | } |
| | | |
| | | private void clearPurchaseStockInRecord(Long purchaseLedgerId) { |
| | | List<StockInRecord> stockInRecords = stockInRecordService.list(new LambdaQueryWrapper<StockInRecord>() |
| | | .eq(StockInRecord::getRecordId, purchaseLedgerId) |
| | | .eq(StockInRecord::getRecordType, StockInQualifiedRecordTypeEnum.PURCHASE_STOCK_IN.getCode()) |
| | | .eq(StockInRecord::getType, "0")); |
| | | if (CollectionUtils.isEmpty(stockInRecords)) { |
| | | return; |
| | | } |
| | | stockInRecordService.batchDelete(stockInRecords.stream() |
| | | .map(StockInRecord::getId) |
| | | .collect(Collectors.toList())); |
| | | } |
| | | |
| | | private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) { |
| | |
| | | if (purchaseLedger.getApprovalStatus().equals(3)) { |
| | | throw new BaseException(purchaseLedger.getPurchaseContractNumber()+"已经审批通过,不允许删除"); |
| | | } |
| | | clearPurchaseStockInRecord(id); |
| | | } |
| | | // 批量删除关联的采购入库记录 |
| | | LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductQueryWrapper = new LambdaQueryWrapper<>(); |