glj_calculate_facade.js 16 KB

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