advance_audit.js 11 KB

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