using Masuit.Tools;
|
using MES.Service.DB;
|
using MES.Service.Dto.service;
|
using MES.Service.Modes;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace MES.Service.service.Warehouse
|
{
|
public class MesItemQtrkManager : Repository<MesItemQtrk>
|
{
|
public bool MiscellaneousReceipts(MesItemQtRKDto mesItemQtRKDto)
|
{
|
var itemQtRKFrom = mesItemQtRKDto.from;
|
var itemQtRKLists = mesItemQtRKDto.items;
|
|
// 根据AsnNo和MesNo对明细进行分组
|
//var groupedItems = itemOutLists
|
// .GroupBy(item => new { item.AsnNo, item.MesNo, item.SqNo })
|
// .ToList();
|
|
bool result = true;
|
//foreach (var group in itemQtRKLists)
|
//{
|
//if (group.Key.AsnNo == null)
|
//{
|
// throw new NotImplementedException("AsnNo不能为空");
|
//}
|
|
//if (group.Key.MesNo == null)
|
//{
|
// throw new NotImplementedException("入库单单号不能为空");
|
//}
|
|
//if (group.Key.SqNo == null)
|
//{
|
// throw new NotImplementedException("退货申请单行号不能为空");
|
//}
|
|
// 创建一个临时的itemOutFrom对象,使用分组的Key作为主要属性
|
//var tempItemOutFrom = new ItemOutFrom
|
//{
|
// AsnNo = group.Key.AsnNo,
|
// MesNo = group.Key.MesNo,
|
// SqNo = group.Key.SqNo,
|
// // 继承原始itemOutFrom的其他属性
|
// RtnNo = itemOutFrom.RtnNo,
|
// Type = itemOutFrom.Type,
|
// CreateBy = itemOutFrom.CreateBy,
|
// FMRMODE = itemOutFrom.FMRMODE,
|
// DepotId = itemOutFrom.DepotId,
|
// SupperId = itemOutFrom.SupperId
|
//};
|
|
// 根据Type执行不同的逻辑
|
switch (itemQtRKFrom.Type)
|
{
|
case "1":
|
{
|
var mesInvItemOuts = Db.Queryable<MesItemQtrk>()
|
.Where(s => s.Qtck == itemQtRKFrom.ApplyNumber
|
|
)
|
.Count();
|
|
if (mesInvItemOuts > 0)
|
{
|
throw new NotImplementedException(
|
itemQtRKFrom.ApplyNumber +
|
"的杂收申请单已经存在");
|
}
|
|
// 为当前分组保存数据
|
var groupResult = Save(itemQtRKFrom, itemQtRKLists);
|
if (!groupResult)
|
{
|
result = false;
|
}
|
|
break;
|
}
|
case "4":
|
var removeResult = Remove(itemQtRKFrom);
|
if (!removeResult)
|
{
|
result = false;
|
}
|
|
break;
|
default:
|
result = false;
|
break;
|
}
|
//}
|
|
return result;
|
}
|
|
|
private bool Save(MesItemQtRKFrom from, List<MesItemQtRKList> items)
|
{
|
//var mesInvItemIns = Db.Queryable<MesInvItemIns>()
|
// .Where(s => s.BillTypeId == 100
|
// && s.TransctionNo == "101"
|
// && s.BillNo == from.MesNo
|
// ).First();
|
|
//if (mesInvItemIns == null)
|
//{
|
// throw new NotImplementedException("采购入库不存在");
|
//}
|
|
|
string SuppNo = "";
|
if (from.SupplierCode!="" && from.SupplierCode!=null)
|
{
|
var mesLinkU9 = Db.Queryable<MesLinkU9>()
|
.Where(s => s.TableType == "MES_SUPPLIER"
|
&& s.U9Id == from.SupplierCode).First();
|
|
if (mesLinkU9 == null)
|
{
|
throw new NotImplementedException("供应商ID不存在或未同步于U9");
|
}
|
|
var mesSupplier = Db.Queryable<MesSupplier>()
|
.Where(s => s.Id == Decimal.Parse(mesLinkU9.MesId))
|
.First();
|
if (mesSupplier == null)
|
{
|
throw new NotImplementedException("[" + from.SupplierCode +
|
"]供应商不存在,请同步给MES");
|
}
|
SuppNo= mesSupplier.SuppNo;
|
}
|
var mesDepots = Db.Queryable<MesDepots>()
|
.Where(s => s.DepotId == Decimal.Parse(from.FromWarehouse)).First();
|
|
if (mesDepots == null)
|
{
|
throw new NotImplementedException("[" + from.FromWarehouse +
|
"]仓库不存在,请同步给MES");
|
}
|
|
var sysDepartment = Db.Queryable<SysDepartment>()
|
.Where(s => s.Departmentid == Decimal.Parse(from.ApplyDept)).First();
|
|
if (sysDepartment == null)
|
{
|
throw new NotImplementedException("[" + from.ApplyDept +
|
"]部门不存在,请同步给MES");
|
}
|
|
|
var nextSequenceValue =
|
Db.Ado.SqlQuery<decimal>("SELECT SEQ_QTRK.NEXTVAL FROM DUAL")
|
.First();
|
|
// var billCode =
|
// Db.Ado.SqlQuery<string>(
|
// "SELECT GETBILLCODE1('1000', '1000', 'TLSQ') FROM DUAL")
|
// .First();
|
//var billCode = from.RtnNo;
|
|
// 创建杂收申请单记录
|
var MesItemQtrk = new MesItemQtrk
|
{
|
Id = nextSequenceValue,
|
Qtck= from.ApplyNumber,
|
Qt001= from.Equals(null) ? DateTime.Now : from.ApplyDate,
|
Qt002= from.Equals(null) ? DateTime.Now : from.AuditDate,
|
Qt003= from.DocumentType,
|
Qt004= from.StockInType,
|
Qt005= from.CreatedBy,
|
Qt006= from.WorkOrder,
|
Qt009= sysDepartment.Departmentname,
|
Qt012 = sysDepartment.Departmentcode,
|
Qt010 = from.Reason,
|
Qt011= mesDepots.DepotCode,
|
Qt008 = mesDepots.DepotName,
|
|
Qt015= 1,
|
Qt016= from.Auditor,
|
Qt017= from.TaskNumber,
|
Qt018= SuppNo,
|
Qt019= from.Direction,
|
Qt020= from.OwnerType,
|
CustomerCode= from.CustomerCode,
|
Receiver= from.Receiver,
|
Department = from.Department,
|
StockKeeper=from.StockKeeper,
|
SourceType = from.SourceType,
|
SourceDocument = from.SourceDocument,
|
ProductionRelated = from.ProductionRelated,
|
ProductionLine = from.ProductionLine,
|
ProductionActivity = from.ProductionActivity,
|
FinishedGoods = from.FinishedGoods,
|
ReversePosting = from.ReversePosting,
|
AccountingPeriod= from.AccountingPeriod,
|
Employee= from.Employee,
|
FalseReturn = from.FalseReturn,
|
ProjectCode = from.ProjectCode,
|
TaskCode = from.TaskCode,
|
Status=from.Status,
|
OwnerOrganization= from.OwnerOrganization
|
|
|
};
|
|
// 创建杂收申请单行记录
|
var mesItemQtrrDetail = new List<MesItemQtrrDetail>();
|
|
foreach (var ItemQtRKList in items)
|
{
|
// 检查必要字段是否为空
|
//if (string.IsNullOrEmpty(itemOutList.SrcDocNo))
|
//{
|
// throw new NotImplementedException("采购订单号不能为空");
|
//}
|
|
|
|
//var mesRohInData = Db.Queryable<MesRohInData>()
|
// .Where(s => s.BillNo == itemOutList.SrcDocNo
|
// && s.OrderLineId == itemOutList.SrcDocLineNo)
|
// .First();
|
|
//if (mesRohInData == null)
|
//{
|
// throw new NotImplementedException("采购订单不存在");
|
//}
|
|
//var deliveryDetail = Db.Queryable<DeliveryDetail>()
|
// .Where(a => a.Zzasn == from.AsnNo
|
// && Int32.Parse(a.ZzitemId) ==
|
// Int32.Parse(itemOutList.AsnLineNo))
|
// .Count();
|
|
//if (deliveryDetail <= 0)
|
//{
|
// throw new NotImplementedException("[" + from.AsnNo + "]的明细行[" +
|
// itemOutList.AsnLineNo +
|
// "]不存在");
|
//}
|
|
var itemIdLinkU9 = Db.Queryable<MesLinkU9>()
|
.Where(s => s.TableType == "MES_ITEMS"
|
&& s.U9Id == ItemQtRKList.ItemId).First();
|
|
if (itemIdLinkU9 == null)
|
{
|
throw new NotImplementedException("物料ID不存在或未同步于U9");
|
}
|
|
var mesItems = Db.Queryable<MesItems>()
|
.Where(s => s.Id == Decimal.Parse(itemIdLinkU9.MesId))
|
.First();
|
|
if (mesItems == null)
|
{
|
throw new NotImplementedException("[" + ItemQtRKList.ItemId +
|
"]物料不存在,请同步给MES");
|
}
|
var mesDepot = "";
|
if (ItemQtRKList.StorageLocation != null && ItemQtRKList.StorageLocation!=null)
|
{
|
mesDepots = Db.Queryable<MesDepots>()
|
.Where(s => s.DepotId == Decimal.Parse(ItemQtRKList.StorageLocation)).First();
|
if (mesDepots == null)
|
{
|
throw new NotImplementedException("[" + ItemQtRKList.StorageLocation +
|
"]仓库不存在,请同步给MES");
|
}
|
mesDepot = mesDepots.DepotCode;
|
}
|
string supplierCode = "";
|
if (ItemQtRKList.SupplierCode!=null && ItemQtRKList.SupplierCode != "")
|
{
|
var mesLinkU9 = Db.Queryable<MesLinkU9>()
|
.Where(s => s.TableType == "MES_SUPPLIER"
|
&& s.U9Id == ItemQtRKList.SupplierCode).First();
|
|
var mesSupplier = Db.Queryable<MesSupplier>()
|
.Where(s => s.Id == Decimal.Parse(mesLinkU9.MesId))
|
.First();
|
if (mesSupplier == null)
|
{
|
throw new NotImplementedException("[" + ItemQtRKList.SupplierCode +
|
"]供应商不存在,请同步给MES");
|
}
|
supplierCode = mesSupplier.SuppNo;
|
}
|
|
|
//var mesInvItemInCItems = Db.Queryable<MesInvItemInCItems>()
|
// .Where(s => s.ItemInId == mesInvItemIns.Id
|
// && s.ItemNo == mesItems.ItemNo
|
// && s.Ebeln == itemOutList.SrcDocNo
|
// && s.EbelnLineNo ==
|
// Decimal.Parse(itemOutList.SrcDocLineNo)
|
// && s.SuppNo == mesSupplier.SuppNo
|
// && Convert.ToInt32(s.ZzitemId) ==
|
// Convert.ToInt32(itemOutList.AsnLineNo)).First();
|
|
//if (mesInvItemInCItems == null)
|
//{
|
// throw new NotImplementedException("没有对应的入库明细");
|
//}
|
|
|
// 确保CbillNo不为空
|
if (ItemQtRKList.AppliedQuantity<=0 || ItemQtRKList.AppliedQuantity==null)
|
{
|
throw new NotImplementedException("申请数量不能为0或NULL值!");
|
}
|
|
mesItemQtrrDetail.Add(new MesItemQtrrDetail
|
{
|
Mid = nextSequenceValue,
|
Qd002= mesItems.ItemNo,
|
Qd003= mesItems.ItemName,
|
Qd004= mesItems.ItemModel,
|
Qd005= mesItems.ColorName,
|
Qd006= ItemQtRKList.PlannedQuantity,
|
Qd007= ItemQtRKList.AppliedQuantity,
|
Qd008=0, // 已入数量默认为0
|
Qd009= mesItems.ItemUnit,
|
Qd010= ItemQtRKList.Remark,
|
Qd012= mesItems.Id, // 物料内码
|
LineNumber= ItemQtRKList.LineNumber,
|
ProductionRelated= ItemQtRKList.ProductionRelated,
|
StorageType= ItemQtRKList.StorageType,
|
OwnerOrganization = ItemQtRKList.OwnerOrganization,
|
StorageLocation= mesDepot,
|
SupplierCode= supplierCode,
|
CustomerCode = ItemQtRKList.CustomerCode,
|
ProjectCode = ItemQtRKList.ProjectCode,
|
TaskCode = ItemQtRKList.TaskCode,
|
ProductionOrder = ItemQtRKList.ProductionOrder,
|
DailyPlan = ItemQtRKList.DailyPlan,
|
ProductionActivity = ItemQtRKList.ProductionActivity,
|
FinishedProduct = ItemQtRKList.FinishedProduct
|
|
});
|
}
|
|
var outItemCommand = Db.Insertable(mesItemQtrrDetail)
|
.PageSize(1).IgnoreColumnsNull().ExecuteCommand();
|
if (outItemCommand <= 0)
|
{
|
throw new Exception("创建杂收单子表失败");
|
}
|
|
// 插入采购退料单记录
|
var insertResult = Db.Insertable(MesItemQtrk).IgnoreColumns(true)
|
.ExecuteCommand();
|
if (insertResult <= 0)
|
{
|
throw new Exception("创建杂收单失败");
|
}
|
|
return outItemCommand + insertResult >= 2;
|
}
|
|
private bool Remove(MesItemQtRKFrom from)
|
{
|
// 查找要删除的杂收单
|
var mesItemQtrk = Db.Queryable<MesItemQtrk>()
|
.Where(s => s.Qtck == from.ApplyNumber
|
).ToList();
|
|
if (mesItemQtrk == null || mesItemQtrk.Count == 0)
|
{
|
throw new NotImplementedException("找不到对应的杂收单: " + from.ApplyNumber);
|
}
|
|
// 检查是否有已审核的单据,如果存在已审核(Status=1)则不允许删除
|
//if (mesInvItemOuts.Any(item => item.Status == 1))
|
//{
|
// throw new NotImplementedException("存在已审核的采购退料单,不允许删除");
|
//}
|
|
// 删除所有相关单据
|
foreach (var itemRK in mesItemQtrk)
|
{
|
// 查找要删除的杂收单明细
|
var mesInvItemOutItems = Db.Queryable<MesItemQtrrDetail>()
|
.Where(s => s.Mid == itemRK.Id)
|
.ToList();
|
|
if (mesInvItemOutItems == null || mesInvItemOutItems.Count == 0)
|
{
|
throw new NotImplementedException(
|
$"找不到杂收单[{from.ApplyNumber}]对应的明细");
|
}
|
|
// 删除杂收单明细
|
var deleteItemsResult = Db.Deleteable<MesItemQtrrDetail>()
|
.Where(s => s.Mid == itemRK.Id)
|
.ExecuteCommand();
|
|
if (deleteItemsResult <= 0)
|
{
|
throw new Exception($"找不到杂收单[ {from.ApplyNumber}]明细,删除失败");
|
}
|
|
// 删除杂收单
|
var deleteResult = Db.Deleteable<MesItemQtrk>()
|
.Where(s => s.Id == itemRK.Id)
|
.ExecuteCommand();
|
|
if (deleteResult <= 0)
|
{
|
throw new Exception($"删除杂收单[{from.ApplyNumber}]失败");
|
}
|
}
|
|
return true;
|
}
|
}
|
}
|