block_controller.js 34 KB

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