using ICS.WCF.Base;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using UFIDA.ISV.BFIssueApprove;
using UFIDA.ISV.BFIssueDelete;
using UFSoft.UBF.Exceptions1;
using UFSoft.UBF.Service;
using UFSoft.UBF.Util.Context;
using www.ufida.org.EntityData;
namespace UFIDA.ISV.CreateAboveIssue
{
public class CustCreateAboveIssue
{
///
/// 创建领料单
///
///
///
public OperationResult CreateAboveIssue(List modellist)
{
#region 入口日志
StringBuilder beginstrlog = new StringBuilder();
beginstrlog.AppendLine("日志记录开始");
beginstrlog.AppendLine("*****************************************************");
foreach (IssueAboveData model in modellist)
{
beginstrlog.AppendLine("领料部门:" + model.DeptNo);
beginstrlog.AppendLine("生产订单:" + model.MoCode);
beginstrlog.AppendLine("组织编码:" + model.OrgCode);
beginstrlog.AppendLine("备注:" + model.Memo);
foreach (IssueLineData lineinfo in model.linelist)
{
bool isLotCheck = Appconfig.WHLotCheckState(lineinfo.WHCode, model.OrgCode);
if (isLotCheck == false)
{
lineinfo.Batch = "";
}
beginstrlog.AppendLine("领料料号:" + lineinfo.ItemCode);
beginstrlog.AppendLine("领料数量:" + lineinfo.ItemQty);
beginstrlog.AppendLine("存储地点:" + lineinfo.WHCode);
beginstrlog.AppendLine("领料批号:" + lineinfo.Batch);
beginstrlog.AppendLine("领料供应商:" + lineinfo.Vender);
}
}
beginstrlog.AppendLine("*****************************************************");
Appconfig.WriteLogFile(beginstrlog.ToString(), "创建U9领料单");
#endregion
OperationResult result = new OperationResult();
ThreadContext context = CreateContextObj();
string OrgID = "";
try
{
string _MOCode = string.Empty;
UFIDAU9ISVMOICreateIssue4ExternalClient client = new UFIDAU9ISVMOICreateIssue4ExternalClient();
MessageBase[] message = null;
List molist = new List();
UFIDAU9ISVMOIssueDTOData mo = new UFIDAU9ISVMOIssueDTOData();
List PL = new List();
foreach (IssueAboveData model in modellist)
{
mo = new UFIDAU9ISVMOIssueDTOData();
PL = new List();
if (!string.IsNullOrWhiteSpace(model.MoCode))
{
string orgsql = @"select ID from Base_Organization
where Code='{0}'";
orgsql = string.Format(orgsql, model.OrgCode);
DataTable orgdt = DBhlper.Query(orgsql, Appconfig.GetU9ConnStr());
if (orgdt.Rows.Count != 0)
{
OrgID = orgdt.Rows[0]["ID"].ToString();
}
else
{
throw new Exception("未获取到组织!");
}
//验证该工单的备料表中是否存在数据
int p = Appconfig.IsExistcPickList(model.MoCode);
if (p > 0)
{
#region 遍历入参LL单行
foreach (var item in model.linelist)
{
bool IsVMI = false;
string strCcode = item.ItemCode; //子料
decimal _useQty = item.ItemQty; //子件使用量
string _strWhID = string.Empty;//仓库ID
string WHID = "";
if (!string.IsNullOrWhiteSpace(strCcode))
{
//虚拟库位 ZP1-01 20190912新增过滤虚拟库位
string _SqlDbType = @" select Distinct ID,Code from CBO_WH
where Code='" + item.WHCode + "' AND Org='" + OrgID + "'";
_SqlDbType = string.Format(_SqlDbType);
DataTable _SqlDbTypedt = DBhlper.Query(_SqlDbType, Appconfig.GetU9ConnStr());
if (_SqlDbTypedt != null && _SqlDbTypedt.Rows.Count > 0)
{
WHID = _SqlDbTypedt.Rows[0]["ID"].ToString();
}
else
{
result.MESSAGE = "传入仓库不存在!";
result.IsSuccess = false;
return result;
}
}
UFIDAU9ISVMOPickListDTOData PLS = new UFIDAU9ISVMOPickListDTOData();
int i = Appconfig.IsExistItem(strCcode);
if (i < 1)
{
result.MESSAGE = "料品编码不存在!";
result.IsSuccess = false;
return result;
}
Decimal MOQty = 0;
long MOID = 0;
_MOCode = model.MoCode;
DataTable _dt = Appconfig.GetMOQtyAndID(model.MoCode);
if (_dt != null && _dt.Rows.Count > 0)
{
foreach (DataRow _item in _dt.Rows)
{
string _strMOQty = string.Empty;
string _strMOID = string.Empty;
_strMOQty = _item["ProductQty"].ToString();
if (!string.IsNullOrWhiteSpace(_strMOQty))
{
MOQty = Decimal.Parse(_strMOQty);//生产订单数量
}
_strMOID = _item["ID"].ToString();
if (!string.IsNullOrWhiteSpace(_strMOID))
{
MOID = long.Parse(_strMOID);
}
}
}
string mOPickList = string.Empty;
mOPickList = Appconfig.GetMoPickListID(model.MoCode, strCcode);// 1001903220007058;// iPickid;
Decimal IssuedQty = 0; //已发放数量
Decimal ActualReqQty = 0; //实际需求数量
if (!string.IsNullOrWhiteSpace(mOPickList))
{
//已发放数量-领料未发数量
DataTable dtQty = Appconfig.GetQtyBymOPickList(model.MoCode, strCcode);
if (dtQty != null && dtQty.Rows.Count > 0)
{
ActualReqQty = Decimal.Parse(dtQty.Rows[0]["ActualReqQty"].ToString());
}
PLS.m_mOPickList = long.Parse(mOPickList);
}
else
{
result.MESSAGE = "备料表单ID不存在!工单:'" + model.MoCode + "',料号:'" + strCcode + "' ";
result.IsSuccess = false;
return result;
}
//PLS.m_issueQty = useQty * MOQty - SendQty;// item.issueQty* useQty;//1; //应发数量
if (ActualReqQty < _useQty)
{
result.MESSAGE += "该料品" + strCcode + "需要领用的数量大于未领数量不能创建改料品的领用;";
}
else
{
//if (useQty * MOQty - SendQty - IssueNotDeliverQty > 0)
//{
//20190920修改ZM
//PLS.m_issuedQty = useQty * MOQty - SendQty - IssueNotDeliverQty;// item.issuedQty* useQty;// 1; //实发数量
PLS.m_issueQty = _useQty;
PLS.m_issuedQty = _useQty;// item.issuedQty* useQty;// 1; //实发数量
//仓库
if (!string.IsNullOrWhiteSpace(WHID))
{
PLS.m_issueWh = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
//PLS.m_issueWh.m_code = strWhCode;// "004";//cWh;
PLS.m_issueWh.m_iD = long.Parse(WHID);// "004";//cWh;
}
//发货单位
PLS.m_issueUOM = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
string uom = Appconfig.GetMaterialUOM(strCcode);
if (!string.IsNullOrWhiteSpace(uom))
{
PLS.m_issueUOM.m_iD = long.Parse(uom);// Appconfig.GetMaterialUOM(item.ItemCode);// 1001607080022602;// iMaterialOutUOM;
}
PLS.m_aCDType = 0;
PLS.m_storageType = 4;//存储类型
//SourceDoc_SrcDocNo
PLS.SourceIssueDocLine = MOID;
#region 获取批号ID
long LotID = 0;
if (item.Batch != "")
{
string lotsql = @"SELECT ID FROM Lot_LotMaster
WHERE LotCode='{0}' AND ItemCode='{1}'
AND ItemOwnerOrg='{2}'";
lotsql = string.Format(lotsql, item.Batch, item.ItemCode, OrgID);
DataTable lotdt = DBhlper.Query(lotsql, Appconfig.GetU9ConnStr());
if (lotdt.Rows.Count == 0)
{
result.MESSAGE += "传入批号对应料号无关联,获取批号ID失败!";
result.IsSuccess = false;
return result;
}
else
{
LotID = long.Parse(lotdt.Rows[0]["ID"].ToString());
}
}
#endregion
if (LotID != 0)
{
PLS.m_lot = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
PLS.m_lot.m_code = item.Batch;
PLS.m_lot.m_iD = LotID;
}
if (IsVMI == true)
{
PLS.m_supplier = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
PLS.m_supplier.m_code = item.Vender;
}
PL.Add(PLS);
}
}
#endregion
}
else
{
result.MESSAGE += "该工单没有备料表信息 ";
result.IsSuccess = false;
return result;
}
if (PL != null && PL.Count() > 0)
{
mo.m_issueType = 0;
//部门
mo.m_dept = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
mo.m_dept.m_code = model.DeptNo;// "MY0601";
mo.m_docType = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
#region 获取生产订单单据类型(作为生产领料单的业务类型)
string IssueDocType = "";
int IssueBusinessType = 0;
string doctypesql = @"select B.Code AS DocTypeCode,B.BusinessType from MO_MO A
LEFT JOIN MO_MODocType B
ON B.ID=A.MODocType AND B.Org=A.Org";
doctypesql = string.Format(doctypesql, model.MoCode, OrgID);
DataTable doctypedt = DBhlper.Query(doctypesql, Appconfig.GetU9ConnStr());
if (doctypedt.Rows.Count != 0)
{
IssueDocType = doctypedt.Rows[0]["DocTypeCode"].ToString();
IssueBusinessType = int.Parse(doctypedt.Rows[0]["BusinessType"].ToString());
}
#endregion
mo.m_docType.m_code = IssueDocType;
mo.m_businessType = IssueBusinessType;
string BusinessType = Appconfig.GetBusinessType(model.DocTypeCode);
mo.m_srcDoc = new UFIDAU9MOPropertyTypesMOSourceDocData();// _MOCode;
mo.m_srcDoc.m_srcDocNo = _MOCode;
mo.m_memo = model.Memo; //备注
mo.m_pickListDTOs = PL.ToArray();
molist.Add(mo);
}
else
{
result.MESSAGE += "传入的数据未获取到创建领料的信息 ";
result.IsSuccess = false;
return result;
}
}
else
{
result.MESSAGE += "该工单号不能为空 ";
result.IsSuccess = false;
return result;
}
}
UFIDAU9ISVMOIssueKeyDTOData[] _result = null;
_result = client.Do(out message, context, molist.ToArray());
List LLCodeList = new List();
if (_result != null && _result.Count() > 0)
{
foreach (var item in _result)
{
if (!string.IsNullOrWhiteSpace(item.m_docNo))
{
LLCodeList.Add(item.m_docNo);
}
}
}
#region 审核生产领料单
CustApproveIssue approveIssue = new CustApproveIssue();
CustApproveIssue.OperationResult approveresult = new CustApproveIssue.OperationResult();
approveresult = approveIssue.CustBFApproveIssue(LLCodeList, modellist[0].OrgCode);
#endregion
if (approveresult.IsSuccess == true)
{
foreach (string docno in LLCodeList)
{
result.LLCode += docno + ";";
}
result.IsSuccess = true;
result.MESSAGE = "成功创建领料单!";
}
else
{
#region 审核失败日志
StringBuilder strlog = new StringBuilder();
strlog.AppendLine("审核失败日志记录开始");
strlog.AppendLine("*****************************************************");
foreach (string code in LLCodeList)
{
strlog.AppendLine("领料单号:" + code);
}
strlog.AppendLine("失败原因:" + approveresult.MESSAGE);
strlog.AppendLine("*****************************************************");
Appconfig.WriteLogFile(strlog.ToString(), "创建U9领料单");
#endregion
#region 审核失败时删除所有生成的领料单
CustBFIssueDelete issueDelete = new CustBFIssueDelete();
issueDelete.CustDeleteIssue(LLCodeList, OrgID);
#endregion
result.IsSuccess = false;
result.MESSAGE = "领料单审核失败,生成的领料单已经删除,失败原因:" + approveresult.MESSAGE;
}
}
catch (Exception ex)
{
#region 错误日志
StringBuilder strlog = new StringBuilder();
strlog.AppendLine("错误日志记录开始");
strlog.AppendLine("*****************************************************");
foreach (IssueAboveData model in modellist)
{
strlog.AppendLine("领料部门:" + model.DeptNo);
strlog.AppendLine("生产订单:" + model.MoCode);
strlog.AppendLine("组织编码:" + model.OrgCode);
strlog.AppendLine("备注:" + model.Memo);
foreach (IssueLineData lineinfo in model.linelist)
{
strlog.AppendLine("领料料号:" + lineinfo.ItemCode);
strlog.AppendLine("领料数量:" + lineinfo.ItemQty);
strlog.AppendLine("存储地点:" + lineinfo.WHCode);
}
}
strlog.AppendLine("失败原因:" + GetExceptionMessage(ex));
strlog.AppendLine("*****************************************************");
Appconfig.WriteLogFile(strlog.ToString(), "创建U9领料单");
#endregion
string Message = GetExceptionMessage(ex);
result.IsSuccess = false;
result.MESSAGE += Message;
}
//"必需设置请指定IssueDTO.BusinessType属性,生产线日计划:51,标准生产:47,返工生产:48,报废生产:49"
return result;
}
# region 辅助方法
///
/// 创建上下文
///
///
private static ThreadContext CreateContextObj()
{
// 实例化应用上下文对象
ThreadContext thContext = new ThreadContext();
System.Collections.Generic.Dictionary