quantity_detail.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. };
  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(this.checkAndCalcResult(args.editingText,args.row,doc) === false){
  358. gljOprObj.showQuantityDetailData();
  359. return;
  360. }
  361. if(recode.hasOwnProperty('rationID')){
  362. query.rationID=recode.rationID;
  363. }else {
  364. query.billID = recode.billID
  365. }
  366. query.refreshQuantity=true;
  367. if(!selected.data.hasOwnProperty('isFromDetail')||selected.data.isFromDetail==0){
  368. if(!args.replace){//为了批量粘贴时不重复提示,普通编辑时不受影响
  369. hintBox.infoBox('操作确认', '确定要使用工程量明细替换原工程量吗?', 2, function () {
  370. args.replace = true;
  371. query.index = args.row;
  372. me.updateQuantityRegex(query,doc,args,batchCallback)
  373. }, function () {
  374. me.cleanQuantityDetail(selected,true);
  375. },['确定','取消'],false);
  376. return;
  377. }
  378. }
  379. query.index = args.row;
  380. me.updateQuantityRegex(query,doc,args,batchCallback);
  381. }else {
  382. me.normalUpdate(query,doc,args,batchCallback);
  383. }
  384. };
  385. quantity_detail.prototype.updateQuantityRegex=function(query,doc,args,batchCallback){
  386. var needupdate = false;
  387. if(args.editingText==null){
  388. needupdate =true;
  389. }else {
  390. args.editingText = _.trim(args.editingText,/\r\n/);
  391. if(this.regexChecking(args.editingText)&&this.referenceChecking(args.editingText,args.row,doc)){
  392. needupdate = true;
  393. }
  394. }
  395. if(needupdate){
  396. this.commonUpdate("/quantity_detail/updateRegex",{query:query,doc:doc},args,batchCallback);
  397. }else {
  398. var sheet = subSpread.getActiveSheet();
  399. sheet.getCell(args.row,args.col).value(gljOprObj.detailData[args.row].regex);
  400. }
  401. };
  402. quantity_detail.prototype.isSummationUpdate=function (args,detailList,newval) {
  403. var query={
  404. ID:detailList[args.row].ID,
  405. projectID:detailList[args.row].projectID
  406. };
  407. var selected = projectObj.project.mainTree.selected;
  408. query.refreshQuantity=true;
  409. if(!selected.data.hasOwnProperty('isFromDetail')||selected.data.isFromDetail==0){
  410. hintBox.infoBox('操作确认', '确定要使用工程量明细替换原工程量吗?', 2, function () {
  411. summationUpdate(args,detailList,newval,query);
  412. }, function () {
  413. query.refreshQuantity=false;
  414. summationUpdate(args,detailList,newval,query);
  415. },['确定','取消'],false);
  416. return;
  417. }
  418. summationUpdate(args,detailList,newval,query);
  419. function summationUpdate(args,detailList,newval,query) {
  420. if(detailList[args.row].hasOwnProperty('rationID')){
  421. query.rationID=detailList[args.row].rationID;
  422. }else {
  423. query.billID = detailList[args.row].billID
  424. }
  425. var doc={
  426. isSummation:newval
  427. };
  428. projectObj.project.quantity_detail.normalUpdate(query,doc);
  429. }
  430. };
  431. quantity_detail.prototype.commonUpdate = function (url,postData,args,batchCallback) {
  432. var me = this;
  433. $.bootstrapLoading.start();
  434. var callback = function (data) {
  435. $.bootstrapLoading.end();
  436. me.refreshAfterUpdate(data,batchCallback);
  437. if(batchCallback){
  438. batchCallback(args)
  439. }else if(data.node){
  440. me.refreshRationOrBillNodes(data.node);
  441. }
  442. }
  443. CommonAjax.post(url,postData,callback,function () {
  444. $.bootstrapLoading.end();
  445. });
  446. };
  447. quantity_detail.prototype.normalUpdate=function(query,doc,args,batchCallback){
  448. var url = "/quantity_detail/update";
  449. this.commonUpdate(url,{query:query,doc:doc},args,batchCallback);
  450. };
  451. quantity_detail.prototype.regexChecking=function(text){
  452. var regex=/^[0-9Cc\+\-\*\^/\(\)\.]*$/g;
  453. if(!regex.test(text)){
  454. alert("输入了非法字符,请重新输入!")
  455. return false;
  456. }else {
  457. return true;
  458. }
  459. };
  460. quantity_detail.prototype.referenceChecking=function (text,row,doc) {
  461. text = text.toUpperCase();
  462. //text= this.replaceSqr(text);
  463. var me = this;
  464. var refReg = /C\d+/g;
  465. var self ='C'+(row+1);
  466. var refList = text.match(refReg);
  467. var invalidate = _.includes(refList,self);
  468. var referenceIndexs = [];
  469. var indexOut = false;
  470. _.forEach(refList,function (item) {
  471. var ref_index = parseInt(item.substring(1));
  472. if(ref_index>gljOprObj.detailData.length){
  473. indexOut=true;
  474. return;
  475. }else {
  476. referenceIndexs.push(ref_index);
  477. }
  478. });
  479. if(indexOut){
  480. alert("引用有误,请重新输入!");
  481. return false;
  482. }
  483. referenceIndexs=_.uniq(referenceIndexs);
  484. doc.referenceIndexs = referenceIndexs;
  485. this.temList = referenceIndexs;
  486. invalidate=this.getAllReferenceList((row+1),referenceIndexs);
  487. if(invalidate){
  488. alert("计算式中产生了循环引用,请重新输入!");
  489. return false;
  490. }
  491. return true;
  492. };
  493. quantity_detail.prototype.getAllReferenceList=function(original,refList){
  494. var me =this;
  495. var invalidate=false;
  496. _.forEach(refList,function (item) {
  497. if(me.getReferenceList(item,original)){
  498. invalidate=true;
  499. }
  500. })
  501. return invalidate;
  502. };
  503. quantity_detail.prototype.getReferenceList=function(item,original) {
  504. var invalidate =false;
  505. if(gljOprObj.detailData.length>=item){
  506. var recode = gljOprObj.detailData[item - 1];
  507. if (recode.referenceIndexs.length > 0) {
  508. if(_.includes(recode.referenceIndexs,original)){
  509. invalidate = true;
  510. return invalidate;
  511. }
  512. this.temList = this.temList.concat(recode.referenceIndexs);
  513. _.forEach(recode.referenceIndex, function (item) {
  514. if(this.getReferenceList(item,original)){
  515. invalidate = true;
  516. }
  517. })
  518. }
  519. }
  520. return invalidate;
  521. };
  522. quantity_detail.prototype.replaceSqr = function(text) {
  523. var squarRegex = /\([^\^]+\)\^\d+/g;
  524. var sqararr = text.match(squarRegex);
  525. var squarRegex2 = /C[0-9]+\^\d+|[0-9]+([.]{1}[0-9]+){0,1}\^\d+/g; //匹配没有括号的
  526. var sqararr2=text.match(squarRegex2);
  527. if(sqararr){
  528. text=converSqrByArr(sqararr,text);
  529. }
  530. if(sqararr2){
  531. text=converSqrByArr(sqararr2,text);
  532. }
  533. return text;
  534. };
  535. quantity_detail.prototype.converSqrByArr = function (sqararr,text) {
  536. var temp = text;
  537. sqararr.forEach(function (item) {
  538. var arr = item.split('\^');
  539. var y = parseInt(arr[1]);
  540. var x_arr = [];
  541. for (var i = 0; i < y; i++) {
  542. x_arr.push(arr[0]);
  543. }
  544. var temStr = x_arr.join('*');
  545. temp = temp.replace(item, temStr);
  546. });
  547. return temp;
  548. };
  549. quantity_detail.prototype.getDetailByRationID = function (rationID) {
  550. return _.filter(this.datas,{'rationID':rationID});
  551. };
  552. quantity_detail.prototype.getDetailByBillID = function (billID) {
  553. return _.filter(this.datas,{'billID':billID});
  554. };
  555. quantity_detail.prototype.addDatasToList = function (datas) {
  556. let me = projectObj.project.quantity_detail;
  557. if(datas&&datas.length>0){
  558. if (me.datas && Array.isArray(me.datas)) {
  559. me.datas = me.datas.concat(datas);
  560. } else {
  561. me.datas = datas;
  562. }
  563. }
  564. };
  565. quantity_detail.prototype.deleteByRation = function(ration){
  566. var detail_list = this.datas;
  567. _.remove(detail_list,{'rationID':ration.ID});
  568. };
  569. quantity_detail.prototype.deleteByBills=function(deleteData){
  570. var detail_list = this.datas;
  571. var billIDList = [];
  572. for(var i=0;i<deleteData.length;i++){
  573. if(deleteData[i].type=='delete'){
  574. billIDList.push(deleteData[i].data.ID);
  575. }
  576. }
  577. var newList =_.filter(detail_list,(d)=>{
  578. return !_.includes(billIDList,d.billID);
  579. });
  580. if(newList!=undefined){
  581. this.datas = newList;
  582. }
  583. };
  584. quantity_detail.prototype.cleanQuantityDetail = function (node,needSave) {
  585. node =node?node:projectObj.project.mainTree.selected;
  586. let query={projectID:node.data.projectID};
  587. if(node.sourceType === project.Bills.getSourceType()){
  588. query.billID = node.data.ID;
  589. this.deleteByBills([{type:'delete',data:node.data}]);
  590. }else if(node.sourceType === project.Ration.getSourceType()){
  591. this.deleteByRation(node.data);
  592. query.rationID = node.data.ID;
  593. }
  594. if(needSave===true){
  595. query.refreshQuantity=false;
  596. CommonAjax.post("/quantity_detail/save",query);
  597. }
  598. gljOprObj.detailData=[];
  599. sheetCommonObj.showData(gljOprObj.detailSheet,gljOprObj.detailSetting,[]);
  600. };
  601. quantity_detail.prototype.quantityEditChecking = function(value,node,fieldName){
  602. var validate = true;
  603. if(fieldName=='quantity'){
  604. if(node.data.hasOwnProperty('isFromDetail')&&node.data.isFromDetail==1){
  605. var c = confirm('已有工程量明细,是否清空明细表,采用手工输入的表达式?')
  606. if(c){
  607. validate = true;
  608. }else {
  609. validate = false;
  610. }
  611. }
  612. }
  613. return validate;
  614. };
  615. quantity_detail.prototype.autoTransformQuantity = function(value,node){//根据单位转换定额工程量
  616. let data = node.data;
  617. let option = optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS,'rationQuanACToRationUnit');
  618. if(node.data.quantityEXP=='GCLMXHJ'||(option==true&&node.sourceType === project.Ration.getSourceType()&&data.unit)) {//还需加入判读是否转换,如果是来自工程量明细的话,默认都进行转换
  619. let times = parseInt(data.unit);
  620. if (isNaN(times)) {
  621. times = 1
  622. }
  623. value = value / times;
  624. }
  625. let EXPString=node.data.quantityEXP+"";
  626. if(option==false&&node.sourceType === project.Ration.getSourceType()&&EXPString!='GCLMXHJ'&&EXPString.indexOf("QDL")==-1&&data.unit){//勾选不根据单位转换工程量,且定额是手输的,
  627. let times = parseInt(data.unit);
  628. if(!isNaN(times)){///如果定额单位可以做转换
  629. if(node.data.quantityEXP){
  630. if(isNum(EXPString)){//如果表达式中只是数字
  631. node.data.quantityEXP = EXPString+" * "+times;
  632. }else {//如果表达式是一个计算式,则要加一个括号
  633. node.data.quantityEXP = "("+EXPString+") * "+times;
  634. }
  635. }
  636. if(node.data.contain!=0){
  637. let billQuantity = scMathUtil.roundForObj(node.parent.data.quantity,getDecimal("quantity",node.parent));
  638. let temValue = scMathUtil.roundForObj(value*times,getDecimal("quantity",node));
  639. billQuantity!=0?node.data.contain = scMathUtil.roundForObj(temValue/billQuantity,getDecimal("process")):'';
  640. }
  641. }
  642. }
  643. return value;
  644. };
  645. quantity_detail.prototype.reverseQuantity = function (value,node) {//根据单位反向运算出工程量
  646. let data = node.data;
  647. if(node.sourceType === project.Ration.getSourceType()&&data.unit){
  648. let times = parseInt(data.unit);
  649. if (isNaN(times)) {
  650. times = 1;
  651. }
  652. value = value * times;
  653. }
  654. return value
  655. };
  656. quantity_detail.prototype.editMainTreeNodeQuantity=function (value,node,fieldName,editingText) {
  657. var me = this;
  658. if(isNaN(value)){
  659. alert("当前输入的数据类型不正确,请重新输入。");
  660. projectObj.mainController.refreshTreeNode([node]);
  661. }else {
  662. value=value?value:0;
  663. if(node.data.hasOwnProperty('isFromDetail')&&node.data.isFromDetail==1){
  664. hintBox.infoBox('操作确认', '已有工程量明细,是否清空明细表,采用手工输入的表达式?', 2, function () {
  665. node.data.isFromDetail=0;
  666. me.updateMainTreeNodeQuantity(value,node,editingText);
  667. }, function () {
  668. projectObj.mainController.refreshTreeNode([node]);
  669. },['确定','取消'],false);
  670. return;
  671. }
  672. me.updateMainTreeNodeQuantity(value,node,editingText);
  673. }
  674. };
  675. quantity_detail.prototype.updateMainTreeNodeQuantity = function(value,node,editingText){
  676. let me = this;
  677. project.quantity_detail.cleanQuantityDetail(node,true);
  678. if(node.sourceType === project.Bills.getSourceType()){
  679. me.updateBillQuantity(value,node,null,editingText);
  680. }else {
  681. me.updateRationQuantity(value,node,null,editingText);
  682. }
  683. };
  684. quantity_detail.prototype.updateBillQuantity=function (value,node,quantityEXP,editingText) {
  685. node.data.quantityEXP = quantityEXP?quantityEXP:editingText;
  686. value = scMathUtil.roundForObj(value,getDecimal("quantity",node));
  687. node.data.quantity = value+"";
  688. console.log(value);
  689. let needUpdateChildren = [];//需更新的子定额
  690. let gljNodes=[];//当定额工程量改变时需刷新的子工料机
  691. if(node.children.length>0){//如果有子项
  692. for(let rationNode of node.children){
  693. let EXPString = rationNode.data.quantityEXP+"";
  694. if(EXPString.indexOf("QDL")!=-1){
  695. if(EXPString=="QDL"){//定额的工程量是直接通过清单量填进来的;
  696. let times = parseInt(rationNode.data.unit);
  697. if(isNaN(times)){
  698. times = 1;
  699. }
  700. rationNode.data.quantity = scMathUtil.roundForObj(value / times,getDecimal("quantity",rationNode));
  701. rationNode.data.contain = scMathUtil.roundForObj(rationNode.data.quantity/value,getDecimal("process"));
  702. }else {//如果定额的工程量是通过计算出来的,则应该重新计算。
  703. let tem_contain = scMathUtil.roundForObj(rationNode.data.contain,getDecimal("process"));
  704. let tem_quantity = scMathUtil.roundForObj(value*tem_contain,getDecimal("quantity",rationNode)); //this.autoTransformQuantity(tem_quantity,rationNode);
  705. rationNode.data.quantity = tem_quantity;
  706. }
  707. rationNode.changed = true;
  708. needUpdateChildren.push(rationNode);
  709. if (rationNode.children.length>0){//如果有子工料机
  710. gljNodes = gljNodes.concat(rationNode.children);
  711. }
  712. }else {
  713. let tem_contain=0;
  714. if(value&&value!=0){
  715. let children_quantity = scMathUtil.roundForObj(rationNode.data.quantity,getDecimal("quantity",rationNode));
  716. // children_quantity = scMathUtil.roundForObj(this.reverseQuantity(children_quantity,rationNode),getDecimal("quantity",rationNode)); 原先是要反算的,现在改成不用反算了
  717. tem_contain =scMathUtil.roundForObj(children_quantity/value,getDecimal("process"));
  718. }
  719. rationNode.data.contain = tem_contain;
  720. rationNode.changed = true;
  721. needUpdateChildren.push(rationNode);
  722. }
  723. }
  724. }
  725. if(needUpdateChildren.length>0){//清单下的定额工程量发生了改变
  726. node.changed = true;//本身发生了改变,需要存储。
  727. /*project.calcProgram.calcNodesAndSave(needUpdateChildren, function () {
  728. project.projectGLJ.loadData();
  729. });*/
  730. project.calcProgram.calcNodesAndSave(needUpdateChildren, function () {
  731. project.projectGLJ.calcQuantity();
  732. if(project.Bills.isFBFX(node)) { //判断是否属于分部分项工程 ,是的话才需要做计取安装费计算
  733. project.installation_fee.calcInstallationFee(function (isChange,rations) {
  734. if(isChange){
  735. project.calcProgram.calcNodesAndSave(rations,function () {
  736. project.projectGLJ.calcQuantity();
  737. });
  738. }
  739. });
  740. }
  741. });
  742. }else {
  743. node.changed = true;
  744. project.calcProgram.calcAndSave(node, function () {
  745. project.projectGLJ.calcQuantity();
  746. });
  747. }
  748. if(gljNodes.length>0){
  749. projectObj.mainController.refreshTreeNode(gljNodes);
  750. }
  751. gljOprObj.refreshView();
  752. };
  753. quantity_detail.prototype.updateRationQuantity=function(value,node,quantityEXP,editingText){
  754. node.data.quantityEXP = quantityEXP?quantityEXP:editingText;
  755. value = scMathUtil.roundForObj(value,getDecimal("ration.quantity"));
  756. value = project.quantity_detail.autoTransformQuantity(value,node);
  757. value = scMathUtil.roundForObj(value,decimalObj.decimal("quantity",node))
  758. node.data.quantity=value;
  759. if(node.parent.data.quantity&&node.parent.data.quantity!=0&&node.parent.data.quantity!=""){
  760. var billQuantity = scMathUtil.roundForObj(node.parent.data.quantity,getDecimal("quantity",node.parent));
  761. node.data.contain = scMathUtil.roundForObj(value/billQuantity,getDecimal("process"));
  762. }else {
  763. node.data.contain=0;
  764. }
  765. node.changed = true;
  766. project.calcProgram.calcAndSave(node, function () {
  767. project.projectGLJ.calcQuantity();
  768. if(project.Bills.isFBFX(node)) { //判断是否属于分部分项工程 ,是的话才需要做计取安装费计算
  769. project.installation_fee.calcInstallationFee(function (isChange,rations) {
  770. if(isChange){
  771. project.calcProgram.calcNodesAndSave(rations);
  772. }
  773. });
  774. }
  775. });
  776. projectObj.mainController.refreshTreeNode(node.children);//刷新子工料机总消耗量
  777. gljOprObj.refreshView();
  778. };
  779. quantity_detail.prototype.getDecimal=function (node) {
  780. var decimal = 3;
  781. if(node.sourceType === project.Bills.getSourceType()){
  782. decimal = billsQuanDecimal.decimal(node.data.unit);
  783. }else {
  784. decimal = decimalObj.ration.quantity
  785. }
  786. return decimal;
  787. };
  788. return new quantity_detail(project);
  789. }
  790. };