glj_calculate_facade.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. module.exports={
  15. calculateQuantity:calculateQuantity,
  16. getGLJTypeByID:getGLJTypeByID
  17. }
  18. //辅助定额调整、替换工料机、标准附注条件调整、添加工料机、自定义消耗量(包括删除工料机)、自定义乘系数、市场单价调整
  19. let stateSeq ={
  20. ass:1,
  21. replace:2,
  22. coe:3,
  23. add:4,
  24. cusQuantity:5,
  25. cusCoe:6,
  26. adjMak:7
  27. };
  28. //自定义乘系数与定额工料机类型映射表
  29. let coeTypeMap = {
  30. "人工":1,
  31. "材料":2,
  32. "机械":3,
  33. "施工机具":3,
  34. "主材":4,
  35. "设备":5
  36. };
  37. async function calculateQuantity(query,noNeedCal,refreshRationName = false){
  38. try {
  39. let result ={
  40. glj_result:[],
  41. rationID:query.rationID
  42. };
  43. let impactRation = await ration.findOne({ID:query.rationID,projectID:query.projectID});
  44. let gljList = await ration_glj.find(query);//{projectID:query.projectID,rationID:query.rationID}
  45. let coeList = await ration_coe.find({projectID:query.projectID,rationID:query.rationID}).sort('seq').exec();
  46. let assList=[];
  47. let assRation = null;
  48. let adjustState=[];
  49. if(!impactRation){//如果定额不存在或者已删除,返回空
  50. return null;
  51. }
  52. if(impactRation._doc.hasOwnProperty("rationAssList")&&impactRation.rationAssList.length>0){
  53. for(let i=0;i<impactRation.rationAssList.length;i++){
  54. let times = calculateTimes(impactRation.rationAssList[i]);
  55. if(times!=0){
  56. assRation = await std_ration_lib_ration_items.findOne({rationRepId:impactRation.libID,code:impactRation.rationAssList[i].assistCode});
  57. assList.push({times:times,assRation:assRation});
  58. adjustState.push({index:stateSeq.ass,content:impactRation.rationAssList[i].name+" "+impactRation.rationAssList[i].actualValue+" : +"+impactRation.rationAssList[i].assistCode+"x"+times});
  59. }
  60. }
  61. }
  62. gljList = sortRationGLJ(gljList);
  63. for(let i =0;i<gljList.length;i++ ){
  64. let r = await calculateQuantityPerGLJ(gljList[i],i,coeList,assList,adjustState,noNeedCal);
  65. result.glj_result.push(r);
  66. }
  67. if(noNeedCal==null){
  68. await ration_glj.bulkWrite(generateUpdateTasks(result.glj_result));
  69. }
  70. adjustState= _.sortByOrder(adjustState, ['index'], ['asc']);
  71. adjustState=_.map(adjustState, _.property('content'));
  72. let adjustStateString = adjustState.join(';');
  73. let setData = {adjustState:adjustStateString};
  74. if(refreshRationName == true){//需要更新定额名称
  75. let newName = generateRationName(impactRation,gljList);
  76. setData.name = newName;
  77. result.rationName = newName;
  78. }
  79. await ration.update({projectID:query.projectID,ID:query.rationID,deleteInfo: null},setData);
  80. result.adjustState=adjustStateString;
  81. return result;
  82. }catch (err){
  83. console.log(err);
  84. throw err;
  85. }
  86. }
  87. function generateRationName(ration,gljList) {
  88. let caption = ration.caption ? ration.caption:ration.name;
  89. if(ration.rationAssList && ration.rationAssList.length > 0){
  90. let ass = ration.rationAssList[0];
  91. if(ass.actualValue != null && ass.actualValue != undefined ){
  92. caption = caption.replace('%s',ass.actualValue);
  93. }
  94. }
  95. let reNameList = [];
  96. for(let g of gljList){
  97. //glj._doc.createType=='replace'&&glj.rcode!=glj.code
  98. if(g.createType=='replace'&&g.rcode!=g.code){ //是替换工料机
  99. let reName = g.name;
  100. if(!_.isEmpty(g.specs)) reName = reName + ' '+g.specs;
  101. reNameList.push(reName);
  102. }
  103. }
  104. if(reNameList.length > 0){
  105. let reNameString = reNameList.join(" ");
  106. caption = caption + " 换为【"+ reNameString + "】";
  107. }
  108. return caption;
  109. }
  110. function generateUpdateTasks(result) {
  111. let tasks = [];
  112. for(let i =0;i<result.length;i++){
  113. let task= {
  114. updateOne: {
  115. filter: result[i].query,
  116. update: result[i].doc
  117. }
  118. }
  119. tasks.push(task);
  120. }
  121. return tasks;
  122. }
  123. function sortRationGLJ(list) {
  124. list = _.sortByAll(list, [function (item) {
  125. return item.type + "";
  126. }, "code"])
  127. return list;
  128. }
  129. async function calculateQuantityPerGLJ(glj,index,coeList,assList,adjustState,noNeedCal) {
  130. let decimalObject =await decimal_facade.getProjectDecimal(glj.projectID);
  131. let decimal = (decimalObject&&decimalObject.glj&&decimalObject.glj.quantity)?decimalObject.glj.quantity:3;
  132. let quantity = scMathUtil.roundTo(parseFloat(glj.quantity),-decimal);
  133. let result={
  134. query:{
  135. ID:glj.ID,
  136. projectID:glj.projectID
  137. },
  138. doc:{
  139. quantity: quantity
  140. }
  141. };
  142. try {
  143. if(noNeedCal==null){
  144. if(!glj._doc.hasOwnProperty('customQuantity')||glj.customQuantity==null||glj.customQuantity==""){
  145. quantity =scMathUtil.roundTo(parseFloat(glj.rationItemQuantity),-decimal);
  146. quantity =scMathUtil.roundTo(await calculateAss(quantity,assList,glj),-decimal);
  147. quantity = calculateQuantityByCoes(quantity,coeList,glj);
  148. }else {
  149. quantity = glj.customQuantity;
  150. result.doc.customQuantity = glj.customQuantity;
  151. }
  152. let customerCoe = _.last(coeList);
  153. if(customerCoe&&customerCoe.isAdjust==1){
  154. quantity = scMathUtil.roundToString(quantity,decimal);
  155. quantity = calculateQuantityByCustomerCoes(quantity,customerCoe,glj);
  156. }
  157. result.doc.quantity =scMathUtil.roundToString(quantity,decimal);
  158. }
  159. generateAdjustState(glj,coeList,adjustState,index,result.doc.quantity);
  160. return result;
  161. }catch (err){
  162. throw err;
  163. }
  164. }
  165. async function calculateAss(quantity,assList,glj) {
  166. for(let i=0;i<assList.length;i++){
  167. if(assList[i].assRation){
  168. let assglj = null;
  169. for(let aglj of assList[i].assRation.rationGljList){
  170. if(glj.createType == 'replace'){//如果工料机是替换过的,要用原始的编码来匹配
  171. let std_glj = await std_glj_lib_gljList_model.findOne({'ID':aglj.gljId});
  172. if(glj.rcode == std_glj.code){
  173. assglj = aglj;
  174. break;
  175. }
  176. }else if(aglj.gljId == glj.GLJID){
  177. assglj = aglj;
  178. break;
  179. }
  180. }
  181. if(assglj){
  182. let calQuantity = assglj.consumeAmt*assList[i].times;
  183. quantity += calQuantity
  184. }
  185. }
  186. }
  187. return scMathUtil.roundTo(quantity,-6);
  188. }
  189. function generateAdjustState(glj,coeList,adjustState,index,quantity) {
  190. //替换工料机 and 添加工料机
  191. if(glj._doc.createType=='replace'&&glj.rcode!=glj.code){
  192. adjustState.push({index:stateSeq.replace,content:glj.rcode+'换'+glj.code});
  193. }else if(glj._doc.createType=='add'){
  194. let displayQuantity = quantity;
  195. if(glj._doc.hasOwnProperty('customQuantity')&&(glj.customQuantity != null||glj.customQuantity != '')){
  196. displayQuantity = glj.customQuantity;
  197. }
  198. displayQuantity = displayQuantity&&displayQuantity!=""?parseFloat(displayQuantity):0;
  199. adjustState.push({index:stateSeq.add,content:'添'+glj.code+'量'+ displayQuantity});
  200. }
  201. // to do
  202. //标准附注条件调整 + 自定义乘系数
  203. if(0==index){
  204. for(let i=0;i<coeList.length;i++){
  205. if(coeList[i].isAdjust==1){
  206. if(i==coeList.length-1){
  207. adjustState.push({index:stateSeq.cusCoe,content:getContent(coeList[i].coes)});//自定义乘系数要去掉倍数为1的
  208. }else {
  209. adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].content});//coeList[i].content
  210. }
  211. }
  212. }
  213. }
  214. //自定义消耗量
  215. if(glj._doc.createType!='add'&&glj._doc.hasOwnProperty('customQuantity')){
  216. if(glj.customQuantity!==null&&glj.customQuantity!=""){
  217. adjustState.push({index:stateSeq.cusQuantity,content:glj.code+'量'+parseFloat(glj.customQuantity)});
  218. }
  219. }
  220. //市场单价调整
  221. if(glj._doc.hasOwnProperty('marketPriceAdjust')&&glj.marketPriceAdjust&&glj.marketPriceAdjust!=0){
  222. //0101005价66.00
  223. adjustState.push({index:stateSeq.adjMak,content:glj.code+'价'+glj.marketPriceAdjust});
  224. }
  225. return adjustState;
  226. }
  227. function getContent(coes) {
  228. let stringList=[];
  229. let temAmount = null;
  230. let theSame = true;
  231. for(let t of coes){
  232. if(temAmount == null){
  233. temAmount = t.amount;
  234. }else if(temAmount != t.amount){
  235. theSame = false;
  236. break;
  237. }
  238. }
  239. for(let c of coes){
  240. if( c.amount&&c.amount!=1){
  241. let operator = c.operator;
  242. if(c.operator =="*"){
  243. operator = "X";
  244. }
  245. if(theSame == true && c.coeType == "定额"){
  246. stringList.push(c.coeType+operator+c.amount);
  247. break;
  248. }else
  249. if(theSame == false && c.coeType != "定额"){
  250. stringList.push(c.coeType+operator+c.amount);
  251. }
  252. }
  253. }
  254. return stringList.join(",");
  255. }
  256. function calculateTimes(ass){
  257. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  258. let r = false;
  259. if(times<0){
  260. r=true;
  261. times=times*-1;
  262. }
  263. if(ass.carryBit=='四舍五入'){
  264. times = _.round(times,ass.decimal);
  265. }else if (ass.carryBit=='进一'){
  266. times =_.ceil(times,ass.decimal);
  267. }
  268. if(r){
  269. times=times*-1;
  270. }
  271. return scMathUtil.roundTo(times,-6);
  272. }
  273. function calculateQuantityByCoes(quantity,coeList,glj){
  274. let coeQuantity = quantity;
  275. if(coeList.length>1){
  276. for(let i=0;i<coeList.length-1;i++){
  277. coeQuantity = everyCoe(coeQuantity,coeList[i],glj);
  278. }
  279. }
  280. return scMathUtil.roundTo(coeQuantity,-6);
  281. }
  282. function everyCoe(quantity,coe,glj) {
  283. let coeQuantity = quantity;
  284. if(coe.isAdjust==1){
  285. for(let i=0;i<coe.coes.length;i++){
  286. if(coe.coes[i].gljCode==glj.code){//if(coe.coes[i].coeType=='单个工料机'&&coe.coes[i].gljCode==glj.code)
  287. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  288. } else if(coe.coes[i].coeType=='定额'){
  289. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  290. }else if(coeTypeMap[coe.coes[i].coeType]==getRootGLJType(glj.type).ID){
  291. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  292. }
  293. }
  294. }
  295. return scMathUtil.roundTo(coeQuantity,-6);
  296. }
  297. function calculateQuantityByCustomerCoes(quantify,coe,glj) {
  298. let rationAmount = coe.coes[0].amount;
  299. if(_.every(coe.coes,'amount',rationAmount)){
  300. return getCalculateResult(quantify, coe.coes[0])
  301. }else {
  302. for(let i=1;i<coe.coes.length;i++){
  303. if(coeTypeMap[coe.coes[i].coeType]==getRootGLJType(glj.type).ID){
  304. return getCalculateResult(quantify,coe.coes[i])
  305. }
  306. }
  307. }
  308. return quantify
  309. }
  310. function getCalculateResult(quantify,c) {
  311. let q = quantify;
  312. switch (c.operator){
  313. case '+' :
  314. q = q + c.amount;
  315. break;
  316. case '-' :
  317. q = q - c.amount;
  318. break;
  319. case '*' :
  320. q = q * c.amount;
  321. break;
  322. case '/' :
  323. q = q / c.amount;
  324. break;
  325. case '=' :
  326. q = c.amount;
  327. break;
  328. }
  329. return q;
  330. }
  331. function getRootGLJType(id){
  332. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  333. let topTypeId = glj_type_object.getTopParentIdByItemId(id);
  334. let type = glj_type_object.getItemById(topTypeId);
  335. return type;
  336. }
  337. function getGLJTypeByID(id) {
  338. let type = getRootGLJType(id);
  339. if(type!=undefined){
  340. return type.fullName;
  341. }else {
  342. return '';
  343. }
  344. }