glj_calculate_facade.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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],impactRation,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,ration,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);
  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) {
  126. //替换工料机 and 添加工料机
  127. // to do
  128. //标准附注条件调整 + 自定义乘系数
  129. for(let i=0;i<coeList.length;i++){
  130. if(coeList[i].isAdjust==1){
  131. if(i==coeList.length-1){
  132. adjustState.push({index:stateSeq.cusCoe,content:coeList[i].content});
  133. }else {
  134. adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].content});
  135. }
  136. }
  137. }
  138. //自定义消耗量
  139. if(glj._doc.hasOwnProperty('customQuantity')){
  140. if(glj.customQuantity!==null){
  141. adjustState.push({index:stateSeq.cusQuantity,content:glj.code+'量'+glj.customQuantity});
  142. }
  143. }
  144. //市场单价调整
  145. if(glj._doc.hasOwnProperty('marketPriceAdjust')&&glj.marketPriceAdjust&&glj.marketPriceAdjust!=0){
  146. //0101005价66.00
  147. adjustState.push({index:stateSeq.adjMak,content:glj.code+'价'+glj.marketPriceAdjust});
  148. }
  149. return adjustState;
  150. }
  151. function calculateTimes(ass){
  152. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  153. let r = false;
  154. if(times<0){
  155. r=true;
  156. times=times*-1;
  157. }
  158. if(ass.carryBit=='四舍五入'){
  159. times = _.round(times,ass.decimal);
  160. }else if (ass.carryBit=='进一'){
  161. times =_.ceil(times,ass.decimal);
  162. }
  163. if(r){
  164. times=times*-1;
  165. }
  166. return times;
  167. }
  168. function calculateQuantityByCoes(quantity,coeList,glj){
  169. let coeQuantity = quantity;
  170. if(coeList.length>1){
  171. for(let i=0;i<coeList.length-1;i++){
  172. coeQuantity = everyCoe(coeQuantity,coeList[i],glj);
  173. }
  174. }
  175. return coeQuantity;
  176. }
  177. function everyCoe(quantity,coe,glj) {
  178. let coeQuantity = quantity;
  179. if(coe.isAdjust==1){
  180. for(let i=0;i<coe.coes.length;i++){
  181. if(coe.coes[i].coeType=='单个'&&coe.coes[i].gljCode==glj.code){
  182. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  183. } else if(coe.coes[i].coeType=='定额'){
  184. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  185. }else if(coe.coes[i].coeType==getGLJTypeByID(glj.type)){
  186. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  187. }
  188. }
  189. }
  190. return coeQuantity;
  191. }
  192. function calculateQuantityByCustomerCoes(quantify,coe,glj) {
  193. let rationAmount = coe.coes[0].amount;
  194. if(_.every(coe.coes,'amount',rationAmount)){
  195. return getCalculateResult(quantify, coe.coes[0])
  196. }else {
  197. for(let i=1;i<coe.coes.length;i++){
  198. if(coe.coes[i].coeType.search(getGLJTypeByID(glj.type))!=-1){
  199. return getCalculateResult(quantify,coe.coes[i])
  200. }
  201. }
  202. }
  203. return quantify
  204. }
  205. function getCalculateResult(quantify,c) {
  206. let q = quantify;
  207. switch (c.operator){
  208. case '+' :
  209. q = q + c.amount;
  210. break;
  211. case '-' :
  212. q = q - c.amount;
  213. break;
  214. case '*' :
  215. q = q * c.amount;
  216. break;
  217. case '/' :
  218. q = q / c.amount;
  219. break;
  220. case '=' :
  221. q = c.amount;
  222. break;
  223. }
  224. return q;
  225. }
  226. function getGLJTypeByID(id) {
  227. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  228. let topTypeId = glj_type_object.getTopParentIdByItemId(id);
  229. let type = glj_type_object.getItemById(topTypeId);
  230. if(type!=undefined){
  231. return type.fullName;
  232. }else {
  233. return '';
  234. }
  235. }