| | |
| | | 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.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.receipt.ErpFinanceReceiptPageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.receipt.ErpFinanceReceiptSaveReqVO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinanceReceiptDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinanceReceiptItemDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.sale.ErpSaleOutDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.sale.ErpSaleReturnDO; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinanceReceiptItemMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinanceReceiptMapper; |
| | | 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.sale.ErpCustomerService; |
| | | import cn.iocoder.yudao.module.erp.service.sale.ErpSaleOutService; |
| | | import cn.iocoder.yudao.module.erp.service.sale.ErpSaleReturnService; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | 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 ErpNoRedisDAO noRedisDAO; |
| | | |
| | | @Resource |
| | | private ErpCustomerService customerService; |
| | | private CrmCustomerApi customerApi; |
| | | @Resource |
| | | private ErpAccountService accountService; |
| | | @Resource |
| | | private ErpSaleOutService saleOutService; |
| | | @Resource |
| | | private ErpSaleReturnService saleReturnService; |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | |
| | | // 1.1 校验订单项的有效性 |
| | | List<ErpFinanceReceiptItemDO> receiptItems = validateFinanceReceiptItems( |
| | | createReqVO.getCustomerId(), createReqVO.getItems()); |
| | | // 1.2 校验客户 |
| | | customerService.validateCustomer(createReqVO.getCustomerId()); |
| | | // 1.2 校验客户(使用 CRM 客户 API) |
| | | customerApi.validateCustomer(createReqVO.getCustomerId()); |
| | | // 1.3 校验结算账户 |
| | | if (createReqVO.getAccountId() != null) { |
| | | accountService.validateAccount(createReqVO.getAccountId()); |
| | |
| | | if (ErpAuditStatus.APPROVE.getStatus().equals(receipt.getStatus())) { |
| | | throw exception(FINANCE_RECEIPT_UPDATE_FAIL_APPROVE, receipt.getNo()); |
| | | } |
| | | // 1.2 校验客户 |
| | | customerService.validateCustomer(updateReqVO.getCustomerId()); |
| | | // 1.2 校验客户(使用 CRM 客户 API) |
| | | customerApi.validateCustomer(updateReqVO.getCustomerId()); |
| | | // 1.3 校验结算账户 |
| | | if (updateReqVO.getAccountId() != null) { |
| | | accountService.validateAccount(updateReqVO.getAccountId()); |
| | |
| | | private List<ErpFinanceReceiptItemDO> validateFinanceReceiptItems( |
| | | Long customerId, |
| | | List<ErpFinanceReceiptSaveReqVO.Item> list) { |
| | | // 销售出库/退货功能已移除,暂时只支持手动录入收款明细 |
| | | return convertList(list, o -> BeanUtils.toBean(o, ErpFinanceReceiptItemDO.class, item -> { |
| | | if (ObjectUtil.equal(item.getBizType(), ErpBizTypeEnum.SALE_OUT.getType())) { |
| | | ErpSaleOutDO saleOut = saleOutService.validateSaleOut(item.getBizId()); |
| | | Assert.equals(saleOut.getCustomerId(), customerId, "客户必须相同"); |
| | | item.setTotalPrice(saleOut.getTotalPrice()).setBizNo(saleOut.getNo()); |
| | | } else if (ObjectUtil.equal(item.getBizType(), ErpBizTypeEnum.SALE_RETURN.getType())) { |
| | | ErpSaleReturnDO saleReturn = saleReturnService.validateSaleReturn(item.getBizId()); |
| | | Assert.equals(saleReturn.getCustomerId(), customerId, "客户必须相同"); |
| | | item.setTotalPrice(saleReturn.getTotalPrice().negate()).setBizNo(saleReturn.getNo()); |
| | | } else { |
| | | throw new IllegalArgumentException("业务类型不正确:" + item.getBizType()); |
| | | // 业务类型校验已移除,直接使用传入的数据 |
| | | if (item.getReceiptPrice() == null) { |
| | | item.setReceiptPrice(BigDecimal.ZERO); |
| | | } |
| | | if (item.getTotalPrice() == null) { |
| | | item.setTotalPrice(BigDecimal.ZERO); |
| | | } |
| | | })); |
| | | } |
| | |
| | | private void updateFinanceReceiptItemList(Long id, List<ErpFinanceReceiptItemDO> newList) { |
| | | // 第一步,对比新老数据,获得添加、修改、删除的列表 |
| | | List<ErpFinanceReceiptItemDO> oldList = financeReceiptItemMapper.selectListByReceiptId(id); |
| | | List<List<ErpFinanceReceiptItemDO>> diffList = diffList(oldList, newList, // id 不同,就认为是不同的记录 |
| | | List<List<ErpFinanceReceiptItemDO>> diffList = diffList(oldList, newList, |
| | | (oldVal, newVal) -> oldVal.getId().equals(newVal.getId())); |
| | | |
| | | // 第二步,批量添加、修改、删除 |
| | |
| | | if (CollUtil.isNotEmpty(diffList.get(2))) { |
| | | financeReceiptItemMapper.deleteByIds(convertList(diffList.get(2), ErpFinanceReceiptItemDO::getId)); |
| | | } |
| | | |
| | | // 第三步,更新销售出库、退货的收款金额情况 |
| | | updateSalePrice(CollectionUtils.newArrayList(diffList)); |
| | | } |
| | | |
| | | private void updateSalePrice(List<ErpFinanceReceiptItemDO> receiptItems) { |
| | | receiptItems.forEach(receiptItem -> { |
| | | BigDecimal totalReceiptPrice = financeReceiptItemMapper.selectReceiptPriceSumByBizIdAndBizType( |
| | | receiptItem.getBizId(), receiptItem.getBizType()); |
| | | if (ErpBizTypeEnum.SALE_OUT.getType().equals(receiptItem.getBizType())) { |
| | | saleOutService.updateSaleInReceiptPrice(receiptItem.getBizId(), totalReceiptPrice); |
| | | } else if (ErpBizTypeEnum.SALE_RETURN.getType().equals(receiptItem.getBizType())) { |
| | | saleReturnService.updateSaleReturnRefundPrice(receiptItem.getBizId(), totalReceiptPrice.negate()); |
| | | } else { |
| | | throw new IllegalArgumentException("业务类型不正确:" + receiptItem.getBizType()); |
| | | } |
| | | }); |
| | | // 销售出库/退货功能已移除,不再更新关联业务金额 |
| | | } |
| | | |
| | | @Override |