block_controller.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /**
  2. * Created by zhang on 2018/5/10.
  3. */
  4. let BlockController = {
  5. datas:[],
  6. disabelCopyRightClick:function(){
  7. if (projectReadOnly) {
  8. return true;
  9. }
  10. let selection = projectObj.mainSpread.getActiveSheet().getSelections()[0];
  11. let firstNode = projectObj.project.mainTree.items[selection.row];//当多选的情况,用mainTree.selected判断不正确,要用第一个选中的节点
  12. for(let i = 0;i< selection.rowCount;i++){ //多选的时候判断所有与第一个节点同级的节点
  13. let temNode = projectObj.project.mainTree.items[selection.row + i];
  14. if(firstNode.getParentID() == temNode.getParentID()){
  15. if(BlockController.copyBtnDisable(temNode) == true){
  16. return true;
  17. }
  18. }
  19. }
  20. return false;
  21. },
  22. copyRightClickCallback:function(){
  23. $.bootstrapLoading.start();
  24. //let selected = project.mainTree.selected;
  25. let selections = projectObj.mainSpread.getActiveSheet().getSelections();
  26. setTimeout(function () {
  27. BlockController.copyBlock(selections[0]);
  28. $.bootstrapLoading.end();
  29. },100)
  30. },
  31. copyBtnDisable:function (selected) {
  32. if(this.isDXFYorMainEq(selected)){
  33. return true;
  34. }
  35. if(selected.sourceType == ModuleNames.ration && selected.data.type == rationType.install){
  36. return true;
  37. }
  38. if (OVER_HEIGHT.isOverHeight(selected)) {
  39. return true;
  40. }
  41. if(projectObj.project.Bills.hasFlags(selected)){//所有固定清单项不能复制
  42. return true;
  43. }
  44. //
  45. return false;
  46. },
  47. pasteBtnDisable:function (selected) {
  48. let blockData = this.getBlockData();
  49. let Bills = projectObj.project.Bills;
  50. if(this.isDXFYorMainEq(selected)){
  51. return true;
  52. }
  53. if(blockData == null){
  54. return true;
  55. }
  56. if(blockData.firstNodeType != blockType.RATION ){//复制的不是定额
  57. //&& blockData.isFBFX != Bills.isFBFX(selected)
  58. if (this.blockIsFB(blockData.firstNodeType) && !Bills.isFBFX(selected)) return true; //第一层是分部,不允许复制到非分部分项
  59. //分项\补项不允许粘贴到“施工组织措施项目”下
  60. if (this.blockIsFXorBX(blockData.firstNodeType) && Bills.isOrgMeasure(selected)) return true;
  61. if (blockData.firstNodeType == blockType.BILL && Bills.isFBFX(selected) ) { //如果是清单 到分部分项
  62. for (let d of blockData.datas) {
  63. //如果清单有多层,不允许复制
  64. if (!this.blockIsLeaveBills(d)) return true;
  65. ///如果有清单基数,不允许复制
  66. if (this.ifCalcBase(d.calcBase)) return true;
  67. //如果清单有费率引用,不允许复制
  68. if (gljUtil.isNotEmpty(d.feeRateID)) return true;
  69. }
  70. }
  71. }
  72. if(this.blockIsFXorBX(blockData.firstNodeType) && this.isFB(selected)){//复制块的第一层是分项或补项,焦点行是分部,且分部有子项并且子项不是分项或补项,则无效。
  73. if(selected.children.length && !this.isFXorBX(selected.children[0])){
  74. return true;
  75. }
  76. }
  77. if(this.isFXorBX(selected) && blockData.firstNodeType == blockType.FB){//焦点行是分项或补项,复制块的第一层是分部,则无效。
  78. return true;
  79. }
  80. if(blockData.firstNodeType == blockType.RATION && selected.sourceType == Bills.getSourceType()){ //复制的是定额/量价/工料机、焦点行是分部/清单父项则无效
  81. if( selected.data.type == billType.FB ){
  82. return true;
  83. }
  84. if(MainTreeCol.readOnly.billsParent(selected)){
  85. return true;
  86. }
  87. if(this.haveCalcBase(selected)){//有使用基数计算,disable
  88. return true;
  89. }
  90. }
  91. if(blockData.firstNodeType != blockType.RATION && selected.sourceType == ModuleNames.ration){
  92. return true;
  93. }
  94. return false;
  95. },
  96. // 恢复删除节点有效性
  97. recoverBlockDisabled: function () {
  98. if (projectReadOnly) {
  99. return true;
  100. }
  101. const recycle = getLocalCache('project_recycle');
  102. if (!recycle) {
  103. return true;
  104. }
  105. const recycleObj = JSON.parse(recycle);
  106. const firstData = recycleObj.datas.length ? recycleObj.datas[0] : null;
  107. if (!firstData || firstData.projectID !== projectObj.project.ID()) {
  108. return true;
  109. }
  110. const parentID = firstData.sourceType === projectObj.project.Bills.getSourceType() ? firstData.ParentID : firstData.billsItemID;
  111. const parent = projectObj.project.mainTree.findNode(parentID);
  112. // parentID为-1时,为大项费用
  113. if (parentID !== -1 && !parent) {
  114. return true;
  115. }
  116. return false;
  117. },
  118. haveCalcBase : function (node) {
  119. return this.ifCalcBase(node.data.calcBase);
  120. },
  121. ifCalcBase: function (calcBase) {
  122. if (calcBase == null || calcBase == undefined || calcBase == "") {
  123. return false;
  124. }
  125. return true;
  126. },
  127. isDXFYorMainEq:function (node) {//焦点行是大项费用或定额下的主材设备
  128. if(node.sourceType == ModuleNames.bills && node.data.type == billType.DXFY){//焦点行是大项费用则无效;
  129. return true;
  130. }
  131. if(node.sourceType == ModuleNames.ration_glj){// 焦点行是定额下的主材设备则无效;
  132. return true;
  133. }
  134. return false;
  135. },
  136. getBlockData:function () {
  137. let blockString =getLocalCache('project_block');
  138. if(blockString !=null){
  139. return JSON.parse(blockString);
  140. }
  141. return null;
  142. },
  143. extractBlockData: function (selection) {
  144. let startTime = +new Date();
  145. let blockObj = {
  146. compilationID:projectObj.project.projectInfo.compilation,
  147. datas:[]
  148. };
  149. let firstNode = projectObj.project.mainTree.items[selection.row];
  150. let copyNodes = [firstNode];
  151. if(selection.rowCount > 1){
  152. for(let i = 1;i<selection.rowCount;i ++){
  153. let temNode = projectObj.project.mainTree.items[selection.row + i];
  154. if(this.copyBtnDisable(temNode) == false&&temNode.getParentID() != -1 && temNode.getParentID() == firstNode.getParentID() ){//多选时,与选中的第一个节点同一层次的才复制,即父节点一样的,其它的忽略,copyBtnDisable 判断要为false
  155. copyNodes.push(temNode);
  156. }
  157. }
  158. }
  159. if(firstNode.sourceType == projectObj.project.Bills.getSourceType()){
  160. // 特殊处理大项费用,因为DXFY在billType中为1,与blockType中的RATION冲突了
  161. blockObj.firstNodeType = firstNode.data.type === billType.DXFY ? blockType.DXFY : firstNode.data.type;
  162. }else if(firstNode.sourceType == projectObj.project.Ration.getSourceType()){
  163. blockObj.firstNodeType = blockType.RATION;
  164. }
  165. blockObj.isFBFX = projectObj.project.Bills.isFBFX(firstNode);
  166. let ration_glj_Map = _.groupBy(projectObj.project.ration_glj.datas, 'rationID');
  167. for(let cNode of copyNodes){
  168. blockObj.datas.push(this.getNodeDatas(cNode,ration_glj_Map));
  169. }
  170. let getDataTime = +new Date();
  171. console.log(`复制所用时间——${getDataTime - startTime}`);
  172. blockObj.copyTime = +new Date();//设置复制时间,可以用来做过期处理
  173. setLocalCache('project_block',JSON.stringify(blockObj));
  174. let endTime = +new Date();
  175. console.log(`复制所用时间——${endTime - startTime}`);
  176. return blockObj;
  177. },
  178. copyBlock:function (selection) {
  179. const block = this.extractBlockData(selection);
  180. setLocalCache('project_block', JSON.stringify(block));
  181. },
  182. recycleBlock: function (selection) {
  183. const block = this.extractBlockData(selection);
  184. setLocalCache('project_recycle', JSON.stringify(block));
  185. },
  186. getNodeDatas : function (node,ration_glj_Map) {
  187. let data = node.data;
  188. data.sourceType = node.sourceType;
  189. if(node.sourceType == projectObj.project.Bills.getSourceType()){
  190. data.quantity_details = projectObj.project.quantity_detail.getDetailByBillID(data.ID);
  191. }
  192. if(node.sourceType == projectObj.project.Ration.getSourceType()){
  193. if(node.data.type == rationType.install){ //如果是生成的安装增加费,则跳过
  194. return null;
  195. }
  196. let gljList = ration_glj_Map[data.ID]?ration_glj_Map[data.ID]:[];//gljOprObj.filterGljByRation(node.data, datas);
  197. gljList = gljOprObj.combineWithProjectGlj(gljList,false);
  198. data.ration_gljs = gljList;
  199. data.ration_coes = projectObj.project.ration_coe.getCoeByRationID(data.ID);
  200. data.ration_installations = projectObj.project.ration_installation.getInstallationByRationID(data.ID);
  201. data.quantity_details = projectObj.project.quantity_detail.getDetailByRationID(data.ID);
  202. data.ration_template = projectObj.project.ration_template.getTemplateByRationID(data.ID);
  203. }else if(node.children.length>0){
  204. let children = [];
  205. for(let c of node.children){
  206. let tem_data = this.getNodeDatas(c,ration_glj_Map);
  207. tem_data?children.push(tem_data):'';
  208. }
  209. data.children = children;
  210. }
  211. return data;
  212. },
  213. pasteBlock:function (selected) {
  214. let blockData = this.getBlockData();
  215. let position = this.initOptionDiv(selected,blockData);
  216. if(position == null){
  217. return;
  218. }
  219. this.confirmPaste(blockData,selected,position);
  220. //do paste
  221. },
  222. // 恢复删除节点
  223. recoverBlock: function () {
  224. const recycle = JSON.parse(getLocalCache('project_recycle'));
  225. const firstData = recycle.datas[0];
  226. const parentID = firstData.sourceType === projectObj.project.Bills.getSourceType() ? firstData.ParentID : firstData.billsItemID;
  227. // 恢复到父节点最末子项
  228. if (parentID !== -1) {
  229. const parent = projectObj.project.mainTree.findNode(parentID);
  230. this.confirmPaste(recycle, parent, 'sub');
  231. } else {
  232. // 恢复到最末根节点后项
  233. const lastRoot = projectObj.project.mainTree.roots[projectObj.project.mainTree.roots.length - 1];
  234. this.confirmPaste(recycle, lastRoot, 'next');
  235. }
  236. },
  237. initOptionDiv:function (selected,blockData) {
  238. let Bills = projectObj.project.Bills;
  239. if( blockData.firstNodeType == blockType.RATION){//如果剪切板中是复制的定额块
  240. if(selected.sourceType == ModuleNames.ration && selected.data.type != rationType.install ){//焦点行是定额/量价/工料机,则粘贴为后项
  241. return "next";
  242. }
  243. if(this.isFXorBX(selected)||(selected.sourceType === Bills.getSourceType()&& selected.source.children.length == 0)){//焦点行是分项/叶子清单,且无基数计算,则粘贴到分项/叶子清单下
  244. if(!this.haveCalcBase(selected)){//无基数计算
  245. return "sub";
  246. }
  247. }
  248. }
  249. //设置选项的初始值
  250. setRadioProp('next_node',{checked:true,disabled:false});
  251. setRadioProp('pre_node',{checked:false,disabled:false});
  252. if(Bills.isFBFX(selected)){// (2.1)、焦点行属于“分部分项工程”:
  253. //复制块的第一层、焦点行的类型都是“分部”
  254. if(blockData.firstNodeType == blockType.FB && this.isFB(selected)){
  255. if(selected.children.length > 0 && this.isFXorBX(selected.children[0])){//焦点行分部下有分项补项,弹出“粘贴位置选择”,默认为当前行的后项,可选前项,子项灰显。
  256. setRadioProp('sub_node',{checked:false,disabled:true});
  257. }else {
  258. //默认为当前行的后项,可选前项、子项。
  259. setRadioProp('sub_node',{checked:false,disabled:false});
  260. }
  261. }
  262. //复制块的第一层、焦点行的类型都是“分项”或补项
  263. if(this.blockIsFXorBX(blockData.firstNodeType) && this.isFXorBX(selected)){
  264. //默认为当前行的后项,可选前项,子项灰显不可选。
  265. setRadioProp('sub_node',{checked:false,disabled:true});
  266. }
  267. //复制块的第一层是分项,清单,焦点行是分部,且分部下无子项或者子项是分项
  268. if((blockData.firstNodeType ==blockType.BILL|| this.blockIsFXorBX(blockData.firstNodeType))&& this.isFB(selected)){
  269. if(selected.children.length == 0 || this.isFXorBX(selected.children[0])){
  270. return 'sub';//不弹出选择窗口,直接粘贴为子项。
  271. }
  272. }
  273. }else {//焦点行不属于“分部分项工程”
  274. if(blockData.firstNodeType == blockType.BILL &&selected.sourceType == Bills.getSourceType()){//如果剪切板中是从“分部分项工程”以外复制的清单块
  275. if(selected.children.length==0 || selected.children[0].sourceType == Bills.getSourceType()){//焦点行没有子项,或者子项是清单,判断是否有基数计算
  276. if(this.haveCalcBase(selected)){//如果焦点行有基数计算,则子项灰显不可选。
  277. setRadioProp('sub_node',{checked:false,disabled:true});
  278. }else{//如果焦点行没有基数计算,则子项可选。
  279. setRadioProp('sub_node',{checked:false,disabled:false});
  280. }
  281. }else {
  282. setRadioProp('sub_node',{checked:false,disabled:true});
  283. }
  284. }
  285. }
  286. $("#pastePosition").modal({show:true});
  287. return null;
  288. function setRadioProp(id,options) {
  289. $("#"+id).prop("checked",options.checked);
  290. $("#"+id).prop("disabled",options.disabled);
  291. }
  292. },
  293. /**
  294. * @param blockData
  295. * @param selected
  296. * @param position next/pre/sub
  297. */
  298. confirmPaste:function (blockData,selected,position) {
  299. let me = this;
  300. let project = projectObj.project;
  301. let Bills = project.Bills;
  302. let parent = null,next = null,pre = null;
  303. let firstParentID = null, lastNextID = null;
  304. let updateData = [],billUpdate = null;
  305. let billsIDMap = {};//用来做新旧ID映射
  306. if(blockData.compilationID != projectObj.project.projectInfo.compilation){//如果编办不一样,不能复制
  307. alert("编办不一致,不能粘贴!");
  308. return;
  309. }
  310. if(position == 'next'){ //插入为选中节点的后项,即选中节点为前项
  311. parent = selected.parent;
  312. next = selected.nextSibling;
  313. pre = selected;
  314. }
  315. if(position == 'pre'){//插入为选中节点的前项,即选中节点为后项
  316. parent = selected.parent;
  317. next = selected;
  318. pre = selected.preSibling;
  319. }
  320. if(position == 'sub'){//即选中节点为父项
  321. parent = selected;
  322. pre = selected.lastChild();
  323. }
  324. let parentID = parent?parent.getID():-1;
  325. let nextID = next?next.getID():-1;
  326. if(blockData.firstNodeType == blockType.RATION){ //复制的是定额,需根据新的父项信息和工程量明细计算工程量
  327. for(let d of blockData.datas){
  328. d.billsItemID = parentID;
  329. if (blockData.zeroQuantity){ // 来自克隆块
  330. d.quantity = 0;
  331. d.quantityEXP = '';
  332. d.contain = '';
  333. }else{
  334. let billsQuantity = scMathUtil.roundForObj(parent.data.quantity,getDecimal("quantity",parent));
  335. this.calcRationQuantityAndContain(billsQuantity,d,parent.data.unit);
  336. }
  337. //如果粘贴位置不属于分部分项工程,或者不是安装工程,则把安装增加费内容置空
  338. if(!Bills.isFBFX(parent)|| !project.isInstall()){
  339. d.ration_installations = [];
  340. }
  341. }
  342. //计算序列号
  343. this.calcRationSerialNo(parentID,blockData,updateData,pre,next);
  344. }else {
  345. firstParentID = parentID;
  346. lastNextID = nextID;
  347. /* blockData.data.ParentID = parentID;
  348. blockData.data.NextSiblingID = nextID;*/
  349. if(pre){
  350. billUpdate = {type:blockData.datas[0].sourceType,query:{ID:pre.getID()},doc:{NextSiblingID:blockData.datas[0].ID}};
  351. }
  352. }
  353. let dataMap = this.preparePasteData(blockData.datas,billsIDMap,firstParentID,lastNextID,selected);
  354. if(billUpdate){
  355. billUpdate.doc.NextSiblingID = billsIDMap[billUpdate.doc.NextSiblingID];
  356. updateData.push(billUpdate);
  357. }
  358. dataMap.updateData = updateData;
  359. dataMap.projectID = projectObj.project.ID();
  360. console.log(dataMap);
  361. $.bootstrapLoading.start();
  362. CommonAjax.post('/bills/pasteBlock',dataMap,function (result) {
  363. $.bootstrapLoading.end();
  364. //更新前端缓存
  365. me.updateCache(result);
  366. //插入树节点
  367. let [rationNodes,calcNodes] = me.addToTree(parentID,nextID,result.bills,result.rations);
  368. //主材设备工料机插入主树
  369. project.ration_glj.addToMainTree(result.ration_gljs);
  370. //更新计算程序模板,并进行重新计算
  371. project.calcProgram.calcNodesAndSave(calcNodes,async function () {
  372. installationFeeObj.calcInstallationFee();
  373. await OVER_HEIGHT.reCalcOverHeightFee();
  374. await itemIncreaseFeeObj.calcItemIncreaseFeeByNodes(rationNodes);
  375. });
  376. })
  377. },
  378. updateCache:function(result){
  379. let project = projectObj.project;
  380. let Bills = project.Bills,Ration = project.Ration;
  381. let mainTree = project.mainTree;
  382. Bills.addDatasToList(result.bills);
  383. Ration.addDatasToList(result.rations);
  384. Ration.addSubListOfRation(result);//这里是新插入定额时
  385. /* project.quantity_detail.addDatasToList(result.quantity_details);
  386. project.ration_glj.addDatasToList(result.ration_gljs);
  387. project.ration_coe.addDatasToList(result.ration_coes);
  388. project.ration_installation.addDatasToList(result.ration_installations);*/
  389. for(let u of result.updateData){
  390. let unode = mainTree.findNode(u.query.ID);
  391. if(unode){
  392. for(let ukey in u.doc){
  393. unode.data[ukey] = u.doc[ukey];
  394. }
  395. }
  396. }
  397. //更新项目工料机模块信息-计算消耗量 ---修改了加载方式
  398. // project.projectGLJ.datas = result.gljData;
  399. // project.projectGLJ.calcQuantity();
  400. for(let r of result.rations){
  401. if(r.type == rationType.gljRation){//对于工料机类型的定额,要重新设置下市场单价
  402. gljOprObj.setGLJPrice(r);
  403. }
  404. }
  405. },
  406. addToTree:function (parentID,nextID,bills,rations) {
  407. let project = projectObj.project;
  408. let Bills = project.Bills,mainTree = project.mainTree;
  409. let parentNodeMap={};//用于去重
  410. let parentMap_b = {},parentMap_r = {};
  411. let newNodes = [],firstNode = null,rationNodes=[],billNodes=[],calcNodes=[];
  412. createParentMap(parentMap_b,bills,'bills');
  413. createParentMap(parentMap_r,rations,'ration');
  414. if(parentMap_b[parentID]){
  415. for(let i = 0;i < parentMap_b[parentID].length;i++){
  416. let temNode = loadTreeNode(parentID,nextID,parentMap_b[parentID][i],'bills');
  417. i==0?firstNode = temNode:'';
  418. }
  419. }else if(parentMap_r[parentID]){
  420. for(let i = 0;i<parentMap_r[parentID].length;i++){
  421. let temNode = loadTreeNode(parentID,nextID,parentMap_r[parentID][i],'ration');
  422. i==0?firstNode = temNode:'';
  423. }
  424. }
  425. ProjectController.syncDisplayNewNodes(projectObj.mainController, newNodes,true);
  426. let sels = projectObj.mainController.sheet.getSelections();
  427. //设置选中并更新下方显示
  428. projectObj.mainController.setTreeSelected(firstNode);
  429. const serialNo = firstNode.serialNo();
  430. projectObj.mainController.sheet.setSelection(serialNo, sels[0].col, 1, 1);
  431. projectObj.mainController.sheet.showRow(serialNo, GC.Spread.Sheets.VerticalPosition.center);
  432. //没有子节点的清单父节点要进行汇总计算
  433. for(let b of billNodes){
  434. if(!b.children || b.children.length == 0){
  435. if(b.parent &&!parentNodeMap[b.parent.ID]) calcNodes.push(b.parent);
  436. }
  437. }
  438. return [rationNodes,rationNodes.concat(calcNodes)];
  439. function loadTreeNode(parentID,nextID,data,type) {
  440. let newNode = null;
  441. if(type == 'bills'){
  442. let newSource = Bills.tree.insertByData(data,parentID,nextID,true);
  443. newNode = mainTree.insert(parentID, nextID,data.ID);
  444. if (newNode) {
  445. newNode.source = newSource;
  446. newNode.sourceType = Bills.getSourceType();
  447. newNode.data = data;
  448. newNodes.push(newNode);
  449. let subType = 'bills';
  450. let children = parentMap_b[data.ID];
  451. if(!children){
  452. children = parentMap_r[data.ID];
  453. subType = 'ration';
  454. }
  455. if(children){
  456. for(let c of children){
  457. loadTreeNode(data.ID,-1,c,subType);
  458. }
  459. }
  460. }
  461. billNodes.push(newNode);
  462. }else if(type == 'ration'){
  463. newNode = project.mainTree.insert(parentID, nextID, data.ID);
  464. newNode.source = data;
  465. newNode.sourceType = project.Ration.getSourceType();
  466. newNode.data = data;
  467. newNodes.push(newNode);
  468. rationNodes.push(newNode);
  469. }
  470. return newNode;
  471. }
  472. function createParentMap(parentMap,list,type) {
  473. let parentKey = type=='bills'?'ParentID':'billsItemID';
  474. for(let l of list){
  475. let parentID = l[parentKey];
  476. if(parentMap[parentID]){
  477. parentMap[parentID].push(l);
  478. }else {
  479. parentMap[parentID] = [l];
  480. }
  481. }
  482. }
  483. },
  484. preparePasteData : function (datas,billsIDMap,firstParentID,lastNextID,selected) {
  485. let me = this;
  486. me.datas = _.cloneDeep(projectObj.project.Bills.datas);
  487. let bills = [],rations=[],ration_gljs = [],ration_coes = [],quantity_details = [],ration_installations = [],ration_templates=[];
  488. let firstBillIDs = [],lastBillID = null;//记录第一层清单的ID,和第一层的最后一个清单ID
  489. let firstIDMap = {};
  490. for(let i = 0 ;i< datas.length ;i++){
  491. if(datas[i].sourceType == 'bills'){
  492. firstBillIDs.push(datas[i].ID);
  493. if(i == datas.length -1){//最后一个清单节点
  494. lastBillID = datas[i].ID
  495. }
  496. }
  497. eachData(datas[i],selected);
  498. }
  499. for(let f of firstBillIDs){
  500. firstIDMap[billsIDMap[f]] = f //反向映射
  501. }
  502. for(let b of bills){//更新ID
  503. if(firstIDMap[b.ID]) {//如果是第一层清单节点对应的数据,更新父ID 为当前树中的父ID
  504. b.ParentID = firstParentID;
  505. }else {
  506. billsIDMap[b.ParentID]?b.ParentID = billsIDMap[b.ParentID]:'';
  507. }
  508. if(b.ID == billsIDMap[lastBillID]){//如果是第一层清单最后节点对应的数据,更新下一节点ID为插入位置的下一个节点ID
  509. b.NextSiblingID = lastNextID;
  510. }else {
  511. billsIDMap[b.NextSiblingID]?b.NextSiblingID = billsIDMap[b.NextSiblingID]:'';
  512. }
  513. }
  514. return {bills:bills,rations:rations,ration_gljs:ration_gljs,ration_coes:ration_coes,quantity_details:quantity_details,ration_installations:ration_installations,ration_templates:ration_templates};
  515. function eachData(data,selected) {
  516. if(data.sourceType == 'bills'){
  517. let tem_b = createBillsData(data,selected);
  518. bills.push(tem_b);
  519. for(let d of data.quantity_details){
  520. quantity_details.push(createQuantityDetails(d,tem_b,'bills'));
  521. }
  522. if(data.children && data.children.length>0){
  523. for(let c of data.children){
  524. eachData(c, selected);
  525. }
  526. }
  527. }
  528. if( data.sourceType == 'ration'){
  529. let tem_r = createRationData(data);
  530. rations.push(tem_r);
  531. for(let d of data.quantity_details){
  532. quantity_details.push(createQuantityDetails(d,tem_r, 'ration'));
  533. }
  534. for(let g of data.ration_gljs){
  535. let tem_rg = createSubList(g,tem_r);
  536. tem_rg.billsItemID = tem_r.billsItemID;
  537. ration_gljs.push(tem_rg);
  538. }
  539. for(let o of data.ration_coes){
  540. ration_coes.push(createSubList(o,tem_r));
  541. }
  542. for(let ri of data.ration_installations){
  543. ration_installations.push(createSubList(ri,tem_r));
  544. }
  545. if(data.ration_template){
  546. ration_templates.push(createRationTemplate(data.ration_template,tem_r))
  547. }
  548. }
  549. }
  550. function createRationTemplate(oldTem,rationData) {
  551. let template = _.cloneDeep(oldTem);
  552. delete template._id;
  553. template.ID = uuid.v1();
  554. template.projectID = projectObj.project.ID();
  555. template.rationID = rationData.ID;
  556. //其它值恢复成默认
  557. for(let t of template.templateList){
  558. t.billID = "";
  559. t.fxID="";
  560. t.quantity="0";
  561. t.coe = "";
  562. if(t.defaultLocation) t.billsLocation = t.defaultLocation;
  563. }
  564. return template;
  565. }
  566. function createSubList(subData,rationData) {
  567. let tem_sub = _.cloneDeep(subData);
  568. delete tem_sub._id;
  569. tem_sub.ID = uuid.v1();
  570. tem_sub.projectID = projectObj.project.ID();
  571. tem_sub.rationID = rationData.ID;
  572. return tem_sub;
  573. }
  574. function createQuantityDetails(detailData,pdata,type) {
  575. let tem_detail = _.cloneDeep(detailData);
  576. delete tem_detail._id;
  577. tem_detail.ID = uuid.v1();
  578. tem_detail.projectID = projectObj.project.ID();
  579. if(type == 'bills'){
  580. tem_detail.billID = pdata.ID;
  581. }
  582. if(type == 'ration'){
  583. tem_detail.rationID = pdata.ID;
  584. }
  585. return tem_detail;
  586. }
  587. function createRationData(rationData) {
  588. let tem_ration = _.cloneDeep(rationData);
  589. //删除旧数据
  590. delete tem_ration._id;
  591. delete tem_ration.fees;
  592. delete tem_ration.feesIndex;
  593. delete tem_ration.ration_gljs;
  594. delete tem_ration.ration_coes;
  595. delete tem_ration.ration_installations;
  596. delete tem_ration.quantity_details;
  597. delete tem_ration.referenceRationID;//删除关联的主定额信息
  598. delete tem_ration.__v;
  599. delete tem_ration.sourceType;
  600. delete tem_ration.quantityCoe;
  601. delete tem_ration.rationQuantityCoe;
  602. delete tem_ration.tenderQuantity;
  603. tem_ration.projectID = projectObj.project.ID();
  604. tem_ration.ID = uuid.v1();
  605. billsIDMap[tem_ration.billsItemID]?tem_ration.billsItemID = billsIDMap[tem_ration.billsItemID]:'';
  606. let defaultLibID = rationLibObj.getDefaultStdRationLibID();
  607. if(defaultLibID&&tem_ration.type == rationType.ration){
  608. tem_ration.prefix = projectObj.project.Ration.getRationPrefix(defaultLibID,tem_ration);
  609. }
  610. return tem_ration;
  611. }
  612. function createBillsData(billsData, selected) { //ID、重新生成code
  613. let Bills = projectObj.project.Bills;
  614. let temData = _.cloneDeep(billsData);
  615. //删除旧数据
  616. if(temData.children && temData.children.length>0){//如果是有子项,说明是计算得到的,要删除重新计算,没有子项,但是fees有值,说明是自已输入的,值要一起粘贴
  617. delete temData.fees;
  618. delete temData.feesIndex;
  619. }
  620. delete temData._id;
  621. delete temData.children;
  622. delete temData.quantity_details;
  623. delete temData.__v;
  624. delete temData.sourceType;
  625. delete temData.quantityCoe;
  626. delete temData.rationQuantityCoe;
  627. //从分项、补项 到清单
  628. if ((temData.type == billType.FX || temData.type == billType.BX) && !Bills.isFBFX(selected)) temData.type = billType.BILL;
  629. //从清单到分部分项
  630. if (temData.type == billType.BILL && Bills.isFBFX(selected)) temData.type = billType.FX;
  631. temData.projectID = projectObj.project.ID();
  632. let newID = uuid.v1(); //新的清单ID
  633. billsIDMap[temData.ID] = newID;
  634. temData.ID = newID; //新的清单ID
  635. if(temData.code && temData.code.length == 12){//是从清单库来的 //temData.billsLibId && temData.billsLibId!="" 2019-09-03 修改需求,不做清单库ID判断
  636. let value = temData.code.substr(0,9);
  637. if (value&&value.length === 9) {//&& /^[\d]+$/.test(value) 去掉全数字判断 07-31 zhang
  638. temData.code = me.newFormatCode(value);
  639. }
  640. }
  641. me.datas.push(temData);//为了下一个清单的std code 一样
  642. return temData;
  643. }
  644. },
  645. calcRationSerialNo:function (billsItemID,blockData,updateData,pre,next) {
  646. let br = projectObj.project.Ration.getBillsSortRation(billsItemID);
  647. let firstSerialNo = 0;
  648. if(next == null){ //没有下一树节点,即为最后节点
  649. firstSerialNo = br.length > 0 ? br[br.length - 1].serialNo + 1 : 1;
  650. }else {//有下一节点
  651. let startIndex =0;
  652. if(pre){
  653. startIndex = br.indexOf(pre.data)+1;
  654. }
  655. firstSerialNo = br[startIndex].serialNo;
  656. for(let i = startIndex;i < br.length; i++){
  657. let br_serialNo = br[i].serialNo + blockData.datas.length; //i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  658. updateData.push({type:blockData.datas[0].sourceType,query:{ID:br[i].ID},doc:{serialNo:br_serialNo}});//更新兄弟节点序列号
  659. }
  660. }
  661. for(let i = 0 ; i < blockData.datas.length;i++){
  662. blockData.datas[i].serialNo = firstSerialNo;
  663. firstSerialNo += 1;
  664. }
  665. },
  666. calcRationQuantityAndContain : function (billsQuantity,ration,billsUnit) {//计算定额工程量和含量
  667. let EXPString = ration.quantityEXP+"";
  668. if(EXPString.indexOf("QDL") != -1){
  669. if(EXPString=="QDL"){//定额的工程量是直接通过清单量填进来的;
  670. let t_unit = ration.unit?ration.unit.replace(/^\d+/,""):"";
  671. if(t_unit!=billsUnit){//如果定额的单位去除前面的数字后不等于清单单位,定额工程量保持不变
  672. return ;
  673. }
  674. let times = parseInt(ration.unit);
  675. if(isNaN(times)){
  676. times = 1;
  677. }
  678. ration.quantity = scMathUtil.roundForObj(billsQuantity / times,getDecimal("ration.quantity"));
  679. billsQuantity?ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,getDecimal("process")):ration.contain = 0;
  680. } else {//如果定额的工程量是通过计算出来的,则应该重新计算。
  681. let tem_contain = scMathUtil.roundForObj(ration.contain,getDecimal("process"));
  682. let tem_quantity = scMathUtil.roundForObj(billsQuantity*tem_contain,getDecimal("ration.quantity"));
  683. ration.quantity = tem_quantity;
  684. }
  685. }else {//GCLMXHJ
  686. let tem_contain=0;
  687. if(billsQuantity&&billsQuantity!=0){
  688. let children_quantity = scMathUtil.roundForObj(ration.quantity,getDecimal("ration.quantity"));
  689. // children_quantity = scMathUtil.roundForObj(this.reverseQuantity(children_quantity,rationNode),getDecimal("quantity",rationNode)); 原先是要反算的,现在改成不用反算了
  690. tem_contain =scMathUtil.roundForObj(children_quantity/billsQuantity,getDecimal("process"));
  691. }
  692. ration.contain = tem_contain;
  693. }
  694. },
  695. newFormatCode : function (stdCode, filterCode) {
  696. let matchs = this.sameStdCode(stdCode, filterCode);
  697. let format = function (Number) {
  698. let s = Number + '';
  699. while (s.length < 3) {
  700. s = '0' + s;
  701. }
  702. return s;
  703. };
  704. for (let i = 0; i <= matchs.length; i++) {
  705. let formatCode = stdCode + format(i+1);
  706. if (matchs.indexOf(formatCode) === -1) {
  707. return formatCode;
  708. }
  709. }
  710. },
  711. sameStdCode : function (stdCode, filterCode) {
  712. let reg = new RegExp('^' + stdCode), matchs= [];
  713. for (let data of this.datas) {
  714. if (data.code && data.code.length === 12 && reg.test(data.code) && data.code !== filterCode) {
  715. matchs.push(data.code);
  716. }
  717. }
  718. return matchs;
  719. },
  720. isFB :function (selected) {
  721. return selected.sourceType == projectObj.project.Bills.getSourceType() && selected.data.type == billType.FB;
  722. },
  723. isFXorBX:function (selected) {//是分项或者补项
  724. return projectObj.project.Bills.isFXorBX(selected);
  725. },
  726. blockIsFB: function (type) {
  727. return type == blockType.FB
  728. },
  729. blockIsFXorBX: function (type) {
  730. return type == blockType.FX||type == blockType.BX;
  731. },
  732. blockIsLeaveBills: function (bills) {
  733. if (bills.children && bills.children.length > 0) {
  734. return bills.children[0].sourceType == "ration" //如果子节点是定额,则说明是页子清单
  735. }
  736. return true;
  737. },
  738. removeBlock:function () {
  739. removeLocalCache('project_block');
  740. }
  741. };