advance_audit.js 15 KB

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