var materailMetadata = ServiceHelper.GetService<Kingdee.BOS.Contracts.IMetaDataService>().Load(Context, "SAL_BATCHADJUSTPRICE") as FormMetadata;
// 获取业务元素据
var materailBiz = materailMetadata.BusinessInfo;
// 定义一个DynamicObject包为量体单动态实体定义
DynamicObject dynObj = new DynamicObject(materailMetadata.BusinessInfo.GetDynamicObjectType());
// 设置单据体
var entityInvPty = materailBiz.GetEntity("FBATCHADJUSTENTRY");
var entityInvPtyData = dynObj[entityInvPty.EntryName] as DynamicObjectCollection;
dynObj["Name"] = FName;
dynObj["Date"] = Date;
dynObj["SaleOrgId_Id"] = e.DataEntitys[0]["SaleOrgId_Id"];
dynObj["SaleOrgId"] = e.DataEntitys[0]["SaleOrgId"];
dynObj["UseORGId_Id"] = e.DataEntitys[0]["SaleOrgId_Id"];
dynObj["UseORGId"] = e.DataEntitys[0]["SaleOrgId"];
dynObj["EffectiveDate"] = e.DataEntitys[0]["EffectiveDate"];
//dynObj["CreateDate"] = Date;
dynObj["PlanEffectiveDate"] = e.DataEntitys[0]["EffectiveDate"];
dynObj["EffectiveStatus"] = "S";
dynObj["EffectiveUserId_Id"] = e.DataEntitys[0]["ApproverId_Id"];
dynObj["EffectiveUserId"] = e.DataEntitys[0]["ApproverId"];
dynObj["F_YDL_GSBM_Id"] = e.DataEntitys[0]["F_PYAE_GSBM_Id"];
dynObj["F_YDL_GSBM"] = e.DataEntitys[0]["F_PYAE_GSBM"];
foreach (var item in Sal_Fid)
{
// 销售报价单当前行物料FID
var Sal_WLFid = Convert.ToString(item["MaterialId_Id"]);
var FMAPID = Convert.ToString(item["MapId_Id"]);
// 获取匹配到的销售价目表,需要修改的行
var FID_FNUMBER = a1.Select(t => t["FNUMBER"]).Distinct().ToList();
var dyObject = Sal_Quotations;
var CWXX = dyObject["SAL_QUOTATIONFIN"] as DynamicObjectCollection;
var entityInvPtyRow = new DynamicObject(entityInvPty.DynamicObjectType);
// 遍历需要修改的行
//for (int i = 0; i < FID_FNUMBER.Count(); i++)
//{
// 获取销售价目表是否存在当前物料
var WLFid = a1.Where(t => (Convert.ToString(t["FMATERIALID"]).Equals(Sal_WLFid)
&& Convert.ToString(t["FMAPID"]).Equals(FMAPID)
&& Convert.ToString(t["FFORBIDSTATUS"]).Equals("A"))).Distinct().ToArray();
Logger.Info("YDL提示:", "开始判断是新增还是修改!");
// 判定,如果不存在就是新增,如果存在就是修改
if (WLFid.Length <= 0)
{
// 新增
Logger.Info("YDL提示:", "开始新增物料物料内码为" + item["MaterialId_Id"]);
entityInvPtyRow["AdjustType"] = "A";
entityInvPtyRow["CurrencyId_Id"] = CWXX[0]["SettleCurrId_Id"];
entityInvPtyRow["CurrencyId"] = CWXX[0]["SettleCurrId"];
entityInvPtyRow["PriceObject"] = "A";
entityInvPtyRow["IsIncludedTax"] = "true";
entityInvPtyRow["MaterialId_Id"] = item["MaterialId_Id"];
entityInvPtyRow["MaterialId"] = item["MaterialId"];
entityInvPtyRow["MapId_Id"] = item["MapId_Id"];
entityInvPtyRow["MapId"] = item["MapId"];
entityInvPtyRow["AfterPrice"] = Convert.ToDecimal(item["TaxPrice"]);
entityInvPtyRow["AfterDownPrice"] = Convert.ToDecimal(item["TaxPrice"]);
entityInvPtyRow["BeforeEffDate"] = Convert.ToDateTime(e.DataEntitys[0]["EffectiveDate"]).ToString("MM/dd/yyyy");//DateTime.Now.ToString("MM/dd/yyyy");//DateTime.Now;
entityInvPtyRow["AfterEffDate"] = Convert.ToDateTime(e.DataEntitys[0]["EffectiveDate"]).ToString("MM/dd/yyyy");//DateTime.Now;
entityInvPtyRow["BeforeUnEffDate"] = Convert.ToDateTime(e.DataEntitys[0]["ExpiryDate"]).ToString("MM/dd/yyyy");
entityInvPtyRow["AfterUnEffDate"] = Convert.ToDateTime(e.DataEntitys[0]["ExpiryDate"]).ToString("MM/dd/yyyy");
//entityInvPtyRow["BeforeEffDate"] = DateTime.Now.ToString("MM/dd/yyyy");//DateTime.Now;
//entityInvPtyRow["AfterEffDate"] = Convert.ToDateTime(e.DataEntitys[0]["ExpiryDate"]).ToString("MM/dd/yyyy");//DateTime.Now;
//entityInvPtyRow["BeforeUnEffDate"] = "2100-01-01";
//entityInvPtyRow["AfterUnEffDate"] = "2100-01-01";
entityInvPtyRow["MatUnitId_Id"] = item["PriceUnitId_Id"];
entityInvPtyRow["MatUnitId"] = item["PriceUnitId"];
sql = string.Format(@"SELECT FID FROM T_SAL_PRICELIST WHERE FNUMBER = @FBILLNO");
var a = DBUtils.ExecuteDynamicObject(Context, sql, null, null, System.Data.CommandType.Text, new SqlParam[] {
new SqlParam("@FBILLNO", KDDbType.String,Convert.ToString( FID_FNUMBER[0]))});
if (a.Count() > 0)
{
FormMetadata meta = MetaDataServiceHelper.Load(Context, "BD_SAL_PriceList") as FormMetadata;
object[] objArray = new object[1]; // 创建 object 数组
objArray[0] = a[0]["FID"]; // 存入 a[0]["FID"] 的值
var qas = BusinessDataServiceHelper.Load(Context, objArray, meta.BusinessInfo.GetDynamicObjectType());
entityInvPtyRow["PriceListId_Id"] = a[0]["FID"];
entityInvPtyRow["PriceListId"] = qas[0];
}
}
else
{
// 修改
Logger.Info("YDL提示:", "开始修改物料物料内码为" + item["MaterialId_Id"]);
}
entityInvPtyData.Add(entityInvPtyRow);
}
Logger.Info("YDL提示:", "开始保存批量调价单!" );
//保存合并后的单据
var saveResult = BusinessDataServiceHelper.Save(Context, materailBiz, new[] { dynObj }, OperateOption.Create(), "Save");
var erro = saveResult.ValidationErrors;
if (erro.Count() > 0)
{
var json = JsonConvert.SerializeObject(erro);
Logger.Info("保存错误提示:", json);
return;
}
//Logger.Info("保存JSON提交结果:", saveRes);
// 获取合并后的单据的FID
var fi = saveResult.Rows;
object[] successPKs = new object[1];
foreach (var item in fi)
{
successPKs[0] = Convert.ToInt64(item["Id"]);
}
// 提交处理
var submitService = new SubmitService();
var submitResult = submitService.Submit(Context, materailBiz, successPKs, FormOperationEnum.Submit.ToString(), Option.Copy());
erro = submitResult.ValidationErrors;
if (erro.Count() > 0)
{
var json = JsonConvert.SerializeObject(erro);
Logger.Info("提交错误提示:", json);
return;
}
var isExit = CheckResult(saveResult, submitResult);
saveResult.MergeResult(submitResult);
if (isExit)
{
// 有提交失败的结果,直接退出
return;
}
// 审核处理
if (successPKs.Length == 0)
{
// 没有提交成功的结果,直接退出
return;
}
var paras = new List<object>() { "1" }; //审核所需参数
var auditPKs = successPKs.Select(x => new KeyValuePair<object, object>(x, "")).ToList();
var statusService = new SetStatusService();
var auditResult = statusService.SetBillStatus(Context, materailBiz, auditPKs, paras, FormOperationEnum.Audit.ToString(), Option.Copy());
erro = auditResult.ValidationErrors;
if (erro.Count() > 0)
{
var json = JsonConvert.SerializeObject(erro);
Logger.Info("审核错误提示:", json);
return;
}
isExit = CheckResult(saveResult, auditResult);
saveResult.MergeResult(auditResult);
if (isExit)
{
// 有审核失败的结果,直接退出
return;
}
// 操作完成后刷新单据状态数据
var billStatusField = this.BusinessInfo.GetBillStatusField();
var billStatusPropertyName = billStatusField.PropertyName;
var objs = new BusinessDataReader(Context).Load(e.DataEntitys.Select(o => o[0]).ToArray()
, BusinessInfo.GetSubBusinessInfo(new List<string>(new[] { billStatusField.Key })).GetDynamicObjectType());
foreach (var obj in objs)
{
var saveObj = e.DataEntitys.FirstOrDefault(o => o[0].ToString() == obj[0].ToString());
if (saveObj != null)
{
saveObj[billStatusPropertyName] = obj[billStatusPropertyName];
}
}
推荐阅读