glj_calculate_facade.js 8.7 KB

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