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 MesItemQtManager:Repository<MesItemQt>
|
{
|
public bool MiscellaneousIssues(MesItemQtDto mesItemQtDto)
|
{
|
var itemQtFrom = mesItemQtDto.from;
|
var itemQtLists = mesItemQtDto.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 (itemQtFrom.Type)
|
{
|
case "1":
|
{
|
var mesInvItemOuts = Db.Queryable<MesItemQt>()
|
.Where(s => s.Qtck == itemQtFrom.ApplyNumber
|
|
)
|
.Count();
|
|
if (mesInvItemOuts > 0)
|
{
|
throw new NotImplementedException(
|
itemQtFrom.ApplyNumber +
|
"的杂发申请单已经存在");
|
}
|
|
// 为当前分组保存数据
|
var groupResult = Save(itemQtFrom, itemQtLists);
|
if (!groupResult)
|
{
|
result = false;
|
}
|
|
break;
|
}
|
case "4":
|
var removeResult = Remove(itemQtFrom);
|
if (!removeResult)
|
{
|
result = false;
|
}
|
|
break;
|
default:
|
result = false;
|
break;
|
}
|
//}
|
|
return result;
|
}
|
|
|
private bool Save(MesItemQtFrom from, List<MesItemQtList> 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_QTCK.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 MesItemQt
|
{
|
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.StockOutType,
|
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 mesItemQtDetail = new List<MesItemQtDatall>();
|
|
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();
|
|
if (mesLinkU9 == null)
|
{
|
throw new NotImplementedException("[" + ItemQtRKList.SupplierCode +
|
"]供应商不存在,请同步给MES");
|
}
|
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值!");
|
}
|
|
mesItemQtDetail.Add(new MesItemQtDatall
|
{
|
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,
|
PickingUsage= ItemQtRKList.PickingUsage
|
|
|
});
|
}
|
|
var outItemCommand = Db.Insertable(mesItemQtDetail)
|
.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(MesItemQtFrom from)
|
{
|
// 查找要删除的杂发单
|
var mesItemQtrk = Db.Queryable<MesItemQt>()
|
.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<MesItemQtDatall>()
|
.Where(s => s.Mid == itemRK.Id)
|
.ToList();
|
|
if (mesInvItemOutItems == null || mesInvItemOutItems.Count == 0)
|
{
|
throw new NotImplementedException(
|
$"找不到杂发单[{from.ApplyNumber}]对应的明细");
|
}
|
|
// 删除杂发单明细
|
var deleteItemsResult = Db.Deleteable<MesItemQtDatall>()
|
.Where(s => s.Mid == itemRK.Id)
|
.ExecuteCommand();
|
|
if (deleteItemsResult <= 0)
|
{
|
throw new Exception($"找不到杂发单[ {from.ApplyNumber}]明细,删除失败");
|
}
|
|
// 删除杂发单
|
var deleteResult = Db.Deleteable<MesItemQt>()
|
.Where(s => s.Id == itemRK.Id)
|
.ExecuteCommand();
|
|
if (deleteResult <= 0)
|
{
|
throw new Exception($"删除杂发单[{from.ApplyNumber}]失败");
|
}
|
}
|
|
return true;
|
}
|
}
|
}
|