contract_detail.js 94 KB

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