glj_calculate_facade.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /**
  2. * Created by chen on 2017/7/12.
  3. */
  4. let mongoose = require('mongoose');
  5. let _=require("lodash");
  6. let ration_glj = mongoose.model('ration_glj');
  7. let ration = mongoose.model('ration');
  8. let ration_coe = mongoose.model('ration_coe');
  9. const ProjectModel = require('../../pm/models/project_model').project;
  10. let mixRatioModel = mongoose.model('mix_ratio');
  11. let std_ration_lib_ration_items = mongoose.model('std_ration_lib_ration_items');
  12. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  13. let glj_type_util = require('../../../public/cache/std_glj_type_util');
  14. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  15. let decimal_facade = require('../../main/facade/decimal_facade');
  16. let gljUtil = require('../../../public/gljUtil');
  17. module.exports={
  18. calculateQuantity:calculateQuantity,
  19. getGLJTypeByID:getGLJTypeByID
  20. }
  21. //辅助定额调整、替换工料机、标准附注条件调整、添加工料机、自定义消耗量(包括删除工料机)、自定义乘系数、市场单价调整
  22. let stateSeq ={
  23. ass:1,
  24. replace:2,
  25. coe:3,
  26. add:4,
  27. cusQuantity:5,
  28. cusCoe:6,
  29. adjMak:7
  30. };
  31. //自定义乘系数与定额工料机类型映射表
  32. let coeTypeMap = {
  33. "人工":1,
  34. "材料":2,
  35. "机械":3,
  36. "施工机具":3,
  37. "主材":4,
  38. "设备":5
  39. };
  40. async function calculateQuantity(query,noNeedCal=null,refreshRationName = false){
  41. try {
  42. let result ={
  43. glj_result:[],
  44. rationID:query.rationID
  45. };
  46. let impactRation = await ration.findOne({ID:query.rationID,projectID:query.projectID});
  47. let gljList = await ration_glj.find(query);//{projectID:query.projectID,rationID:query.rationID}
  48. let coeList = await ration_coe.find({projectID:query.projectID,rationID:query.rationID}).sort('seq').exec();
  49. let assList=[], assRation = null, adjustState=[],mixRatioMap = {};
  50. if(!impactRation){//如果定额不存在或者已删除,返回空
  51. return null;
  52. }
  53. if(impactRation._doc.hasOwnProperty("rationAssList")&&impactRation.rationAssList.length>0){
  54. for(let i=0;i<impactRation.rationAssList.length;i++){
  55. let times = calculateTimes(impactRation.rationAssList[i]);
  56. if(times!=0){
  57. assRation = await std_ration_lib_ration_items.findOne({rationRepId:impactRation.libID,code:impactRation.rationAssList[i].assistCode});
  58. assList.push({times:times,assRation:assRation});
  59. adjustState.push({index:stateSeq.ass,content:impactRation.rationAssList[i].name+" "+impactRation.rationAssList[i].actualValue+" : +"+impactRation.rationAssList[i].assistCode+"x"+times});
  60. }
  61. }
  62. }
  63. for(let glj of gljList){//先把混凝土,砂浆,配合比有自定义消耗的挑出来
  64. if(gljUtil.isConcreteType(glj.type)&& !noCustomQuantiyt(glj)) await getMixRatioMap(glj,gljList,coeList,assList,mixRatioMap);
  65. }
  66. gljList = sortRationGLJ(gljList);
  67. for(let i =0;i<gljList.length;i++ ){
  68. let r = await calculateQuantityPerGLJ(gljList[i],gljList,coeList,assList,adjustState,mixRatioMap,noNeedCal);
  69. result.glj_result.push(r);
  70. }
  71. if(noNeedCal==null){
  72. await ration_glj.bulkWrite(generateUpdateTasks(result.glj_result));
  73. }
  74. adjustState= _.sortByOrder(adjustState, ['index'], ['asc']);
  75. adjustState=_.map(adjustState, _.property('content'));
  76. let adjustStateString = adjustState.join(';');
  77. let setData = {adjustState:adjustStateString};
  78. if(refreshRationName == true){//需要更新定额名称
  79. let newName = generateRationName(impactRation,gljList);
  80. setData.name = newName;
  81. result.rationName = newName;
  82. }
  83. await ration.update({projectID:query.projectID,ID:query.rationID,deleteInfo: null},setData);
  84. result.adjustState=adjustStateString;
  85. return result;
  86. }catch (err){
  87. console.log(err);
  88. throw err;
  89. }
  90. }
  91. function generateRationName(ration,gljList) {
  92. let caption = ration.caption ? ration.caption:ration.name;
  93. if(ration.rationAssList && ration.rationAssList.length > 0){
  94. let ass = ration.rationAssList[0];
  95. if(ass.actualValue != null && ass.actualValue != undefined ){
  96. caption = caption.replace('%s',ass.actualValue);
  97. }
  98. }
  99. let reNameList = [];
  100. for(let g of gljList){
  101. //glj._doc.createType=='replace'&&glj.rcode!=glj.code
  102. if(g.createType=='replace'&&g.rcode!=g.code){ //是替换工料机
  103. let reName = g.name;
  104. if(!_.isEmpty(g.specs)) reName = reName + ' '+g.specs;
  105. reNameList.push(reName);
  106. }
  107. }
  108. if(reNameList.length > 0){
  109. let reNameString = reNameList.join(" ");
  110. caption = caption + " 换为【"+ reNameString + "】";
  111. }
  112. return caption;
  113. }
  114. function generateUpdateTasks(result) {
  115. let tasks = [];
  116. for(let i =0;i<result.length;i++){
  117. let task= {
  118. updateOne: {
  119. filter: result[i].query,
  120. update: result[i].doc
  121. }
  122. }
  123. tasks.push(task);
  124. }
  125. return tasks;
  126. }
  127. function sortRationGLJ(list) {
  128. list = _.sortByAll(list, [function (item) {
  129. return gljUtil.getMainType(item.type);
  130. }, "code"])
  131. return list;
  132. }
  133. async function calcWhenNoCustomQuantiyt(decimal,glj,gljList,coeList,assList) {
  134. let quantity = glj.rationItemQuantity ? scMathUtil.roundTo(parseFloat(glj.rationItemQuantity),-decimal):0;
  135. quantity =scMathUtil.roundTo(await calculateAss(quantity,assList,glj),-decimal);
  136. quantity = calculateQuantityByCoes(quantity,coeList,glj,gljList,decimal);
  137. return quantity;
  138. }
  139. async function calculateQuantityPerGLJ(glj,gljList,coeList,assList,adjustState,mixRatioMap,noNeedCal) {
  140. let decimalObject = await decimal_facade.getProjectDecimal(glj.projectID);
  141. let decimal = (decimalObject&&decimalObject.glj&&decimalObject.glj.quantity)?decimalObject.glj.quantity:3;
  142. let quantity = scMathUtil.roundTo(parseFloat(glj.quantity),-decimal);
  143. let result={
  144. query:{
  145. ID:glj.ID,
  146. projectID:glj.projectID
  147. },
  148. doc:{
  149. quantity: quantity
  150. }
  151. };
  152. try {
  153. if(noNeedCal==null){//计算顺序:辅助定额,附注条件,自定义消耗(如果有就不用前计算两项),自定义乘系数
  154. if(noCustomQuantiyt(glj)){
  155. quantity = await calcWhenNoCustomQuantiyt(decimal,glj,gljList,coeList,assList);
  156. let mIndex = gljUtil.getIndex(glj);
  157. if(mixRatioMap[mIndex]){//组成物
  158. quantity = calcQuantityForMix(quantity,mixRatioMap[mIndex],decimal);
  159. }
  160. }else {//对于有自定义消耗量的,并且是混凝土,浆砂,配合比 这三种类型,其组成物的消耗量要跟据自定义消耗量做相应的换算
  161. quantity = glj.customQuantity;
  162. result.doc.customQuantity = glj.customQuantity;
  163. }
  164. let customerCoe = _.last(coeList);
  165. if(customerCoe&&customerCoe.isAdjust==1){
  166. quantity = scMathUtil.roundToString(quantity,decimal);
  167. quantity = calculateQuantityByCustomerCoes(quantity,customerCoe,glj,decimal);
  168. }
  169. if(quantity < 0) quantity = 0;
  170. result.doc.quantity =scMathUtil.roundToString(quantity,decimal);
  171. //2019-01-03 需求修改中间过程的价格不参与计算
  172. //glj.quantity = quantity;//这里保存中间过程计算出来的消耗量,后面处理“+*”操作符时要用到
  173. }
  174. generateAdjustState(glj,coeList,adjustState,gljList,result.doc.quantity);
  175. return result;
  176. }catch (err){
  177. throw err;
  178. }
  179. }
  180. function calcQuantityForMix(quantity,plist,decimal) {
  181. for(let pglj of plist){
  182. let consumption = scMathUtil.roundForObj(pglj.consumption,decimal);
  183. let customQuantity = scMathUtil.roundForObj(pglj.customQuantity,decimal);
  184. let temQuantity = scMathUtil.roundForObj(consumption * pglj.assCoeQuantity,6);
  185. let pcustomQuantity = scMathUtil.roundForObj(consumption * customQuantity,6);
  186. quantity = scMathUtil.roundForObj(quantity - temQuantity,6);
  187. quantity = scMathUtil.roundForObj(quantity + pcustomQuantity,decimal);
  188. }
  189. return quantity;
  190. }
  191. async function getMixRatioMap(glj,gljList,coeList,assList,mixRatioMap) {//生成组成物对应的父工料机的映射表
  192. let decimalObject = await decimal_facade.getProjectDecimal(glj.projectID);
  193. let decimal = (decimalObject&&decimalObject.glj&&decimalObject.glj.quantity)?decimalObject.glj.quantity:3;
  194. let assCoeQuantity = await calcWhenNoCustomQuantiyt(decimal,glj,gljList,coeList,assList);//计算要用父工料机经过辅助子目换算后的量
  195. let unitPriceFileId = await ProjectModel.getUnitPriceFileId(glj.projectID);
  196. let connect_key = gljUtil.getIndex(glj);
  197. let mixList = await mixRatioModel.find({"unit_price_file_id":unitPriceFileId,'connect_key':connect_key})
  198. for(let m of mixList){
  199. let mkey = gljUtil.getIndex(m);
  200. if(mixRatioMap[mkey]){
  201. mixRatioMap[mkey].push({customQuantity:glj.customQuantity,assCoeQuantity:assCoeQuantity,consumption:m.consumption});
  202. }else {
  203. mixRatioMap[mkey] = [{customQuantity:glj.customQuantity,assCoeQuantity:assCoeQuantity,consumption:m.consumption}];
  204. }
  205. }
  206. }
  207. function noCustomQuantiyt(glj) {//是否有自定义消耗量
  208. return !glj._doc.hasOwnProperty('customQuantity')||glj.customQuantity==null||glj.customQuantity==""
  209. }
  210. async function calculateAss(quantity,assList,glj) {
  211. for(let i=0;i<assList.length;i++){
  212. if(assList[i].assRation){
  213. let assglj = null;
  214. for(let aglj of assList[i].assRation.rationGljList){
  215. if(glj.createType == 'replace'){//如果工料机是替换过的,要用原始的编码来匹配
  216. let std_glj = await std_glj_lib_gljList_model.findOne({'ID':aglj.gljId});
  217. if(glj.rcode == std_glj.code){
  218. assglj = aglj;
  219. break;
  220. }
  221. }else if(aglj.gljId == glj.GLJID){
  222. assglj = aglj;
  223. break;
  224. }
  225. }
  226. if(assglj){
  227. let calQuantity = assglj.consumeAmt*assList[i].times;
  228. quantity += calQuantity
  229. }
  230. }
  231. }
  232. return scMathUtil.roundTo(quantity,-6);
  233. }
  234. function generateAdjustState(glj,coeList,adjustState,gljList,quantity) {
  235. //替换工料机 and 添加工料机
  236. if(glj._doc.createType=='replace'&&glj.rcode!=glj.code){
  237. adjustState.push({index:stateSeq.replace,content:glj.rcode+'换'+glj.code});
  238. }else if(glj._doc.createType=='add'){
  239. let displayQuantity = quantity;
  240. if(glj._doc.hasOwnProperty('customQuantity')&&(glj.customQuantity != null||glj.customQuantity != '')){
  241. displayQuantity = glj.customQuantity;
  242. }
  243. displayQuantity = displayQuantity&&displayQuantity!=""?parseFloat(displayQuantity):0;
  244. adjustState.push({index:stateSeq.add,content:'添'+glj.code+'量'+ displayQuantity,type:"添"+glj.code});
  245. }
  246. // to do
  247. //标准附注条件调整 + 自定义乘系数
  248. if(_.last(gljList).ID == glj.ID){//最后一个工料机的时候才生成,生成一次就可以了
  249. for(let i=0;i<coeList.length;i++){
  250. if(coeList[i].isAdjust==1){
  251. if(i==coeList.length-1){
  252. adjustState.push({index:stateSeq.cusCoe,content:getContent(coeList[i].coes)});//自定义乘系数要去掉倍数为1的
  253. }else {
  254. if(coeList[i].select_code && coeList[i].select_code!=""){
  255. _.remove(adjustState,{'content':coeList[i].original_code+'换'+coeList[i].select_code});//去掉替换工料机自动生成的调整状态
  256. adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].original_code+'换'+coeList[i].select_code});
  257. }
  258. for(let c of coeList[i].coes){
  259. if(c.coeType=='单个工料机') _.remove(adjustState,{'type':"添"+c.gljCode});//如果是单个工料机子目换算类型自动添加的,去掉前面手动生成的调整状态
  260. if(c.coeType=='替换人材机') _.remove(adjustState,{'content':c.gljCode+'换'+c.replaceCode});//如果是替换人材机子目换算类型自动添加的,去掉前面手动生成的调整状态
  261. }
  262. if(coeList[i].content) adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].content});//coeList[i].content
  263. }
  264. }
  265. }
  266. }
  267. //自定义消耗量
  268. if(glj._doc.createType!='add'&&glj._doc.hasOwnProperty('customQuantity')){
  269. if(glj.customQuantity!==null&&glj.customQuantity!=""){
  270. adjustState.push({index:stateSeq.cusQuantity,content:glj.code+'量'+parseFloat(glj.customQuantity)});
  271. }
  272. }
  273. //市场单价调整
  274. if(glj._doc.hasOwnProperty('marketPriceAdjust')&&glj.marketPriceAdjust&&glj.marketPriceAdjust!=0){
  275. //0101005价66.00
  276. adjustState.push({index:stateSeq.adjMak,content:glj.code+'价'+glj.marketPriceAdjust});
  277. }
  278. return adjustState;
  279. }
  280. function getContent(coes) {
  281. let stringList=[];
  282. let temAmount = null;
  283. let theSame = true;
  284. for(let t of coes){
  285. if(temAmount == null){
  286. temAmount = t.amount;
  287. }else if(temAmount != t.amount){
  288. theSame = false;
  289. break;
  290. }
  291. }
  292. for(let c of coes){
  293. if( c.amount&&c.amount!=1){
  294. let operator = c.operator;
  295. if(c.operator =="*"){
  296. operator = "X";
  297. }
  298. if(theSame == true && c.coeType == "定额"){
  299. stringList.push(c.coeType+operator+c.amount);
  300. break;
  301. }else
  302. if(theSame == false && c.coeType != "定额"){
  303. stringList.push(c.coeType+operator+c.amount);
  304. }
  305. }
  306. }
  307. return stringList.join(",");
  308. }
  309. function calculateTimes(ass){
  310. if(ass.isAdjust == 0) return 0;//打勾辅助定额才计算
  311. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  312. let r = false;
  313. if(times<0){
  314. r=true;
  315. times=times*-1;
  316. }
  317. if(ass.carryBit=='四舍五入'){
  318. times = _.round(times,ass.decimal);
  319. }else if (ass.carryBit=='进一'){
  320. times =_.ceil(times,ass.decimal);
  321. }
  322. if(r){
  323. times=times*-1;
  324. }
  325. return scMathUtil.roundTo(times,-6);
  326. }
  327. function calculateQuantityByCoes(quantity,coeList,glj,gljList,decimal){
  328. let coeQuantity = quantity;
  329. if(coeList.length>1){
  330. for(let i=0;i<coeList.length-1;i++){
  331. coeQuantity = everyCoe(coeQuantity,coeList[i],glj,gljList,decimal);
  332. }
  333. }
  334. return scMathUtil.roundTo(coeQuantity,-6);
  335. }
  336. function everyCoe(quantity,coe,glj,gljList,decimal) {
  337. let coeQuantity = quantity;
  338. if(coe.isAdjust==1){
  339. for(let i=0;i<coe.coes.length;i++){
  340. if(coe.coes[i].coeType=='单个工料机' &&coe.coes[i].gljCode==glj.code){//if(coe.coes[i].coeType=='单个工料机'&&coe.coes[i].gljCode==glj.code)
  341. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  342. }else if(coe.coes[i].coeType== "替换人材机" && glj.rcode == coe.coes[i].gljCode && glj.code == coe.coes[i].replaceCode){
  343. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  344. }else if(coe.coes[i].coeType== "所选人材机" && glj.rcode == coe.original_code && glj.code == coe.select_code ){
  345. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  346. } else if(coe.coes[i].coeType=='定额'){
  347. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  348. }else if(coeTypeMap[coe.coes[i].coeType]==getRootGLJType(glj.type).ID){
  349. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  350. }
  351. }
  352. }
  353. return scMathUtil.roundTo(coeQuantity,-6);
  354. }
  355. function calculateQuantityByCustomerCoes(quantify,coe,glj) {
  356. let rationAmount = coe.coes[0].amount;
  357. if(_.every(coe.coes,'amount',rationAmount)){
  358. return getCalculateResult(quantify, coe.coes[0])
  359. }else {
  360. for(let i=1;i<coe.coes.length;i++){
  361. if(coeTypeMap[coe.coes[i].coeType]==getRootGLJType(glj.type).ID){
  362. return getCalculateResult(quantify,coe.coes[i])
  363. }
  364. }
  365. }
  366. return quantify
  367. }
  368. function getCoeSelectedGLJ(gljList,rcode,code) {
  369. if(gljList&& code && code !=""){
  370. let o_glj = _.find(gljList,{'rcode':rcode,'code':code});
  371. return o_glj;
  372. }
  373. return null;
  374. }
  375. function getCalculateResult(quantify,c,coe,gljList,decimal) {
  376. let q = quantify;
  377. let o_glj = null;
  378. switch (c.operator){
  379. case '+' :
  380. q = q + c.amount;
  381. break;
  382. case '-' :
  383. q = q - c.amount;
  384. break;
  385. case '*' :
  386. q = q * c.amount;
  387. break;
  388. case '/' :
  389. q = q / c.amount;
  390. break;
  391. case '+*' :
  392. o_glj = getCoeSelectedGLJ(gljList,coe.original_code,coe.select_code);
  393. if(o_glj){
  394. q = q + c.amount * scMathUtil.roundForObj(o_glj.rationItemQuantity,decimal);
  395. }
  396. break;
  397. case '-*' :
  398. o_glj = getCoeSelectedGLJ(gljList,coe.original_code,coe.select_code);
  399. if(o_glj){
  400. q = q - c.amount * scMathUtil.roundForObj(o_glj.rationItemQuantity,decimal);
  401. }
  402. break;
  403. case '=' :
  404. q = c.amount;
  405. break;
  406. }
  407. return q;
  408. }
  409. function getRootGLJType(id){
  410. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  411. let topTypeId = glj_type_object.getTopParentIdByItemId(id);
  412. let type = glj_type_object.getItemById(topTypeId);
  413. return type;
  414. }
  415. function getGLJTypeByID(id) {
  416. let type = getRootGLJType(id);
  417. if(type!=undefined){
  418. return type.fullName;
  419. }else {
  420. return '';
  421. }
  422. }