(function () {
var addDataThat = null;
let isTimes = []
let viewState = z.ui.comm.getUrlParam("_o");
let read = z.ui.comm.getUrlParam("read");
let finance = z.ui.comm.getUrlParam("finance");
let detailProjectIndex = 0;
let previousAmount = z.ui.input("[name='createReqVO$totalAmount']").getValue();
let costTypeJSON = JSON.parse(document.querySelector('#costTypeJSON').value)
let lastProjectId = '';
let lastProjectName = '';
let lastProjectDeptName = '';
window.onload = function (ex) {
let timeObject = document.querySelector('#detailList')
let arrays = JSON.parse(timeObject.value)
addDataThat = $("#addData")
$("#addData").click(function () {
addDataThat.attr('disabled', "true");
let obj = {
amount: "",
bz1: "",
costType: "",
commonCostId: "",
costContent: "",
costDate: "",
createTime: "",
id: "",
invoiceNums: "",
isvalid: "",
latestModifyTime: "",
projectId: "",
projectName: "",
projectDeptName: ""
}
isTimes.push(obj)
generateTableTrHtml(isTimes.length - 1)
buildQjsjSeq();
})
$("#selectProject").click(function () {
selectProject(null, setProject);
})
initCompany();
bindEvents();
//根据返回的json对象渲染
if (arrays.length > 0) {
for (let i = 0; i < arrays.length; i++) {
let obj = {
amount: arrays[i].amount || '',
bz1: arrays[i].bz1 || '',
costType: arrays[i].costType || '',
commonCostId: arrays[i].commonCostId || '',
costContent: arrays[i].costContent || '',
costDate: arrays[i].costDate || '',
createTime: arrays[i].createTime || '',
id: arrays[i].id || '',
invoiceNums: arrays[i].invoiceNums || '',
isvalid: arrays[i].isvalid || '',
latestModifyTime: arrays[i].latestModifyTime || '',
projectId: arrays[i].projectId || '',
projectName: arrays[i].projectName || '',
projectDeptName: arrays[i].projectDeptName || ''
}
isTimes.push(obj)
}
for (let i = 0; i < isTimes.length; i++) {
generateTableTrHtml(i);
}
}
let bz2 = z.ui.input("[name='createReqVO$bz2']").getValue();
if(finance === "1"){
if(!bz2){
bz2 = 0;
}
let amount = Number(previousAmount) + Number(bz2);
let previousHtml = "(原金额:"+ amount.toFixed(2) +")"
$("#previousAmount").text(previousHtml)
}
if(bz2){
let amount = Number(previousAmount) + Number(bz2);
let previousHtml = "(原金额:"+ amount.toFixed(2) +")"
$("#previousAmount").text(previousHtml)
}
}
function generateTableTrHtml(i) {
var trDom = document.createElement("tr");
trDom.style.backgroundColor = "#fff";
trDom.id = "qjsj_" + i;
trDom.className = 'qjsjClass'
trDom.innerHTML = '
' + (i + 1) + ' | '
+ ''
+ ''
+ ' | '
+ ''
+ ''
+ ' | '
+ ''
+ ''
+ ' | '
+ ''
+ ''
+ ' | '
+ ''
+ ''
+ ' | '
+ ''
+ ''
+ ' | '
+ ''
+ ''
+ ' | '
+ ''
+ ''
+ ' | '
+ ''
+ ''
+ ' '
+ '保存'
+ ' '
+ ' '
+ '删除'
+ ' '
+ ' '
+ ' | ';
let trFragment = document.createDocumentFragment();
trFragment.appendChild(trDom);
$(".tbody").append(trFragment)
costTypeJSON.forEach(obj => {
obj.treeName = 'CommonCostDetailDO[' + i + ']$costType';
})
selecttree("[name='costType[" + i + "]']", costTypeJSON, clickCostType, allowChildClick)
z.ui.date("[name='CommonCostDetailDO[" + i + "]$costDate']").init()
if (!(!isTimes[i].costType || isTimes[i].costType.trim() === '')) {
z.ui.selecttree("[name='costType[" + i + "]']").setValue(isTimes[i].costType)
$("[name='CommonCostDetailDO[" + i + "]$costType']").val(isTimes[i].costType);
}
if(!(!lastProjectId || lastProjectId.trim() === '')){
$("[name='CommonCostDetailDO[" + i + "]$projectId']").val(lastProjectId);
}
if(!(!lastProjectName || lastProjectName.trim() === '')){
z.ui.input("[name='CommonCostDetailDO[" + i + "]$projectName']").setValue(lastProjectName);
}
if(!(!lastProjectDeptName || lastProjectDeptName.trim() === '')){
z.ui.input("[name='CommonCostDetailDO[" + i + "]$xmzrbm']").setValue(lastProjectDeptName);
}
if (!(!isTimes[i].projectId || isTimes[i].projectId.trim() === '')) {
$("[name='CommonCostDetailDO[" + i + "]$projectId']").val(isTimes[i].projectId);
}
if (!(!isTimes[i].projectName || isTimes[i].projectName.trim() === '')) {
z.ui.input("[name='CommonCostDetailDO[" + i + "]$projectName']").setValue(isTimes[i].projectName);
}
if (!(!isTimes[i].projectDeptName || isTimes[i].projectDeptName.trim() === '')) {
z.ui.input("[name='CommonCostDetailDO[" + i + "]$xmzrbm']").setValue(isTimes[i].projectDeptName);
}
if (!(!isTimes[i].costDate || isTimes[i].costDate.trim() === '')) {
z.ui.date("[name='CommonCostDetailDO[" + i + "]$costDate']").setValue(isTimes[i].costDate);
}
if (!(!isTimes[i].costContent || isTimes[i].costContent.trim() === '')) {
z.ui.input("[name='CommonCostDetailDO[" + i + "]$costContent']").setValue(isTimes[i].costContent);
}
if (isTimes[i].invoiceNums != '') {
z.ui.input("[name='CommonCostDetailDO[" + i + "]$invoiceNums']").setValue(isTimes[i].invoiceNums);
}
if (isTimes[i].amount != '') {
z.ui.input("[name='CommonCostDetailDO[" + i + "]$amount']").setValue(isTimes[i].amount);
}
if (!(!isTimes[i].bz1 || isTimes[i].bz1.trim() === '')) {
z.ui.input("[name='CommonCostDetailDO[" + i + "]$bz1']").setValue(isTimes[i].bz1);
}
if (!(!isTimes[i].commonCostId || isTimes[i].commonCostId.trim() === '')) {
$("#editBtn_" + i).find("span").html("编辑")
detailReadonly(i, false)
$("#qjsj_" + i).addClass("form-table-disable")
}
$("#selectProject_" + i).click(function () {
detailProjectIndex = i;
selectProject(null, setProject);
})
detailTotalInit(i)
$("#editBtn_" + i).click(function () {
const span = $(this).find("span");
const html = span.html();
if (html === '编辑') {
span.html('保存')
detailWrite(i);
addDataThat.attr('disabled', "true");
$("#qjsj_" + i).removeClass("form-table-disable")
return;
}
if (html === '保存') {
if(!$("[name='CommonCostDetailDO[" + i + "]$projectId']").val()){
z.ui.alertWarning(`项目不能为空!`)
return false;
}
lastProjectId = $("[name='CommonCostDetailDO[" + i + "]$projectId']").val();
lastProjectName = z.ui.input("[name='CommonCostDetailDO[" + i + "]$projectName']").getValue();
lastProjectDeptName = z.ui.input("[name='CommonCostDetailDO[" + i + "]$xmzrbm']").getValue();
if(!z.ui.date("[name='CommonCostDetailDO[" + i + "]$costDate']").getValue()){
z.ui.alertWarning(`发生日期不能为空!`)
return false;
}
if(!$("[name='CommonCostDetailDO[" + i + "]$costType']").val()){
z.ui.alertWarning(`费用实际所属类型不能为空!`)
return false;
}
if(!z.ui.input("[name='CommonCostDetailDO[" + i + "]$costContent']").getValue()){
z.ui.alertWarning(`发票内容不能为空!`)
return false;
}
if(!z.ui.input("[name='CommonCostDetailDO[" + i + "]$invoiceNums']").getValue()){
z.ui.alertWarning(`单据张数不能为空!`)
return false;
}
if(!z.ui.input("[name='CommonCostDetailDO[" + i + "]$amount']").getValue()){
z.ui.alertWarning(`金额不能为空!`)
return false;
}
span.html('编辑')
detailReadonly(i, false)
addDataThat.removeAttr('disabled')
$("#qjsj_" + i).addClass("form-table-disable")
return;
}
})
$("#delBtn_" + i).click(function () {
z.ui.confirm("compid").init({
content: "确定删除吗?",
onConfirm: function () {
var element = document.getElementById("qjsj_" + i);
element.remove();
buildQjsjSeq();
isTimes = isTimes.splice(i, 1)
jeSumInit(i);
addDataThat.removeAttr('disabled')
},
onCancel: function () {
}
})
})
if (viewState === 'v' || (read === '1'&& finance != '1')) {
detailReadonly(i)
}
}
function detailReadonly(i, isBool = true) {
if (isBool) {
$("#delBtn_" + i).css("display", "none");
$("#editBtn_" + i).css("display", "none");
}
$("[name='costType[" + i + "]']").addClass("z-readonly");
$("#selectProject_" + i).css("pointer-events", "none");
$("[name='costType[" + i + "]']").css("pointer-events", "none");
let detailElement = document.getElementById("qjsj_" + i);
let detailInput = detailElement.getElementsByTagName("input");
for (let j = 0; j < detailInput.length; j++) {
detailInput[j].disabled = true;
detailInput[j].className += "z-readonly"
}
}
function detailWrite(i) {
//财务审核时可以核减修改金额
$("[name='costType[" + i + "]']").removeClass("z-readonly");
$("#selectProject_" + i).css("pointer-events", "auto");
$("[name='costType[" + i + "]']").css("pointer-events", "auto");
let detailElement = document.getElementById("qjsj_" + i);
let detailInput = detailElement.getElementsByTagName("input");
for (let j = 0; j < detailInput.length; j++) {
detailInput[j].disabled = false;
$(detailInput[j]).removeClass("z-readonly")
}
}
function detailTotalInit(i) {
let obj = {
amount: "",
bz1: "",
commonCostId: "",
costContent: "",
costDate: "",
createTime: "",
id: "",
invoiceNums: "",
isvalid: "",
latestModifyTime: "",
projectId: "",
projectName: "",
projectDeptName: ""
}
let inpsJe = $("[name='CommonCostDetailDO[" + i + "]$amount']")[0].children[0]; //金额
let inpsPj = $("[name='CommonCostDetailDO[" + i + "]$invoiceNums']")[0].children[0];//单据张数
inpsJe.addEventListener("blur", e => {
let event = e || window.event;
let inputInfo = event.target.value;
if (!(inputInfo > 0)) {
z.ui.alertWarning(`金额不能为负数、0`)
inpsJe.value = ''
} else {
obj.amount = inputInfo
jeSumInit()
}
});
inpsPj.addEventListener("blur", e => {
let event = e || window.event;
let inputInfo = event.target.value;
if (!(inputInfo > 0) || (String(inputInfo).indexOf('.') > -1)) {
z.ui.alertWarning(`单据张数不能为负数、0、小数`)
inpsPj.value = ''
} else {
obj.invoiceNums = inputInfo
}
});
}
function jeSumInit(idx) {
let jeSum = 0 //金额汇总
for (let i = 0; i < isTimes.length; i++) {
if (idx == i) {
return
} else {
let inpsJe = $("[name='CommonCostDetailDO[" + i + "]$amount']")[0].children[0]; //金额
jeSum += Number(inpsJe.value)
}
}
z.ui.input("[name='createReqVO$totalAmount']").setValue(jeSum.toFixed(2));
z.ui.input("[name='createReqVO$totalAmountCn']").setValue(convertCurrency(jeSum.toFixed(2)));
}
//注册业务保存事件
function bindEvents() {
z.workflow.saveBtn.addListener("onSaveClick", saveForm);
}
function buildQjsjSeq() {
let elementsByName = document.getElementsByName("qjsj_seq");
for (let i = 0; i < elementsByName.length; i++) {
const seqElement = elementsByName[i];
seqElement.innerHTML = i + 1;
}
}
/*
* all 工作流js传递到业务的参数 success执行的方法
* istransfer 工作流js传递到业务的参数 是否转件
* */
function saveForm(all, istransfer) {
var postData = z.ui.form.getFormFields($("[name=createReqVO]"));
if (postData === false) {
all({success: false});
return;
}
postData.createReqVO.instanceId = z.ui.comm.getUrlParam("flowInstanceId");
var detailList = new Array()
for (let key of Object.keys(postData)) {
let mealName = postData[key];
if (key.startsWith("CommonCostDetailDO")) {
mealName.costDate = Date.parse(mealName.costDate);
detailList.push(z.ui.form.childStringify(mealName));
}
}
if(finance === "1"){
if(Number(postData.createReqVO.totalAmount) > Number(previousAmount)){
all({success: false, errorMsg: "核减后金额不能大于原来金额!"});
return;
}
//发生金额变化
if(Number(postData.createReqVO.totalAmount) != Number(previousAmount)){
let num = Number(previousAmount) - Number(postData.createReqVO.totalAmount);
postData.createReqVO.bz3 = '发生了核减:'+'核减'+num+'(元)';
postData.createReqVO.bz2 = num;
}
}
postData.createReqVO.detailList = detailList;
z.ui.ajax({
type: "post",
url: "/CommonCost/update",
data: JSON.stringify(postData.createReqVO),
contentType: "application/json",
success: function () {
all({success: true});
},
error: function () {
all({success: false});
}
})
}
function setProject(res) {
if (res.data) {
$("[name='CommonCostDetailDO[" + detailProjectIndex + "]$projectId']").val(res.data.id);
z.ui.input("[name='CommonCostDetailDO[" + detailProjectIndex + "]$projectName']").setValue(res.data.xmmc);
z.ui.input("[name='CommonCostDetailDO[" + detailProjectIndex + "]$xmzrbm']").setValue(res.data.zrbm);
}
}
function initCompany() {
z.ui.ajax({
type: "get",
url: "/common/company-tree",
data: {},
success: function (res) {
if (res && res.data.length > 0) {
selecttree("[name='createReqVO$paymentCompany']", res.data, clickCompany)
}
},
error: function () {
}
})
}
function clickCompany(even, treeId, treeNode) {
$("[name='createReqVO$paymentCompanyId']").val(treeNode.id);
}
function clickCostType(even, treeId, treeNode) {
$("[name='" + treeNode.treeName + "']").val(treeNode.id);
}
}());