using Model.InterFaceModel;
|
using Model.ModelDto.BllAsnDto;
|
using Model.ModelVm.BllAsnVm;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using WMS.BLL.LogServer;
|
using WMS.DAL;
|
using WMS.Entity.BllAsnEntity;
|
using WMS.Entity.Context;
|
using WMS.Entity.SysEntity;
|
using WMS.IBLL.IBllAsnServer;
|
|
namespace WMS.BLL.BllAsnServer
|
{
|
public class ProcurePlanServer : DbHelper<BllProcurePlanNotice>, IProcurePlanServer
|
{
|
#region 依赖注入
|
private static readonly SqlSugarScope Db = DataContext.Db;
|
|
public ProcurePlanServer() : base(Db)
|
{
|
}
|
#endregion
|
|
/// <summary>
|
/// 获取采购单信息
|
/// </summary>
|
/// <param name="model"></param>
|
/// <param name="count"></param>
|
/// <returns></returns>
|
public List<ProcurePlanNoticeDto> GetProcurePlanNoticeList(ProcurePlanNoticeVm model, out int count)
|
{
|
string sqlString = string.Empty;
|
string sqlCount = string.Empty;
|
string sqlPub = string.Empty;
|
try
|
{
|
sqlCount += "SELECT DISTINCT COUNT(tb1.ID) FROM BllProcurePlanNotice AS tb1 ";
|
sqlString += "SELECT DISTINCT tb1.*,tb3.RealName as CreateUserName,tb4.RealName as UpdateUserName FROM BllProcurePlanNotice AS tb1 ";
|
sqlPub += "LEFT JOIN BllProcurePlanNoticeDetail AS tb2 ON tb1.Id = tb2.ParentId ";
|
sqlPub += "LEFT JOIN SysUserInfor AS tb3 ON tb1.CreateUser = tb3.Id ";
|
sqlPub += "LEFT JOIN SysUserInfor AS tb4 ON tb1.UpdateUser = tb4.Id ";
|
sqlPub += $"WHERE tb1.IsDel = '0' ";
|
sqlPub += $"AND tb2.SkuNo LIKE '%{model.SkuNo}%' AND tb2.SkuName LIKE '%{model.SkuName}%' ";
|
sqlPub += $"AND tb2.CustomerName LIKE '%{model.CustomerName}%' ";
|
if (!string.IsNullOrEmpty(model.Status))
|
{
|
sqlPub += $"AND tb1.Status = '{model.Status}' ";
|
}
|
if (!string.IsNullOrEmpty(model.StartTime))
|
{
|
sqlPub += $"AND tb1.CreateTime >= '{model.StartTime}' ";
|
}
|
if (!string.IsNullOrEmpty(model.EndTime))
|
{
|
sqlPub += $"AND tb1.CreateTime <= '{model.EndTime}' ";
|
}
|
sqlCount += sqlPub;
|
sqlPub += " order by tb1.Id desc ";
|
if (model.Page == 0)
|
{
|
model.Page = 1;
|
}
|
sqlString += sqlPub + $" offset {((model.Page - 1) * model.Limit)} rows fetch next {model.Limit} rows only;";
|
|
var com = new Common();
|
count = com.GetRowCount(sqlCount);
|
|
var modelList = Db.Ado.SqlQuery<ProcurePlanNoticeDto>(sqlString);
|
|
return modelList;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
}
|
/// <summary>
|
/// 获取采购单明细信息
|
/// </summary>
|
/// <param name="model"></param>
|
/// <param name="count"></param>
|
/// <returns></returns>
|
public List<ProcurePlanNoticeDetailDto> GetProcurePlanNoticeDetailList(ProcurePlanNoticeDetailVm model, out int count)
|
{
|
string sqlString = string.Empty;
|
string sqlCount = string.Empty;
|
int rowCount = 1;
|
try
|
{
|
if (model.Page == 0)
|
{
|
model.Page = 1;
|
}
|
sqlCount += $"SELECT COUNT(ID) FROM BllProcurePlanNoticeDetail where ParentId = '{model.ParentId}' and IsDel = '0';";
|
var com = new Common();
|
count = com.GetRowCount(sqlCount);
|
if (count != 0)
|
{
|
rowCount = count;
|
}
|
|
sqlString += "SELECT DISTINCT tb1.*,tb3.RealName as CreateUserName, ";
|
sqlString += "tb4.RealName as UpdateUserName,isnull(tb6.UnitName,tb5.UnitNo) as UnitName,tb7.PackagName ";
|
sqlString += "FROM BllProcurePlanNoticeDetail AS tb1 ";
|
sqlString += "LEFT JOIN SysUserInfor AS tb3 ON tb1.CreateUser = tb3.Id ";
|
sqlString += "LEFT JOIN SysUserInfor AS tb4 ON tb1.UpdateUser = tb4.Id ";
|
sqlString += "LEFT JOIN SysMaterials AS tb5 on tb1.SkuNo = tb5.SkuNo ";
|
sqlString += "LEFT JOIN SysUnit AS tb6 on tb5.UnitNo = tb6.UnitNo ";
|
sqlString += "LEFT JOIN SysPackag AS tb7 on tb1.PackagNo = tb7.PackagNo ";
|
sqlString += $"WHERE tb1.ParentId = '{model.ParentId}' AND tb1.IsDel = '0' order by tb1.SkuNo desc ";
|
sqlString += $"offset {((model.Page - 1) * model.Limit)} rows fetch next {rowCount} rows only;";
|
|
var modelList = Db.Ado.SqlQuery<ProcurePlanNoticeDetailDto>(sqlString);
|
|
return modelList;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
}
|
/// <summary>
|
/// 通过采购单生成入库单据
|
/// </summary>
|
/// <param name="planDetailId"></param>
|
/// <param name="userId"></param>
|
public void CreateAsnByProcurePlan(int planDetailId,int userId)
|
{
|
try
|
{
|
//采购单明细
|
var planDetail= Db.Queryable<BllProcurePlanNoticeDetail>().First(it => it.Id == planDetailId && it.IsDel == "0");
|
if (planDetail == null)
|
{
|
throw new Exception("未查询到采购单据明细信息");
|
}
|
//采购总单信息
|
var planOrd = Db.Queryable<BllProcurePlanNotice>().First(it => it.Id == planDetail.ParentId && it.IsDel == "0");
|
if (planOrd == null)
|
{
|
throw new Exception("未查询到采购单据总单信息");
|
}
|
if (planOrd.Status == "3")
|
{
|
throw new Exception("采购单据总单已关单");
|
}
|
|
// 根据客户编号获取客户名称
|
var CustomerModel = Db.Queryable<SysCustomer>().First(it => it.CustomerNo == planDetail.CustomerNo && it.IsDel == "0");
|
if (CustomerModel == null)
|
{
|
throw new Exception("客户编号不存在!");
|
}
|
// 入库总表信息
|
string asnNo = new Common().GetMaxNo("ASN");
|
var asnModel = new BllArrivalNotice()
|
{
|
ASNNo = asnNo,
|
Status = "0",//执行状态,0:等待执行
|
Type = "1",//单据类型,1:采购入库
|
Origin = "采购单",
|
CustomerNo = CustomerModel.CustomerNo,
|
CustomerName = CustomerModel.CustomerName,
|
OrderCode = planOrd.OrderCode,
|
CreateUser = userId
|
};
|
// 获取物料详细信息
|
var skuModel = Db.Queryable<SysMaterials>().First(it => it.SkuNo == planDetail.SkuNo && it.IsDel == "0");
|
if (skuModel == null)
|
{
|
throw new Exception("不存在当前物料信息!");
|
}
|
// 验证包装信息是否存在
|
var packagModel = Db.Queryable<SysPackag>().First(it => it.PackagNo == planDetail.PackagNo && it.IsDel == "0");
|
if (packagModel == null)
|
{
|
throw new Exception("不存在当前包装信息!");
|
}
|
//入库单明细
|
var detailModel = new BllArrivalNoticeDetail()
|
{
|
ASNNo = asnNo,
|
SkuNo = skuModel.SkuNo,
|
SkuName = skuModel.SkuName,
|
Standard = skuModel.Standard,
|
LotNo = "",
|
LotText = "",
|
Qty = 0,
|
FactQty = 0,
|
CompleteQty = 0,
|
PackagNo = packagModel.PackagNo,
|
SupplierLot = "",
|
Status = "0",
|
IsSampling = "0",
|
InspectStatus = "0",
|
OrderDetailCode = planDetail.OrderDetailCode,
|
CreateUser = userId
|
};
|
|
//开启事务
|
Db.BeginTran();
|
|
// 插入入库单总表
|
Db.Insertable(asnModel).ExecuteCommand();
|
// 插入入库单明细
|
Db.Insertable(detailModel).ExecuteCommand();
|
|
//添加操作日志
|
new OperationASNServer().AddLogOperationAsn("入库作业", "入库单据", asnNo, "添加", $"在采购单页面添加了单据号为{asnNo}的单据信息", userId);
|
|
//提交事务
|
Db.CommitTran();
|
}
|
catch (Exception ex)
|
{
|
Db.RollbackTran();
|
throw new Exception(ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 关闭采购单据
|
/// </summary>
|
/// <param name="Id"></param>
|
/// <param name="userId"></param>
|
public void CloseProcurePlan(int Id,int userId)
|
{
|
try
|
{
|
//采购总单信息
|
var planOrd = Db.Queryable<BllProcurePlanNotice>().First(it => it.Id == Id && it.IsDel == "0" && it.Status == "2");
|
if (planOrd == null)
|
{
|
throw new Exception("未查询到采购单据总单信息");
|
}
|
//开启事务
|
Db.BeginTran();
|
|
planOrd.Status = "3";
|
planOrd.UpdateUser = userId;
|
planOrd.UpdateTime = DateTime.Now;
|
//更新采购单状态
|
Db.Updateable(planOrd).ExecuteCommand();
|
|
//添加操作日志
|
new OperationASNServer().AddLogOperationAsn("入库作业", "入库单据", planOrd.OrderCode, "关单", $"关闭了采购单据号为{planOrd.OrderCode}的采购单据信息", userId);
|
|
//提交事务
|
Db.CommitTran();
|
}
|
catch (Exception ex)
|
{
|
Db.RollbackTran();
|
throw new Exception(ex.Message);
|
}
|
}
|
|
#region 接口方法
|
/// <summary>
|
/// 上游系统下发创建采购单
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
public ErpModel CreateProcurePlan(ProcurePlanInfo model)
|
{
|
try
|
{
|
var resultModel = new ErpModel() { Success = -1, Message = "" };
|
|
if (string.IsNullOrEmpty(model.OrderCode))
|
{
|
resultModel.Message = "上游系统单号不可为空!";
|
return resultModel;
|
}
|
if (model.ProcurePlanDetails.Count <= 0)
|
{
|
resultModel.Message = "采购单明细不可为空!";
|
return resultModel;
|
}
|
|
//开启事务
|
Db.BeginTran();
|
|
// 采购单总表信息
|
var planModel = new BllProcurePlanNotice()
|
{
|
Status = "0",
|
OrderCode = model.OrderCode,
|
};
|
// 插入采购单总表
|
int parentId = Db.Insertable(planModel).ExecuteReturnIdentity();
|
|
// 采购单明细表信息
|
List<BllProcurePlanNoticeDetail> detailModels = new List<BllProcurePlanNoticeDetail>();
|
foreach (ProcurePlanDetail asnDetailModel in model.ProcurePlanDetails)
|
{
|
if (string.IsNullOrEmpty(asnDetailModel.SkuNo))
|
{
|
resultModel.Message = "物料编码不可为空!";
|
return resultModel;
|
}
|
if (asnDetailModel.Qty <= 0)
|
{
|
resultModel.Message = "数量不能为空!";
|
return resultModel;
|
}
|
if (string.IsNullOrEmpty(asnDetailModel.Customer))
|
{
|
resultModel.Message = "供应商不可为空!";
|
return resultModel;
|
}
|
|
// 获取物料详细信息
|
var skuModel = Db.Queryable<SysMaterials>().First(it => it.SkuNo == asnDetailModel.SkuNo && it.IsDel == "0");
|
if (skuModel == null)
|
{
|
resultModel.Message = "不存在当前物料信息!";
|
return resultModel;
|
}
|
|
// 根据客户编号获取客户名称
|
var CustomerModel = Db.Queryable<SysCustomer>().First(it => it.CustomerNo == asnDetailModel.Customer && it.IsDel == "0");
|
if (CustomerModel == null)
|
{
|
resultModel.Message = "供应商编号不存在!";
|
return resultModel;
|
}
|
|
//采购单明细信息
|
var detailModel = new BllProcurePlanNoticeDetail()
|
{
|
ParentId = parentId,
|
OrderDetailCode= asnDetailModel.OrderDetailCode,
|
SkuNo = asnDetailModel.SkuNo,
|
SkuName = skuModel.SkuName,
|
Standard = skuModel.Standard,
|
Qty = (decimal)asnDetailModel.Qty,
|
CompleteQty=0,
|
PackagNo = skuModel.PackagNo,
|
Status = "0",
|
CustomerNo = asnDetailModel.Customer,
|
CustomerName = CustomerModel.CustomerName,
|
CreateUser = 0
|
};
|
detailModels.Add(detailModel);
|
|
#region 入库单
|
// 入库总表信息
|
string asnNo = new Common().GetMaxNo("ASN");
|
var asnModel = new BllArrivalNotice()
|
{
|
ASNNo = asnNo,
|
Status = "0",//执行状态,0:等待执行
|
Type = "1",//单据类型,1:采购入库
|
Origin = "采购单",
|
CustomerNo = CustomerModel.CustomerNo,
|
CustomerName = CustomerModel.CustomerName,
|
OrderCode = model.OrderCode,
|
CreateUser = 0
|
};
|
// 插入入库总表信息
|
Db.Insertable(asnModel).ExecuteCommand();
|
//入库单明细
|
var arrDetailModel = new BllArrivalNoticeDetail()
|
{
|
ASNNo = asnNo,
|
SkuNo = skuModel.SkuNo,
|
SkuName = skuModel.SkuName,
|
Standard = skuModel.Standard,
|
LotNo = "",
|
LotText = "",
|
Qty = 0,
|
FactQty=0,
|
CompleteQty=0,
|
PackagNo = skuModel.PackagNo,
|
SupplierLot = "",
|
Status = "0",
|
IsSampling = "0",
|
InspectStatus = "0",
|
OrderDetailCode = asnDetailModel.OrderDetailCode,
|
CreateUser = 0
|
};
|
// 插入入库总表信息
|
Db.Insertable(arrDetailModel).ExecuteCommand();
|
#endregion
|
|
}
|
// 插入采购单明细表
|
Db.Insertable(detailModels).ExecuteCommand();
|
Db.CommitTran();
|
|
resultModel.Success = 0;
|
resultModel.Message = "成功";
|
return resultModel;
|
}
|
catch (Exception ex)
|
{
|
Db.RollbackTran();
|
throw ex;
|
}
|
}
|
|
#endregion
|
}
|
}
|