405 lines
17 KiB
JavaScript
405 lines
17 KiB
JavaScript
// 全局变量
|
||
let idParam, objParam;
|
||
let fileList = []; // 附件列表
|
||
let delIdArr = []; // 删除旧附件ID
|
||
let jjDataArr = []; // 计划明细
|
||
let allDataList = []; // 派车明细
|
||
let planId = ''; // 当前选择计划ID
|
||
|
||
let form, laydate, layer, upload, table, util;
|
||
let pageNum = 1, tableIns;
|
||
|
||
// ------------------- 初始化参数 -------------------
|
||
function setParams(obj) {
|
||
objParam = JSON.parse(obj);
|
||
delIdArr = [];
|
||
layui.use(['form', 'layer', 'laydate', 'upload', 'table'], function () {
|
||
form = layui.form;
|
||
layer = layui.layer;
|
||
laydate = layui.laydate;
|
||
upload = layui.upload;
|
||
table = layui.table;
|
||
util = layui.util;
|
||
|
||
laydate.render({elem: '#fkTime'});
|
||
form.verify();
|
||
form.on('submit(formData)', function (data) {
|
||
submitApply(data);
|
||
});
|
||
form.render();
|
||
|
||
// 初始化上传
|
||
let uploadObj = upload.render({
|
||
elem: '#test2',
|
||
multiple: true,
|
||
dataType: "json",
|
||
exts: 'jpg|png|jpeg|doc|docx|pdf|xlsx|xls',
|
||
acceptMime: 'image/jpg,image/png,image/jpeg,application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||
number: 5,
|
||
size: 10240,
|
||
auto: false,
|
||
bindAction: '#hideUpload',
|
||
choose: function (obj) {
|
||
let existCount = fileList.length;
|
||
if (existCount >= 5) {
|
||
return layer.msg('最多上传5个附件证明', {icon: 7});
|
||
}
|
||
uploadObj.config.elem.next()[0].value = '';
|
||
obj.preview(function (index, file, result) {
|
||
if (fileList.length >= 5) return;
|
||
const uid = 'new_' + Date.now() + '_' + Math.random();
|
||
fileList.push({uid, name: file.name, file, isExist: false, fileId: null});
|
||
$('#uploader-list').append(`
|
||
<div class="file-iteme" data-uid="${uid}">
|
||
<div class="handle">x</div>
|
||
${handleFileType(index, file, result)}
|
||
</div>
|
||
`);
|
||
});
|
||
}
|
||
});
|
||
|
||
getBalanceDataDetails();
|
||
});
|
||
}
|
||
|
||
// ------------------- 获取数据详情 -------------------
|
||
function getBalanceDataDetails() {
|
||
let encryptedData = {id: objParam.id};
|
||
let url = dataUrl + 'backstage/carBalance/getBalanceDataDetails?encryptedData=' + encodeURIComponent(JSON.stringify(encryptedData));
|
||
ajaxRequest(url, "GET", null, true, null, function (result) {
|
||
if (result.code === 200) {
|
||
let dataObj = result.data;
|
||
|
||
// 基本信息回显
|
||
$('#supId').val(dataObj.supId);
|
||
$('#supName').val(dataObj.supName);
|
||
$('#fkTime').val(util.toDateString(dataObj.fkTime, 'yyyy-MM-dd'));
|
||
$('#money').val(dataObj.money);
|
||
$('#remark').val(dataObj.remark);
|
||
$('#planListNum').val(dataObj.planList.length);
|
||
|
||
// 附件回显
|
||
fileList = [];
|
||
delIdArr = [];
|
||
$('#uploader-list').html('');
|
||
if (dataObj.fileList && dataObj.fileList.length > 0) {
|
||
dataObj.fileList.forEach((f, i) => {
|
||
const uid = 'old_' + f.id;
|
||
const path = fileUrl + f.fileUrl + '?token=' + sessionStorage.getItem("gz-token");
|
||
// 从 suffix 取文件后缀
|
||
const ext = f.suffix ? f.suffix.replace('.', '').toLowerCase() : '';
|
||
fileList.push({uid, name: f.fileName, isExist: true, fileId: f.id, file: null});
|
||
$('#uploader-list').append(`
|
||
<div class="file-iteme" data-uid="${uid}" data-file-id="${f.id}">
|
||
<div class="handle">x</div>
|
||
${handleFileType(i, {name: f.fileName, ext: ext}, path)}
|
||
</div>
|
||
`);
|
||
});
|
||
}
|
||
|
||
// 计划明细回显
|
||
planDetail(dataObj.planList);
|
||
|
||
// 用车明细回显
|
||
carDetail(dataObj.detailsList);
|
||
|
||
// 初始化全局数据
|
||
allDataList = dataObj.detailsList || [];
|
||
jjDataArr = dataObj.planList || [];
|
||
}
|
||
}, function (xhr, status, error) {
|
||
errorFn(xhr, status, error);
|
||
}, null);
|
||
}
|
||
|
||
// ------------------- 计划明细 -------------------
|
||
function planDetail(list) {
|
||
$('#plan-detail-table tr:not(:first)').remove();
|
||
if (list.length > 0) {
|
||
let html = '';
|
||
list.forEach((item, index) => {
|
||
html += `<tr>
|
||
<td>${index + 1}</td>
|
||
<td>${item.proName}</td>
|
||
<td>${item.code}</td>
|
||
<td>${item.supName}</td>
|
||
<td>${item.carNum}</td>
|
||
<td>${item.money}</td>
|
||
<td>${item.ygMoney}</td>
|
||
<td><div class="handle"><a style="color:#f56c6c;cursor:pointer;" onclick="delPlanDetail(${index})">删除</a></div></td>
|
||
</tr>`;
|
||
});
|
||
$('#plan-detail-table').append(html);
|
||
}
|
||
}
|
||
|
||
// ------------------- 删除计划明细 -------------------
|
||
function delPlanDetail(index) {
|
||
let planList = $('#plan-detail-table tr:not(:first)');
|
||
if (planList.length <= 1) {
|
||
return layer.msg('计划明细至少保留1条数据,无法删除!', {icon: 7});
|
||
}
|
||
|
||
let currentTr = planList.eq(index);
|
||
let delCode = currentTr.find('td:eq(2)').text().trim();
|
||
if (!delCode) return layer.msg('未获取到需求计划编号,无法删除!', {icon: 2});
|
||
|
||
layer.confirm('确定删除该计划明细及对应的用车明细数据吗?', {icon: 3, title: '提示'}, function (layIndex) {
|
||
currentTr.remove();
|
||
$('#plan-detail-table tr:not(:first)').each(function (i) {
|
||
$(this).find('td:eq(0)').text(i + 1);
|
||
});
|
||
deleteCarDetailByCode(delCode);
|
||
jjDataArr = jjDataArr.filter(item => item.code !== delCode);
|
||
allDataList = allDataList.filter(item => item.planCode !== delCode);
|
||
layer.close(layIndex);
|
||
});
|
||
}
|
||
|
||
// ------------------- 删除用车明细 -------------------
|
||
function deleteCarDetailByCode(delCode) {
|
||
['#dispatch-car-table', '#dispatch-car-table2'].forEach(tableId => {
|
||
$(`${tableId} tr:not(:first)`).each(function () {
|
||
let $tr = $(this);
|
||
let code = $tr.find('td:last a').text().trim() || $tr.find('td:last').text().replace(/<.*?>/g, '').trim();
|
||
if (code === delCode) $tr.remove();
|
||
});
|
||
if ($(`${tableId} tr:not(:first)`).length === 0) {
|
||
$(`${tableId}`).css('display', 'none');
|
||
}
|
||
});
|
||
}
|
||
|
||
// ------------------- 用车明细 -------------------
|
||
function carDetail(list) {
|
||
const carList = list.filter(item => item.type === '车辆');
|
||
const dcList = list.filter(item => item.type === '吊车');
|
||
setDispatchCarTable(carList);
|
||
setDispatchCarTable2(dcList);
|
||
|
||
function setDispatchCarTable(list) {
|
||
$('#dispatch-car-table tr:not(:first)').remove();
|
||
if (list.length > 0) {
|
||
let html = '';
|
||
list.forEach(item => {
|
||
const imgNum = (item.carImage.filter(i => i.type !== '6').length) + (item.driverUserImage.filter(i => i.type !== '6').length) + item.fileList.length;
|
||
const actualGls = formatToTwoDecimals(item.exeGls || 0);
|
||
html += `<tr>
|
||
<td>${item.type}</td>
|
||
<td>${item.name}</td>
|
||
<td>${item.model}</td>
|
||
<td>${item.carNum}</td>
|
||
<td>${item.ton}吨</td>
|
||
<td>${item.goodsName}</td>
|
||
<td>${item.startAddress}</td>
|
||
<td>${item.endAddress}</td>
|
||
<td><input type='number' class='form-control actual-gls' value='${actualGls}' style='width:100%;padding:2px;border:1px solid #ddd;' data-id='${item.id}'></td>
|
||
<td>${item.glsPrice}</td>
|
||
<td>¥ ${item.glsMoney || 0}</td>
|
||
<td style='color:#409eff'>${imgNum}<a style='color:#409eff;margin:0 5px;cursor:pointer;' onclick='viewFileDetail(${safeStringify(item)},1)'>查看附件>></a></td>
|
||
<td><a style='color:#409eff;margin:0 5px;cursor:pointer;' onclick='viewPlanDetail(${safeStringify(item)})'>${item.planCode}</a></td>
|
||
<td>${item.remark}</td>
|
||
</tr>`;
|
||
});
|
||
$('#dispatch-car-table').removeAttr('style').append(html);
|
||
} else {
|
||
$('#dispatch-car-table').css('display', 'none');
|
||
}
|
||
}
|
||
|
||
function setDispatchCarTable2(list) {
|
||
$('#dispatch-car-table2 tr:not(:first)').remove();
|
||
if (list.length > 0) {
|
||
let html = '';
|
||
list.forEach(item => {
|
||
const imgNum = item.driverUserImage.filter(i => ['2', '3'].includes(i.type)).length + item.operaImage.filter(i => ['2', '3', '6'].includes(i.type)).length + item.fileList.length;
|
||
const actualPlanDay = item.exeDay || 0;
|
||
const actualDcMoney = formatToTwoDecimals(item.money || 0);
|
||
html += `<tr>
|
||
<td>${item.type}</td>
|
||
<td>${item.name}</td>
|
||
<td>${item.model}</td>
|
||
<td>${item.carNum}</td>
|
||
<td>${item.useAddress}</td>
|
||
<td><input type='number' class='form-control actual-dc-planDay' value='${actualPlanDay}' style='width:100%;padding:2px;border:1px solid #ddd;' data-id='${item.id}'></td>
|
||
<td>${item.dcUnit === '元/月/台' ? item.monthPrice + '(' + item.dcUnit + ')' : item.dayPrice + '(' + item.dcUnit + ')'}${item.isOutSet === 1 ? '<br/>' + (item.jcMoney || 0) + '(进出场费)' : ''}</td>
|
||
<td>¥ ${item.dcMoney}</td>
|
||
<td><input type='number' class='form-control actual-dc-money' value='${actualDcMoney}' style='width:100%;padding:2px;border:1px solid #ddd;' data-id='${item.id}'></td>
|
||
<td style='color:#409eff'>${imgNum}<a style='color:#409eff;margin:0 5px;cursor:pointer;' onclick='viewFileDetail(${safeStringify(item)},2)'>查看附件>></a></td>
|
||
<td><a style='color:#409eff;margin:0 5px;cursor:pointer;' onclick='viewPlanDetail(${safeStringify(item)})'>${item.planCode}</a></td>
|
||
<td>${item.remark}</td>
|
||
</tr>`;
|
||
});
|
||
$('#dispatch-car-table2').removeAttr('style').append(html);
|
||
} else {
|
||
$('#dispatch-car-table2').css('display', 'none');
|
||
}
|
||
}
|
||
}
|
||
|
||
// ------------------- 安全序列化 -------------------
|
||
function safeStringify(obj) {
|
||
const cache = new Set();
|
||
return JSON.stringify(obj, (key, value) => {
|
||
if (typeof value === 'object' && value !== null) {
|
||
if (cache.has(value)) return;
|
||
cache.add(value);
|
||
}
|
||
return value;
|
||
});
|
||
}
|
||
|
||
// ------------------- 文件类型显示 -------------------
|
||
function handleFileType(index, file, result) {
|
||
let html = '', img = '';
|
||
if (file.ext) file.ext = file.ext.toLowerCase();
|
||
if (file.name) file.name = file.name.toLowerCase();
|
||
if (file.ext === 'doc' || file.ext === 'docx') {
|
||
img = '../../../images/docx.png';
|
||
} else if (file.ext === 'xls' || file.ext === 'xlsx') {
|
||
img = '../../../images/xlsx.png';
|
||
} else if (file.ext === 'pdf') {
|
||
img = '../../../images/pdf.png';
|
||
} else {
|
||
return `<img class="img" style="width:130px;height:120px;" data-index=${index} data-name=${file.name} src=${result}>`;
|
||
}
|
||
html = `<div class="layout upload-file" data-index=${index}>
|
||
<img src="${img}">
|
||
<p style="text-align:center;font-size:12px;">${file.name}</p>
|
||
</div>`;
|
||
return html;
|
||
}
|
||
|
||
// ------------------- 删除附件 -------------------
|
||
$(document).on("click", ".file-iteme .handle", function (e) {
|
||
e.stopPropagation();
|
||
const $item = $(this).closest('.file-iteme');
|
||
const uid = $item.attr('data-uid');
|
||
const fileId = $item.attr('data-file-id');
|
||
if (fileId && !delIdArr.includes(fileId)) delIdArr.push(fileId);
|
||
fileList = fileList.filter(f => f.uid !== uid);
|
||
$item.remove();
|
||
});
|
||
|
||
// ------------------- 获取编辑后的实际值 -------------------
|
||
function getEditedActualValues() {
|
||
let actualValues = {carActualList: [], craneActualList: []};
|
||
$('#dispatch-car-table .actual-gls').each(function () {
|
||
const id = $(this).data('id');
|
||
const val = formatToTwoDecimals($(this).val());
|
||
actualValues.carActualList.push({outDetailId: id, exeGls: val, inMoney: 0});
|
||
});
|
||
$('#dispatch-car-table2 tr:not(:first)').each(function () {
|
||
const id = $(this).find('.actual-dc-money').data('id');
|
||
if (!id) return;
|
||
const day = $(this).find('.actual-dc-planDay').val() || 0;
|
||
const money = formatToTwoDecimals($(this).find('.actual-dc-money').val());
|
||
actualValues.craneActualList.push({outDetailId: id, exeDay: day, inMoney: money});
|
||
});
|
||
return actualValues;
|
||
}
|
||
|
||
// ------------------- 格式化两位小数 -------------------
|
||
function formatToTwoDecimals(num) {
|
||
return isNaN(Number(num)) ? '0.00' : Number(num).toFixed(2);
|
||
}
|
||
|
||
// ------------------- 正则校验输入框 -------------------
|
||
$(document).on('input', '.actual-gls,.actual-money,.actual-dc-money', function () {
|
||
this.value = this.value.replace(/[^0-9.]/g, '').replace(/\.{2,}/g, '.').replace(/^0+(?=\d)/, '');
|
||
if (this.value.indexOf('.') !== -1) {
|
||
const parts = this.value.split('.');
|
||
this.value = parts[0] + '.' + parts[1].substring(0, 2);
|
||
}
|
||
const numValue = Number(this.value || 0);
|
||
if (numValue > 100000) this.value = (100000).toFixed(2);
|
||
});
|
||
$(document).on('blur', '.actual-gls,.actual-money,.actual-dc-money', function () {
|
||
this.value = formatToTwoDecimals(this.value);
|
||
});
|
||
|
||
// ------------------- 提交 -------------------
|
||
function submitApply(data) {
|
||
if (fileList.length === 0) return layer.msg('请上传付款单', {icon: 7});
|
||
if (jjDataArr.length === 0) return layer.msg('请选择需求计划', {icon: 7});
|
||
if (allDataList.length === 0) return layer.msg('用车明细数据为空,无法提交', {icon: 7});
|
||
|
||
data.field.supId = objParam.supId;
|
||
data.field.id = objParam.id;
|
||
data.field.delFileId = delIdArr.join(',');
|
||
|
||
let planList = [], detailsList = [];
|
||
jjDataArr.forEach(item => {
|
||
planList.push({
|
||
id: item.id || '',
|
||
planId: item.planId || item.id,
|
||
supId: item.supId,
|
||
money: item.money,
|
||
type: item.type || '1',
|
||
proId: item.proId || '',
|
||
carNum: item.carNum || item.needNum || 0
|
||
});
|
||
});
|
||
|
||
const actualValues = getEditedActualValues();
|
||
allDataList.forEach(item => {
|
||
const carActual = actualValues.carActualList.find(v => v.outDetailId == item.id);
|
||
const craneActual = actualValues.craneActualList.find(v => v.outDetailId == item.id);
|
||
detailsList.push({
|
||
id: item.id || '',
|
||
planId: item.planId || '',
|
||
type: item.type || '车辆',
|
||
supId: item.supId || objParam.supId,
|
||
proId: item.proId || '',
|
||
money: item.cost || item.money || 0,
|
||
outId: item.outId || '',
|
||
exeGls: carActual?.exeGls || item.exeGls || '',
|
||
inMoney: carActual?.inMoney || craneActual?.inMoney || '',
|
||
exeDay: craneActual?.exeDay || item.exeDay || '',
|
||
outDetailId: item.outDetailId || '',
|
||
modelId: item.modelId || '',
|
||
ton: item.ton || 0
|
||
});
|
||
});
|
||
|
||
data.field.planList = planList;
|
||
data.field.detailsList = detailsList;
|
||
|
||
let formData = new FormData();
|
||
fileList.forEach(item => {
|
||
if (!item.isExist && item.file) formData.append("file[]", item.file);
|
||
});
|
||
formData.append('params', JSON.stringify(data.field));
|
||
|
||
let loadingMsg = layer.msg('正在提交保存,请稍等...', {icon: 16, shade: 0.01, time: '0'});
|
||
let url = dataUrl + 'backstage/carBalance/updateBalanceData';
|
||
ajaxRequestByUploadFile(url, formData, function () {
|
||
$('.save,.cancel').addClass("layui-btn-disabled").attr("disabled", true);
|
||
}, function (result) {
|
||
layer.close(loadingMsg);
|
||
$('.save,.cancel').removeClass("layui-btn-disabled").attr("disabled", false);
|
||
if (result.code === 200) {
|
||
parent.layer.msg(result.msg, {icon: 1});
|
||
closePage(1);
|
||
} else layer.msg(result.msg, {icon: 2});
|
||
}, function (xhr, status, error) {
|
||
layer.close(loadingMsg);
|
||
layer.msg('服务异常,请稍后重试', {icon: 16, scrollbar: false, time: 2000});
|
||
$('.save,.cancel').removeClass("layui-btn-disabled").attr("disabled", false);
|
||
errorFn(xhr, status, error);
|
||
}, null);
|
||
}
|
||
|
||
function saveData2() {
|
||
$('#formSubmit').trigger('click')
|
||
}
|
||
|
||
// ------------------- 关闭页面 -------------------
|
||
function closePage(type) {
|
||
let index = parent.layer.getFrameIndex(window.name);
|
||
if (type == 1) window.parent.reloadData();
|
||
parent.layer.close(index);
|
||
}
|