advance_audit.js 11 KB

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