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