glj_calculate_facade.js 11 KB

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