glj_calculate_facade.js 9.2 KB

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