|
@@ -1,8 +1,11 @@
|
|
|
package com.sd.business.service.purchase.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+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.file.utils.ObsFileUtil;
|
|
@@ -12,10 +15,10 @@ import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
import com.sd.business.entity.apply.po.ApplyBuyBom;
|
|
|
+import com.sd.business.entity.bom.po.BomSpec;
|
|
|
import com.sd.business.entity.in.po.InOutStorageBom;
|
|
|
-import com.sd.business.entity.purchase.dto.PurchaseDto;
|
|
|
-import com.sd.business.entity.purchase.dto.PurchaseSelectDto;
|
|
|
-import com.sd.business.entity.purchase.dto.PurchaseSelectListDto;
|
|
|
+import com.sd.business.entity.order.po.OrderInfo;
|
|
|
+import com.sd.business.entity.purchase.dto.*;
|
|
|
import com.sd.business.entity.purchase.enums.PurchaseStatusEnum;
|
|
|
import com.sd.business.entity.purchase.po.Purchase;
|
|
|
import com.sd.business.entity.purchase.po.PurchaseBom;
|
|
@@ -27,15 +30,15 @@ import com.sd.business.service.apply.ApplyBuyBomService;
|
|
|
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.supplier.SupplierService;
|
|
|
import com.sd.business.util.CodeEnum;
|
|
|
+import com.sd.framework.util.Assert;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@@ -59,13 +62,16 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i
|
|
|
@Autowired
|
|
|
private ApplyBuyBomService applyBuyBomService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SupplierService supplierService;
|
|
|
+
|
|
|
@Override
|
|
|
public Page<PurchaseVo> getPage(PurchaseSelectDto dto) {
|
|
|
IWrapper<Purchase> wrapper = getWrapper();
|
|
|
- wrapper.like("p", Purchase::getCode, dto.getCode());
|
|
|
wrapper.eq("p", Purchase::getFlowStatus, dto.getFlowStatus());
|
|
|
wrapper.eq("p", Purchase::getStatus, dto.getStatus());
|
|
|
wrapper.like("s", Supplier::getName, dto.getSupplierName());
|
|
|
+ wrapper.and(q -> q.like("p", Purchase::getCode, dto.getCode()).or().like("p", Purchase::getErpCode, dto.getCode()));
|
|
|
wrapper.orderByDesc("p", Purchase::getId);
|
|
|
|
|
|
// 查询采购单数据
|
|
@@ -112,6 +118,11 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i
|
|
|
item.setBomSpecWidth(bomSpec.getWidth());
|
|
|
item.setBomSpecHeight(bomSpec.getHeight());
|
|
|
});
|
|
|
+ // 赋值在途数量
|
|
|
+ purchaseBomList.forEach(item -> item.setInTransitQuantity(
|
|
|
+ item.getPurchaseQuantity()
|
|
|
+ .subtract(item.getArrivalQuantity())
|
|
|
+ .add(item.getReturnQuantity())));
|
|
|
|
|
|
result.setPurchaseBomList(purchaseBomList);
|
|
|
return result;
|
|
@@ -296,10 +307,121 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i
|
|
|
// 采购入库查询排除胜德塑胶供应商
|
|
|
if (ObjectUtil.equals(dto.getQueryType(), 1)) {
|
|
|
wrapper.ne("s", Supplier::getName, "福清市胜德塑胶制品有限公司");
|
|
|
+ wrapper.eq("p", Purchase::getStatus, PurchaseStatusEnum.UNDER_PURCHASE);
|
|
|
}
|
|
|
wrapper.orderByDesc("p", Purchase::getId);
|
|
|
|
|
|
// 查询采购单数据
|
|
|
return this.baseMapper.getPage(dto.getPage(), wrapper);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void purchaseImport(List<PurchaseImportDataDto> list, List<PurchaseBomImportDataDto> bomImportDataList) {
|
|
|
+ // 验证导入数据
|
|
|
+ validatedImportData(list, bomImportDataList);
|
|
|
+
|
|
|
+ Map<String, List<PurchaseBomImportDataDto>> bomMap = bomImportDataList.stream().collect(Collectors.groupingBy(PurchaseBomImportDataDto::getPurchaseCode));
|
|
|
+
|
|
|
+ List<String> purchaseCodes = list.stream().map(PurchaseImportDataDto::getCode).collect(Collectors.toList());
|
|
|
+ Map<String, Long> purchaseMap = this.mapKV(Purchase::getErpCode, BaseIdPo::getId, q -> q.in(Purchase::getErpCode, purchaseCodes));
|
|
|
+
|
|
|
+ List<String> supplierCodes = list.stream().map(PurchaseImportDataDto::getSupplierCode).collect(Collectors.toList());
|
|
|
+ Map<String, Long> supplierMap = supplierService.mapKV(Supplier::getCode, BaseIdPo::getId, q -> q.in(Supplier::getCode, supplierCodes));
|
|
|
+ List<String> bomSpecCodes = bomImportDataList.stream().map(PurchaseBomImportDataDto::getBomSpecCode).collect(Collectors.toList());
|
|
|
+ Map<String, Long> bomSpecMap = bomSpecService.mapKV(BomSpec::getCode, BaseIdPo::getId, q -> q.in(BomSpec::getCode, bomSpecCodes));
|
|
|
+
|
|
|
+ List<Purchase> purchaseList = new ArrayList<>();
|
|
|
+ List<PurchaseBom> purchaseBomList = new ArrayList<>();
|
|
|
+ for (PurchaseImportDataDto importDataDto : list) {
|
|
|
+ // 判断是否存在重复编号
|
|
|
+ Long purchaseId = purchaseMap.get(importDataDto.getCode());
|
|
|
+ if (purchaseId != null) {
|
|
|
+ throw new ServiceException("采购合同编号:" + importDataDto.getCode() + " 已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long supplierId = supplierMap.get(importDataDto.getSupplierCode());
|
|
|
+ if (supplierId == null) {
|
|
|
+ throw new ServiceException("未知供应商编号:" + importDataDto.getSupplierCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ List<PurchaseBomImportDataDto> bomList = bomMap.get(importDataDto.getCode());
|
|
|
+
|
|
|
+ if (ObjectUtil.isEmpty(bomList)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存采购合同
|
|
|
+ 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(importDataDto.getCode());
|
|
|
+ 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);
|
|
|
+ try {
|
|
|
+ purchase.setDeliveryDate(DateUtil.parse(bomList.get(0).getDeliveryDate()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("交付日期格式异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<PurchaseBom> bomData = bomList.stream().map(item -> {
|
|
|
+ Long bomSpecId = bomSpecMap.get(item.getBomSpecCode());
|
|
|
+ if (bomSpecId == null) {
|
|
|
+ throw new ServiceException("未知bom品号:" + item.getBomSpecCode());
|
|
|
+ }
|
|
|
+ PurchaseBom purchaseBom = new PurchaseBom();
|
|
|
+ purchaseBom.setPurchaseId(purchase.getId());
|
|
|
+ purchaseBom.setBomSpecId(bomSpecId);
|
|
|
+ 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));
|
|
|
+ return purchaseBom;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ purchaseList.add(purchase);
|
|
|
+ purchaseBomList.addAll(bomData);
|
|
|
+ purchaseMap.put(importDataDto.getCode(), purchase.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ this.saveBatch(purchaseList);
|
|
|
+ purchaseBomService.saveBatch(purchaseBomList);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validatedImportData(List<PurchaseImportDataDto> list, List<PurchaseBomImportDataDto> bomImportDataList) {
|
|
|
+ // 删除第一行表头字段
|
|
|
+ list.remove(0);
|
|
|
+ bomImportDataList.remove(0);
|
|
|
+ Assert.notEmpty(list, "采购合同不能为空");
|
|
|
+ Assert.notEmpty(bomImportDataList, "采购合同明细不能为空");
|
|
|
+ for (PurchaseImportDataDto dto : list) {
|
|
|
+ Assert.notNull(dto.getCode(), "采购合同编号不能为空");
|
|
|
+ Assert.notNull(dto.getSupplierCode(), "供应商编号不能为空");
|
|
|
+ }
|
|
|
+ for (PurchaseBomImportDataDto dto : bomImportDataList) {
|
|
|
+ Assert.notNull(dto.getPurchaseCode(), "采购合同编号不能为空");
|
|
|
+ Assert.notNull(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");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|