glj_calculate_facade.js 7.2 KB

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