在采购单界面添加按钮的提交后增加代码,用于更新库存表记录
包含了查找表数据,添加表记录,修改表记录三个表单用法。分别是:
- 查找:$.form.getFormDocumentsByFieldNameAndValue(appId, formName, fieldMap); //找到的是个列表
- 添加:$.form.saveFormDocument(doc, appId);
- 修改:$.form.updateDocument(appId, formName, documentInfo);
(function () {
var appId = $.context.getCurrentApplicationId();
var storageDoc = $.context.getCurrentDocument();
var storageDetailDocs = new Packages.java.util.ArrayList();
var subOriginalDocuments = $.form.getSubFormDocumentsByName(storageDoc.id, "采购业务号", "采购明细", appId);
var types = [{ value: 1, name: '商品编码' }, { value: 2, name: '配件编码' }];
for (var index = 0; index < subOriginalDocuments.size(); index++) {
var subOriginalDocument = subOriginalDocuments.get(index);
var type = subOriginalDocument.getElementByName("类别").getValue();
var productCode = subOriginalDocument.getElementByName("商品编码").getValue();
var partCode = subOriginalDocument.getElementByName("配件编码").getValue();
var num = subOriginalDocument.getElementByName("数量").getIntValue();
var price = Number(subOriginalDocument.getElementByName("单价").getValue());
for (var t = 0; t < types.length; t++) {
var pType = types[t];
if (pType.value == type) {
//查询库存表中对应类别与编码的数据,如果没有找到就新增,如果找到就修改
var fieldMap = new Packages.java.util.HashMap();
fieldMap.put("类别", type);
fieldMap.put(pType.name, type==1?productCode:partCode);
var formName = "库存表";
//查询出的是个列表
var productForm = $.form.getFormDocumentsByFieldNameAndValue(appId, formName, fieldMap);
if (!productForm) {
//新增
var doc = $.form.createEmptyDocument(appId, formName);
doc.addElement("类别", type);
doc.addElement(pType.name,type==1?productCode:partCode);
doc.addElement("数量", num);
doc.addElement("累计入库数量", num);
doc.addElement("最新成本价", price);
doc.addElement("累计入库金额", num * price);
$.form.saveFormDocument(doc, appId);
} else {
var obj = productForm.get(0);
var id = obj.getId();
var nums = obj.getElementByName('数量').getIntValue();
var totalNum = obj.getElementByName('累计入库数量').getIntValue();
var totalMoney = Number(obj.getElementByName('累计入库金额').getValue());
nums += nums;
totalNum += num;
totalMoney += Number((num * price).toFixed(2));
var avgPrice = Number((totalMoney / totalNum).toFixed(2));
var documentInfo = new Packages.java.util.HashMap();
//documentInfo必须存在id字段
documentInfo.put("id", id);
documentInfo.put("数量", nums);
documentInfo.put("累计入库数量", totalNum);
documentInfo.put("累计入库金额", totalMoney);
documentInfo.put("最新成本价", avgPrice);
$.log.info("{}", documentInfo);
try {
$.form.updateDocument(appId, formName, documentInfo);
} catch (e) {
$.log.info("{}", e)
}
}
}
}
}
})()