准备工作-创建测试单据
1)单据标识:kdyq_flexqw
2)分录新增两个字段,会计科目(kdyq_account),核算维度(kdyq_flexfield)
增加空操作按钮(代码在插件中实现)代码解读
public class FlexFieldDemo3 extends AbstractOperationServicePlugIn{
//
// 会计科目的主键ID, 需修改, 样例导入后需手动维护一条会计科目数据, 并启用"核算维度"
// 【库存现金】的主键id
private String VAL_BASEDATA = "1320125868361649152";
//【现金账户001】的主键id
private String VAL_FLEX2_BASEDATA = "2073176969289170944";
// 单据上的基础资料字段的基础资料类型所绑定的基础资料(即:会计科目)
private static String KEY_BASEDATAENTITYNUM = "bd_accountview";
// 样例单据页面编码
private static String KEY_BILLNUMBER = "kdyq_flexqw";
// 单据上的弹性域字段的标识
private static String KEY_FLEXFIELD = "kdyq_flexfield";
private final static Log logger = LogFactory.getLog(FlexFieldDemo3.class);
@Override
public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
super.beforeExecuteOperationTransaction(e);
// 查询基础资料-会计科目数据所启用的核算维度,第三个参数核算维度值asstactitem,第三个参数辅助属性auxpty
List<Long> flexPropertyIds = FlexService.getBaseUseFlexProperties(KEY_BASEDATAENTITYNUM, VAL_BASEDATA, "asstactitem");
// 获取待新建单据的单据实体类型 kdyq_flexqw(样例单据标识)kdyq_flexfield(弹性域字段)
MainEntityType entityType = EntityMetadataCache.getDataEntityType("kdyq_flexqw");
FlexProp flexFieldProperty = (FlexProp) entityType.findProperty("kdyq_flexfield");
int flexTypeId = flexFieldProperty.getFlexTypeId();
// 获取弹性域实体类型
FlexEntityType flexFieldEntityType = FlexEntityMetaUtils.getBasedataPropFlexEntityType(KEY_BILLNUMBER, KEY_FLEXFIELD, flexTypeId, flexPropertyIds);
// 创建一个弹性域实体对象
DynamicObject flexFieldVal = new DynamicObject(flexFieldEntityType);
DataEntityPropertyCollection properties = flexFieldEntityType.getProperties();
// 设置初始值
for (IDataEntityProperty prop : properties) {
String key = prop.getName();
if ("id".equals(key)) {
continue;
}
if (!(prop instanceof BasedataProp)) {
if (key.endsWith("_id")) {
// 可不设置
// prop.setValue(flexFieldVal, VAL_FLEX1_BASEDATA);
continue;
}
String subKey = key;
key = subKey.split("__")[1];
switch (key) {
case "f000028":
// prop.setValue(flexFieldVal, VAL_FLEX1_TXT);
break;
default:
break;
}
} else {
// 会计科目【库存现金】的核算维度【现金账号】的值【现金账户001】 (现金账号的编码f0004)
prop.setValue(flexFieldVal, BusinessDataServiceHelper.loadSingle(VAL_FLEX2_BASEDATA, "cas_accountcash"));
}
}
// 将新创建的弹性域实体对象封装成可赋值给单据上弹性域字段数据的格式
FlexEntireData flexEntireData = new FlexEntireData();
flexEntireData.setFlexData(flexFieldEntityType, flexFieldVal);
long id = FlexService.saveFlexData(flexFieldEntityType, flexEntireData);
DynamicObject flexObject = (DynamicObject) flexFieldProperty.getComplexType().createInstance();
flexObject.set("id", id);
Map<String, Object> values = flexEntireData.getFlexValue();
flexObject.set("value", SerializationUtils.toJsonString(values));
// 创建单据数据,并给各字段赋值(含基础资料 & 弹性域字段)
DynamicObject demobillorgObj = BusinessDataServiceHelper.newDynamicObject(KEY_BILLNUMBER);
demobillorgObj.set("billno", "FlexFieldDemo2-0011");
demobillorgObj.set("billstatus", "A");
DynamicObjectCollection entryentity = demobillorgObj.getDynamicObjectCollection("entryentity");
DynamicObject dynamicObject = entryentity.addNew();
dynamicObject.set("kdyq_account",BusinessDataServiceHelper.loadSingle(VAL_BASEDATA, KEY_BASEDATAENTITYNUM));
dynamicObject.set("kdyq_flexfield",flexObject);
OperationResult result = SaveServiceHelper.saveOperate(KEY_BILLNUMBER, new DynamicObject[] { demobillorgObj }, OperateOption.create());
logger.info("result: " + result.getSuccessPkIds());
// IDataEntityProperty property = properties.get("$$kdyq_flexfield__f0004_id");
// property.setValue(kdyq_flexfield,1980521077037609984l);
}
}
数据准备
1)会计科目:{库存现金}
2)【会计科目】对应【核算维度】的基础资料【现金账号】{现金户001}
演示效果,点击空操作按钮,回到列表页面会计科目和核算维度插入值如下。
kdyq_text-kdyq_demo-2024120612 …(8.54KB)