| | |
| | | package cn.iocoder.yudao.module.erp.service.finance; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.lang.Assert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.payment.ErpFinancePaymentPageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.payment.ErpFinancePaymentSaveReqVO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentItemDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseReturnDO; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinancePaymentItemMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinancePaymentMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpAuditStatus; |
| | | import cn.iocoder.yudao.module.erp.enums.common.ErpBizTypeEnum; |
| | | import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchaseInService; |
| | | import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchaseReturnService; |
| | | import cn.iocoder.yudao.module.erp.service.purchase.ErpSupplierService; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import jakarta.annotation.Resource; |
| | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; |
| | | import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*; |
| | | |
| | | // TODO 芋艿:记录操作日志 |
| | | |
| | | /** |
| | | * ERP 付款单 Service 实现类 |
| | |
| | | private ErpSupplierService supplierService; |
| | | @Resource |
| | | private ErpAccountService accountService; |
| | | @Resource |
| | | private ErpPurchaseInService purchaseInService; |
| | | @Resource |
| | | private ErpPurchaseReturnService purchaseReturnService; |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | |
| | | private List<ErpFinancePaymentItemDO> validateFinancePaymentItems( |
| | | Long supplierId, |
| | | List<ErpFinancePaymentSaveReqVO.Item> list) { |
| | | // 采购入库/退货功能已移除,暂时只支持手动录入付款明细 |
| | | return convertList(list, o -> BeanUtils.toBean(o, ErpFinancePaymentItemDO.class, item -> { |
| | | if (ObjectUtil.equal(item.getBizType(), ErpBizTypeEnum.PURCHASE_IN.getType())) { |
| | | ErpPurchaseInDO purchaseIn = purchaseInService.validatePurchaseIn(item.getBizId()); |
| | | Assert.equals(purchaseIn.getSupplierId(), supplierId, "供应商必须相同"); |
| | | item.setTotalPrice(purchaseIn.getTotalPrice()).setBizNo(purchaseIn.getNo()); |
| | | } else if (ObjectUtil.equal(item.getBizType(), ErpBizTypeEnum.PURCHASE_RETURN.getType())) { |
| | | ErpPurchaseReturnDO purchaseReturn = purchaseReturnService.validatePurchaseReturn(item.getBizId()); |
| | | Assert.equals(purchaseReturn.getSupplierId(), supplierId, "供应商必须相同"); |
| | | item.setTotalPrice(purchaseReturn.getTotalPrice().negate()).setBizNo(purchaseReturn.getNo()); |
| | | } else { |
| | | throw new IllegalArgumentException("业务类型不正确:" + item.getBizType()); |
| | | if (item.getPaymentPrice() == null) { |
| | | item.setPaymentPrice(BigDecimal.ZERO); |
| | | } |
| | | if (item.getTotalPrice() == null) { |
| | | item.setTotalPrice(BigDecimal.ZERO); |
| | | } |
| | | })); |
| | | } |
| | | |
| | | private void updateFinancePaymentItemList(Long id, List<ErpFinancePaymentItemDO> newList) { |
| | | // 第一步,对比新老数据,获得添加、修改、删除的列表 |
| | | List<ErpFinancePaymentItemDO> oldList = financePaymentItemMapper.selectListByPaymentId(id); |
| | | List<List<ErpFinancePaymentItemDO>> diffList = diffList(oldList, newList, // id 不同,就认为是不同的记录 |
| | | List<List<ErpFinancePaymentItemDO>> diffList = diffList(oldList, newList, |
| | | (oldVal, newVal) -> oldVal.getId().equals(newVal.getId())); |
| | | |
| | | // 第二步,批量添加、修改、删除 |
| | | if (CollUtil.isNotEmpty(diffList.get(0))) { |
| | | diffList.get(0).forEach(o -> o.setPaymentId(id)); |
| | | financePaymentItemMapper.insertBatch(diffList.get(0)); |
| | |
| | | if (CollUtil.isNotEmpty(diffList.get(2))) { |
| | | financePaymentItemMapper.deleteByIds(convertList(diffList.get(2), ErpFinancePaymentItemDO::getId)); |
| | | } |
| | | |
| | | // 第三步,更新采购入库、退货的付款金额情况 |
| | | updatePurchasePrice(CollectionUtils.newArrayList(diffList)); |
| | | } |
| | | |
| | | private void updatePurchasePrice(List<ErpFinancePaymentItemDO> paymentItems) { |
| | | paymentItems.forEach(paymentItem -> { |
| | | BigDecimal totalPaymentPrice = financePaymentItemMapper.selectPaymentPriceSumByBizIdAndBizType( |
| | | paymentItem.getBizId(), paymentItem.getBizType()); |
| | | if (ErpBizTypeEnum.PURCHASE_IN.getType().equals(paymentItem.getBizType())) { |
| | | purchaseInService.updatePurchaseInPaymentPrice(paymentItem.getBizId(), totalPaymentPrice); |
| | | } else if (ErpBizTypeEnum.PURCHASE_RETURN.getType().equals(paymentItem.getBizType())) { |
| | | purchaseReturnService.updatePurchaseReturnRefundPrice(paymentItem.getBizId(), totalPaymentPrice.negate()); |
| | | } else { |
| | | throw new IllegalArgumentException("业务类型不正确:" + paymentItem.getBizType()); |
| | | } |
| | | }); |
| | | // 采购入库/退货功能已移除,不再更新关联业务金额 |
| | | } |
| | | |
| | | @Override |