You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-spms-java/src/main/java/com/glxp/api/service/inout/IoChangeInoutService.java

300 lines
14 KiB
Java

package com.glxp.api.service.inout;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.glxp.api.constant.Constant;
import com.glxp.api.constant.ConstantStatus;
import com.glxp.api.constant.ConstantType;
import com.glxp.api.entity.auth.InvWarehouseEntity;
import com.glxp.api.entity.basic.BasicBusTypePreEntity;
import com.glxp.api.entity.basic.BasicBussinessTypeEntity;
import com.glxp.api.entity.inout.*;
import com.glxp.api.entity.inv.InvProductDetailEntity;
import com.glxp.api.entity.system.SystemParamConfigEntity;
import com.glxp.api.service.auth.InvWarehouseService;
import com.glxp.api.service.basic.IBasicBusTypePreService;
import com.glxp.api.service.basic.IBasicBussinessTypeService;
import com.glxp.api.service.inv.InvProductDetailService;
import com.glxp.api.service.inv.InvProductService;
import com.glxp.api.service.system.SystemParamConfigService;
import com.glxp.api.util.CustomUtil;
import com.glxp.api.util.DateUtil;
import com.glxp.api.util.GennerOrderUtils;
import com.glxp.api.util.OrderNoTypeBean;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
/**
* 单据流转
*/
@Service
public class IoChangeInoutService {
@Resource
IBasicBusTypePreService basicBusTypePreService;
@Resource
IoCodeService codeService;
@Resource
IoOrderService orderService;
@Resource
InvProductDetailService invProductDetailService;
@Resource
IBasicBussinessTypeService basicBussinessTypeService;
@Resource
GennerOrderUtils gennerOrderUtils;
@Resource
IoAddInoutService addInoutService;
@Resource
@Lazy
IoCheckInoutService ioCheckInoutService;
@Resource
InvWarehouseService invWarehouseService;
@Resource
SystemParamConfigService systemParamConfigService;
@Resource
IoCodeTempService codeTempService;
@Resource
IoOrderInvoiceService ioOrderInvoiceService;
@Resource
IoOrderDetailBizService orderDetailBizService;
public void changeOrder(IoOrderEntity orderEntity) {
BasicBusTypePreEntity basicBusTypePreEntity = basicBusTypePreService.findByOriginAction(orderEntity.getAction());
List<IoCodeEntity> codeEnttities = codeService.findByOrderId(orderEntity.getBillNo());
if (basicBusTypePreEntity != null) {
buildInOrder(basicBusTypePreEntity, codeEnttities, orderEntity, true);
}
//结束,不生成库存,
}
//后续做修改
public void changeBusOrder(IoOrderEntity orderEntity) {
}
//普通出入库单据流转
public void genNewOrder(IoOrderEntity orderEntity, List<InvProductDetailEntity> invProductDetailEntities) {
if (orderEntity.getFromType() == ConstantStatus.FROM_UDISP && (orderEntity.getOutChangeEnable() != null && !orderEntity.getOutChangeEnable())) {
return;
}
BasicBusTypePreEntity basicBusTypePreEntity = basicBusTypePreService.findByOriginAction(orderEntity.getAction());
List<IoCodeEntity> codeEnttities = codeService.findByOrderId(orderEntity.getBillNo());
if (basicBusTypePreEntity.getSupplementAll()) {//全量补单
invProductDetailService.insertList(invProductDetailEntities);
buildInOrder(basicBusTypePreEntity, codeEnttities, orderEntity, false);
} else { //缺量补单
List<IoCodeEntity> inList = new ArrayList<>();
for (IoCodeEntity warehouseEntity : codeEnttities) {
Integer invCount = getInvCount(warehouseEntity);
Integer count = warehouseEntity.getMyReCount();
if (invCount > 0) {//仓库已存在该产品
if (count > invCount) { //当前数量>结余数量,说明该产品是无序列号或者是多级包装
if (count > invCount) { //该产品实际数量大于结余数量,说明是多级包装,直接补
inList.add(warehouseEntity);
} else if (warehouseEntity.getSerialNo() == null) { //无序列号(多级包装无序列号的已在上一步被排除),补齐数量
int lea = count - invCount;
warehouseEntity.setCount(lea / (count)); //
inList.add(warehouseEntity);
}
}
} else { //结余数量《=0直接补
inList.add(warehouseEntity);
}
InvProductDetailEntity invProductDetailEntity = filterInvByCode(invProductDetailEntities, warehouseEntity.getCode());
invProductDetailService.insert(invProductDetailEntity);
}
buildInOrder(basicBusTypePreEntity, inList, orderEntity, false);
}
}
public InvProductDetailEntity filterInvByCode(List<InvProductDetailEntity> invProductDetailEntities, String
code) {
for (InvProductDetailEntity invProductDetailEntity : invProductDetailEntities) {
if (invProductDetailEntity.getCode().equals(code)) {
return invProductDetailEntity;
}
}
return null;
}
//计算当前库存数量
public int getInvCount(IoCodeEntity codeEntity) {
List<InvProductDetailEntity> resultLists = invProductDetailService.selectByUnique(codeEntity.getRelId(), codeEntity.getBatchNo(), codeEntity.getSupId(), codeEntity.getDeptCode(), codeEntity.getInvCode());
int invCount = 0;
if (CollUtil.isNotEmpty(resultLists)) {
for (InvProductDetailEntity invProductDetailEntity : resultLists) {
if (invProductDetailEntity.getMainAction().equals(ConstantType.TYPE_OUT)) {
invCount = invCount - invProductDetailEntity.getReCount();
} else {
invCount = invCount + invProductDetailEntity.getReCount();
}
}
}
return invCount;
}
//生成单据
public void buildInOrder(BasicBusTypePreEntity bussinessChangeTypeEntity, List<IoCodeEntity> useInList, IoOrderEntity orderEntity, boolean isGenInovice) {
if (StrUtil.isEmpty(orderEntity.getRelKey())) {
orderEntity.setRelKey(UUID.fastUUID().toString(true));
orderService.update(orderEntity);
}
if (useInList.size() <= 0) {
return;
}
Map<String, List<IoCodeEntity>> filterSupMaps = new HashMap<>();
BasicBussinessTypeEntity targetBustypeEntity = basicBussinessTypeService.findByAction(bussinessChangeTypeEntity.getAction());
//过滤供应商
if (targetBustypeEntity.getCorpType() == ConstantStatus.CORP_TYPE_OUT) {
for (IoCodeEntity warehouseEntity : useInList) {
if (warehouseEntity.getSupId() != null) {
if (filterSupMaps.get(warehouseEntity.getSupId()) == null) {
List<IoCodeEntity> temps = new ArrayList<>();
temps.add(warehouseEntity);
filterSupMaps.put(warehouseEntity.getSupId(), temps);
} else {
filterSupMaps.get(warehouseEntity.getSupId()).add(warehouseEntity);
}
}
}
} else {
// 原先单据流程
filterSupMaps.put("test", useInList);
// filterSupMaps.put(bussinessChangeTypeEntity.getDefaultInvCode(), useInList);
}
//根据往来单位生成入库单
for (String corpName : filterSupMaps.keySet()) {
List<IoCodeEntity> temps;
temps = filterSupMaps.get(corpName);
IoOrderEntity outOrder = new IoOrderEntity();
BeanUtils.copyProperties(orderEntity, outOrder);
outOrder.setId(null);
outOrder.setSupplementNo(null);//补单需置空
if (bussinessChangeTypeEntity.getBeforeTime() != null)
outOrder.setCreateTime(DateUtil.getBeforeDay(DateUtil.fromDate(orderEntity), bussinessChangeTypeEntity.getBeforeTime()));
else
outOrder.setCreateTime(new Date()); //不设,默认为当前时间
outOrder.setAction(targetBustypeEntity.getAction());
outOrder.setMainAction(targetBustypeEntity.getMainAction());
outOrder.setBusType(targetBustypeEntity.getBusType());
outOrder.setFromType(ConstantStatus.FROM_CHANGE);
String orderNo = gennerOrderUtils.createScOrderNo(new OrderNoTypeBean(Constant.SCAN_ORDER + StrUtil.trimToEmpty(targetBustypeEntity.getPrefix()), "yyyyMMdd"));
outOrder.setBillNo(orderNo);
if (bussinessChangeTypeEntity.getCreateType() == 1) {//送货至入库补单
outOrder.setCustomerId(corpName);
outOrder.setFromCorp(corpName);
outOrder.setFromInvCode(null);
outOrder.setFromDeptCode(null);
} else if (bussinessChangeTypeEntity.getCreateType() == 3) { //寄售、预验收补单
outOrder.setCustomerId(corpName);
outOrder.setFromCorp(corpName);
InvWarehouseEntity invWarehouseEntity = invWarehouseService.findByInvSubByCode(Constant.INV_PRE_IN_CODE);
outOrder.setFromInvCode(invWarehouseEntity.getCode());
outOrder.setFromDeptCode(invWarehouseEntity.getParentId());
} else if (bussinessChangeTypeEntity.getCreateType() == 4) {
outOrder.setCustomerId(corpName);
outOrder.setFromCorp(corpName);
InvWarehouseEntity invWarehouseEntity = invWarehouseService.findByInvSubByCode(Constant.INV_PRE_CODE);
outOrder.setFromInvCode(invWarehouseEntity.getCode());
outOrder.setFromDeptCode(invWarehouseEntity.getParentId());
} else {
outOrder.setCustomerId(Constant.SYSTEM_CUSTOMER_ID);
InvWarehouseEntity curInv = invWarehouseService.findByInvSubByCode(orderEntity.getInvCode());
InvWarehouseEntity upInv = invWarehouseService.findByInvSubByCode(curInv.getParentCode());
if (upInv != null) {
outOrder.setFromInvCode(upInv.getCode());
outOrder.setFromDeptCode(upInv.getParentId());
}
//原先流程---使用流转单据默认仓库
// outOrder.setFromInvCode(bussinessChangeTypeEntity.getDefaultInvCode());
// outOrder.setFromDeptCode(bussinessChangeTypeEntity.getDefaultDeptCode());
outOrder.setFromCorp(null);
}
outOrder.setCorpOrderId(CustomUtil.getId() + "x");
//原先流程---使用流转单据默认仓库
// InvWarehouseEntity invWarehouseEntity = invWarehouseService.findByInvSubByCode(bussinessChangeTypeEntity.getInvCode());
// outOrder.setInvCode(invWarehouseEntity.getCode());
// outOrder.setDeptCode(invWarehouseEntity.getParentId());
InvWarehouseEntity invWarehouseEntity = invWarehouseService.findByInvSubByCode(orderEntity.getInvCode());
outOrder.setInvCode(invWarehouseEntity.getCode());
outOrder.setDeptCode(invWarehouseEntity.getParentId());
outOrder.setStatus(ConstantStatus.ORDER_STATUS_PROCESS);
//互填单号
orderEntity.setUllageSupNo(outOrder.getBillNo());
orderEntity.setUpdateTime(new Date());
orderService.update(orderEntity);
outOrder.setOriginUllageSupNo(orderEntity.getBillNo());
outOrder.setCreateUser(null);
outOrder.setReviewUser(null);
orderService.insertOrder(outOrder);
//生成条码
List<IoCodeTempEntity> codeTempEntities = new ArrayList<>();
for (IoCodeEntity warehouseEntity : temps) {
IoCodeTempEntity codeTempEntity = new IoCodeTempEntity();
BeanUtils.copyProperties(warehouseEntity, codeTempEntity);
codeTempEntity.setId(null);
codeTempEntity.setOrderId(outOrder.getBillNo());
codeTempEntity.setAction(outOrder.getAction());
codeTempEntity.setMainAction(outOrder.getMainAction());
codeTempEntities.add(codeTempEntity);
}
codeTempService.insertBatch(codeTempEntities);
// 生成业务单
List<IoOrderDetailBizEntity> orderDetailBizEntities = orderDetailBizService.findByOrderId(orderEntity.getBillNo());
if (CollUtil.isNotEmpty(orderDetailBizEntities)) {
for (IoOrderDetailBizEntity orderDetailBizEntity : orderDetailBizEntities) {
orderDetailBizEntity.setOrderIdFk(outOrder.getBillNo());
orderDetailBizEntity.setId(IdUtil.getSnowflakeNextId());
orderDetailBizService.insert(orderDetailBizEntity);
}
}
if (isGenInovice) {
//生成发票信息
List<IoOrderInvoiceEntity> invoiceEntities = ioOrderInvoiceService.findByBillNo(orderEntity.getBillNo());
if (CollUtil.isNotEmpty(invoiceEntities)) {
for (IoOrderInvoiceEntity orderInvoiceEntity : invoiceEntities) {
orderInvoiceEntity.setOrderIdFk(outOrder.getBillNo());
orderInvoiceEntity.setId(IdUtil.getSnowflakeNextId());
orderInvoiceEntity.setBizIdFk(null);
orderInvoiceEntity.setUpdateTime(new Date());
ioOrderInvoiceService.insertInvoice(orderInvoiceEntity);
}
}
}
//根据单据设置状态 1:草稿
if (bussinessChangeTypeEntity.getAuditStatus() == 1) {
orderEntity.setStatus(ConstantStatus.ORDER_STATUS_TEMP_SAVE);
orderService.update(orderEntity);
} else if (bussinessChangeTypeEntity.getAuditStatus() == 2 || bussinessChangeTypeEntity.getAuditStatus() == 3) {
addInoutService.dealProcess(outOrder);
if (!ioCheckInoutService.checkManual(outOrder.getBillNo())) {
ioCheckInoutService.check(outOrder.getBillNo());
}
}
}
}
}