|
@@ -1,25 +1,41 @@
|
|
|
package com.sd.business.service.purchase.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.flow.enums.FlowStatusEnum;
|
|
|
import com.ruoyi.common.constant.StatusConstant;
|
|
|
+import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.sd.business.entity.bom.bo.BomSpecBo;
|
|
|
+import com.sd.business.entity.bom.po.BomSpec;
|
|
|
import com.sd.business.entity.in.po.InOutStorageBom;
|
|
|
+import com.sd.business.entity.purchase.dto.PurchaseBomImportDataDto;
|
|
|
import com.sd.business.entity.purchase.dto.PurchaseDto;
|
|
|
import com.sd.business.entity.purchase.dto.PurchaseSelectDto;
|
|
|
import com.sd.business.entity.purchase.enums.PurchaseStatusEnum;
|
|
|
import com.sd.business.entity.purchase.po.Purchase;
|
|
|
import com.sd.business.entity.purchase.po.PurchaseBom;
|
|
|
import com.sd.business.entity.purchase.vo.PurchaseVo;
|
|
|
+import com.sd.business.entity.supplier.po.Supplier;
|
|
|
import com.sd.business.mapper.purchase.PurchaseMapper;
|
|
|
+import com.sd.business.service.bom.BomSpecService;
|
|
|
import com.sd.business.service.purchase.PurchaseBomService;
|
|
|
import com.sd.business.service.purchase.PurchaseService;
|
|
|
+import com.sd.business.service.sku.SkuSpecService;
|
|
|
+import com.sd.business.service.supplier.SupplierService;
|
|
|
+import com.sd.business.util.CodeEnum;
|
|
|
import com.sd.framework.util.Assert;
|
|
|
import com.sd.framework.util.sql.Sql;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.List;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -36,6 +52,16 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i
|
|
|
@Autowired
|
|
|
private PurchaseBomService purchaseBomService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private BomSpecService bomSpecService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SkuSpecService skuSpecService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SupplierService supplierService;
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public Page<PurchaseVo> getPage(PurchaseSelectDto dto) {
|
|
|
|
|
@@ -111,4 +137,134 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void purchaseTermination(Long id) {
|
|
|
+ Purchase purchase = this.getById(id);
|
|
|
+ if (purchase == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ purchase.setStatus(PurchaseStatusEnum.TERMINATION.getKey());
|
|
|
+ purchase.setStorageStatus(StatusConstant.YES);
|
|
|
+ this.updateById(purchase);
|
|
|
+
|
|
|
+ // 合同终止将bom到货数量设置为采购数量
|
|
|
+ List<PurchaseBom> purchaseBomList = purchaseBomService.list(q -> q
|
|
|
+ .eq(PurchaseBom::getPurchaseId, id)
|
|
|
+ .apply("(purchase_quantity + return_quantity) > arrival_quantity"));
|
|
|
+ for (PurchaseBom purchaseBom : purchaseBomList) {
|
|
|
+ purchaseBom.setArrivalQuantity(purchaseBom.getPurchaseQuantity().add(purchaseBom.getReturnQuantity()));
|
|
|
+ }
|
|
|
+ purchaseBomService.updateBatchById(purchaseBomList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void purchaseImport(List<PurchaseBomImportDataDto> list) {
|
|
|
+
|
|
|
+ // 验证导入数据
|
|
|
+ validatedImportData(list);
|
|
|
+
|
|
|
+ Map<String, List<PurchaseBomImportDataDto>> bomMap = list.stream().collect(Collectors.groupingBy(PurchaseBomImportDataDto::getPurchaseCode));
|
|
|
+ Map<String, Long> purchaseMap = this.mapKV(Purchase::getErpCode, BaseIdPo::getId, q -> q.in(Purchase::getErpCode, bomMap.keySet()));
|
|
|
+
|
|
|
+ Set<String> supplierCodes = list.stream().map(PurchaseBomImportDataDto::getSupplierCode).collect(Collectors.toSet());
|
|
|
+ Map<String, Long> supplierMap = supplierService.mapKV(Supplier::getCode, BaseIdPo::getId, q -> q.in(Supplier::getCode, supplierCodes));
|
|
|
+
|
|
|
+ List<String> bomSpecCodes = list.stream().map(PurchaseBomImportDataDto::getBomSpecCode).collect(Collectors.toList());
|
|
|
+ Map<String, BomSpec> bomSpecMap = bomSpecService.mapKEntity(BomSpec::getCode, q -> q.in(BomSpec::getCode, bomSpecCodes));
|
|
|
+
|
|
|
+ List<Purchase> purchaseList = new ArrayList<>();
|
|
|
+ List<PurchaseBom> purchaseBomList = new ArrayList<>();
|
|
|
+ List<BomSpec> bomSpecList = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, List<PurchaseBomImportDataDto>> entry : bomMap.entrySet()) {
|
|
|
+ String purchaseCode = entry.getKey();
|
|
|
+ List<PurchaseBomImportDataDto> bomList = entry.getValue();
|
|
|
+ if (ObjectUtil.isEmpty(bomList)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断是否存在重复编号
|
|
|
+ Long purchaseId = purchaseMap.get(purchaseCode);
|
|
|
+ if (purchaseId != null) {
|
|
|
+ throw new ServiceException("采购合同编号:" + purchaseCode + " 已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long supplierId = supplierMap.get(bomList.get(0).getSupplierCode());
|
|
|
+ if (supplierId == null) {
|
|
|
+ throw new ServiceException("未知供应商编号:" + bomList.get(0).getSupplierCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存采购合同
|
|
|
+ Purchase purchase = new Purchase();
|
|
|
+ purchase.setId(IdWorker.getId());
|
|
|
+ purchase.setCode(CodeEnum.PURCHASE_CODE.getCode());
|
|
|
+ purchase.setSupplierId(supplierId);
|
|
|
+ purchase.setStatus(PurchaseStatusEnum.UNDER_PURCHASE.getKey());
|
|
|
+ purchase.setFlowStatus(FlowStatusEnum.PASS.getKey());
|
|
|
+ purchase.setErpCode(purchaseCode);
|
|
|
+ purchase.setReceiveGoodsType(1);
|
|
|
+ purchase.setAdvancePayment(BigDecimal.ZERO);
|
|
|
+ purchase.setReturnAmount(BigDecimal.ZERO);
|
|
|
+ purchase.setClosedAccountAmount(BigDecimal.ZERO);
|
|
|
+ purchase.setDeductibleAmount(BigDecimal.ZERO);
|
|
|
+ purchase.setPaymentStatus(StatusConstant.NO);
|
|
|
+ purchase.setStorageStatus(StatusConstant.NO);
|
|
|
+ purchase.setTotalAmountIncludingTax(BigDecimal.ZERO);
|
|
|
+ purchase.setTotalAmountExcludingTax(BigDecimal.ZERO);
|
|
|
+ purchase.setDeliveryDate(bomList.get(0).getDeliveryDate());
|
|
|
+
|
|
|
+
|
|
|
+ List<PurchaseBom> bomData = bomList.stream().map(item -> {
|
|
|
+ BomSpec bomSpec = bomSpecMap.get(item.getBomSpecCode());
|
|
|
+ if (bomSpec == null) {
|
|
|
+ throw new ServiceException("未知bom品号:" + item.getBomSpecCode());
|
|
|
+ }
|
|
|
+ PurchaseBom purchaseBom = new PurchaseBom();
|
|
|
+ purchaseBom.setPurchaseId(purchase.getId());
|
|
|
+ purchaseBom.setBomSpecId(bomSpec.getId());
|
|
|
+ purchaseBom.setUnitPrice(new BigDecimal(item.getUnitPrice()));
|
|
|
+ purchaseBom.setTaxRate(BigDecimal.TEN);
|
|
|
+ purchaseBom.setPurchaseQuantity(new BigDecimal(item.getQuantity()));
|
|
|
+ purchaseBom.setArrivalQuantity(BigDecimal.ZERO);
|
|
|
+ purchaseBom.setReturnQuantity(BigDecimal.ZERO);
|
|
|
+ purchaseBom.setPaidAmount(BigDecimal.ZERO);
|
|
|
+
|
|
|
+ purchase.setTotalAmountIncludingTax(purchase.getTotalAmountIncludingTax().add(new BigDecimal(item.getTotalAmount())));
|
|
|
+ // 计算不含税总金额
|
|
|
+ BigDecimal totalAmountIncludingTax = purchaseBom.getUnitPrice()
|
|
|
+ .divide(new BigDecimal("1.1"), 4, RoundingMode.HALF_UP)
|
|
|
+ .multiply(purchaseBom.getPurchaseQuantity());
|
|
|
+ purchase.setTotalAmountExcludingTax(purchase.getTotalAmountExcludingTax().add(totalAmountIncludingTax));
|
|
|
+
|
|
|
+ // 保存最新的合同价格
|
|
|
+ bomSpec.setCostPrice(purchaseBom.getUnitPrice());
|
|
|
+ bomSpec.setInternalSellingPrice(purchaseBom.getUnitPrice());
|
|
|
+ bomSpecList.add(bomSpec);
|
|
|
+
|
|
|
+ return purchaseBom;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ purchaseList.add(purchase);
|
|
|
+ purchaseBomList.addAll(bomData);
|
|
|
+ purchaseMap.put(purchaseCode, purchase.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ this.saveBatch(purchaseList);
|
|
|
+ purchaseBomService.saveBatch(purchaseBomList);
|
|
|
+ if (!bomSpecList.isEmpty()) {
|
|
|
+ bomSpecService.updateBatchById(bomSpecList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validatedImportData(List<PurchaseBomImportDataDto> list) {
|
|
|
+ Assert.notEmpty(list, "采购合同明细不能为空");
|
|
|
+ for (PurchaseBomImportDataDto dto : list) {
|
|
|
+ Assert.notBlank(dto.getPurchaseCode(), "采购合同编号不能为空");
|
|
|
+ Assert.notBlank(dto.getSupplierCode(), "供应商编号不能为空");
|
|
|
+ Assert.notBlank(dto.getBomSpecCode(), "bom品号不能为空");
|
|
|
+ Assert.eqTrue(NumberUtil.isNumber(dto.getQuantity()), "采购数量格式不正确");
|
|
|
+ Assert.eqTrue(NumberUtil.isNumber(dto.getUnitPrice()), "采购单价格式不正确");
|
|
|
+ Assert.eqTrue(NumberUtil.isNumber(dto.getTotalAmount()), "采购金额合计格式不正确");
|
|
|
+ Assert.gtZero(new BigDecimal(dto.getQuantity()), "采购数量必须大于0");
|
|
|
+ Assert.gtZero(new BigDecimal(dto.getUnitPrice()), "采购单价必须大于0");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|