glj_calculate_facade.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. let std_ration_lib_ration_items = mongoose.model('std_ration_lib_ration_items');
  10. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  11. let project_model = mongoose.model('projects');
  12. let glj_type_util = require('../../../public/cache/std_glj_type_util');
  13. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  14. let decimal_facade = require('../../main/facade/decimal_facade');
  15. let gljUtil = require('../../../public/gljUtil');
  16. module.exports={
  17. calculateQuantity:calculateQuantity,
  18. getGLJTypeByID:getGLJTypeByID
  19. }
  20. //辅助定额调整、替换工料机、标准附注条件调整、添加工料机、自定义消耗量(包括删除工料机)、自定义乘系数、面积增加、市场单价调整
  21. let stateSeq ={
  22. ass:1,
  23. replace:2,
  24. coe:3,
  25. add:4,
  26. cusQuantity:5,
  27. cusCoe:6,
  28. area:7,
  29. adjMak:8
  30. };
  31. //自定义乘系数与定额工料机类型映射表
  32. let coeTypeMap = {
  33. "人工":1,
  34. "材料":2,
  35. "机械":3,
  36. "施工机具":3,
  37. "主材":4,
  38. "设备":5
  39. };
  40. let areaIncreaseMap = {
  41. 1:"labour",
  42. 2:"material",
  43. 3:"machine"
  44. };
  45. async function calculateQuantity(query,noNeedCal,refreshRationName = false,areaIncreaseFee,areaSetting){
  46. try {
  47. let result ={
  48. glj_result:[],
  49. rationID:query.rationID
  50. };
  51. let impactRation = await ration.findOne({ID:query.rationID});
  52. let gljList = await ration_glj.find(query);//{projectID:query.projectID,rationID:query.rationID}
  53. let coeList = await ration_coe.find({rationID:query.rationID}).sort('seq').exec();
  54. let assList=[];
  55. let assRation = null;
  56. let adjustState=[];
  57. if(!impactRation){//如果定额不存在或者已删除,返回空
  58. return null;
  59. }
  60. if(areaIncreaseFee !== null && areaIncreaseFee !== undefined) impactRation.areaIncreaseFee = areaIncreaseFee;
  61. if(impactRation.areaIncreaseFee == true && !areaSetting){
  62. let project = await project_model.findOne({ID:impactRation.projectID},'property.areaSetting');
  63. areaSetting = project.property.areaSetting;
  64. }
  65. if(impactRation._doc.hasOwnProperty("rationAssList")&&impactRation.rationAssList.length>0){
  66. prepareAss(impactRation.rationAssList);
  67. let temTimes = [];
  68. let thirdRationCodes=[];
  69. for(let i=0;i<impactRation.rationAssList.length;i++){
  70. let times = calculateTimes(impactRation.rationAssList[i]);
  71. if(times!=0){
  72. let thirdRationCode = impactRation.rationAssList[i].thirdRationCode;
  73. if(thirdRationCode && thirdRationCode !=''){
  74. temTimes.push(times);
  75. thirdRationCodes.push(thirdRationCode)
  76. }
  77. assRation = await std_ration_lib_ration_items.findOne({rationRepId:impactRation.libID,code:impactRation.rationAssList[i].assistCode});
  78. assList.push({times:times,assRation:assRation});
  79. adjustState.push({index:stateSeq.ass,content:impactRation.rationAssList[i].name+" "+impactRation.rationAssList[i].actualValue+" : +"+impactRation.rationAssList[i].assistCode+"x"+times});
  80. }
  81. }
  82. if(temTimes.length == 2 &&thirdRationCodes[0] == thirdRationCodes[1] ){ //说明有第三定额
  83. let times_t = temTimes[0] * temTimes[1];
  84. let tration = await std_ration_lib_ration_items.findOne({rationRepId:impactRation.libID,code:thirdRationCodes[0]});
  85. if(tration){
  86. assList.push({times:times_t,assRation:tration});
  87. adjustState.push({index:stateSeq.ass,content:"+"+thirdRationCodes[0]+"x"+times_t});
  88. }
  89. }
  90. }
  91. gljList = sortRationGLJ(gljList);
  92. for(let i =0;i<gljList.length;i++ ){
  93. let r = await calculateQuantityPerGLJ(gljList[i],gljList,coeList,assList,adjustState,noNeedCal,impactRation.areaIncreaseFee,areaSetting);
  94. if(quantityUpdateCheck(gljList[i],r) == true) result.glj_result.push(r);
  95. }
  96. if(noNeedCal==null && result.glj_result.length > 0){
  97. await ration_glj.bulkWrite(generateUpdateTasks(result.glj_result));
  98. }
  99. if(impactRation.areaIncreaseFee == true) await setAreaAdjustState(impactRation.projectID,adjustState,areaSetting);
  100. adjustState= _.sortByOrder(adjustState, ['index'], ['asc']);
  101. adjustState=_.map(adjustState, _.property('content'));
  102. let adjustStateString = adjustState.join(';');
  103. let setData = {adjustState:adjustStateString};
  104. if(refreshRationName == true){//需要更新定额名称
  105. let newName = generateRationName(impactRation,gljList);
  106. setData.name = newName;
  107. result.rationName = newName;
  108. }
  109. //如果uareaIncreaseFee有值说明是从其它更新定额的地方进来的,后面会一起更新
  110. if(areaIncreaseFee === null || areaIncreaseFee === undefined) await ration.update({ID:query.rationID},setData);
  111. result.adjustState=adjustStateString;
  112. return result;
  113. }catch (err){
  114. console.log(err);
  115. throw err;
  116. }
  117. }
  118. async function setAreaAdjustState(projectID,adjustState,areaSetting){
  119. if(areaSetting){
  120. let stringArr = [];
  121. let labour = getStr(areaSetting.labour);
  122. let material = getStr(areaSetting.material);
  123. let machine = getStr(areaSetting.machine);
  124. if(labour !="") stringArr.push(`人工*${labour}`);
  125. if(material !="") stringArr.push(`材料*${material}`);
  126. if(machine !="") stringArr.push(`机械*${machine}`);
  127. if(stringArr.length > 0){
  128. adjustState.push({index:stateSeq.area,content:`面积:${stringArr.join(',')}`});//面积:人工*1.1,材料*1.015”。
  129. }
  130. }
  131. function getStr(num) {
  132. if(num != 0) return 1 + num/100;
  133. return "";
  134. }
  135. }
  136. function quantityUpdateCheck(glj,r) {//检查,有改变的才更新
  137. for(let key in r.doc){
  138. if(glj._doc[key] != r.doc[key]) return true
  139. }
  140. return false
  141. }
  142. function generateRationName(ration,gljList) {
  143. let caption = ration.caption ? ration.caption:ration.name;
  144. let replaceList = [];
  145. if(ration.rationAssList && ration.rationAssList.length > 0){//这里要处理第三定额和多辅助定额的情况
  146. let isThird = ration.rationAssList.length == 2 && ration.rationAssList[0].thirdRationCode&&ration.rationAssList[0].thirdRationCode!='';//说明有第三定额的情况
  147. let adjustMatch = "",notAdjust="";
  148. for(let ass of ration.rationAssList){
  149. let tem = "";
  150. if( ass.isAdjust == 1 && ass.actualValue != null && ass.actualValue != undefined ){
  151. tem = ass.actualValue;
  152. adjustMatch = tem;
  153. }else {
  154. tem = ass.stdValue;
  155. notAdjust = tem;
  156. }
  157. if(isThird) replaceList.push(tem);
  158. }
  159. if(replaceList.length == 0){
  160. adjustMatch!=""?replaceList.push(adjustMatch):replaceList.push(notAdjust);
  161. }
  162. for(let r of replaceList){
  163. caption = caption.replace('%s',r);
  164. }
  165. }
  166. let reNameList = [];
  167. for(let g of gljList){
  168. //glj._doc.createType=='replace'&&glj.rcode!=glj.code
  169. if(g.createType=='replace'&&g.rcode!=g.code){ //是替换工料机
  170. let reName = g.name;
  171. if(!_.isEmpty(g.specs)) reName = reName + ' '+g.specs;
  172. reNameList.push(reName);
  173. }
  174. }
  175. if(reNameList.length > 0){
  176. let reNameString = reNameList.join(" ");
  177. caption = caption + " 换为【"+ reNameString + "】";
  178. }
  179. return caption;
  180. }
  181. function generateUpdateTasks(result) {
  182. let tasks = [];
  183. for(let i =0;i<result.length;i++){
  184. let task= {
  185. updateOne: {
  186. filter: result[i].query,
  187. update: result[i].doc
  188. }
  189. }
  190. tasks.push(task);
  191. }
  192. return tasks;
  193. }
  194. function sortRationGLJ(list) {
  195. list = _.sortByAll(list, [function (item) {
  196. return _.indexOf(gljUtil.getGljTypeSeq(),item.type);
  197. }, "code"])
  198. return list;
  199. }
  200. async function calculateQuantityPerGLJ(glj,gljList,coeList,assList,adjustState,noNeedCal,areaIncreaseFee,areaSetting) {
  201. let decimalObject =await decimal_facade.getProjectDecimal(glj.projectID);
  202. let decimal = (decimalObject&&decimalObject.glj&&decimalObject.glj.quantity)?decimalObject.glj.quantity:3;
  203. let quantity = scMathUtil.roundTo(parseFloat(glj.quantity),-decimal);
  204. let result={
  205. query:{
  206. ID:glj.ID,
  207. projectID:glj.projectID
  208. },
  209. doc:{
  210. quantity: quantity
  211. }
  212. };
  213. try {
  214. if(noNeedCal==null){
  215. if(!glj._doc.hasOwnProperty('customQuantity')||glj.customQuantity==null||glj.customQuantity==""){
  216. quantity =scMathUtil.roundTo(parseFloat(glj.rationItemQuantity),-decimal);
  217. quantity =scMathUtil.roundTo(await calculateAss(quantity,assList,glj),-decimal);
  218. quantity = calculateQuantityByCoes(quantity,coeList,glj,gljList,decimal);
  219. }else {
  220. quantity = glj.customQuantity;
  221. result.doc.customQuantity = glj.customQuantity;
  222. }
  223. let customerCoe = _.last(coeList);
  224. if(customerCoe&&customerCoe.isAdjust==1){
  225. quantity = scMathUtil.roundToString(quantity,decimal);
  226. quantity = calculateQuantityByCustomerCoes(quantity,customerCoe,glj,decimal);
  227. }
  228. //计算面积增加费
  229. if(areaIncreaseFee == true) quantity = calclateAreaIncrease(quantity,glj,areaSetting,decimal);
  230. result.doc.quantity =scMathUtil.roundToString(quantity,decimal);
  231. //2019-01-03 需求修改中间过程的价格不参与计算
  232. //glj.quantity = quantity;//这里保存中间过程计算出来的消耗量,后面处理“+*”操作符时要用到
  233. }
  234. generateAdjustState(glj,coeList,adjustState,gljList,result.doc.quantity);
  235. return result;
  236. }catch (err){
  237. throw err;
  238. }
  239. }
  240. function calclateAreaIncrease(quantity,glj,areaSetting,decimal){
  241. if(areaSetting){
  242. let atype = areaIncreaseMap[getRootGLJType(glj.type).ID];
  243. if(atype && areaSetting[atype]){
  244. quantity = scMathUtil.roundForObj(quantity,decimal);
  245. quantity = scMathUtil.roundToString(quantity * (1 + areaSetting[atype]/100 ),decimal)
  246. }
  247. }
  248. return quantity
  249. }
  250. async function calculateAss(quantity,assList,glj) {
  251. for(let i=0;i<assList.length;i++){
  252. if(assList[i].assRation){
  253. let assglj = null;
  254. for(let aglj of assList[i].assRation.rationGljList){
  255. if(glj.createType == 'replace'){//如果工料机是替换过的,要用原始的编码来匹配
  256. let std_glj = await std_glj_lib_gljList_model.findOne({'ID':aglj.gljId});
  257. if(glj.rcode == std_glj.code){
  258. assglj = aglj;
  259. break;
  260. }
  261. }else if(aglj.gljId == glj.GLJID){
  262. assglj = aglj;
  263. break;
  264. }
  265. }
  266. if(assglj){
  267. let calQuantity = assglj.consumeAmt*assList[i].times;
  268. quantity += calQuantity
  269. }
  270. }
  271. }
  272. return scMathUtil.roundTo(quantity,-6);
  273. }
  274. function generateAdjustState(glj,coeList,adjustState,gljList,quantity) {
  275. //替换工料机 and 添加工料机
  276. if(glj._doc.createType=='replace'&&glj.rcode!=glj.code){
  277. adjustState.push({index:stateSeq.replace,content:glj.rcode+'换'+glj.code});
  278. }else if(glj._doc.createType=='add'){
  279. let displayQuantity = quantity;
  280. if(glj._doc.hasOwnProperty('customQuantity')&&(glj.customQuantity != null||glj.customQuantity != '')){
  281. displayQuantity = glj.customQuantity;
  282. }
  283. displayQuantity = displayQuantity&&displayQuantity!=""?parseFloat(displayQuantity):0;
  284. adjustState.push({index:stateSeq.add,content:'添'+glj.code+'量'+ displayQuantity,type:"添"+glj.code});
  285. }
  286. // to do
  287. //标准附注条件调整 + 自定义乘系数
  288. if(_.last(gljList).ID == glj.ID){//最后一个工料机的时候才生成,生成一次就可以了
  289. for(let i=0;i<coeList.length;i++){
  290. if(coeList[i].isAdjust==1){
  291. if(i==coeList.length-1){
  292. let cus_content = getContent(coeList[i].coes);
  293. if(cus_content !="") adjustState.push({index:stateSeq.cusCoe,content:cus_content});//自定义乘系数要去掉倍数为1的
  294. }else {
  295. if(coeList[i].select_code && coeList[i].select_code!=""){
  296. _.remove(adjustState,{'content':coeList[i].original_code+'换'+coeList[i].select_code});//去掉替换工料机自动生成的调整状态
  297. adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].original_code+'换'+coeList[i].select_code});
  298. }
  299. for(let c of coeList[i].coes){
  300. if(c.coeType=='单个工料机') _.remove(adjustState,{'type':"添"+c.gljCode});//如果是单个工料机子目换算类型自动添加的,去掉前面手动生成的调整状态
  301. if(c.coeType=='替换人材机') _.remove(adjustState,{'content':c.gljCode+'换'+c.replaceCode});//如果是替换人材机子目换算类型自动添加的,去掉前面手动生成的调整状态
  302. }
  303. if(coeList[i].content) adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].content});//coeList[i].content
  304. }
  305. }
  306. }
  307. }
  308. //自定义消耗量
  309. if(glj._doc.createType!='add'&&glj._doc.hasOwnProperty('customQuantity')){
  310. if(glj.customQuantity!==null&&glj.customQuantity!=""){
  311. adjustState.push({index:stateSeq.cusQuantity,content:glj.code+'量'+parseFloat(glj.customQuantity)});
  312. }
  313. }
  314. //市场单价调整
  315. if(glj._doc.hasOwnProperty('marketPriceAdjust')&&glj.marketPriceAdjust&&glj.marketPriceAdjust!=0){
  316. //0101005价66.00
  317. adjustState.push({index:stateSeq.adjMak,content:glj.code+'价'+glj.marketPriceAdjust});
  318. }
  319. return adjustState;
  320. }
  321. function getContent(coes) {
  322. let stringList=[];
  323. let temAmount = null;
  324. let theSame = true;
  325. for(let t of coes){
  326. if(temAmount == null){
  327. temAmount = t.amount;
  328. }else if(temAmount != t.amount){
  329. theSame = false;
  330. break;
  331. }
  332. }
  333. for(let c of coes){
  334. if( c.amount&&c.amount!=1){
  335. let operator = c.operator;
  336. if(c.operator =="*"){
  337. operator = "X";
  338. }
  339. if(theSame == true && c.coeType == "定额"){
  340. stringList.push(c.coeType+operator+c.amount);
  341. break;
  342. }else
  343. if(theSame == false && c.coeType != "定额"){
  344. stringList.push(c.coeType+operator+c.amount);
  345. }
  346. }
  347. }
  348. return stringList.join(",");
  349. }
  350. function prepareAss(assList) {//处理辅助定额,支持多个辅助定额的情况
  351. for(let a of assList){
  352. if(a.groupList && a.groupList.length > 1){//组里有多个定额的情况
  353. let newList = _.sortByAll(a.groupList,[function(item){
  354. return parseFloat(item.param)
  355. }]);//先按参数排序
  356. let pre = 0;
  357. for(let n of newList){
  358. if(a.actualValue > pre && a.actualValue <= parseFloat(n.param)){//落在中间,则用组里的这条定额
  359. a._doc.param = n.param;
  360. a._doc.paramName = n.paramName;
  361. a._doc.assistCode = n.assistCode;
  362. break;
  363. }
  364. pre = parseFloat(n.param);
  365. }
  366. }
  367. }
  368. }
  369. function calculateTimes(ass){
  370. if(ass.isAdjust == 0) return 0;//打勾辅助定额才计算
  371. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  372. let r = false;
  373. if(times<0){
  374. r=true;
  375. times=times*-1;
  376. }
  377. if(ass.carryBit=='四舍五入'){
  378. times = _.round(times,ass.decimal);
  379. }else if (ass.carryBit=='进一'){
  380. times =_.ceil(times,ass.decimal);
  381. }else if(ass.carryBit == '舍一'){
  382. times = _.floor(times,ass.decimal);
  383. }
  384. if(r){
  385. times=times*-1;
  386. }
  387. return scMathUtil.roundTo(times,-6);
  388. }
  389. function calculateQuantityByCoes(quantity,coeList,glj,gljList,decimal){
  390. let coeQuantity = quantity;
  391. if(coeList.length>1){
  392. for(let i=0;i<coeList.length-1;i++){
  393. coeQuantity = everyCoe(coeQuantity,coeList[i],glj,gljList,decimal);
  394. }
  395. }
  396. return scMathUtil.roundTo(coeQuantity,-6);
  397. }
  398. function everyCoe(quantity,coe,glj,gljList,decimal) {
  399. let coeQuantity = quantity;
  400. if(coe.isAdjust==1){
  401. for(let i=0;i<coe.coes.length;i++){
  402. if(coe.coes[i].coeType=='单个工料机' &&coe.coes[i].gljCode==glj.code){//if(coe.coes[i].coeType=='单个工料机'&&coe.coes[i].gljCode==glj.code)
  403. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  404. }else if(coe.coes[i].coeType== "替换人材机" && glj.rcode == coe.coes[i].gljCode && glj.code == coe.coes[i].replaceCode){
  405. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  406. }else if(coe.coes[i].coeType== "所选人材机" && glj.rcode == coe.original_code && glj.code == coe.select_code ){
  407. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  408. } else if(coe.coes[i].coeType=='定额'){
  409. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  410. }else if(coeTypeMap[coe.coes[i].coeType]==getRootGLJType(glj.type).ID){
  411. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  412. }
  413. }
  414. }
  415. return scMathUtil.roundTo(coeQuantity,-6);
  416. }
  417. function calculateQuantityByCustomerCoes(quantify,coe,glj) {
  418. let rationAmount = coe.coes[0].amount;
  419. if(_.every(coe.coes,'amount',rationAmount)){
  420. return getCalculateResult(quantify, coe.coes[0])
  421. }else {
  422. for(let i=1;i<coe.coes.length;i++){
  423. if(coeTypeMap[coe.coes[i].coeType]==getRootGLJType(glj.type).ID){
  424. return getCalculateResult(quantify,coe.coes[i])
  425. }
  426. }
  427. }
  428. return quantify
  429. }
  430. function getCoeSelectedGLJ(gljList,rcode,code) {
  431. if(gljList&& code && code !=""){
  432. let o_glj = _.find(gljList,{'rcode':rcode,'code':code});
  433. return o_glj;
  434. }
  435. return null;
  436. }
  437. function getCalculateResult(quantify,c,coe,gljList,decimal) {
  438. let q = quantify;
  439. let o_glj = null;
  440. switch (c.operator){
  441. case '+' :
  442. q = q + c.amount;
  443. break;
  444. case '-' :
  445. q = q - c.amount;
  446. break;
  447. case '*' :
  448. q = q * c.amount;
  449. break;
  450. case '/' :
  451. q = q / c.amount;
  452. break;
  453. case '+*' :
  454. o_glj = getCoeSelectedGLJ(gljList,coe.original_code,coe.select_code);
  455. if(o_glj){
  456. q = q + c.amount * scMathUtil.roundForObj(o_glj.rationItemQuantity,decimal);
  457. }
  458. break;
  459. case '-*' :
  460. o_glj = getCoeSelectedGLJ(gljList,coe.original_code,coe.select_code);
  461. if(o_glj){
  462. q = q - c.amount * scMathUtil.roundForObj(o_glj.rationItemQuantity,decimal);
  463. }
  464. break;
  465. case '=' :
  466. q = c.amount;
  467. break;
  468. }
  469. return q;
  470. }
  471. function getRootGLJType(id){
  472. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  473. let topTypeId = glj_type_object.getTopParentIdByItemId(id);
  474. let type = glj_type_object.getItemById(topTypeId);
  475. return type;
  476. }
  477. function getGLJTypeByID(id) {
  478. let type = getRootGLJType(id);
  479. if(type!=undefined){
  480. return type.fullName;
  481. }else {
  482. return '';
  483. }
  484. }