glj_calculate_facade.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 =calculateAss(quantity,assList,glj);
  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 = calculateQuantityByCustomerCoes(quantity,customerCoe,glj);
  106. }
  107. result.doc.quantity =scMathUtil.roundToString(quantity,decimal);
  108. }
  109. generateAdjustState(glj,coeList,adjustState,index,result.doc.quantity);
  110. return result;
  111. }catch (err){
  112. throw err;
  113. }
  114. }
  115. function calculateAss(quantity,assList,glj) {
  116. for(let i=0;i<assList.length;i++){
  117. if(assList[i].assRation){
  118. let assglj = _.find(assList[i].assRation.rationGljList,function (aglj) {
  119. return aglj.gljId == glj.GLJID
  120. })
  121. if(assglj){
  122. let calQuantity = assglj.consumeAmt*assList[i].times;
  123. quantity += calQuantity
  124. }
  125. }
  126. }
  127. return scMathUtil.roundTo(quantity,-6);
  128. }
  129. function generateAdjustState(glj,coeList,adjustState,index,quantity) {
  130. //替换工料机 and 添加工料机
  131. if(glj._doc.createType=='replace'&&glj.rcode!=glj.code){
  132. adjustState.push({index:stateSeq.replace,content:glj.rcode+'换'+glj.code});
  133. }else if(glj._doc.createType=='add'){
  134. adjustState.push({index:stateSeq.add,content:'添'+glj.code+'量'+parseFloat(quantity)});
  135. }
  136. // to do
  137. //标准附注条件调整 + 自定义乘系数
  138. if(0==index){
  139. for(let i=0;i<coeList.length;i++){
  140. if(coeList[i].isAdjust==1){
  141. if(i==coeList.length-1){
  142. adjustState.push({index:stateSeq.cusCoe,content:coeList[i].content});
  143. }else {
  144. adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].content});
  145. }
  146. }
  147. }
  148. }
  149. //自定义消耗量
  150. if(glj._doc.createType!='add'&&glj._doc.hasOwnProperty('customQuantity')){
  151. if(glj.customQuantity!==null&&glj.customQuantity!=""){
  152. adjustState.push({index:stateSeq.cusQuantity,content:glj.code+'量'+parseFloat(glj.customQuantity)});
  153. }
  154. }
  155. //市场单价调整
  156. if(glj._doc.hasOwnProperty('marketPriceAdjust')&&glj.marketPriceAdjust&&glj.marketPriceAdjust!=0){
  157. //0101005价66.00
  158. adjustState.push({index:stateSeq.adjMak,content:glj.code+'价'+glj.marketPriceAdjust});
  159. }
  160. return adjustState;
  161. }
  162. function calculateTimes(ass){
  163. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  164. let r = false;
  165. if(times<0){
  166. r=true;
  167. times=times*-1;
  168. }
  169. if(ass.carryBit=='四舍五入'){
  170. times = _.round(times,ass.decimal);
  171. }else if (ass.carryBit=='进一'){
  172. times =_.ceil(times,ass.decimal);
  173. }
  174. if(r){
  175. times=times*-1;
  176. }
  177. return scMathUtil.roundTo(times,-6);
  178. }
  179. function calculateQuantityByCoes(quantity,coeList,glj){
  180. let coeQuantity = quantity;
  181. if(coeList.length>1){
  182. for(let i=0;i<coeList.length-1;i++){
  183. coeQuantity = everyCoe(coeQuantity,coeList[i],glj);
  184. }
  185. }
  186. return scMathUtil.roundTo(coeQuantity,-6);
  187. }
  188. function everyCoe(quantity,coe,glj) {
  189. let coeQuantity = quantity;
  190. if(coe.isAdjust==1){
  191. for(let i=0;i<coe.coes.length;i++){
  192. if(coe.coes[i].coeType=='单个'&&coe.coes[i].gljCode==glj.code){
  193. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  194. } else if(coe.coes[i].coeType=='定额'){
  195. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  196. }else if(coe.coes[i].coeType==getGLJTypeByID(glj.type)){
  197. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  198. }
  199. }
  200. }
  201. return scMathUtil.roundTo(coeQuantity,-6);
  202. }
  203. function calculateQuantityByCustomerCoes(quantify,coe,glj) {
  204. let rationAmount = coe.coes[0].amount;
  205. if(_.every(coe.coes,'amount',rationAmount)){
  206. return getCalculateResult(quantify, coe.coes[0])
  207. }else {
  208. for(let i=1;i<coe.coes.length;i++){
  209. if(coe.coes[i].coeType.search(getGLJTypeByID(glj.type))!=-1){
  210. return getCalculateResult(quantify,coe.coes[i])
  211. }
  212. }
  213. }
  214. return quantify
  215. }
  216. function getCalculateResult(quantify,c) {
  217. let q = quantify;
  218. switch (c.operator){
  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 = q / c.amount;
  230. break;
  231. case '=' :
  232. q = c.amount;
  233. break;
  234. }
  235. return q;
  236. }
  237. function getGLJTypeByID(id) {
  238. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  239. let topTypeId = glj_type_object.getTopParentIdByItemId(id);
  240. let type = glj_type_object.getItemById(topTypeId);
  241. if(type!=undefined){
  242. return type.fullName;
  243. }else {
  244. return '';
  245. }
  246. }