change_revise.js 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598
  1. 'use strict';
  2. /**
  3. * 台账修订页面js
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const ckBillsSpread = '/tender/' + window.location.pathname.split('/')[2] + '/change/revise-billsSelect';
  10. const invalidFields = {
  11. parent: ['sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'deal_qty', 'deal_tp', 'unit_price'],
  12. gcl: ['dgn_qty1', 'dgn_qty2'],
  13. posCode: ['b_code'],
  14. posCalc: ['sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp'],
  15. posXmj: ['code'],
  16. };
  17. function getExprInfo (field) {
  18. const exprField = [
  19. {qty: 'sgfh_qty', expr: 'sgfh_expr'},
  20. {qty: 'sjcl_qty', expr: 'sjcl_expr'},
  21. {qty: 'qtcl_qty', expr: 'qtcl_expr'},
  22. ];
  23. return _.find(exprField, {qty: field});
  24. }
  25. function transExpr(expr) {
  26. return $.trim(expr).replace('\t', '').replace('=', '').replace('%', '/100');
  27. }
  28. const copyBlockTag = 'zh.calc.copyBlock';
  29. const checkOption = {
  30. sibling: { enable: 1 },
  31. empty_code: { enable: 1 },
  32. calc: {
  33. enable: 1,
  34. fields: ['sgfh_qty', 'qtcl_qty', 'sjcl_qty', 'quantity'],
  35. },
  36. zero: { enable: 1 },
  37. zeroPos: { enable: 1 },
  38. tp: {
  39. enable: 1,
  40. fields: [
  41. {qty: 'sgfh_qty', tp: 'sgfh_tp'},
  42. {qty: 'sjcl_qty', tp: 'sjcl_tp'},
  43. {qty: 'qtcl_qty', tp: 'qtcl_tp'},
  44. {qty: 'quantity', tp: 'total_price'},
  45. {qty: 'deal_qty', tp: 'deal_tp'},
  46. ],
  47. },
  48. same_code: { enable: 1 },
  49. };
  50. $(document).ready(() => {
  51. let stdXmj, stdGcl, searchLedger;
  52. autoFlashHeight();
  53. if (!billsSpreadSetting.readOnly) {
  54. for (const col of billsSpreadSetting.cols) {
  55. if (!col.readOnly || col.readOnly === false)
  56. col.readOnly = 'readOnly.isChangeAdd';
  57. }
  58. }
  59. if (!posSpreadSetting.readOnly) {
  60. for (const col of posSpreadSetting.cols) {
  61. if (!col.readOnly || col.readOnly === false)
  62. col.readOnly = 'readOnly.isChangeAdd';
  63. }
  64. }
  65. const billsCol = {
  66. readOnly: {
  67. isChangeAdd: function (data) {
  68. return !readOnly && !data.ccid;
  69. }
  70. }
  71. };
  72. const posCol = {
  73. readOnly: {
  74. isChangeAdd: function (data) {
  75. return !readOnly && !data.ccid;
  76. }
  77. }
  78. };
  79. // 初始化spread
  80. const billsSpread = SpreadJsObj.createNewSpread($('#bills-spread')[0]);
  81. const billsSheet = billsSpread.getActiveSheet();
  82. billsSpreadSetting.rowHeader = [
  83. {
  84. rowHeaderType: 'circle',
  85. setting: {
  86. size: 5,
  87. indent: 16,
  88. getColor: function (index, data) {
  89. if (!data) return;
  90. if(!data.ccid && !data.cid) return;
  91. return '#dc3545';
  92. }
  93. },
  94. },
  95. ];
  96. billsSpreadSetting.localCache = {
  97. key: 'changes-ledger-spread',
  98. colWidth: true,
  99. };
  100. sjsSettingObj.setFxTreeStyle(billsSpreadSetting, sjsSettingObj.FxTreeStyle.jz);
  101. if (thousandth) sjsSettingObj.setTpThousandthFormat(billsSpreadSetting);
  102. SpreadJsObj.initSpreadSettingEvents(billsSpreadSetting, billsCol);
  103. SpreadJsObj.initSheet(billsSheet, billsSpreadSetting);
  104. const posSpread = SpreadJsObj.createNewSpread($('#pos-spread')[0]);
  105. const posSheet = posSpread.getActiveSheet();
  106. posSpreadSetting.rowHeader = [
  107. {
  108. rowHeaderType: 'circle',
  109. setting: {
  110. size: 5,
  111. indent: 16,
  112. getColor: function (index, data) {
  113. if (!data) return;
  114. if(!data.ccid) return;
  115. return '#dc3545';
  116. }
  117. },
  118. },
  119. ];
  120. posSpreadSetting.localCache = {
  121. key: 'changes-pos-spread',
  122. colWidth: true,
  123. };
  124. sjsSettingObj.setGridSelectStyle(posSpreadSetting);
  125. if (thousandth) sjsSettingObj.setTpThousandthFormat(posSpreadSetting);
  126. SpreadJsObj.initSpreadSettingEvents(posSpreadSetting, posCol);
  127. SpreadJsObj.initSheet(posSheet, posSpreadSetting);
  128. const posSearch = $.posSearch({selector: '#pos-search', searchSpread: posSpread});
  129. const errorList = $.cs_errorList({
  130. tabSelector: '#error-list-tab',
  131. selector: '#error-list',
  132. relaSpread: billsSpread,
  133. storeKey: 'change-revise-error-' + window.location.pathname.split('/')[2],
  134. afterLocated: function () {
  135. posSpreadObj.loadCurPosData();
  136. },
  137. afterShow: function () {
  138. billsSpread.refresh();
  139. if (posSpread) posSpread.refresh();
  140. },
  141. });
  142. const checkList = $.ledger_checkList({
  143. id: 'check-list',
  144. tabSelector: '#check-list-tab',
  145. selector: '#check-list',
  146. relaSpread: billsSpread,
  147. storeKey: 'change-revise-check-' + window.location.pathname.split('/')[2],
  148. checkType: getCheckType(checkOption),
  149. afterLocated: function () {
  150. posSpreadObj.loadCurPosData();
  151. },
  152. afterShow: function () {
  153. billsSpread.refresh();
  154. if (posSpread) posSpread.refresh();
  155. },
  156. });
  157. const sumLoadMiss = $.sumLoadMiss({
  158. tabSelector: '#sum-load-miss-tab',
  159. selector: '#sum-load-miss',
  160. relaSpread: billsSpread,
  161. storeKey: 'change-revise-slm-' + window.location.pathname.split('/')[2],
  162. id: 'revise-slm',
  163. afterLocated: function () {
  164. posSpreadObj.loadCurPosData();
  165. },
  166. afterShow: function () {
  167. billsSpread.refresh();
  168. if (posSpread) posSpread.refresh();
  169. },
  170. });
  171. // 初始化 节点树结构
  172. const treeSetting = {
  173. id: 'ledger_id',
  174. pid: 'ledger_pid',
  175. order: 'order',
  176. level: 'level',
  177. rootId: -1,
  178. keys: ['id', 'tender_id', 'ledger_id'],
  179. calcFields: ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price'],
  180. autoExpand: 3,
  181. markExpandKey: 'change-bills-expand',
  182. markExpandSubKey: window.location.pathname.split('/')[2],
  183. };
  184. if (!isTz) {
  185. treeSetting.calcFields.push('deal_tp');
  186. }
  187. treeSetting.calcFun = function (node) {
  188. if (node && node.b_code && isTz) {
  189. const posData = pos.getLedgerPos(node.id) || [];
  190. if (posData.length > 0) {
  191. let sgfh_qty = 0;
  192. let sjcl_qty = 0;
  193. let qtcl_qty = 0;
  194. for (const np of posData) {
  195. sgfh_qty = ZhCalc.add(sgfh_qty, np.sgfh_qty);
  196. sjcl_qty = ZhCalc.add(sjcl_qty, np.sjcl_qty);
  197. qtcl_qty = ZhCalc.add(qtcl_qty, np.qtcl_qty);
  198. }
  199. if (!_.isEqual(node.sgfh_qty, sgfh_qty) || !_.isEqual(node.sjcl_qty, sjcl_qty) || !_.isEqual(node.qtcl_qty, qtcl_qty)) {
  200. node.sgfh_qty = sgfh_qty;
  201. node.sjcl_qty = sjcl_qty;
  202. node.qtcl_qty = qtcl_qty;
  203. node.quantity = ZhCalc.sum([node.sgfh_qty, node.sjcl_qty, node.qtcl_qty]);
  204. node.sgfh_tp = ZhCalc.mul(sgfh_qty, node.unit_price, decimal.tp) || 0;
  205. node.sjcl_tp = ZhCalc.mul(sjcl_qty, node.unit_price, decimal.tp) || 0;
  206. node.qtcl_tp = ZhCalc.mul(qtcl_qty, node.unit_price, decimal.tp) || 0;
  207. node.total_price = ZhCalc.mul(node.quantity, node.unit_price, decimal.tp) || 0;
  208. }
  209. }
  210. }
  211. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), 2);
  212. };
  213. const billsTree = createNewPathTree('revise', treeSetting);
  214. // 初始化 计量单元
  215. const pos = new PosData({ id: 'id', ledgerId: 'lid' });
  216. // 清单 相关方法&绑定spreadjs事件
  217. const billsTreeSpreadObj = {
  218. loadExprToInput(sheet) {
  219. const sel = sheet.getSelections()[0];
  220. const col = sheet.zh_setting.cols[sel.col], cell = sheet.getCell(sel.row, sel.col);
  221. if (col && col.type === 'Number') {
  222. const data = SpreadJsObj.getSelectObject(sheet);
  223. if (data) {
  224. const exprInfo = getExprInfo(col.field);
  225. if (exprInfo) {
  226. $('#bills-expr').val(data[exprInfo.expr] ? data[exprInfo.expr] : data[col.field])
  227. .attr('field', col.field).attr('org', data[col.field]);
  228. } else {
  229. $('#bills-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field]);
  230. }
  231. if (col.field.indexOf('dgn') >= 0) {
  232. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (_.isString(data.b_code) && data.b_code !== '') || !data.ccid);
  233. } else if (col.field === 'unit_price') {
  234. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0) || (_.isBoolean(data.used) && data.used === true) || !data.ccid);
  235. } else {
  236. const nodePos = pos.getLedgerPos(data.id);
  237. if (nodePos && nodePos.length > 0) {
  238. $('#bills-expr').val('').attr('readOnly', true);
  239. $('#bills-expr').removeAttr('data-row');
  240. } else {
  241. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0) || !data.ccid);
  242. }
  243. }
  244. $('#bills-expr').attr('data-row', sel.row);
  245. } else {
  246. $('#bills-expr').val('').attr('readOnly', true);
  247. $('#bills-expr').removeAttr('data-row');
  248. }
  249. } else {
  250. $('#bills-expr').val('').attr('readOnly', true);
  251. $('#bills-expr').removeAttr('data-row');
  252. }
  253. },
  254. getDefaultSelectInfo: function (sheet) {
  255. const tree = sheet.zh_tree;
  256. if (!tree) return;
  257. const sel = sheet.getSelections()[0];
  258. const node = sheet.zh_tree.nodes[sel.row];
  259. if (!node) return;
  260. let count = 1;
  261. if (sel.rowCount > 1) {
  262. for (let r = 1; r < sel.rowCount; r++) {
  263. const rNode = sheet.zh_tree.nodes[sel.row + r];
  264. if (rNode.level > node.level) continue;
  265. if ((rNode.level < node.level) || (rNode.level === node.level && rNode.pid !== node.pid)) {
  266. toastr.warning('请选择同一节点下的节点,进行该操作');
  267. return;
  268. }
  269. count += 1;
  270. }
  271. }
  272. return [tree, node, count];
  273. },
  274. /**
  275. * 刷新顶部按钮是否可用
  276. * @param sheet
  277. * @param selections
  278. */
  279. refreshOperationValid: function (sheet, selection) {
  280. const setObjEnable = function (obj, enable) {
  281. if (enable) {
  282. obj.removeClass('disabled');
  283. } else {
  284. obj.addClass('disabled');
  285. }
  286. };
  287. const invalidAll = function () {
  288. setObjEnable($('a[name=base-opr][type=add]'), false);
  289. setObjEnable($('a[name=base-opr][type=delete]'), false);
  290. setObjEnable($('a[name=base-opr][type=up-move]'), false);
  291. setObjEnable($('a[name=base-opr][type=down-move]'), false);
  292. setObjEnable($('a[name=base-opr][type=up-level]'), false);
  293. setObjEnable($('a[name=base-opr][type=down-level]'), false);
  294. };
  295. const sel = selection ? selection[0] : sheet.getSelections()[0];
  296. const row = sel ? sel.row : -1;
  297. const tree = sheet.zh_tree;
  298. if (!tree) {
  299. invalidAll();
  300. return;
  301. }
  302. const first = sheet.zh_tree.nodes[row];
  303. if (!first) {
  304. invalidAll();
  305. return;
  306. }
  307. let last = first, sameParent = true, nodeUsed = first.used;
  308. if (sel.rowCount > 1 && first) {
  309. for (let r = 1; r < sel.rowCount; r++) {
  310. const rNode = tree.nodes[sel.row + r];
  311. if (!rNode) {
  312. sameParent = false;
  313. break;
  314. }
  315. if (!rNode.ccid) {
  316. sameParent = false;
  317. break;
  318. }
  319. nodeUsed = nodeUsed || rNode.used;
  320. if (rNode.level > first.level) continue;
  321. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  322. sameParent = false;
  323. break;
  324. }
  325. last = rNode;
  326. }
  327. }
  328. const preNode = tree.getPreSiblingNode(first);
  329. const siblings = tree.getChildren(tree.getParent(first));
  330. let upPower = true;
  331. for (let order = last.order; order < siblings.length - 1; order ++) {
  332. if (!siblings[order].ccid) {
  333. upPower = false;
  334. break;
  335. }
  336. }
  337. const valid = !sheet.zh_setting.readOnly;
  338. setObjEnable($('a[name=base-opr][type=add]'), valid && first && first.level > 1);
  339. setObjEnable($('a[name=base-opr][type=delete]'), valid && first && sameParent && first.level > 1 && !nodeUsed && first.ccid);
  340. setObjEnable($('a[name=base-opr][type=up-move]'), valid && first && sameParent && first.level > 1 && preNode && first.ccid);
  341. setObjEnable($('a[name=base-opr][type=down-move]'), valid && first && sameParent && first.level > 1 && !tree.isLastSibling(last) && first.ccid);
  342. const posRange = last ? pos.getLedgerPos(last.id) : [];
  343. setObjEnable($('a[name=base-opr][type=up-level]'), valid && first && sameParent && tree.getParent(first) && !nodeUsed
  344. && first.level > 2 && ((!posRange || posRange.length === 0) || tree.isLastSibling(last)) && upPower && first.ccid);
  345. const preNodePosRange = preNode ? pos.getLedgerPos(preNode.id) : [];
  346. setObjEnable($('a[name=base-opr][type=down-level]'), valid && first && sameParent
  347. && first.level > 1 && preNode && (preNode.children.length > 0 || (preNode.children.length === 0 && preNode.ccid && (!preNodePosRange || preNodePosRange.length === 0))) && !preNode.used && first.ccid);
  348. setObjEnable($('#cut'), valid);
  349. setObjEnable($('#paste'), valid);
  350. },
  351. /**
  352. *
  353. * @param sheet
  354. * @param data
  355. */
  356. refreshTree: function (sheet, data) {
  357. SpreadJsObj.massOperationSheet(sheet, function () {
  358. const tree = sheet.zh_tree;
  359. // 处理删除
  360. if (data.delete) {
  361. data.delete.sort(function (a, b) {
  362. return b.deleteIndex - a.deleteIndex;
  363. });
  364. for (const d of data.delete) {
  365. sheet.deleteRows(d.deleteIndex, 1);
  366. }
  367. }
  368. // 处理新增
  369. if (data.create) {
  370. const newNodes = data.create;
  371. if (newNodes) {
  372. newNodes.sort(function (a, b) {
  373. return a.index - b.index;
  374. });
  375. for (const node of newNodes) {
  376. sheet.addRows(node.index, 1);
  377. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  378. }
  379. }
  380. }
  381. // 处理更新
  382. if (data.update) {
  383. const rows = [];
  384. for (const u of data.update) {
  385. rows.push(tree.nodes.indexOf(u));
  386. }
  387. SpreadJsObj.reLoadRowsData(sheet, rows);
  388. }
  389. // 处理展开
  390. if (data.expand) {
  391. const expanded = [];
  392. for (const e of data.expand) {
  393. if (expanded.indexOf(e) === -1) {
  394. const posterity = tree.getPosterity(e);
  395. for (const p of posterity) {
  396. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  397. expanded.push(p);
  398. }
  399. }
  400. }
  401. }
  402. });
  403. },
  404. selectionChanged: function (e, info) {
  405. if (info.newSelections) {
  406. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row || info.newSelections[0].rowCount !== info.oldSelections[0].rowCount) {
  407. billsTreeSpreadObj.refreshOperationValid(info.sheet);
  408. billsTreeSpreadObj.refreshPosData();
  409. }
  410. }
  411. billsTreeSpreadObj.loadExprToInput(info.sheet);
  412. },
  413. refreshPosData: function() {
  414. SpreadJsObj.resetTopAndSelect(posSheet);
  415. posSpreadObj.loadCurPosData();
  416. SpreadJsObj.saveTopAndSelect(billsSheet, ckBillsSpread);
  417. posSearch.search($('#pos-keyword').val());
  418. },
  419. /**
  420. * 新增节点
  421. * @param spread
  422. */
  423. baseOpr: function (sheet, type, addCount = 1) {
  424. const self = this;
  425. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  426. if (!tree || !node || !count) return;
  427. if (type === 'delete') {
  428. const parent = tree.getParent(node);
  429. const children = parent ? parent.children : tree.children;
  430. const index = children.indexOf(node);
  431. for (let i = 0; i < count; i++) {
  432. const child = children[i+index];
  433. if (tree.checkNodeUsed(child, pos)) {
  434. toastr.warning('选中的节点已计量,不可删除');
  435. return;
  436. }
  437. }
  438. } else if (type === 'up-level') {
  439. const parent = tree.getParent(node);
  440. const children = parent ? parent.children : tree.children;
  441. const index = children.indexOf(node);
  442. for (let i = index; i < children.length; i++) {
  443. const child = children[index];
  444. if (tree.checkNodeUsed(child, pos)) {
  445. if (i >= index + count) {
  446. toastr.warning('其后节点已计量,选中的节点不可升级');
  447. } else {
  448. toastr.warning('选中的节点已计量,不可升级');
  449. }
  450. return;
  451. }
  452. }
  453. } else if (type === 'down-level') {
  454. const parent = tree.getParent(node);
  455. const children = parent ? parent.children : tree.children;
  456. const index = children.indexOf(node);
  457. for (let i = 0; i < count; i++) {
  458. const child = children[i+index];
  459. if (tree.checkNodeUsed(child, pos)) {
  460. toastr.warning('选中的节点已计量,不可降级');
  461. return;
  462. }
  463. }
  464. }
  465. if (type === 'delete') {
  466. deleteAfterHint(function () {
  467. postData(window.location.pathname + '/update', {
  468. postType: type,
  469. postData: {
  470. id: node.ledger_id,
  471. count: type === 'add' ? addCount : count,
  472. }
  473. }, function (result) {
  474. const refreshData = tree.loadPostData(result);
  475. self.refreshTree(sheet, refreshData);
  476. const sel = sheet.getSelections()[0];
  477. if (sel) {
  478. sheet.setSelection(sel.row, sel.col, 1, sel.colCount);
  479. billsTreeSpreadObj.refreshPosData();
  480. }
  481. self.refreshOperationValid(sheet);
  482. });
  483. });
  484. } else {
  485. postData(window.location.pathname + '/update', {
  486. postType: type,
  487. postData: {
  488. id: node.ledger_id,
  489. count: type === 'add' ? addCount : count,
  490. }
  491. }, function (result) {
  492. const refreshData = tree.loadPostData(result);
  493. self.refreshTree(sheet, refreshData);
  494. if (['up-move', 'down-move'].indexOf(type) > -1) {
  495. const sel = sheet.getSelections()[0];
  496. if (sel) {
  497. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  498. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(node)]);
  499. }
  500. } else if (type === 'add') {
  501. const sel = sheet.getSelections()[0];
  502. if (sel) {
  503. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  504. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(refreshData.create[0])]);
  505. billsTreeSpreadObj.refreshPosData();
  506. }
  507. }
  508. self.refreshOperationValid(sheet);
  509. });
  510. }
  511. },
  512. /**
  513. * 编辑单元格响应事件
  514. * @param {Object} e
  515. * @param {Object} info
  516. */
  517. editEnded: function (e, info) {
  518. if (info.sheet.zh_setting) {
  519. const col = info.sheet.zh_setting.cols[info.col];
  520. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  521. const node = sortData[info.row];
  522. const data = {
  523. id: node.id,
  524. tender_id: node.tender_id,
  525. ledger_id: node.ledger_id
  526. };
  527. // 未改变值则不提交
  528. const orgValue = node[col.field];
  529. const newValue = trimInvalidChar(info.editingText);
  530. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (newValue === ''))) {
  531. return;
  532. }
  533. // 台账模式,检查计量单元相关
  534. if (isTz) {
  535. if (col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  536. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  537. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  538. if (!node.children || node.children.length ===0) {
  539. const lPos = pos.getLedgerPos(node.id);
  540. if (lPos && lPos.length > 0) {
  541. toastr.error('清单含有计量单元,不可修改施工图复核数量');
  542. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  543. return;
  544. }
  545. }
  546. }
  547. if (col.field === 'b_code' && (newValue === '' || !newValue)) {
  548. const lPos = pos.getLedgerPos(node.id);
  549. if (lPos && lPos.length > 0) {
  550. toastr.error('清单含有计量单元,请先删除计量单元,再删除清单编号');
  551. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  552. return;
  553. }
  554. }
  555. }
  556. // 获取更新数据
  557. const exprInfo = getExprInfo(col.field);
  558. if (info.editingText) {
  559. const text = newValue;
  560. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '' && text === '') {
  561. toastr.error('节点已计量,请勿删除编号');
  562. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  563. return;
  564. }
  565. if (col.type === 'Number') {
  566. const num = _.toNumber(text);
  567. if (_.isFinite(num)) {
  568. data[col.field] = num;
  569. if (exprInfo) {
  570. data[exprInfo.expr] = '';
  571. }
  572. } else {
  573. try {
  574. data[col.field] = math.evaluate(transExpr(text));
  575. if (exprInfo) {
  576. data[exprInfo.expr] = newValue;
  577. }
  578. } catch(err) {
  579. toastr.error('输入的表达式非法');
  580. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  581. return;
  582. }
  583. }
  584. } else {
  585. data[col.field] = text;
  586. }
  587. } else {
  588. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '') {
  589. toastr.error('节点已计量,请勿删除编号');
  590. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  591. return;
  592. }
  593. data[col.field] = null;
  594. if (exprInfo) {
  595. data[exprInfo.expr] = '';
  596. }
  597. }
  598. // 更新至服务器
  599. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  600. const refreshNode = billsTree.loadPostData(result);
  601. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  602. });
  603. }
  604. },
  605. clipboardPasting: function (e, info) {
  606. const tree = info.sheet.zh_tree, setting = info.sheet.zh_setting;
  607. info.cancel = true;
  608. if (!setting || !tree) return;
  609. // const range = info.cellRange;
  610. // for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  611. // const node = tree.nodes[iRow];
  612. // if (tree.checkNodeUsed(node, pos)) {
  613. // toastr.warning('"' + node.code + node.b_code + ' ' + node.name +'"已计量,请勿修改');
  614. // return;
  615. // }
  616. // }
  617. const pasteData = info.pasteData.html
  618. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  619. : (info.pasteData.text === ''
  620. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  621. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  622. const hint = {
  623. usedUp: {type: 'warning', msg: '节点已计量,不可修改单价'},
  624. usedCode: {type: 'warning', msg: '节点已计量,编号不可修改为空值'},
  625. invalidExpr: {type: 'warning', msg: '粘贴的表达式非法'},
  626. posCode: {type: 'warning', msg: '清单含有计量单元,不可粘贴清单编号为空'},
  627. posQty: {type: 'warning', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可粘贴'},
  628. parent: {type: 'warning', msg: '含有子项的清单,不可粘贴数量、单价、金额'},
  629. gcl: {type: 'warning', msg: '工程量清单,不可粘贴项目节数量'},
  630. posXmj: {type: 'warning', msg: '清单含有计量单元,不可粘贴项目节编号'},
  631. sameParent: {type: 'warning', msg: '仅可粘贴同层节点'},
  632. };
  633. const datas = [], filterNodes = [];
  634. let pid, level, filterRow = 0;
  635. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  636. const curRow = info.cellRange.row + iRow;
  637. const node = tree.nodes[curRow];
  638. if (!node) continue;
  639. if (!pid) pid = node.ledger_pid;
  640. if (!level) level = node.level;
  641. if (node.ledger_pid !== pid) {
  642. toastMessageUniq(hint.sameParent);
  643. filterRow+=1;
  644. continue;
  645. }
  646. if (node.level < level) break;
  647. let bPaste = false;
  648. const data = info.sheet.zh_tree.getNodeKeyData(node);
  649. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  650. const curCol = info.cellRange.col + iCol;
  651. const colSetting = info.sheet.zh_setting.cols[curCol];
  652. const value = trimInvalidChar(pasteData[iRow-filterRow][iCol]);
  653. if (node.children && node.children.length > 0 && invalidFields.parent.indexOf(colSetting.field) >= 0) {
  654. toastMessageUniq(hint.parent);
  655. continue;
  656. }
  657. if (!_.isEmpty(node.b_code) && invalidFields.gcl.indexOf(colSetting.field) >= 0) {
  658. toastMessageUniq(hint.gcl);
  659. continue;
  660. }
  661. const lPos = pos.getLedgerPos(node.id);
  662. if (lPos && lPos.length > 0) {
  663. if (value === '' && colSetting.field === 'b_code') {
  664. toastMessageUniq(hint.posCode);
  665. continue;
  666. }
  667. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  668. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  669. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  670. toastMessageUniq(hint.posQty);
  671. continue;
  672. }
  673. if (value !== '' && invalidFields.posXmj.indexOf(colSetting.field) >= 0) {
  674. toastMessageUniq(hint.posXmj);
  675. continue;
  676. }
  677. }
  678. if (tree.checkNodeUsed(node, pos) && colSetting.field === 'unit_price') {
  679. toastMessageUniq (hint.usedUp);
  680. continue;
  681. }
  682. if (colSetting.type === 'Number') {
  683. const num = _.toNumber(value);
  684. if (num) {
  685. data[colSetting.field] = num;
  686. } else {
  687. try {
  688. data[colSetting.field] = math.evaluate(transExpr(value));
  689. const exprInfo = getExprInfo(colSetting.field);
  690. if (exprInfo) {
  691. data[exprInfo.expr] = value;
  692. }
  693. bPaste = true;
  694. } catch (err) {
  695. toastMessageUniq(hint.invalidExpr);
  696. continue;
  697. }
  698. }
  699. } else {
  700. if (node.used && (colSetting.field === 'code' || colSetting.field ==='b_code')
  701. && data[colSetting.field] !== '' && value === '') {
  702. toastMessageUniq(hint.usedCode);
  703. continue;
  704. }
  705. data[colSetting.field] = value;
  706. }
  707. bPaste = true;
  708. }
  709. if (bPaste) {
  710. datas.push(data);
  711. } else {
  712. filterNodes.push(node);
  713. }
  714. }
  715. if (datas.length > 0) {
  716. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  717. const refreshNode = tree.loadPostData(result);
  718. if (refreshNode.update) {
  719. refreshNode.update = refreshNode.update.concat(filterNodes);
  720. }
  721. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  722. }, function () {
  723. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  724. });
  725. } else {
  726. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  727. }
  728. },
  729. clipboardPasted: function (e, info) {
  730. const hint = {
  731. usedUp: {type: 'error', msg: '节点已计量,不可修改单价'},
  732. usedCode: {type: 'error', msg: '节点已计量,编号不可修改为空值'},
  733. invalidExpr: {type: 'error', msg: '粘贴的表达式非法'},
  734. posCode: {type: 'error', msg: '清单含有计量单元,请先删除计量单元,再修改清单编号为空'},
  735. posQty: {type: 'error', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可修改'},
  736. };
  737. const tree = info.sheet.zh_tree;
  738. if (!tree) { return; }
  739. const sortData = info.sheet.zh_tree.nodes;
  740. const datas = [], filterNodes = [];
  741. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  742. let bPaste = false;
  743. const curRow = info.cellRange.row + iRow;
  744. const node = sortData[curRow];
  745. if (node) {
  746. const data = info.sheet.zh_tree.getNodeKeyData(node);
  747. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  748. const curCol = info.cellRange.col + iCol;
  749. const colSetting = info.sheet.zh_setting.cols[curCol];
  750. const value = trimInvalidChar(info.sheet.getText(curRow, curCol));
  751. const lPos = pos.getLedgerPos(node.id);
  752. if (lPos && lPos.length > 0) {
  753. if (value === '' && colSetting.field === 'b_code') {
  754. toastMessageUniq(hint.posCode);
  755. continue;
  756. }
  757. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  758. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  759. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  760. toastMessageUniq(hint.posQty);
  761. continue;
  762. }
  763. }
  764. if (node.used && col.field === 'unit_price') {
  765. toastMessageUniq (hint.usedUp);
  766. continue;
  767. }
  768. if (colSetting.type === 'Number') {
  769. const num = _.toNumber(value);
  770. if (num) {
  771. data[colSetting.field] = num;
  772. } else {
  773. try {
  774. data[colSetting.field] = math.evaluate(transExpr(value));
  775. } catch (err) {
  776. toastMessageUniq(hint.invalidExpr);
  777. continue;
  778. }
  779. }
  780. } else {
  781. if (node.used && (col.field === 'code' || col.field ==='b_code')
  782. && data[colSetting.field] !== '' && value === '') {
  783. toastMessageUniq(hint.usedCode);
  784. continue;
  785. }
  786. data[colSetting.field] = value;
  787. }
  788. bPaste = true;
  789. }
  790. if (bPaste) {
  791. datas.push(data);
  792. } else {
  793. filterNodes.push(node);
  794. }
  795. }
  796. }
  797. if (datas.length > 0) {
  798. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  799. const refreshNode = tree.loadPostData(result);
  800. if (refreshNode.update) {
  801. refreshNode.update = refreshNode.update.concat(filterNodes);
  802. }
  803. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  804. }, function () {
  805. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  806. });
  807. } else {
  808. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  809. }
  810. },
  811. deletePress: function (sheet) {
  812. if (!sheet.zh_setting) return;
  813. const sel = sheet.getSelections()[0], datas = [];
  814. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  815. let bDel = false;
  816. const node = sheet.zh_tree.nodes[iRow];
  817. const data = sheet.zh_tree.getNodeKeyData(node);
  818. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  819. const col = sheet.zh_setting.cols[iCol];
  820. const style = sheet.getStyle(iRow, iCol);
  821. if (style.locked || (['dgn_qty1', 'dgn_qty2'].indexOf(col.field) >= 0 && node.b_code)) continue;
  822. if (['dgn_qty1', 'dgn_qty2'].indexOf(col.field) < 0 && sheet.zh_tree.checkNodeUsed(node, pos)) {
  823. toastr.warning('"' + (node.code || '') + (node.b_code || '') + ' ' + node.name +'"已计量,请勿修改');
  824. return;
  825. }
  826. data[col.field] = null;
  827. const exprInfo = getExprInfo(col.field);
  828. if (exprInfo) {
  829. data[exprInfo.expr] = '';
  830. }
  831. bDel = true;
  832. }
  833. if (bDel) datas.push(data);
  834. }
  835. if (datas.length > 0) {
  836. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  837. const refreshNode = sheet.zh_tree.loadPostData(result);
  838. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  839. billsTreeSpreadObj.loadExprToInput(sheet);
  840. });
  841. }
  842. },
  843. pasteBlock: function (spread, copyInfo) {
  844. const self = this;
  845. const sheet = spread.getActiveSheet();
  846. const [tree, node] = this.getDefaultSelectInfo(spread.getActiveSheet());
  847. postData(window.location.pathname + '/update', {
  848. postType: 'paste-block',
  849. postData: {
  850. id: tree.getNodeKey(node),
  851. tid: copyInfo.tid,
  852. block: copyInfo.block,
  853. }
  854. }, function (data) {
  855. pos.updateDatas(data.pos);
  856. const result = tree.loadPostData(data.ledger);
  857. self.refreshTree(sheet, result);
  858. const sel = sheet.getSelections()[0];
  859. if (sel) {
  860. sheet.setSelection(tree.nodes.indexOf(result.create[0]), sel.col, sel.rowCount, sel.colCount);
  861. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(result.create[0])]);
  862. }
  863. self.refreshOperationValid(sheet);
  864. removeLocalCache(copyBlockTag);
  865. }, null, true);
  866. },
  867. topRowChanged: function (e, info) {
  868. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  869. },
  870. editStarting(e, info) {
  871. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  872. const col = info.sheet.zh_setting.cols[info.col];
  873. const node = info.sheet.zh_tree.nodes[info.row];
  874. if (!node) {
  875. info.cancel = true;
  876. return;
  877. }
  878. const exprInfo = getExprInfo(col.field);
  879. if (exprInfo) {
  880. if (node[exprInfo.expr] && node[exprInfo.expr] !== '') {
  881. info.sheet.getCell(info.row, info.col).text(node[exprInfo.expr]);
  882. }
  883. }
  884. switch (col.field) {
  885. case 'code':
  886. const posRange = pos.getLedgerPos(node.id);
  887. info.cancel = posRange && posRange.length > 0;
  888. break;
  889. case 'unit_price':
  890. info.cancel = (node.children && node.children.length > 0) || node.used;
  891. break;
  892. case 'sgfh_qty':
  893. case 'sgfh_tp':
  894. case 'sjcl_qty':
  895. case 'sjcl_tp':
  896. case 'qtcl_qty':
  897. case 'qtcl_tp':
  898. case 'deal_qty':
  899. case 'deal_tp':
  900. info.cancel = (node.children && node.children.length > 0);
  901. break;
  902. case 'dgn_qty1':
  903. case 'dgn_qty2':
  904. info.cancel = !_.isEmpty(node.b_code);
  905. break;
  906. }
  907. },
  908. cut: function (sheet, sel, callback) {
  909. if (!sheet || !sel) return;
  910. if (sel.colCount >= sheet.zh_setting.cols.length) {
  911. toastr.warning('请勿选中整行剪切');
  912. return;
  913. }
  914. const sortData = SpreadJsObj.getSortData(sheet), datas = [];
  915. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  916. const node = sortData[iRow];
  917. if (node) {
  918. const data = sheet.zh_tree.getNodeKeyData(node);
  919. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  920. const col = sheet.zh_setting.cols[iCol];
  921. if (col.field === 'b_code' || col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  922. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  923. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  924. const lPos = pos.getLedgerPos(node.id);
  925. if (lPos && lPos.length > 0) {
  926. toastr.error('不可剪切');
  927. return;
  928. }
  929. }
  930. const style = sheet.getStyle(iRow, iCol);
  931. if (style.locked) {
  932. toastr.error('不可剪切');
  933. return;
  934. }
  935. const colSetting = sheet.zh_setting.cols[iCol];
  936. data[colSetting.field] = null;
  937. const exprInfo = getExprInfo(colSetting.field);
  938. if (exprInfo) {
  939. data[exprInfo.expr] = '';
  940. }
  941. }
  942. datas.push(data);
  943. }
  944. }
  945. if (datas.length > 0) {
  946. callback();
  947. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  948. const refreshNode = sheet.zh_tree.loadPostData(result);
  949. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  950. });
  951. }
  952. },
  953. sortCode: function (sheet) {
  954. const tree = sheet.zh_tree;
  955. if (!tree) return;
  956. const select = SpreadJsObj.getSelectObject(sheet);
  957. if (!select || !select.code || select.code === '') return;
  958. const recursiveSortCode = function (data, parentCode, children) {
  959. if (!children || children.length === 0) return;
  960. for (const [i, child] of children.entries()) {
  961. if (!child.b_code || child.b_code === '') {
  962. const code = parentCode + '-' + (i + 1);
  963. const cData = tree.getNodeKeyData(child);
  964. cData.code = code;
  965. data.push(cData);
  966. if (!tree.isLeafXmj(child)) {
  967. recursiveSortCode(data, code, child.children);
  968. }
  969. }
  970. }
  971. };
  972. const data = [];
  973. recursiveSortCode(data, select.code, select.children);
  974. if (data.length > 0) {
  975. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  976. const refreshNode = tree.loadPostData(result);
  977. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  978. })
  979. }
  980. }
  981. };
  982. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  983. billsTreeSpreadObj.loadExprToInput(billsSheet);
  984. billsSpread.bind(spreadNS.Events.SelectionChanged, billsTreeSpreadObj.selectionChanged);
  985. billsSpread.bind(spreadNS.Events.topRowChanged, billsTreeSpreadObj.topRowChanged);
  986. if (!readOnly) {
  987. // 增删上下移升降级
  988. $('a[name="base-opr"]').click(function () {
  989. billsTreeSpreadObj.baseOpr(billsSheet, this.getAttribute('type'));
  990. });
  991. $('a[name=cpc]').click(function () {
  992. billsSpread.commandManager().execute({
  993. cmd: this.getAttribute('type'),
  994. sheetName: billsSheet.name()
  995. });
  996. });
  997. $('#bills-expr').bind('change onblur', function () {
  998. if (this.readOnly) return;
  999. const expr = $(this);
  1000. const row = expr.attr('data-row') ? _.toInteger(expr.attr('data-row')) : -1;
  1001. const select = billsTree.getItemsByIndex(row);
  1002. if (!select) return;
  1003. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  1004. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  1005. const data = {
  1006. id: select.id,
  1007. tender_id: select.tender_id,
  1008. ledger_id: select.ledger_id
  1009. };
  1010. const exprInfo = getExprInfo(field);
  1011. if (newValue !== '') {
  1012. const num = _.toNumber(newValue);
  1013. if (num) {
  1014. data[field] = num;
  1015. if (exprInfo) {
  1016. data[exprInfo.expr] = '';
  1017. }
  1018. } else {
  1019. try {
  1020. data[field] = math.evaluate(transExpr(newValue));
  1021. if (exprInfo) {
  1022. data[exprInfo.expr] = newValue;
  1023. }
  1024. } catch (err) {
  1025. toastr.error('输入的表达式非法');
  1026. return;
  1027. }
  1028. }
  1029. } else {
  1030. data[field] = null;
  1031. if (exprInfo) {
  1032. data[exprInfo.expr] = '';
  1033. }
  1034. }
  1035. // 更新至服务器
  1036. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  1037. const refreshNode = billsTree.loadPostData(result);
  1038. billsTreeSpreadObj.refreshTree(billsSheet, refreshNode);
  1039. });
  1040. });
  1041. billsSpread.bind(spreadNS.Events.EditStarting, billsTreeSpreadObj.editStarting);
  1042. billsSpread.bind(spreadNS.Events.EditEnded, billsTreeSpreadObj.editEnded);
  1043. billsSpread.bind(spreadNS.Events.ClipboardPasting, billsTreeSpreadObj.clipboardPasting);
  1044. billsSpread.bind(spreadNS.Events.ClipboardChanging, function (e, info) {
  1045. const copyText = SpreadJsObj.getFilterCopyText(info.sheet);
  1046. SpreadJsObj.Clipboard.setCopyData(copyText);
  1047. });
  1048. SpreadJsObj.addDeleteBind(billsSpread, billsTreeSpreadObj.deletePress);
  1049. SpreadJsObj.addCutEvents(billsSpread, billsTreeSpreadObj.cut);
  1050. }
  1051. // 右键菜单
  1052. let batchInsertObj;
  1053. $.contextMenu.types.batchInsert = function (item, opt, root) {
  1054. const self = this;
  1055. if ($.isFunction(item.icon)) {
  1056. item._icon = item.icon.call(this, this, $t, key, item);
  1057. } else {
  1058. if (typeof(item.icon) === 'string' && item.icon.substring(0, 3) === 'fa-') {
  1059. // to enable font awesome
  1060. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '--fa fa ' + item.icon;
  1061. } else {
  1062. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '-' + item.icon;
  1063. }
  1064. }
  1065. this.addClass(item._icon);
  1066. 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>')
  1067. .appendTo(this);
  1068. const $input = $obj.find('input');
  1069. const event = () => {
  1070. if (self.hasClass('context-menu-disabled')) return;
  1071. item.batchInsert($input[0], root);
  1072. };
  1073. $obj.on('click', event).keypress(function (e) {if (e.keyCode === 13) { event(); }});
  1074. $input.click((e) => {e.stopPropagation();})
  1075. .keyup((e) => {if (e.keyCode === 13) item.batchInsert($input[0], root);})
  1076. .on('input', function () {this.value = this.value.replace(/[^\d]/g, '');});
  1077. };
  1078. const billsContextMenuOptions = {
  1079. selector: '#bills-spread',
  1080. build: function ($trigger, e) {
  1081. const target = SpreadJsObj.safeRightClickSelection($trigger, e, billsSpread);
  1082. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1083. },
  1084. items: {}
  1085. };
  1086. if (!readOnly) {
  1087. billsContextMenuOptions.items.create = {
  1088. name: '新增',
  1089. icon: 'fa-sign-in',
  1090. callback: function (key, opt) {
  1091. billsTreeSpreadObj.baseOpr(billsSheet, 'add');
  1092. },
  1093. disabled: function (key, opt) {
  1094. const sheet = billsSheet;
  1095. const selection = sheet.getSelections();
  1096. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1097. const row = sel ? sel.row : -1;
  1098. const tree = sheet.zh_tree;
  1099. if (!tree) return true;
  1100. const first = sheet.zh_tree.nodes[row];
  1101. const valid = !sheet.zh_setting.readOnly;
  1102. return !(valid && first && first.level > 1);
  1103. }
  1104. };
  1105. billsContextMenuOptions.items.delete = {
  1106. name: '删除',
  1107. icon: 'fa-remove',
  1108. callback: function (key, opt) {
  1109. billsTreeSpreadObj.baseOpr(billsSheet, 'delete');
  1110. },
  1111. disabled: function (key, opt) {
  1112. const sheet = billsSheet;
  1113. const selection = sheet.getSelections();
  1114. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1115. const row = sel ? sel.row : -1;
  1116. const tree = sheet.zh_tree;
  1117. if (!tree) return true;
  1118. const first = sheet.zh_tree.nodes[row];
  1119. let last = first, sameParent = true, nodeUsed = first.used;
  1120. if (sel.rowCount > 1 && first) {
  1121. for (let r = 1; r < sel.rowCount; r++) {
  1122. const rNode = tree.nodes[sel.row + r];
  1123. if (!rNode) {
  1124. sameParent = false;
  1125. break;
  1126. }
  1127. if (!rNode.ccid) {
  1128. return true;
  1129. }
  1130. nodeUsed = nodeUsed || rNode.used;
  1131. if (rNode.level > first.level) continue;
  1132. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  1133. sameParent = false;
  1134. break;
  1135. }
  1136. last = rNode;
  1137. }
  1138. }
  1139. const valid = !sheet.zh_setting.readOnly;
  1140. return !(valid && first && sameParent && !(first.level === 1 && first.node_type) && !nodeUsed && first.ccid);
  1141. }
  1142. };
  1143. billsContextMenuOptions.items.sprBase = '----';
  1144. }
  1145. billsContextMenuOptions.items.copyBlock = {
  1146. name: '复制整块',
  1147. icon: 'fa-files-o',
  1148. callback: function (key, opt) {
  1149. const copyBlockList = [];
  1150. const sheet = billsSheet;
  1151. const sel = sheet.getSelections()[0];
  1152. let iRow = sel.row;
  1153. const pid = sheet.zh_tree.nodes[iRow].ledger_pid;
  1154. while (iRow < sel.row + sel.rowCount) {
  1155. const node = sheet.zh_tree.nodes[iRow];
  1156. if (node.ledger_pid !== pid) {
  1157. toastr.error('仅可同时选中同层节点');
  1158. return;
  1159. }
  1160. const posterity = sheet.zh_tree.getPosterity(node);
  1161. iRow += posterity.length + 1;
  1162. posterity.unshift(node);
  1163. copyBlockList.push(sheet.zh_tree.getDefaultData(posterity));
  1164. }
  1165. for (const cbl of copyBlockList) {
  1166. for (const b of cbl) {
  1167. const posRange = pos.getLedgerPos(b.id);
  1168. if (posRange && posRange.length > 0) b.pos = posRange;
  1169. }
  1170. }
  1171. setLocalCache(copyBlockTag, JSON.stringify({ block: copyBlockList }));
  1172. },
  1173. visible: function (key, opt) {
  1174. const sheet = billsSheet;
  1175. const selection = sheet.getSelections();
  1176. const row = selection[0].row;
  1177. const select = billsTree.nodes[row];
  1178. return select;
  1179. },
  1180. disabled: function (key, opt) {
  1181. const sheet = billsSheet;
  1182. const selection = sheet.getSelections();
  1183. const row = selection[0].row;
  1184. const select = billsTree.nodes[row];
  1185. return select && select.level <= 1;
  1186. }
  1187. };
  1188. billsContextMenuOptions.items.copyBlockXmj = {
  1189. name: '复制整块(只复制项目节)',
  1190. icon: 'fa-files-o',
  1191. callback: function (key, opt) {
  1192. const copyBlockList = [];
  1193. const sheet = billsSheet;
  1194. const sel = sheet.getSelections()[0];
  1195. let iRow = sel.row;
  1196. const pid = sheet.zh_tree.nodes[iRow].ledger_pid;
  1197. while (iRow < sel.row + sel.rowCount) {
  1198. const node = sheet.zh_tree.nodes[iRow];
  1199. if (node.ledger_pid !== pid) {
  1200. toastr.error('仅可同时选中同层节点');
  1201. return;
  1202. }
  1203. const posterity = sheet.zh_tree.getPosterity(node);
  1204. iRow += posterity.length + 1;
  1205. const copyPosterity = posterity.filter(x => { return !x.b_code; });
  1206. copyPosterity.unshift(node);
  1207. const copyData = sheet.zh_tree.getDefaultData(copyPosterity);
  1208. for (const p of copyData) {
  1209. const children = copyData.filter(y => {return y.ledger_pid === p.ledger_id}) || [];
  1210. p.is_leaf = children.length === 0;
  1211. }
  1212. copyBlockList.push(copyData);
  1213. }
  1214. setLocalCache(copyBlockTag, JSON.stringify({ block: copyBlockList }));
  1215. },
  1216. visible: function (key, opt) {
  1217. const sheet = billsSheet;
  1218. const selection = sheet.getSelections();
  1219. const row = selection[0].row;
  1220. const select = billsTree.nodes[row];
  1221. return select;
  1222. },
  1223. disabled: function (key, opt) {
  1224. const sheet = billsSheet;
  1225. const selection = sheet.getSelections();
  1226. const row = selection[0].row;
  1227. const select = billsTree.nodes[row];
  1228. return select && select.level <= 1;
  1229. }
  1230. };
  1231. if (!readOnly) {
  1232. billsContextMenuOptions.items.pasteBlock = {
  1233. name: '粘贴整块',
  1234. icon: 'fa-clipboard',
  1235. disabled: function (key, opt) {
  1236. //const block = treeOperationObj.block || [];
  1237. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1238. return !(copyInfo && copyInfo.block && copyInfo.block.length > 0);
  1239. },
  1240. callback: function (key, opt) {
  1241. //const block = treeOperationObj.block || [];
  1242. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1243. if (copyInfo.block.length > 0) {
  1244. billsTreeSpreadObj.pasteBlock(billsSpread, copyInfo);
  1245. } else {
  1246. document.execCommand('paste');
  1247. }
  1248. },
  1249. visible: function (key, opt) {
  1250. return !readOnly;
  1251. }
  1252. };
  1253. billsContextMenuOptions.items.sprBlock = '----';
  1254. }
  1255. if (!readOnly) {
  1256. billsContextMenuOptions.items.batchInsert = {
  1257. name: '批量插入',
  1258. type: 'batchInsert',
  1259. value: '2',
  1260. icon: 'fa-sign-in',
  1261. batchInsert: function (obj, root) {
  1262. if (_.toNumber(obj.value) > _.toNumber(obj.max)) {
  1263. obj.value = obj.max;
  1264. toastr.warning('批量插入不可多于' + obj.max);
  1265. } else if (_.toNumber(obj.value) < _.toNumber(obj.min)) {
  1266. obj.value = obj.min;
  1267. toastr.warning('批量插入不可少于' + obj.min);
  1268. } else {
  1269. billsTreeSpreadObj.baseOpr(billsSheet, 'add', parseInt(obj.value));
  1270. root.$menu.trigger('contextmenu:hide');
  1271. }
  1272. },
  1273. disabled: function (key, opt) {
  1274. const sheet = billsSheet;
  1275. const selection = sheet.getSelections();
  1276. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1277. const row = sel ? sel.row : -1;
  1278. const tree = sheet.zh_tree;
  1279. if (!tree) return true;
  1280. const first = sheet.zh_tree.nodes[row];
  1281. const valid = !sheet.zh_setting.readOnly;
  1282. return !(valid && first && first.level > 1);
  1283. }
  1284. };
  1285. billsContextMenuOptions.items.batchInsertBillsPos = {
  1286. name: '批量插入清单-计量单元',
  1287. icon: 'fa-sign-in',
  1288. visible: function (key, opt) {
  1289. return false;
  1290. },
  1291. disabled: function (key, opt) {
  1292. if (!isTz) return true;
  1293. const select = SpreadJsObj.getSelectObject(billsSheet);
  1294. if (select) {
  1295. if (select.code && select.code !== '') {
  1296. return !billsTree.isLeafXmj(select);
  1297. } else {
  1298. const parent = billsTree.getParent(select);
  1299. return !(parent && billsTree.isLeafXmj(parent));
  1300. }
  1301. } else {
  1302. return false;
  1303. }
  1304. },
  1305. callback: function (key, opt) {
  1306. if (!batchInsertObj) {
  1307. batchInsertObj = new BatchInsertBillsPosObj($('#batch'));
  1308. } else {
  1309. batchInsertObj.initView();
  1310. }
  1311. $('#batch').modal('show');
  1312. }
  1313. };
  1314. }
  1315. billsContextMenuOptions.items.sprTag = '----';
  1316. billsContextMenuOptions.items.showLast = {
  1317. name: '显示至最底层',
  1318. callback: function (key, opt, menu, e) {
  1319. const node = SpreadJsObj.getSelectObject(billsSheet);
  1320. setTimeout(() => {
  1321. showWaitingView();
  1322. billsTree.expandByCustom(x => {
  1323. return x.expanded || (x.id === node.id) || (x.full_path.indexOf(node.ledger_id + '-') >= 0);
  1324. });
  1325. SpreadJsObj.refreshTreeRowVisible(billsSheet);
  1326. closeWaitingView();
  1327. }, 100);
  1328. },
  1329. };
  1330. $.contextMenu(billsContextMenuOptions);
  1331. // 计量单元 相关方法&绑定spreadjs事件
  1332. const posSpreadObj = {
  1333. refreshOperationValid: function (sheet, selection) {
  1334. const setObjEnable = function (obj, enable) {
  1335. if (enable) {
  1336. obj.removeClass('disabled');
  1337. } else {
  1338. obj.addClass('disabled');
  1339. }
  1340. };
  1341. const invalidAll = function () {
  1342. setObjEnable($('a[name=pos-opr][type=up-move]'), false);
  1343. setObjEnable($('a[name=pos-opr][type=down-move]'), false);
  1344. };
  1345. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1346. const row = sel ? sel.row : -1;
  1347. const first = sheet.zh_data[row];
  1348. if (!first) {
  1349. invalidAll();
  1350. return;
  1351. }
  1352. let last = first;
  1353. if (sel.rowCount > 1 && first) {
  1354. for (let r = 1; r < sel.rowCount; r++) {
  1355. const rNode = sheet.zh_data[sel.row + r];
  1356. if (!rNode) break;
  1357. last = rNode;
  1358. }
  1359. }
  1360. const preNode = sheet.zh_data[row - 1];
  1361. const valid = !sheet.zh_setting.readOnly;
  1362. setObjEnable($('a[name=pos-opr][type=up-move]'), valid && first && preNode && first.ccid);
  1363. setObjEnable($('a[name=pos-opr][type=down-move]'), valid && first && (sheet.zh_data.indexOf(last) < sheet.zh_data.length - 1) && first.ccid);
  1364. },
  1365. loadExprToInput: function () {
  1366. const sel = posSheet.getSelections()[0];
  1367. if (!sel) return;
  1368. const col = posSheet.zh_setting.cols[sel.col];
  1369. const cell = posSheet.getCell(sel.col, sel.col);
  1370. if (col && col.type === 'Number') {
  1371. const data = SpreadJsObj.getSelectObject(posSheet);
  1372. if (data) {
  1373. const exprInfo = getExprInfo(col.field);
  1374. const value = exprInfo
  1375. ? (data[exprInfo.expr] ? data[exprInfo.expr] : data[col.field])
  1376. : data[col.field];
  1377. $('#pos-expr').val(value).attr('field', col.field).attr('org', data[col.field])
  1378. .attr('readOnly', readOnly || (!data.ccid && cell.locked())).attr('data-row', sel.row);
  1379. } else {
  1380. $('#pos-expr').val('').attr('readOnly', true);
  1381. $('#pos-expr').removeAttr('data-row');
  1382. }
  1383. } else {
  1384. $('#pos-expr').val('').attr('readOnly', true);
  1385. $('#pos-expr').removeAttr('data-row');
  1386. }
  1387. },
  1388. /**
  1389. * 加载计量单元 根据当前台账选择节点
  1390. */
  1391. loadCurPosData: function () {
  1392. const node = SpreadJsObj.getSelectObject(billsSheet);
  1393. // SpreadJsObj.initSpreadSettingEvents(posSpreadSetting, posCol);
  1394. // SpreadJsObj.initSheet(posSheet, posSpreadSetting);
  1395. if (node) {
  1396. const posData = pos.getLedgerPos(node.id) || [];
  1397. SpreadJsObj.loadSheetData(posSheet, 'data', posData);
  1398. posSheet.zh_setting.readOnly = readOnly || (node.used && posData.length === 0);
  1399. } else {
  1400. SpreadJsObj.loadSheetData(posSheet, 'data', []);
  1401. posSheet.zh_setting.readOnly = true;
  1402. }
  1403. SpreadJsObj.massOperationSheet(posSheet, function () {
  1404. if (posSheet.zh_setting) {
  1405. posSheet.zh_setting.cols.forEach(function (col, i) {
  1406. for (let iRow = 0; iRow < posSheet.getRowCount(); iRow++) {
  1407. posSheet.getCell(iRow, i).locked((posSheet.zh_data[iRow] && !posSheet.zh_data[iRow].ccid) || posSheet.zh_setting.readOnly || false);
  1408. }
  1409. });
  1410. }
  1411. });
  1412. // SpreadJsObj.refreshSheetReadOnly(posSheet);
  1413. posSpreadObj.loadExprToInput();
  1414. posSpreadObj.refreshOperationValid(posSheet);
  1415. },
  1416. baseOpr: function (sheet, type) {
  1417. const data = {
  1418. postType: 'pos',
  1419. posPostType: type === 'delete' ? type : 'update',
  1420. postData: [],
  1421. };
  1422. const selection = sheet.getSelections();
  1423. const row = selection[0].row, count = selection[0].rowCount;
  1424. const first = sheet.zh_data[row];
  1425. if (type === 'delete') {
  1426. for (let iRow = 0; iRow < count; iRow++) {
  1427. const posData = sheet.zh_data[iRow + row];
  1428. if (posData) {
  1429. if (posData.used) {
  1430. toastr.error('"' + posData.name + '"已计量,请勿删除');
  1431. return;
  1432. }
  1433. data.postData.push(sheet.zh_data[iRow + row].id);
  1434. }
  1435. }
  1436. } else if (type === 'up-move') {
  1437. const pre = sheet.zh_data[row - 1], preUpdate = {id: pre.id};
  1438. for (let iRow = 0; iRow < count; iRow++) {
  1439. const posData = sheet.zh_data[iRow + row];
  1440. if (posData) {
  1441. data.postData.push({id: posData.id, lid: posData.lid, porder: sheet.zh_data[iRow + row - 1].porder});
  1442. preUpdate.porder = posData.porder;
  1443. }
  1444. }
  1445. data.postData.push(preUpdate);
  1446. } else if (type === 'down-move') {
  1447. const next = sheet.zh_data[row + count], nextUpdate = {id: next.id};
  1448. for (let iRow = count - 1; iRow >= 0; iRow--) {
  1449. const posData = sheet.zh_data[iRow + row];
  1450. if (posData) {
  1451. data.postData.push({id: posData.id, lid: posData.lid, porder: sheet.zh_data[iRow + row + 1].porder});
  1452. nextUpdate.porder = posData.porder;
  1453. }
  1454. }
  1455. data.postData.push(nextUpdate);
  1456. }
  1457. if (data.postData.length > 0) {
  1458. postData(window.location.pathname + '/update', data, function (result) {
  1459. if (type === 'delete') {
  1460. pos.removeDatas(result.pos);
  1461. sheet.deleteRows(row, count);
  1462. const loadResult = billsTree.loadPostData(result.ledger);
  1463. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1464. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1465. posSpreadObj.refreshOperationValid(posSheet);
  1466. } else {
  1467. pos.updateDatas(result.pos);
  1468. const sel = selection[0];
  1469. if (sel) {
  1470. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  1471. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, sheet.zh_data.indexOf(first)]);
  1472. }
  1473. posSpreadObj.loadCurPosData();
  1474. }
  1475. });
  1476. }
  1477. },
  1478. editStarting: function (e, info) {
  1479. posSpreadObj.billsNode = SpreadJsObj.getSelectObject(billsSheet);
  1480. const sel = info.sheet.getSelections();
  1481. if (!sel || !sel[0]) return;
  1482. const col = info.sheet.zh_setting.cols[sel[0].col];
  1483. const node = SpreadJsObj.getSelectObject(info.sheet);
  1484. const exprInfo = getExprInfo(col.field);
  1485. if (node && exprInfo) {
  1486. if (node[exprInfo.expr] && node[exprInfo.expr] !== '') {
  1487. info.sheet.getCell(info.row, info.col).text(node[exprInfo.expr]);
  1488. }
  1489. }
  1490. },
  1491. /**
  1492. * 编辑单元格响应事件
  1493. * @param {Object} e
  1494. * @param {Object} info
  1495. */
  1496. editEnded: function (e, info) {
  1497. if (!info.sheet.zh_setting) {
  1498. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1499. return;
  1500. }
  1501. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  1502. const col = info.sheet.zh_setting.cols[info.col];
  1503. const orgText = posData ? posData[col.field] : null;
  1504. const newText = trimInvalidChar(info.editingText);
  1505. if (orgText === newText || ((!orgText || orgText === '') && (newText === ''))) return;
  1506. const node = posSpreadObj.billsNode;
  1507. if (!node) {
  1508. toastr.error('数据错误,请选择台账节点后再试');
  1509. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1510. return;
  1511. } else if (newText && newText !== '' && node.children && node.children.length > 0) {
  1512. toastr.error('父节点不可插入计量单元');
  1513. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1514. return;
  1515. } else if (newText && newText !== '' && (!node.b_code || node.b_code === '')) {
  1516. toastr.error('项目节不可插入计量单元');
  1517. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1518. return;
  1519. }
  1520. const data = {postType: 'pos'};
  1521. if (col.field === 'name') {
  1522. if (newText === '' && posData) {
  1523. toastr.error('部位名称不可为空', 'error');
  1524. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1525. return;
  1526. } else if (!posData) {
  1527. if (newText && newText !== '') {
  1528. data.posPostType = 'add';
  1529. const sortData = info.sheet.zh_data;
  1530. const order = (!sortData || sortData.length === 0) ? 1 : Math.max(sortData[sortData.length - 1].porder + 1, sortData.length + 1);
  1531. data.postData = { name: newText, lid: node.id, porder: order};
  1532. } else {
  1533. return;
  1534. }
  1535. } else {
  1536. data.posPostType = 'update';
  1537. data.postData = {id: posData.id, name: newText};
  1538. }
  1539. } else if (!posData) {
  1540. toastr.warning('新增计量单元请先输入名称');
  1541. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1542. return;
  1543. } else {
  1544. data.posPostType = 'update';
  1545. data.postData = {id: posData.id};
  1546. if (col.type === 'Number') {
  1547. const exprInfo = getExprInfo(col.field);
  1548. const num = _.toNumber(newText);
  1549. if (_.isFinite(num)) {
  1550. data.postData[col.field] = num;
  1551. if (exprInfo) {
  1552. data.postData[exprInfo.expr] = '';
  1553. }
  1554. } else {
  1555. try {
  1556. data.postData[col.field] = math.evaluate(transExpr(newText));
  1557. if (exprInfo) {
  1558. data.postData[exprInfo.expr] = newText;
  1559. }
  1560. } catch(err) {
  1561. toastr.error('输入的表达式非法');
  1562. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1563. return;
  1564. }
  1565. }
  1566. } else {
  1567. data.postData[col.field] = newText;
  1568. }
  1569. }
  1570. postData(window.location.pathname + '/update', data, function (result) {
  1571. const updateRst = pos.updateDatas(result.pos);
  1572. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1573. if (updateRst.create.length > 0) {
  1574. posSpreadObj.loadCurPosData();
  1575. } else {
  1576. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1577. }
  1578. const loadResult = billsTree.loadPostData(result.ledger);
  1579. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1580. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1581. }, function () {
  1582. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1583. });
  1584. },
  1585. /**
  1586. * 删除按钮响应事件
  1587. * @param sheet
  1588. */
  1589. deletePress: function (sheet) {
  1590. if (!sheet.zh_setting) return;
  1591. const sortData = sheet.zh_data;
  1592. const datas = [], posSelects = [];
  1593. const sel = sheet.getSelections()[0];
  1594. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  1595. let bDel = false;
  1596. const node = sortData[iRow];
  1597. if (node) {
  1598. const data = {id: node.id};
  1599. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  1600. const colSetting = sheet.zh_setting.cols[iCol];
  1601. if (colSetting.field === 'name') {
  1602. toastr.error('部位名称不能为空');
  1603. return;
  1604. }
  1605. const style = sheet.getStyle(iRow, iCol);
  1606. if (!style.locked) {
  1607. const colSetting = sheet.zh_setting.cols[iCol];
  1608. data[colSetting.field] = null;
  1609. const exprInfo = getExprInfo(colSetting.field);
  1610. if (exprInfo) {
  1611. data[exprInfo.expr] = '';
  1612. }
  1613. bDel = true;
  1614. }
  1615. }
  1616. if (bDel) {
  1617. datas.push(data);
  1618. posSelects.push(node);
  1619. }
  1620. }
  1621. }
  1622. if (datas.length > 0) {
  1623. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: datas}, function (result) {
  1624. pos.updateDatas(result.pos);
  1625. posSpreadObj.loadCurPosData();
  1626. const loadResult = billsTree.loadPostData(result.ledger);
  1627. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1628. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1629. }, function () {
  1630. posSpreadObj.loadCurPosData();
  1631. });
  1632. }
  1633. },
  1634. /**
  1635. * 删除 计量单元
  1636. * @param sheet
  1637. */
  1638. deletePos: function (sheet) {
  1639. const selection = sheet.getSelections();
  1640. const data = {
  1641. postType: 'pos',
  1642. posPostType: 'delete',
  1643. postData: [],
  1644. };
  1645. const row = selection[0].row, count = selection[0].rowCount;
  1646. const sortData = sheet.zh_data;
  1647. for (let iRow = 0; iRow < count; iRow++) {
  1648. const posData = sortData[iRow + row];
  1649. if (posData) {
  1650. if (posData.used) {
  1651. toastr.error('"' + posData.name + '"已计量,请勿删除');
  1652. return;
  1653. }
  1654. data.postData.push(sortData[iRow + row].id);
  1655. }
  1656. }
  1657. if (data.postData.length > 0) {
  1658. postData(window.location.pathname + '/update', data, function (result) {
  1659. pos.removeDatas(result.pos);
  1660. sheet.deleteRows(row, count);
  1661. const loadResult = billsTree.loadPostData(result.ledger);
  1662. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1663. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1664. });
  1665. }
  1666. },
  1667. /**
  1668. * 粘贴单元格响应事件
  1669. * @param e
  1670. * @param info
  1671. */
  1672. clipboardPasted: function (e, info) {
  1673. if (info.sheet.getColumnCount() > info.sheet.zh_setting.cols.length) {
  1674. info.sheet.setColumnCount(info.sheet.zh_setting.cols.length);
  1675. }
  1676. const node = SpreadJsObj.getSelectObject(billsSheet);
  1677. if (node.code && (node.code !== '')) {
  1678. toastr.error('项目节不可含有节点明细');
  1679. posSpreadObj.loadCurPosData();
  1680. return;
  1681. }
  1682. if (node.children && (node.children.length > 0)) {
  1683. toastr.error('仅节点子项可以含有计量单元');
  1684. posSpreadObj.loadCurPosData();
  1685. return;
  1686. }
  1687. if (!info.sheet.zh_setting) {
  1688. posSpreadObj.loadCurPosData();
  1689. return;
  1690. }
  1691. const data = [];
  1692. const sortData = info.sheet.zh_data || [];
  1693. if (sortData.length === 0 || info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1694. if (info.cellRange.col !== 0) {
  1695. toastr.warning('新增计量单元请先输入名称');
  1696. posSpreadObj.loadCurPosData();
  1697. return;
  1698. }
  1699. }
  1700. let bHint = false;
  1701. const lastOrder = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1702. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  1703. let bPaste = true;
  1704. const curRow = info.cellRange.row + iRow;
  1705. const posData = curRow >= sortData.length ? {lid: node.id, porder: lastOrder + curRow - sortData.length} : {id: sortData[curRow].id, lid: node.id};
  1706. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  1707. const curCol = info.cellRange.col + iCol;
  1708. const colSetting = info.sheet.zh_setting.cols[curCol];
  1709. if (!colSetting) continue;
  1710. posData[colSetting.field] = trimInvalidChar(info.sheet.getText(curRow, curCol));
  1711. if (colSetting.type === 'Number') {
  1712. const num = _.toNumber(posData[colSetting.field]);
  1713. if (num) {
  1714. posData[colSetting.field] = num;
  1715. } else {
  1716. try {
  1717. posData[colSetting.field] = math.evaluate(transExpr(posData[colSetting.field]));
  1718. const exprInfo = getExprInfo(colSetting.field);
  1719. if (exprInfo) {
  1720. posData[exprInfo.expr] = trimInvalidChar(info.sheet.getText(curRow, curCol));
  1721. }
  1722. } catch (err) {
  1723. if (!bHint) {
  1724. toastr.warning('粘贴了非法表达式,已过滤');
  1725. bHint = true;
  1726. }
  1727. bPaste = false;
  1728. }
  1729. }
  1730. }
  1731. }
  1732. if (bPaste) {
  1733. data.push(posData);
  1734. }
  1735. }
  1736. if (data.length === 0) {
  1737. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1738. return;
  1739. }
  1740. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: data}, function (result) {
  1741. pos.updateDatas(result.pos);
  1742. posSpreadObj.loadCurPosData();
  1743. const loadResult = billsTree.loadPostData(result.ledger);
  1744. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1745. posSpreadObj.loadCurPosData();
  1746. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1747. }, function () {
  1748. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1749. });
  1750. },
  1751. selectionChanged: function (e, info) {
  1752. posSpreadObj.loadExprToInput();
  1753. posSpreadObj.refreshOperationValid(posSheet);
  1754. },
  1755. addPegs: function (pegs) {
  1756. if (!pegs || pegs.length <= 0) return;
  1757. const node = SpreadJsObj.getSelectObject(billsSheet);
  1758. if (!node) return;
  1759. const sheet = posSpread.getActiveSheet();
  1760. const sortData = sheet.zh_data || [];
  1761. let order = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1762. pegs.forEach(function (p) {p.porder = ++order; p.lid = node.id});
  1763. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: pegs}, function (result) {
  1764. pos.updateDatas(result.pos);
  1765. posSpreadObj.loadCurPosData();
  1766. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1767. });
  1768. }
  1769. };
  1770. posSpread.bind(spreadNS.Events.SelectionChanged, posSpreadObj.selectionChanged);
  1771. if (!readOnly && isTz) {
  1772. $('a[name="pos-opr"]').click(function () {
  1773. posSpreadObj.baseOpr(posSheet, this.getAttribute('type'));
  1774. });
  1775. $('#pos-expr').bind('change onblur', function () {
  1776. if (this.readOnly) return;
  1777. const expr = $(this);
  1778. const row = expr.attr('data-row') ? _.toInteger(expr.attr('data-row')) : -1;
  1779. const select = posSheet.zh_data ? posSheet.zh_data[row] : null;
  1780. if (!select) return;
  1781. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  1782. if (orgValue === newValue || (!orgValue && newValue == '')) return;
  1783. const data = {id: select.id};
  1784. const exprInfo = getExprInfo(field);
  1785. if (newValue !== '') {
  1786. const num = _.toNumber(newValue);
  1787. if (num) {
  1788. data[field] = num;
  1789. if (exprInfo) data[exprInfo.expr] = '';
  1790. } else {
  1791. try {
  1792. data[field] = math.evaluate(transExpr(newValue));
  1793. if (exprInfo) data[exprInfo.expr] = newValue;
  1794. } catch (err) {
  1795. toastr.error('输入的表达式非法');
  1796. return;
  1797. }
  1798. }
  1799. } else {
  1800. data[field] = null;
  1801. if (exprInfo) data[exprInfo.expr] = '';
  1802. }
  1803. // 更新至服务器
  1804. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: data}, function (result) {
  1805. const updateRst = pos.updateDatas(result.pos);
  1806. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1807. SpreadJsObj.reLoadRowData(posSheet, row);
  1808. const loadResult = billsTree.loadPostData(result.ledger);
  1809. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1810. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1811. });
  1812. });
  1813. posSpread.bind(spreadNS.Events.EditStarting, posSpreadObj.editStarting);
  1814. posSpread.bind(spreadNS.Events.EditEnded, posSpreadObj.editEnded);
  1815. posSpread.bind(spreadNS.Events.ClipboardPasted, posSpreadObj.clipboardPasted);
  1816. SpreadJsObj.addDeleteBind(posSpread, posSpreadObj.deletePress);
  1817. $.contextMenu({
  1818. selector: '#pos-spread',
  1819. build: function ($trigger, e) {
  1820. const target = SpreadJsObj.safeRightClickSelection($trigger, e, posSpread);
  1821. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1822. },
  1823. items: {
  1824. 'delete': {
  1825. name: '删除',
  1826. icon: 'fa-remove',
  1827. disabled: function (key, opt) {
  1828. if (posSheet.zh_data) {
  1829. const selection = posSheet.getSelections();
  1830. return (posSheet.zh_data.length < selection[0].row + selection[0].rowCount) || (posSheet.zh_data[selection[0].row] && !posSheet.zh_data[selection[0].row].ccid);
  1831. } else {
  1832. return true;
  1833. }
  1834. },
  1835. callback: function (key, opt) {
  1836. posSpreadObj.deletePos(posSheet);
  1837. }
  1838. },
  1839. }
  1840. });
  1841. }
  1842. // 加载清单&计量单元数据
  1843. const preUrl = window.location.pathname.split('/').slice(0, 4).join('/');
  1844. postData(preUrl + '/defaultBills', { from: 'revise' }, function (result) {
  1845. billsTree.loadDatas(result.bills);
  1846. pos.loadDatas(result.pos);
  1847. treeCalc.calculateAll(billsTree);
  1848. SpreadJsObj.loadSheetData(billsSheet, SpreadJsObj.DataType.Tree, billsTree);
  1849. SpreadJsObj.loadTopAndSelect(billsSheet, ckBillsSpread);
  1850. checkShowLast(result.bills.length);
  1851. posSpreadObj.loadCurPosData();
  1852. SpreadJsObj.resetTopAndSelect(posSheet);
  1853. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1854. billsTreeSpreadObj.loadExprToInput(billsSheet);
  1855. checkList.loadHisCheckData();
  1856. }, null);
  1857. $.divResizer({
  1858. select: '#revise-resize',
  1859. callback: function () {
  1860. billsSpread.refresh();
  1861. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1862. $(".sp-wrap").height(bcontent-30);
  1863. posSpread.refresh();
  1864. }
  1865. });
  1866. class DealBills {
  1867. constructor (selector, spreadSetting) {
  1868. const self = this;
  1869. this.loaded = false;
  1870. this.obj = $(selector)[0];
  1871. this.url = '/tender/' + window.location.pathname.split('/')[2] + '/deal';
  1872. this.spreadSetting = spreadSetting;
  1873. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1874. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1875. if (!readOnly) {
  1876. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1877. const dealSheet = info.sheet;
  1878. const mainSheet = billsSheet;
  1879. const dealBills = SpreadJsObj.getSelectObject(dealSheet);
  1880. if (!dealBills) { return; }
  1881. const mainTree = mainSheet.zh_tree;
  1882. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1883. if (!mainNode || !mainTree) { return; }
  1884. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1885. toastr.warning('非最底层项目下,不应添加节点');
  1886. return;
  1887. }
  1888. postData(window.location.pathname + '/update', {
  1889. postType: 'add-deal',
  1890. postData: {
  1891. id: mainNode.ledger_id,
  1892. type: mainNode.code ? 'child' : 'next',
  1893. dealBills: {
  1894. b_code: dealBills.code, name: dealBills.name, unit: dealBills.unit,
  1895. unit_price: dealBills.unit_price,
  1896. }
  1897. },
  1898. }, function (result) {
  1899. const refreshData = mainTree.loadPostData(result);
  1900. billsTreeSpreadObj.refreshTree(mainSheet, refreshData);
  1901. const sel = mainSheet.getSelections()[0];
  1902. if (sel && refreshData.create[0]) {
  1903. mainSheet.setSelection(mainTree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  1904. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, mainTree.nodes.indexOf(refreshData.create[0])]);
  1905. }
  1906. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1907. billsSpread.focus();
  1908. posSpreadObj.loadCurPosData();
  1909. });
  1910. });
  1911. }
  1912. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1913. }
  1914. loadData (callback) {
  1915. if (this.loaded) {
  1916. if (callback) callback();
  1917. return;
  1918. }
  1919. const self = this;
  1920. postData(this.url+'/get-data', {}, function (data) {
  1921. self.data = data;
  1922. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1923. self.loaded = true;
  1924. if (callback) callback();
  1925. });
  1926. }
  1927. calculateData () {
  1928. if (this.data) {
  1929. for (const d of this.data) {
  1930. d.total_price = _.multiply(d.quantity, d.unit_price);
  1931. }
  1932. }
  1933. }
  1934. }
  1935. class BatchInsertBillsPosObj {
  1936. constructor (obj) {
  1937. const self = this;
  1938. this.obj = obj;
  1939. this.billsCount = 6;
  1940. this.posCount = 1000;
  1941. this.filter = getLocalCache('zh-calc-batch-filter');
  1942. if (!this.filter) {
  1943. this.filter = '1';
  1944. }
  1945. $('input[name=batch-filter]')[0].checked = this.filter && this.filter != '0';
  1946. // 初始化 清单编号窗口 参数
  1947. this.qdSpreadSetting = {
  1948. cols: [
  1949. {title: '编号', field: 'code', hAlign: 0, width: 80, formatter: '@'},
  1950. {title: '名称', field: 'name', hAlign: 0, width: 120, formatter: '@'},
  1951. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'},
  1952. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  1953. {title: '图册号', field: 'name', hAlign: 0, width: 60, formatter: '@'},
  1954. ],
  1955. emptyRows: this.billsCount,
  1956. headRows: 1,
  1957. headRowHeight: [32],
  1958. headerFont: '12px 微软雅黑',
  1959. font: '12px 微软雅黑',
  1960. };
  1961. this.qdSpread = SpreadJsObj.createNewSpread($('.batch-l-t', this.obj)[0]);
  1962. // 初始化 部位数量复核表 参数
  1963. this.posSpreadSetting = {
  1964. cols: [
  1965. {title: '名称', field: 'bw', hAlign: 0, width: 80, formatter: '@'},
  1966. {title: '图册号', field: 'drawingCode', hAlign: 0, formatter: '@', width: 60},
  1967. ],
  1968. emptyRows: this.posCount,
  1969. headRows: 1,
  1970. headRowHeight: [32],
  1971. headerFont: '12px 微软雅黑',
  1972. font: '12px 微软雅黑',
  1973. };
  1974. for (let iNum = 1; iNum <= this.billsCount; iNum++) {
  1975. this.posSpreadSetting.cols.push(
  1976. {title: '清单' + iNum, field: 'bills' + iNum, hAlign: 2, width: 50}
  1977. )
  1978. }
  1979. this.posSpread = SpreadJsObj.createNewSpread($('.batch-l-b', this.obj)[0]);
  1980. // 初始化 签约节点 参数
  1981. this.dealSpreadSetting = {
  1982. cols: [
  1983. {title: '清单编号', field: 'code', width: 80, hAlign: 0, formatter: '@', readOnly: true},
  1984. {title: '名称', field: 'name', width: 230, hAlign: 0, formatter: '@', readOnly: true},
  1985. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1986. {title: '单价', field: 'unit_price', hAlign: 2, width: 60, readOnly: true},
  1987. ],
  1988. emptyRows: 0,
  1989. headRows: 1,
  1990. headRowHeight: [32],
  1991. headerFont: '12px 微软雅黑',
  1992. font: '12px 微软雅黑',
  1993. };
  1994. this.dealSpread = SpreadJsObj.createNewSpread($('.batch-r', this.obj)[0]);
  1995. // 初始化 清单编号、部位数量复核表 表格
  1996. this.initView();
  1997. SpreadJsObj.initSheet(this.dealSpread.getActiveSheet(), this.dealSpreadSetting);
  1998. SpreadJsObj.refreshColumnAlign(this.dealSpread.getActiveSheet());
  1999. // 拉取签约节点数据
  2000. dealBills.loadData(() => {
  2001. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  2002. });
  2003. // 双击签约节点,自动添加到清单编号窗口
  2004. this.dealSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  2005. const deal = info.sheet.zh_data[info.row];
  2006. const qdSheet = self.qdSpread.getActiveSheet(), posSheet = self.posSpread.getActiveSheet();
  2007. const sel = qdSheet.getSelections()[0];
  2008. qdSheet.getCell(sel.row, 0).value(deal.code);
  2009. qdSheet.getCell(sel.row, 1).value(deal.name);
  2010. qdSheet.getCell(sel.row, 2).value(deal.unit);
  2011. qdSheet.getCell(sel.row, 3).value(deal.unit_price);
  2012. if (sel.row + 1 === qdSheet.getRowCount()) {
  2013. const count = sel.row + 2;
  2014. qdSheet.setRowCount(count);
  2015. qdSheet.getCell(sel.row + 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + count);
  2016. const colCount = posSheet.getColumnCount() + 1;
  2017. posSheet.setColumnCount(colCount);
  2018. posSheet.getCell(0, colCount - 1, GC.Spread.Sheets.SheetArea.colHeader).text('清单' + count);
  2019. }
  2020. qdSheet.setSelection(sel.row + 1, sel.col, 1, 1);
  2021. qdSheet.getParent().focus();
  2022. });
  2023. this.qdSpread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  2024. const billsCount = info.sheet.getRowCount(), posSheet = self.posSpread.getActiveSheet();
  2025. const count = posSheet.getColumnCount() - 2;
  2026. if (billsCount > count) {
  2027. posSheet.setColumnCount(billsCount + 2);
  2028. for (let i = count + 1; i <= billsCount; i++) {
  2029. info.sheet.getCell(i - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + i);
  2030. posSheet.getCell(0, i + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + i);
  2031. }
  2032. }
  2033. if (info.cellRange.col === 0 && info.cellRange.colCount === 1) {
  2034. const dealBills = self.dealSpread.getActiveSheet().zh_data;
  2035. if (dealBills && dealBills.length > 0) {
  2036. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  2037. const curRow = iRow + info.cellRange.row;
  2038. const bills = _.find(dealBills, {code: info.sheet.getText(curRow, 0)});
  2039. if (bills) {
  2040. info.sheet.getCell(curRow, 1).value(bills.name);
  2041. info.sheet.getCell(curRow, 2).value(bills.unit);
  2042. info.sheet.getCell(curRow, 3).value(bills.unit_price);
  2043. }
  2044. }
  2045. }
  2046. }
  2047. });
  2048. this.posSpread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  2049. const billsCount = info.sheet.getColumnCount() - 2, qdSheet = self.qdSpread.getActiveSheet();
  2050. const count = qdSheet.getRowCount();
  2051. if (billsCount > count) {
  2052. qdSheet.setRowCount(billsCount);
  2053. for (let i = count + 1; i <= billsCount; i++) {
  2054. qdSheet.getCell(i - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + i);
  2055. info.sheet.getCell(0, i + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + i);
  2056. }
  2057. }
  2058. });
  2059. this.qdSpread.bind(spreadNS.Events.EditEnded, function (e, info) {
  2060. if (info.col === 0) {
  2061. const dealBills = self.dealSpread.getActiveSheet().zh_data;
  2062. if (dealBills && dealBills.length > 0) {
  2063. const bills = _.find(dealBills, {code: info.editingText});
  2064. if (bills) {
  2065. info.sheet.getCell(info.row, 1).value(bills.name);
  2066. info.sheet.getCell(info.row, 2).value(bills.unit);
  2067. info.sheet.getCell(info.row, 3).value(bills.unit_price);
  2068. }
  2069. }
  2070. }
  2071. });
  2072. this.obj.bind('shown.bs.modal', function () {
  2073. self.qdSpread.refresh();
  2074. self.posSpread.refresh();
  2075. self.dealSpread.refresh();
  2076. });
  2077. $('#batch-ok').click(function () {
  2078. const selection = billsSheet.getSelections();
  2079. const sel = selection[0];
  2080. const row = selection[0].row;
  2081. const select = billsTree.nodes[row];
  2082. if (select) {
  2083. const insertData = {};
  2084. insertData.batchType = (select.code && select.code !== '') ? 'child' : 'next';
  2085. insertData.id = select[billsTree.setting.id];
  2086. insertData.batchData = self.getBatchData();
  2087. if (insertData.batchData.length > 0) {
  2088. postData(window.location.pathname + '/update', {
  2089. postType: 'batch-insert',
  2090. postData: insertData
  2091. }, function (data) {
  2092. pos.updateDatas(data.pos);
  2093. const result = billsTree.loadPostData(data.ledger);
  2094. billsTreeSpreadObj.refreshTree(billsSheet, result);
  2095. billsSheet.setSelection(result.create[0].index, sel.col, sel.rowCount, sel.colCount);
  2096. SpreadJsObj.reloadRowsBackColor(billsSheet, [sel.row, result.create[0].index]);
  2097. billsTreeSpreadObj.refreshOperationValid(billsSheet, selection);
  2098. posSpreadObj.loadCurPosData();
  2099. self.obj.modal('hide');
  2100. }, null, true);
  2101. } else {
  2102. self.obj.modal('hide');
  2103. }
  2104. }
  2105. });
  2106. $('input[name=batch-filter]').change(function () {
  2107. setLocalCache('zh-calc-batch-filter', this.checked ? 1 : 0);
  2108. });
  2109. $.contextMenu({
  2110. selector: '.batch-l-t',
  2111. build: function ($trigger, e) {
  2112. const target = SpreadJsObj.safeRightClickSelection($trigger, e, self.qdSpread);
  2113. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  2114. },
  2115. items: {
  2116. 'create': {
  2117. name: '新增行',
  2118. icon: 'fa-sign-in',
  2119. callback: function (key, opt) {
  2120. const qdSheet = self.qdSpread.getActiveSheet();
  2121. const posSheet = self.posSpread.getActiveSheet();
  2122. qdSheet.addRows(qdSheet.getRowCount(), 1);
  2123. const index = qdSheet.getRowCount();
  2124. qdSheet.getCell(qdSheet.getRowCount() - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + index);
  2125. posSheet.addColumns(posSheet.getColumnCount(), 1);
  2126. posSheet.getCell(0, index + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + index);
  2127. },
  2128. },
  2129. 'delete': {
  2130. name: '删除行',
  2131. icon: 'fa-remove',
  2132. callback: function (key, opt) {
  2133. const qdSheet = self.qdSpread.getActiveSheet();
  2134. const posSheet = self.posSpread.getActiveSheet();
  2135. const sel = qdSheet.getSelections()[0];
  2136. qdSheet.deleteRows(sel.row, sel.rowCount);
  2137. posSheet.deleteColumns(sel.row + 2, sel.rowCount);
  2138. for (let iRow = 0, iLen = qdSheet.getRowCount(); iRow < iLen; iRow++) {
  2139. qdSheet.getCell(iRow, 0, spreadNS.SheetArea.rowHeader).text('清单' + (iRow+1));
  2140. }
  2141. for (let iCol = 0, iLen = posSheet.getColumnCount() - 2; iCol < iLen; iCol++) {
  2142. posSheet.getCell(0, iCol + 2, spreadNS.SheetArea.colHeader).text('清单' + (iCol+1));
  2143. }
  2144. qdSheet.setSelection(sel.row, sel.col, 1, 1);
  2145. },
  2146. },
  2147. }
  2148. });
  2149. }
  2150. // 初始化左侧表格
  2151. initView () {
  2152. // 初始化 清单编号
  2153. const qdSheet = this.qdSpread.getActiveSheet();
  2154. SpreadJsObj.initSheet(qdSheet, this.qdSpreadSetting);
  2155. SpreadJsObj.refreshColumnAlign(qdSheet);
  2156. // 清理原有数据
  2157. SpreadJsObj.beginMassOperation(qdSheet);
  2158. qdSheet.clear(0, 0, qdSheet.getRowCount(), qdSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  2159. for (let iRow = 1; iRow <= this.billsCount; iRow++) {
  2160. qdSheet.getCell(iRow - 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + iRow);
  2161. }
  2162. qdSheet.setSelection(0, 0, 1 ,1);
  2163. SpreadJsObj.endMassOperation(qdSheet);
  2164. // 初始化 部位数量复核表
  2165. const posSheet = this.posSpread.getActiveSheet();
  2166. SpreadJsObj.initSheet(posSheet, this.posSpreadSetting);
  2167. SpreadJsObj.refreshColumnAlign(posSheet);
  2168. // 清理原有数据
  2169. SpreadJsObj.beginMassOperation(posSheet);
  2170. posSheet.setColumnWidth(0, 45, GC.Spread.Sheets.SheetArea.rowHeader);
  2171. posSheet.clear(0, 0, posSheet.getRowCount(), posSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  2172. posSheet.setSelection(0, 0, 1 ,1);
  2173. SpreadJsObj.endMassOperation(posSheet);
  2174. // 检查签约节点数据,以工具栏数据为准
  2175. if (dealBills) {
  2176. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  2177. }
  2178. this.dealSpread.getActiveSheet().setSelection(0, 0, 1, 1);
  2179. }
  2180. // 获取界面数据
  2181. getBatchData () {
  2182. const result = [];
  2183. const qdSheet = this.qdSpread.getActiveSheet(), posSheet = this.posSpread.getActiveSheet();
  2184. for (let iRow = 0; iRow < qdSheet.getRowCount(); iRow++) {
  2185. if (qdSheet.getText(iRow, 0) === '') { continue; }
  2186. const qd = {
  2187. b_code: qdSheet.getText(iRow, 0),
  2188. name: qdSheet.getText(iRow, 1),
  2189. unit: qdSheet.getText(iRow, 2),
  2190. price: _.toNumber(qdSheet.getText(iRow, 3)),
  2191. pos: [],
  2192. };
  2193. for (let iPosRow = 0; iPosRow < posSheet.getRowCount(); iPosRow++) {
  2194. const value = _.toNumber(posSheet.getText(iPosRow, iRow + 2));
  2195. if (value !== 0 && !isNaN(value)) {
  2196. qd.pos.push({
  2197. name: posSheet.getText(iPosRow, 0),
  2198. drawing_code: posSheet.getText(iPosRow, 1),
  2199. quantity: value, porder: qd.pos.length + 1,
  2200. });
  2201. }
  2202. }
  2203. if (!$('input[name=batch-filter]')[0].checked || qd.pos.length > 0) result.push(qd);
  2204. }
  2205. return result;
  2206. }
  2207. }
  2208. const dealBills = new DealBills('#deal-bills-spread', {
  2209. cols: [
  2210. {title: '清单编号', field: 'code', hAlign: 0, width: 85, formatter: '@', readOnly: true},
  2211. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  2212. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  2213. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  2214. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  2215. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  2216. ],
  2217. emptyRows: 0,
  2218. headRows: 1,
  2219. headRowHeight: [32],
  2220. headColWidth: [30],
  2221. defaultRowHeight: 21,
  2222. headerFont: '12px 微软雅黑',
  2223. font: '12px 微软雅黑',
  2224. selectedBackColor: '#fffacd',
  2225. });
  2226. $.divResizer({
  2227. select: '#revise-right-spr',
  2228. callback: function () {
  2229. billsSpread.refresh();
  2230. if (posSpread) posSpread.refresh();
  2231. if (stdXmj) stdXmj.spread.refresh();
  2232. if (stdGcl) stdGcl.spread.refresh();
  2233. if (dealBills) dealBills.spread.refresh();
  2234. if (searchLedger) searchLedger.spread.refresh();
  2235. if (errorList) errorList.spread.refresh();
  2236. if (checkList) checkList.spread.refresh();
  2237. if (sumLoadMiss) sumLoadMiss.spread.refresh();
  2238. }
  2239. });
  2240. $.subMenu({
  2241. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  2242. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  2243. key: 'menu.1.0.0',
  2244. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  2245. callback: function (info) {
  2246. if (info.mini) {
  2247. $('.panel-title').addClass('fluid');
  2248. $('#sub-menu').removeClass('panel-sidebar');
  2249. } else {
  2250. $('.panel-title').removeClass('fluid');
  2251. $('#sub-menu').addClass('panel-sidebar');
  2252. }
  2253. autoFlashHeight();
  2254. billsSpread.refresh();
  2255. if (posSpread) posSpread.refresh();
  2256. if (stdXmj) stdXmj.spread.refresh();
  2257. if (stdGcl) stdGcl.spread.refresh();
  2258. if (dealBills) dealBills.spread.refresh();
  2259. if (searchLedger) searchLedger.spread.refresh();
  2260. if (errorList) errorList.spread.refresh();
  2261. if (checkList) checkList.spread.refresh();
  2262. if (sumLoadMiss) sumLoadMiss.spread.refresh();
  2263. }
  2264. });
  2265. // showSideTools(true);
  2266. billsSpread.refresh();
  2267. if (posSpread) posSpread.refresh();
  2268. const stdLibCellDoubleClick = function (e, info) {
  2269. const stdSheet = info.sheet;
  2270. const mainSheet = billsSheet;
  2271. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) { return; }
  2272. const stdTree = stdSheet.zh_tree;
  2273. const stdNode = stdTree.nodes[info.row];
  2274. const mainTree = mainSheet.zh_tree;
  2275. const sel = mainSheet.getSelections()[0];
  2276. const mainNode = mainTree.nodes[sel.row];
  2277. if (!stdNode) return;
  2278. if (info.sheet.zh_setting.stdType === 'gcl') {
  2279. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  2280. toastr.warning('非最底层项目下,不应添加节点');
  2281. return;
  2282. }
  2283. }
  2284. postData(window.location.pathname + '/update', {
  2285. postType: 'add-std',
  2286. postData: {
  2287. id: mainTree.getNodeKey(mainNode),
  2288. tender_id: mainNode.tender_id,
  2289. stdType: info.sheet.zh_setting.stdType,
  2290. stdLibId: stdNode.list_id,
  2291. stdNode: stdTree.getNodeKey(stdNode)
  2292. }
  2293. }, function (result) {
  2294. const refreshNode = mainTree.loadPostData(result);
  2295. billsTreeSpreadObj.refreshTree(mainSheet, refreshNode);
  2296. if (sel) {
  2297. if (refreshNode.create && refreshNode.create.length > 0) {
  2298. mainSheet.setSelection(refreshNode.create[refreshNode.create.length - 1].index, sel.col, sel.rowCount, sel.colCount);
  2299. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, refreshNode.create[refreshNode.create.length - 1].index]);
  2300. } else {
  2301. const node = _.find(mainTree.nodes, {code: stdNode.code, name: stdNode.name});
  2302. if (node) {
  2303. mainSheet.setSelection(mainTree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  2304. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, mainTree.nodes.indexOf(node)]);
  2305. }
  2306. }
  2307. }
  2308. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  2309. billsSpread.focus();
  2310. posSpreadObj.loadCurPosData();
  2311. });
  2312. };
  2313. const stdXmjSetting = {
  2314. selector: '#std-xmj',
  2315. stdType: 'xmj',
  2316. libs: stdChapters,
  2317. treeSetting: {
  2318. id: 'chapter_id',
  2319. pid: 'pid',
  2320. order: 'order',
  2321. level: 'level',
  2322. rootId: -1,
  2323. keys: ['id', 'list_id', 'chapter_id'],
  2324. },
  2325. spreadSetting: {
  2326. cols: [
  2327. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  2328. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  2329. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  2330. ],
  2331. treeCol: 0,
  2332. emptyRows: 0,
  2333. headRows: 1,
  2334. headRowHeight: [32],
  2335. defaultRowHeight: 21,
  2336. headerFont: '12px 微软雅黑',
  2337. font: '12px 微软雅黑',
  2338. headColWidth: [30],
  2339. selectedBackColor: '#fffacd',
  2340. },
  2341. cellDoubleClick: stdLibCellDoubleClick,
  2342. page: 'revise',
  2343. tid: window.location.pathname.split('/')[2],
  2344. };
  2345. const stdGclSetting = {
  2346. selector: '#std-gcl',
  2347. stdType: 'gcl',
  2348. libs: stdBills,
  2349. treeSetting: {
  2350. id: 'bill_id',
  2351. pid: 'pid',
  2352. order: 'order',
  2353. level: 'level',
  2354. rootId: -1,
  2355. keys: ['id', 'list_id', 'bill_id']
  2356. },
  2357. spreadSetting: {
  2358. cols: [
  2359. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  2360. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  2361. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  2362. ],
  2363. treeCol: 0,
  2364. emptyRows: 0,
  2365. headRows: 1,
  2366. headRowHeight: [32],
  2367. defaultRowHeight: 21,
  2368. headerFont: '12px 微软雅黑',
  2369. font: '12px 微软雅黑',
  2370. headColWidth: [30],
  2371. selectedBackColor: '#fffacd',
  2372. },
  2373. cellDoubleClick: stdLibCellDoubleClick,
  2374. page: 'revise',
  2375. tid: window.location.pathname.split('/')[2],
  2376. };
  2377. // 展开收起标准节点
  2378. $('a', '#side-menu').bind('click', function (e) {
  2379. e.preventDefault();
  2380. const tab = $(this), tabPanel = $(tab.attr('content'));
  2381. // 展开工具栏、切换标签
  2382. if (!tab.hasClass('active')) {
  2383. const close = $('.active', '#side-menu').length === 0;
  2384. $('a', '#side-menu').removeClass('active');
  2385. tab.addClass('active');
  2386. $('.tab-content .tab-pane').removeClass('active');
  2387. tabPanel.addClass('active');
  2388. showSideTools(tab.hasClass('active'));
  2389. if (tab.attr('content') === '#std-xmj') {
  2390. if (!stdXmj) {
  2391. stdXmj = $.stdLib(stdXmjSetting);
  2392. }
  2393. stdXmj.spread.refresh();
  2394. } else if (tab.attr('content') === '#std-gcl') {
  2395. if (!stdGcl) {
  2396. stdGcl = $.stdLib(stdGclSetting);
  2397. }
  2398. stdGcl.spread.refresh();
  2399. } else if (tab.attr('content') === '#deal-bills') {
  2400. dealBills.loadData();
  2401. dealBills.spread.refresh();
  2402. } else if (tab.attr('content') === '#search') {
  2403. if (!searchLedger) {
  2404. searchLedger = $.billsSearch({
  2405. selector: '#search',
  2406. searchSpread: billsSpread,
  2407. searchOver: true,
  2408. searchEmpty: true,
  2409. resultSpreadSetting: {
  2410. cols: [
  2411. {title: '项目节编号', field: 'code', hAlign: 0, width: 90, formatter: '@', readOnly: true},
  2412. {title: '清单编号', field: 'b_code', hAlign: 0, width: 80, formatter: '@', readOnly: true},
  2413. {title: '名称', field: 'name', width: 150, hAlign: 0, formatter: '@', readOnly: true},
  2414. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@', readOnly: true},
  2415. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  2416. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  2417. ],
  2418. emptyRows: 0,
  2419. headRows: 1,
  2420. headRowHeight: [32],
  2421. headColWidth: [30],
  2422. defaultRowHeight: 21,
  2423. headerFont: '12px 微软雅黑',
  2424. font: '12px 微软雅黑',
  2425. selectedBackColor: '#fffacd',
  2426. },
  2427. afterLocated: function () {
  2428. posSpreadObj.loadCurPosData();
  2429. },
  2430. customSearch: [
  2431. {
  2432. key: 'revise', title: '新增部位', valid: true, parent: true,
  2433. check: function (node) {
  2434. if (node.ccid || node.cid) {
  2435. return true;
  2436. } else {
  2437. return false;
  2438. }
  2439. }
  2440. }
  2441. ],
  2442. });
  2443. }
  2444. searchLedger.spread.refresh();
  2445. } else if (tab.attr('content') === '#error-list') {
  2446. errorList.spread.refresh();
  2447. } else if (tab.attr('content') === '#check-list') {
  2448. checkList.spread.refresh();
  2449. } else if (tab.attr('content') === '#sum-load-miss') {
  2450. sumLoadMiss.spread.refresh();
  2451. }
  2452. }
  2453. else {// 收起工具栏
  2454. tab.removeClass('active');
  2455. tabPanel.removeClass('active');
  2456. showSideTools(tab.hasClass('active'));
  2457. }
  2458. billsSpread.refresh();
  2459. if (posSpread) posSpread.refresh();
  2460. });
  2461. // 显示层次
  2462. (function (select, sheet) {
  2463. $(select).click(function () {
  2464. if (!sheet.zh_tree) return;
  2465. const tag = $(this).attr('tag');
  2466. const tree = sheet.zh_tree;
  2467. setTimeout(() => {
  2468. showWaitingView();
  2469. switch (tag) {
  2470. case "1":
  2471. case "2":
  2472. case "3":
  2473. case "4":
  2474. case "5":
  2475. tree.expandByLevel(parseInt(tag));
  2476. SpreadJsObj.refreshTreeRowVisible(sheet);
  2477. break;
  2478. case "last":
  2479. tree.expandByCustom(() => { return true; });
  2480. SpreadJsObj.refreshTreeRowVisible(sheet);
  2481. break;
  2482. case "leafXmj":
  2483. tree.expandToLeafXmj();
  2484. SpreadJsObj.refreshTreeRowVisible(sheet);
  2485. break;
  2486. }
  2487. closeWaitingView();
  2488. }, 100);
  2489. });
  2490. })('a[name=showLevel]', billsSheet);
  2491. const dataChecker = DataChecker({
  2492. checkUrl: window.location.pathname + '/check',
  2493. completeData: function (data) {
  2494. pos.updateDatas({update: data.source.pos});
  2495. const loadResult = billsTree.loadPostData({update: data.source.bills});
  2496. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  2497. posSpreadObj.loadCurPosData();
  2498. for (const e of data.error) {
  2499. e.serialNo = billsTree.getNodeIndex(billsTree.getItems(e.ledger_id)) + 1;
  2500. }
  2501. },
  2502. errorList: errorList,
  2503. });
  2504. $('#ledger-check2').click(() => {
  2505. ledgerCheck2({
  2506. ledgerTree: billsTree,
  2507. ledgerPos: pos,
  2508. checkList: checkList,
  2509. decimal: decimal,
  2510. checkOption: checkOption,
  2511. }).then(result => {
  2512. check2Viewing({
  2513. extra: ZhCalc.div(billsTree.datas.length + pos.datas.length, 10000, 0),
  2514. randomWait: true,
  2515. prefix: 'check2-',
  2516. checks: result,
  2517. checkList: checkList,
  2518. });
  2519. });
  2520. });
  2521. });