chenrui
8 天以前 d1903c17568e1c373ca37a8baddbefbc330d12bf
src/main/java/com/ruoyi/sales/service/impl/InvoiceLedgerServiceImpl.java
@@ -5,17 +5,16 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.SupplierManageDto;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.vo.FileVo;
import com.ruoyi.sales.dto.InvoiceLedgerDto;
import com.ruoyi.sales.dto.InvoiceRegistrationProductDto;
import com.ruoyi.sales.excel.InvoiceLedgerExcelDto;
import com.ruoyi.sales.mapper.InvoiceLedgerFileMapper;
import com.ruoyi.sales.mapper.InvoiceLedgerMapper;
import com.ruoyi.sales.mapper.ReceiptPaymentMapper;
import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper;
import com.ruoyi.sales.pojo.InvoiceLedger;
import com.ruoyi.sales.pojo.InvoiceLedgerFile;
import com.ruoyi.sales.pojo.ReceiptPayment;
import com.ruoyi.sales.service.InvoiceLedgerService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
@@ -29,8 +28,10 @@
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.math.BigDecimal;
import java.util.Collections;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
@@ -47,36 +48,48 @@
    private InvoiceLedgerFileMapper invoiceLedgerFileMapper;
    @Autowired
    private ReceiptPaymentMapper receiptPaymentMapper;
    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
    /**
     * 开票台账新增
     * @param invoiceLedgerDto
     * @param productDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int invoiceLedgerSaveOrUpdate(InvoiceLedgerDto invoiceLedgerDto) {
        InvoiceLedger invoiceLedger = new InvoiceLedger();
        BeanUtils.copyProperties(invoiceLedgerDto, invoiceLedger);
    public int invoiceLedgerSaveOrUpdate(InvoiceRegistrationProductDto productDto) {
        // 判断是否已经新增开票台账
        QueryWrapper<InvoiceLedger> ledgerQueryWrapper = new QueryWrapper<>();
        ledgerQueryWrapper.eq("invoice_registration_product_id", productDto.getId());
        InvoiceLedger invoiceLedger = invoiceLedgerMapper.selectOne(ledgerQueryWrapper);
        int result;
        if(invoiceLedgerDto.getId() == null){
        if(ObjectUtils.isEmpty(invoiceLedger)){
            invoiceLedger = new InvoiceLedger();
            invoiceLedger.setInvoiceRegistrationProductId(productDto.getId());
            invoiceLedger.setInvoiceNo(productDto.getInvoiceNo());
            invoiceLedger.setInvoiceTotal(productDto.getInvoiceTotal());
            invoiceLedger.setInvoiceDate(productDto.getInvoiceDate());
            invoiceLedger.setInvoicePerson(productDto.getInvoicePerson());
            result = invoiceLedgerMapper.insert(invoiceLedger);
        }else {
            invoiceLedger.setInvoiceNo(productDto.getInvoiceNo());
            invoiceLedger.setInvoiceTotal(productDto.getInvoiceTotal());
            invoiceLedger.setInvoiceDate(productDto.getInvoiceDate());
            invoiceLedger.setInvoicePerson(productDto.getInvoicePerson());
            result = invoiceLedgerMapper.updateById(invoiceLedger);
            //删除所有附件关联
            LambdaQueryWrapper<InvoiceLedgerFile> delWrapper = new LambdaQueryWrapper<>();
            delWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedgerDto.getId());
            delWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedger.getId());
            invoiceLedgerFileMapper.delete(delWrapper);
        }
        List<FileVo> fileList = invoiceLedgerDto.getFileList();
        List<FileVo> fileList = productDto.getFileList();
        if(CollectionUtils.isNotEmpty(fileList)){
            fileList.forEach(fileVo -> {
            for (FileVo fileVo : fileList) {
                InvoiceLedgerFile invoiceLedgerFile = new InvoiceLedgerFile();
                BeanUtils.copyProperties(fileVo, invoiceLedgerFile);
                invoiceLedgerFile.setInvoiceLedgerId(invoiceLedger.getId());
                invoiceLedgerFileMapper.insert(invoiceLedgerFile);
            });
            }
        }
        return result;
    }
@@ -197,11 +210,17 @@
        if(CollectionUtils.isEmpty(invoiceLedgerDto.getFileList())){
            throw new RuntimeException("缺少文件信息");
        }
        QueryWrapper<InvoiceLedger> ledgerQueryWrapper = new QueryWrapper<>();
        ledgerQueryWrapper.eq("invoice_registration_product_id", invoiceLedgerDto.getId());
        InvoiceLedger invoiceLedger = invoiceLedgerMapper.selectOne(ledgerQueryWrapper);
        if(ObjectUtils.isEmpty(invoiceLedger)){
            throw new RuntimeException("开票台账未登记");
        }
        List<FileVo> fileList = invoiceLedgerDto.getFileList();
        fileList.forEach(fileVo -> {
            InvoiceLedgerFile invoiceLedgerFile = new InvoiceLedgerFile();
            BeanUtils.copyProperties(fileVo, invoiceLedgerFile);
            invoiceLedgerFile.setInvoiceLedgerId(invoiceLedgerDto.getId());
            invoiceLedgerFile.setInvoiceLedgerId(invoiceLedger.getId());
            invoiceLedgerFileMapper.insert(invoiceLedgerFile);
        });
    }
@@ -225,21 +244,63 @@
    @Override
    public IPage<InvoiceLedgerDto> invoiceLedgerSalesAccount(Page page, InvoiceLedgerDto invoiceLedgerDto) {
        IPage<InvoiceLedgerDto> invoiceLedgerDtoIPage = invoiceLedgerMapper.invoiceLedgerSalesAccount(page, invoiceLedgerDto);
        for (InvoiceLedgerDto record : invoiceLedgerDtoIPage.getRecords()) {
            QueryWrapper<ReceiptPayment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("customer_id", record.getCustomerId());
            List<ReceiptPayment> receiptPaymentList = receiptPaymentMapper.selectList(queryWrapper);
            BigDecimal totalAmount = BigDecimal.ZERO;
            if(!CollectionUtils.isEmpty(receiptPaymentList)){
                for (ReceiptPayment receiptPayment : receiptPaymentList) {
                    totalAmount = totalAmount.add(receiptPayment.getInvoiceAmount());
                }
            }
            BigDecimal unReceiptPaymentAmount = record.getInvoiceAmount().subtract(totalAmount);
            record.setReceiptPaymentAmount(totalAmount);
            record.setUnReceiptPaymentAmount(unReceiptPaymentAmount);
        }
        return invoiceLedgerDtoIPage;
    }
    @Override
    public BigDecimal getInvoiceAmount() {
        LocalDate now = LocalDate.now();
        YearMonth currentMonth = YearMonth.from(now);
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<InvoiceLedger> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(InvoiceLedger::getInvoiceDate, currentMonth.atDay(1).atStartOfDay())  // 大于等于本月第一天
                .lt(InvoiceLedger::getInvoiceDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天
        // 执行查询并计算总和
        List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(queryWrapper);
        BigDecimal totalContractAmount = invoiceLedgers.stream()
                .map(InvoiceLedger::getInvoiceTotal)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        return totalContractAmount;
    }
    /**
     * 开票登记产品分页查询
     * @param page
     * @param registrationProductDto
     * @return
     */
    @Override
    public IPage<InvoiceRegistrationProductDto> registrationProductPage(Page page, InvoiceRegistrationProductDto registrationProductDto) {
        return invoiceRegistrationProductMapper.invoiceRegistrationProductPage(page,registrationProductDto);
    }
    /**
     * 产品开票台账详情
     * @param id
     * @return
     */
    @Override
    public InvoiceRegistrationProductDto invoiceLedgerProductDetail(Integer id) {
        InvoiceRegistrationProductDto invoiceRegistrationProductDto = invoiceLedgerMapper.invoiceLedgerProductInfo(id);
        if(ObjectUtils.isEmpty(invoiceRegistrationProductDto)){
            throw new RuntimeException("产品开票台账查找失败");
        }
        // 查询附件
        QueryWrapper<InvoiceLedgerFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("invoice_ledger_id", invoiceRegistrationProductDto.getInvoiceLedgerId());
        List<InvoiceLedgerFile> invoiceLedgerFileList = invoiceLedgerFileMapper.selectList(queryWrapper);
        List<FileVo> fileList = invoiceLedgerFileList.stream().map(item -> {
            FileVo fileVo = new FileVo();
            BeanUtils.copyProperties(item, fileVo);
            return fileVo;
        }).collect(Collectors.toList());
        invoiceRegistrationProductDto.setFileList(fileList);
        return invoiceRegistrationProductDto;
    }
}