quantity_detail.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var quantity_detail = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. var tools = {};
  8. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  9. var quantity_detail = function (proj) {
  10. this.gljTree = cacheTree.createNew(this);
  11. // this.project = proj;
  12. this.datas = [];
  13. var sourceType = ModuleNames.quantity_detail;
  14. this.getSourceType = function () {
  15. return sourceType;
  16. }
  17. proj.registerModule(ModuleNames.quantity_detail, this);
  18. this.temList=[];
  19. };
  20. // prototype用于定义public方法
  21. quantity_detail.prototype.loadData = function (datas) {
  22. this.datas = datas;
  23. };
  24. quantity_detail.prototype.getListByID = function (ID,field) {
  25. let condition={};
  26. condition[field] = ID;
  27. let details = _.filter(this.datas, condition);
  28. details = _.sortBy(details, 'seq');
  29. return details;
  30. };
  31. // 提交数据后返回数据处理
  32. quantity_detail.prototype.doAfterUpdate = function(err, data){
  33. if(!err){
  34. if(data.updateTpye=='ut_update'){
  35. this.refreshAfterUpdate(data);
  36. }else if(data.updateTpye=='ut_delete'){
  37. this.refreshAfterDelete(data);
  38. } else {
  39. this.refreshAfterSave(data);
  40. }
  41. }else {
  42. alert(err.message);
  43. this.refreshSheetData();
  44. }
  45. };
  46. quantity_detail.prototype.refreshAfterSave=function(data,batchCallback){
  47. var me = this;
  48. let newData = null;
  49. if(data.hasOwnProperty('resort')){
  50. this.resortData(data.doc,1);
  51. _.forEach(data.update_task,function (item) {
  52. me.refreshEachItme(item.query,item.doc);
  53. })
  54. newData = data.doc;
  55. }else {
  56. newData = data;
  57. }
  58. this.datas.push(newData);
  59. if(batchCallback){
  60. gljOprObj.detailData.push(newData);
  61. }else {
  62. this.refreshSheetData();
  63. }
  64. };
  65. quantity_detail.prototype.resortData=function(data,req){
  66. for(var i =0;i<gljOprObj.detailData.length;i++){
  67. var item = gljOprObj.detailData[i];
  68. if(item.seq>=data.seq){
  69. item.seq=item.seq+req;
  70. }
  71. }
  72. };
  73. quantity_detail.prototype.refreshAfterUpdate=function(data,batchCallback){
  74. var me = this;
  75. var filter_object;
  76. if(data.hasOwnProperty('refreshList')){
  77. _.forEach(data.refreshList,function (item) {
  78. filter_object= me.refreshEachItme(item.query,item.doc);
  79. })
  80. }else {
  81. filter_object = me.refreshEachItme(data.query,data.doc);
  82. }
  83. var showList = _.filter(this.datas,filter_object);
  84. gljOprObj.detailData=showList;
  85. gljOprObj.detailData=_.sortBy(gljOprObj.detailData,'seq');
  86. if(batchCallback == undefined){
  87. this.refreshSheetData();
  88. }
  89. };
  90. quantity_detail.prototype.refreshEachItme = function(query,doc){
  91. var detail_list = this.datas;
  92. var detail_index= _.findIndex(detail_list,function(detail){
  93. return detail.ID==query.ID;
  94. })
  95. _.forEach(doc, function(n, key) {
  96. detail_list[detail_index][key] = n;
  97. });
  98. var filter_object;
  99. if(detail_list[detail_index].hasOwnProperty('rationID')){
  100. filter_object={'rationID':detail_list[detail_index].rationID};
  101. }else {
  102. filter_object={'billID':detail_list[detail_index].billID};
  103. }
  104. return filter_object;
  105. };
  106. quantity_detail.prototype.refreshAfterDelete=function(data){
  107. var me = this;
  108. if(data.doc.seq != gljOprObj.detailData.length - 1){
  109. this.resortData(data.doc,-1);
  110. }
  111. _.forEach(data.update_task,function (item) {
  112. me.refreshEachItme(item.query,item.doc);
  113. });
  114. _.remove(this.datas,{ID:data.doc.ID});
  115. this.refreshSheetData();
  116. };
  117. quantity_detail.prototype.refreshSheetData=function () {
  118. gljOprObj.showQuantityDetailData();
  119. };
  120. quantity_detail.prototype.saveQuantityDetail=function (args,dataCode,selected,batchCallback) {
  121. var me = this;
  122. var doc={};
  123. var selected = selected?selected:projectObj.project.mainTree.selected;
  124. if(selected.sourceType==ModuleNames.ration){
  125. doc.rationID=selected.data.ID;
  126. }
  127. if(selected.sourceType==ModuleNames.bills){
  128. doc.billID=selected.data.ID;
  129. }
  130. doc.projectID = selected.data.projectID;
  131. doc[dataCode]=args.editingText;
  132. doc.seq=args.row;
  133. if(dataCode=='regex'){// if(this.checkAndCalcResult(args.editingText,args.row,doc) === false){
  134. if(this.checkAndCalcResult(args.editingText,args.row,doc) === false){
  135. gljOprObj.showQuantityDetailData();
  136. return;
  137. }
  138. doc.refreshQuantity=true;
  139. if(!selected.data.hasOwnProperty('isFromDetail')||selected.data.isFromDetail==0){
  140. if(!args.replace){//为了批量粘贴时不重复提示,普通编辑时不受影响
  141. hintBox.infoBox('操作确认', '确定要使用工程量明细替换原工程量吗?', 2, function () {
  142. args.replace = true;
  143. me.doSaveAction(doc,args,batchCallback);
  144. }, function () {
  145. doc.refreshQuantity=false;
  146. me.doSaveAction(doc,args,batchCallback);
  147. },['确定','取消'],false);
  148. return;
  149. }
  150. }
  151. }
  152. me.doSaveAction(doc,args,batchCallback);
  153. };
  154. quantity_detail.prototype.doSaveAction = function(doc,args,batchCallback){
  155. let url="",me = this;
  156. $.bootstrapLoading.start();
  157. if(args.hasOwnProperty("insertRecode")){//右键插入或者是通过直接编辑保存
  158. url = "/quantity_detail/insertRecode";
  159. }else{
  160. url = "/quantity_detail/save";
  161. }
  162. CommonAjax.post(url,doc,function (data) {
  163. $.bootstrapLoading.end();
  164. if(doc.refreshQuantity==false){//清空数据
  165. me.cleanQuantityDetail();
  166. }else {
  167. data.newRecord?me.refreshAfterSave(data.newRecord,batchCallback):me.refreshAfterSave(data,batchCallback);
  168. if(batchCallback){
  169. batchCallback(args);
  170. }else if(data.node){
  171. me.refreshRationOrBillNodes(data.node);
  172. }
  173. //gljOprObj.detailSheet.setActiveCell(0,0);
  174. //gljOprObj.detailSheet.clearSelection();
  175. }
  176. },function () {
  177. $.bootstrapLoading.end();
  178. });
  179. };
  180. quantity_detail.prototype.refreshRationOrBillNodes=function(node){//工程量明细更新后触发定额或清单工程量改变,进行相应的更新
  181. let treeNode = gljOprObj.updateDataNodeProperty(node.ID,node.data);
  182. if(treeNode){//触发计算
  183. if(treeNode.sourceType === project.Bills.getSourceType()){
  184. console.log(treeNode.data.quantity);
  185. this.updateBillQuantity(treeNode.data.quantity,treeNode,treeNode.data.quantityEXP);
  186. }else {//更新定额所使用的值要用还没转换前的
  187. node.data? this.updateRationQuantity(node.data.r_quantity,treeNode,treeNode.data.quantityEXP):"";
  188. }
  189. }
  190. };
  191. quantity_detail.prototype.insertQuantityDetail = function (row) {
  192. var args = {
  193. row:row,
  194. editingText:1
  195. };
  196. if(row < gljOprObj.detailData.length){
  197. args.insertRecode = true;
  198. }
  199. this.saveQuantityDetail(args,'isSummation');
  200. };
  201. quantity_detail.prototype.deleteQuantityDetail = function (row) {
  202. var me = this;
  203. var deleteable = this.checkReference(row);
  204. if(deleteable){
  205. var recode = gljOprObj.detailData[row];
  206. $.bootstrapLoading.start();
  207. var callback=function (result) {
  208. me.refreshAfterDelete(result.data);
  209. if(result.node){//触发计算
  210. me.refreshRationOrBillNodes(result.node);
  211. }
  212. $.bootstrapLoading.end();
  213. }
  214. CommonAjax.post("/quantity_detail/deleteRecode",recode,callback,function () {
  215. $.bootstrapLoading.end();
  216. });
  217. }else {
  218. alert("当前行已被引用,不可删除。");
  219. }
  220. };
  221. quantity_detail.prototype.checkReference = function (row) {
  222. var deleteable = true;
  223. for(var i =0;i<gljOprObj.detailData.length;i++){
  224. var item = gljOprObj.detailData[i];
  225. if(_.includes(item.referenceIndexs,row+1)){
  226. deleteable = false;
  227. break;
  228. }
  229. }
  230. return deleteable;
  231. };
  232. quantity_detail.prototype.moveDown = function (row) {
  233. let sel = gljOprObj.detailSheet.getSelections()[0];
  234. this.swapRow(row);
  235. gljOprObj.detailSheet.setSelection(row+1,sel.col,sel.rowCount,sel.colCount);//更改选中行
  236. };
  237. quantity_detail.prototype.moveUp = function (row) {
  238. let sel = gljOprObj.detailSheet.getSelections()[0];
  239. this.swapRow(row-1);
  240. gljOprObj.detailSheet.setSelection(row-1,sel.col,sel.rowCount,sel.colCount);//更改选中行
  241. };
  242. quantity_detail.prototype.swapRow = function (preRow) {
  243. let me = this;
  244. let update_task = [];
  245. let a_row = gljOprObj.detailData[preRow];//
  246. let b_row = gljOprObj.detailData[preRow +1];//
  247. let temA = a_row.seq;
  248. let temB = b_row.seq;
  249. a_row.seq = temB;
  250. update_task.push({query:{ID:a_row.ID,projectID:a_row.projectID},doc:{seq:a_row.seq}});
  251. b_row.seq = temA;
  252. update_task.push({query:{ID:b_row.ID,projectID:b_row.projectID},doc:{seq:b_row.seq}});
  253. gljOprObj.detailData.forEach(function (item) {
  254. if(_.includes(item.referenceIndexs,temA+1)||_.includes(item.referenceIndexs,temB+1)){
  255. var regex = item.regex;
  256. for (var i=0;i<item.referenceIndexs.length;i++){
  257. if(item.referenceIndexs[i]==temA+1){
  258. regex = me.replaceAll('C'+item.referenceIndexs[i],'B'+(temB+1),regex);
  259. regex = me.replaceAll('c'+item.referenceIndexs[i],'b'+(temB+1),regex);
  260. item.referenceIndexs[i]=temB+1;
  261. }else if(item.referenceIndexs[i]==temB+1){
  262. regex = me.replaceAll('C'+item.referenceIndexs[i],'B'+(temA+1),regex);
  263. regex = me.replaceAll('c'+item.referenceIndexs[i],'b'+(temA+1),regex);
  264. item.referenceIndexs[i]=temA+1;
  265. }
  266. }
  267. regex = me.replaceAll('B','C',regex);
  268. regex = me.replaceAll('b','c',regex);
  269. update_task.push({query:{ID:item.ID,projectID:item.projectID},doc:{regex:regex,referenceIndexs:item.referenceIndexs}});
  270. }
  271. });
  272. me.commonUpdate("/quantity_detail/swapRow",update_task);
  273. };
  274. quantity_detail.prototype.replaceAll=function(FindText, RepText,str) {
  275. /*let regExp = new RegExp(FindText, "g");
  276. return str.replace(regExp, RepText);*/
  277. return replaceAll(FindText, RepText,str);
  278. };
  279. quantity_detail.prototype.calcResult = function (doc) {//return false 代表输入有误
  280. let field = doc.billID?"billID":"rationID";// doc.rationID
  281. let detailList = this.getListByID(doc[field],field);
  282. let result = this.getEvalResult(doc.referenceIndexs,detailList,doc.regex);
  283. if(result === null){
  284. return false;
  285. }else {
  286. doc.result = result;
  287. return true;
  288. }
  289. };
  290. quantity_detail.prototype.getEvalResult = function(referenceIndexs,detailList,regex) {
  291. try {
  292. let decimal = getDecimal("quantity_detail");
  293. for(let i of referenceIndexs){
  294. regex = this.replaceReference(i,detailList,regex)
  295. }
  296. console.log('replace all C reference -----'+regex);
  297. regex = this.replaceSqr(regex);
  298. console.log('replace all sqar reference -----'+regex);
  299. return scMathUtil.roundTo(eval(regex), -decimal);
  300. }catch (error){
  301. alert('输入的表达式有误,请重新输入!');
  302. return null;
  303. }
  304. };
  305. quantity_detail.prototype.replaceReference = function(index,detailList,str) {
  306. str=str.toUpperCase();
  307. let rstr= detailList[index-1].regex==null?'0':'('+detailList[index-1].regex+')';
  308. str=this.replaceAll('C'+index,rstr,str);
  309. if(detailList[index-1].referenceIndexs.length>0){
  310. for (let i of detailList[index-1].referenceIndexs){
  311. str =this.replaceReference(i,detailList,str);
  312. }
  313. }
  314. return str;
  315. };
  316. quantity_detail.prototype.replaceSqr=function(text) {
  317. var squarRegex = /\([^\^]+\)\^\d+/g;
  318. var sqararr = text.match(squarRegex);
  319. var squarRegex2 = /C[0-9]+\^\d+|[0-9]+([.]{1}[0-9]+){0,1}\^\d+/g; //匹配没有括号的
  320. var sqararr2=text.match(squarRegex2);
  321. if(sqararr){
  322. text=this.converSqrByArr(sqararr,text);
  323. }
  324. if(sqararr2){
  325. text=this.converSqrByArr(sqararr2,text);
  326. }
  327. return text;
  328. };
  329. quantity_detail.prototype.converSqrByArr = function(sqararr,text) {
  330. var temp = text;
  331. sqararr.forEach(function (item) {
  332. var arr = item.split('\^');
  333. var y = parseInt(arr[1]);
  334. var x_arr = [];
  335. for (var i = 0; i < y; i++) {
  336. x_arr.push(arr[0]);
  337. }
  338. var temStr = x_arr.join('*');
  339. temp = temp.replace(item, temStr);
  340. });
  341. return temp;
  342. };
  343. quantity_detail.prototype.checkAndCalcResult = function (regex,row,doc) {//return false 表示没有通过检查
  344. if(!this.regexChecking(regex)||!this.referenceChecking(regex,row,doc)||!this.calcResult(doc)){
  345. return false;
  346. }
  347. return true;
  348. };
  349. quantity_detail.prototype.updateQuantityDetail=function (args,dataCode,recode,selected,batchCallback) {
  350. var doc ={},me = this;
  351. var query={
  352. ID:recode.ID,
  353. projectID:recode.projectID
  354. };
  355. var selected = selected?selected:projectObj.project.mainTree.selected;
  356. doc[dataCode]=args.editingText;
  357. if (dataCode == 'regex') {
  358. if(recode.hasOwnProperty('rationID')){
  359. query.rationID=recode.rationID;
  360. doc.rationID=recode.rationID;
  361. }else {
  362. query.billID = recode.billID;
  363. doc.billID=recode.billID;
  364. }
  365. if(gljUtil.isDef(args.editingText)&&this.checkAndCalcResult(args.editingText,args.row,doc) === false){
  366. gljOprObj.showQuantityDetailData();
  367. return;
  368. }
  369. query.refreshQuantity=true;
  370. if(!selected.data.hasOwnProperty('isFromDetail')||selected.data.isFromDetail==0){
  371. if(!args.replace){//为了批量粘贴时不重复提示,普通编辑时不受影响
  372. hintBox.infoBox('操作确认', '确定要使用工程量明细替换原工程量吗?', 2, function () {
  373. args.replace = true;
  374. query.index = args.row;
  375. me.updateQuantityRegex(query,doc,args,batchCallback)
  376. }, function () {
  377. me.cleanQuantityDetail(selected,true);
  378. },['确定','取消'],false);
  379. return;
  380. }
  381. }
  382. query.index = args.row;
  383. me.updateQuantityRegex(query,doc,args,batchCallback);
  384. }else {
  385. me.normalUpdate(query,doc,args,batchCallback);
  386. }
  387. };
  388. quantity_detail.prototype.updateQuantityRegex=function(query,doc,args,batchCallback){
  389. var needupdate = false;
  390. if(args.editingText==null){
  391. needupdate =true;
  392. }else {
  393. args.editingText = _.trim(args.editingText,/\r\n/);
  394. if(this.regexChecking(args.editingText)&&this.referenceChecking(args.editingText,args.row,doc)){
  395. needupdate = true;
  396. }
  397. }
  398. if(needupdate){
  399. this.commonUpdate("/quantity_detail/updateRegex",{query:query,doc:doc},args,batchCallback);
  400. }else {
  401. var sheet = subSpread.getActiveSheet();
  402. sheet.getCell(args.row,args.col).value(gljOprObj.detailData[args.row].regex);
  403. }
  404. };
  405. quantity_detail.prototype.isSummationUpdate=function (args,detailList,newval) {
  406. var query={
  407. ID:detailList[args.row].ID,
  408. projectID:detailList[args.row].projectID
  409. };
  410. var selected = projectObj.project.mainTree.selected;
  411. query.refreshQuantity=true;
  412. if(!selected.data.hasOwnProperty('isFromDetail')||selected.data.isFromDetail==0){
  413. hintBox.infoBox('操作确认', '确定要使用工程量明细替换原工程量吗?', 2, function () {
  414. summationUpdate(args,detailList,newval,query);
  415. }, function () {
  416. query.refreshQuantity=false;
  417. summationUpdate(args,detailList,newval,query);
  418. },['确定','取消'],false);
  419. return;
  420. }
  421. summationUpdate(args,detailList,newval,query);
  422. function summationUpdate(args,detailList,newval,query) {
  423. if(detailList[args.row].hasOwnProperty('rationID')){
  424. query.rationID=detailList[args.row].rationID;
  425. }else {
  426. query.billID = detailList[args.row].billID
  427. }
  428. var doc={
  429. isSummation:newval
  430. };
  431. projectObj.project.quantity_detail.normalUpdate(query,doc);
  432. }
  433. };
  434. quantity_detail.prototype.commonUpdate = function (url,postData,args,batchCallback) {
  435. var me = this;
  436. $.bootstrapLoading.start();
  437. var callback = function (data) {
  438. $.bootstrapLoading.end();
  439. me.refreshAfterUpdate(data,batchCallback);
  440. if(batchCallback){
  441. batchCallback(args)
  442. }else if(data.node){
  443. me.refreshRationOrBillNodes(data.node);
  444. }
  445. }
  446. CommonAjax.post(url,postData,callback,function () {
  447. $.bootstrapLoading.end();
  448. });
  449. };
  450. quantity_detail.prototype.normalUpdate=function(query,doc,args,batchCallback){
  451. var url = "/quantity_detail/update";
  452. this.commonUpdate(url,{query:query,doc:doc},args,batchCallback);
  453. };
  454. quantity_detail.prototype.regexChecking=function(text){
  455. var regex=/^[0-9Cc\+\-\*\^/\(\)\.]*$/g;
  456. if(!regex.test(text)){
  457. alert("输入了非法字符,请重新输入!")
  458. return false;
  459. }else {
  460. return true;
  461. }
  462. };
  463. quantity_detail.prototype.referenceChecking=function (text,row,doc) {
  464. text = text.toUpperCase();
  465. //text= this.replaceSqr(text);
  466. var me = this;
  467. var refReg = /C\d+/g;
  468. var self ='C'+(row+1);
  469. var refList = text.match(refReg);
  470. var invalidate = _.includes(refList,self);
  471. var referenceIndexs = [];
  472. var indexOut = false;
  473. _.forEach(refList,function (item) {
  474. var ref_index = parseInt(item.substring(1));
  475. if(ref_index>gljOprObj.detailData.length){
  476. indexOut=true;
  477. return;
  478. }else {
  479. referenceIndexs.push(ref_index);
  480. }
  481. });
  482. if(indexOut){
  483. alert("引用有误,请重新输入!");
  484. return false;
  485. }
  486. referenceIndexs=_.uniq(referenceIndexs);
  487. doc.referenceIndexs = referenceIndexs;
  488. this.temList = referenceIndexs;
  489. invalidate=this.getAllReferenceList((row+1),referenceIndexs);
  490. if(invalidate){
  491. alert("计算式中产生了循环引用,请重新输入!");
  492. return false;
  493. }
  494. return true;
  495. };
  496. quantity_detail.prototype.getAllReferenceList=function(original,refList){
  497. var me =this;
  498. var invalidate=false;
  499. _.forEach(refList,function (item) {
  500. if(me.getReferenceList(item,original)){
  501. invalidate=true;
  502. }
  503. })
  504. return invalidate;
  505. };
  506. quantity_detail.prototype.getReferenceList=function(item,original) {
  507. var invalidate =false;
  508. if(gljOprObj.detailData.length>=item){
  509. var recode = gljOprObj.detailData[item - 1];
  510. if (recode.referenceIndexs.length > 0) {
  511. if(_.includes(recode.referenceIndexs,original)){
  512. invalidate = true;
  513. return invalidate;
  514. }
  515. this.temList = this.temList.concat(recode.referenceIndexs);
  516. _.forEach(recode.referenceIndex, function (item) {
  517. if(this.getReferenceList(item,original)){
  518. invalidate = true;
  519. }
  520. })
  521. }
  522. }
  523. return invalidate;
  524. };
  525. quantity_detail.prototype.replaceSqr = function(text) {
  526. var squarRegex = /\([^\^]+\)\^\d+/g;
  527. var sqararr = text.match(squarRegex);
  528. var squarRegex2 = /C[0-9]+\^\d+|[0-9]+([.]{1}[0-9]+){0,1}\^\d+/g; //匹配没有括号的
  529. var sqararr2=text.match(squarRegex2);
  530. if(sqararr){
  531. text=converSqrByArr(sqararr,text);
  532. }
  533. if(sqararr2){
  534. text=converSqrByArr(sqararr2,text);
  535. }
  536. return text;
  537. };
  538. quantity_detail.prototype.converSqrByArr = function (sqararr,text) {
  539. var temp = text;
  540. sqararr.forEach(function (item) {
  541. var arr = item.split('\^');
  542. var y = parseInt(arr[1]);
  543. var x_arr = [];
  544. for (var i = 0; i < y; i++) {
  545. x_arr.push(arr[0]);
  546. }
  547. var temStr = x_arr.join('*');
  548. temp = temp.replace(item, temStr);
  549. });
  550. return temp;
  551. };
  552. quantity_detail.prototype.getDetailByRationID = function (rationID) {
  553. return _.filter(this.datas,{'rationID':rationID});
  554. };
  555. quantity_detail.prototype.getDetailByBillID = function (billID) {
  556. return _.filter(this.datas,{'billID':billID});
  557. };
  558. quantity_detail.prototype.addDatasToList = function (datas) {
  559. let me = projectObj.project.quantity_detail;
  560. if(datas&&datas.length>0){
  561. if (me.datas && Array.isArray(me.datas)) {
  562. me.datas = me.datas.concat(datas);
  563. } else {
  564. me.datas = datas;
  565. }
  566. }
  567. };
  568. quantity_detail.prototype.deleteByRation = function(ration){
  569. var detail_list = this.datas;
  570. _.remove(detail_list,{'rationID':ration.ID});
  571. };
  572. quantity_detail.prototype.deleteByBills=function(deleteData){
  573. var detail_list = this.datas;
  574. var billIDList = [];
  575. for(var i=0;i<deleteData.length;i++){
  576. if(deleteData[i].type=='delete'){
  577. billIDList.push(deleteData[i].data.ID);
  578. }
  579. }
  580. var newList =_.filter(detail_list,(d)=>{
  581. return !_.includes(billIDList,d.billID);
  582. });
  583. if(newList!=undefined){
  584. this.datas = newList;
  585. }
  586. };
  587. quantity_detail.prototype.cleanQuantityDetail = function (node,needSave) {
  588. node =node?node:projectObj.project.mainTree.selected;
  589. let query={projectID:node.data.projectID};
  590. if(node.sourceType === project.Bills.getSourceType()){
  591. query.billID = node.data.ID;
  592. this.deleteByBills([{type:'delete',data:node.data}]);
  593. }else if(node.sourceType === project.Ration.getSourceType()){
  594. this.deleteByRation(node.data);
  595. query.rationID = node.data.ID;
  596. }
  597. if(needSave===true){
  598. query.refreshQuantity=false;
  599. CommonAjax.post("/quantity_detail/save",query);
  600. }
  601. gljOprObj.detailData=[];
  602. sheetCommonObj.showData(gljOprObj.detailSheet,gljOprObj.detailSetting,[]);
  603. };
  604. quantity_detail.prototype.quantityEditChecking = function(value,node,fieldName){
  605. var validate = true;
  606. if(fieldName=='quantity'){
  607. if(node.data.hasOwnProperty('isFromDetail')&&node.data.isFromDetail==1){
  608. var c = confirm('已有工程量明细,是否清空明细表,采用手工输入的表达式?')
  609. if(c){
  610. validate = true;
  611. }else {
  612. validate = false;
  613. }
  614. }
  615. }
  616. return validate;
  617. };
  618. quantity_detail.prototype.autoTransformQuantity = function(value,node){//根据单位转换定额工程量
  619. let data = node.data;
  620. let option = optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS,'rationQuanACToRationUnit');
  621. if(node.data.quantityEXP=='GCLMXHJ'||node.data.quantityEXP.indexOf('QDL')!=-1||(option==true&&node.sourceType === project.Ration.getSourceType()&&data.unit)) {
  622. //还需加入判读是否转换,如果是来自工程量明细或者包含了表达式中包含了清单量的话,默认都进行转换
  623. let times = parseInt(data.unit);
  624. if (isNaN(times)) {
  625. times = 1
  626. }
  627. value = value / times;
  628. }
  629. /* let EXPString=node.data.quantityEXP+""; 这里现在都没用到了
  630. if(option==false&&node.sourceType === project.Ration.getSourceType()&&EXPString!='GCLMXHJ'&&EXPString.indexOf("QDL")==-1&&data.unit){//勾选不根据单位转换工程量,且定额是手输的,
  631. let times = parseInt(data.unit);
  632. if(!isNaN(times)){///如果定额单位可以做转换
  633. if(node.data.quantityEXP){
  634. if(isNum(EXPString)){//如果表达式中只是数字
  635. node.data.quantityEXP = EXPString+" * "+times;
  636. }else {//如果表达式是一个计算式,则要加一个括号
  637. node.data.quantityEXP = "("+EXPString+") * "+times;
  638. }
  639. }
  640. if(node.data.contain!=0){
  641. let billQuantity = scMathUtil.roundForObj(node.parent.data.quantity,getDecimal("quantity",node.parent));
  642. let temValue = scMathUtil.roundForObj(value*times,getDecimal("quantity",node));
  643. billQuantity!=0?node.data.contain = scMathUtil.roundForObj(temValue/billQuantity,getDecimal("process")):'';
  644. }
  645. }
  646. }*/
  647. return value;
  648. };
  649. quantity_detail.prototype.reverseQuantity = function (value,node) {//根据单位反向运算出工程量
  650. let data = node.data;
  651. if(node.sourceType === project.Ration.getSourceType()&&data.unit){
  652. let times = parseInt(data.unit);
  653. if (isNaN(times)) {
  654. times = 1;
  655. }
  656. value = value * times;
  657. }
  658. return value
  659. };
  660. quantity_detail.prototype.editMainTreeNodeQuantity=function (value,node,fieldName,editingText) {
  661. var me = this;
  662. if(isNaN(value)){
  663. alert("当前输入的数据类型不正确,请重新输入。");
  664. projectObj.mainController.refreshTreeNode([node]);
  665. }else {
  666. value=value?value:0;
  667. if(node.data.hasOwnProperty('isFromDetail')&&node.data.isFromDetail==1){
  668. hintBox.infoBox('操作确认', '已有工程量明细,是否清空明细表,采用手工输入的表达式?', 2, function () {
  669. node.data.isFromDetail=0;
  670. me.updateMainTreeNodeQuantity(value,node,editingText);
  671. }, function () {
  672. projectObj.mainController.refreshTreeNode([node]);
  673. },['确定','取消'],false);
  674. return;
  675. }
  676. me.updateMainTreeNodeQuantity(value,node,editingText);
  677. }
  678. };
  679. quantity_detail.prototype.updateMainTreeNodeQuantity = function(value,node,editingText){
  680. let me = this;
  681. project.quantity_detail.cleanQuantityDetail(node,true);
  682. if(node.sourceType === project.Bills.getSourceType()){
  683. me.updateBillQuantity(value,node,null,editingText);
  684. }else {
  685. me.updateRationQuantity(value,node,null,editingText);
  686. }
  687. };
  688. quantity_detail.prototype.updateBillQuantity=function (value,node,quantityEXP,editingText) {
  689. let oldQuantityEXP = node.data.quantityEXP;
  690. node.data.quantityEXP = quantityEXP?quantityEXP:editingText;
  691. value = scMathUtil.roundForObj(value,getDecimal("quantity",node));
  692. let newQuantity = value+"";
  693. if(quantityEXP!="GCLMXHJ"&& oldQuantityEXP == node.data.quantityEXP && node.data.quantity == newQuantity){ //除了修改工程量明细表达式进来的操作,相当于什么都没改,不用做提交操作
  694. projectObj.mainController.refreshTreeNode([node]);//这里要再刷新一下,因为工程量要把手工输入的值刷新为转换后的值再显示
  695. return;
  696. }
  697. node.data.quantity = newQuantity;
  698. let needUpdateChildren = [];//需更新的子定额
  699. let gljNodes=[];//当定额工程量改变时需刷新的子工料机
  700. if(node.children.length>0){//如果有子项
  701. for(let rationNode of node.children){
  702. let EXPString = rationNode.data.quantityEXP+"";
  703. if(EXPString.indexOf("QDL")!=-1){//包含了清单量的定额才要再重新计算
  704. let ration_value = quantityEditObj.evalQuantityExp(EXPString,rationNode);
  705. if(ration_value !== 'evalError'){
  706. let times = parseInt(rationNode.data.unit);
  707. if(isNaN(times)){
  708. times = 1;
  709. }
  710. rationNode.data.quantity = scMathUtil.roundForObj(ration_value / times,getDecimal("quantity",rationNode));
  711. rationNode.data.contain = value?scMathUtil.roundForObj(rationNode.data.quantity/value,getDecimal("process")):0;
  712. rationNode.changed = true;
  713. needUpdateChildren.push(rationNode);
  714. if (rationNode.children.length>0){//如果有子工料机
  715. gljNodes = gljNodes.concat(rationNode.children);
  716. }
  717. }
  718. }else {//如果不是通过清单量计算得来的定额工程量,则重新计算含量
  719. let tem_contain=0;
  720. if(value&&value!=0){
  721. let children_quantity = scMathUtil.roundForObj(rationNode.data.quantity,getDecimal("quantity",rationNode));
  722. // children_quantity = scMathUtil.roundForObj(this.reverseQuantity(children_quantity,rationNode),getDecimal("quantity",rationNode)); 原先是要反算的,现在改成不用反算了
  723. tem_contain =scMathUtil.roundForObj(children_quantity/value,getDecimal("process"));
  724. }
  725. rationNode.data.contain = tem_contain;
  726. rationNode.changed = true;
  727. needUpdateChildren.push(rationNode);
  728. }
  729. }
  730. }
  731. if(needUpdateChildren.length>0){//清单下的定额工程量发生了改变
  732. node.changed = true;//本身发生了改变,需要存储。
  733. /*project.calcProgram.calcNodesAndSave(needUpdateChildren, function () {
  734. project.projectGLJ.loadData();
  735. });*/
  736. project.calcProgram.calcNodesAndSave(needUpdateChildren, function () {
  737. project.projectGLJ.calcQuantity();
  738. OVER_HEIGHT.reCalcOverHeightFee();
  739. if(project.Bills.isFBFX(node)) { //判断是否属于分部分项工程 ,是的话才需要做计取安装费计算
  740. project.installation_fee.calcInstallationFee(function (isChange,rations) {
  741. if(isChange){
  742. project.calcProgram.calcNodesAndSave(rations,function () {
  743. project.projectGLJ.calcQuantity();
  744. });
  745. }
  746. });
  747. }
  748. });
  749. }else {
  750. node.changed = true;
  751. project.calcProgram.calcAndSave(node, function () {
  752. project.projectGLJ.calcQuantity();
  753. });
  754. }
  755. if(gljNodes.length>0){
  756. projectObj.mainController.refreshTreeNode(gljNodes);
  757. }
  758. gljOprObj.refreshView();
  759. };
  760. quantity_detail.prototype.updateRationQuantity=function(value,node,quantityEXP,editingText){
  761. let oldQuantityEXP = node.data.quantityEXP;
  762. node.data.quantityEXP = quantityEXP?quantityEXP:editingText;
  763. //value = scMathUtil.roundForObj(value,getDecimal("ration.quantity"));
  764. value = project.quantity_detail.autoTransformQuantity(value,node);//先转换再4舍5入
  765. value = scMathUtil.roundForObj(value,decimalObj.decimal("quantity",node));
  766. if( quantityEXP!="MBGCL"&& quantityEXP!="GCLMXHJ" && oldQuantityEXP == node.data.quantityEXP && node.data.quantity == value){ //除了修改工程量明细表达式和模析子目进来的操作,相当于什么都没改,不用做提交操作
  767. projectObj.mainController.refreshTreeNode([node]);//这里要再刷新一下,因为工程量要把手工输入的值刷新为转换后的值再显示
  768. return;
  769. }
  770. node.data.quantity=value;
  771. if(node.parent.data.quantity&&node.parent.data.quantity!=0&&node.parent.data.quantity!=""){
  772. var billQuantity = scMathUtil.roundForObj(node.parent.data.quantity,getDecimal("quantity",node.parent));
  773. node.data.contain = scMathUtil.roundForObj(value/billQuantity,getDecimal("process"));
  774. }else {
  775. node.data.contain=0;
  776. }
  777. node.changed = true;
  778. let rationNodes = mbzm_obj.updateReferenceRationNodeQuantity(node);
  779. rationNodes.push(node);
  780. project.calcProgram.calcNodesAndSave(rationNodes, function () {
  781. project.projectGLJ.calcQuantity();
  782. let calcInstall = false;
  783. for(let t of rationNodes){
  784. if(project.Bills.isFBFX(t)){//判断是否属于分部分项工程 ,是的话才需要做计取安装费计算
  785. calcInstall = true;
  786. break;
  787. }
  788. }
  789. if(calcInstall == true) {
  790. project.installation_fee.calcInstallationFee(function (isChange,rations) {
  791. if(isChange){
  792. project.calcProgram.calcNodesAndSave(rations);
  793. }
  794. });
  795. }
  796. OVER_HEIGHT.reCalcOverHeightFee();
  797. });
  798. let childrenNodes = [];
  799. for(let r of rationNodes){
  800. childrenNodes = childrenNodes.concat(r.children)
  801. }
  802. if(childrenNodes.length > 0) projectObj.mainController.refreshTreeNode(childrenNodes);//刷新子工料机总消耗量
  803. mbzm_obj.refreshSheetData();
  804. gljOprObj.refreshView();
  805. };
  806. quantity_detail.prototype.getDecimal=function (node) {
  807. var decimal = 3;
  808. if(node.sourceType === project.Bills.getSourceType()){
  809. decimal = billsQuanDecimal.decimal(node.data.unit);
  810. }else {
  811. decimal = decimalObj.ration.quantity
  812. }
  813. return decimal;
  814. };
  815. return new quantity_detail(project);
  816. }
  817. };