maven
14 小时以前 63328dd80adc6331db581dda6418308e6fd4da12
src/main/java/com/ruoyi/purchase/service/impl/PaymentRegistrationServiceImpl.java
@@ -11,6 +11,7 @@
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.purchase.dto.PaymentHistoryRecordVo;
import com.ruoyi.purchase.dto.PaymentLedgerDto;
import com.ruoyi.purchase.dto.PaymentRegistrationDto;
import com.ruoyi.purchase.mapper.*;
@@ -21,10 +22,12 @@
import com.ruoyi.purchase.service.IPaymentRegistrationService;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.ReceiptPayment;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
@@ -46,8 +49,6 @@
    private PaymentRegistrationMapper paymentRegistrationMapper;
    private PurchaseLedgerMapper purchaseLedgerMapper;
    private InvoicePurchaseMapper invoicePurchaseMapper;
    private SalesLedgerMapper salesLedgerMapper;
@@ -83,7 +84,8 @@
            List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                    .eq("ticket_registration_id", registrationDto.getTicketRegistrationId()));
            BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            registrationDto.setUnPaymentAmount(registrationDto.getInvoiceAmount().subtract(total));
            BigDecimal invoiceAmount = (null != registrationDto.getInvoiceAmount())? registrationDto.getInvoiceAmount() : BigDecimal.ZERO;
            registrationDto.setUnPaymentAmount(invoiceAmount.subtract(total));
        }
        return list;
    }
