glj_calculate_facade.js 11 KB

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