fubin il y a 2 ans
Parent
commit
7d07e4b136

+ 15 - 1
src/main/java/com/dk/mdm/mapper/mac/AccountItemMapper.java

@@ -39,7 +39,21 @@ public interface AccountItemMapper extends BaseMapper<AccountItem>{
      * @author : 付斌
      * @date   : 2024-03-09 11:36
      */
-    Map<String, Object> getSumAmtRecPay(String id);
+    Map<String, Object> getSumAmtRec(String id);
+
+    /**
+     * @desc   : 查询收款总额(锁定金额)
+     * @author : 付斌
+     * @date   : 2024-03-09 11:36
+     */
+    Map<String, Object> getSumAmtRecLock(String id);
+
+    /**
+     * @desc   : 查询付款总额
+     * @author : 付斌
+     * @date   : 2024-03-09 11:36
+     */
+    Map<String, Object> getSumAmtPay(String id);
 
     /**
      * @desc   : 查询订单未出库金额

+ 35 - 8
src/main/java/com/dk/mdm/mapper/mac/AccountItemMapper.xml

@@ -5,8 +5,7 @@
     <!-- 通用设置 -->
     <!-- 通用查询列 -->
     <sql id="Base_Column_List">
-        item_id
-        , acc_item_type, object_id, org_id, staff_id, biznis_type, biznis_id, biznis_no, acc_date, rec_status, amt_rec, amt_pay, amt_should, amt_handle, amt_waive, amt_residue, remarks, make_staff, make_time, flg_valid, cp_id
+        item_id, acc_item_type, object_id, org_id, staff_id, biznis_type, biznis_id, biznis_no, acc_date, rec_status, amt_rec, amt_pay, amt_should, amt_handle, amt_waive, amt_residue, flg_lock, remarks, make_staff, make_time, flg_valid, cp_id
     </sql>
 
     <!-- 通用查询映射结果 -->
@@ -27,6 +26,7 @@
         <result column="amt_handle" property="amtHandle"/>
         <result column="amt_waive" property="amtWaive"/>
         <result column="amt_residue" property="amtResidue"/>
+        <result column="flg_lock" property="flgLock"/>
         <result column="remarks" property="remarks"/>
         <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
         <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
@@ -52,6 +52,7 @@
         <result column="amt_handle" property="amtHandle"/>
         <result column="amt_waive" property="amtWaive"/>
         <result column="amt_residue" property="amtResidue"/>
+        <result column="flg_lock" property="flgLock"/>
         <result column="remarks" property="remarks"/>
         <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
         <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
@@ -112,6 +113,9 @@
         <if test="amtResidue != null">
             AND t.amt_residue = #{amtResidue}
         </if>
+        <if test="flgLock != null">
+            AND t.flg_lock = #{flgLock}
+        </if>
         <if test="remarks != null and remarks != ''">
             AND t.remarks = #{remarks}
         </if>
@@ -131,6 +135,9 @@
         <if test="formType == 'refund'">
             AND t.amt_residue <![CDATA[ < ]]> 0
         </if>
+        <if test="orderId != null">
+            AND tpo.from_id = #{orderId}::uuid
+        </if>
     </sql>
 
     <sql id="idsForeach">
@@ -368,12 +375,32 @@
     </insert>
 
     <!-- 查询收款总额 -->
-    <select id="getSumAmtRecPay" resultType="java.util.Map">
-        SELECT COALESCE(sum(tmai.amt_rec), 0) as "sumAmtRec",
-               COALESCE(sum(tmai.amt_pay), 0) as "sumAmtPay"
-        FROM dkic_b.t_mac_account_item as tmai
-        where tmai.flg_valid
-          and tmai.object_id = #{id}::uuid
+    <select id="getSumAmtRec" resultType="java.util.Map">
+        SELECT COALESCE(sum(t.amt_rec), 0) as "sumAmtRec"
+        FROM dkic_b.t_mac_account_item as t
+        where t.flg_valid
+          and t.flg_lock = false
+          and t.acc_item_type = '账款类型-收款'
+          and t.object_id = #{id}::uuid
+    </select>
+
+    <!-- 查询收款总额(锁定金额) -->
+    <select id="getSumAmtRecLock" resultType="java.util.Map">
+        SELECT COALESCE(sum(t.amt_rec), 0) as "sumAmtRecLock"
+        FROM dkic_b.t_mac_account_item as t
+        where t.flg_valid
+          and t.flg_lock
+          and t.acc_item_type = '账款类型-收款'
+          and t.object_id = #{id}::uuid
+    </select>
+
+    <!-- 查询付款总额 -->
+    <select id="getSumAmtPay" resultType="java.util.Map">
+        SELECT COALESCE(sum(t.amt_pay), 0) as "sumAmtPay"
+        FROM dkic_b.t_mac_account_item as t
+        where t.flg_valid
+          and t.acc_item_type = '账款类型-付款'
+          and t.object_id = #{id}::uuid
     </select>
 
     <!-- 查询订单未出库金额  -->

+ 5 - 0
src/main/java/com/dk/mdm/mapper/mac/RecPayHandleItemMapper.xml

@@ -171,4 +171,9 @@
             )
         </foreach>
     </insert>
+
+    <delete id="deleteById">
+        DELETE FROM dkic_b.t_mac_rec_pay_handle_item
+        WHERE item_id = #{id}::uuid;
+    </delete>
 </mapper>

+ 8 - 0
src/main/java/com/dk/mdm/model/pojo/mac/AccountItem.java

@@ -171,6 +171,14 @@ public class AccountItem extends PageInfo<AccountItem> implements Serializable {
 
 
     /**
+     * 锁定标识 (开单收款,自动锁定)
+     */
+    @Excel(name = "锁定标识 (开单收款,自动锁定)")
+    @ApiModelProperty(value = "锁定标识 (开单收款,自动锁定)")
+    private Boolean flgLock;
+
+
+    /**
      * 备注
      */
     @Excel(name = "备注")

+ 10 - 0
src/main/java/com/dk/mdm/model/query/mac/AccountItemQuery.java

@@ -173,6 +173,14 @@ public class AccountItemQuery extends PageInfo<AccountItemQuery> implements Seri
 
 
     /**
+     * 锁定标识 (开单收款,自动锁定)
+     */
+    @Excel(name = "锁定标识 (开单收款,自动锁定)")
+    @ApiModelProperty(value = "锁定标识 (开单收款,自动锁定)")
+    private Boolean flgLock;
+
+
+    /**
      * 备注
      */
     @Excel(name = "备注")
@@ -242,6 +250,8 @@ public class AccountItemQuery extends PageInfo<AccountItemQuery> implements Seri
     @ApiModelProperty(value = "功能入口类型:add/refund")
     private String formType;
 
+    @ApiModelProperty(value = "订单Id")
+    private String orderId;
 
     private static final long serialVersionUID = 1L;
 

+ 8 - 0
src/main/java/com/dk/mdm/model/response/mac/AccountItemResponse.java

@@ -173,6 +173,14 @@ public class AccountItemResponse extends PageInfo<AccountItemResponse> implement
 
 
     /**
+     * 锁定标识 (开单收款,自动锁定)
+     */
+    @Excel(name = "锁定标识 (开单收款,自动锁定)")
+    @ApiModelProperty(value = "锁定标识 (开单收款,自动锁定)")
+    private Boolean flgLock;
+
+
+    /**
      * 备注
      */
     @Excel(name = "备注")

+ 13 - 13
src/main/java/com/dk/mdm/service/ivt/outbound/OutboundSaleOrderService.java

@@ -270,7 +270,7 @@ public class OutboundSaleOrderService extends BaseService<Outbound> {
         }
         //endregion
 
-        //region  已出库  账
+        //region  已出库  应收记
         if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
             accountService.accReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
         }
