glj_calculate_facade.js 7.5 KB

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