| | |
| | | package com.ruoyi.purchase.service.impl; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.account.pojo.AccountExpense; |
| | | import com.ruoyi.account.pojo.AccountIncome; |
| | | import com.ruoyi.account.service.AccountExpenseService; |
| | | import com.ruoyi.account.service.AccountIncomeService; |
| | | import com.ruoyi.approve.bean.vo.ApproveProcessVO; |
| | | import com.ruoyi.approve.pojo.ApproveProcess; |
| | | import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl; |
| | | import com.ruoyi.basic.dto.ProductModelAnticlockwiseDto; |
| | | import com.ruoyi.basic.mapper.ProductMapper; |
| | | import com.ruoyi.basic.mapper.ProductModelMapper; |
| | | import com.ruoyi.basic.mapper.SupplierManageMapper; |
| | |
| | | import com.ruoyi.basic.pojo.SupplierManage; |
| | | import com.ruoyi.common.enums.FileNameType; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.framework.security.LoginUser; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.other.mapper.TempFileMapper; |
| | | import com.ruoyi.other.pojo.TempFile; |
| | | import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper; |
| | | import com.ruoyi.procurementrecord.pojo.ProcurementRecordStorage; |
| | | import com.ruoyi.project.system.domain.SysUser; |
| | | import com.ruoyi.project.system.mapper.SysUserMapper; |
| | | import com.ruoyi.purchase.dto.PurchaseLedgerDto; |
| | | import com.ruoyi.purchase.mapper.PaymentRegistrationMapper; |
| | | import com.ruoyi.purchase.mapper.ProductRecordMapper; |
| | | import com.ruoyi.purchase.mapper.PurchaseLedgerMapper; |
| | | import com.ruoyi.purchase.mapper.TicketRegistrationMapper; |
| | | import com.ruoyi.purchase.dto.PurchaseLedgerImportDto; |
| | | import com.ruoyi.purchase.dto.PurchaseLedgerProductImportDto; |
| | | import com.ruoyi.purchase.mapper.*; |
| | | import com.ruoyi.purchase.pojo.PaymentRegistration; |
| | | import com.ruoyi.purchase.pojo.ProductRecord; |
| | | import com.ruoyi.purchase.pojo.PurchaseLedger; |
| | | import com.ruoyi.purchase.pojo.TicketRegistration; |
| | | import com.ruoyi.purchase.service.IPurchaseLedgerService; |
| | | import com.ruoyi.quality.mapper.QualityInspectMapper; |
| | | import com.ruoyi.quality.mapper.QualityInspectParamMapper; |
| | | import com.ruoyi.quality.mapper.QualityTestStandardMapper; |
| | | import com.ruoyi.quality.mapper.*; |
| | | import com.ruoyi.quality.pojo.QualityInspect; |
| | | import com.ruoyi.quality.pojo.QualityInspectParam; |
| | | import com.ruoyi.quality.pojo.QualityTestStandard; |
| | | import com.ruoyi.sales.mapper.*; |
| | | import com.ruoyi.quality.pojo.QualityTestStandardParam; |
| | | import com.ruoyi.sales.mapper.CommonFileMapper; |
| | | import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper; |
| | | import com.ruoyi.sales.mapper.SalesLedgerMapper; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.pojo.CommonFile; |
| | | import com.ruoyi.sales.pojo.InvoiceRegistrationProduct; |
| | | import com.ruoyi.sales.pojo.SalesLedger; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import lombok.RequiredArgsConstructor; |
| | | import com.ruoyi.sales.service.impl.CommonFileServiceImpl; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | |
| | | * @date 2025-05-09 |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | @Slf4j |
| | | public class PurchaseLedgerServiceImpl extends ServiceImpl<PurchaseLedgerMapper, PurchaseLedger> implements IPurchaseLedgerService { |
| | | private final AccountExpenseService accountExpenseService; |
| | | private final PurchaseLedgerMapper purchaseLedgerMapper; |
| | | @Autowired |
| | | private AccountExpenseService accountExpenseService; |
| | | @Autowired |
| | | private PurchaseLedgerMapper purchaseLedgerMapper; |
| | | |
| | | private final SalesLedgerMapper salesLedgerMapper; |
| | | private final SalesLedgerProductMapper salesLedgerProductMapper; |
| | | @Autowired |
| | | private SalesLedgerMapper salesLedgerMapper; |
| | | @Autowired |
| | | private SalesLedgerProductMapper salesLedgerProductMapper; |
| | | |
| | | private final SysUserMapper userMapper; |
| | | @Autowired |
| | | private SysUserMapper userMapper; |
| | | |
| | | private final TempFileMapper tempFileMapper; |
| | | @Autowired |
| | | private TempFileMapper tempFileMapper; |
| | | |
| | | private final CommonFileMapper commonFileMapper; |
| | | @Autowired |
| | | private CommonFileMapper commonFileMapper; |
| | | |
| | | private final SupplierManageMapper supplierManageMapper; |
| | | @Autowired |
| | | private SupplierManageMapper supplierManageMapper; |
| | | |
| | | private final ProductMapper productMapper; |
| | | @Autowired |
| | | private ProductMapper productMapper; |
| | | |
| | | private final ProductModelMapper productModelMapper; |
| | | @Autowired |
| | | private ProductModelMapper productModelMapper; |
| | | |
| | | private final TicketRegistrationMapper ticketRegistrationMapper; |
| | | @Autowired |
| | | private SysUserMapper sysUserMapper; |
| | | |
| | | private final ProductRecordMapper productRecordMapper; |
| | | @Autowired |
| | | private TicketRegistrationMapper ticketRegistrationMapper; |
| | | |
| | | private final PaymentRegistrationMapper paymentRegistrationMapper; |
| | | @Autowired |
| | | private ProductRecordMapper productRecordMapper; |
| | | |
| | | private final InvoiceRegistrationProductMapper invoiceRegistrationProductMapper; |
| | | |
| | | private final StringRedisTemplate redisTemplate; |
| | | private final QualityInspectMapper qualityInspectMapper; |
| | | private final QualityTestStandardMapper qualityTestStandardMapper; |
| | | private final QualityInspectParamMapper qualityInspectParamMapper; |
| | | |
| | | @Autowired |
| | | private PaymentRegistrationMapper paymentRegistrationMapper; |
| | | @Autowired |
| | | private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper; |
| | | @Autowired |
| | | private StringRedisTemplate redisTemplate; |
| | | @Autowired |
| | | private QualityInspectMapper qualityInspectMapper; |
| | | @Autowired |
| | | private CommonFileServiceImpl commonFileService; |
| | | @Autowired |
| | | private QualityTestStandardBindingMapper qualityTestStandardBindingMapper; |
| | | @Autowired |
| | | private QualityTestStandardParamMapper qualityTestStandardParamMapper; |
| | | @Autowired |
| | | private QualityTestStandardMapper qualityTestStandardMapper; |
| | | @Autowired |
| | | private QualityInspectParamMapper qualityInspectParamMapper; |
| | | @Autowired |
| | | private ApproveProcessServiceImpl approveProcessService; |
| | | @Autowired |
| | | private ProcurementRecordMapper procurementRecordStorageMapper; |
| | | @Autowired |
| | | private PurchaseLedgerTemplateMapper purchaseLedgerTemplateMapper; |
| | | @Autowired |
| | | private SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper; |
| | | @Value("${file.upload-dir}") |
| | | private String uploadDir; |
| | | |
| | |
| | | } |
| | | return purchaseLedgerMapper.selectList(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int addPurchaseTemplate(PurchaseLedgerDto purchaseLedgerDto)throws IOException { |
| | | //录入人 |
| | | SysUser sysUser = userMapper.selectUserById(purchaseLedgerDto.getRecorderId()); |
| | | |
| | | SupplierManage supplierManage = supplierManageMapper.selectById(purchaseLedgerDto.getSupplierId()); |
| | | public int addOrEditPurchase(PurchaseLedgerDto purchaseLedgerDto) throws Exception { |
| | | PurchaseLedger purchaseLedger = new PurchaseLedger(); |
| | | // BeanUtils.copyProperties(purchaseLedger,purchaseLedgerDto); |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | if(ObjectUtils.isNotEmpty(loginUser) && null != loginUser.getTenantId()) { |
| | | purchaseLedger.setTenantId(loginUser.getTenantId()); |
| | | } |
| | | purchaseLedger.setPaymentMethod(purchaseLedgerDto.getPaymentMethod()); |
| | | purchaseLedger.setRecorderId(purchaseLedgerDto.getRecorderId()); |
| | | purchaseLedger.setSupplierId(purchaseLedgerDto.getSupplierId()); |
| | | purchaseLedger.setTemplateName(purchaseLedgerDto.getTemplateName()); |
| | | // purchaseLedger.setSalesLedgerPId(purchaseLedgerDto.getSalesLedgerId()); |
| | | purchaseLedger.setApprovalStatus(3); |
| | | purchaseLedger.setSupplierName(supplierManage.getSupplierName()); |
| | | purchaseLedger.setRecorderName(sysUser.getNickName()); |
| | | purchaseLedger.setPhoneNumber(sysUser.getPhonenumber()); |
| | | purchaseLedger.setPurchaseContractNumber(UUID.randomUUID().toString().replaceAll("-", "")); |
| | | purchaseLedger.setEntryDate(Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant())); |
| | | int insert = purchaseLedgerMapper.insert(purchaseLedger); |
| | | |
| | | LambdaQueryWrapper<PurchaseLedger> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(PurchaseLedger::getSupplierName, purchaseLedger.getSupplierName()) |
| | | .eq(PurchaseLedger::getPurchaseContractNumber, purchaseLedger.getPurchaseContractNumber()) |
| | | .eq(PurchaseLedger::getApprovalStatus,3); |
| | | PurchaseLedger purchaseLedger1 = purchaseLedgerMapper.selectOne(queryWrapper); |
| | | |
| | | if(ObjectUtils.isNotEmpty(purchaseLedgerDto.getProductData())) { |
| | | // 4. 处理子表数据 |
| | | List<SalesLedgerProduct> salesLedgerProductList = purchaseLedgerDto.getProductData(); |
| | | salesLedgerProductList.forEach(salesLedgerProduct -> { |
| | | salesLedgerProduct.setSalesLedgerId(purchaseLedger1.getId()); |
| | | salesLedgerProduct.setType(2); |
| | | }); |
| | | salesLedgerProductList.forEach(salesLedgerProductMapper::insert); |
| | | } |
| | | return insert; |
| | | } |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int addOrEditPurchase(PurchaseLedgerDto purchaseLedgerDto) throws IOException { |
| | | |
| | | SalesLedger salesLedger = salesLedgerMapper.selectById(purchaseLedgerDto.getSalesLedgerId()); |
| | | BeanUtils.copyProperties(purchaseLedgerDto, purchaseLedger); |
| | | //录入人 |
| | | SysUser sysUser = userMapper.selectUserById(purchaseLedgerDto.getRecorderId()); |
| | | if (ObjectUtils.isNotEmpty(sysUser)) { |
| | | purchaseLedger.setRecorderName(sysUser.getNickName()); |
| | | purchaseLedger.setPhoneNumber(sysUser.getPhonenumber()); |
| | | }else { |
| | | purchaseLedger.setRecorderName(SecurityUtils.getLoginUser().getNickName()); |
| | | SysUser sysUser1 = userMapper.selectUserById(SecurityUtils.getUserId()); |
| | | purchaseLedger.setPhoneNumber(sysUser1.getPhonenumber()); |
| | | } |
| | | |
| | | SupplierManage supplierManage = supplierManageMapper.selectById(purchaseLedgerDto.getSupplierId()); |
| | | |
| | | // DTO转Entity |
| | | PurchaseLedger purchaseLedger = new PurchaseLedger(); |
| | | BeanUtils.copyProperties(purchaseLedgerDto, purchaseLedger); |
| | | |
| | | |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | if (ObjectUtils.isNotEmpty(loginUser) && null != loginUser.getTenantId()) { |
| | | purchaseLedger.setTenantId(loginUser.getTenantId()); |
| | |
| | | purchaseLedger.setSalesLedgerId(ObjectUtils.isNotEmpty(salesLedger) ? salesLedger.getId() : -1); |
| | | purchaseLedger.setSupplierName(supplierManage.getSupplierName()); |
| | | purchaseLedger.setRecorderId(purchaseLedgerDto.getRecorderId()); |
| | | purchaseLedger.setRecorderName(sysUser.getNickName()); |
| | | purchaseLedger.setPhoneNumber(sysUser.getPhonenumber()); |
| | | // 2. 处理账户收入 |
| | | AccountExpense accountExpense = new AccountExpense(); |
| | | accountExpense.setExpenseDate(purchaseLedger.getEntryDate()); |
| | | accountExpense.setExpenseType("0"); |
| | | accountExpense.setSupplierName(purchaseLedger.getSupplierName()); |
| | | accountExpense.setExpenseMoney(purchaseLedger.getContractAmount()); |
| | | accountExpense.setExpenseDescribed("采购合同:" + purchaseLedger.getPurchaseContractNumber()); |
| | | accountExpense.setExpenseMethod("0"); |
| | | accountExpense.setInvoiceNumber(purchaseLedger.getPurchaseContractNumber()); |
| | | accountExpense.setInputTime(new Date()); |
| | | accountExpense.setInputUser(loginUser.getNickName()); |
| | | |
| | | |
| | | purchaseLedger.setApprovalStatus(1); |
| | | // 3. 新增或更新主表 |
| | | if (purchaseLedger.getId() == null) { |
| | | purchaseLedgerMapper.insert(purchaseLedger); |
| | | // accountIncomeService.save(accountIncome); |
| | | accountExpenseService.save(accountExpense); |
| | | } else { |
| | | purchaseLedgerMapper.updateById(purchaseLedger); |
| | | PurchaseLedger purchaseLedgerDB = purchaseLedgerMapper.selectById(purchaseLedger.getId()); |
| | | List<AccountExpense> accountExpenseDBs = accountExpenseService.getByInvoiceNumberList(purchaseLedger.getPurchaseContractNumber()); |
| | | if (!CollectionUtils.isEmpty(accountExpenseDBs)) { |
| | | accountExpenseDBs.forEach(accountExpenseDB ->{ |
| | | accountExpenseDB.setExpenseDate(purchaseLedgerDB.getEntryDate()); |
| | | accountExpenseDB.setExpenseType("0"); |
| | | accountExpenseDB.setSupplierName(purchaseLedgerDB.getSupplierName()); |
| | | accountExpenseDB.setExpenseMoney(purchaseLedgerDB.getContractAmount()); |
| | | accountExpenseDB.setExpenseDescribed("采购合同:" + purchaseLedgerDB.getPurchaseContractNumber()); |
| | | accountExpenseDB.setExpenseMethod("0"); |
| | | accountExpenseDB.setInvoiceNumber(purchaseLedgerDB.getPurchaseContractNumber()); |
| | | accountExpenseService.updateById(accountExpenseDB); |
| | | }); |
| | | // 删除采购审核,重新提交 |
| | | 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())); |
| | | } |
| | | |
| | | purchaseLedgerMapper.updateById(purchaseLedger); |
| | | } |
| | | // 6.采购审核新增 |
| | | addApproveByPurchase(loginUser, purchaseLedger); |
| | | |
| | | // 4. 处理子表数据 |
| | | List<SalesLedgerProduct> productList = purchaseLedgerDto.getProductData(); |
| | | if (productList != null && !productList.isEmpty()) { |
| | | handleSalesLedgerProducts(purchaseLedger.getId(), productList, purchaseLedgerDto.getType()); |
| | | } |
| | | //新增原材料检验 |
| | | for (SalesLedgerProduct saleProduct : productList) { |
| | | QualityInspect qualityInspect = new QualityInspect(); |
| | | qualityInspect.setInspectType(0); |
| | | qualityInspect.setSupplier(purchaseLedger.getSupplierName()); |
| | | qualityInspect.setPurchaseLedgerId(purchaseLedger.getId()); |
| | | qualityInspect.setProductId(saleProduct.getId()); |
| | | qualityInspect.setProductName(saleProduct.getProductCategory()); |
| | | qualityInspect.setModel(saleProduct.getSpecificationModel()); |
| | | qualityInspect.setUnit(saleProduct.getUnit()); |
| | | qualityInspect.setQuantity(saleProduct.getQuantity()); |
| | | qualityInspectMapper.insert(qualityInspect); |
| | | QualityTestStandard qualityTestStandard = new QualityTestStandard(); |
| | | qualityTestStandard.setProductId(saleProduct.getProductId()); |
| | | List<QualityTestStandard> qualityTestStandards = qualityTestStandardMapper.qualityTestStandardList(qualityTestStandard); |
| | | for (QualityTestStandard qualityTestStandardDB : qualityTestStandards) { |
| | | QualityInspectParam qualityInspectParam = new QualityInspectParam(); |
| | | qualityInspectParam.setInspectId(qualityInspect.getId()); |
| | | qualityInspectParam.setParameterItem(qualityTestStandardDB.getParameterItem()); |
| | | qualityInspectParam.setUnit(qualityTestStandardDB.getUnit()); |
| | | qualityInspectParam.setStandardValue(qualityTestStandardDB.getStandardValue()); |
| | | qualityInspectParam.setControlValue(qualityTestStandardDB.getControlValue()); |
| | | qualityInspectParamMapper.insert(qualityInspectParam); |
| | | } |
| | | } |
| | | |
| | | //新增原材料检验 审批之后才生成检验 |
| | | // if (productList != null) { |
| | | // for (SalesLedgerProduct saleProduct : productList) { |
| | | // //是否推送质检,如果true就添加 |
| | | // if (saleProduct.getIsChecked()) { |
| | | // addQualityInspect(purchaseLedger, saleProduct); |
| | | // } |
| | | // } |
| | | // } |
| | | // 5. 迁移临时文件到正式目录 |
| | | if (purchaseLedgerDto.getTempFileIds() != null && !purchaseLedgerDto.getTempFileIds().isEmpty()) { |
| | | migrateTempFilesToFormal(purchaseLedger.getId(), purchaseLedgerDto.getTempFileIds()); |
| | | } |
| | | |
| | | return 1; |
| | | } |
| | | |
| | | |
| | | public void addQualityInspect(PurchaseLedger purchaseLedger, SalesLedgerProduct saleProduct) { |
| | | QualityInspect qualityInspect = new QualityInspect(); |
| | | qualityInspect.setInspectType(0); |
| | | qualityInspect.setSupplier(purchaseLedger.getSupplierName()); |
| | | qualityInspect.setPurchaseLedgerId(purchaseLedger.getId()); |
| | | qualityInspect.setProductId(saleProduct.getProductId()); |
| | | qualityInspect.setProductName(saleProduct.getProductCategory()); |
| | | qualityInspect.setModel(saleProduct.getSpecificationModel()); |
| | | qualityInspect.setProductModelId(saleProduct.getProductModelId()); |
| | | qualityInspect.setUnit(saleProduct.getUnit()); |
| | | qualityInspect.setQuantity(saleProduct.getQuantity()); |
| | | qualityInspectMapper.insert(qualityInspect); |
| | | List<QualityTestStandard> qualityTestStandard = qualityTestStandardMapper.getQualityTestStandardByProductId(saleProduct.getProductId(), 0,null); |
| | | if (qualityTestStandard.size()>0){ |
| | | qualityInspect.setTestStandardId(qualityTestStandard.get(0).getId()); |
| | | qualityInspectMapper.updateById(qualityInspect); |
| | | qualityTestStandardParamMapper.selectList(Wrappers.<QualityTestStandardParam>lambdaQuery() |
| | | .eq(QualityTestStandardParam::getTestStandardId,qualityTestStandard.get(0).getId())) |
| | | .forEach(qualityTestStandardParam -> { |
| | | QualityInspectParam param = new QualityInspectParam(); |
| | | com.ruoyi.common.utils.bean.BeanUtils.copyProperties(qualityTestStandardParam, param); |
| | | param.setId(null); |
| | | param.setInspectId(qualityInspect.getId()); |
| | | qualityInspectParamMapper.insert(param); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) { |
| | |
| | | |
| | | LocalDateTime localDateTime = entryDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); |
| | | salesLedgerProduct.setRegisterDate(localDateTime); |
| | | salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity()); |
| | | salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int deletePurchaseLedgerByIds(Long[] ids) { |
| | | if (ids == null || ids.length == 0) { |
| | | throw new BaseException("请选中至少一条数据"); |
| | | } |
| | | for (Long id : ids) { |
| | | PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id); |
| | | if (purchaseLedger.getApprovalStatus().equals(3)) { |
| | | throw new BaseException(purchaseLedger.getPurchaseContractNumber()+"已经审批通过,不允许删除"); |
| | | } |
| | | } |
| | | // 批量删除关联的采购入库记录 |
| | | LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductQueryWrapper = new LambdaQueryWrapper<>(); |
| | | salesLedgerProductQueryWrapper.in(SalesLedgerProduct::getSalesLedgerId, ids) |
| | | .eq(SalesLedgerProduct::getType, 2); |
| | | List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(salesLedgerProductQueryWrapper); |
| | | if (CollectionUtils.isNotEmpty(salesLedgerProducts)) { |
| | | salesLedgerProducts.stream().forEach(salesLedgerProduct -> { |
| | | // 批量删除关联的采购台账产品 |
| | | LambdaQueryWrapper<ProcurementRecordStorage> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(ProcurementRecordStorage::getSalesLedgerProductId, salesLedgerProduct.getId()); |
| | | procurementRecordStorageMapper.delete(queryWrapper); |
| | | }); |
| | | } |
| | | // 批量删除关联的采购台账产品 |
| | | LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>(); |
| | |
| | | salesLedgerProductMapper.delete(queryWrapper); |
| | | // 批量删除关联的采购台账的来票登记 |
| | | LambdaQueryWrapper<TicketRegistration> ticketRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getSalesLedgerId,ids); |
| | | ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getPurchaseLedgerId,ids); |
| | | ticketRegistrationMapper.delete(ticketRegistrationLambdaQueryWrapper); |
| | | // 批量删除关联的采购台账的来票登记记录 |
| | | LambdaQueryWrapper<ProductRecord> productRecordLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | |
| | | //批量删除检验标准 |
| | | LambdaQueryWrapper<QualityInspect> materialInspectLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | materialInspectLambdaQueryWrapper.in(QualityInspect::getPurchaseLedgerId, ids); |
| | | |
| | | List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(materialInspectLambdaQueryWrapper); |
| | | qualityInspects.stream().forEach(qualityInspect -> { |
| | | if (ObjectUtils.isNotEmpty(qualityInspect.getInspectState())&&qualityInspect.getInspectState().equals(1)) { |
| | | throw new BaseException("已提交的检验单不能删除"); |
| | | } |
| | | }); |
| | | List<Long> inspectIds = qualityInspects.stream() |
| | | .map(QualityInspect::getId) |
| | | .collect(Collectors.toList()); |
| | |
| | | qualityStandardLambdaQueryWrapper.in(QualityInspectParam::getInspectId, inspectIds); |
| | | qualityInspectParamMapper.delete(qualityStandardLambdaQueryWrapper); |
| | | } |
| | | |
| | | // 删除采购审批记录 |
| | | for (Long id : ids) { |
| | | PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id); |
| | | if(purchaseLedger != null){ |
| | | ApproveProcess one = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>() |
| | | .eq(ApproveProcess::getApproveType, 5) |
| | | .eq(ApproveProcess::getApproveDelete, 0) |
| | | .eq(ApproveProcess::getApproveReason, purchaseLedger.getPurchaseContractNumber()) |
| | | .last("limit 1")); |
| | | if (one != null) { |
| | | approveProcessService.delByIds(Collections.singletonList(one.getId())); |
| | | } |
| | | } |
| | | } |
| | | //批量删除原材料检验数据 |
| | | qualityInspectMapper.delete(materialInspectLambdaQueryWrapper); |
| | | //删除附件 |
| | | commonFileService.deleteByBusinessIds(Arrays.asList(ids), 2); |
| | | // 批量删除采购台账 |
| | | return purchaseLedgerMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | } |
| | |
| | | return purchaseNo + String.format("%03d", sequence); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult importData(MultipartFile file) { |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | int successCount = 0; |
| | | int skipCount = 0; |
| | | List<String> errorMessages = new ArrayList<>(); |
| | | |
| | | try { |
| | | InputStream inputStream = file.getInputStream(); |
| | | ExcelUtil<PurchaseLedgerImportDto> excelUtil = new ExcelUtil<>(PurchaseLedgerImportDto.class); |
| | | Map<String, List<PurchaseLedgerImportDto>> stringListMap = excelUtil.importExcelMultiSheet(Arrays.asList("采购台账数据", "采购产品数据"), inputStream, 0); |
| | | if (CollectionUtils.isEmpty(stringListMap)) return AjaxResult.error("采购表格为空!"); |
| | | |
| | | List<PurchaseLedgerImportDto> ledgerList = stringListMap.get("采购台账数据"); |
| | | if (CollectionUtils.isEmpty(ledgerList)) return AjaxResult.error("采购台账数据为空!"); |
| | | List<PurchaseLedgerImportDto> productList = stringListMap.get("采购产品数据"); |
| | | if (CollectionUtils.isEmpty(productList)) return AjaxResult.error("采购产品数据为空!"); |
| | | |
| | | List<SupplierManage> suppliers = supplierManageMapper.selectList(new LambdaQueryWrapper<SupplierManage>().in(SupplierManage::getSupplierName, |
| | | ledgerList.stream().map(PurchaseLedgerImportDto::getSupplierName).filter(StrUtil::isNotBlank).collect(Collectors.toList()))); |
| | | List<SysUser> sysUsers = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>().in(SysUser::getNickName, |
| | | ledgerList.stream().map(PurchaseLedgerImportDto::getRecorderName).filter(StrUtil::isNotBlank).collect(Collectors.toList()))); |
| | | |
| | | for (int i = 0; i < ledgerList.size(); i++) { |
| | | PurchaseLedgerImportDto importDto = ledgerList.get(i); |
| | | int rowNum = i + 2; |
| | | |
| | | if (StrUtil.isBlank(importDto.getPurchaseContractNumber())) { |
| | | errorMessages.add(String.format("第%d行:采购单号为空", rowNum)); |
| | | skipCount++; |
| | | continue; |
| | | } |
| | | |
| | | try { |
| | | PurchaseLedger existLedger = purchaseLedgerMapper.selectOne(new LambdaQueryWrapper<PurchaseLedger>() |
| | | .eq(PurchaseLedger::getPurchaseContractNumber, importDto.getPurchaseContractNumber()) |
| | | .last("limit 1")); |
| | | if (existLedger != null) { |
| | | errorMessages.add(String.format("第%d行:采购单号[%s]已存在", rowNum, importDto.getPurchaseContractNumber())); |
| | | skipCount++; |
| | | continue; |
| | | } |
| | | |
| | | PurchaseLedger purchaseLedger = new PurchaseLedger(); |
| | | BeanUtils.copyProperties(importDto, purchaseLedger); |
| | | |
| | | purchaseLedger.setSupplierId(suppliers.stream() |
| | | .filter(s -> s.getSupplierName().equals(importDto.getSupplierName())) |
| | | .findFirst() |
| | | .map(SupplierManage::getId) |
| | | .orElse(null)); |
| | | |
| | | Long recorderId = sysUsers.stream() |
| | | .filter(u -> u.getNickName().equals(importDto.getRecorderName())) |
| | | .findFirst() |
| | | .map(SysUser::getUserId) |
| | | .orElse(null); |
| | | if (recorderId == null) { |
| | | throw new RuntimeException("录入人:" + importDto.getRecorderName() + "不存在"); |
| | | } |
| | | purchaseLedger.setRecorderId(recorderId); |
| | | |
| | | List<PurchaseLedgerProductImportDto> productDtos = productList.stream() |
| | | .filter(p -> p.getPurchaseContractNumber().equals(importDto.getPurchaseContractNumber())) |
| | | .collect(Collectors.toList()); |
| | | if (CollectionUtils.isEmpty(productDtos)) { |
| | | throw new RuntimeException("无对应产品数据"); |
| | | } |
| | | |
| | | purchaseLedger.setContractAmount(productDtos.stream() |
| | | .map(PurchaseLedgerProductImportDto::getTaxInclusiveTotalPrice) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | |
| | | SalesLedger salesLedger = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>() |
| | | .eq(SalesLedger::getSalesContractNo, purchaseLedger.getSalesContractNo()) |
| | | .last("LIMIT 1")); |
| | | if (salesLedger != null) { |
| | | purchaseLedger.setSalesLedgerId(salesLedger.getId()); |
| | | } |
| | | |
| | | purchaseLedgerMapper.insert(purchaseLedger); |
| | | |
| | | for (PurchaseLedgerProductImportDto productDto : productDtos) { |
| | | SalesLedgerProduct product = new SalesLedgerProduct(); |
| | | BeanUtils.copyProperties(productDto, product); |
| | | product.setSalesLedgerId(purchaseLedger.getId()); |
| | | product.setType(2); |
| | | product.setTaxExclusiveTotalPrice(product.getTaxInclusiveTotalPrice() |
| | | .divide(new BigDecimal(1).add(product.getTaxRate().divide(new BigDecimal(100))), 2, RoundingMode.HALF_UP)); |
| | | product.setFutureTickets(product.getQuantity()); |
| | | product.setFutureTicketsAmount(product.getTaxExclusiveTotalPrice()); |
| | | |
| | | ProductModelAnticlockwiseDto modelDto = new ProductModelAnticlockwiseDto(); |
| | | modelDto.setProductName(product.getProductCategory()); |
| | | modelDto.setModel(product.getSpecificationModel()); |
| | | modelDto.setUnit(product.getUnit()); |
| | | modelDto.setSubUnit(product.getUnit()); |
| | | product.setProductModelId(productModelAnticlockwise(modelDto)); |
| | | |
| | | product.setRegister(loginUser.getNickName()); |
| | | product.setRegisterDate(LocalDateTime.now()); |
| | | product.setApproveStatus(0); |
| | | product.setPendingTicketsTotal(productDto.getTaxInclusiveTotalPrice()); |
| | | product.setIsChecked(productDto.getIsChecked() == 1); |
| | | |
| | | if (productDto.getIsChecked() == 1) { |
| | | addQualityInspect(purchaseLedger, product); |
| | | } |
| | | salesLedgerProductMapper.insert(product); |
| | | } |
| | | |
| | | addApproveByPurchase(loginUser, purchaseLedger); |
| | | successCount++; |
| | | |
| | | } catch (Exception e) { |
| | | log.error("第{}行采购单[{}]导入失败:{}", rowNum, importDto.getPurchaseContractNumber(), e.getMessage(), e); |
| | | errorMessages.add(String.format("第%d行采购单[%s]:失败-%s", rowNum, importDto.getPurchaseContractNumber(), e.getMessage())); |
| | | skipCount++; |
| | | } |
| | | } |
| | | |
| | | String message = String.format("导入完成!成功%d条,跳过%d条", successCount, skipCount); |
| | | if (!errorMessages.isEmpty()) { |
| | | message += "。错误详情:" + String.join(";", errorMessages); |
| | | } |
| | | return AjaxResult.success(message); |
| | | } catch (Exception e) { |
| | | log.error("采购导入异常", e); |
| | | return AjaxResult.error("导入失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | public Long productModelAnticlockwise(ProductModelAnticlockwiseDto productModelDto) { |
| | | ProductModel oldProductModel = productModelMapper.selectOldProductModel(productModelDto.getModel(), productModelDto.getProductName()); |
| | | //存在就更新 |
| | | if (oldProductModel != null) { |
| | | oldProductModel.setModel(productModelDto.getModel()); |
| | | oldProductModel.setUnit(productModelDto.getUnit()); |
| | | oldProductModel.setSubUnit(productModelDto.getSubUnit()); |
| | | oldProductModel.setDeptId(SecurityUtils.getDeptId()[0]); |
| | | productModelMapper.updateById(oldProductModel); |
| | | Product product = productMapper.selectById(oldProductModel.getProductId()); |
| | | product.setProductName(productModelDto.getProductName()); |
| | | productMapper.updateById(product); |
| | | return oldProductModel.getId(); |
| | | }else { |
| | | //找到父节点 |
| | | Product productParent = productMapper.selectOne(new QueryWrapper<Product>().lambda().eq(Product::getProductName, "原料").last("limit 1")); |
| | | if (ObjectUtils.isEmpty(productParent)) { |
| | | Product product = new Product(); |
| | | product.setProductName("原料"); |
| | | product.setDeptId(SecurityUtils.getDeptId()[0]); |
| | | productMapper.insert(product); |
| | | productParent.setId(product.getId()); |
| | | } |
| | | //新增产品大类 |
| | | Product product = new Product(); |
| | | product.setProductName(productModelDto.getProductName()); |
| | | product.setParentId(productParent.getId()); |
| | | product.setDeptId(SecurityUtils.getDeptId()[0]); |
| | | productMapper.insert( product); |
| | | //新增产品规格 |
| | | ProductModel productModel = new ProductModel(); |
| | | productModel.setProductId(product.getId()); |
| | | productModel.setModel(productModelDto.getModel()); |
| | | productModel.setUnit(productModelDto.getUnit()); |
| | | productModel.setSubUnit(productModelDto.getSubUnit()); |
| | | productModel.setDeptId(SecurityUtils.getDeptId()[0]); |
| | | productModelMapper.insert(productModel); |
| | | return productModel.getId(); |
| | | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public PurchaseLedgerDto getPurchaseByCode(PurchaseLedgerDto purchaseLedgerDto) { |
| | | // 1. 查询主表 |
| | | PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectOne(new LambdaQueryWrapper<PurchaseLedger>() |
| | | .eq(PurchaseLedger::getPurchaseContractNumber, purchaseLedgerDto.getPurchaseContractNumber()) |
| | | .last("LIMIT 1")); |
| | | if (purchaseLedger == null) { |
| | | throw new BaseException("采购台账不存在"); |
| | | } |
| | | |
| | | // 2. 查询子表 |
| | | LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>(); |
| | | productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId()) |
| | | .eq(SalesLedgerProduct::getType, 2); |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper); |
| | | |
| | | // 4. 转换 DTO |
| | | PurchaseLedgerDto resultDto = new PurchaseLedgerDto(); |
| | | BeanUtils.copyProperties(purchaseLedger, resultDto); |
| | | if (!products.isEmpty()) { |
| | | resultDto.setHasChildren(true); |
| | | resultDto.setProductData(products); |
| | | } |
| | | return resultDto; |
| | | } |
| | | |
| | | public void addApproveByPurchase(LoginUser loginUser,PurchaseLedger purchaseLedger) throws Exception { |
| | | ApproveProcessVO approveProcessVO = new ApproveProcessVO(); |
| | | approveProcessVO.setApproveType(5); |
| | | approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId()); |
| | | approveProcessVO.setApproveReason(purchaseLedger.getPurchaseContractNumber()); |
| | | approveProcessVO.setApproveUserIds(purchaseLedger.getApproveUserIds()); |
| | | approveProcessVO.setApproveUser(loginUser.getUserId()); |
| | | approveProcessVO.setApproveTime(LocalDate.now().toString()); |
| | | approveProcessService.addApprove(approveProcessVO); |
| | | } |
| | | |
| | | /** |
| | | * 下划线命名转驼峰命名 |
| | | */ |