block_controller.js 31 KB

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