chenrui
4 天以前 05de62989998157eab6ae4809ec3a45db6783e10
src/main/java/com/ruoyi/sales/service/impl/InvoiceLedgerServiceImpl.java
@@ -5,17 +5,13 @@
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.pojo.InvoiceLedger;
import com.ruoyi.sales.pojo.InvoiceLedgerFile;
import com.ruoyi.sales.pojo.ReceiptPayment;
import com.ruoyi.sales.mapper.*;
import com.ruoyi.sales.pojo.*;
import com.ruoyi.sales.service.InvoiceLedgerService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
@@ -29,8 +25,11 @@
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.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
@@ -47,36 +46,75 @@
    private InvoiceLedgerFileMapper invoiceLedgerFileMapper;
    @Autowired
    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
    @Autowired
    private ReceiptPaymentMapper receiptPaymentMapper;
    @Autowired
    private SalesLedgerProductMapper salesLedgerProductMapper;
    /**
     * 开票台账新增
     * @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 {
            // 修改sale_leger_product
            InvoiceRegistrationProduct invoiceRegistrationProduct = invoiceRegistrationProductMapper.selectById(productDto.getId());
            SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(invoiceRegistrationProduct.getSalesLedgerProductId());
            BigDecimal invoiceNum = productDto.getInvoiceTotal().divide(invoiceRegistrationProduct.getTaxInclusiveTotalPrice(), 2, BigDecimal.ROUND_HALF_UP);
            //计算 未开票数/未开票金额
            BigDecimal noInvoiceAmount = salesLedgerProduct.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount()).subtract(productDto.getInvoiceTotal());
            BigDecimal noInvoiceNum = salesLedgerProduct.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum()).subtract(invoiceNum);
            salesLedgerProduct.setNoInvoiceAmount(noInvoiceAmount);
            salesLedgerProduct.setNoInvoiceNum(noInvoiceNum);
            salesLedgerProductMapper.updateById(salesLedgerProduct);
            // 修改当前产品开票登记记录数据
            BigDecimal currentNoInvoiceNum = invoiceRegistrationProduct.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum()).subtract(invoiceNum);
            BigDecimal currentNoInvoiceAmount = invoiceRegistrationProduct.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount()).subtract(productDto.getInvoiceTotal());
            invoiceRegistrationProduct.setInvoiceNum(invoiceNum);
            invoiceRegistrationProduct.setInvoiceAmount(productDto.getInvoiceTotal());
            invoiceRegistrationProduct.setNoInvoiceNum(currentNoInvoiceNum);
            invoiceRegistrationProduct.setNoInvoiceAmount(currentNoInvoiceAmount);
            invoiceRegistrationProductMapper.updateById(invoiceRegistrationProduct);
            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;
    }
@@ -145,13 +183,13 @@
    /**
     * 附件下载
     * @param response
     * @param invoiceLedgerDto
     * @param invoiceRegistrationProductDto
     * @return
     */
    @Override
    public void invoiceLedgerExport(HttpServletResponse response, InvoiceLedgerDto invoiceLedgerDto) {
        List<InvoiceLedgerDto> invoiceLedgerDtoList = invoiceLedgerMapper.invoiceLedgerList(invoiceLedgerDto);
        List<InvoiceLedgerExcelDto> invoiceLedgerExcelDtoList = invoiceLedgerDtoList.stream().map(item -> {
    public void invoiceLedgerExport(HttpServletResponse response, InvoiceRegistrationProductDto invoiceRegistrationProductDto) {
        List<InvoiceRegistrationProductDto> invoiceRegistrationProductList = invoiceRegistrationProductMapper.invoiceRegistrationProductList(invoiceRegistrationProductDto);
        List<InvoiceLedgerExcelDto> invoiceLedgerExcelDtoList = invoiceRegistrationProductList.stream().map(item -> {
            InvoiceLedgerExcelDto invoiceLedgerExcelDto = new InvoiceLedgerExcelDto();
            BeanUtils.copyProperties(item, invoiceLedgerExcelDto);
            return invoiceLedgerExcelDto;
@@ -197,11 +235,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 +269,203 @@
    @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;
    }
    /**
     * 开票台账删除
     * @param invoiceRegistrationProductId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delInvoiceLedger(Integer invoiceRegistrationProductId) {
        // 判断是否有回款记录
        List<ReceiptPayment> receiptPaymentList = receiptPaymentMapper.receiptPaymentListByProdRegId(invoiceRegistrationProductId);
        if(!CollectionUtils.isEmpty(receiptPaymentList)){
            throw new RuntimeException("请先删除回款记录");
        }
        // 判断是否有开票登记记录
        InvoiceRegistrationProduct invoiceRegistrationProduct = invoiceRegistrationProductMapper.selectById(invoiceRegistrationProductId);
        if(ObjectUtils.isEmpty(invoiceRegistrationProduct)){
            throw new RuntimeException("开票登记记录查找失败");
        }
        // 判断该开票台账是否未当月,如果为当月直接删除否则新增一条相反负数数据
        QueryWrapper<InvoiceLedger> ledgerQueryWrapper = new QueryWrapper<>();
        ledgerQueryWrapper.eq("invoice_registration_product_id", invoiceRegistrationProductId);
        InvoiceLedger invoiceLedger = invoiceLedgerMapper.selectOne(ledgerQueryWrapper);
        if(ObjectUtils.isEmpty(invoiceLedger)){
            throw new RuntimeException("开票台账查找失败");
        }
        if( invoiceLedger.getInvoiceDate() == null){
            dealCurrentMonthDel(invoiceRegistrationProductId,invoiceRegistrationProduct);
            return;
        }
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 使用YearMonth比较年份和月份是否相同
        boolean equalsFlag = YearMonth.from(invoiceLedger.getInvoiceDate()).equals(YearMonth.from(today));
        if(equalsFlag){
            dealCurrentMonthDel(invoiceRegistrationProductId,invoiceRegistrationProduct);
        }else {
            dealOtherMonthDel(invoiceRegistrationProduct,invoiceLedger);
        }
    }
    /**
     * 处理当月开票台账删除
     * @param invoiceRegistrationProductId
     * @param invoiceRegistrationProduct
     */
    private void dealCurrentMonthDel(Integer invoiceRegistrationProductId,InvoiceRegistrationProduct invoiceRegistrationProduct){
        // 删除开票台账
        QueryWrapper<InvoiceLedger> delMapper = new QueryWrapper<>();
        delMapper.eq("invoice_registration_product_id", invoiceRegistrationProductId);
        invoiceLedgerMapper.delete(delMapper);
        // 删除开票登记并回滚
        QueryWrapper<InvoiceRegistrationProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sales_ledger_id", invoiceRegistrationProduct.getSalesLedgerId());
        queryWrapper.eq("sales_ledger_product_id", invoiceRegistrationProduct.getSalesLedgerProductId());
        queryWrapper.orderByAsc("create_time");
        List<InvoiceRegistrationProduct> invoiceRegistrationProductList = invoiceRegistrationProductMapper.selectList(queryWrapper);
        int index =  -1;
        for (int i = 0; i < invoiceRegistrationProductList.size(); i++) {
            InvoiceRegistrationProduct currentInvoiceRegProduct = invoiceRegistrationProductList.get(i);
            if(invoiceRegistrationProduct.getId() == currentInvoiceRegProduct.getId()){
                index = i;
            }
        }
        if(index == -1){
            return;
        }
        for (int i = index + 1; i < invoiceRegistrationProductList.size(); i++) {
            InvoiceRegistrationProduct currentInvoiceRegProduct = invoiceRegistrationProductList.get(i);
            // 回滚未开票数/未开票金额
            BigDecimal noInvoiceAmount = currentInvoiceRegProduct.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount());
            BigDecimal noInvoiceNum = currentInvoiceRegProduct.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum());
            currentInvoiceRegProduct.setNoInvoiceAmount(noInvoiceAmount);
            currentInvoiceRegProduct.setNoInvoiceNum(noInvoiceNum);
            invoiceRegistrationProductMapper.updateById(currentInvoiceRegProduct);
        }
        invoiceRegistrationProductMapper.deleteById(invoiceRegistrationProductId);
        // 修改sale_product数据
        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(invoiceRegistrationProduct.getSalesLedgerProductId());
        QueryWrapper<InvoiceRegistrationProduct> newQueryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sales_ledger_id", invoiceRegistrationProduct.getSalesLedgerId());
        queryWrapper.eq("sales_ledger_product_id", invoiceRegistrationProduct.getSalesLedgerProductId());
        queryWrapper.orderByAsc("create_time");
        List<InvoiceRegistrationProduct> newInvoiceRegistrationProductList = invoiceRegistrationProductMapper.selectList(newQueryWrapper);
        if(CollectionUtils.isEmpty(newInvoiceRegistrationProductList)){
            salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
            salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
        }else {
            salesLedgerProduct.setNoInvoiceNum(newInvoiceRegistrationProductList.get(newInvoiceRegistrationProductList.size()-1).getNoInvoiceNum());
            salesLedgerProduct.setNoInvoiceAmount(newInvoiceRegistrationProductList.get(newInvoiceRegistrationProductList.size()-1).getNoInvoiceAmount());
        }
        salesLedgerProductMapper.updateById(salesLedgerProduct);
    }
    /**
     * 处理其他月份开票台账删除
     * @param invoiceRegistrationProduct
     * @param invoiceLedger
     */
    private void dealOtherMonthDel(InvoiceRegistrationProduct invoiceRegistrationProduct,InvoiceLedger invoiceLedger ){
        InvoiceRegistrationProduct copyRegProduct = new InvoiceRegistrationProduct();
        BeanUtils.copyProperties(invoiceRegistrationProduct, copyRegProduct);
        BigDecimal invoiceNum = copyRegProduct.getInvoiceNum().negate();
        BigDecimal invoiceAmount = copyRegProduct.getInvoiceAmount().negate();
        // invoice_registration_product
        // 删除开票登记并回滚
        QueryWrapper<InvoiceRegistrationProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sales_ledger_id", invoiceRegistrationProduct.getSalesLedgerId());
        queryWrapper.eq("sales_ledger_product_id", invoiceRegistrationProduct.getSalesLedgerProductId());
        queryWrapper.orderByAsc("create_time");
        List<InvoiceRegistrationProduct> invoiceRegistrationProductList = invoiceRegistrationProductMapper.selectList(queryWrapper);
        InvoiceRegistrationProduct lastInvoiceRegistrationProduct = invoiceRegistrationProductList.get(invoiceRegistrationProductList.size() - 1);
        BigDecimal noInvoiceNum = lastInvoiceRegistrationProduct.getNoInvoiceNum().subtract(invoiceNum);
        BigDecimal noInvoiceAmount = lastInvoiceRegistrationProduct.getNoInvoiceAmount().subtract(invoiceAmount);
        copyRegProduct.setInvoiceNum(invoiceNum);
        copyRegProduct.setInvoiceAmount(invoiceAmount);
        copyRegProduct.setNoInvoiceNum(noInvoiceNum);
        copyRegProduct.setNoInvoiceAmount(noInvoiceAmount);
        copyRegProduct.setId(null);
        invoiceRegistrationProductMapper.insert(copyRegProduct);
        // 复制开票台账
        InvoiceLedger invoiceLedgerCopy = new InvoiceLedger();
        BeanUtils.copyProperties(invoiceLedger, invoiceLedgerCopy);
        invoiceLedgerCopy.setInvoiceRegistrationProductId(copyRegProduct.getId());
        BigDecimal invoiceTotal = invoiceLedgerCopy.getInvoiceTotal().negate();
        invoiceLedgerCopy.setInvoiceTotal(invoiceTotal);
        invoiceLedgerCopy.setId(null);
        invoiceLedgerCopy.setInvoiceDate(LocalDate.now());
        invoiceLedgerMapper.insert(invoiceLedgerCopy);
        // 更新sales_leger_product
        QueryWrapper<SalesLedgerProduct> salesLedgerProductQueryWrapper = new QueryWrapper<>();
        salesLedgerProductQueryWrapper.eq("id", invoiceRegistrationProduct.getSalesLedgerProductId());
        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectOne(salesLedgerProductQueryWrapper);
        salesLedgerProduct.setNoInvoiceNum(noInvoiceNum);
        salesLedgerProduct.setNoInvoiceAmount(noInvoiceAmount);
        BigDecimal newInvoiceNum = salesLedgerProduct.getInvoiceNum().add(invoiceNum);
        BigDecimal newInvocieAmount = salesLedgerProduct.getInvoiceAmount().add(invoiceAmount);
        salesLedgerProduct.setInvoiceNum(newInvoiceNum);
        salesLedgerProduct.setInvoiceAmount(newInvocieAmount);
        salesLedgerProductMapper.updateById(salesLedgerProduct);
    }
}