glj_calculate_facade.js 9.3 KB

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