@@ -91,42 +93,33 @@
    /**
     * 新增付款登记
     *
     * @param paymentRegistration 付款登记
     * @param paymentRegistrationList 付款登记
     * @return 结果
     */
    @Override
    public int insertPaymentRegistration(PaymentRegistration paymentRegistration) {
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(paymentRegistration.getPurchaseLedgerId());
        SalesLedger salesLedger = salesLedgerMapper.selectOne(new QueryWrapper<SalesLedger>().
                eq("sales_contract_no", purchaseLedger.getSalesContractNo()));
        if (salesLedger == null) {
            throw new RuntimeException("关联销售合同号不存在");
    public int insertPaymentRegistration(List<PaymentRegistration> paymentRegistrationList) {
        for (PaymentRegistration paymentRegistration : paymentRegistrationList) {
            PaymentRegistration byId = paymentRegistration;
            if (!ObjectUtils.isEmpty(paymentRegistration.getId())){
                paymentRegistration = this.getById(paymentRegistration.getId());
            }
            PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(paymentRegistration.getPurchaseLedgerId());
            if(null==purchaseLedger) throw new RuntimeException("未找到采购单");
            // 已回款金额
            SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(paymentRegistration.getSaleLedgerProductId());
            if(null==salesLedgerProduct) throw new RuntimeException("未找到采购单产品");
            if (salesLedgerProduct.getPendingInvoiceTotal().compareTo(paymentRegistration.getCurrentPaymentAmount())<0){
                throw new RuntimeException("本次回款金额不能大于待回款金额");
            }
            paymentRegistration.setCreateTime(DateUtils.getNowDate());
            paymentRegistration.setUpdateTime(DateUtils.getNowDate());
            salesLedgerProduct.setInvoiceTotal(salesLedgerProduct.getInvoiceTotal().add(paymentRegistration.getCurrentPaymentAmount()));
            salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProduct.getTaxInclusiveTotalPrice().subtract(salesLedgerProduct.getInvoiceTotal()));
            paymentRegistrationMapper.insert(paymentRegistration);
            salesLedgerProductMapper.updateById(salesLedgerProduct);
        }
        return 1;
        paymentRegistration.setSaleLedgerId(salesLedger.getId());
        paymentRegistration.setSupplierId(purchaseLedger.getSupplierId());
        TicketRegistration tr = ticketRegistrationMapper.selectOne(new LambdaQueryWrapper<TicketRegistration>().eq(TicketRegistration::getId, paymentRegistration.getTicketRegistrationId()));
        if (tr == null) {
            throw new RuntimeException("关联发票不存在");
        }
        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                .eq("ticket_registration_id", tr.getId()));
        BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        if (total.add(paymentRegistration.getCurrentPaymentAmount()).compareTo(tr.getInvoiceAmount()) > 0) {
            throw new RuntimeException("付款金额超出发票金额");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Integer tenantId = loginUser.getTenantId();
        paymentRegistration.setTenantId(tenantId.longValue());
        paymentRegistration.setRegistrantId(loginUser.getUserId());
        paymentRegistration.setCreateTime(DateUtils.getNowDate());
        paymentRegistration.setUpdateTime(DateUtils.getNowDate());
        return paymentRegistrationMapper.insert(paymentRegistration);
    }
    /**
@@ -137,7 +130,9 @@
     */
    @Override
    public int updatePaymentRegistration(PaymentRegistration paymentRegistration) {
        TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(paymentRegistration.getTicketRegistrationId());
        PaymentRegistration paymentRegistration1 = paymentRegistrationMapper.selectById(paymentRegistration.getId());
        TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(paymentRegistration.getTicketRegistrationId()==null?paymentRegistration1.getTicketRegistrationId():paymentRegistration.getTicketRegistrationId());
        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                .eq("ticket_registration_id", paymentRegistration.getTicketRegistrationId()).ne("id", paymentRegistration.getId()));
@@ -202,6 +197,7 @@
        for (SupplierManage supplierManage : supplierManages) {
            Map<String, Object> res = new HashMap<>();
            res.put("supplierName", supplierManage.getSupplierName());
            res.put("supplierId", supplierManage.getId());
            // 应付金额计算
            BigDecimal payableAmount = BigDecimal.ZERO;
@@ -213,7 +209,7 @@
                    .map(PurchaseLedger::getId)
                    .filter(Objects::nonNull)
                    .flatMap(id -> salesLedgerProductMapper.selectList(
                            new QueryWrapper<SalesLedgerProduct>().eq("sales_ledger_id", id)
                            new QueryWrapper<SalesLedgerProduct>().eq("sales_ledger_id", id).eq("type",2)
                    ).stream())
                    .collect(Collectors.toList());
            payableAmount = salesLedgerProducts.stream()
@@ -267,7 +263,6 @@
                                            new LambdaQueryWrapper<ProductRecord>().eq(ProductRecord::getTicketRegistrationId, ticketRegistration.getId())
                                    ).stream()
                                    .map(ProductRecord::getTicketsNum)
                                    .map(BigDecimal::new)
                                    .filter(Objects::nonNull)
                                    .reduce(BigDecimal.ZERO, BigDecimal::add);
                            detail.put("voteCount", voteCount);
@@ -288,6 +283,8 @@
            detailPagination.put("pageSize", detailPageSize);
            detailPagination.put("pages", (int) Math.ceil((double) totalDetails / detailPageSize));
            // 应付金额逻辑不采用合同金额改成  发票金额减付款金额
            payableAmount = invoiceAmount.subtract(paymentAmount);
            res.put("invoiceAmount", invoiceAmount);
            res.put("payableAmount", payableAmount);
            res.put("paymentAmount", paymentAmount);
@@ -344,6 +341,121 @@
        return result;
    }
    /**
     * 查询付款登记列表
     *
     * @param paymentRegistrationDto 付款登记
     * @return 付款登记集合
     */
    @Override
    public List<PaymentRegistrationDto> paymentHistoryList(PaymentRegistrationDto paymentRegistrationDto) {
        return paymentRegistrationMapper.paymentHistoryList(paymentRegistrationDto);
    }
    /**
     * 查询供应商往来记录
     * @param supplierId
     * @return
     */
    @Override
    public List<PaymentHistoryRecordVo> getPaymentRecordList(Long supplierId) {
        List<PaymentHistoryRecordVo> paymentRecordList = paymentRegistrationMapper.getPaymentRecordList(supplierId);
        List<PaymentHistoryRecordVo> result = new ArrayList<>();
        List<PaymentHistoryRecordVo> newResult = new ArrayList<>();
        // 应付总金额金额计算
        BigDecimal amountTotal = BigDecimal.ZERO;
        if(CollectionUtils.isNotEmpty(paymentRecordList)) {
            Map<LocalDate, List<PaymentHistoryRecordVo>> dateListMap = paymentRecordList.stream().collect(
                                                                        Collectors.groupingBy(
                                                                                PaymentHistoryRecordVo::getHappenTime,
                                                                                LinkedHashMap::new,
                                                                                Collectors.toList()
                                                                        )
                                                                        );
            for (LocalDate localDate : dateListMap.keySet()) {
                BigDecimal currentPaymentAmount = BigDecimal.ZERO;
                BigDecimal invoiceAmount = BigDecimal.ZERO;
                BigDecimal currentDateTotal = BigDecimal.ZERO;
                List<PaymentHistoryRecordVo> paymentHistoryRecordVos = dateListMap.get(localDate);
                // 计算当天汇款数
                currentPaymentAmount = paymentHistoryRecordVos.stream()
                                            .filter(item ->item.getType() == 0)
                                            .map(PaymentHistoryRecordVo::getCurrentPaymentAmount)
                                            .reduce(BigDecimal.ZERO,BigDecimal::add);
                // 计算今天来票数
                invoiceAmount = paymentHistoryRecordVos.stream()
                        .filter(item ->item.getType() == 1)
                        .map(PaymentHistoryRecordVo::getInvoiceAmount)
                        .reduce(BigDecimal.ZERO,BigDecimal::add);
                // 计算当日汇总
                currentDateTotal = currentDateTotal.add(invoiceAmount).subtract(currentPaymentAmount);
                PaymentHistoryRecordVo paymentHistoryRecordVo = new PaymentHistoryRecordVo();
                paymentHistoryRecordVo.setHappenTime(localDate);
                paymentHistoryRecordVo.setCurrentPaymentAmount(currentPaymentAmount);
                paymentHistoryRecordVo.setInvoiceAmount(invoiceAmount);
                amountTotal = paymentHistoryRecordVo.getInvoiceAmount().subtract(paymentHistoryRecordVo.getCurrentPaymentAmount());
                paymentHistoryRecordVo.setPayableAmount(amountTotal);
                result.add(paymentHistoryRecordVo);
            }
            for (int i = 0; i < result.size(); i++) {
                PaymentHistoryRecordVo paymentHistoryRecordVo = result.get(i);
                if (i == 0) {
                    paymentHistoryRecordVo.setPayableAmount(paymentHistoryRecordVo.getInvoiceAmount().subtract(paymentHistoryRecordVo.getCurrentPaymentAmount()));
                }else {
                    PaymentHistoryRecordVo paymentHistoryRecordVo1 = result.get(i-1);
                    paymentHistoryRecordVo.setPayableAmount(paymentHistoryRecordVo1.getPayableAmount()
                            .add(paymentHistoryRecordVo.getInvoiceAmount()).subtract(paymentHistoryRecordVo.getCurrentPaymentAmount()));
                }
                newResult.add(paymentHistoryRecordVo);
            }
        }
        return newResult;
    }
    /**
     * 查询付款登记列表分页
     *
     * @param paymentRegistrationDto 付款登记
     * @return 付款登记集合
     */
    @Override
    public IPage<PaymentRegistrationDto> paymentHistoryListPage(Page page, PaymentRegistrationDto paymentRegistrationDto) {
        return paymentRegistrationMapper.paymentHistoryListPage(page, paymentRegistrationDto);
    }
    @Override
    public Boolean delete(Long[] ids) {
        for (Long id : ids) {
            List<ProductRecord> productRecords = productRecordMapper.selectList(new QueryWrapper<ProductRecord>().lambda().eq(ProductRecord::getTicketRegistrationId, id));
            for (ProductRecord productRecord : productRecords) {
                SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(productRecord.getSaleLedgerProjectId());
                salesLedgerProduct.setTicketsNum(salesLedgerProduct.getTicketsNum().subtract(productRecord.getTicketsNum()));
                salesLedgerProduct.setTicketsAmount(salesLedgerProduct.getTicketsAmount().subtract(productRecord.getTicketsAmount()));
                salesLedgerProduct.setFutureTickets(salesLedgerProduct.getFutureTickets().add(productRecord.getTicketsNum()));
                salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getFutureTicketsAmount().add(productRecord.getTicketsAmount()));
                salesLedgerProductMapper.updateById(salesLedgerProduct);
            }
            ticketRegistrationMapper.delete(new QueryWrapper<TicketRegistration>().lambda().eq(TicketRegistration::getId, id));
            productRecordMapper.delete(new QueryWrapper<ProductRecord>().lambda().eq(ProductRecord::getTicketRegistrationId, id));
        }
        return true;
    }
    @Override
    public int delPaymentRegistration(Long id) {
//        PaymentRegistration paymentRegistration = paymentRegistrationMapper.selectById(id);
//        TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(paymentRegistration.getTicketRegistrationId());
//        if (ticketRegistration != null) {
//            ticketRegistration.setPaymentAmountTotal(ticketRegistration.getPaymentAmountTotal().subtract(paymentRegistration.getCurrentPaymentAmount()));
//            ticketRegistration.setUnPaymentAmountTotal(ticketRegistration.getUnPaymentAmountTotal().add(paymentRegistration.getCurrentPaymentAmount()));
//            ticketRegistrationMapper.updateById(ticketRegistration);
//        }
        return paymentRegistrationMapper.deleteById(id);
    }
    // 批量查询采购台账(当月)
    private Map<Long, List<PurchaseLedger>> batchQueryPurchaseLedgers(List<Long> supplierIds, LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<PurchaseLedger> query = new LambdaQueryWrapper<>();