package com.dk.mdm.service.mac; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.dk.common.exception.BaseBusinessException; import com.dk.common.infrastructure.annotaiton.Pagination; import com.dk.common.infrastructure.constant.Constant; import com.dk.common.infrastructure.enums.ErrorCodeEnum; import com.dk.common.response.ResponseCodeEnum; 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.mst.MoneyAccountItemMapper; 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.common.service.BaseService; import com.dk.common.mapper.BaseMapper; import com.dk.mdm.model.pojo.mac.AccountItem; import com.dk.mdm.model.pojo.mst.MoneyAccount; import com.dk.mdm.model.query.mac.AccountItemQuery; import com.dk.mdm.model.response.mac.AccountItemResponse; import com.dk.mdm.model.response.mac.AccountResponse; import com.dk.mdm.service.ivt.inbound.InboundService; import com.dk.mdm.service.ivt.outbound.OutboundService; import com.dk.mdm.service.mst.MoneyAccountService; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.List; import java.util.Map; import java.util.UUID; @Service @Transactional public class AccountService extends BaseService { @Override public String getPrimaryKey() { return "object_id"; } @Override public BaseMapper getRepository() { return accountMapper; } @Autowired private AccountMapper accountMapper; @Autowired private AccountItemMapper accountItemMapper; @Autowired private MoneyAccountService moneyAccountService; @Autowired private MoneyAccountItemMapper moneyAccountItemMapper; @Autowired private OutboundService outboundService; @Autowired private OutboundMapper outboundMapper; @Autowired private InboundService inboundService; @Autowired private InboundMapper inboundMapper; /** * @desc : 查看来源单据,总单加明细 * @author : 姜永辉 * @date : 2024/3/6 10:36 */ public ResponseResultVO selectById(String id) { //根据id查询 AccountResponse accountResponse = accountMapper.selectById(id); List accountItemResponses = accountItemMapper.getReceivableAccountItem(new AccountItemQuery().setObjectId(id)); accountResponse.setList(accountItemResponses); return ResponseResultUtil.success(accountResponse); } /** * @desc : 只查询总单, 不包含总单加明细 * @author : 姜永辉 * @date : 2024/3/6 10:36 */ public ResponseResultVO selectAccountById(String id) { //根据id查询 AccountResponse accountResponse = accountMapper.selectById(id); return ResponseResultUtil.success(accountResponse); } /** * @desc : 查询明细的总数量 * @author : 姜永辉 * @date : 2024/3/6 10:36 */ public ResponseResultVO countByCond(AccountItemQuery accountItemQuery) { //根据id查询 Long aLong = accountItemMapper.countByCond(accountItemQuery); return ResponseResultUtil.success(aLong); } /** * @desc : 查询应收账款明细 * @author : 付斌 * @date : 2024-02-28 13:25 */ @Pagination public ResponseResultVO getReceivableAccountItem(AccountItemQuery accountItemQuery) { List accountItemResponse = accountItemMapper.getReceivableAccountItem(accountItemQuery); return ResponseResultUtil.success(accountItemResponse); } /** * @desc : 查询应付账款明细 * @author : 付斌 * @date : 2024-02-28 13:25 */ @Pagination public ResponseResultVO getPayableAccountItem(AccountItemQuery accountItemQuery) { List accountItemResponse = accountItemMapper.getPayableAccountItem(accountItemQuery); return ResponseResultUtil.success(accountItemResponse); } /********************* 账款部分共通方法 begin **********************/ /** * @desc : 获取账款总表,没有则新建(客户) * @author : 付斌 * @date : 2024-03-23 16:32 */ public Account getCusAccountForUpdate(String objectId) { return getAccountForUpdate(objectId, Constant.ObjectType.CUS.getName()); } /** * @desc : 获取账款总表,没有则新建(供应商) * @author : 付斌 * @date : 2024-03-23 16:32 */ public Account getSupAccountForUpdate(String objectId) { return getAccountForUpdate(objectId, Constant.ObjectType.SUP.getName()); } /** * @desc : 更新总帐上收款类字段 * @author : 付斌 * @date : 2024-03-22 11:08 */ public void updateReceipt(String objectId) { Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId); Map mapSumAmtRec = accountItemMapper.getSumAmtRec(objectId); BigDecimal sumAmtRec = new BigDecimal(mapSumAmtRec.get("sumAmtRec").toString()); // 可退金额 = 总收款额-应收应款额+优惠金额 BigDecimal sumReceiptResidue = sumAmtRec.subtract(accountForUpdate.getReceivableHandle()).add(accountForUpdate.getReceivableWaive()); // 如果可退金额小于0 ,则提示余额不足 if (sumReceiptResidue.compareTo(BigDecimal.ZERO) == -1) { throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage()); } // 更新账款总表上的收款总额和可用金额 Account accountUpdate = new Account(); accountUpdate.setReceipt(sumAmtRec).setReceiptResidue(sumReceiptResidue).setObjectId(objectId); super.updateByUuid(accountUpdate); } /** * @desc : 更新资金账户余额 * @author : 付斌 * @date : 2024-03-22 11:08 */ public void updateMac(String macId) { // 查询当前账户流水合计 Map mapSumAmtInflow = moneyAccountItemMapper.getSumAmtInflow(macId); BigDecimal sumAmtInflow = new BigDecimal(mapSumAmtInflow.get("sumAmtInflow").toString()); // 如果余额小于0 ,则提示余额不足 if (sumAmtInflow.compareTo(BigDecimal.ZERO) == -1) { throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage()); } // 更新账款总表上的收款总额和可用金额 MoneyAccount moneyAccountUpdate = new MoneyAccount(); moneyAccountUpdate.setBalance(sumAmtInflow).setMacId(macId); moneyAccountService.updateByUuid(moneyAccountUpdate); } /** * @desc : 应收记账 * @author : 付斌 * @date : 2024-03-22 11:08 */ public void accReceivable(String invoiceId, String biznisType) { // 账款明细 AccountItem accountItemInsert = new AccountItem(); // 账务对象Id String objectId = null; if ("t_psi_outbound".equals(biznisType)) { Outbound outbound = outboundMapper.selectByIdForUpdate(invoiceId); objectId = outbound.getCusId(); // 当前单据已经记账 if (outbound.getReceivableId() != null) { throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage()); } // 插入账款明细 accountItemInsert.setAccItemType(Constant.accItemType.YING_SHOU.getName()) .setObjectId(objectId).setOrgId(outbound.getOrgId()).setStaffId(outbound.getStaffId()) .setAccDate(outbound.getOutDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()) .setAmtShould(outbound.getOutAmt()).setAmtResidue(outbound.getOutAmt()) .setBiznisType(biznisType).setBiznisId(outbound.getOutId()).setBiznisNo(outbound.getOutNo()) .setMakeStaff(outbound.getMakeStaff()).setCpId(outbound.getCpId()); accountItemMapper.insert(accountItemInsert); // 更新出库单上的账款明细Id Outbound outboundUpdate = new Outbound(); outboundUpdate.setReceivableId(accountItemInsert.getItemId()).setOutId(invoiceId); outboundService.updateByUuid(outboundUpdate); } // 更新账款总表上的总应收账款和总剩余应收 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); } /** * @desc : 应收反记账 * @author : 付斌 * @date : 2024-03-22 11:08 */ public void reverseReceivable(String invoiceId, String biznisType) { // 更新账款明细应收收款 AccountItem accountItemForUpdate = null; // 账务对象Id String objectId = null; if ("t_psi_outbound".equals(biznisType)) { Outbound outbound = outboundMapper.selectByIdForUpdate(invoiceId); objectId = outbound.getCusId(); // 当前单据已经记账 if (outbound.getReceivableId() == null) { throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage()); } accountItemForUpdate = accountItemMapper.selectByIdForUpdate(outbound.getReceivableId()); // 当前单据已核销 if (accountItemForUpdate.getAmtShould().compareTo(accountItemForUpdate.getAmtResidue()) != 0) { throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage()); } // 将出库单上的账款明细Id更为null LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.set(Outbound::getReceivableId, null).eq(Outbound::getOutId, UUID.fromString(invoiceId)); outboundMapper.update(null, updateWrapper); } // 删除账款明细 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); } /** * @desc : 应付记账 * @author : 付斌 * @date : 2024-03-22 11:08 */ public void accPayable(String invoiceId, String biznisType) { // 账款明细 AccountItem accountItemInsert = new AccountItem(); // 账务对象Id String objectId = null; if ("t_psi_inbound".equals(biznisType)) { Inbound inbound = inboundMapper.selectByIdForUpdate(invoiceId); objectId = inbound.getSupId(); // 当前单据已经记账 if (inbound.getReceivableId() != null) { throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage()); } // 插入账款明细 accountItemInsert.setAccItemType(Constant.accItemType.YING_SHOU.getName()) .setObjectId(objectId).setOrgId(inbound.getOrgId()).setStaffId(inbound.getStaffId()) .setAccDate(inbound.getIntoDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()) .setAmtShould(inbound.getIntoAmt()).setAmtResidue(inbound.getIntoAmt()) .setBiznisType(biznisType).setBiznisId(inbound.getIntoId()).setBiznisNo(inbound.getIntoNo()) .setMakeStaff(inbound.getMakeStaff()).setCpId(inbound.getCpId()); accountItemMapper.insert(accountItemInsert); // 更新出库单上的账款明细Id Inbound inboundUpdate = new Inbound(); inboundUpdate.setReceivableId(accountItemInsert.getItemId()).setIntoId(invoiceId); inboundService.updateByUuid(inboundUpdate); } // 更新账款总表上的总应收账款和总剩余应收 Account accountForUpdate = getSupAccountForUpdate(objectId); Account accountUpdate = new Account(); accountUpdate.setPayable(accountForUpdate.getPayable().add(accountItemInsert.getAmtShould())) .setPayableResidue(accountForUpdate.getPayableResidue().add(accountItemInsert.getAmtShould())) .setObjectId(objectId); super.updateByUuid(accountUpdate); } /** * @desc : 应付反记账 * @author : 付斌 * @date : 2024-03-22 11:08 */ public void reversePayable(String invoiceId, String biznisType) { // 更新账款明细应收收款 AccountItem accountItemForUpdate = null; // 账务对象Id String objectId = null; if ("t_psi_inbound".equals(biznisType)) { Inbound inbound = inboundMapper.selectByIdForUpdate(invoiceId); objectId = inbound.getSupId(); // 当前单据已经记账 if (inbound.getReceivableId() == null) { throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage()); } accountItemForUpdate = accountItemMapper.selectByIdForUpdate(inbound.getReceivableId()); // 当前单据已核销 if (accountItemForUpdate.getAmtShould().compareTo(accountItemForUpdate.getAmtResidue()) != 0) { throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage()); } // 将出库单上的账款明细Id更为null LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.set(Inbound::getReceivableId, null).eq(Inbound::getIntoId, UUID.fromString(invoiceId)); inboundMapper.update(null, updateWrapper); } // 删除账款明细 accountItemMapper.deleteById(accountItemForUpdate.getItemId()); // 更新账款总表上的总应收账款和总剩余应收 Account accountForUpdate = getSupAccountForUpdate(objectId); Account accountUpdate = new Account(); accountUpdate.setPayable(accountForUpdate.getPayable().subtract(accountItemForUpdate.getAmtShould())) .setPayableResidue(accountForUpdate.getPayableResidue().subtract(accountItemForUpdate.getAmtShould())) .setObjectId(objectId); super.updateByUuid(accountUpdate); } /********************* 账款部分共通方法 end **********************/ /********************* 账款部分私有方法 begin **********************/ /** * @desc : 获取账款总表,没有则新建(私有方法) * @author : 付斌 * @date : 2024-03-23 16:32 */ private Account getAccountForUpdate(String objectId, String objectType) { // 查询账款总表 Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId); // 没有账款对象,需要新建 if (accountForUpdate == null) { accountForUpdate = new Account(); accountForUpdate.setObjectId(objectId).setObjectType(objectType); accountMapper.insert(accountForUpdate); accountForUpdate = accountMapper.selectByIdForUpdate(objectId); } return accountForUpdate; } /********************* 账款部分私有方法 end **********************/ }