advance_audit.js 14 KB

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