assetArticlesUseEdit.js 10.5 KB
Newer Older
doc_wei's avatar
doc_wei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

var assetArticles = new Array(); //用品集合

// 用品领用
layui.config({
	base: basePath,
	version: skyeyeVersion
}).extend({
	window: 'js/winui.window'
}).define(['window', 'jquery', 'winui', 'fileUpload', 'form'], function(exports) {
	winui.renderColor();
	var index = parent.layer.getFrameIndex(window.name);
	var $ = layui.$,
		form = layui.form;
	var rowNum = 1; //表格的序号
	var typeHtml = "";

	var usetableTemplate = $("#usetableTemplate").html();
	var selOption = getFileContent('tpl/template/select-option.tpl');

	var sTableData = "";

doc_wei's avatar
doc_wei 已提交
23
	AjaxPostUtil.request({url: flowableBasePath + "assetarticles021", params: {rowId: parent.rowId}, type: 'json', method: "POST", callback: function(json) {
doc_wei's avatar
doc_wei 已提交
24 25 26 27 28 29 30 31 32
		if(json.returnCode == 0) {
			$("#useTitle").html(json.bean.title);
			$("#useName").html(json.bean.userName);
			$("#remark").val(json.bean.remark);
			// 附件回显
			skyeyeEnclosure.initTypeISData({'enclosureUpload': json.bean.enclosureInfo});

			if(json.bean.state == '1'){
				$(".typeTwo").removeClass("layui-hide");
doc_wei's avatar
doc_wei 已提交
33
			} else {
doc_wei's avatar
doc_wei 已提交
34 35 36 37 38 39 40 41 42 43 44 45
				$(".typeOne").removeClass("layui-hide");
			}
			sTableData = json.bean.goods;
			initTypeHtml();
			matchingLanguage();
		} else {
			winui.window.msg(json.returnMessage, {icon: 2, time: 2000});
		}
	}});

	//初始化用品类别
	function initTypeHtml() {
doc_wei's avatar
doc_wei 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		// 获取已经上线的用品类别列表
		adminAssistantUtil.queryAssetArticlesTypeUpStateList(function (data){
			typeHtml = getDataUseHandlebars(selOption, data);
		});
		form.render();
		//类型加载变化事件
		form.on('select(selectTypeProperty)', function(data) {
			var thisRowNum = data.elem.id.replace("typeId", "");
			var thisRowValue = data.value;
			if(!isNull(thisRowValue) && thisRowValue != '请选择') {
				if(inPointArray(thisRowValue, assetArticles)) {
					//类型对应的用品存在js对象中
					var list = getListPointArray(thisRowValue, assetArticles);
					resetAssetList(thisRowNum, list); //重置选择行的用品列表
				} else {
					//类型对应的用品不存在js对象中
					AjaxPostUtil.request({url: flowableBasePath + "assetarticles018", params: {typeId: thisRowValue}, type: 'json', callback: function(json) {
						if(json.returnCode == 0) {
							assetArticles.push({
								id: thisRowValue,
								list: json.rows
							});
							resetAssetList(thisRowNum, json.rows); //重置选择行的用品列表
doc_wei's avatar
doc_wei 已提交
69
						} else {
doc_wei's avatar
doc_wei 已提交
70
							winui.window.msg(json.returnMessage, {icon: 2, time: 2000});
doc_wei's avatar
doc_wei 已提交
71
						}
doc_wei's avatar
doc_wei 已提交
72 73 74 75
					}});
				}
			}
		});
doc_wei's avatar
doc_wei 已提交
76

doc_wei's avatar
doc_wei 已提交
77 78 79 80 81 82 83 84 85 86 87 88
		//商品加载变化事件
		form.on('select(selectAssetarProperty)', function(data) {
			var thisRowNum = data.elem.id.replace("assetarId", "");
			var thisRowValue = data.value;
			var thisRowTypeChooseId = $("#typeId" + thisRowNum).val();
			if(!isNull(thisRowValue) && thisRowValue != '请选择') {
				var list = getListPointArray(thisRowTypeChooseId, assetArticles);
				$.each(list, function(i, item) {
					if(item.id === thisRowValue) {
						$("#specificationsName" + thisRowNum).html(item.specificationsName);
						$("#residualNum" + thisRowNum).html(item.residualNum);
						return false;
doc_wei's avatar
doc_wei 已提交
89 90 91
					}
				});
			} else {
doc_wei's avatar
doc_wei 已提交
92 93
				$("#specificationsName" + thisRowNum).html(""); //重置规格为空
				$("#residualNum" + thisRowNum).html(""); //重置库存为空
doc_wei's avatar
doc_wei 已提交
94
			}
doc_wei's avatar
doc_wei 已提交
95 96 97
		});
		//加载表格数据
		initTableAssetList();
doc_wei's avatar
doc_wei 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	}

	//加载表格数据
	function initTableAssetList() {
		$.each(sTableData, function(i, item) {
			addDataRow(item);
		});
	}

	// 保存为草稿
	form.on('submit(formEditBean)', function(data) {
		if(winui.verifyForm(data.elem)) {
			saveData('1', "");
		}
		return false;
	});

	// 提交审批
	form.on('submit(formSubBean)', function(data) {
		if(winui.verifyForm(data.elem)) {
			activitiUtil.startProcess(sysActivitiModel["assetArticlesUse"]["key"], function (approvalId) {
				saveData("2", approvalId);
			});
		}
		return false;
	});

	// 工作流中保存
	form.on('submit(subBean)', function(data) {
		if(winui.verifyForm(data.elem)) {
			saveData('3', "");
		}
		return false;
	});

	function saveData(subType, approvalId){
		// 获取已选用品数据
		var rowTr = $("#useTable tr");
		if(rowTr.length == 0) {
			winui.window.msg('请选择需要领用的用品~', {icon: 2, time: 2000});
			return false;
		}
		var tableData = new Array();
		var noError = false; //循环遍历表格数据时,是否有其他错误信息
		$.each(rowTr, function(i, item) {
			var rowNum = $(item).attr("trcusid").replace("tr", "");
			var residualNum = parseInt($("#residualNum" + rowNum).html());
			if(parseInt($("#useNum" + rowNum).val()) == 0) {
				$("#useNum" + rowNum).addClass("layui-form-danger");
				$("#useNum" + rowNum).focus();
				winui.window.msg('领用数量不能为0', {icon: 2, time: 2000});
				noError = true;
				return false;
			}
			if(parseInt($("#useNum" + rowNum).val()) > residualNum) {
				$("#useNum" + rowNum).addClass("layui-form-danger");
				$("#useNum" + rowNum).focus();
				winui.window.msg('领用数量不能超过库存数量', {icon: 2, time: 2000});
				noError = true;
				return false;
			}
			if(inTableDataArrayByAssetarId($("#assetarId" + rowNum).val(), tableData)){
				winui.window.msg('领用单存在相同的用品', {icon: 2, time: 2000});
				noError = true;
				return false;
			}
			var row = {
				typeId: $("#typeId" + rowNum).val(),
				assetarId: $("#assetarId" + rowNum).val(),
				useNum: $("#useNum" + rowNum).val(),
				remark: $("#remark" + rowNum).val()
			};
			tableData.push(row);
		});
		if(noError) {
			return false;
		}

		var params = {
			remark: $("#remark").val(),
			assetArticlesStr: JSON.stringify(tableData),
			rowId: parent.rowId,
			enclosureInfo: skyeyeEnclosure.getEnclosureIdsByBoxId('enclosureUpload'),
			subType: subType, // 1:保存为草稿  2.提交到工作流  3.在工作流中编辑
			approvalId: approvalId,
		};
doc_wei's avatar
doc_wei 已提交
184
		AjaxPostUtil.request({url: flowableBasePath + "assetarticles022", params: params, type: 'json', callback: function(json) {
doc_wei's avatar
doc_wei 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
			if(json.returnCode == 0) {
				parent.layer.close(index);
				parent.refreshCode = '0';
			} else {
				winui.window.msg(json.returnMessage, {icon: 2, time: 2000});
			}
		}});
	}

	// 判断选中的用品是否也在数组中
	function inTableDataArrayByAssetarId(str, array) {
		var isIn = false;
		$.each(array, function(i, item) {
			if(item.assetarId === str) {
				isIn = true;
				return false;
			}
		});
		return isIn;
	}

	//新增行
	$("body").on("click", "#addRow", function() {
		addRow();
	});

	//删除行
	$("body").on("click", "#deleteRow", function() {
		deleteRow();
	});

	//加载数据行行
	function addDataRow(item) {
		var thisRowNum = rowNum.toString();
		var par = {
			id: "row" + thisRowNum, //checkbox的id
			trId: "tr" + thisRowNum, //行的id
			typeId: "typeId" + thisRowNum, //类型id
			assetarId: "assetarId" + thisRowNum, //用品id
			specificationsName: "specificationsName" + thisRowNum, //规格id
			residualNum: "residualNum" + thisRowNum, //库存id
			useNum: "useNum" + thisRowNum, //领用数量id
			remark: "remark" + thisRowNum //备注id
		};
		$("#useTable").append(getDataUseHandlebars(usetableTemplate, par));

		//赋值给用品类别
		$("#" + "typeId" + thisRowNum).html(typeHtml);

		//数据回显
		$("#typeId" + thisRowNum).val(item.typeId);
		$("#specificationsName" + thisRowNum).html(item.specificationsName);
		$("#remark" + thisRowNum).val(item.remark);
		$("#residualNum" + thisRowNum).html(item.residualNum);
		$("#useNum" + thisRowNum).val(item.applyUseNum);
		var thisRowValue = item.typeId;
		if(!isNull(thisRowValue) && thisRowValue != '请选择') {
			if(inPointArray(thisRowValue, assetArticles)) {
				//类型对应的用品存在js对象中
				var list = getListPointArray(thisRowValue, assetArticles);
				//重置选择行的用品列表
				var sHtml = getDataUseHandlebars(selOption, {rows: list});
				$("#assetarId" + thisRowNum).html(sHtml); //重置商品列表下拉框
				$("#assetarId" + thisRowNum).val(item.articleId);
				form.render('select');
			} else {
				//类型对应的用品不存在js对象中
doc_wei's avatar
doc_wei 已提交
252
				AjaxPostUtil.request({url: flowableBasePath + "assetarticles018", params: {typeId: thisRowValue}, type: 'json', callback: function(json) {
doc_wei's avatar
doc_wei 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
					if(json.returnCode == 0) {
						assetArticles.push({
							id: thisRowValue,
							list: json.rows
						});
						//重置选择行的用品列表
						var sHtml = getDataUseHandlebars(selOption, json);
						$("#assetarId" + thisRowNum).html(sHtml); //重置商品列表下拉框
						$("#assetarId" + thisRowNum).val(item.articleId);
						form.render('select');
					} else {
						winui.window.msg(json.returnMessage, {icon: 2, time: 2000});
					}
				}, async: false});
			}
		}

		form.render('select');
		form.render('checkbox');
		rowNum++;
	}

	//新增行
	function addRow() {
		var par = {
			id: "row" + rowNum.toString(), //checkbox的id
			trId: "tr" + rowNum.toString(), //行的id
			typeId: "typeId" + rowNum.toString(), //类型id
			assetarId: "assetarId" + rowNum.toString(), //用品id
			specificationsName: "specificationsName" + rowNum.toString(), //规格id
			residualNum: "residualNum" + rowNum.toString(), //库存id
			useNum: "useNum" + rowNum.toString(), //领用数量id
			remark: "remark" + rowNum.toString() //备注id
		};
		$("#useTable").append(getDataUseHandlebars(usetableTemplate, par));
		//赋值给用品类别
		$("#" + "typeId" + rowNum.toString()).html(typeHtml);
		form.render('select');
		form.render('checkbox');
		rowNum++;
	}

	//删除行
	function deleteRow() {
		var checkRow = $("#useTable input[type='checkbox'][name='tableCheckRow']:checked");
		if(checkRow.length > 0) {
			$.each(checkRow, function(i, item) {
				$(item).parent().parent().remove();
			});
		} else {
			winui.window.msg('请选择要删除的行', {icon: 2, time: 2000});
		}
	}

	//根据类型重置用户列表
	function resetAssetList(thisRowNum, list) {
		var sHtml = getDataUseHandlebars(selOption, {
			rows: list
		});
		$("#assetarId" + thisRowNum).html(sHtml); //重置商品列表下拉框
		$("#specificationsName" + thisRowNum).html(""); //重置规格为空
		$("#residualNum" + thisRowNum).html(""); //重置库存为空
		form.render('select');
	}

	//判断是否在数组中
	function inPointArray(str, array) {
		var isIn = false;
		$.each(array, function(i, item) {
			if(item.id === str) {
				isIn = true;
				return false;
			}
		});
		return isIn;
	}

	//获取指定key对应的集合
	function getListPointArray(str, array) {
		var isList = [];
		$.each(array, function(i, item) {
			if(item.id === str) {
				$.extend(true, isList, item.list);
				return false;
			}
		});
		return isList;
	}

	$("body").on("click", "#cancle", function() {
		parent.layer.close(index);
	});
});