glj_calculate_facade.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 = quantity;
  150. if(glj._doc.hasOwnProperty('customQuantity')&&(glj.customQuantity != null||glj.customQuantity != '')){
  151. displayQuantity = glj.customQuantity;
  152. }
  153. displayQuantity = displayQuantity&&displayQuantity!=""?parseFloat(displayQuantity):0;
  154. adjustState.push({index:stateSeq.add,content:'添'+glj.code+'量'+ displayQuantity});
  155. }
  156. // to do
  157. //标准附注条件调整 + 自定义乘系数
  158. if(0==index){
  159. for(let i=0;i<coeList.length;i++){
  160. if(coeList[i].isAdjust==1){
  161. if(i==coeList.length-1){
  162. adjustState.push({index:stateSeq.cusCoe,content:getContent(coeList[i].coes)});//自定义乘系数要去掉倍数为1的
  163. }else {
  164. adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].content});//coeList[i].content
  165. }
  166. }
  167. }
  168. }
  169. //自定义消耗量
  170. if(glj._doc.createType!='add'&&glj._doc.hasOwnProperty('customQuantity')){
  171. if(glj.customQuantity!==null&&glj.customQuantity!=""){
  172. adjustState.push({index:stateSeq.cusQuantity,content:glj.code+'量'+parseFloat(glj.customQuantity)});
  173. }
  174. }
  175. //市场单价调整
  176. if(glj._doc.hasOwnProperty('marketPriceAdjust')&&glj.marketPriceAdjust&&glj.marketPriceAdjust!=0){
  177. //0101005价66.00
  178. adjustState.push({index:stateSeq.adjMak,content:glj.code+'价'+glj.marketPriceAdjust});
  179. }
  180. return adjustState;
  181. }
  182. function getContent(coes) {
  183. let stringList=[];
  184. let temAmount = null;
  185. let theSame = true;
  186. for(let t of coes){
  187. if(temAmount == null){
  188. temAmount = t.amount;
  189. }else if(temAmount != t.amount){
  190. theSame = false;
  191. break;
  192. }
  193. }
  194. for(let c of coes){
  195. if( c.amount&&c.amount!=1){
  196. let operator = c.operator;
  197. if(c.operator =="*"){
  198. operator = "X";
  199. }
  200. if(theSame == true && c.coeType == "定额"){
  201. stringList.push(c.coeType+operator+c.amount);
  202. break;
  203. }else
  204. if(theSame == false && c.coeType != "定额"){
  205. stringList.push(c.coeType+operator+c.amount);
  206. }
  207. }
  208. }
  209. return stringList.join(",");
  210. }
  211. function calculateTimes(ass){
  212. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  213. let r = false;
  214. if(times<0){
  215. r=true;
  216. times=times*-1;
  217. }
  218. if(ass.carryBit=='四舍五入'){
  219. times = _.round(times,ass.decimal);
  220. }else if (ass.carryBit=='进一'){
  221. times =_.ceil(times,ass.decimal);
  222. }
  223. if(r){
  224. times=times*-1;
  225. }
  226. return scMathUtil.roundTo(times,-6);
  227. }
  228. function calculateQuantityByCoes(quantity,coeList,glj){
  229. let coeQuantity = quantity;
  230. if(coeList.length>1){
  231. for(let i=0;i<coeList.length-1;i++){
  232. coeQuantity = everyCoe(coeQuantity,coeList[i],glj);
  233. }
  234. }
  235. return scMathUtil.roundTo(coeQuantity,-6);
  236. }
  237. function everyCoe(quantity,coe,glj) {
  238. let coeQuantity = quantity;
  239. if(coe.isAdjust==1){
  240. for(let i=0;i<coe.coes.length;i++){
  241. if(coe.coes[i].gljCode==glj.code){//if(coe.coes[i].coeType=='单个工料机'&&coe.coes[i].gljCode==glj.code)
  242. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  243. } else if(coe.coes[i].coeType=='定额'){
  244. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  245. }else if(coe.coes[i].coeType==getGLJTypeByID(glj.type)){
  246. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i]);
  247. }
  248. }
  249. }
  250. return scMathUtil.roundTo(coeQuantity,-6);
  251. }
  252. function calculateQuantityByCustomerCoes(quantify,coe,glj) {
  253. let rationAmount = coe.coes[0].amount;
  254. if(_.every(coe.coes,'amount',rationAmount)){
  255. return getCalculateResult(quantify, coe.coes[0])
  256. }else {
  257. for(let i=1;i<coe.coes.length;i++){
  258. if(coe.coes[i].coeType.search(getGLJTypeByID(glj.type))!=-1){
  259. return getCalculateResult(quantify,coe.coes[i])
  260. }
  261. }
  262. }
  263. return quantify
  264. }
  265. function getCalculateResult(quantify,c) {
  266. let q = quantify;
  267. switch (c.operator){
  268. case '+' :
  269. q = q + c.amount;
  270. break;
  271. case '-' :
  272. q = q - c.amount;
  273. break;
  274. case '*' :
  275. q = q * c.amount;
  276. break;
  277. case '/' :
  278. q = q / c.amount;
  279. break;
  280. case '=' :
  281. q = c.amount;
  282. break;
  283. }
  284. return q;
  285. }
  286. function getGLJTypeByID(id) {
  287. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  288. let topTypeId = glj_type_object.getTopParentIdByItemId(id);
  289. let type = glj_type_object.getItemById(topTypeId);
  290. if(type!=undefined){
  291. return type.fullName;
  292. }else {
  293. return '';
  294. }
  295. }