| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102 |
- package com.dk.mdm.service.mac;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
- 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.model.pojo.PageList;
- import com.dk.common.response.ResponseCodeEnum;
- import com.dk.common.response.ResponseResultUtil;
- import com.dk.common.response.ResponseResultVO;
- import com.dk.mdm.infrastructure.convert.mac.RecPayConvert;
- import com.dk.mdm.infrastructure.convert.mac.RecPayHandleItemConvert;
- import com.dk.mdm.infrastructure.convert.mac.RecPayItemConvert;
- import com.dk.mdm.infrastructure.util.AuthUtils;
- import com.dk.mdm.mapper.mac.*;
- import com.dk.mdm.mapper.mst.CustomerMapper;
- import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
- import com.dk.mdm.mapper.mst.MoneyAccountMapper;
- import com.dk.mdm.mapper.mst.SaleChannelMapper;
- import com.dk.mdm.model.pojo.ivt.Inbound;
- import com.dk.mdm.model.pojo.ivt.OutboundItem;
- 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.mst.Customer;
- import com.dk.mdm.model.pojo.mst.MoneyAccount;
- import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
- import com.dk.mdm.model.query.mac.RecPayHandleItemQuery;
- import com.dk.mdm.model.query.mac.RecPayItemQuery;
- import com.dk.mdm.model.query.mac.RecPayQuery;
- import com.dk.mdm.model.query.mst.CustomerQuery;
- import com.dk.mdm.model.query.mst.SaleChannelQuery;
- import com.dk.mdm.model.response.mac.RecPayHandleItemResponse;
- import com.dk.mdm.model.response.mac.RecPayItemResponse;
- import com.dk.mdm.model.response.mac.RecPayResponse;
- import com.dk.mdm.model.response.mst.CustomerResponse;
- import com.dk.mdm.model.response.mst.SaleChannelResponse;
- import com.dk.mdm.model.vo.mac.RecPayHandleItemVO;
- import com.dk.mdm.model.vo.mac.RecPayItemVO;
- import com.dk.mdm.model.vo.mac.RecPayVO;
- import com.dk.mdm.service.common.CommonService;
- import com.dk.mdm.service.mst.CustomerService;
- 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.*;
- @Service
- @Transactional
- public class ReceiptService extends BaseService<RecPay> {
- @Override
- public String getPrimaryKey() {
- return "rp_id";
- }
- @Override
- public BaseMapper<RecPay> getRepository() {
- return recPayMapper;
- }
- @Autowired
- private RecPayMapper recPayMapper;
- @Autowired
- private RecPayItemService recPayItemService;
- @Autowired
- private RecPayItemMapper recPayItemMapper;
- @Autowired
- private AccountService accountService;
- @Autowired
- private AccountMapper accountMapper;
- @Autowired
- private AccountItemService accountItemService;
- @Autowired
- private MoneyAccountMapper moneyAccountMapper;
- @Autowired
- private AccountItemMapper accountItemMapper;
- @Autowired
- private MoneyAccountService moneyAccountService;
- @Autowired
- private MoneyAccountItemMapper moneyAccountItemMapper;
- @Autowired
- private RecPayHandleItemService recPayHandleItemService;
- @Autowired
- private RecPayHandleItemMapper recPayHandleItemMapper;
- @Autowired
- private CommonService commonService;
- @Autowired
- private CustomerMapper customerMapper;
- @Autowired
- private SaleChannelMapper saleChannelMapper;
- @Autowired
- private CustomerService customerService;
- @Autowired
- private RecPayConvert recPayConvert;
- @Autowired
- private RecPayItemConvert recPayItemConvert;
- @Autowired
- private RecPayHandleItemConvert recPayHandleItemConvert;
- @Autowired
- private AuthUtils authUtils;
- /**
- * @desc : 条件查询
- * @author : 付斌
- * @date : 2023/1/9 10:40
- */
- @Pagination
- public ResponseResultVO<PageList<RecPayResponse>> selectByCond(RecPayQuery recPayQuery) {
- return super.mergeListWithCount(recPayQuery, recPayMapper.selectByCond(recPayQuery),
- recPayMapper.countByCond(recPayQuery));
- }
- /**
- * @desc : 条件查询-客户详情的收款列表
- * @author : 姜永辉
- * @date : 2023/1/9 10:40
- */
- @Pagination
- public ResponseResultVO<PageList<RecPayResponse>> selectByCondCus(RecPayQuery recPayQuery) {
- return super.mergeListWithCount(recPayQuery, recPayMapper.selectByCondCus(recPayQuery),
- recPayMapper.countByCondCus(recPayQuery));
- }
- /**
- * @desc : 查询收款明细
- * @author : 付斌
- * @date : 2024-02-28 13:25
- */
- @Pagination
- public ResponseResultVO selectById(String id) {
- Map<String, Object> result = new HashMap<>();
- RecPayResponse recPayResponse = recPayMapper.selectById(id);
- result.put("data", recPayResponse);
- // 收款明细
- List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
- result.put("recPayItem", recPayItem);
- // 应收核销明细
- List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
- result.put("recPayHandleItem", recPayHandleItem);
- // 附件
- return ResponseResultUtil.success(result);
- }
- /**
- * @desc : 查询收款明细
- * @author : 付斌
- * @date : 2024-02-28 13:25
- */
- @Pagination
- public ResponseResultVO<Map<String, Object>> selectRpInfoById(String id) {
- Map<String, Object> result = new HashMap<>();
- // 收款明细
- List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
- result.put("recPayItem", recPayItem);
- // 应收核销明细
- List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
- result.put("recPayHandleItem", recPayHandleItem);
- // 附件
- return ResponseResultUtil.success(result);
- }
- /**
- * @desc : 新建客户收款
- * @author : 付斌
- * @date : 2023/1/9 10:49
- */
- @Transactional(
- rollbackFor = {Exception.class}
- )
- public ResponseResultVO<?> insertReceipt(RecPayVO recPayVO) {
- // 获取单号
- Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
- recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
- // 转化实体
- RecPay recPay = recPayConvert.convertToPo(recPayVO);
- // 客户的新增和更新跟进人
- // 如果没有客户id,要新建
- if (recPay.getObjectId() == null) {
- List<CustomerResponse> listCustomer = customerMapper.selectByCond(new CustomerQuery().setCpId(recPay.getCpId()).setCusPhone(recPayVO.getCusPhone()));
- // 如果客户电话已存在
- if (listCustomer == null || listCustomer.size() == 0) {
- String channelId;
- List<SaleChannelResponse> saleChannelResponses = saleChannelMapper.selectByCond(new SaleChannelQuery());
- channelId = (saleChannelResponses!=null && saleChannelResponses.size() > 0)?saleChannelResponses.get(0).getChannelId():"";
- // 创建客户,获取编码和主键UuId
- Map<String, Object> codeMapCus = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMER.getName(), true);
- Customer customer = new Customer();
- List<String> followStaffs = new ArrayList<>();
- followStaffs.add(authUtils.getStaff().getStaffId());
- customer.setCusId(codeMapCus.get("outId").toString()).setCusCode(codeMapCus.get("outNote").toString()).setCusName(recPayVO.getCusName())
- .setCusPhone(recPayVO.getCusPhone()).setAddressArea(recPayVO.getAddressArea()).setAddressName(recPayVO.getAddressName())
- .setAddressNo(recPayVO.getAddressNo()).setAddressGcj02(recPayVO.getAddressGcj02()).setAddressFull(recPayVO.getAddressFull())
- .setChannelId(channelId).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId()).setFollowStaffs(followStaffs)
- .setReportStaff(recPayVO.getStaffId()).setSaleStatus(Constant.SaleStatus.QIANKE.getName()).setCpId(recPayVO.getCpId());
- customerMapper.insert(customer);
- recPayVO.setObjectId(customer.getCusId());
- recPay.setObjectId(customer.getCusId());
- } else{
- recPayVO.setObjectId(listCustomer.get(0).getCusId());
- recPay.setObjectId(listCustomer.get(0).getCusId());
- }
- }else{ //如果有客户id 更新 客户信息
- Customer customer = new Customer();
- customer.setCusId(recPayVO.getObjectId()).setCusName(recPayVO.getCusName())
- .setCusPhone(recPayVO.getCusPhone()).setAddressArea(recPayVO.getAddressArea()).setAddressName(recPayVO.getAddressName())
- .setAddressNo(recPayVO.getAddressNo()).setAddressGcj02(recPayVO.getAddressGcj02()).setAddressFull(recPayVO.getAddressFull());
- //修改
- customerMapper.update(customer, new UpdateWrapper<Customer>().lambda().eq(Customer::getCusId, UUID.fromString(customer.getCusId())));
- }
- // 总单保存
- super.insert(recPay);
- // 明细保存
- if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
- for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
- RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
- recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
- recPayItemMapper.insert(recPayItem);
- // 插入账款明细
- AccountItem accountItem = new AccountItem();
- accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
- .setObjectId(recPayVO.getObjectId())
- .setOrgId(recPayVO.getOrgId())
- .setStaffId(recPayVO.getStaffId())
- .setAccDate(recPayVO.getAccDate())
- .setRecStatus(Constant.recStatuse.QUE_DING.getName())
- .setAmtRec(recPayItem.getAmtRec())
- .setBiznisType("t_mac_rec_pay_item")
- .setBiznisId(recPayItem.getRpId())
- .setBiznisNo(recPayVO.getRpNo())
- .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
- accountItemMapper.insert(accountItem);
- // 更新收款单上的账款明细Id
- RecPayItem recPayItemUpdate = new RecPayItem();
- recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
- recPayItemService.updateByUuid(recPayItemUpdate);
- // 插入资金流水
- MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
- moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
- .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
- .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
- moneyAccountItemMapper.insert(moneyAccountItem);
- // 更新资金账户
- MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
- MoneyAccount moneyAccountUpdate = new MoneyAccount();
- moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
- .setMacId(moneyAccountForUpdate.getMacId());
- moneyAccountService.updateByUuid(moneyAccountUpdate);
- }
- }
- // 插入账款总表
- Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
- // 更新账款总表上收款的相关字段
- Account accountUpdate = new Account();
- accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
- .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
- .setObjectId(accountForUpdate.getObjectId());
- accountService.updateByUuid(accountUpdate);
- return ResponseResultUtil.success();
- }
- /**
- * @desc : 新建客户退款
- * @author : 付斌
- * @date : 2023/1/9 10:49
- */
- @Transactional(
- rollbackFor = {Exception.class}
- )
- public ResponseResultVO<?> insertRefund(RecPayVO recPayVO) {
- // 查总账,看可退金额是否满足
- Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
- // if (accountForUpdate == null || accountForUpdate.getReceiptResidue().compareTo(recPayVO.getSumAmtRec().abs()) == -1) {
- // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
- // }
- // 获取单号
- Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
- recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
- // 转化实体
- RecPay recPay = recPayConvert.convertToPo(recPayVO);
- // 总单保存
- super.insert(recPay);
- // 明细保存
- if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
- for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
- RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
- recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
- recPayItemMapper.insert(recPayItem);
- // 插入账款明细
- AccountItem accountItem = new AccountItem();
- accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
- .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
- .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
- .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
- .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
- accountItemMapper.insert(accountItem);
- // 更新收款单上的账款明细Id
- RecPayItem recPayItemUpdate = new RecPayItem();
- recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
- recPayItemService.updateByUuid(recPayItemUpdate);
- // 插入资金流水
- MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
- moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
- .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
- .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
- moneyAccountItemMapper.insert(moneyAccountItem);
- // 更新资金账户
- MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
- MoneyAccount moneyAccountUpdate = new MoneyAccount();
- moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
- .setMacId(moneyAccountForUpdate.getMacId());
- // 如果账户不允许为负数
- if (!moneyAccountForUpdate.getFlgNegative()) {
- // 如果余额小于0 ,则提示余额不足
- if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
- }
- }
- moneyAccountService.updateByUuid(moneyAccountUpdate);
- }
- }
- // 更新账款总表上收款的相关字段
- Account accountUpdate = new Account();
- // 新建
- if(accountForUpdate == null ){
- // TODO 退货新客户插入的问题
- }else{
- // 说明已经存在
- accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
- .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
- .setObjectId(accountForUpdate.getObjectId());
- accountService.updateByUuid(accountUpdate);
- }
- return ResponseResultUtil.success();
- }
- /**
- * @desc : 编辑客户收款/退款
- * @author : 付斌
- * @date : 2023/1/9 10:49
- */
- @Transactional(
- rollbackFor = {Exception.class}
- )
- public ResponseResultVO<?> update(RecPayVO recPayVO) {
- RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(recPayVO.getRpId());
- // 并发校验
- if (!recPayForUpdate.getFlgValid()) {
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
- }
- // if(recPayForUpdate.getBiznisId() != null){
- // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
- // }
- // 将之前的明细全部删除
- List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayVO.getRpId());
- // 需要重新计算的资金账户
- List<String> macList = new ArrayList<>();
- for (RecPayItem recPayItem : recPayItemOriginalList) {
- // 删除收付款明细
- // recPayItemMapper.deleteById(recPayItem.getItemId());
- // 改为更新false
- RecPayItem recPayItemNew = new RecPayItem();
- recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
- recPayItemService.updateByUuid(recPayItemNew);
- // 删除账款明细
- accountItemMapper.deleteById(recPayItem.getAccItemId());
- // 删除账户流水
- moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
- if (!macList.contains(recPayItem.getMacId())) {
- macList.add(recPayItem.getMacId());
- }
- }
- // 新增明细
- for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
- RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
- recPayItem.setItemId(null).setRpId(recPayVO.getRpId()).setCpId(recPayVO.getCpId());
- recPayItemMapper.insert(recPayItem);
- // 插入账款明细
- AccountItem accountItem = new AccountItem();
- accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
- .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
- .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
- .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
- .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
- accountItemMapper.insert(accountItem);
- // 更新收款单上的账款明细Id
- RecPayItem recPayItemUpdate = new RecPayItem();
- recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
- recPayItemService.updateByUuid(recPayItemUpdate);
- // 插入资金流水
- MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
- moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
- .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
- .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
- moneyAccountItemMapper.insert(moneyAccountItem);
- if (!macList.contains(recPayItem.getMacId())) {
- macList.add(recPayItem.getMacId());
- }
- }
- // 更新总账表的总收款额和可用额
- accountService.updateReceipt(recPayVO.getObjectId());
- // 更新账户余额
- for (String macId : macList) {
- accountService.updateMac(macId);
- }
- // 更新收款单总表
- RecPay recPay = recPayConvert.convertToPo(recPayVO);
- return ResponseResultUtil.success(super.updateByUuid(recPay));
- }
- /**
- * @desc : 获取订单信息(编辑用)
- * @author : 付斌
- * @date : 2024-03-02 17:27
- */
- public ResponseResultVO<?> getRpForUpdate(String id) {
- Map<String, Object> dataInfo = new HashMap<>();
- RecPayResponse recPayResponse = recPayMapper.selectById(id);
- dataInfo.put("data", recPayResponse);
- // 收款明细
- List<RecPayItemResponse> recPayItemResponse = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
- dataInfo.put("dataItem", recPayItemResponse);
- return ResponseResultUtil.success(dataInfo);
- }
- /**
- * @desc : 新建应收核销(收款+核销应收)
- * @author : 付斌
- * @date : 2023/1/9 10:49
- */
- @Transactional(
- rollbackFor = {Exception.class}
- )
- public ResponseResultVO<?> insertReceivableReceipt(RecPayVO recPayVO) {
- /********************* 收款的处理 begin **********************/
- // 获取单号
- Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
- recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
- // 转化实体
- RecPay recPay = recPayConvert.convertToPo(recPayVO);
- // 总单保存
- super.insert(recPay);
- // 明细保存
- if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
- for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
- RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
- recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
- recPayItemMapper.insert(recPayItem);
- // 插入账款明细
- AccountItem accountItem = new AccountItem();
- accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
- .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
- .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
- .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
- .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
- accountItemMapper.insert(accountItem);
- // 更新收款单上的账款明细Id
- RecPayItem recPayItemUpdate = new RecPayItem();
- recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
- recPayItemService.updateByUuid(recPayItemUpdate);
- // 插入资金流水
- MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
- moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
- .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
- .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
- moneyAccountItemMapper.insert(moneyAccountItem);
- // 更新资金账户
- MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
- MoneyAccount moneyAccountUpdate = new MoneyAccount();
- moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
- .setMacId(moneyAccountForUpdate.getMacId());
- // 如果账户不允许为负数
- if (!moneyAccountForUpdate.getFlgNegative()) {
- // 如果余额小于0 ,则提示余额不足
- if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
- }
- }
- moneyAccountService.updateByUuid(moneyAccountUpdate);
- }
- }
- // 插入账款总表
- Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
- // 更新账款总表上收款的相关字段
- Account accountUpdate = new Account();
- accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
- .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
- .setObjectId(accountForUpdate.getObjectId());
- accountService.updateByUuid(accountUpdate);
- /********************* 收款的处理 end **********************/
- /********************* 应收收款的处理 begin **********************/
- // 应收收款的处理
- if (recPayVO.getReceivableList() != null && recPayVO.getReceivableList().size() > 0) {
- for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getReceivableList()) {
- RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
- recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
- recPayHandleItemMapper.insert(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()))
- .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
- .setItemId(recPayHandleItem.getAccItemId());
- // 剩余金额 = 应收金额-应收收款金额
- accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
- accountItemService.updateByUuid(accountItemUpdate);
- }
- // 计算明细的核销金额,优惠金额合计
- RecPayHandleItemVO recPayHandleItemVO = recPayVO.getReceivableList().stream().reduce((x, y) -> {
- RecPayHandleItemVO item = new RecPayHandleItemVO();
- item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
- item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
- return item;
- }).get();
- // 更新总账上
- accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
- accountUpdate = new Account();
- accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItemVO.getAmtReceivableHandle()))// 总应收收款金额
- .setReceivableWaive(accountForUpdate.getReceivableWaive().add(recPayHandleItemVO.getAmtWaive()))// 总应收优惠金额
- .setObjectId(accountForUpdate.getObjectId());
- // 剩余应收 = 总应收账款-总应收收款金额
- accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
- // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
- accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
- // 更新前的最后校验
- // 剩余应收为负数,则不能保存
- // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
- // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
- // }
- // 可用金额为负数,则不能保存
- if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
- }
- accountService.updateByUuid(accountUpdate);
- }
- /********************* 应收收款的处理 end **********************/
- return ResponseResultUtil.success();
- }
- /**
- * @desc : 更新应收核销(小编辑)
- * @author : 付斌
- * @date : 2023/1/9 10:49
- */
- @Transactional(
- rollbackFor = {Exception.class}
- )
- public ResponseResultVO<?> updateReceivableReceipt(RecPayVO recPayVO) {
- // 先只改备注和附件
- RecPay recPay = new RecPay();
- recPay.setRemarks(recPayVO.getRemarks()).setAnnexPaths(recPayVO.getAnnexPaths()).setRpId(recPayVO.getRpId());
- super.updateByUuid(recPay);
- return ResponseResultUtil.success();
- }
- /**
- * @desc : 作废
- * @author : 付斌
- * @date : 2024-03-08 16:38
- */
- public ResponseResultVO<?> invalid(String id) {
- RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
- // 并发校验
- if (!recPayForUpdate.getFlgValid()) {
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
- }
- // 240624 问完付斌后
- // if(recPayForUpdate.getBiznisId() != null){
- // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
- // }
- // 如果所在月份已结账,则不能作废 todo
- // 查出并锁定所有应收核销明细
- AccountItem accountItemForUpdate;
- List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
- for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
- // 更新账款明细应收收款
- accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
- AccountItem accountItemUpdate = new AccountItem();
- accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtReceivableHandle()))
- .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
- .setItemId(recPayHandleItemForUpdate.getAccItemId());
- accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
- accountItemService.updateByUuid(accountItemUpdate);
- // 将核销明细有效标识置为false
- RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
- recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
- recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
- }
- // 把总帐上的钱加回来
- Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
- Account accountUpdate = new Account();
- accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayForUpdate.getSumAmtReceivableHandle()))// 总应收收款金额
- .setReceivableWaive(accountForUpdate.getReceivableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应收优惠金额
- .setObjectId(accountForUpdate.getObjectId());
- // 剩余应收 = 总应收账款-总应收收款金额
- accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
- // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
- accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
- // 更新前的最后校验
- // 剩余应收为负数,则不能保存
- // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
- // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
- // }
- // 可用金额为负数,则不能保存
- // if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
- // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
- // }
- accountService.updateByUuid(accountUpdate);
- // 将之前的明细全部删除
- List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
- // 需要重新计算的资金账户
- List<String> macList = new ArrayList<>();
- for (RecPayItem recPayItem : recPayItemOriginalList) {
- // 删除收付款明细
- // recPayItemMapper.deleteById(recPayItem.getItemId());
- // 改为更新false
- RecPayItem recPayItemNew = new RecPayItem();
- recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
- recPayItemService.updateByUuid(recPayItemNew);
- // 删除账款明细
- accountItemMapper.deleteById(recPayItem.getAccItemId());
- // 删除账户流水
- moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
- if (!macList.contains(recPayItem.getMacId())) {
- macList.add(recPayItem.getMacId());
- }
- }
- // 更新总账表的总收款额和可用额
- accountService.updateReceipt(recPayForUpdate.getObjectId());
- // 更新账户余额
- for (String macId : macList) {
- accountService.updateMac(macId);
- }
- // 作废
- RecPay recPayUpdate = new RecPay();
- recPayUpdate.setFlgValid(false).setRpId(id);
- super.updateByUuid(recPayUpdate);
- return ResponseResultUtil.success();
- }
- /**
- * @desc : 获取应收收款(编辑用)
- * @author : 付斌
- * @date : 2024-02-28 13:25
- */
- @Pagination
- public ResponseResultVO getReceivableReceiptForUpdate(String id) {
- Map<String, Object> result = new HashMap<>();
- RecPayResponse recPayResponse = recPayMapper.selectById(id);
- result.put("data", recPayResponse);
- // 收款明细
- List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
- result.put("dataItem", recPayItem);
- // 应收核销明细
- List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
- result.put("recPayHandleItem", recPayHandleItem);
- // 附件
- return ResponseResultUtil.success(result);
- }
- /**
- * @desc : 供应商收退款查询
- * @author : 于继渤
- * @date : 2024/7/5 10:36
- */
- @Pagination
- public ResponseResultVO<PageList<RecPayResponse>> getRpSupList(RecPayQuery recPayQuery) {
- return super.mergeListWithCount(recPayQuery, recPayMapper.getRpSupList(recPayQuery),
- recPayMapper.getRpSupListCount(recPayQuery));
- }
- /**
- * @desc : 供应商收退款查询
- * @author : 于继渤
- * @date : 2024/7/5 10:36
- */
- @Pagination
- public ResponseResultVO<PageList<RecPayResponse>> getRpSupListDetail(RecPayQuery recPayQuery) {
- return super.mergeListWithCount(recPayQuery, recPayMapper.getRpSupListDetail(recPayQuery),
- recPayMapper.getRpSupListCountDetail(recPayQuery));
- }
- /**
- * @desc : 新建供应商收款
- * @author : 于继渤
- * @date : 2024/7/5 10:48
- */
- @Transactional(
- rollbackFor = {Exception.class}
- )
- public ResponseResultVO<?> insertSupReceipt(RecPayVO recPayVO) {
- // 获取单号
- Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
- recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
- // 转化实体
- RecPay recPay = recPayConvert.convertToPo(recPayVO);
- // 总单保存
- super.insert(recPay);
- // 明细保存
- if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
- for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
- RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
- recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
- recPayItemMapper.insert(recPayItem);
- // 插入账款明细
- AccountItem accountItem = new AccountItem();
- accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
- .setObjectId(recPayVO.getObjectId())
- .setOrgId(recPayVO.getOrgId())
- .setStaffId(recPayVO.getStaffId())
- .setAccDate(recPayVO.getAccDate())
- .setRecStatus(Constant.recStatuse.QUE_DING.getName())
- .setAmtPay(recPayItem.getAmtPay())
- .setBiznisType("t_mac_rec_pay_item")
- .setBiznisId(recPayItem.getRpId())
- .setBiznisNo(recPayVO.getRpNo())
- .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
- accountItemMapper.insert(accountItem);
- // 更新收款单上的账款明细Id
- RecPayItem recPayItemUpdate = new RecPayItem();
- recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
- recPayItemService.updateByUuid(recPayItemUpdate);
- // 插入资金流水
- MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
- moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
- .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtPay().negate()).setAccDate(recPayVO.getAccDate())
- .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
- moneyAccountItemMapper.insert(moneyAccountItem);
- // 更新资金账户
- MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
- MoneyAccount moneyAccountUpdate = new MoneyAccount();
- moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtPay()))
- .setMacId(moneyAccountForUpdate.getMacId());
- moneyAccountService.updateByUuid(moneyAccountUpdate);
- }
- }
- // 插入账款总表
- Account accountForUpdate = accountService.getSupAccountForUpdate(recPayVO.getObjectId());
- // 更新账款总表上收款的相关字段
- Account accountUpdate = new Account();
- accountUpdate.setPayment(accountForUpdate.getPayment().add(recPayVO.getSumAmtPay()))// 总收款金额
- .setPaymentResidue(accountForUpdate.getPaymentResidue().add(recPayVO.getSumAmtPay()))// 可退金额
- .setObjectId(accountForUpdate.getObjectId());
- accountService.updateByUuid(accountUpdate);
- return ResponseResultUtil.success();
- }
- /**
- * @desc : 新建供应商退款
- * @author : 于继渤
- * @date : 2024/7/5 10:48
- */
- @Transactional(
- rollbackFor = {Exception.class}
- )
- public ResponseResultVO<?> insertSupRefund(RecPayVO recPayVO) {
- // 查总账,看可退金额是否满足
- Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
- // 获取单号
- Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
- recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
- // 转化实体
- RecPay recPay = recPayConvert.convertToPo(recPayVO);
- // 总单保存
- super.insert(recPay);
- // 明细保存
- if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
- for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
- RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
- recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
- recPayItemMapper.insert(recPayItem);
- // 插入账款明细
- AccountItem accountItem = new AccountItem();
- accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
- .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
- .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtPay(recPayItem.getAmtPay())
- .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
- .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
- accountItemMapper.insert(accountItem);
- // 更新收款单上的账款明细Id
- RecPayItem recPayItemUpdate = new RecPayItem();
- recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
- recPayItemService.updateByUuid(recPayItemUpdate);
- // 插入资金流水
- MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
- moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
- .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtPay().negate()).setAccDate(recPayVO.getAccDate())
- .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
- moneyAccountItemMapper.insert(moneyAccountItem);
- // 更新资金账户
- MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
- MoneyAccount moneyAccountUpdate = new MoneyAccount();
- moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtPay()))
- .setMacId(moneyAccountForUpdate.getMacId());
- // 如果账户不允许为负数
- if (!moneyAccountForUpdate.getFlgNegative()) {
- // 如果余额小于0 ,则提示余额不足
- if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
- }
- }
- moneyAccountService.updateByUuid(moneyAccountUpdate);
- }
- }
- // 更新账款总表上收款的相关字段
- Account accountUpdate = new Account();
- // 新建
- if(accountForUpdate == null ){
- // TODO 退货新客户插入的问题
- }else{
- // 说明已经存在
- accountUpdate.setPayment(accountForUpdate.getPayment().add(recPayVO.getSumAmtPay()))// 总收款金额
- .setPaymentResidue(accountForUpdate.getPaymentResidue().add(recPayVO.getSumAmtPay()))// 可退金额
- .setObjectId(accountForUpdate.getObjectId());
- accountService.updateByUuid(accountUpdate);
- }
- return ResponseResultUtil.success();
- }
- /**
- * @desc : 编辑供应商收款/退款
- * @author : 于继渤
- * @date : 2023/1/9 10:49
- */
- @Transactional(
- rollbackFor = {Exception.class}
- )
- public ResponseResultVO<?> updateSupReceipt(RecPayVO recPayVO) {
- RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(recPayVO.getRpId());
- // 并发校验
- if (!recPayForUpdate.getFlgValid()) {
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
- }
- // 将之前的明细全部删除
- List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayVO.getRpId());
- // 需要重新计算的资金账户
- List<String> macList = new ArrayList<>();
- for (RecPayItem recPayItem : recPayItemOriginalList) {
- // 删除收付款明细
- // recPayItemMapper.deleteById(recPayItem.getItemId());
- // 改为更新false
- RecPayItem recPayItemNew = new RecPayItem();
- recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
- recPayItemService.updateByUuid(recPayItemNew);
- // 删除账款明细
- accountItemMapper.deleteById(recPayItem.getAccItemId());
- // 删除账户流水
- moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
- if (!macList.contains(recPayItem.getMacId())) {
- macList.add(recPayItem.getMacId());
- }
- }
- // 新增明细
- for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
- RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
- recPayItem.setItemId(null).setRpId(recPayVO.getRpId()).setCpId(recPayVO.getCpId());
- recPayItemMapper.insert(recPayItem);
- // 插入账款明细
- AccountItem accountItem = new AccountItem();
- accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
- .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
- .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtPay(recPayItem.getAmtPay())
- .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
- .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
- accountItemMapper.insert(accountItem);
- // 更新收款单上的账款明细Id
- RecPayItem recPayItemUpdate = new RecPayItem();
- recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
- recPayItemService.updateByUuid(recPayItemUpdate);
- // 插入资金流水
- MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
- moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
- .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtPay()).setAccDate(recPayVO.getAccDate())
- .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
- moneyAccountItemMapper.insert(moneyAccountItem);
- if (!macList.contains(recPayItem.getMacId())) {
- macList.add(recPayItem.getMacId());
- }
- }
- // 更新总账表的总收款额和可用额
- accountService.updatePayment(recPayVO.getObjectId());
- // 更新账户余额
- for (String macId : macList) {
- accountService.updateMac(macId);
- }
- // 更新收款单总表
- RecPay recPay = recPayConvert.convertToPo(recPayVO);
- return ResponseResultUtil.success(super.updateByUuid(recPay));
- }
- /**
- * @desc : 作废
- * @author : 于继渤
- * @date : 2024-03-08 16:38
- */
- public ResponseResultVO<?> invalidSup(String id) {
- RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
- // 并发校验
- if (!recPayForUpdate.getFlgValid()) {
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
- }
- // 240624 问完付斌后
- // if(recPayForUpdate.getBiznisId() != null){
- // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
- // }
- // 如果所在月份已结账,则不能作废 todo
- // 查出并锁定所有应收核销明细
- AccountItem accountItemForUpdate;
- List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
- for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
- // 更新账款明细应收收款
- accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
- AccountItem accountItemUpdate = new AccountItem();
- accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtPayableHandle()))
- .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
- .setItemId(recPayHandleItemForUpdate.getAccItemId());
- accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
- accountItemService.updateByUuid(accountItemUpdate);
- // 将核销明细有效标识置为false
- RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
- recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
- recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
- }
- // 把总帐上的钱加回来
- Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
- Account accountUpdate = new Account();
- accountUpdate.setPayableHandle(accountForUpdate.getPayableHandle().subtract(recPayForUpdate.getSumAmtPayableHandle()))// 总应收收款金额
- .setPayableWaive(accountForUpdate.getPayableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应收优惠金额
- .setObjectId(accountForUpdate.getObjectId());
- // 剩余应收 = 总应收账款-总应收收款金额
- accountUpdate.setPaymentResidue(accountForUpdate.getPayable().subtract(accountUpdate.getPayableHandle()));
- // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
- accountUpdate.setPaymentResidue(accountForUpdate.getPayment().subtract(accountUpdate.getPayableHandle()).add(accountUpdate.getPayableWaive()));
- // 更新前的最后校验
- // 剩余应收为负数,则不能保存
- // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
- // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
- // }
- // 可用金额为负数,则不能保存
- if (accountUpdate.getPaymentResidue().compareTo(BigDecimal.ZERO) == -1) {
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
- }
- accountService.updateByUuid(accountUpdate);
- // 将之前的明细全部删除
- List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
- // 需要重新计算的资金账户
- List<String> macList = new ArrayList<>();
- for (RecPayItem recPayItem : recPayItemOriginalList) {
- // 删除收付款明细
- // recPayItemMapper.deleteById(recPayItem.getItemId());
- // 改为更新false
- RecPayItem recPayItemNew = new RecPayItem();
- recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
- recPayItemService.updateByUuid(recPayItemNew);
- // 删除账款明细
- accountItemMapper.deleteById(recPayItem.getAccItemId());
- // 删除账户流水
- moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
- if (!macList.contains(recPayItem.getMacId())) {
- macList.add(recPayItem.getMacId());
- }
- }
- // 更新总账表的总收款额和可用额
- accountService.updatePayment(recPayForUpdate.getObjectId());
- // 更新账户余额
- for (String macId : macList) {
- accountService.updateMac(macId);
- }
- // 作废
- RecPay recPayUpdate = new RecPay();
- recPayUpdate.setFlgValid(false).setRpId(id);
- super.updateByUuid(recPayUpdate);
- return ResponseResultUtil.success();
- }
- }
|