chenhj
2025-05-15 ffd0d1fde083d3094363c67e1236a2b3d6736e1c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.ruoyi.purchase.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.mapper.SupplierManageMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.purchase.dto.PaymentRegistrationDto;
import com.ruoyi.purchase.mapper.InvoicePurchaseMapper;
import com.ruoyi.purchase.mapper.PaymentRegistrationMapper;
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
import com.ruoyi.purchase.pojo.InvoicePurchase;
import com.ruoyi.purchase.pojo.PaymentRegistration;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.purchase.service.IPaymentRegistrationService;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.List;
 
/**
 * 付款登记Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-15
 */
@Service
public class PaymentRegistrationServiceImpl extends ServiceImpl<PaymentRegistrationMapper, PaymentRegistration> implements IPaymentRegistrationService {
    @Autowired
    private PaymentRegistrationMapper paymentRegistrationMapper;
 
    @Autowired
    private PurchaseLedgerMapper purchaseLedgerMapper;
 
    @Autowired
    private InvoicePurchaseMapper invoicePurchaseMapper;
 
    @Autowired
    private SalesLedgerMapper salesLedgerMapper;
 
    @Autowired
    private SupplierManageMapper supplierManageMapper;
 
    /**
     * 查询付款登记
     *
     * @param id 付款登记主键
     * @return 付款登记
     */
    @Override
    public PaymentRegistration selectPaymentRegistrationById(Long id) {
        return paymentRegistrationMapper.selectPaymentRegistrationById(id);
    }
 
    /**
     * 查询付款登记列表
     *
     * @param paymentRegistrationDto 付款登记
     * @return 付款登记
     */
    @Override
    public List<PaymentRegistrationDto> selectPaymentRegistrationList(PaymentRegistrationDto paymentRegistrationDto) {
        List<PaymentRegistrationDto> list = paymentRegistrationMapper.selectPaymentRegistrationList(paymentRegistrationDto);
        for (PaymentRegistrationDto registrationDto : list) {
            List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                    .eq("invoice_purchase_id", registrationDto.getInvoicePurchaseId()));
            BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            registrationDto.setUnPaymentAmount(registrationDto.getInvoiceAmount().subtract(total));
        }
        return list;
    }
 
    /**
     * 新增付款登记
     *
     * @param paymentRegistration 付款登记
     * @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("关联销售合同号不存在");
        }
 
        paymentRegistration.setSaleLedgerId(salesLedger.getId());
        paymentRegistration.setSupplierId(purchaseLedger.getSupplierId());
 
        List<InvoicePurchase> invoicePurchases = invoicePurchaseMapper.selectList(new QueryWrapper<InvoicePurchase>().
                eq("purchase_contract_no", purchaseLedger.getPurchaseContractNumber()));
        if (invoicePurchases == null || invoicePurchases.size() == 0) {
            throw new RuntimeException("关联发票不存在");
        }
        paymentRegistration.setInvoicePurchaseId(invoicePurchases.get(0).getId());
 
        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                .eq("invoice_purchase_id", invoicePurchases.get(0).getId()));
        BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
 
        if (total.add(paymentRegistration.getCurrentPaymentAmount()).compareTo(invoicePurchases.get(0).getInvoiceAmount()) > 0) {
            throw new RuntimeException("付款金额超出发票金额");
        }
 
 
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Integer tenantId = loginUser.getTenantId();
        paymentRegistration.setTenantId(tenantId.longValue());
        paymentRegistration.setCreateTime(DateUtils.getNowDate());
        paymentRegistration.setUpdateTime(DateUtils.getNowDate());
        return paymentRegistrationMapper.insert(paymentRegistration);
    }
 
    /**
     * 修改付款登记
     *
     * @param paymentRegistration 付款登记
     * @return 结果
     */
    @Override
    public int updatePaymentRegistration(PaymentRegistration paymentRegistration) {
        InvoicePurchase invoicePurchase = invoicePurchaseMapper.selectById(paymentRegistration.getInvoicePurchaseId());
 
        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                .eq("invoice_purchase_id", paymentRegistration.getInvoicePurchaseId()).ne("id", paymentRegistration.getId()));
        BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
 
        if (total.add(paymentRegistration.getCurrentPaymentAmount()).compareTo(invoicePurchase.getInvoiceAmount()) > 0) {
            throw new RuntimeException("付款金额超出发票金额");
        }
 
        paymentRegistration.setUpdateTime(DateUtils.getNowDate());
        return paymentRegistrationMapper.updateById(paymentRegistration);
    }
 
    /**
     * 批量删除付款登记
     *
     * @param ids 需要删除的付款登记主键
     * @return 结果
     */
    @Override
    public int deletePaymentRegistrationByIds(Long[] ids) {
        return paymentRegistrationMapper.delete(new QueryWrapper<PaymentRegistration>().in("id", ids));
    }
 
    @Override
    public PaymentRegistration selectPaymentRegistrationByPurchaseId(Long id) {
        PaymentRegistrationDto paymentRegistrationDto = new PaymentRegistrationDto();
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id);
        paymentRegistrationDto.setSalesContractNo(purchaseLedger.getSalesContractNo());
        paymentRegistrationDto.setSupplierName(purchaseLedger.getSupplierName());
        paymentRegistrationDto.setSupplierId(purchaseLedger.getSupplierId());
 
        List<InvoicePurchase> invoicePurchaseList = invoicePurchaseMapper.selectList(new QueryWrapper<InvoicePurchase>()
                .eq("purchase_contract_no", purchaseLedger.getPurchaseContractNumber()));
        if (invoicePurchaseList != null && invoicePurchaseList.size() > 0) {
            paymentRegistrationDto.setInvoiceNumber(invoicePurchaseList.get(0).getInvoiceNumber());
            paymentRegistrationDto.setInvoiceAmount(invoicePurchaseList.get(0).getInvoiceAmount());
            paymentRegistrationDto.setTaxRate(invoicePurchaseList.get(0).getTaxRate());
        }
        return paymentRegistrationDto;
    }
}