glj_calculate_facade.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. async function calculateQuantity(query,noNeedCal){
  29. try {
  30. let result ={
  31. glj_result:[],
  32. rationID:query.rationID
  33. };
  34. let impactRation = await ration.findOne({projectID:query.projectID,ID:query.rationID,deleteInfo:null});
  35. let gljList = await ration_glj.find(query)//{projectID:query.projectID,rationID:query.rationID}
  36. let coeList = await ration_coe.find({projectID:query.projectID,rationID:query.rationID}).sort('seq').exec();
  37. let assList=[];
  38. let assRation = null;
  39. let adjustState=[];
  40. if(!impactRation){//如果定额不存在或者已删除,返回空
  41. return null;
  42. }
  43. if(impactRation._doc.hasOwnProperty("rationAssList")&&impactRation.rationAssList.length>0){
  44. for(let i=0;i<impactRation.rationAssList.length;i++){
  45. let times = calculateTimes(impactRation.rationAssList[i]);
  46. if(times!=0){
  47. assRation = await std_ration_lib_ration_items.findOne({rationRepId:impactRation.libID,code:impactRation.rationAssList[i].assistCode});
  48. assList.push({times:times,assRation:assRation});
  49. adjustState.push({index:stateSeq.ass,content:impactRation.rationAssList[i].name+" "+impactRation.rationAssList[i].actualValue+" : +"+impactRation.rationAssList[i].assistCode+"x"+times});
  50. }
  51. }
  52. }
  53. for(let i =0;i<gljList.length;i++ ){
  54. let r = await calculateQuantityPerGLJ(gljList[i],i,coeList,assList,adjustState,noNeedCal);
  55. result.glj_result.push(r);
  56. }
  57. if(noNeedCal==null){
  58. await ration_glj.bulkWrite(generateUpdateTasks(result.glj_result));
  59. }
  60. adjustState= _.sortByOrder(adjustState, ['index'], ['asc']);
  61. adjustState=_.map(adjustState, _.property('content'));
  62. let adjustStateString = adjustState.join(';');
  63. await ration.update({projectID:query.projectID,ID:query.rationID,deleteInfo: null},{adjustState:adjustStateString});
  64. result.adjustState=adjustStateString;
  65. return result;
  66. }catch (err){
  67. console.log(err);
  68. throw err;
  69. }
  70. }
  71. function generateUpdateTasks(result) {
  72. let tasks = [];
  73. for(let i =0;i<result.length;i++){
  74. let task= {
  75. updateOne: {
  76. filter: result[i].query,
  77. update: result[i].doc
  78. }
  79. }
  80. tasks.push(task);
  81. }
  82. return tasks;
  83. }
  84. async function calculateQuantityPerGLJ(glj,index,coeList,assList,adjustState,noNeedCal) {
  85. let decimalObject =await decimal_facade.getProjectDecimal(glj.projectID);
  86. let decimal = (decimalObject&&decimalObject.glj&&decimalObject.glj.quantity)?decimalObject.glj.quantity:3;
  87. let quantity = scMathUtil.roundTo(parseFloat(glj.quantity),-decimal);
  88. let result={
  89. query:{
  90. ID:glj.ID,
  91. projectID:glj.projectID
  92. },
  93. doc:{
  94. quantity: quantity
  95. }
  96. };
  97. try {
  98. if(noNeedCal==null){
  99. if(!glj._doc.hasOwnProperty('customQuantity')||glj.customQuantity==null||glj.customQuantity==""){
  100. quantity =scMathUtil.roundTo(parseFloat(glj.rationItemQuantity),-decimal);
  101. quantity =scMathUtil.roundTo(await calculateAss(quantity,assList,glj),-decimal);
  102. quantity = calculateQuantityByCoes(quantity,coeList,glj);
  103. }else {
  104. quantity = glj.customQuantity;
  105. result.doc.customQuantity = glj.customQuantity;
  106. }
  107. let customerCoe = _.last(coeList);
  108. if(customerCoe&&customerCoe.isAdjust==1){
  109. quantity = scMathUtil.roundToString(quantity,decimal);
  110. quantity = calculateQuantityByCustomerCoes(quantity,customerCoe,glj);
  111. }
  112. result.doc.quantity =scMathUtil.roundToString(quantity,decimal);
  113. }
  114. generateAdjustState(glj,coeList,adjustState,index,result.doc.quantity);
  115. return result;
  116. }catch (err){
  117. throw err;
  118. }
  119. }
  120. async function calculateAss(quantity,assList,glj) {
  121. for(let i=0;i<assList.length;i++){
  122. if(assList[i].assRation){
  123. let assglj = null;
  124. for(let aglj of assList[i].assRation.rationGljList){
  125. if(glj.createType == 'replace'){//如果工料机是替换过的,要用原始的编码来匹配
  126. let std_glj = await std_glj_lib_gljList_model.findOne({'ID':aglj.gljId});
  127. if(glj.rcode == std_glj.code){
  128. assglj = aglj;
  129. break;
  130. }
  131. }else if(aglj.gljId == glj.GLJID){
  132. assglj = aglj;
  133. break;
  134. }
  135. }
  136. if(assglj){
  137. let calQuantity = assglj.consumeAmt*assList[i].times;
  138. quantity += calQuantity
  139. }
  140. }
  141. }
  142. return scMathUtil.roundTo(quantity,-6);
  143. }
  144. function generateAdjustState(glj,coeList,adjustState,index,quantity) {
  145. //替换工料机 and 添加工料机
  146. if(glj._doc.createType=='replace'&&glj.rcode!=glj.code){
  147. adjustState.push({index:stateSeq.replace,content:glj.rcode+'换'+glj.code});
  148. }else if(glj._doc.createType=='add'){
  149. adjustState.push({index:stateSeq.add,content:'添'+glj.code+'量'+parseFloat(quantity)});
  150. }
  151. // to do
  152. //标准附注条件调整 + 自定义乘系数
  153. if(0==index){
  154. for(let i=0;i<coeList.length;i++){
  155. if(coeList[i].isAdjust==1){
  156. if(i==coeList.length-1){
  157. adjustState.push({index:stateSeq.cusCoe,content:getContent(coeList[i].coes)});//自定义乘系数要去掉倍数为1的
  158. }else {
  159. adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].content});//coeList[i].content
  160. }
  161. }
  162. }
  163. }
  164. //自定义消耗量
  165. if(glj._doc.createType!='add'&&glj._doc.hasOwnProperty('customQuantity')){
  166. if(glj.customQuantity!==null&&glj.customQuantity!=""){
  167. adjustState.push({index:stateSeq.cusQuantity,content:glj.code+'量'+parseFloat(glj.customQuantity)});
  168. }
  169. }
  170. //市场单价调整
  171. if(glj._doc.hasOwnProperty('marketPriceAdjust')&&glj.marketPriceAdjust&&glj.marketPriceAdjust!=0){
  172. //0101005价66.00
  173. adjustState.push({index:stateSeq.adjMak,content:glj.code+'价'+glj.marketPriceAdjust});
  174. }
  175. return adjustState;
  176. }
  177. function getContent(coes) {
  178. let stringList=[];
  179. for(let c of coes){
  180. if( c.amount&&c.amount!=1){
  181. let operator = c.operator;
  182. if(c.operator =="*"){
  183. operator = "X";
  184. }
  185. stringList.push(c.coeType+operator+c.amount);
  186. }
  187. }
  188. return stringList.join(",");
  189. }
  190. function calculateTimes(ass){
  191. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  192. let r = false;
  193. if(times<0){
  194. r=true;
  195. times=times*-1;
  196. }
  197. if(ass.carryBit=='四舍五入'){
  198. times = _.round(times,ass.decimal);
  199. }else if (ass.carryBit=='进一'){
  200. times =_.ceil(times,ass.decimal);
  201. }
  202. if(r){
  203. times=times*-1;
  204. }
  205. return scMathUtil.roundTo(times,-6);
  206. }
  207. function calculateQuantityByCoes(quantity,coeList,glj){
  208. let coeQuantity = quantity;
  209. if(coeList.length>1){
  210. for(let i=0;i<coeList.length-1;i++){
  211. coeQuantity = everyCoe(coeQuantity,coeList[i],glj);
  212. }
  213. }
  214. return scMathUtil.roundTo(coeQuantity,-6);
  215. }
  216. function everyCoe(quantity,coe,glj) {
  217. let coeQuantity = quantity;
  218. if(coe.isAdjust==1){
  219. for(let i=0;i<coe.coes.length;i++){
  220. if(coe.coes[i].gljCode==glj.code){//if(coe.coes[i].coeType=='单个工料机'&&coe.coes[i].gljCode==glj.code)
  221. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  222. } else if(coe.coes[i].coeType=='定额'){
  223. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  224. }else if(coe.coes[i].coeType==getGLJTypeByID(glj.type)){
  225. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  226. }
  227. }
  228. }
  229. return scMathUtil.roundTo(coeQuantity,-6);
  230. }
  231. function calculateQuantityByCustomerCoes(quantify,coe,glj) {
  232. let rationAmount = coe.coes[0].amount;
  233. if(_.every(coe.coes,'amount',rationAmount)){
  234. return getCalculateResult(quantify, coe.coes[0])
  235. }else {
  236. for(let i=1;i<coe.coes.length;i++){
  237. if(coe.coes[i].coeType.search(getGLJTypeByID(glj.type))!=-1){
  238. return getCalculateResult(quantify,coe.coes[i])
  239. }
  240. }
  241. }
  242. return quantify
  243. }
  244. function getCalculateResult(quantify,c) {
  245. let q = quantify;
  246. switch (c.operator){
  247. case '+' :
  248. q = q + c.amount;
  249. break;
  250. case '-' :
  251. q = q - c.amount;
  252. break;
  253. case '*' :
  254. q = q * c.amount;
  255. break;
  256. case '/' :
  257. q = q / c.amount;
  258. break;
  259. case '=' :
  260. q = c.amount;
  261. break;
  262. }
  263. return q;
  264. }
  265. function getGLJTypeByID(id) {
  266. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  267. let topTypeId = glj_type_object.getTopParentIdByItemId(id);
  268. let type = glj_type_object.getItemById(topTypeId);
  269. if(type!=undefined){
  270. return type.fullName;
  271. }else {
  272. return '';
  273. }
  274. }