@@ -326,9 +326,9 @@ public class OutboundSaleOrderService extends BaseService<Outbound> {
             //region  自动办理参数为true  已入库编辑
             //自动办理参数为true
             if (Constant.FlgHandleSetting.TRUE.getValue().equals(outboundVO.getFlgHandleSetting())) {
-                //region 退
+                //region 应收反记
                 if (outboundVO.getReceivableId() != null) {
-                    accountService.reversePayable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
+                    accountService.reverseReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
                 }
                 //endregion
 
@@ -493,9 +493,9 @@ public class OutboundSaleOrderService extends BaseService<Outbound> {
                 }
                 //endregion
 
-                //region  账
+                //region  应收记
                 if (outboundVO.getReceivableId() != null) {
-                    accountService.reversePayable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
+                    accountService.reverseReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
                 }
                 //endregion
 
@@ -676,9 +676,9 @@ public class OutboundSaleOrderService extends BaseService<Outbound> {
         //region  自动办理参数为true  已入库编辑
         //自动办理参数为true
         if (Constant.FlgHandleSetting.TRUE.getValue().equals(outboundResponse.getFlgHandleSetting())) {
-            //region 退
+            //region 应收反记
             if (outboundResponse.getReceivableId() != null) {
-                accountService.reversePayable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
+                accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
             }
             //endregion
 
@@ -755,9 +755,9 @@ public class OutboundSaleOrderService extends BaseService<Outbound> {
 
             //endregion
 
-            //region  
+            //region  应收反记
             if (outboundResponse.getReceivableId() != null) {
-                accountService.reversePayable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
+                accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
             }
             //endregion
 
@@ -1051,8 +1051,8 @@ public class OutboundSaleOrderService extends BaseService<Outbound> {
         }
         //endregion
 
-        //region  
-        accountService.reversePayable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
+        //region  应收记
+        accountService.accReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
         //endregion
 
         //region   调用库存
@@ -1075,9 +1075,9 @@ public class OutboundSaleOrderService extends BaseService<Outbound> {
         OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
         //endregion
 
-        //region    退
+        //region    应收反记
         if (outboundResponse.getReceivableId() != null) {
-            accountService.reversePayable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
+            accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
         }
         //endregion
 

+ 241 - 28
src/main/java/com/dk/mdm/service/mac/AccountService.java

@@ -10,21 +10,17 @@ import com.dk.common.response.ResponseResultUtil;
 import com.dk.common.response.ResponseResultVO;
 import com.dk.mdm.mapper.ivt.InboundMapper;
 import com.dk.mdm.mapper.ivt.OutboundMapper;
-import com.dk.mdm.mapper.mac.AccountItemMapper;
-import com.dk.mdm.mapper.mac.OtherPayableMapper;
-import com.dk.mdm.mapper.mac.OtherReceivableMapper;
+import com.dk.mdm.mapper.mac.*;
 import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
 import com.dk.mdm.mapper.mst.MoneyAccountMapper;
+import com.dk.mdm.mapper.sale.OrderMapper;
 import com.dk.mdm.model.pojo.ivt.Inbound;
 import com.dk.mdm.model.pojo.ivt.Outbound;
-import com.dk.mdm.model.pojo.mac.Account;
-import com.dk.mdm.mapper.mac.AccountMapper;
+import com.dk.mdm.model.pojo.mac.*;
 import com.dk.common.service.BaseService;
 import com.dk.common.mapper.BaseMapper;
-import com.dk.mdm.model.pojo.mac.AccountItem;
-import com.dk.mdm.model.pojo.mac.OtherPayable;
-import com.dk.mdm.model.pojo.mac.OtherReceivable;
 import com.dk.mdm.model.pojo.mst.MoneyAccount;
+import com.dk.mdm.model.pojo.sale.Order;
 import com.dk.mdm.model.query.mac.AccountItemQuery;
 import com.dk.mdm.model.response.mac.AccountItemResponse;
 import com.dk.mdm.model.response.mac.AccountResponse;
@@ -34,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
@@ -56,6 +53,9 @@ public class AccountService extends BaseService<Account> {
     private AccountMapper accountMapper;
 
     @Autowired
+    private AccountItemService accountItemService;
+
+    @Autowired
     private AccountItemMapper accountItemMapper;
 
     @Autowired
@@ -79,6 +79,21 @@ public class AccountService extends BaseService<Account> {
     @Autowired
     private OtherPayableMapper otherPayableMapper;
 
+    @Autowired
+    private OrderMapper orderMapper;
+
+    @Autowired
+    private RecPayMapper recPayMapper;
+
+    @Autowired
+    private ReceiptService receiptService;
+
+    @Autowired
+    private RecPayItemMapper recPayItemMapper;
+
+    @Autowired
+    private RecPayHandleItemMapper recPayHandleItemMapper;
+
     /**
      * @desc : 查看来源单据,总单加明细
      * @author : 姜永辉
@@ -163,7 +178,7 @@ public class AccountService extends BaseService<Account> {
      */
     public void updateReceipt(String objectId) {
         Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
-        Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtRecPay(objectId);
+        Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtRec(objectId);
         BigDecimal sumAmtRec = new BigDecimal(mapSumAmtRecPay.get("sumAmtRec").toString());
         // 可退金额 = 总收款额-应收应款额+优惠金额
         BigDecimal sumReceiptResidue = sumAmtRec.subtract(accountForUpdate.getReceivableHandle()).add(accountForUpdate.getReceivableWaive());
@@ -180,13 +195,29 @@ public class AccountService extends BaseService<Account> {
     }
 
     /**
+     * @desc : 更新总帐上收款类字段
+     * @author : 付斌
+     * @date : 2024-03-22 11:08
+     */
+    public void updateReceiptLock(String objectId) {
+        Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
+        Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtRecLock(objectId);
+        BigDecimal sumAmtRecLock = new BigDecimal(mapSumAmtRecPay.get("sumAmtRecLock").toString());
+
+        // 更新账款总表上的收款总额和可用金额
+        Account accountUpdate = new Account();
+        accountUpdate.setReceiptLock(sumAmtRecLock).setObjectId(accountForUpdate.getObjectId());
+        super.updateByUuid(accountUpdate);
+    }
+
+    /**
      * @desc : 更新总帐上付款类字段
      * @author : 付斌
      * @date : 2024-03-22 11:08
      */
     public void updatePayment(String objectId) {
         Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
-        Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtRecPay(objectId);
+        Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtPay(objectId);
         BigDecimal sumAmtPay = new BigDecimal(mapSumAmtRecPay.get("sumAmtPay").toString());
         // 可退金额 = 总收款额-应收应款额+优惠金额
         BigDecimal sumPaymentResidue = sumAmtPay.subtract(accountForUpdate.getPayableHandle()).add(accountForUpdate.getPayableWaive());
@@ -311,6 +342,117 @@ public class AccountService extends BaseService<Account> {
             LambdaUpdateWrapper<Outbound> updateWrapper = new LambdaUpdateWrapper<>();
             updateWrapper.set(Outbound::getReceivableId, UUID.fromString(accountItemInsert.getItemId())).eq(Outbound::getOutId, UUID.fromString(invoiceId));
             outboundMapper.update(null, updateWrapper);
+
+            // 更新账款总表上的总应收账款和总剩余应收
+            Account accountForUpdate = getCusAccountForUpdate(objectId);
+            Account accountUpdate = new Account();
+            accountUpdate.setReceivable(accountForUpdate.getReceivable().add(accountItemInsert.getAmtShould()))
+                    .setReceivableResidue(accountForUpdate.getReceivableResidue().add(accountItemInsert.getAmtShould()))
+                    .setObjectId(objectId);
+            super.updateByUuid(accountUpdate);
+
+            // 如果当前出库单对应的订单全部出库了,并且订单有收款(锁定金额),则自动做应收核销
+            // 如果是销售出库
+            if(outbound.getOutType().equals(Constant.OutType.SALE.getName())){
+                Order orderForUpdate = orderMapper.selectByIdForUpdate(outbound.getFromId());
+                // 如果当前订单已经全部出库
+                if(orderForUpdate.getOutQty().compareTo(orderForUpdate.getSumQuantity()) == 0){
+                    RecPay recPayForUpdate = recPayMapper.selectByBiznisIdForUpdate(orderForUpdate.getOrderId());
+                    if(recPayForUpdate != null && recPayForUpdate.getFlgLock()){
+                        // 先把收款单解锁
+                        RecPay recPayUpdate = new RecPay();
+                        recPayUpdate.setFlgLock(false).setRpId(recPayForUpdate.getRpId());
+                        receiptService.updateByUuid(recPayUpdate);
+
+                        // 账款明细解锁
+                        List<RecPayItem> recPayItemList = recPayItemMapper.selectByZIdForUpdate(recPayForUpdate.getRpId());
+                        for (RecPayItem recPayItem : recPayItemList) {
+                            AccountItem accountItemUpdate = new AccountItem();
+                            accountItemUpdate.setFlgLock(false).setItemId(recPayItem.getAccItemId());
+                            accountItemService.updateByUuid(accountItemUpdate);
+                        }
+
+                        // 钱进到现金池中
+                        accountUpdate = new Account();
+                        accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayForUpdate.getSumAmtRec()))
+                                .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayForUpdate.getSumAmtRec()))
+                                .setReceiptLock(accountForUpdate.getReceiptLock().subtract(recPayForUpdate.getSumAmtRec()))
+                                .setObjectId(objectId);
+                        super.updateByUuid(accountUpdate);
+
+                        List<AccountItemResponse> accountItemResponseList = accountItemMapper.getReceivableAccountItem(
+                                new AccountItemQuery().setObjectId(objectId).setOrderId(orderForUpdate.getOrderId()));
+
+                        // 锁定金额
+                        BigDecimal lockAmt = recPayForUpdate.getSumAmtRec();
+                        // 记录核销了哪些应收账
+                        List<RecPayHandleItem> receivableList = new ArrayList<>();
+                        for (AccountItemResponse accountItemResponse : accountItemResponseList) {
+                            RecPayHandleItem recPayHandleItem = new RecPayHandleItem();
+                            recPayHandleItem.setRpId(recPayForUpdate.getRpId()).setCpId(recPayForUpdate.getCpId()).setAccDate(recPayForUpdate.getAccDate())
+                                .setAccItemId(accountItemResponse.getAccItemId());
+                            // 如果定金比应收金额大
+                            if(lockAmt.compareTo(accountItemResponse.getAmtResidue()) == 1){
+                                recPayHandleItem.setAmtReceivableHandle(accountItemResponse.getAmtResidue());
+                                lockAmt = lockAmt.subtract(accountItemResponse.getAmtResidue());
+                            }else{
+                                recPayHandleItem.setAmtReceivableHandle(lockAmt);
+                                lockAmt = BigDecimal.ZERO;
+                            }
+                            recPayHandleItemMapper.insert(recPayHandleItem);
+                            receivableList.add(recPayHandleItem);
+
+                            // 账款明细的核销金额和优惠金额
+                            AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
+                            AccountItem accountItemUpdate = new AccountItem();
+                            // 核销金额,超出剩余应收金额
+                            if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
+                                throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
+                            }
+                            accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
+                                    .setItemId(recPayHandleItem.getAccItemId());
+                            // 剩余金额 = 应收金额-应收收款金额
+                            accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
+                            accountItemService.updateByUuid(accountItemUpdate);
+                            
+                            // 如果定金都用完了,跳出循环
+                            if(lockAmt.compareTo(BigDecimal.ZERO)==0){
+                                break;
+                            }
+                        }
+
+                        // 计算明细的核销金额,优惠金额合计
+                        RecPayHandleItem recPayHandleItem = receivableList.stream().reduce((x, y) -> {
+                            RecPayHandleItem item = new RecPayHandleItem();
+                            item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
+                            return item;
+                        }).get();
+
+                        // 更新收款单
+                        recPayUpdate = new RecPay();
+                        recPayUpdate.setSumAmtReceivableHandle(recPayHandleItem.getAmtReceivableHandle())
+                                .setSumShouldHandle(recPayHandleItem.getAmtReceivableHandle())
+                                .setRpId(recPayForUpdate.getRpId());
+                        receiptService.updateByUuid(recPayUpdate);
+
+                        // 更新总账上
+                        accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
+                        accountUpdate = new Account();
+                        accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItem.getAmtReceivableHandle()))// 总应收收款金额
+                                .setObjectId(accountForUpdate.getObjectId());
+                        // 剩余应收 = 总应收账款-总应收收款金额
+                        accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
+                        // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
+                        accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()));
+
+                        // 可用金额为负数,则不能保存
+                        if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
+                            throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
+                        }
+                        super.updateByUuid(accountUpdate);
+                    }
+                }
+            }
         }
         // 其他收入单
         else if ("t_mac_other_receivable".equals(biznisType)) {
@@ -339,15 +481,15 @@ public class AccountService extends BaseService<Account> {
             LambdaUpdateWrapper<OtherReceivable> updateWrapper = new LambdaUpdateWrapper<>();
             updateWrapper.set(OtherReceivable::getAccItemId, UUID.fromString(accountItemInsert.getItemId())).eq(OtherReceivable::getReceivableId, UUID.fromString(invoiceId));
             otherReceivableMapper.update(null, updateWrapper);
-        }
 
-        // 更新账款总表上的总应收账款和总剩余应收
-        Account accountForUpdate = getCusAccountForUpdate(objectId);
-        Account accountUpdate = new Account();
-        accountUpdate.setReceivable(accountForUpdate.getReceivable().add(accountItemInsert.getAmtShould()))
-                .setReceivableResidue(accountForUpdate.getReceivableResidue().add(accountItemInsert.getAmtShould()))
-                .setObjectId(objectId);
-        super.updateByUuid(accountUpdate);
+            // 更新账款总表上的总应收账款和总剩余应收
+            Account accountForUpdate = getCusAccountForUpdate(objectId);
+            Account accountUpdate = new Account();
+            accountUpdate.setReceivable(accountForUpdate.getReceivable().add(accountItemInsert.getAmtShould()))
+                    .setReceivableResidue(accountForUpdate.getReceivableResidue().add(accountItemInsert.getAmtShould()))
+                    .setObjectId(objectId);
+            super.updateByUuid(accountUpdate);
+        }
     }
 
     /**
@@ -370,6 +512,66 @@ public class AccountService extends BaseService<Account> {
                 throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
             }
 
+            Account accountForUpdate;
+            Account accountUpdate;
+            // 如果当前出库单对应的订单做了应收收款,则自动作废应收核销,并且锁定金额订单收款(锁定金额)
+            // 如果是销售出库
+            if(outbound.getOutType().equals(Constant.OutType.SALE.getName())){
+                RecPay recPayForUpdate = recPayMapper.selectByBiznisIdForUpdate(outbound.getFromId());
+                // 查出并锁定所有应收核销明细
+                List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(recPayForUpdate.getRpId());
+                for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
+                    // 更新账款明细应收收款
+                    accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
+                    AccountItem accountItemUpdate = new AccountItem();
+                    accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtReceivableHandle()))
+                            .setItemId(recPayHandleItemForUpdate.getAccItemId());
+                    accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
+                    accountItemService.updateByUuid(accountItemUpdate);
+
+                    // 删掉核销明细
+                    recPayHandleItemMapper.deleteById(recPayHandleItemForUpdate.getItemId());
+                }
+
+                // 把总帐上的钱加回来
+                accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
+                accountUpdate = new Account();
+                accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayForUpdate.getSumAmtReceivableHandle()))// 总应收收款金额
+                        .setObjectId(accountForUpdate.getObjectId());
+                // 剩余应收 = 总应收账款-总应收收款金额
+                accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
+                // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
+                accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()));
+
+                // 更新前的最后校验
+                // 可用金额为负数,则不能保存
+                if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
+                    throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
+                }
+                super.updateByUuid(accountUpdate);
+
+                // 先把收款单锁定
+                RecPay recPayUpdate = new RecPay();
+                recPayUpdate.setFlgLock(true).setRpId(recPayForUpdate.getRpId());
+                receiptService.updateByUuid(recPayUpdate);
+
+                // 账款明细解锁
+                List<RecPayItem> recPayItemList = recPayItemMapper.selectByZIdForUpdate(recPayForUpdate.getRpId());
+                for (RecPayItem recPayItem : recPayItemList) {
+                    AccountItem accountItemUpdate = new AccountItem();
+                    accountItemUpdate.setFlgLock(true).setItemId(recPayItem.getAccItemId());
+                    accountItemService.updateByUuid(accountItemUpdate);
+                }
+
+                // 钱进到锁定金额中
+                accountUpdate = new Account();
+                accountUpdate.setReceipt(accountForUpdate.getReceipt().subtract(recPayForUpdate.getSumAmtRec()))
+                        .setReceiptResidue(accountForUpdate.getReceiptResidue().subtract(recPayForUpdate.getSumAmtRec()))
+                        .setReceiptLock(accountForUpdate.getReceiptLock().add(recPayForUpdate.getSumAmtRec()))
+                        .setObjectId(objectId);
+                super.updateByUuid(accountUpdate);
+            }
+
             accountItemForUpdate = accountItemMapper.selectByIdForUpdate(outbound.getReceivableId());
             // 如果核销金额不为0,说明当前单据已核销
             if (accountItemForUpdate.getAmtHandle().compareTo(BigDecimal.ZERO) != 0) {
@@ -380,6 +582,17 @@ public class AccountService extends BaseService<Account> {
             LambdaUpdateWrapper<Outbound> updateWrapper = new LambdaUpdateWrapper<>();
             updateWrapper.set(Outbound::getReceivableId, null).eq(Outbound::getOutId, UUID.fromString(invoiceId));
             outboundMapper.update(null, updateWrapper);
+
+            // 删除账款明细
+            accountItemMapper.deleteById(accountItemForUpdate.getItemId());
+
+            // 更新账款总表上的总应收账款和总剩余应收
+            accountForUpdate = getCusAccountForUpdate(objectId);
+            accountUpdate = new Account();
+            accountUpdate.setReceivable(accountForUpdate.getReceivable().subtract(accountItemForUpdate.getAmtShould()))
+                    .setReceivableResidue(accountForUpdate.getReceivableResidue().subtract(accountItemForUpdate.getAmtShould()))
+                    .setObjectId(objectId);
+            super.updateByUuid(accountUpdate);
         }
         // 其他收入单
         else if ("t_mac_other_receivable".equals(biznisType)) {
@@ -401,18 +614,18 @@ public class AccountService extends BaseService<Account> {
             LambdaUpdateWrapper<OtherReceivable> updateWrapper = new LambdaUpdateWrapper<>();
             updateWrapper.set(OtherReceivable::getAccItemId, null).eq(OtherReceivable::getReceivableId, UUID.fromString(invoiceId));
             otherReceivableMapper.update(null, updateWrapper);
-        }
 
-        // 删除账款明细
-        accountItemMapper.deleteById(accountItemForUpdate.getItemId());
+            // 删除账款明细
+            accountItemMapper.deleteById(accountItemForUpdate.getItemId());
 
-        // 更新账款总表上的总应收账款和总剩余应收
-        Account accountForUpdate = getCusAccountForUpdate(objectId);
-        Account accountUpdate = new Account();
-        accountUpdate.setReceivable(accountForUpdate.getReceivable().subtract(accountItemForUpdate.getAmtShould()))
-                .setReceivableResidue(accountForUpdate.getReceivableResidue().subtract(accountItemForUpdate.getAmtShould()))
-                .setObjectId(objectId);
-        super.updateByUuid(accountUpdate);
+            // 更新账款总表上的总应收账款和总剩余应收
+            Account accountForUpdate = getCusAccountForUpdate(objectId);
+            Account accountUpdate = new Account();
+            accountUpdate.setReceivable(accountForUpdate.getReceivable().subtract(accountItemForUpdate.getAmtShould()))
+                    .setReceivableResidue(accountForUpdate.getReceivableResidue().subtract(accountItemForUpdate.getAmtShould()))
+                    .setObjectId(objectId);
+            super.updateByUuid(accountUpdate);
+        }
     }
 
     /**

+ 8 - 9
src/main/java/com/dk/mdm/service/sale/OrderService.java

@@ -267,7 +267,7 @@ public class OrderService extends BaseService<Order> {
                     .setRpType(Constant.RpType.SHOU_KUAN.getName()).setObjectId(orderVO.getCusId())
                     .setOrgId(orderVO.getOrgId()).setStaffId(orderVO.getStaffId()).setAccDate(LocalDate.now())
                     .setBiznisType(Constant.InventoryDocCode.ORDER.getTableName()).setBiznisId(orderVO.getOrderId()).setBiznisNo(orderVO.getOrderNo())
-                    .setMakeStaff(orderVO.getMakeStaff()).setCpId(orderVO.getCpId());
+                    .setMakeStaff(orderVO.getMakeStaff()).setCpId(orderVO.getCpId()).setFlgLock(true);
             // 计算订单明细的出库中数量,金额合计
             RecPayItemVO itemSum = orderVO.getReceiptList().stream().reduce((x, y) -> {
                 RecPayItemVO item = new RecPayItemVO();
@@ -291,7 +291,7 @@ public class OrderService extends BaseService<Order> {
                         .setObjectId(recPay.getObjectId()).setOrgId(recPay.getOrgId()).setStaffId(recPay.getStaffId())
                         .setAccDate(recPay.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
                         .setBiznisType(Constant.InventoryDocCode.REC_PAY_ITEM.getTableName()).setBiznisId(recPayItem.getItemId()).setBiznisNo(recPay.getRpNo())
-                        .setMakeStaff(recPay.getMakeStaff()).setCpId(recPay.getCpId());
+                        .setMakeStaff(recPay.getMakeStaff()).setCpId(recPay.getCpId()).setFlgLock(true);
                 accountItemMapper.insert(accountItem);
 
                 // 更新收款单上的账款明细Id
@@ -318,8 +318,7 @@ public class OrderService extends BaseService<Order> {
             Account accountForUpdate = accountService.getCusAccountForUpdate(recPay.getObjectId());
             // 更新账款总表上收款的相关字段
             Account accountUpdate = new Account();
-            accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPay.getSumAmtRec()))// 总收款金额
-                    .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPay.getSumAmtRec()))//  可退金额
+            accountUpdate.setReceiptLock(accountForUpdate.getReceiptLock().add(recPay.getSumAmtRec()))//  锁定金额
                     .setObjectId(accountForUpdate.getObjectId());
             accountService.updateByUuid(accountUpdate);
         }
@@ -449,7 +448,7 @@ public class OrderService extends BaseService<Order> {
                         .setRpType(Constant.RpType.SHOU_KUAN.getName()).setObjectId(orderVO.getCusId())
                         .setOrgId(orderVO.getOrgId()).setStaffId(orderVO.getStaffId()).setAccDate(LocalDate.now())
                         .setBiznisType(Constant.InventoryDocCode.ORDER.getTableName()).setBiznisId(orderVO.getOrderId()).setBiznisNo(orderVO.getOrderNo())
-                        .setMakeStaff(orderVO.getMakeStaff()).setCpId(orderVO.getCpId());
+                        .setMakeStaff(orderVO.getMakeStaff()).setCpId(orderVO.getCpId()).setFlgLock(true);
 
                 recPay.setSumAmtRec(itemSum.getAmtRec());
                 recPayMapper.insert(recPay);
@@ -475,7 +474,7 @@ public class OrderService extends BaseService<Order> {
                         .setObjectId(recPay.getObjectId()).setOrgId(recPay.getOrgId()).setStaffId(recPay.getStaffId())
                         .setAccDate(recPay.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
                         .setBiznisType(Constant.InventoryDocCode.REC_PAY_ITEM.getTableName()).setBiznisId(recPayItem.getItemId()).setBiznisNo(recPay.getRpNo())
-                        .setMakeStaff(recPay.getMakeStaff()).setCpId(recPay.getCpId());
+                        .setMakeStaff(recPay.getMakeStaff()).setCpId(recPay.getCpId()).setFlgLock(true);
                 accountItemMapper.insert(accountItem);
 
                 // 更新收款单上的账款明细Id
@@ -496,8 +495,8 @@ public class OrderService extends BaseService<Order> {
             }
         }
         if(recPayForUpdate != null) {
-            // 更新总账表的总收款额和可用
-            accountService.updateReceipt(recPayForUpdate.getObjectId());
+            // 更新总账表的锁定金
+            accountService.updateReceiptLock(recPayForUpdate.getObjectId());
         }
 
         // 更新账户余额
@@ -553,7 +552,7 @@ public class OrderService extends BaseService<Order> {
             }
 
             // 更新总账表的总收款额和可用额
-            accountService.updateReceipt(recPayForUpdate.getObjectId());
+            accountService.updateReceiptLock(recPayForUpdate.getObjectId());
 
             // 更新账户余额
             for (String macId : macList) {