quantity_detail.js 39 KB

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