glj_calculate_facade.js 8.2 KB

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