物料清单中,点击保存,自动将单据体内的物料行生成替代关系。我重写的服务插件 BeginOperationTransaction
如果满足生成替代关系的行,则复制当前行,成为新增行,然后修改新增行的辅助属性及其他内容。
在保存的时候,提示异常,违反唯一约束,FentryID重复。
这个功能,我在表单插件中已经实现了,重写的AfterDoOperation。因为在列表中也需要,所以,想写到保存的服务插件中,但系统提示异常。
[code]
///
/// 操作开始前功能处理
///
///
public override void BeginOperationTransaction(BeginOperationTransactionArgs e)
{
foreach (DynamicObject billObj in e.DataEntitys)
{
//获取元数据的服务接口
IMetaDataService metaSrvice = Kingdee.BOS.Contracts.ServiceFactory.GetMetaDataService(this.Context);
//比如获取BOM元数据
FormMetadata formMetadata = metaSrvice.Load(this.Context, "ENG_BOM") as FormMetadata;
BusinessInfo info = formMetadata.BusinessInfo;
DynamicObjectCollection entityRows = billObj["TreeEntity"] as DynamicObjectCollection; //单据体数据集
//BOM分类为 配置BOM的不需要生成替代关系 BOM分类为枚举:1:标准BOM;2:配置BOM
string BOMCATEGORY = billObj["BOMCATEGORY"].ToString();
if (BOMCATEGORY == "2")
return;
string dianzhuang = string.Empty;
string yanse = string.Empty;
string tezheng = string.Empty;
string entityID = string.Empty;
string materialID = string.Empty;
string fid = billObj["id"].ToString();
string value = " ";
string valueType = " ";
string valueColor = " ";
string valueColorType = " ";
string valueState = " ";
string valueStateType = " ";
int Priority = 1;//优先级
int count = entityRows.Count;
int newrow = count + 1; //最新的行数
for (int i = 0; i < count; i++)
{
Priority = 1;
entityID = entityRows[i]["id"].ToString();
//读取每一行
DynamicObject materialObj = (DynamicObject)entityRows[i]["MATERIALIDCHILD"]; //物料
materialID = materialObj["id"].ToString();
//读取当前行,辅助属性内容
DynamicObject auxProp = (DynamicObject)entityRows[i]["AuxPropId"]; //辅助属性
value = auxProp["f100001_id"].ToString();
valueColor = auxProp["f100501_id"].ToString();
valueState = auxProp["f100502_id"].ToString();
DynamicObject f100001 = (DynamicObject)auxProp["f100001"]; //
if (f100001 != null)
dianzhuang = f100001["FDataValue"].ToString();
DynamicObject f100501 = (DynamicObject)auxProp["f100501"]; //
if (f100501 != null)
yanse = f100501["FDataValue"].ToString();
DynamicObject f100502 = (DynamicObject)auxProp["f100502"]; //
if (f100502 != null)
tezheng = f100502["FDataValue"].ToString();
DataTable dtFZinfo = GetFZInfo(this.Context, entityID);
if (dianzhuang == "通用")
{
//获取该物料对应的辅助属性值
string type = "点装";// "点装"; 取所有属性
DataTable dtFzList = GetMaterialFzList(this.Context, materialID, type);
long auxpropid = 0;
string replaceGroup = entityRows[i]["ReplaceGroup"].ToString();
for (int j = 0; j < dtFzList.Rows.Count; j++)
{
value = dtFzList.Rows[j]["辅助资料内码"].ToString();
valueType = dtFzList.Rows[j]["辅助属性字段名"].ToString();
//判断颜色和特征状态是否有值,且不等于通用
if (!string.IsNullOrEmpty(yanse) && yanse != "通用")
{
valueColor = dtFZinfo.Rows[0]["辅助属性内码2"].ToString();// dtColorList.Rows[p]["辅助资料内码"].ToString();
}
if (!string.IsNullOrEmpty(tezheng) && tezheng != "通用")
{
valueState = dtFZinfo.Rows[0]["辅助属性内码3"].ToString();// dtColorList.Rows[p]["辅助资料内码"].ToString();
}
//获取该行物料对应的点装的辅助属性值对应的auxpropid
auxpropid = GetAuxpropid(this.Context, value, valueColor, valueState);
//如果辅助属性ID为空 ,需要新增辅助属性
if (auxpropid == 0)
{
DynamicObject newAuxObj = entryRow["AuxPropId"] as DynamicObject;
DynamicObject newAuxObj = (DynamicObject)entityRows[i]["AuxPropId"]; //
newAuxObj["F100001_id"] = value;
newAuxObj["F100501_id"] = valueColor;
newAuxObj["F100502_id"] = valueState;
entityRows[i]["AuxPropId"] = newAuxObj;
// 调用辅助属性保存服务:处理规则和框架一致
FlexSaveService flexSave = new FlexSaveService(this.Context, "ENG_BOM"); //单据唯一标识
flexSave.BatchSaveEntityFlexObject(info, info.GetEntity("FTreeEntity"), new DynamicObject[] { billObj });
// 获取辅助属性ID
auxpropid = FlexServiceHelper.GetFlexDataId(this.Context, newAuxObj, FormIdConst.BD_FLEXSITEMDETAILV);
}
//新增前,判断是否已经存在该辅助属性的行
bool isExist = IsexistBOMFZ(entityRows, auxpropid, materialID);
if (isExist)
{
Priority++;
continue;
}
DynamicObject newEntryRow = (DynamicObject)entityRows[i].Clone(false, true);
newEntryRow["AuxPropId_ID"] = auxpropid;
newEntryRow["ReplaceGroup"] = replaceGroup;
newEntryRow["MATERIALTYPE"] = 3;
newEntryRow["ParentRowId"] = newEntryRow["RowId"].ToString();
newEntryRow["RowId"] = "";
newEntryRow["EntryRowId"] = newrow;
newEntryRow["ReplacePolicy"] = 1; //策略
newEntryRow["ReplaceType"] = 1; //替代方式
newEntryRow["ReplacePriority"] = Priority; //替代优先级
newEntryRow["MRPPriority"] = Priority; //动态优先级
newEntryRow["IskeyItem"] = 1; //替代主料
newEntryRow["IsCanChoose"] = 0; //可选择
newEntryRow["IsCanEdit"] = 0; //可修改
newEntryRow["IsCanReplace"] = 0; //可替换
newEntryRow["EFFECTDATE"] = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")); //生效时间
// 把新行,插入到单据中,排在最后
entityRows.Insert(newrow - 1, newEntryRow);
newrow++;
Priority++;
}
//更改父行的替代策略等信息
entityRows[i]["ReplacePolicy"] = 1;
entityRows[i]["ReplaceType"] = 1;
entityRows[i]["ReplacePriority"] = 0;
entityRows[i]["MRPPriority"] = 0;
entityRows[i]["IskeyItem"] = 1;
entityRows[i]["IsCanChoose"] = 0;
entityRows[i]["IsCanEdit"] = 0;
entityRows[i]["IsCanReplace"] = 0;
}
}
int seq = 1;
foreach (var entityRow in entityRows)
{
entityRow["SEQ"] = seq;
seq++;
}
}
}[/code]
推荐阅读