contract_detail.js 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  1. const copyBlockTag = 'zh.calc.copyBlock';
  2. const ckBillsSpread = '/contract/' + window.location.pathname.split('/')[2] + '/detail/contract-billsSelect';
  3. $(document).ready(function() {
  4. autoFlashHeight();
  5. const contractSpreadSetting = {
  6. cols: [
  7. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 100, formatter: '@', readOnly: 'readOnly.code', cellType: 'tree'},
  8. {title: '合同编号', colSpan: '1', rowSpan: '2', field: 'c_code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  9. {title: '项目名称/合同名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 240, formatter: '@', readOnly: 'readOnly.code', cellType: 'ellipsisAutoTip', scrollHeightClass: '.sjs-height-1'},
  10. {title: '创建人', colSpan: '1', rowSpan: '2', field: 'username', hAlign: 1, width: 80, formatter: '@', readOnly: true},
  11. {title: '合同金额', colSpan: '1', rowSpan: '2', field: 'total_price', hAlign: 2, width: 120, formatter: '@', readOnly: true},
  12. ],
  13. emptyRows: 0,
  14. headRows: 2,
  15. headRowHeight: [25, 25],
  16. defaultRowHeight: 21,
  17. headerFont: '12px 微软雅黑',
  18. font: '12px 微软雅黑',
  19. // readOnly: readOnly,
  20. rowHeader:[
  21. {
  22. rowHeaderType: 'circle',
  23. setting: {
  24. size: 5,
  25. indent: 16,
  26. getColor: function (index, data) {
  27. if (!data) return;
  28. if (is_admin) return;
  29. if (_.findIndex(contractTreeAudits, { contract_id: data.contract_id, uid: user_id }) !== -1) {
  30. return '#dc3545';
  31. }
  32. }
  33. },
  34. },
  35. ],
  36. localCache: {
  37. key: 'contract-spread',
  38. colWidth: true,
  39. }
  40. };
  41. const contractCol = {
  42. getValue: {
  43. status: function (data) {
  44. return data.c_code ? (data.settle_code ? '● 已结算' : data.yf_price < data.total_price ? '● 履行中' : '● 已完成') : '';
  45. }
  46. },
  47. foreColor: {
  48. status: function (data) {
  49. return data.c_code ? (data.settle_code ? '#6c757d' : data.yf_price < data.total_price ? '#da9500' : '#28a745') : '';
  50. }
  51. },
  52. readOnly: {
  53. code: function (data) {
  54. return !permission_edit || data.c_code;
  55. }
  56. }
  57. }
  58. const getStackedBarTip = function (data) {
  59. return data.stackedBarTips ? data.stackedBarTips.join('\n') : '';
  60. };
  61. if (contract_type === contractConst.type.expenses) {
  62. contractSpreadSetting.cols.push({title: '累计应付', colSpan: '1', rowSpan: '2', field: 'yf_price', hAlign: 2, width: 120, formatter: '@', readOnly: true});
  63. contractSpreadSetting.cols.push({title: '支付进度', colSpan: '1', rowSpan: '2', formatter: '@', readOnly: true, field: 'stackedBar', hAlign: 0, width: 200, cellType: 'stackedBar', stackedBarCover: true, bc_type: 'grid', getTip: getStackedBarTip});
  64. } else if (contract_type === contractConst.type.income) {
  65. contractSpreadSetting.cols.push({title: '累计应回', colSpan: '1', rowSpan: '2', field: 'yf_price', hAlign: 2, width: 120, formatter: '@', readOnly: true});
  66. contractSpreadSetting.cols.push({title: '回款进度', colSpan: '1', rowSpan: '2', formatter: '@', readOnly: true, field: 'stackedBar', hAlign: 0, width: 200, cellType: 'stackedBar', stackedBarCover: true, bc_type: 'grid', getTip: getStackedBarTip});
  67. }
  68. contractSpreadSetting.cols.push({title: '合同状态', colSpan: '1', rowSpan: '2', field: 'status', hAlign: 1, width: 100, formatter: '@', readOnly: true, getValue:'getValue.status', foreColor:'foreColor.status'});
  69. contractSpreadSetting.cols.push({title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 120, formatter: '@', readOnly: 'readOnly.code', cellType: 'ellipsisAutoTip', scrollHeightClass: '.sjs-height-1'});
  70. const contractSpread = SpreadJsObj.createNewSpread($('#contract-spread')[0]);
  71. const contractSheet = contractSpread.getActiveSheet();
  72. sjsSettingObj.setGridSelectStyle(contractSpreadSetting);
  73. // sjsSettingObj.setFxTreeStyle(contractSpreadSetting, sjsSettingObj.FxTreeStyle.jz);
  74. SpreadJsObj.initSpreadSettingEvents(contractSpreadSetting, contractCol);
  75. SpreadJsObj.initSheet(contractSheet, contractSpreadSetting);
  76. const treeSetting = {
  77. id: 'contract_id',
  78. pid: 'contract_pid',
  79. order: 'order',
  80. level: 'level',
  81. rootId: -1,
  82. keys: ['id', 'tid', 'spid'],
  83. calcFields: ['pay_price', 'total_price', 'sf_price', 'debit_price', 'yf_price'],
  84. autoExpand: 3,
  85. markExpandKey: 'contract-bills-expand' + window.location.pathname.split('/')[2] + contractConst.typeMap[contract_type],
  86. markExpandSubKey: window.location.pathname.split('/')[2] + contractConst.typeMap[contract_type],
  87. };
  88. const calcField = ['total_price', 'yf_price'];
  89. const calcFieldColor = {'total_price': '#bbb', 'yf_price': '#28a745'};
  90. const calcFieldCaption = {'total_price': '合同金额', 'yf_price': contract_type === contractConst.type.expenses ? '累计应付' : '累计应回'};
  91. treeSetting.calcFun = function (node) {
  92. if (node && node.c_code) {
  93. const base = node.total_price;
  94. node.stackedBar = [];
  95. node.stackedBarTips = [];
  96. for (const cf of calcField) {
  97. node.stackedBar.push({ color: calcFieldColor[cf], percent: ZhCalc.div(node[cf], base), field: cf });
  98. node.stackedBarTips.push(`${calcFieldCaption[cf]}: ${node[cf] || 0}`);
  99. }
  100. }
  101. };
  102. const contractTree = createNewPathTree('revise', treeSetting);
  103. const contractDetail = ['c_code', 'name', 'total_price', 'pay_price', 'debit_price','yf_price', 'sf_price', 'party_a', 'party_a_user', 'party_b', 'party_b_user', 'sign_date', 'address', 'entity', 'bank', 'bank_account', 'remark', 'settle_code', 'create_time'];
  104. // 清单 相关方法&绑定spreadjs事件
  105. const contractTreeSpreadObj = {
  106. getDefaultSelectInfo: function (sheet) {
  107. const tree = sheet.zh_tree;
  108. if (!tree) return;
  109. const sel = sheet.getSelections()[0];
  110. const node = sheet.zh_tree.nodes[sel.row];
  111. if (!node) return;
  112. let count = 1;
  113. if (sel.rowCount > 1) {
  114. for (let r = 1; r < sel.rowCount; r++) {
  115. const rNode = sheet.zh_tree.nodes[sel.row + r];
  116. if (rNode.level > node.level) continue;
  117. if ((rNode.level < node.level) || (rNode.level === node.level && rNode.contract_pid !== node.contract_pid)) {
  118. toastr.warning('请选择同一节点下的节点,进行该操作');
  119. return;
  120. }
  121. count += 1;
  122. }
  123. }
  124. return [tree, node, count];
  125. },
  126. /**
  127. * 刷新顶部按钮是否可用
  128. * @param sheet
  129. * @param selections
  130. */
  131. refreshOperationValid: function (sheet, selection) {
  132. const setObjEnable = function (obj, enable) {
  133. if (enable) {
  134. obj.removeClass('disabled');
  135. } else {
  136. obj.addClass('disabled');
  137. }
  138. };
  139. const invalidAll = function () {
  140. setObjEnable($('a[name=base-opr][type=add]'), false);
  141. setObjEnable($('a[name=base-opr][type=add-child]'), false);
  142. setObjEnable($('a[name=base-opr][type=delete]'), false);
  143. setObjEnable($('a[name=base-opr][type=up-move]'), false);
  144. setObjEnable($('a[name=base-opr][type=down-move]'), false);
  145. setObjEnable($('a[name=base-opr][type=up-level]'), false);
  146. setObjEnable($('a[name=base-opr][type=down-level]'), false);
  147. };
  148. const sel = selection ? selection[0] : sheet.getSelections()[0];
  149. const row = sel ? sel.row : -1;
  150. const tree = sheet.zh_tree;
  151. if (!tree) {
  152. invalidAll();
  153. return;
  154. }
  155. const first = sheet.zh_tree.nodes[row];
  156. if (first && permission_add && (is_admin || _.findIndex(contractTreeAudits, { uid: user_id }) === -1 || _.findIndex(contractTreeAudits, { contract_id: first.contract_id, uid: user_id }) !== -1)) {
  157. $('#add-cons-btn').show();
  158. } else {
  159. $('#add-cons-btn').hide();
  160. }
  161. if (!first) {
  162. invalidAll();
  163. return;
  164. }
  165. let last = first, sameParent = true, childCanDel = true, hadContract = this.checkContract(first);
  166. if (sel.rowCount > 1 && first) {
  167. for (let r = 1; r < sel.rowCount; r++) {
  168. const rNode = tree.nodes[sel.row + r];
  169. childCanDel = this.checkDelete(rNode, childCanDel);
  170. if (!hadContract) hadContract = this.checkContract(rNode);
  171. if (!rNode) {
  172. sameParent = false;
  173. break;
  174. }
  175. if (rNode.level > first.level) continue;
  176. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.contract_pid !== first.contract_pid)) {
  177. sameParent = false;
  178. break;
  179. }
  180. last = rNode;
  181. }
  182. }
  183. const preNode = tree.getPreSiblingNode(first);
  184. // if (!hadContract && preNode) hadContract = preNode.c_code !== undefined;
  185. // const siblings = tree.getChildren(tree.getParent(first));
  186. let upPower = true;
  187. // for (let order = last.order; order < siblings.length - 1; order ++) {
  188. // if (!siblings[order].formc) {
  189. // upPower = false;
  190. // break;
  191. // }
  192. // }
  193. const valid = !sheet.zh_setting.readOnly;
  194. setObjEnable($('a[name=base-opr][type=add]'), valid && first && first.level > 1);
  195. setObjEnable($('a[name=base-opr][type=add-child]'), valid && first && !first.c_code && first.level >= 1);
  196. setObjEnable($('a[name=base-opr][type=delete]'), valid && first && sameParent && first.level > 1 && childCanDel && this.checkDelete(first));
  197. setObjEnable($('a[name=base-opr][type=up-move]'), valid && first && sameParent && first.level > 1 && preNode);
  198. setObjEnable($('a[name=base-opr][type=down-move]'), valid && first && sameParent && first.level > 1 && !tree.isLastSibling(last));
  199. setObjEnable($('a[name=base-opr][type=up-level]'), valid && first && sameParent && tree.getParent(first)
  200. && first.level > 2 && tree.isLastSibling(last) && upPower && !hadContract);
  201. setObjEnable($('a[name=base-opr][type=down-level]'), valid && first && sameParent
  202. && first.level > 1 && preNode && (preNode.children.length > 0 || preNode.children.length === 0) && !hadContract && preNode.c_code === undefined);
  203. // setObjEnable($('#cut'), valid);
  204. // setObjEnable($('#paste'), valid);
  205. },
  206. /**
  207. *
  208. * @param sheet
  209. * @param data
  210. */
  211. refreshTree: function (sheet, data) {
  212. SpreadJsObj.massOperationSheet(sheet, function () {
  213. const tree = sheet.zh_tree;
  214. // 处理删除
  215. if (data.delete) {
  216. data.delete.sort(function (a, b) {
  217. return b.deleteIndex - a.deleteIndex;
  218. });
  219. for (const d of data.delete) {
  220. sheet.deleteRows(d.deleteIndex, 1);
  221. }
  222. }
  223. // 处理新增
  224. if (data.create) {
  225. const newNodes = data.create;
  226. if (newNodes) {
  227. newNodes.sort(function (a, b) {
  228. return a.index - b.index;
  229. });
  230. for (const node of newNodes) {
  231. sheet.addRows(node.index, 1);
  232. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  233. }
  234. }
  235. }
  236. // 处理更新
  237. if (data.update) {
  238. const rows = [];
  239. for (const u of data.update) {
  240. rows.push(tree.nodes.indexOf(u));
  241. }
  242. SpreadJsObj.reLoadRowsData(sheet, rows);
  243. }
  244. // 处理展开
  245. if (data.expand) {
  246. const expanded = [];
  247. for (const e of data.expand) {
  248. if (expanded.indexOf(e) === -1) {
  249. const posterity = tree.getPosterity(e);
  250. for (const p of posterity) {
  251. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  252. expanded.push(p);
  253. }
  254. }
  255. }
  256. }
  257. });
  258. },
  259. changeContractTab: function (node, need = false) {
  260. if ($('.bc-bar .nav li .active').attr('href') === '#htdetail') {
  261. if (node.settle_code) {
  262. $('#edit_contract_btn').hide();
  263. } else {
  264. $('#edit_contract_btn').show();
  265. }
  266. $('#save_contract_btn').hide();
  267. $('#cancel_contract_btn').hide();
  268. $('#add_contract_pay_btn').hide();
  269. $('a[href*="#cons-upfile"]').hide();
  270. if (need) {
  271. for (const c of contractDetail) {
  272. if (c === 'create_time') {
  273. $('#htdetail_' + c).text(node[c] ? moment(node[c]).format('YYYY-MM-DD HH:mm:ss') : '');
  274. continue;
  275. } else if (c === 'sign_date') {
  276. $('#htdetail_' + c).text(node[c] ? moment(node[c]).format('YYYY-MM-DD') : '');
  277. continue;
  278. }
  279. $('#htdetail_' + c).text(node[c] || '');
  280. }
  281. $('#htdetail_df_price').text(ZhCalc.sub(node.yf_price, node.sf_price) || '');
  282. }
  283. } else if ($('.bc-bar .nav li .active').attr('href') === '#htpay') {
  284. $('#edit_contract_btn').hide();
  285. $('#save_contract_btn').hide();
  286. $('#cancel_contract_btn').hide();
  287. if (node.settle_code) {
  288. $('#add_contract_pay_btn').hide();
  289. if (need) {
  290. $('#htpay-table tbody').find('.pay-edit').hide();
  291. $('#htpay-table tbody').find('.pay-del').hide();
  292. }
  293. } else {
  294. $('#add_contract_pay_btn').show();
  295. if (need && contractPays && contractPays.length > 0) {
  296. for (const [i, cp] of contractPays.entries()) {
  297. if (cp.uid === user_id) {
  298. $('#htpay-table tbody tr').eq(i).find('.pay-edit').show();
  299. $('#htpay-table tbody tr').eq(i).find('.pay-del').show();
  300. }
  301. }
  302. }
  303. }
  304. $('a[href*="#cons-upfile"]').hide();
  305. } else if ($('.bc-bar .nav li .active').attr('href') === '#htfile') {
  306. $('#edit_contract_btn').hide();
  307. $('#save_contract_btn').hide();
  308. $('#cancel_contract_btn').hide();
  309. $('#add_contract_pay_btn').hide();
  310. $('a[href*="#cons-upfile"]').show();
  311. }
  312. },
  313. openContractPayFiles: function (pay) {
  314. this.setContractFiles(pay.files, pay.cid, pay.id, '#cons-pay-file table tbody');
  315. },
  316. setContractFiles: function (files, cid, cpid = null, _this = '#htfile-table tbody') {
  317. let filesHtml = '';
  318. const newFiles = files.map(file => {
  319. let showDel = false;
  320. if (file.uid === user_id) {
  321. showDel = true
  322. }
  323. return {...file, showDel}
  324. })
  325. newFiles.forEach((file, idx) => {
  326. filesHtml += `<tr class="text-center">
  327. <td>${idx + 1}</td><td class="text-left"><a href="${file.filepath}" target="_blank">${file.filename}</a></td><td>${file.username}</td><td>${moment(file.upload_time).format('YYYY-MM-DD HH:mm:ss')}</td>
  328. <td>
  329. <div class="btn-group-table">
  330. ${file.viewpath ? `<a href="${file.viewpath}" target="_blank" class="mr-1"><i class="fa fa-eye fa-fw"></i></a>` : ''}
  331. <a href="/contract/${stid}/detail/${contractConst.typeMap[contract_type]}/${cid}${cpid ? '/pay/' + cpid : ''}/file/${file.id}/download" class="mr-1"><i class="fa fa-download fa-fw"></i></a>
  332. ${file.showDel ? `<a href="javascript: void(0);" class="text-danger file-del mr-1" data-id="${file.id}"><i class="fa fa-trash-o fa-fw text-danger"></i></a>` : ''}
  333. </div>
  334. </td>
  335. </tr>`;
  336. });
  337. $(_this).html(filesHtml);
  338. },
  339. setContractPays: function (pays, node) {
  340. contractPays = pays;
  341. let paysHtml = '';
  342. const newPays = pays.map(pay => {
  343. let showEdit = false;
  344. if (pay.uid === user_id && !node.settle_code) {
  345. showEdit = true
  346. }
  347. return {...pay, showEdit}
  348. })
  349. newPays.forEach((pay, idx) => {
  350. paysHtml += `<tr class="text-center">
  351. <td>${idx + 1}</td>
  352. <td>${moment(pay.pay_time).format('YYYY-MM-DD')}</td>
  353. <td>${pay.pay_price}</td>
  354. <td>${pay.debit_price}</td>
  355. <td>${pay.yf_price}</td>
  356. <td>${pay.sf_price}</td>
  357. <td>${pay.pay_type}</td>
  358. <td>${pay.username}</td>
  359. <td>${moment(pay.create_time).format('YYYY-MM-DD HH:mm:ss')}</td>
  360. <td>${pay.remark}</td>
  361. <td><a href="javascript:void(0);" class="text-primary open-pay-files" data-cpid="${pay.id}"><i class="fa fa-paperclip fa-rotate-90"></i></a></td>
  362. <td><a href="javascript:void(0);" class="text-primary pay-edit" data-id="${pay.id}" ${ !pay.showEdit ? `style="display:none"` : ''}>编辑</a> <a href="javascript:void(0);" class="text-danger pay-del" data-id="${pay.id}" ${ !pay.showEdit ? `style="display:none"` : ''}>删除</a></td>
  363. </tr>`;
  364. });
  365. $('#htpay-table tbody').html(paysHtml);
  366. },
  367. setContract: function (sheet) {
  368. const node = SpreadJsObj.getSelectObject(sheet);
  369. if (node && node.c_code) {
  370. $('#htdetail-table').show();
  371. $('#htpay-table').show();
  372. $('#htfile-table').show();
  373. for (const c of contractDetail) {
  374. if (c === 'create_time') {
  375. $('#htdetail_' + c).text(node[c] ? moment(node[c]).format('YYYY-MM-DD HH:mm:ss') : '');
  376. continue;
  377. } else if (c === 'sign_date') {
  378. $('#htdetail_' + c).text(node[c] ? moment(node[c]).format('YYYY-MM-DD') : '');
  379. continue;
  380. }
  381. $('#htdetail_' + c).text(node[c] || '');
  382. }
  383. $('#htdetail_df_price').text(ZhCalc.sub(node.yf_price, node.sf_price) || '');
  384. postData(window.location.pathname + '/update', {postType: 'get-contract', postData: node.id}, function (result) {
  385. contractTreeSpreadObj.setContractPays(result.pays, node);
  386. contractTreeSpreadObj.setContractFiles(result.files, node.id);
  387. });
  388. if (node.uid === user_id) {
  389. if (node.settle_code) {
  390. $('a[href*="#cons-unlock"]').show();
  391. $('a[href*="#cons-close"]').hide();
  392. } else {
  393. $('a[href*="#cons-unlock"]').hide();
  394. $('a[href*="#cons-close"]').show();
  395. }
  396. contractTreeSpreadObj.changeContractTab(node);
  397. } else {
  398. $('#edit_contract_btn').hide();
  399. $('#save_contract_btn').hide();
  400. $('#cancel_contract_btn').hide();
  401. $('a[href*="#cons-unlock"]').hide();
  402. $('a[href*="#cons-close"]').hide();
  403. $('a[href*="#cons-upfile"]').hide();
  404. $('#add_contract_pay_btn').hide();
  405. }
  406. } else {
  407. $('#htdetail-table').hide();
  408. $('#htpay-table').hide();
  409. $('#htfile-table').hide();
  410. $('#edit_contract_btn').hide();
  411. $('#save_contract_btn').hide();
  412. $('#cancel_contract_btn').hide();
  413. $('a[href*="#cons-unlock"]').hide();
  414. $('a[href*="#cons-close"]').hide();
  415. $('a[href*="#cons-upfile"]').hide();
  416. $('#add_contract_pay_btn').hide();
  417. }
  418. },
  419. selectionChanged: function (e, info) {
  420. if (info.newSelections) {
  421. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row || info.newSelections[0].rowCount !== info.oldSelections[0].rowCount) {
  422. contractTreeSpreadObj.refreshOperationValid(info.sheet);
  423. contractTreeSpreadObj.setContract(info.sheet);
  424. }
  425. } else {
  426. $('#add-cons-btn').hide();
  427. }
  428. },
  429. /**
  430. * 新增节点
  431. * @param spread
  432. */
  433. baseOpr: function (sheet, type, addCount = 1) {
  434. const self = this;
  435. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  436. if (!tree || !node || !count) return;
  437. // if (type === 'delete') {
  438. // const parent = tree.getParent(node);
  439. // const children = parent ? parent.children : tree.children;
  440. // const index = children.indexOf(node);
  441. // for (let i = 0; i < count; i++) {
  442. // const child = children[i+index];
  443. // if (tree.checkNodeUsed(child, pos)) {
  444. // toastr.warning('选中的节点已计量,不可删除');
  445. // return;
  446. // }
  447. // }
  448. // } else if (type === 'up-level') {
  449. // const parent = tree.getParent(node);
  450. // const children = parent ? parent.children : tree.children;
  451. // const index = children.indexOf(node);
  452. // const lastSelect = children[index + count - 1];
  453. // // if (tree.checkNodeUsed(lastSelect, pos) && (!lastSelect.children || lastSelect.children.length === 0) && (children.length > index + count)) {
  454. // // toastr.warning('选中的节点已计量,不可升级');
  455. // // return;
  456. // // }
  457. // } else if (type === 'down-level') {
  458. // const parent = tree.getParent(node);
  459. // const children = parent ? parent.children : tree.children;
  460. // const index = children.indexOf(node);
  461. // if (index > 0 && children[index-1].used) {
  462. // toastr.warning('其前节点已计量,选中的节点不可降级');
  463. // return;
  464. // }
  465. // for (let i = index; i < count; i++) {
  466. // const child = children[i+index];
  467. // if (tree.checkNodeUsed(child, pos)) {
  468. // toastr.warning('选中的节点已计量,不可降级');
  469. // return;
  470. // }
  471. // }
  472. // }
  473. if (type === 'delete') {
  474. deleteAfterHint(function () {
  475. postData(window.location.pathname + '/update', {
  476. postType: type,
  477. postData: {
  478. id: node.contract_id,
  479. count: type === 'add' ? addCount : count,
  480. }
  481. }, function (result) {
  482. const refreshData = tree.loadPostData(result);
  483. self.refreshTree(sheet, refreshData);
  484. const sel = sheet.getSelections()[0];
  485. if (sel) {
  486. sheet.setSelection(sel.row, sel.col, 1, sel.colCount);
  487. }
  488. self.refreshOperationValid(sheet);
  489. });
  490. }, '确认删除「当前合同或节点及子项」?');
  491. } else {
  492. postData(window.location.pathname + '/update', {
  493. postType: type,
  494. postData: {
  495. id: node.contract_id,
  496. count: type === 'add' ? addCount : count,
  497. }
  498. }, function (result) {
  499. const refreshData = tree.loadPostData(result);
  500. self.refreshTree(sheet, refreshData);
  501. if (['up-move', 'down-move'].indexOf(type) > -1) {
  502. const sel = sheet.getSelections()[0];
  503. if (sel) {
  504. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  505. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(node)]);
  506. }
  507. } else if (type === 'add' || type === 'add-child') {
  508. const sel = sheet.getSelections()[0];
  509. if (sel) {
  510. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  511. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(refreshData.create[0])]);
  512. }
  513. }
  514. self.refreshOperationValid(sheet);
  515. });
  516. }
  517. },
  518. /**
  519. * 编辑单元格响应事件
  520. * @param {Object} e
  521. * @param {Object} info
  522. */
  523. editEnded: function (e, info) {
  524. if (info.sheet.zh_setting) {
  525. const col = info.sheet.zh_setting.cols[info.col];
  526. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  527. const node = sortData[info.row];
  528. const data = {
  529. id: node.id,
  530. };
  531. // 未改变值则不提交
  532. const orgValue = node[col.field];
  533. const newValue = trimInvalidChar(info.editingText);
  534. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (newValue === ''))) {
  535. return;
  536. }
  537. // 获取更新数据
  538. if (info.editingText) {
  539. const text = newValue;
  540. data[col.field] = text;
  541. } else {
  542. data[col.field] = null;
  543. }
  544. console.log(data);
  545. // 更新至服务器
  546. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  547. const refreshNode = contractTree.loadPostData(result);
  548. contractTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  549. });
  550. }
  551. },
  552. clipboardPasting: function (e, info) {
  553. const tree = info.sheet.zh_tree, setting = info.sheet.zh_setting;
  554. info.cancel = true;
  555. if (!setting || !tree) return;
  556. const pasteData = info.pasteData.html
  557. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  558. : (info.pasteData.text === ''
  559. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  560. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  561. const datas = [], filterNodes = [];
  562. let level, filterRow = 0;
  563. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  564. const curRow = info.cellRange.row + iRow;
  565. const node = tree.nodes[curRow];
  566. if (!node) continue;
  567. if (!level) level = node.level;
  568. if (node.level < level) break;
  569. let bPaste = false;
  570. const data = info.sheet.zh_tree.getNodeKeyData(node);
  571. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  572. const curCol = info.cellRange.col + iCol;
  573. const colSetting = info.sheet.zh_setting.cols[curCol];
  574. const value = trimInvalidChar(pasteData[iRow-filterRow][iCol]);
  575. data[colSetting.field] = value;
  576. bPaste = true;
  577. }
  578. if (bPaste) {
  579. datas.push(data);
  580. } else {
  581. filterNodes.push(node);
  582. }
  583. }
  584. if (datas.length > 0) {
  585. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  586. const refreshNode = tree.loadPostData(result);
  587. if (refreshNode.update) {
  588. refreshNode.update = refreshNode.update.concat(filterNodes);
  589. }
  590. contractTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  591. }, function () {
  592. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  593. });
  594. } else {
  595. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  596. }
  597. },
  598. clipboardPasted: function (e, info) {
  599. const tree = info.sheet.zh_tree;
  600. if (!tree) { return; }
  601. const sortData = info.sheet.zh_tree.nodes;
  602. const datas = [], filterNodes = [];
  603. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  604. let bPaste = false;
  605. const curRow = info.cellRange.row + iRow;
  606. const node = sortData[curRow];
  607. if (node) {
  608. const data = info.sheet.zh_tree.getNodeKeyData(node);
  609. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  610. const curCol = info.cellRange.col + iCol;
  611. const colSetting = info.sheet.zh_setting.cols[curCol];
  612. const value = trimInvalidChar(info.sheet.getText(curRow, curCol));
  613. data[colSetting.field] = value;
  614. bPaste = true;
  615. }
  616. if (bPaste) {
  617. datas.push(data);
  618. } else {
  619. filterNodes.push(node);
  620. }
  621. }
  622. }
  623. if (datas.length > 0) {
  624. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  625. const refreshNode = tree.loadPostData(result);
  626. if (refreshNode.update) {
  627. refreshNode.update = refreshNode.update.concat(filterNodes);
  628. }
  629. contractTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  630. }, function () {
  631. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  632. });
  633. } else {
  634. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  635. }
  636. },
  637. deletePress: function (sheet) {
  638. if (!sheet.zh_setting) return;
  639. const sel = sheet.getSelections()[0], datas = [];
  640. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  641. let bDel = false;
  642. const node = sheet.zh_tree.nodes[iRow];
  643. const data = sheet.zh_tree.getNodeKeyData(node);
  644. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  645. const col = sheet.zh_setting.cols[iCol];
  646. const style = sheet.getStyle(iRow, iCol);
  647. if (style.locked) continue;
  648. data[col.field] = null;
  649. bDel = true;
  650. }
  651. if (bDel) datas.push(data);
  652. }
  653. if (datas.length > 0) {
  654. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  655. const refreshNode = sheet.zh_tree.loadPostData(result);
  656. contractTreeSpreadObj.refreshTree(sheet, refreshNode);
  657. });
  658. }
  659. },
  660. pasteBlock: function (spread, copyInfo) {
  661. const self = this;
  662. const sheet = spread.getActiveSheet();
  663. const [tree, node] = this.getDefaultSelectInfo(spread.getActiveSheet());
  664. postData(window.location.pathname + '/update', {
  665. postType: 'paste-block',
  666. postData: {
  667. id: tree.getNodeKey(node),
  668. // tid: copyInfo.tid,
  669. block: copyInfo.block,
  670. }
  671. }, function (data) {
  672. const result = tree.loadPostData(data.ledger);
  673. self.refreshTree(sheet, result);
  674. const sel = sheet.getSelections()[0];
  675. if (sel) {
  676. sheet.setSelection(tree.nodes.indexOf(result.create[0]), sel.col, sel.rowCount, sel.colCount);
  677. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(result.create[0])]);
  678. }
  679. self.refreshOperationValid(sheet);
  680. removeLocalCache(copyBlockTag);
  681. }, null, true);
  682. },
  683. topRowChanged: function (e, info) {
  684. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  685. },
  686. editStarting(e, info) {
  687. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  688. const col = info.sheet.zh_setting.cols[info.col];
  689. const node = info.sheet.zh_tree.nodes[info.row];
  690. if (!node) {
  691. info.cancel = true;
  692. return;
  693. }
  694. },
  695. cut: function (sheet, sel, callback) {
  696. if (!sheet || !sel) return;
  697. if (sel.colCount >= sheet.zh_setting.cols.length) {
  698. toastr.warning('请勿选中整行剪切');
  699. return;
  700. }
  701. const sortData = SpreadJsObj.getSortData(sheet), datas = [];
  702. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  703. const node = sortData[iRow];
  704. if (node) {
  705. const data = sheet.zh_tree.getNodeKeyData(node);
  706. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  707. const style = sheet.getStyle(iRow, iCol);
  708. if (style.locked) {
  709. toastr.error('不可剪切');
  710. return;
  711. }
  712. const colSetting = sheet.zh_setting.cols[iCol];
  713. data[colSetting.field] = null;
  714. }
  715. datas.push(data);
  716. }
  717. }
  718. if (datas.length > 0) {
  719. callback();
  720. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  721. const refreshNode = sheet.zh_tree.loadPostData(result);
  722. contractTreeSpreadObj.refreshTree(sheet, refreshNode);
  723. });
  724. }
  725. },
  726. sortCode: function (sheet) {
  727. const tree = sheet.zh_tree;
  728. if (!tree) return;
  729. const select = SpreadJsObj.getSelectObject(sheet);
  730. if (!select || !select.code || select.code === '') return;
  731. const recursiveSortCode = function (data, parentCode, children) {
  732. if (!children || children.length === 0) return;
  733. for (const [i, child] of children.entries()) {
  734. if (!child.b_code || child.b_code === '') {
  735. const code = parentCode + '-' + (i + 1);
  736. const cData = tree.getNodeKeyData(child);
  737. cData.code = code;
  738. data.push(cData);
  739. if (!tree.isLeafXmj(child)) {
  740. recursiveSortCode(data, code, child.children);
  741. }
  742. }
  743. }
  744. };
  745. const data = [];
  746. recursiveSortCode(data, select.code, select.children);
  747. if (data.length > 0) {
  748. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  749. const refreshNode = tree.loadPostData(result);
  750. contractTreeSpreadObj.refreshTree(sheet, refreshNode);
  751. })
  752. }
  753. },
  754. checkContract: function (select) {
  755. let haveContract = false;
  756. if (select.c_code) {
  757. haveContract = true;
  758. } else {
  759. const selects = contractSheet.zh_tree.getPosterity(select);
  760. const select_contracts = _.filter(selects, function (item) {
  761. return item.c_code;
  762. });
  763. if (select_contracts.length > 0) haveContract = true;
  764. }
  765. return haveContract;
  766. },
  767. checkDelete: function (select, oldFlag = true) {
  768. let canDelete = oldFlag;
  769. if (select.c_code) {
  770. if (!(select.uid === user_id || is_admin)) {
  771. canDelete = false;
  772. } else if (select.exist_pay) {
  773. canDelete = false;
  774. }
  775. } else {
  776. const selects = contractSheet.zh_tree.getPosterity(select);
  777. const select_contracts = _.filter(selects, function (item) {
  778. return item.c_code;
  779. });
  780. if (select_contracts.length > 0) {
  781. for (const sc of select_contracts) {
  782. if (!(sc.uid === user_id || is_admin)) {
  783. canDelete = false;
  784. break;
  785. } else if (sc.exist_pay) {
  786. canDelete = false;
  787. break;
  788. }
  789. }
  790. }
  791. }
  792. return canDelete;
  793. }
  794. };
  795. contractSpread.bind(spreadNS.Events.SelectionChanged, contractTreeSpreadObj.selectionChanged);
  796. contractSpread.bind(spreadNS.Events.topRowChanged, contractTreeSpreadObj.topRowChanged);
  797. // 右键菜单
  798. let batchInsertObj;
  799. $.contextMenu.types.batchInsert = function (item, opt, root) {
  800. const self = this;
  801. if ($.isFunction(item.icon)) {
  802. item._icon = item.icon.call(this, this, $t, key, item);
  803. } else {
  804. if (typeof(item.icon) === 'string' && item.icon.substring(0, 3) === 'fa-') {
  805. // to enable font awesome
  806. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '--fa fa ' + item.icon;
  807. } else {
  808. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '-' + item.icon;
  809. }
  810. }
  811. this.addClass(item._icon);
  812. const $obj = $('<div>' + item.name + '<input class="text-right ml-1 mr-1" type="tel" max="20" min="1" value="' + item.value + '" style="width: 30px; height: 18px; padding-right: 4px;">行</div>')
  813. .appendTo(this);
  814. const $input = $obj.find('input');
  815. const event = () => {
  816. if (self.hasClass('context-menu-disabled')) return;
  817. item.batchInsert($input[0], root);
  818. };
  819. $obj.on('click', event).keypress(function (e) {if (e.keyCode === 13) { event(); }});
  820. $input.click((e) => {e.stopPropagation();})
  821. .keyup((e) => {if (e.keyCode === 13) item.batchInsert($input[0], root);})
  822. .on('input', function () {this.value = this.value.replace(/[^\d]/g, '');});
  823. };
  824. const contractContextMenuOptions = {
  825. selector: '#contract-spread',
  826. build: function ($trigger, e) {
  827. const target = SpreadJsObj.safeRightClickSelection($trigger, e, contractSpread);
  828. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  829. },
  830. items: {}
  831. };
  832. if (permission_edit) {
  833. contractSpread.bind(spreadNS.Events.EditStarting, contractTreeSpreadObj.editStarting);
  834. contractSpread.bind(spreadNS.Events.EditEnded, contractTreeSpreadObj.editEnded);
  835. contractSpread.bind(spreadNS.Events.ButtonClicked, contractTreeSpreadObj.buttonClicked);
  836. contractSpread.bind(spreadNS.Events.ClipboardPasting, contractTreeSpreadObj.clipboardPasting);
  837. contractSpread.bind(spreadNS.Events.ClipboardChanging, function (e, info) {
  838. const copyText = SpreadJsObj.getFilterCopyText(info.sheet);
  839. SpreadJsObj.Clipboard.setCopyData(copyText);
  840. });
  841. SpreadJsObj.addDeleteBind(contractSpread, contractTreeSpreadObj.deletePress);
  842. SpreadJsObj.addCutEvents(contractSpread, contractTreeSpreadObj.cut);
  843. $('a[name="base-opr"]').click(function () {
  844. contractTreeSpreadObj.baseOpr(contractSheet, this.getAttribute('type'));
  845. });
  846. $('a[name=cpc]').click(function () {
  847. contractSpread.commandManager().execute({
  848. cmd: this.getAttribute('type'),
  849. sheetName: contractSheet.name()
  850. });
  851. });
  852. contractContextMenuOptions.items.create = {
  853. name: '新增节点',
  854. icon: 'fa-plus',
  855. callback: function (key, opt) {
  856. contractTreeSpreadObj.baseOpr(contractSheet, 'add');
  857. },
  858. disabled: function (key, opt) {
  859. const sheet = contractSheet;
  860. const selection = sheet.getSelections();
  861. const sel = selection ? selection[0] : sheet.getSelections()[0];
  862. const row = sel ? sel.row : -1;
  863. const tree = sheet.zh_tree;
  864. if (!tree) return true;
  865. const first = sheet.zh_tree.nodes[row];
  866. const valid = !sheet.zh_setting.readOnly;
  867. return !(valid && first && first.level > 1);
  868. }
  869. };
  870. contractContextMenuOptions.items.create3 = {
  871. name: '新增子节点',
  872. icon: 'fa-sign-in',
  873. callback: function (key, opt) {
  874. contractTreeSpreadObj.baseOpr(contractSheet, 'add-child');
  875. },
  876. disabled: function (key, opt) {
  877. const sheet = contractSheet;
  878. const selection = sheet.getSelections();
  879. const sel = selection ? selection[0] : sheet.getSelections()[0];
  880. const row = sel ? sel.row : -1;
  881. const tree = sheet.zh_tree;
  882. if (!tree) return true;
  883. const first = sheet.zh_tree.nodes[row];
  884. const valid = !sheet.zh_setting.readOnly;
  885. return !(valid && first && !first.c_code && first.level >= 1);
  886. }
  887. };
  888. }
  889. if (permission_add) {
  890. contractContextMenuOptions.items.create2 = {
  891. name: '新增合同',
  892. icon: 'fa-sign-in',
  893. callback: function (key, opt) {
  894. $('#cons-add').modal('show');
  895. },
  896. disabled: function (key, opt) {
  897. const sheet = contractSheet;
  898. const selection = sheet.getSelections();
  899. const sel = selection ? selection[0] : sheet.getSelections()[0];
  900. const row = sel ? sel.row : -1;
  901. const tree = sheet.zh_tree;
  902. if (!tree) return true;
  903. const first = sheet.zh_tree.nodes[row];
  904. const valid = !sheet.zh_setting.readOnly;
  905. return !(valid && first && first.level > 1);
  906. }
  907. };
  908. }
  909. contractContextMenuOptions.items.delete = {
  910. name: '删除',
  911. icon: 'fa-remove',
  912. callback: function (key, opt) {
  913. contractTreeSpreadObj.baseOpr(contractSheet, 'delete');
  914. },
  915. visible: function (key, opt) {
  916. const sheet = contractSheet;
  917. const selection = sheet.getSelections();
  918. const sel = selection ? selection[0] : sheet.getSelections()[0];
  919. const row = sel ? sel.row : -1;
  920. const tree = sheet.zh_tree;
  921. if (!tree) return false;
  922. const first = sheet.zh_tree.nodes[row];
  923. return (first && first.c_code) || permission_edit;
  924. },
  925. disabled: function (key, opt) {
  926. const sheet = contractSheet;
  927. const selection = sheet.getSelections();
  928. const sel = selection ? selection[0] : sheet.getSelections()[0];
  929. const row = sel ? sel.row : -1;
  930. const tree = sheet.zh_tree;
  931. if (!tree) return true;
  932. const first = sheet.zh_tree.nodes[row];
  933. let last = first, sameParent = true, childCanDel = true;
  934. if (sel.rowCount > 1 && first) {
  935. for (let r = 1; r < sel.rowCount; r++) {
  936. const rNode = tree.nodes[sel.row + r];
  937. childCanDel = contractTreeSpreadObj.checkDelete(rNode, childCanDel);
  938. if (!rNode) {
  939. sameParent = false;
  940. break;
  941. }
  942. if (rNode.level > first.level) continue;
  943. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.contract_pid !== first.contract_pid)) {
  944. sameParent = false;
  945. break;
  946. }
  947. last = rNode;
  948. }
  949. }
  950. const valid = !sheet.zh_setting.readOnly;
  951. return !(valid && first && first.level > 1 && sameParent && childCanDel && contractTreeSpreadObj.checkDelete(first));
  952. }
  953. };
  954. contractContextMenuOptions.items.sprBase = '----';
  955. contractContextMenuOptions.items.copyBlock = {
  956. name: '复制整块(只复制节点)',
  957. icon: 'fa-files-o',
  958. callback: function (key, opt) {
  959. const copyBlockList = [];
  960. const sheet = contractSheet;
  961. const sel = sheet.getSelections()[0];
  962. let iRow = sel.row;
  963. const pid = sheet.zh_tree.nodes[iRow].contract_pid;
  964. while (iRow < sel.row + sel.rowCount) {
  965. const node = sheet.zh_tree.nodes[iRow];
  966. if (node.contract_pid !== pid) {
  967. toastr.error('仅可同时选中同层节点');
  968. return;
  969. }
  970. const posterity = sheet.zh_tree.getPosterity(node);
  971. iRow += posterity.length + 1;
  972. posterity.unshift(node);
  973. const copyData = sheet.zh_tree.getDefaultData(posterity);
  974. const newCopyData = _.filter(_.cloneDeep(copyData), function (d) {
  975. return d.c_code === undefined;
  976. });
  977. const newCopyBlock = [];
  978. for (const p of newCopyData) {
  979. const children = _.filter(newCopyData, function (item) {
  980. return item.contract_pid === p.contract_id;
  981. });
  982. if (children.length > 0) {
  983. let i = 1;
  984. for (const c of children) {
  985. c.order = i;
  986. i++;
  987. }
  988. } else {
  989. p.is_leaf = 1;
  990. }
  991. if (_.findIndex(newCopyBlock, { contract_id: p.contract_id }) === -1) {
  992. newCopyBlock.push(p);
  993. }
  994. }
  995. copyBlockList.push(newCopyBlock);
  996. }
  997. setLocalCache(copyBlockTag, JSON.stringify({ block: copyBlockList }));
  998. },
  999. visible: function (key, opt) {
  1000. const sheet = contractSheet;
  1001. const selection = sheet.getSelections();
  1002. const row = selection[0].row;
  1003. const select = contractTree.nodes[row];
  1004. return select;
  1005. },
  1006. disabled: function (key, opt) {
  1007. const sheet = contractSheet;
  1008. const selection = sheet.getSelections();
  1009. const row = selection[0].row;
  1010. const select = contractTree.nodes[row];
  1011. console.log(select);
  1012. return !(select && select.level > 1 && !select.c_code);
  1013. }
  1014. };
  1015. if (permission_edit) {
  1016. contractContextMenuOptions.items.pasteBlock = {
  1017. name: '粘贴整块',
  1018. icon: 'fa-clipboard',
  1019. disabled: function (key, opt) {
  1020. //const block = treeOperationObj.block || [];
  1021. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1022. return !(copyInfo && copyInfo.block && copyInfo.block.length > 0);
  1023. },
  1024. callback: function (key, opt) {
  1025. //const block = treeOperationObj.block || [];
  1026. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1027. if (copyInfo.block.length > 0) {
  1028. contractTreeSpreadObj.pasteBlock(contractSpread, copyInfo);
  1029. } else {
  1030. document.execCommand('paste');
  1031. }
  1032. },
  1033. visible: function (key, opt) {
  1034. return permission_edit;
  1035. }
  1036. };
  1037. contractContextMenuOptions.items.sprBlock = '----';
  1038. }
  1039. if (permission_edit) {
  1040. contractContextMenuOptions.items.batchInsert = {
  1041. name: '批量插入',
  1042. type: 'batchInsert',
  1043. value: '2',
  1044. icon: 'fa-sign-in',
  1045. batchInsert: function (obj, root) {
  1046. if (_.toNumber(obj.value) > _.toNumber(obj.max)) {
  1047. obj.value = obj.max;
  1048. toastr.warning('批量插入不可多于' + obj.max);
  1049. } else if (_.toNumber(obj.value) < _.toNumber(obj.min)) {
  1050. obj.value = obj.min;
  1051. toastr.warning('批量插入不可少于' + obj.min);
  1052. } else {
  1053. contractTreeSpreadObj.baseOpr(contractSheet, 'add', parseInt(obj.value));
  1054. root.$menu.trigger('contextmenu:hide');
  1055. }
  1056. },
  1057. disabled: function (key, opt) {
  1058. const sheet = contractSheet;
  1059. const selection = sheet.getSelections();
  1060. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1061. const row = sel ? sel.row : -1;
  1062. const tree = sheet.zh_tree;
  1063. if (!tree) return true;
  1064. const first = sheet.zh_tree.nodes[row];
  1065. const valid = !sheet.zh_setting.readOnly;
  1066. return !(valid && first && first.level > 1);
  1067. }
  1068. };
  1069. }
  1070. contractContextMenuOptions.items.sprTag = '----';
  1071. contractContextMenuOptions.items.showLast = {
  1072. name: '显示至最底层',
  1073. callback: function (key, opt, menu, e) {
  1074. const node = SpreadJsObj.getSelectObject(contractSheet);
  1075. setTimeout(() => {
  1076. showWaitingView();
  1077. contractTree.expandByCustom(x => {
  1078. return x.expanded || (x.id === node.id) || (x.full_path.indexOf(node.contract_id + '-') >= 0);
  1079. });
  1080. SpreadJsObj.refreshTreeRowVisible(contractSheet);
  1081. closeWaitingView();
  1082. }, 100);
  1083. },
  1084. };
  1085. $.contextMenu(contractContextMenuOptions);
  1086. $('.bc-bar ul li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  1087. const node = SpreadJsObj.getSelectObject(contractSheet);
  1088. if (node && node.c_code && node.uid === user_id) {
  1089. contractTreeSpreadObj.changeContractTab(node, true);
  1090. }
  1091. });
  1092. $('#edit_contract_btn').on('click', function () {
  1093. const node = SpreadJsObj.getSelectObject(contractSheet);
  1094. if (node && node.c_code && node.uid === user_id) {
  1095. $('#edit_contract_btn').hide();
  1096. $('#save_contract_btn').show();
  1097. $('#cancel_contract_btn').show();
  1098. for (const c of contractDetail) {
  1099. if ($('#htdetail_' + c) && $('#htdetail_' + c).hasClass('change-input-td')) {
  1100. if (c === 'sign_date') {
  1101. $('#htdetail_' + c).html('<input type="date" class="form-control form-control-sm" value="' + moment(node[c]).format('YYYY-MM-DD') + '">');
  1102. continue;
  1103. } else if (c === 'remark') {
  1104. $('#htdetail_' + c).html('<textarea class="form-control form-control-sm" rows="3">' + node[c] + '</textarea>');
  1105. continue;
  1106. }
  1107. $('#htdetail_' + c).html('<input type="text" class="form-control form-control-sm" value="' + node[c] + '">');
  1108. }
  1109. }
  1110. }
  1111. });
  1112. $('#save_contract_btn').on('click', function () {
  1113. const node = SpreadJsObj.getSelectObject(contractSheet);
  1114. if (node && node.c_code && node.uid === user_id) {
  1115. const data = {
  1116. id: node.id,
  1117. };
  1118. for (const c of contractDetail) {
  1119. if ($('#htdetail_' + c) && $('#htdetail_' + c).hasClass('change-input-td')) {
  1120. if (c === 'sign_date') {
  1121. data[c] = $('#htdetail_' + c + ' input').val() || null;
  1122. continue;
  1123. } else if (c === 'remark') {
  1124. data[c] = $('#htdetail_' + c + ' textarea').val();
  1125. continue;
  1126. }
  1127. data[c] = $('#htdetail_' + c + ' input').val();
  1128. }
  1129. }
  1130. if (data.c_code === '' || data.c_name === '') {
  1131. toastr.warning('合同编号和合同名称不能为空');
  1132. return;
  1133. }
  1134. if (data.total_price === '') {
  1135. toastr.warning('合同金额不能为空');
  1136. return;
  1137. } else if (!/^\d+(\.\d+)?$/.test(data.total_price)) {
  1138. toastr.warning('合同金额只能输入数字');
  1139. return;
  1140. }
  1141. if (data.party_b === '') {
  1142. toastr.warning('签订单位(乙方)不能为空');
  1143. return;
  1144. }
  1145. console.log(data);
  1146. // 更新至服务器
  1147. postData(window.location.pathname + '/update', {postType: 'update-contract', postData: data}, function (result) {
  1148. toastr.success('已编辑成功');
  1149. const refreshNode = contractTree.loadPostData(result);
  1150. console.log(refreshNode);
  1151. contractTreeSpreadObj.refreshTree(contractSheet, refreshNode);
  1152. const newNode = SpreadJsObj.getSelectObject(contractSheet);
  1153. contractTreeSpreadObj.changeContractTab(newNode, true);
  1154. });
  1155. }
  1156. });
  1157. $('#cancel_contract_btn').on('click', function () {
  1158. const node = SpreadJsObj.getSelectObject(contractSheet);
  1159. if (node && node.c_code && node.uid === user_id) {
  1160. $('#edit_contract_btn').show();
  1161. $('#save_contract_btn').hide();
  1162. $('#cancel_contract_btn').hide();
  1163. for (const c of contractDetail) {
  1164. if ($('#htdetail_' + c) && $('#htdetail_' + c).hasClass('change-input-td')) {
  1165. if (c === 'sign_date') {
  1166. $('#htdetail_' + c).text(node[c] ? moment(node[c]).format('YYYY-MM-DD') : '');
  1167. } else {
  1168. $('#htdetail_' + c).html(node[c]);
  1169. }
  1170. }
  1171. }
  1172. }
  1173. });
  1174. $('#file-ok').click(function () {
  1175. const files = Array.from($('#file-modal')[0].files)
  1176. const valiData = files.map(v => {
  1177. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  1178. return {
  1179. size: v.size,
  1180. ext
  1181. }
  1182. })
  1183. const node = SpreadJsObj.getSelectObject(contractSheet);
  1184. if (!node || !node.c_code) {
  1185. toastr.warning('请选择合同再上传文件');
  1186. return;
  1187. }
  1188. if (validateFiles(valiData)) {
  1189. if (files.length) {
  1190. const formData = new FormData()
  1191. files.forEach(file => {
  1192. formData.append('name', file.name)
  1193. formData.append('size', file.size)
  1194. formData.append('file', file)
  1195. })
  1196. postDataWithFile('/contract/' + stid + '/detail/' + contractConst.typeMap[contract_type] + '/' + node.id + '/file/upload', formData, function (result) {
  1197. contractTreeSpreadObj.setContractFiles(result, node.id);
  1198. $('#file-modal').val('');
  1199. $('#file-cancel').click()
  1200. });
  1201. }
  1202. }
  1203. });
  1204. $('body').on('click', '#htfile .file-del', function () {
  1205. const node = SpreadJsObj.getSelectObject(contractSheet);
  1206. const fid = $(this).data('id');
  1207. deleteAfterHint(function () {
  1208. postData('/contract/' + stid + '/detail/' + contractConst.typeMap[contract_type] + '/' + node.id + '/file/delete', { id: fid }, function (result) {
  1209. contractTreeSpreadObj.setContractFiles(result, node.id);
  1210. });
  1211. }, '确认删除该文件?');
  1212. });
  1213. $('body').on('click', '#htpay .pay-del', function () {
  1214. const node = SpreadJsObj.getSelectObject(contractSheet);
  1215. const cpid = $(this).data('id');
  1216. if (!node || !node.c_code) {
  1217. toastr.warning('请选择合同节点');
  1218. return;
  1219. }
  1220. if (node.settle_code) {
  1221. toastr.warning('已结算的合同不能删除合同' + contractConst.typeName[contract_type]);
  1222. return;
  1223. }
  1224. deleteAfterHint(function () {
  1225. postData(window.location.pathname + '/update', {postType: 'del-contract-pay', postData: { select: node.id, pay: cpid }}, function (result) {
  1226. const refreshNode = contractTree.loadPostData(result.node);
  1227. contractTreeSpreadObj.refreshTree(contractSheet, refreshNode);
  1228. contractTreeSpreadObj.setContractPays(result.pays, node);
  1229. })
  1230. }, '确认删除该合同' + contractConst.typeName[contract_type] + '?');
  1231. });
  1232. const payTime = $('#cons-addpay input[name="pay_time"]').datepicker().data('datepicker');
  1233. $('.datepicker-here').datepicker({
  1234. autoClose: true,
  1235. });
  1236. $('#add_contract_pay_btn').on('click', function () {
  1237. const node = SpreadJsObj.getSelectObject(contractSheet);
  1238. if (!node || !node.c_code) {
  1239. toastr.error('请选择一个合同节点');
  1240. return;
  1241. }
  1242. $('#cons-addpay .modal-title').text('添加' + contractConst.typeName[contract_type]);
  1243. $('#cons-addpay input[name="cpid"]').val('');
  1244. $('#add-contract-pay').show();
  1245. $('#save-contract-pay').hide();
  1246. setPayModalInfo();
  1247. $('#cons-addpay').modal('show');
  1248. });
  1249. $('body').on('click', '#htpay .pay-edit', function () {
  1250. const node = SpreadJsObj.getSelectObject(contractSheet);
  1251. if (!node || !node.c_code) {
  1252. toastr.error('请选择一个合同节点');
  1253. return;
  1254. }
  1255. const cpid = $(this).data('id');
  1256. const cpInfo = _.find(contractPays, { id: parseInt(cpid) });
  1257. if (!cpInfo) {
  1258. toastr.error('未找到该合同' + contractConst.typeName[contract_type]);
  1259. return
  1260. }
  1261. $('#cons-addpay .modal-title').text('编辑' + contractConst.typeName[contract_type]);
  1262. $('#cons-addpay input[name="cpid"]').val(cpid);
  1263. $('#add-contract-pay').hide();
  1264. $('#save-contract-pay').show();
  1265. setPayModalInfo(cpInfo);
  1266. $('#cons-addpay').modal('show');
  1267. });
  1268. function setPayModalInfo(cpInfo = null) {
  1269. $('#cons-addpay input[name="pay_time"]').val(cpInfo ? moment(cpInfo.pay_time).format('YYYY-MM-DD') : '');
  1270. payTime.selectDate(cpInfo ? new Date(cpInfo.pay_time) : '');
  1271. $('#cons-addpay input[name="pay_price"]').val(cpInfo ? cpInfo.pay_price : '');
  1272. $('#cons-addpay input[name="debit_price"]').val(cpInfo ? cpInfo.debit_price : '');
  1273. $('#cons-addpay input[name="yf_price"]').val(cpInfo ? cpInfo.yf_price : '');
  1274. $('#cons-addpay input[name="sf_price"]').val(cpInfo ? cpInfo.sf_price : '');
  1275. $('#cons-addpay select[name="pay_type"]').val(cpInfo ? cpInfo.pay_type : $('#cons-addpay select[name="pay_type"] option:first').text());
  1276. $('#cons-addpay textarea[name="remark"]').val(cpInfo ? cpInfo.remark : '');
  1277. }
  1278. // 上传附件
  1279. $('#cons-pay-file input[type="file"]').change(function () {
  1280. const files = Array.from(this.files);
  1281. console.log(files);
  1282. const valiData = files.map(v => {
  1283. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  1284. return {
  1285. size: v.size,
  1286. ext
  1287. }
  1288. })
  1289. const node = SpreadJsObj.getSelectObject(contractSheet);
  1290. if (!node || !node.c_code) {
  1291. toastr.warning('请选择合同再上传文件');
  1292. $('#cons-pay-file input[type="file"]').val('');
  1293. return;
  1294. }
  1295. const cpid = $('#cons-pay-file input[name="cpid"]').val();
  1296. const cpInfo = _.find(contractPays, { id: parseInt(cpid) });
  1297. if (!cpInfo) {
  1298. toastr.warning('请选择合同'+ contractConst.typeName[contract_type] +'再上传文件');
  1299. $('#cons-pay-file input[type="file"]').val('');
  1300. return;
  1301. }
  1302. if (validateFiles(valiData)) {
  1303. if (files.length) {
  1304. const formData = new FormData()
  1305. files.forEach(file => {
  1306. formData.append('name', file.name)
  1307. formData.append('size', file.size)
  1308. formData.append('file', file)
  1309. })
  1310. postDataWithFile('/contract/' + stid + '/detail/' + contractConst.typeMap[contract_type] + '/' + node.id + '/pay/' + cpInfo.id + '/file/upload', formData, function (result) {
  1311. cpInfo.files = result;
  1312. contractTreeSpreadObj.openContractPayFiles(cpInfo);
  1313. });
  1314. }
  1315. }
  1316. $('#cons-pay-file input[type="file"]').val('');
  1317. });
  1318. $('body').on('click', '#cons-pay-file .file-del', function () {
  1319. const node = SpreadJsObj.getSelectObject(contractSheet);
  1320. const cpid = $('#cons-pay-file input[name="cpid"]').val();
  1321. const cpInfo = _.find(contractPays, { id: parseInt(cpid) });
  1322. if (!cpInfo) {
  1323. toastr.warning('未选择合同' + contractConst.typeName[contract_type]);
  1324. return;
  1325. }
  1326. const fid = $(this).data('id');
  1327. deleteAfterHint(function () {
  1328. postData('/contract/' + stid + '/detail/' + contractConst.typeMap[contract_type] + '/' + node.id + '/pay/' + cpInfo.id + '/file/delete', { id: fid }, function (result) {
  1329. cpInfo.files = result;
  1330. contractTreeSpreadObj.openContractPayFiles(cpInfo);
  1331. });
  1332. }, '确认删除该文件?');
  1333. });
  1334. postData(window.location.pathname + '/load', {}, function (result) {
  1335. const datas = result.contractTree.concat(result.contractList);
  1336. for (const t of datas) {
  1337. if (!t.is_leaf && _.findIndex(datas, { contract_pid: t.contract_id }) === -1) {
  1338. t.is_leaf = 1;
  1339. }
  1340. }
  1341. contractTree.loadDatas(datas);
  1342. treeCalc.calculateAll(contractTree);
  1343. // contractTree.setExpanded(node, !node.expanded);
  1344. // SpreadJsObj.initSpreadSettingEvents(changeSpreadSetting, changeCol);
  1345. SpreadJsObj.loadSheetData(contractSheet, SpreadJsObj.DataType.Tree, contractTree);
  1346. SpreadJsObj.loadTopAndSelect(contractSheet, ckBillsSpread);
  1347. checkShowLast(result.contractTree.length);
  1348. contractTreeSpreadObj.refreshOperationValid(contractSheet);
  1349. // contractTreeSpreadObj.setAllForeColor(contractSheet);
  1350. console.log(contractTree);
  1351. });
  1352. if (is_admin) {
  1353. const sqSpreadSetting = {
  1354. cols: [
  1355. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 100, formatter: '@', cellType: 'tree'},
  1356. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 240, formatter: '@'},
  1357. ],
  1358. emptyRows: 0,
  1359. headRows: 2,
  1360. headRowHeight: [25, 25],
  1361. defaultRowHeight: 21,
  1362. headerFont: '12px 微软雅黑',
  1363. font: '12px 微软雅黑',
  1364. readOnly: true,
  1365. rowHeader:[
  1366. // {
  1367. // rowHeaderType: 'circle',
  1368. // setting: {
  1369. // size: 5,
  1370. // indent: 16,
  1371. // getColor: function (index, data) {
  1372. // if (!data) return;
  1373. // if (_.findIndex(changeLedgerList, { id: data.gcl_id }) !== -1 || _.findIndex(changePosList, { id: data.mx_id }) !== -1) {
  1374. // return '#dc3545';
  1375. // }
  1376. // if(data.lid != 0) return;
  1377. // return '#007bff';
  1378. // }
  1379. // },
  1380. // },
  1381. ],
  1382. localCache: {
  1383. key: 'contract-sq-spread',
  1384. colWidth: true,
  1385. }
  1386. };
  1387. const sqSpread = SpreadJsObj.createNewSpread($('#sq-spread')[0]);
  1388. const sqSheet = sqSpread.getActiveSheet();
  1389. sjsSettingObj.setGridSelectStyle(sqSpreadSetting);
  1390. // sjsSettingObj.setFxTreeStyle(contractSpreadSetting, sjsSettingObj.FxTreeStyle.jz);
  1391. SpreadJsObj.initSheet(sqSheet, sqSpreadSetting);
  1392. const sqTreeSetting = {
  1393. id: 'contract_id',
  1394. pid: 'contract_pid',
  1395. order: 'order',
  1396. level: 'level',
  1397. rootId: -1,
  1398. keys: ['id', 'tid', 'spid'],
  1399. autoExpand: 3,
  1400. markExpandKey: 'contract-sq-bills-expand',
  1401. markExpandSubKey: window.location.pathname.split('/')[2],
  1402. };
  1403. sqTreeSetting.calcFun = function (node) {
  1404. };
  1405. const sqTree = createNewPathTree('base', sqTreeSetting);
  1406. const sqTreeSpreadObj = {
  1407. setTreeAudits: function (sheet, selection) {
  1408. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1409. const row = sel ? sel.row : -1;
  1410. const tree = sheet.zh_tree;
  1411. if (!tree) {
  1412. return;
  1413. }
  1414. const first = sheet.zh_tree.nodes[row];
  1415. const auditList = _.orderBy(_.filter(contractTreeAudits, { contract_id: first.contract_id }), ['create_time'], ['desc']);
  1416. let auditHtml = '';
  1417. for (const audit of auditList) {
  1418. audit.name = _.find(accountList, { id: audit.uid }).name;
  1419. auditHtml += `<tr data-id="${audit.uid}">
  1420. <td><input type="checkbox"></td><td>${audit.name}</td><td>${moment(new Date(audit.create_time)).format('YYYY-MM-DD HH:mm:ss')}</td><td>${this.setAuditPermissionTxt(audit)}</td>
  1421. <td><button class="btn btn-sm btn-outline-danger del-tree-audit">移除</button></td>
  1422. </tr>`;
  1423. }
  1424. $('#contract-tree-audits').html(auditHtml);
  1425. },
  1426. setAuditPermissionTxt: function (audit) {
  1427. const txt = [];
  1428. const ca = _.find(contractAudits, { uid: audit.uid });
  1429. if (ca && ca.permission_edit) {
  1430. txt.push('编辑节点');
  1431. }
  1432. if (ca && ca.permission_add) {
  1433. txt.push('添加合同');
  1434. }
  1435. if (ca && ca.permission_show_unit) {
  1436. txt.push('查看本单位合同');
  1437. }
  1438. if (ca && ca.permission_show_node) {
  1439. txt.push('查看本节点合同');
  1440. }
  1441. if (ca && !ca.permission_show_unit && !ca.permission_show_node) {
  1442. txt.push('查看本人合同');
  1443. }
  1444. return txt.join(',');
  1445. },
  1446. selectionChanged: function (e, info) {
  1447. if (info.newSelections) {
  1448. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row || info.newSelections[0].rowCount !== info.oldSelections[0].rowCount) {
  1449. $('#select-permission-tree-audit-all').prop('checked', false);
  1450. sqTreeSpreadObj.setTreeAudits(info.sheet);
  1451. }
  1452. }
  1453. },
  1454. }
  1455. sqSpread.bind(spreadNS.Events.SelectionChanged, sqTreeSpreadObj.selectionChanged);
  1456. let contractAudits = [];
  1457. let contractAccountGroup = [];
  1458. $('#empower').on('shown.bs.modal', function () {
  1459. postData(window.location.pathname + '/load', {}, function (result) {
  1460. const tree = result.contractTree;
  1461. for (const t of tree) {
  1462. if (!t.is_leaf && _.findIndex(tree, { contract_pid: t.contract_id }) === -1) {
  1463. t.is_leaf = 1;
  1464. }
  1465. }
  1466. sqTree.loadDatas(tree);
  1467. SpreadJsObj.loadSheetData(sqSheet, SpreadJsObj.DataType.Tree, sqTree);
  1468. sqSpread.refresh();
  1469. contractAudits = result.contractAudits;
  1470. contractAccountGroup = result.accountGroup;
  1471. let groupBookListHtml = '';
  1472. result.accountGroup.forEach((group, idx) => {
  1473. groupBookListHtml += '<dt><a href="javascript: void(0);" class="acc-btn" data-groupid="'+ idx +'" data-type="hide"><i class="fa fa-plus-square"></i></a> '+ group.groupName +'</dt>\n' +
  1474. ' <div class="dd-content" data-toggleid="'+ idx +'">\n';
  1475. let groupListHtml = '';
  1476. group.groupList.forEach(item => {
  1477. groupListHtml += '<dd class="border-bottom p-2 mb-0 " data-id="'+ item.uid + '" >\n' +
  1478. ' <p class="mb-0 d-flex"><span class="text-primary">'+ item.name + '</span><span\n' +
  1479. ' class="ml-auto">' + item.mobile + '</span></p>\n' +
  1480. ' <span class="text-muted">'+ item.role +'</span>\n' +
  1481. ' </dd>\n';
  1482. });
  1483. groupBookListHtml = groupBookListHtml + groupListHtml + '</div>';
  1484. });
  1485. $('#empower .book-list').html(groupBookListHtml);
  1486. sqTreeSpreadObj.setTreeAudits(sqSheet);
  1487. });
  1488. });
  1489. let timer2 = null
  1490. let oldSearchVal2 = null
  1491. $('#gr-search2').bind('input propertychange', function (e) {
  1492. oldSearchVal2 = e.target.value
  1493. timer2 && clearTimeout(timer2)
  1494. timer2 = setTimeout(() => {
  1495. const newVal = $('#gr-search2').val()
  1496. let html = ''
  1497. if (newVal && newVal === oldSearchVal2) {
  1498. contractAudits.filter(item => item && (item.name.indexOf(newVal) !== -1 || (item.mobile && item.mobile.indexOf(newVal) !== -1))).forEach(item => {
  1499. html += `<dd class="border-bottom p-2 mb-0 " data-id="${item.uid}" >
  1500. <p class="mb-0 d-flex"><span class="text-primary">${item.name}</span><span
  1501. class="ml-auto">${item.mobile || ''}</span></p>
  1502. <span class="text-muted">${item.role || ''}</span>
  1503. </dd>`
  1504. })
  1505. $('#empower .book-list').empty()
  1506. $('#empower .book-list').append(html)
  1507. } else {
  1508. if (!$('#empower .acc-btn').length) {
  1509. contractAccountGroup.forEach((group, idx) => {
  1510. if (!group) return
  1511. html += `<dt><a href="javascript: void(0);" class="acc-btn" data-groupid="${idx}" data-type="hide"><i class="fa fa-plus-square"></i>
  1512. </a> ${group.groupName}</dt>
  1513. <div class="dd-content" data-toggleid="${idx}">`
  1514. group.groupList.forEach(item => {
  1515. html += `<dd class="border-bottom p-2 mb-0 " data-id="${item.uid}" >
  1516. <p class="mb-0 d-flex"><span class="text-primary">${item.name}</span><span
  1517. class="ml-auto">${item.mobile || ''}</span></p>
  1518. <span class="text-muted">${item.role || ''}</span>
  1519. </dd>`
  1520. });
  1521. html += '</div>'
  1522. })
  1523. $('#empower .book-list').empty()
  1524. $('#empower .book-list').append(html)
  1525. }
  1526. }
  1527. }, 400);
  1528. });
  1529. // 添加到成员中
  1530. $('body').on('click', '#empower dl dd', function () {
  1531. const id = parseInt($(this).data('id'));
  1532. const node = SpreadJsObj.getSelectObject(sqSheet);
  1533. if (!node) {
  1534. toastr.error('请选择节点');
  1535. return;
  1536. }
  1537. if (!isNaN(id) && id !== 0) {
  1538. if (_.findIndex(contractTreeAudits, { uid: id, contract_id: node.contract_id }) !== -1) {
  1539. toastr.error('该成员已存在, 请勿重复添加');
  1540. return;
  1541. }
  1542. postData(window.location.pathname + '/update', {postType: 'add-tree-audit', postData: { select: node, auditId: id }}, function (result) {
  1543. contractTreeAudits.push(result);
  1544. sqTreeSpreadObj.setTreeAudits(sqSheet);
  1545. })
  1546. }
  1547. });
  1548. $('#select-permission-tree-audit-all').on('click', function () {
  1549. $('#contract-tree-audits').find('input[type="checkbox"]').prop('checked', $(this).prop('checked'));
  1550. });
  1551. $('#batch-del-audits').on('click', function () {
  1552. let uids = [];
  1553. $('#contract-tree-audits').find('input[type="checkbox"]:checked').each(function () {
  1554. uids.push(parseInt($(this).parents('tr').data('id')));
  1555. });
  1556. if (uids.length === 0) {
  1557. toastr.warning('请选择要删除的成员');
  1558. return;
  1559. }
  1560. deleteAfterHint(function () {
  1561. postData(window.location.pathname + '/update', {
  1562. postType: 'del-tree-audit',
  1563. postData: {
  1564. select: SpreadJsObj.getSelectObject(sqSheet),
  1565. auditIds: uids,
  1566. }
  1567. }, function (result) {
  1568. // contractTreeAudits数组清空再赋值
  1569. contractTreeAudits = result;
  1570. sqTreeSpreadObj.setTreeAudits(sqSheet);
  1571. });
  1572. }, '确认删除「当前节点已勾选授权用户」?');
  1573. });
  1574. $('body').on('click', '#empower .del-tree-audit', function () {
  1575. const uid = parseInt($(this).parents('tr').data('id'));
  1576. deleteAfterHint(function () {
  1577. postData(window.location.pathname + '/update', {
  1578. postType: 'del-tree-audit',
  1579. postData: {
  1580. select: SpreadJsObj.getSelectObject(sqSheet),
  1581. auditIds: uid,
  1582. }
  1583. }, function (result) {
  1584. // contractTreeAudits数组清空再赋值
  1585. contractTreeAudits = result;
  1586. sqTreeSpreadObj.setTreeAudits(sqSheet);
  1587. });
  1588. }, '确认删除「当前节点授权用户」?');
  1589. });
  1590. }
  1591. if (permission_add) {
  1592. $('#add-contract').click(function () {
  1593. const node = SpreadJsObj.getSelectObject(contractSheet);
  1594. if (!node) {
  1595. toastr.error('请选择一个节点');
  1596. return;
  1597. }
  1598. console.log(node);
  1599. const data = {
  1600. code: $('#cons-add input[name="code"]').val(),
  1601. name: $('#cons-add input[name="name"]').val(),
  1602. total_price: $('#cons-add input[name="total_price"]').val(),
  1603. party_b: $('#cons-add input[name="party_b"]').val(),
  1604. remark: $('#cons-add textarea[name="remark"]').val(),
  1605. }
  1606. if (!data.code) {
  1607. toastr.error('请输入合同编号');
  1608. return;
  1609. }
  1610. if (!data.name) {
  1611. toastr.error('请输入合同名称');
  1612. return;
  1613. }
  1614. if (!data.total_price) {
  1615. toastr.error('请输入合同金额');
  1616. return;
  1617. }
  1618. // 金额只能输入数字
  1619. if (!/^\d+(\.\d+)?$/.test(data.total_price)) {
  1620. toastr.error('合同金额只能输入数字');
  1621. return;
  1622. }
  1623. if (!data.party_b) {
  1624. toastr.error('请输入签订单位(乙方)');
  1625. return;
  1626. }
  1627. console.log(node, data);
  1628. postData(window.location.pathname + '/update', {postType: 'add-contract', postData: { select: node, contract: data }}, function (result) {
  1629. const refreshNode = contractTree.loadPostData(result);
  1630. contractTreeSpreadObj.refreshTree(contractSheet, refreshNode);
  1631. $('#cons-add').modal('hide');
  1632. })
  1633. });
  1634. }
  1635. $('#cons-addpay input[type="number"]').change(function () {
  1636. const name = $(this).attr('name');
  1637. if (name === 'pay_price' || name === 'debit_price') {
  1638. const pay_price = $('#cons-addpay input[name="pay_price"]').val() || 0;
  1639. const debit_price = $('#cons-addpay input[name="debit_price"]').val() || 0;
  1640. const yf_price = ZhCalc.sub(pay_price, debit_price);
  1641. $('#cons-addpay input[name="yf_price"]').val(yf_price);
  1642. }
  1643. });
  1644. $('#add-contract-pay').click(function () {
  1645. const node = SpreadJsObj.getSelectObject(contractSheet);
  1646. if (!node || !node.c_code) {
  1647. toastr.error('请选择一个合同节点');
  1648. return;
  1649. }
  1650. if (node.uid !== user_id) {
  1651. toastr.error('只能添加自己的合同' + contractConst.typeName[contract_type]);
  1652. return;
  1653. }
  1654. if (node.settle_code) {
  1655. toastr.error('该合同已结算,不能添加');
  1656. return;
  1657. }
  1658. const data = {
  1659. pay_time: $('#cons-addpay input[name="pay_time"]').val(),
  1660. pay_price: $('#cons-addpay input[name="pay_price"]').val() || 0,
  1661. debit_price: $('#cons-addpay input[name="debit_price"]').val() || 0,
  1662. yf_price: $('#cons-addpay input[name="yf_price"]').val() || 0,
  1663. sf_price: $('#cons-addpay input[name="sf_price"]').val() || 0,
  1664. pay_type: $('#cons-addpay select[name="pay_type"]').val(),
  1665. remark: $('#cons-addpay textarea[name="remark"]').val(),
  1666. }
  1667. const name = contract_type === contractConst.type.expenses ? '付' : '回';
  1668. if (!judgePays(node, data, name)) {
  1669. return;
  1670. }
  1671. console.log(node, data);
  1672. postData(window.location.pathname + '/update', {postType: 'add-contract-pay', postData: { select: node.id, pay: data }}, function (result) {
  1673. const refreshNode = contractTree.loadPostData(result.node);
  1674. contractTreeSpreadObj.refreshTree(contractSheet, refreshNode);
  1675. contractTreeSpreadObj.setContractPays(result.pays, node);
  1676. // const selection = contractSheet.getSelections();
  1677. // const sel = selection ? selection[0] : contractSheet.getSelections()[0];
  1678. // const row = sel ? sel.row : -1;
  1679. // contractTreeSpreadObj.setForeColor(contractSheet, row);
  1680. $('#cons-addpay').modal('hide');
  1681. })
  1682. });
  1683. $('#save-contract-pay').click(function () {
  1684. const node = SpreadJsObj.getSelectObject(contractSheet);
  1685. if (!node || !node.c_code) {
  1686. toastr.error('请选择一个合同节点');
  1687. return;
  1688. }
  1689. if (node.settle_code) {
  1690. toastr.error('该合同已结算,不能修改');
  1691. return;
  1692. }
  1693. const cpid = $('#cons-addpay input[name="cpid"]').val();
  1694. const cpInfo = _.find(contractPays, { id: parseInt(cpid) });
  1695. if (!cpInfo) {
  1696. toastr.error('未找到该合同' + contractConst.typeName[contract_type]);
  1697. return
  1698. }
  1699. if (cpInfo.uid !== user_id) {
  1700. toastr.error('只能编辑自己的合同' + contractConst.typeName[contract_type]);
  1701. return;
  1702. }
  1703. const data = {
  1704. id: cpInfo.id,
  1705. pay_time: $('#cons-addpay input[name="pay_time"]').val(),
  1706. pay_price: $('#cons-addpay input[name="pay_price"]').val() || 0,
  1707. debit_price: $('#cons-addpay input[name="debit_price"]').val() || 0,
  1708. yf_price: $('#cons-addpay input[name="yf_price"]').val() || 0,
  1709. sf_price: $('#cons-addpay input[name="sf_price"]').val() || 0,
  1710. pay_type: $('#cons-addpay select[name="pay_type"]').val(),
  1711. remark: $('#cons-addpay textarea[name="remark"]').val(),
  1712. }
  1713. const name = contract_type === contractConst.type.expenses ? '付' : '回';
  1714. if (!judgePays(node, data, name, cpInfo)) {
  1715. return;
  1716. }
  1717. console.log(node, data);
  1718. postData(window.location.pathname + '/update', {postType: 'save-contract-pay', postData: { select: node.id, pay: data }}, function (result) {
  1719. const refreshNode = contractTree.loadPostData(result.node);
  1720. contractTreeSpreadObj.refreshTree(contractSheet, refreshNode);
  1721. contractTreeSpreadObj.setContractPays(result.pays, node);
  1722. // const selection = contractSheet.getSelections();
  1723. // const sel = selection ? selection[0] : contractSheet.getSelections()[0];
  1724. // const row = sel ? sel.row : -1;
  1725. // contractTreeSpreadObj.setForeColor(contractSheet, row);
  1726. $('#cons-addpay').modal('hide');
  1727. })
  1728. });
  1729. function judgePays(node, data, name, cpInfo = null) {
  1730. let flag = true;
  1731. if (!data.pay_price) {
  1732. toastr.error('请输入'+ name +'款金额');
  1733. return false;
  1734. }
  1735. // 金额只能输入数字
  1736. if (!/^\d+(\.\d+)?$/.test(data.pay_price)) {
  1737. toastr.error(name + '款金额只能输入数字');
  1738. return false;
  1739. }
  1740. // if (cpInfo) {
  1741. // const newNodePayPrice = ZhCalc.add(ZhCalc.sub(node.pay_price, cpInfo.pay_price), parseFloat(data.pay_price));
  1742. // const newNodeDebitPrice = ZhCalc.add(ZhCalc.sub(node.debit_price, cpInfo.debit_price), parseFloat(data.debit_price));
  1743. // const newNodeYfPrice = ZhCalc.sub(newNodePayPrice, newNodeDebitPrice);
  1744. // const newNodeSfPrice = ZhCalc.add(ZhCalc.sub(node.sf_price, cpInfo.sf_price), parseFloat(data.sf_price));
  1745. // // if (parseFloat(data.pay_price) > ZhCalc.sub(newNodeYfPrice, newNodeSfPrice)) {
  1746. // // toastr.error(name + '款金额不能大于待支付金额 ' + ZhCalc.sub(newNodeYfPrice, newNodeSfPrice) + '元');
  1747. // // return false;
  1748. // // }
  1749. // } else {
  1750. // if (parseFloat(data.pay_price) > ZhCalc.sub(node.yf_price, node.sf_price)) {
  1751. // toastr.error(name + '款金额不能大于待支付金额 ' + ZhCalc.sub(node.yf_price, node.sf_price) + '元');
  1752. // return false;
  1753. // }
  1754. // }
  1755. if (data.debit_price && !/^\d+(\.\d+)?$/.test(data.debit_price)) {
  1756. toastr.error('扣款金额只能输入数字');
  1757. return false;
  1758. }
  1759. if (parseFloat(data.debit_price) > parseFloat(data.pay_price)) {
  1760. toastr.error('扣款金额不能大于'+ name +'款金额');
  1761. return false;
  1762. }
  1763. if (data.sf_price && !/^\d+(\.\d+)?$/.test(data.sf_price)) {
  1764. toastr.error('实'+ name +'金额只能输入数字');
  1765. return false;
  1766. }
  1767. if (parseFloat(data.sf_price) > parseFloat(data.yf_price)) {
  1768. toastr.error('实'+ name +'金额不能大于应'+ name +'金额');
  1769. return false;
  1770. }
  1771. if (!data.pay_type) {
  1772. toastr.error('请选择' + contractConst.typeName[contract_type] + '方式');
  1773. return false;
  1774. }
  1775. return flag;
  1776. }
  1777. $('body').on('click', '.open-pay-files', function () {
  1778. const cpid = $(this).attr('data-cpid');
  1779. if (!cpid) {
  1780. toastr.error('获取合同'+ contractConst.typeName[contract_type] +'信息失败');
  1781. return;
  1782. }
  1783. const pay = _.find(contractPays, { id: parseInt(cpid) });
  1784. if (!pay) {
  1785. toastr.error('获取合同'+ contractConst.typeName[contract_type] +'信息失败');
  1786. return;
  1787. }
  1788. const node = SpreadJsObj.getSelectObject(contractSheet);
  1789. if (!node || !node.c_code) {
  1790. toastr.error('请选择一个合同节点');
  1791. return;
  1792. }
  1793. if (node.uid !== user_id) {
  1794. $('#cons-pay-file .upload-permission').hide();
  1795. } else {
  1796. $('#cons-pay-file .upload-permission').show();
  1797. }
  1798. $('#cons-pay-file').modal('show');
  1799. $('#cons-pay-file input[name="cpid"]').val(cpid);
  1800. contractTreeSpreadObj.openContractPayFiles(pay);
  1801. });
  1802. $('#cons-close').on('show.bs.modal', function () {
  1803. const node = SpreadJsObj.getSelectObject(contractSheet);
  1804. if (!node || !node.c_code) {
  1805. toastr.error('请选择一个合同节点');
  1806. $('#cons-close').modal('hide');
  1807. return;
  1808. }
  1809. const df_price = ZhCalc.sub(ZhCalc.sub(node.yf_price, node.sf_price) || 0);
  1810. if (df_price !== 0) {
  1811. $('#close_df_price').text(df_price);
  1812. $('#close_df_price').parents('h5').show();
  1813. } else {
  1814. $('#close_df_price').parents('h5').hide();
  1815. }
  1816. $('#close_contract_code').text(node.c_code + '/' + node.name);
  1817. $('#close_settle_code').val('');
  1818. });
  1819. $('#close_contract_btn').click(function () {
  1820. const settle_code = $('#close_settle_code').val();
  1821. if (!settle_code) {
  1822. toastr.error('请输入结算书编号');
  1823. return;
  1824. }
  1825. const node = SpreadJsObj.getSelectObject(contractSheet);
  1826. if (!node || !node.c_code) {
  1827. toastr.error('请选择一个合同节点');
  1828. return;
  1829. }
  1830. const data = {
  1831. id: node.id,
  1832. settle_code,
  1833. }
  1834. // 更新至服务器
  1835. postData(window.location.pathname + '/update', {postType: 'update-contract', postData: data}, function (result) {
  1836. const refreshNode = contractTree.loadPostData(result);
  1837. contractTreeSpreadObj.refreshTree(contractSheet, refreshNode);
  1838. const newNode = SpreadJsObj.getSelectObject(contractSheet);
  1839. contractTreeSpreadObj.changeContractTab(newNode, true);
  1840. // const selection = contractSheet.getSelections();
  1841. // const sel = selection ? selection[0] : contractSheet.getSelections()[0];
  1842. // const row = sel ? sel.row : -1;
  1843. // contractTreeSpreadObj.setForeColor(contractSheet, row);
  1844. $('#cons-close').modal('hide');
  1845. $('a[href*="#cons-unlock"]').show();
  1846. $('a[href*="#cons-close"]').hide();
  1847. });
  1848. });
  1849. $('#unlock_contract_btn').click(function () {
  1850. const node = SpreadJsObj.getSelectObject(contractSheet);
  1851. if (!node || !node.c_code) {
  1852. toastr.error('请选择一个合同节点');
  1853. return;
  1854. }
  1855. const data = {
  1856. id: node.id,
  1857. settle_code: '',
  1858. }
  1859. // 更新至服务器
  1860. postData(window.location.pathname + '/update', {postType: 'update-contract', postData: data}, function (result) {
  1861. const refreshNode = contractTree.loadPostData(result);
  1862. contractTreeSpreadObj.refreshTree(contractSheet, refreshNode);
  1863. const newNode = SpreadJsObj.getSelectObject(contractSheet);
  1864. contractTreeSpreadObj.changeContractTab(newNode, true);
  1865. // const selection = contractSheet.getSelections();
  1866. // const sel = selection ? selection[0] : contractSheet.getSelections()[0];
  1867. // const row = sel ? sel.row : -1;
  1868. // contractTreeSpreadObj.setForeColor(contractSheet, row);
  1869. $('#cons-unlock').modal('hide');
  1870. $('a[href*="#cons-unlock"]').hide();
  1871. $('a[href*="#cons-close"]').show();
  1872. });
  1873. });
  1874. // 显示层次
  1875. (function (select, sheet) {
  1876. $(select).click(function () {
  1877. if (!sheet.zh_tree) return;
  1878. const tag = $(this).attr('tag');
  1879. const tree = sheet.zh_tree;
  1880. setTimeout(() => {
  1881. showWaitingView();
  1882. switch (tag) {
  1883. case "1":
  1884. case "2":
  1885. case "3":
  1886. case "4":
  1887. case "5":
  1888. tree.expandByLevel(parseInt(tag));
  1889. SpreadJsObj.refreshTreeRowVisible(sheet);
  1890. break;
  1891. case "last":
  1892. tree.expandByCustom(() => { return true; });
  1893. SpreadJsObj.refreshTreeRowVisible(sheet);
  1894. break;
  1895. case "leafXmj":
  1896. tree.expandToLeafXmj();
  1897. SpreadJsObj.refreshTreeRowVisible(sheet);
  1898. break;
  1899. }
  1900. closeWaitingView();
  1901. }, 100);
  1902. });
  1903. })('a[name=showLevel]', contractSheet);
  1904. $.divResizer({
  1905. select: '#contract-resize',
  1906. callback: function () {
  1907. contractSpread.refresh();
  1908. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1909. $(".sp-wrap").height(bcontent-30);
  1910. // posSpread.refresh();
  1911. }
  1912. });
  1913. $.subMenu({
  1914. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  1915. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  1916. key: 'menu.1.0.0',
  1917. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  1918. callback: function (info) {
  1919. if (info.mini) {
  1920. $('.panel-title').addClass('fluid');
  1921. $('#sub-menu').removeClass('panel-sidebar');
  1922. } else {
  1923. $('.panel-title').removeClass('fluid');
  1924. $('#sub-menu').addClass('panel-sidebar');
  1925. }
  1926. autoFlashHeight();
  1927. contractSpread.refresh();
  1928. }
  1929. });
  1930. });
  1931. /**
  1932. * 校验文件大小、格式
  1933. * @param {Array} files 文件数组
  1934. */
  1935. function validateFiles(files) {
  1936. if (files.length > 10) {
  1937. toastr.error('至多同时上传10个文件');
  1938. return false
  1939. }
  1940. return files.every(file => {
  1941. if (file.size > 1024 * 1024 * 30) {
  1942. toastr.error('文件大小限制为30MB');
  1943. return false
  1944. }
  1945. if (whiteList.indexOf('.' + file.ext.toLowerCase()) === -1) {
  1946. toastr.error('请上传正确的格式文件');
  1947. return false
  1948. }
  1949. return true
  1950. })
  1951. }