glj_calculate_facade.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. module.exports={
  11. calculateQuantity:calculateQuantity
  12. }
  13. //辅助定额调整、替换工料机、标准附注条件调整、添加工料机、自定义消耗量(包括删除工料机)、自定义乘系数、市场单价调整
  14. let stateSeq ={
  15. ass:1,
  16. replase:2,
  17. coe:3,
  18. add:4,
  19. cusQuantity:5,
  20. cusCoe:6,
  21. adjMak:7
  22. }
  23. async function calculateQuantity(query,isMarkPriceAjust){
  24. try {
  25. let result ={
  26. glj_result:[],
  27. rationID:query.rationID
  28. };
  29. let impactRation = await ration.findOne({projectID:query.projectID,ID:query.rationID,deleteInfo:null});
  30. let gljList = await ration_glj.find(query)//{projectID:query.projectID,rationID:query.rationID}
  31. let coeList = await ration_coe.find({projectID:query.projectID,rationID:query.rationID}).sort('seq').exec();
  32. let assList=[];
  33. let assRation = null;
  34. let adjustState=[];
  35. if(impactRation._doc.hasOwnProperty("rationAssList")&&impactRation.rationAssList.length>0){
  36. for(let i=0;i<impactRation.rationAssList.length;i++){
  37. let times = calculateTimes(impactRation.rationAssList[i]);
  38. if(times!=0){
  39. assRation = await std_ration_lib_ration_items.findOne({rationRepId:impactRation.libID,code:impactRation.rationAssList[i].assistCode});
  40. assList.push({times:times,assRation:assRation})
  41. adjustState.push({index:stateSeq.ass,content:impactRation.rationAssList[i].name+" "+impactRation.rationAssList[i].actualValue+" : +"+impactRation.rationAssList[i].assistCode+"x"+times});
  42. }
  43. }
  44. }
  45. for(let i =0;i<gljList.length;i++ ){
  46. let r = await calculateQuantityPerGLJ(gljList[i],impactRation,coeList,assList,adjustState,isMarkPriceAjust);
  47. result.glj_result.push(r);
  48. }
  49. if(isMarkPriceAjust==null){
  50. await ration_glj.bulkWrite(generateUpdateTasks(result.glj_result));
  51. }
  52. adjustState= _.sortByOrder(adjustState, ['index'], ['asc']);
  53. adjustState=_.map(adjustState, _.property('content'));
  54. let adjustStateString = adjustState.join(';');
  55. await ration.update({projectID:query.projectID,ID:query.rationID,deleteInfo: null},{adjustState:adjustStateString});
  56. result.adjustState=adjustStateString;
  57. return result;
  58. }catch (err){
  59. console.log(err);
  60. throw err;
  61. }
  62. }
  63. function generateUpdateTasks(result) {
  64. let tasks = [];
  65. for(let i =0;i<result.length;i++){
  66. let task= {
  67. updateOne: {
  68. filter: result[i].query,
  69. update: result[i].doc
  70. }
  71. }
  72. tasks.push(task);
  73. }
  74. return tasks;
  75. }
  76. async function calculateQuantityPerGLJ(glj,ration,coeList,assList,adjustState,isMarkPriceAjust) {
  77. let quantity = glj.quantity;
  78. let result={
  79. query:{
  80. ID:glj.ID,
  81. projectID:glj.projectID
  82. },
  83. doc:{
  84. quantity: quantity
  85. }
  86. };
  87. try {
  88. if(isMarkPriceAjust==null){
  89. if(!glj._doc.hasOwnProperty('customQuantity')||glj.customQuantity==null){
  90. quantity =glj.rationItemQuantity;
  91. quantity =calculateAss(quantity,assList,glj);
  92. quantity = calculateQuantityByCoes(quantity,coeList,glj);
  93. }else {
  94. quantity = glj.customQuantity;
  95. result.doc.customQuantity = glj.customQuantity;
  96. }
  97. let customerCoe = _.last(coeList);
  98. if(customerCoe.isAdjust==1){
  99. quantity = calculateQuantityByCustomerCoes(quantity,customerCoe,glj);
  100. }
  101. result.doc.quantity =_.round(quantity,3);
  102. }
  103. generateAdjustState(glj,coeList,adjustState);
  104. return result;
  105. }catch (err){
  106. throw err;
  107. }
  108. }
  109. function calculateAss(quantity,assList,glj) {
  110. for(let i=0;i<assList.length;i++){
  111. let assglj = _.find(assList[i].assRation.rationGljList,function (aglj) {
  112. return aglj.gljId == glj.GLJID
  113. })
  114. if(assglj){
  115. let calQuantity = assglj.consumeAmt*assList[i].times;
  116. quantity += calQuantity
  117. }
  118. }
  119. return quantity;
  120. }
  121. function generateAdjustState(glj,coeList,adjustState) {
  122. //替换工料机 and 添加工料机
  123. // to do
  124. //标准附注条件调整 + 自定义乘系数
  125. for(let i=0;i<coeList.length;i++){
  126. if(coeList[i].isAdjust==1){
  127. if(i==coeList.length-1){
  128. adjustState.push({index:stateSeq.cusCoe,content:coeList[i].content});
  129. }else {
  130. adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].content});
  131. }
  132. }
  133. }
  134. //自定义消耗量
  135. if(glj._doc.hasOwnProperty('customQuantity')){
  136. if(glj.customQuantity!==null){
  137. adjustState.push({index:stateSeq.cusQuantity,content:glj.code+'量'+glj.customQuantity});
  138. }
  139. }
  140. //市场单价调整
  141. if(glj._doc.hasOwnProperty('marketPriceAdjust')&&glj.marketPriceAdjust&&glj.marketPriceAdjust!=0){
  142. //0101005价66.00
  143. adjustState.push({index:stateSeq.adjMak,content:glj.code+'价'+glj.marketPriceAdjust});
  144. }
  145. return adjustState;
  146. }
  147. function calculateTimes(ass){
  148. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  149. let r = false;
  150. if(times<0){
  151. r=true;
  152. times=times*-1;
  153. }
  154. if(ass.carryBit=='四舍五入'){
  155. times = _.round(times,ass.decimal);
  156. }else if (ass.carryBit=='进一'){
  157. times =_.ceil(times,ass.decimal);
  158. }
  159. if(r){
  160. times=times*-1;
  161. }
  162. return times;
  163. }
  164. function calculateQuantityByCoes(quantity,coeList,glj){
  165. let coeQuantity = quantity;
  166. if(coeList.length>1){
  167. for(let i=0;i<coeList.length-1;i++){
  168. coeQuantity = everyCoe(coeQuantity,coeList[i],glj);
  169. }
  170. }
  171. return coeQuantity;
  172. }
  173. function everyCoe(quantity,coe,glj) {
  174. let coeQuantity = quantity;
  175. if(coe.isAdjust==1){
  176. for(let i=0;i<coe.coes.length;i++){
  177. if(coe.coes[i].coeType=='单个'&&coe.coes[i].gljCode==glj.code){
  178. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  179. } else if(coe.coes[i].coeType=='定额'){
  180. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  181. }else if(coe.coes[i].coeType==glj.gljDistType){
  182. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  183. }
  184. }
  185. }
  186. return coeQuantity;
  187. }
  188. function calculateQuantityByCustomerCoes(quantify,coe,glj) {
  189. let rationAmount = coe.coes[0].amount;
  190. if(_.every(coe.coes,'amount',rationAmount)){
  191. return getCalculateResult(quantify, coe.coes[0])
  192. }else {
  193. for(let i=1;i<coe.coes.length;i++){
  194. if(coe.coes[i].coeType.search(glj.gljDistType)!=-1){
  195. return getCalculateResult(quantify,coe.coes[i])
  196. }
  197. }
  198. }
  199. return quantify
  200. }
  201. function getCalculateResult(quantify,c) {
  202. let q = quantify;
  203. switch (c.operator){
  204. case '+' :
  205. q = q + c.amount;
  206. break;
  207. case '-' :
  208. q = q - c.amount;
  209. break;
  210. case '*' :
  211. q = q * c.amount;
  212. break;
  213. case '/' :
  214. q = q / c.amount;
  215. break;
  216. case '=' :
  217. q = c.amount;
  218. break;
  219. }
  220. return q;
  221. }