glj_calculate_facade.js 12 KB

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