advance_audit.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author lanjianrong
  6. * @date 2020/8/7
  7. * @version
  8. */
  9. $(document).ready(function () {
  10. autoFlashHeight()
  11. let oldVal = null
  12. let timer = null
  13. let oldSearchVal = null
  14. handleFileList(fileList)
  15. // 控制上报弹窗的文案
  16. function checkModal(isHide) {
  17. $('#start-modal').empty()
  18. if (isHide) {
  19. // 隐藏上传按钮
  20. $('#start-modal').append('<p class="text-danger">无法上报,请设置审批流程。</p>')
  21. $('#tm-submit').hide()
  22. } else {
  23. $('#start-modal').append(`<p>确认上报第${advance.order}期${advance.type === 0 ? '开工' : '材料'}预付款?</p>`)
  24. $('#tm-submit').show()
  25. }
  26. }
  27. $('#gr-search').bind('input propertychange', function(e) {
  28. oldSearchVal = e.target.value
  29. timer && clearTimeout(timer)
  30. timer = setTimeout(() => {
  31. const newVal = $('#gr-search').val()
  32. let html = ''
  33. if (newVal && newVal === oldSearchVal) {
  34. accountList.filter(item => item && cur_uid !== item.id && (item.name.indexOf(newVal) !== -1 || (item.mobile && item.mobile.indexOf(newVal) !== -1))).forEach(item => {
  35. html += `<dd class="border-bottom p-2 mb-0 " data-id="${item.id}" >
  36. <p class="mb-0 d-flex"><span class="text-primary">${item.name}</span><span
  37. class="ml-auto">${item.mobile || ''}</span></p>
  38. <span class="text-muted">${item.role || ''}</span>
  39. </dd>`
  40. })
  41. $('.book-list').empty()
  42. $('.book-list').append(html)
  43. } else {
  44. if (!$('.acc-btn').length) {
  45. accountGroup.forEach((group, idx) => {
  46. if (!group) return
  47. html += `<dt><a href="javascript: void(0);" class="acc-btn" data-groupid="${idx}" data-type="hide"><i class="fa fa-plus-square"></i>
  48. </a> ${group.groupName}</dt>
  49. <div class="dd-content" data-toggleid="${idx}">`
  50. group.groupList.forEach(item => {
  51. if (item.id !== cur_uid) {
  52. html += `<dd class="border-bottom p-2 mb-0 " data-id="${item.id}" >
  53. <p class="mb-0 d-flex"><span class="text-primary">${item.name}</span><span
  54. class="ml-auto">${item.mobile || ''}</span></p>
  55. <span class="text-muted">${item.role || ''}</span>
  56. </dd>`
  57. }
  58. });
  59. html += '</div>'
  60. })
  61. $('.book-list').empty()
  62. $('.book-list').append(html)
  63. }
  64. }
  65. }, 400);
  66. })
  67. // 添加审批流程按钮逻辑
  68. $('.book-list').on('click', 'dt', function () {
  69. const idx = $(this).find('.acc-btn').attr('data-groupid')
  70. const type = $(this).find('.acc-btn').attr('data-type')
  71. if (type === 'hide') {
  72. $(this).parent().find(`div[data-toggleid="${idx}"]`).show(() => {
  73. $(this).children().find('i').removeClass('fa-plus-square').addClass('fa-minus-square-o')
  74. $(this).find('.acc-btn').attr('data-type', 'show')
  75. })
  76. } else {
  77. $(this).parent().find(`div[data-toggleid="${idx}"]`).hide(() => {
  78. $(this).children().find('i').removeClass('fa-minus-square-o').addClass('fa-plus-square')
  79. $(this).find('.acc-btn').attr('data-type', 'hide')
  80. })
  81. }
  82. return false
  83. })
  84. // 添加到审批流程中
  85. $('dl').on('click', 'dd', function () {
  86. const id = parseInt($(this).data('id'))
  87. if (id !== 0) {
  88. postData(preUrl + '/audit/add', { auditorId: id }, (datas) => {
  89. // <p class="m-0 ml-2"><small class="text-muted">中交第一公路工程局有限公司国道311线满别公路施工一分部</small></p>
  90. const html = [];
  91. // 如果是重新上报,添加到重新上报列表中
  92. const auditorshtml = [];
  93. for (const [index,data] of datas.entries()) {
  94. if (index !== 0) {
  95. html.push('<li class="list-group-item" auditorId="'+ data.audit_id +'">');
  96. if (shenpi_status === shenpiConst.sp_status.sqspr || (shenpi_status === shenpiConst.sp_status.gdzs && index+1 !== datas.length)) {
  97. html.push('<a href="javascript: void(0)" class="text-danger pull-right">移除</a>');
  98. }
  99. html.push('<span>');
  100. html.push(data.order + ' ');
  101. html.push(data.name + ' ');
  102. html.push('</span>');
  103. html.push('<small class="text-muted">');
  104. html.push(data.role);
  105. html.push('</small>');
  106. html.push(`<p class="m-0 ml-2"><small class="text-muted">${data.company}</small></p></li>`);
  107. }
  108. // 添加新审批人流程修改
  109. auditorshtml.push('<li class="list-group-item" data-auditorid="' + data.audit_id + '">');
  110. auditorshtml.push('<i class="fa ' + (index+1 === datas.length ? 'fa-stop-circle' : 'fa-chevron-circle-down') + '"></i> ');
  111. auditorshtml.push(data.name + ' <small class="text-muted">' + data.role + '</small>');
  112. if (index === 0) {
  113. auditorshtml.push('<span class="pull-right">原报</span>');
  114. } else if (index+1 === datas.length) {
  115. auditorshtml.push('<span class="pull-right">终审</span>');
  116. } else {
  117. auditorshtml.push('<span class="pull-right">'+ transFormToChinese(index) +'审</span>');
  118. }
  119. auditorshtml.push('</li>');
  120. }
  121. $('#auditors').html(html.join(''));
  122. $('#auditors2').html(auditorshtml.join(''));
  123. // if ($('.fa-stop-circle').length) {
  124. // $('.fa-stop-circle').removeClass('fa-stop-circle').addClass('fa-chevron-circle-down')
  125. // }
  126. // const auditorsHTML = `<li class="list-group-item" data-auditorId='${data.audit_id}'><i class="fa fa fa-stop-circle" ></i> ${data.name} <small class="text-muted">${data.role}</small></li>`
  127. // $('#auditors2').append(auditorsHTML)
  128. if ($('#auditors')[0].children.length > 0) {
  129. checkModal(false)
  130. }
  131. });
  132. }
  133. });
  134. // 删除审批人
  135. $('body').on('click', '#auditors li>a', function () {
  136. const li = $(this).parent()
  137. const data = {
  138. auditorId: parseInt(li.attr('auditorId')),
  139. };
  140. postData(preUrl + '/audit/delete', data, (result) => {
  141. li.remove();
  142. for (const rst of result) {
  143. const aLi = $('li[auditorId=' + rst.audit_id + ']');
  144. $('span', aLi).text(rst.order + ' ' + rst.name + ' ')
  145. }
  146. // 删除左边审核人
  147. $(`#auditors2 li[data-auditorid='${data.auditorId}']`).remove();
  148. if ($('#auditors2 li').length !== 0 && !$('#auditors2 li i').hasClass('fa-stop-circle')) {
  149. $('#auditors2 li').eq($('#auditors2 li').length-1).children('i')
  150. .removeClass('fa-chevron-circle-down').addClass('fa-stop-circle');
  151. }
  152. for (let i = 0; i < $('#auditors2 li').length; i++) {
  153. $('#auditors2 li').eq(i).find('.pull-right').text(i === 0 ? '原报' : (i+1 === $('#auditors2 li').length ? '终' : transFormToChinese(i)) + '审');
  154. }
  155. })
  156. });
  157. $('#au-btn').on('click','a', function() {
  158. const content = $(this).data('target')
  159. switch (content) {
  160. case '#sub-sp':
  161. if ($('#auditors')[0].children.length) {
  162. checkModal(false)
  163. } else {
  164. checkModal(true)
  165. }
  166. break;
  167. default:
  168. break;
  169. }
  170. })
  171. // 上报审批
  172. $('#tm-submit').click(function() {
  173. const pay_ratio = parseFloat($(`.pay-input[data-type=0]`).val())
  174. const cur_amount = parseFloat(parseFloat($(`.pay-input[data-type=1]`).val()).toFixed(decimal))
  175. if (!pay_ratio || !cur_amount) {
  176. return toastr.error('请填写本期金额!')
  177. }
  178. const prev_amount = prevAdvance && prevAdvance.prev_total_amount || 0
  179. const prev_total_amount = ZhCalc.add(cur_amount, prev_amount)
  180. const remark = filterText($('#ad-remark').val())
  181. const data = {pay_ratio, cur_amount, prev_amount, prev_total_amount, remark, status: auditConst.status.checking}
  182. postData(preUrl + '/audit/start', data, (data) => {
  183. window.location.reload()
  184. }, () => {
  185. window.location.reload()
  186. })
  187. })
  188. // 重新上报
  189. $('#sp-list2-btn').click(() => {
  190. $('#tm-submit').trigger('click')
  191. })
  192. // 转化成两位小数
  193. function fixedToSub(s, decimal = 2) {
  194. return parseFloat(parseFloat(s).toFixed(decimal))
  195. }
  196. // 自动转换支付比例和本期金额
  197. $('.pay-input').on('change', function(e) {
  198. let val = parseFloat(e.target.value)
  199. const p_amount = prevAdvance && prevAdvance.prev_total_amount || 0 // 截止本期金额
  200. const re_amount = ZhCalc.sub(advancePayTotal, p_amount) // 剩余未付款的总额
  201. const min = parseFloat($(this).attr('min'))
  202. const max = parseFloat($(this).attr('max'))
  203. const type = parseInt($(this).data('type'))
  204. let pay_ratio = null
  205. let cur_amount = null
  206. if (val < min) {
  207. // 限制最小值为min
  208. $(this).val(min)
  209. val = min
  210. }
  211. if (max && val > max) {
  212. // 限制最大值为max
  213. $(this).val(max)
  214. val = max
  215. }
  216. // 本期金额转化
  217. if (type === 1) {
  218. if (val > re_amount) {
  219. // 限制不能超过最大值
  220. val = re_amount
  221. }
  222. $(this).val(fixedToSub(val, decimal)) // 重新赋值限制只有两位小数
  223. const pay_a_input = $(`.pay-input[data-type=${reverse(type)}]`)
  224. pay_ratio = parseFloat(ZhCalc.mul(ZhCalc.div(val, advancePayTotal), 100).toFixed(2))
  225. cur_amount = ZhCalc.round(val, decimal)
  226. pay_a_input.val(pay_ratio)
  227. const total = parseFloat(ZhCalc.add(cur_amount, p_amount)).toString().split('.')[1]
  228. // 截止本期金额文案更新
  229. $('#p_total2').text(formatMoney(ZhCalc.add(cur_amount, p_amount), ',', total && total.length || 0) + '元')
  230. } else {
  231. // 支付比例转化
  232. val = fixedToSub(val)
  233. if (val.toFixed(1) === max.toFixed(1)) {
  234. val = max
  235. }
  236. $(this).val(val) // 重新赋值限制只有两位小数
  237. const cur_m_input = $(`.pay-input[data-type=${reverse(type)}]`)
  238. cur_amount = ZhCalc.round(ZhCalc.mul(advancePayTotal, ZhCalc.div(val, 100)), decimal)
  239. pay_ratio = val
  240. cur_m_input.val(parseFloat(cur_amount.toFixed(decimal)))
  241. const total = parseFloat(ZhCalc.add(cur_amount, p_amount)).toString().split('.')[1]
  242. // 截止本期金额文案更新
  243. $('#p_total2').text(formatMoney(ZhCalc.add(cur_amount, p_amount), ',', total && total.length || 0) + '元')
  244. }
  245. const data = {
  246. pay_ratio,
  247. cur_amount,
  248. }
  249. oldVal = {
  250. cur_amount: parseFloat($(`.pay-input[data-type=${1}]`).val()),
  251. remark: filterText($('#ad-remark').val())
  252. }
  253. clearTimeout(timer)
  254. timer = setTimeout(() => {
  255. if (checkInput()) {
  256. update(data)
  257. clearTimeout(timer)
  258. }
  259. }, 500);
  260. })
  261. function checkInput() {
  262. const newVal = {
  263. cur_amount: parseFloat($(`.pay-input[data-type=${1}]`).val()),
  264. remark: filterText($('#ad-remark').val())
  265. }
  266. return newVal.cur_amount === oldVal.cur_amount && newVal.remark === oldVal.remark
  267. }
  268. $('#ad-remark').on('change', function(e) {
  269. const remark = filterText(e.target.value);
  270. oldVal = {
  271. pay_ratio: parseFloat($(`.pay-input[data-type=${0}]`).val()),
  272. cur_amount: parseFloat($(`.pay-input[data-type=${1}]`).val()),
  273. remark
  274. }
  275. const data = oldVal
  276. clearTimeout(timer)
  277. timer = setTimeout(() => {
  278. if (checkInput()) {
  279. update(data)
  280. clearTimeout(timer)
  281. }
  282. }, 500);
  283. })
  284. function filterText(text) {
  285. if (!text) return null
  286. return text.replace(/(\r\n)|(\n)/g, '<br/>').replace(/\s/g, ' ')
  287. }
  288. function update(data) {
  289. postData(preUrl + '/update', data)
  290. }
  291. // $('#file-modal-target').click(function () {
  292. // $('#file-modal').trigger('click')
  293. // })
  294. $('#file-ok').click(function () {
  295. const files = Array.from($('#file-modal')[0].files)
  296. const valiData = files.map(v => {
  297. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  298. return {
  299. size: v.size,
  300. ext
  301. }
  302. })
  303. if (validateFiles(valiData)) {
  304. if (files.length) {
  305. const formData = new FormData()
  306. files.forEach(file => {
  307. formData.append('name', file.name)
  308. formData.append('size', file.size)
  309. formData.append('file', file)
  310. })
  311. postDataWithFile(preUrl + '/file/upload', formData, function (result) {
  312. handleFileList(result)
  313. $('#file-cancel').click()
  314. });
  315. }
  316. }
  317. })
  318. function handleFileList(files = []) {
  319. $('#file-content').empty()
  320. const { uncheck, checkNo } = auditConst.status
  321. const newFiles = files.map(file => {
  322. let showDel = false;
  323. if (file.uid === cur_uid) {
  324. if (!curAuditor) {
  325. advance.status === uncheck && cur_uid === advance.uid && (showDel = true)
  326. advance.status === checkNo && cur_uid === advance.uid && (showDel = true)
  327. } else {
  328. curAuditor.audit_id === cur_uid && (showDel = true)
  329. }
  330. }
  331. return {...file, showDel}
  332. })
  333. let html = `<tr><td colspan="3"><a href="#addfujian" data-toggle="modal" class="btn btn-sm btn-light text-primary" data-placement="bottom" title="" data-original-title="添加清单"><i class="fa fa-cloud-upload" aria-hidden="true"></i> 上传附件</a></td></tr>`
  334. newFiles.forEach((file, idx) => {
  335. if (file.showDel) {
  336. html += `<tr><td width="70">${idx + 1}</td><td><a href="/${file.filepath}" target="_blank">${file.filename}</a></td><td width="90"><a href="javascript: void(0);" class="text-danger file-del" data-id="${file.id}">移除</a></td></tr>`
  337. } else {
  338. html += `<tr><td width="70">${idx + 1}</td><td><a href="/${file.filepath}" target="_blank">${file.filename}</a></td><td width="90"></td></tr>`
  339. }
  340. })
  341. $('#file-content').append(html)
  342. }
  343. $('#file-content').on('click', 'a', function () {
  344. if ($(this).hasClass('file-del')) {
  345. const id = $(this).data('id')
  346. postData(preUrl + '/file/del', {id}, (result) => {
  347. handleFileList(result)
  348. })
  349. }
  350. })
  351. })
  352. /**
  353. * 校验文件大小、格式
  354. * @param {Array} files 文件数组
  355. */
  356. function validateFiles(files) {
  357. if (files.length > 10) {
  358. toastr.error('至多同时上传10个文件');
  359. return false
  360. }
  361. return files.every(file => {
  362. if (file.size > 1024 * 1024 * 30) {
  363. toastr.error('文件大小限制为30MB');
  364. return false
  365. }
  366. if (whiteList.indexOf('.' + file.ext) === -1) {
  367. toastr.error('请上传正确的格式文件');
  368. return false
  369. }
  370. return true
  371. })
  372. }
  373. function reverse(num){
  374. return 1^num
  375. }
  376. function formatMoney(s, dot = ',', decimal = 2) {
  377. if (!s) {
  378. s = 0;
  379. return s.toFixed(decimal);
  380. }
  381. s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(decimal) + '';
  382. if (!decimal) {
  383. s += '.';
  384. }
  385. const l = s.split('.')[0].split('').reverse(),
  386. r = s.split('.')[1];
  387. let t = '';
  388. for (let i = 0; i < l.length; i++) {
  389. t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? dot : '');
  390. }
  391. return t.split('').reverse().join('') + (decimal === 0 ? '' : '.' + r);
  392. }