glj_calculate_facade.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /**
  2. * Created by chen on 2017/7/12.
  3. */
  4. module.exports={
  5. calculateQuantity:calculateQuantity,
  6. calculateQuantityFromDivide:calculateQuantityFromDivide,
  7. getGLJTypeByID:getGLJTypeByID
  8. }
  9. let mongoose = require('mongoose');
  10. let _=require("lodash");
  11. let ration_glj = mongoose.model('ration_glj');
  12. let ration = mongoose.model('ration');
  13. let ration_coe = mongoose.model('ration_coe');
  14. const commonFacade = require('../../main/facade/common_facade');
  15. let mixRatioModel = mongoose.model('mix_ratio');
  16. let std_ration_lib_ration_items = mongoose.model('std_ration_lib_ration_items');
  17. let divideModel = mongoose.model("divide_setting");
  18. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  19. let glj_type_util = require('../../../public/cache/std_glj_type_util');
  20. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  21. let decimal_facade = require('../../main/facade/decimal_facade');
  22. let gljUtil = require('../../../public/gljUtil');
  23. const common_util = require('../../../public/common_util');
  24. //辅助定额调整、稳定土配合比、替换工料机、标准附注条件调整、添加工料机、自定义消耗量(包括删除工料机)、自定义乘系数、市场单价调整
  25. let stateSeq ={
  26. ass:1,
  27. proportion: 2,
  28. replace:3,
  29. coe:4,
  30. add:5,
  31. cusQuantity:6,
  32. cusCoe:7,
  33. adjMak:8
  34. };
  35. //自定义乘系数与定额工料机类型映射表
  36. let coeTypeMap = {
  37. "人工":1,
  38. "材料":2,
  39. "机械":3,
  40. "施工机具":3,
  41. "主材":4,
  42. "设备":5
  43. };
  44. async function handleAndGetAssList(impactRation,adjustState){
  45. let assList=[], assRation = null;
  46. if(impactRation && impactRation.rationAssList.length>0){
  47. prepareAss(impactRation.rationAssList);
  48. let temTimes = [];
  49. let thirdRationCodes=[];
  50. for(let i=0;i<impactRation.rationAssList.length;i++){
  51. let times = calculateTimes(impactRation.rationAssList[i]);
  52. if(times!=0){
  53. let thirdRationCode = impactRation.rationAssList[i].thirdRationCode;
  54. if(thirdRationCode && thirdRationCode !=''){
  55. temTimes.push(times);
  56. thirdRationCodes.push(thirdRationCode)
  57. }
  58. assRation = await std_ration_lib_ration_items.findOne({rationRepId:impactRation.libID,code:impactRation.rationAssList[i].assistCode});
  59. assList.push({times:times,assRation:assRation});
  60. adjustState.push({index:stateSeq.ass,content:impactRation.rationAssList[i].name+" "+impactRation.rationAssList[i].actualValue+" : +"+impactRation.rationAssList[i].assistCode+"x"+times});
  61. }
  62. }
  63. if(temTimes.length == 2 &&thirdRationCodes[0] == thirdRationCodes[1] ){ //说明有第三定额
  64. let times_t = temTimes[0] * temTimes[1];
  65. let tration = await std_ration_lib_ration_items.findOne({rationRepId:impactRation.libID,code:thirdRationCodes[0]});
  66. if(tration){
  67. assList.push({times:times_t,assRation:tration});
  68. adjustState.push({index:stateSeq.ass,content:"+"+thirdRationCodes[0]+"x"+times_t});
  69. }
  70. }
  71. }
  72. return assList;
  73. }
  74. async function calculateQuantityFromDivide(ID,rationID){
  75. let tasks=[],udatas=[];
  76. let divide_setting = await divideModel.findOne({ID:ID}).lean();
  77. let impactRation = null,gljList=[],coeList=[],assList=[], adjustState=[],mixRatioMap = {};
  78. for(let r of divide_setting.divideList){
  79. if(r.ID == rationID){
  80. impactRation = r;
  81. break;
  82. }
  83. }
  84. if(impactRation == null) return null;
  85. for(let rg of divide_setting.ration_gljs){
  86. if(rg.rationID == rationID) gljList.push(rg);
  87. }
  88. for(let co of divide_setting.ration_coes){
  89. if(co.rationID == rationID) coeList.push(co);
  90. }
  91. assList = await handleAndGetAssList(impactRation,adjustState);
  92. for(let glj of gljList){//先把混凝土,砂浆,配合比有自定义消耗的挑出来
  93. if(gljUtil.isConcreteType(glj.type)) await getMixRatioMap(glj,gljList,coeList,assList,mixRatioMap);
  94. }
  95. for(let i =0;i<gljList.length;i++ ){
  96. let r = await calculateQuantityPerGLJ(gljList[i],gljList,coeList,assList,adjustState,mixRatioMap,null);
  97. if(quantityUpdateCheck(gljList[i],r) == true){
  98. let ndoc={};
  99. for(let key in r.doc){
  100. ndoc["ration_gljs.$."+key] = r.doc[key]
  101. }
  102. udatas.push({ID:gljList[i].ID,model:"ration_gljs",type:"update",doc:r.doc});
  103. tasks.push({updateOne: {filter: {"ID": ID,"ration_gljs.ID":gljList[i].ID}, update: ndoc}});
  104. }
  105. }
  106. let newName = generateRationName(impactRation,gljList);
  107. udatas.push({ID:impactRation.ID,type:"update",doc:{name:newName}});
  108. tasks.push({updateOne: {filter: {"ID": ID,"divideList.ID":impactRation.ID}, update: {"divideList.$.name":newName}}});
  109. return [tasks,udatas];
  110. }
  111. async function calculateQuantity(query,noNeedCal=null,refreshRationName = false){
  112. try {
  113. let result ={
  114. glj_result:[],
  115. rationID:query.rationID
  116. };
  117. let impactRation = await ration.findOne({ID:query.rationID});
  118. let gljList = await ration_glj.find(query);//{projectID:query.projectID,rationID:query.rationID}
  119. let coeList = await ration_coe.find({rationID:query.rationID}).sort('seq').exec();
  120. let assList=[], adjustState=[],mixRatioMap = {};
  121. if(!impactRation){//如果定额不存在或者已删除,返回空
  122. return null;
  123. }
  124. if(impactRation._doc.hasOwnProperty("rationAssList")&&impactRation.rationAssList.length>0){
  125. assList = await handleAndGetAssList(impactRation,adjustState);
  126. }
  127. // 稳定土调整状态
  128. const proportionStr = gljList.filter(glj => glj.rationProportion).map(glj => glj.adjustProportion || 0).join(':');
  129. if (proportionStr) {
  130. adjustState.push({ index: stateSeq.proportion, content: proportionStr });
  131. }
  132. for(let glj of gljList){//先把混凝土,砂浆,配合比有自定义消耗的挑出来
  133. if(gljUtil.isConcreteType(glj.type)) await getMixRatioMap(glj,gljList,coeList,assList,mixRatioMap);
  134. }
  135. gljList = gljUtil.sortRationGLJ(gljList);
  136. for(let i =0;i<gljList.length;i++ ){
  137. let r = await calculateQuantityPerGLJ(gljList[i],gljList,coeList,assList,adjustState,mixRatioMap,noNeedCal);
  138. if(quantityUpdateCheck(gljList[i],r) == true) result.glj_result.push(r);
  139. }
  140. if(noNeedCal==null && result.glj_result.length > 0){
  141. await ration_glj.bulkWrite(generateUpdateTasks(result.glj_result));
  142. }
  143. adjustState= _.sortByOrder(adjustState, ['index'], ['asc']);
  144. adjustState=_.map(adjustState, _.property('content'));
  145. let adjustStateString = adjustState.join(';');
  146. let setData = {adjustState:adjustStateString};
  147. if(refreshRationName == true){//需要更新定额名称
  148. let newName = generateRationName(impactRation,gljList);
  149. setData.name = newName;
  150. result.rationName = newName;
  151. }
  152. await ration.update({ID:query.rationID},setData);
  153. result.adjustState=adjustStateString;
  154. return result;
  155. }catch (err){
  156. console.log(err);
  157. throw err;
  158. }
  159. }
  160. function quantityUpdateCheck(glj,r) {//检查,有改变的才更新
  161. for(let key in r.doc){
  162. if(glj[key] != r.doc[key]) return true
  163. }
  164. return false
  165. }
  166. function generateRationName(ration,gljList) {
  167. let caption = ration.caption ? ration.caption:ration.name;
  168. let replaceList = [];
  169. if(ration.rationAssList && ration.rationAssList.length > 0){//这里要处理第三定额和多辅助定额的情况
  170. let isThird = ration.rationAssList.length == 2 && ration.rationAssList[0].thirdRationCode&&ration.rationAssList[0].thirdRationCode!='';//说明有第三定额的情况
  171. let adjustMatch = "",notAdjust="";
  172. for(let ass of ration.rationAssList){
  173. let tem = "";
  174. if( ass.isAdjust == 1 && ass.actualValue != null && ass.actualValue != undefined ){
  175. tem = ass.actualValue;
  176. adjustMatch = tem;
  177. }else {
  178. tem = ass.stdValue;
  179. notAdjust = tem;
  180. }
  181. if(isThird) replaceList.push(tem);
  182. }
  183. if(replaceList.length == 0){
  184. adjustMatch!=""?replaceList.push(adjustMatch):replaceList.push(notAdjust);
  185. }
  186. for(let r of replaceList){
  187. caption = caption.replace(/%s/i,r);
  188. }
  189. }
  190. // 更新定额名称中显示的稳定土配合比
  191. const proportionStr = gljList.filter(glj => glj.rationProportion).map(glj => glj.adjustProportion || 0).join(':');
  192. if (proportionStr) {
  193. caption = caption.replace(/%p/i, proportionStr);
  194. }
  195. let reNameList = [];
  196. for(let g of gljList){
  197. //glj._doc.createType=='replace'&&glj.rcode!=glj.code
  198. if(g.createType=='replace'&&g.rcode!=g.code){ //是替换工料机
  199. let reName = g.name;
  200. if(!_.isEmpty(g.specs)) reName = reName + ' '+g.specs;
  201. reNameList.push(reName);
  202. }
  203. }
  204. if(reNameList.length > 0){
  205. let reNameString = reNameList.join(" ");
  206. caption = caption + " 换为【"+ reNameString + "】";
  207. }
  208. return caption;
  209. }
  210. function generateUpdateTasks(result) {
  211. let tasks = [];
  212. for(let i =0;i<result.length;i++){
  213. let task= {
  214. updateOne: {
  215. filter: result[i].query,
  216. update: result[i].doc
  217. }
  218. };
  219. tasks.push(task);
  220. }
  221. return tasks;
  222. }
  223. async function calcWhenNoCustomQuantiyt(decimal,glj,gljList,coeList,assList) {
  224. let quantity = glj.rationItemQuantity ? scMathUtil.roundTo(parseFloat(glj.rationItemQuantity),-decimal):0;
  225. quantity =scMathUtil.roundTo(await calculateAss(quantity,assList,glj),-decimal);
  226. if (glj.rationProportion && common_util.isDef(glj.adjustProportion)) {
  227. const proportionRate = glj.adjustProportion / glj.rationProportion;
  228. quantity *= proportionRate;
  229. }
  230. quantity = calculateQuantityByCoes(quantity,coeList,glj,gljList,decimal);
  231. return quantity;
  232. }
  233. async function calculateQuantityPerGLJ(glj,gljList,coeList,assList,adjustState,mixRatioMap,noNeedCal) {
  234. let decimalObject = await decimal_facade.getProjectDecimal(glj.projectID);
  235. let decimal = (decimalObject&&decimalObject.glj&&decimalObject.glj.quantity)?decimalObject.glj.quantity:3;
  236. let quantity = scMathUtil.roundTo(parseFloat(glj.quantity),-decimal);
  237. let result={
  238. query:{
  239. ID:glj.ID
  240. },
  241. doc:{
  242. quantity: quantity
  243. }
  244. };
  245. try {
  246. if(noNeedCal==null){//计算顺序:辅助定额, 稳定土配合比、附注条件,自定义消耗(如果有就不用前计算两项),自定义乘系数
  247. if(noCustomQuantiyt(glj)){
  248. quantity = await calcWhenNoCustomQuantiyt(decimal,glj,gljList,coeList,assList);
  249. let mIndex = gljUtil.getIndex(glj);
  250. if(mixRatioMap[mIndex]){//组成物
  251. quantity = calcQuantityForMix(quantity,mixRatioMap[mIndex],decimal);
  252. }
  253. }else {//对于有自定义消耗量的,并且是混凝土,浆砂,配合比 这三种类型,其组成物的消耗量要跟据自定义消耗量做相应的换算
  254. quantity = glj.customQuantity;
  255. result.doc.customQuantity = glj.customQuantity;
  256. }
  257. let customerCoe = _.last(coeList);
  258. if(customerCoe&&customerCoe.isAdjust==1){
  259. quantity = scMathUtil.roundToString(quantity,decimal);
  260. quantity = calculateQuantityByCustomerCoes(quantity,customerCoe,glj,decimal);
  261. }
  262. if(quantity < 0) quantity = 0;
  263. result.doc.quantity =scMathUtil.roundToString(quantity,decimal);
  264. //2019-01-03 需求修改中间过程的价格不参与计算
  265. //glj.quantity = quantity;//这里保存中间过程计算出来的消耗量,后面处理“+*”操作符时要用到
  266. }
  267. generateAdjustState(glj,coeList,adjustState,gljList,result.doc.quantity);
  268. return result;
  269. }catch (err){
  270. throw err;
  271. }
  272. }
  273. function calcQuantityForMix(quantity,plist,decimal) {
  274. for(let pglj of plist){
  275. let consumption = scMathUtil.roundForObj(pglj.consumption,decimal);
  276. let customQuantity = scMathUtil.roundForObj(pglj.customQuantity,decimal);
  277. let temQuantity = scMathUtil.roundForObj(consumption * pglj.assCoeQuantity,6);
  278. let pcustomQuantity = scMathUtil.roundForObj(consumption * customQuantity,6);
  279. quantity = scMathUtil.roundForObj(quantity - temQuantity,6);
  280. quantity = scMathUtil.roundForObj(quantity + pcustomQuantity,decimal);
  281. }
  282. return quantity;
  283. }
  284. async function getMixRatioMap(glj,gljList,coeList,assList,mixRatioMap) {//生成组成物对应的父工料机的映射表
  285. let decimalObject = await decimal_facade.getProjectDecimal(glj.projectID);
  286. let decimal = (decimalObject&&decimalObject.glj&&decimalObject.glj.quantity)?decimalObject.glj.quantity:3;
  287. let assCoeQuantity = await calcWhenNoCustomQuantiyt(decimal,glj,gljList,coeList,assList);//计算要用父工料机经过辅助子目换算后的量
  288. let unitPriceFileId = await commonFacade.getUnitPriceFileId(glj.projectID);
  289. let connect_key = gljUtil.getIndex(glj);
  290. let mixList = await mixRatioModel.find({"unit_price_file_id":unitPriceFileId,'connect_key':connect_key});
  291. for(let m of mixList){
  292. if(glj.createType == 'replace'&& glj.mIndexObj){//如果是替换过的工料机,要造一条加的数据去补回下面的减数据
  293. //与下面的减相对应,如果有没被引用子组成物工料机,会少一条加的数据,这样,那条没被引用的消耗量会被减为0
  294. addToMixMap(m,mixRatioMap,assCoeQuantity,0); //addToMixMap(m,mixRatioMap,glj.rationItemQuantity,0); -- 2019-04-15 和下面的减相对做修改
  295. }
  296. if(!noCustomQuantiyt(glj)) {//有自定义消耗量才做正常的计算处理
  297. addToMixMap(m,mixRatioMap,glj.customQuantity,assCoeQuantity);
  298. }
  299. }
  300. if(glj.createType == 'replace' && glj.mIndexObj){//如果这条是替换过的混凝土,原来的组成物的消耗量还要减去替换前的混凝土得到的消耗量
  301. let t_con_key = gljUtil.getIndex(glj.mIndexObj);
  302. let tmixList = await mixRatioModel.find({"unit_price_file_id":unitPriceFileId,'connect_key':t_con_key});
  303. for(let tm of tmixList ){
  304. addToMixMap(tm,mixRatioMap,0,assCoeQuantity); //addToMixMap(tm,mixRatioMap,0,glj.rationItemQuantity);
  305. // -- 2019-04-15 修改bugs 这里把glj.rationItemQuantity 改成assCoeQuantity。 定额下组成物的消耗量要减去父工料机经过辅助定额或者子目换算后的对应的增加量
  306. }
  307. }
  308. function addToMixMap(m,map,cust,ass,isReplace = false) {
  309. let mkey = gljUtil.getIndex(m);
  310. let pObj = {customQuantity:cust,assCoeQuantity:ass,consumption:m.consumption,isReplace:isReplace};
  311. if(map[mkey]){
  312. map[mkey].push(pObj);
  313. }else {
  314. map[mkey] = [pObj];
  315. }
  316. }
  317. }
  318. function noCustomQuantiyt(glj) {//是否有自定义消耗量
  319. return glj.customQuantity===undefined||glj.customQuantity===null||glj.customQuantity==""
  320. }
  321. async function calculateAss(quantity,assList,glj) {
  322. for(let i=0;i<assList.length;i++){
  323. if(assList[i].assRation){
  324. let assglj = null;
  325. for(let aglj of assList[i].assRation.rationGljList){
  326. if(glj.createType == 'replace'){//如果工料机是替换过的,要用原始的编码来匹配
  327. let std_glj = await std_glj_lib_gljList_model.findOne({'ID':aglj.gljId});
  328. if(glj.rcode == std_glj.code){
  329. assglj = aglj;
  330. break;
  331. }
  332. }else if(aglj.gljId == glj.GLJID){
  333. assglj = aglj;
  334. break;
  335. }
  336. }
  337. if(assglj){
  338. let calQuantity = assglj.consumeAmt*assList[i].times;
  339. quantity += calQuantity
  340. }
  341. }
  342. }
  343. return scMathUtil.roundTo(quantity,-6);
  344. }
  345. function generateAdjustState(glj,coeList,adjustState,gljList,quantity) {
  346. //替换工料机 and 添加工料机
  347. if(glj.createType=='replace'&&glj.rcode!=glj.code){
  348. adjustState.push({index:stateSeq.replace,content:glj.rcode+'换'+glj.code});
  349. }else if(glj.createType=='add'){
  350. let displayQuantity = quantity;
  351. if(glj.hasOwnProperty('customQuantity')&&(glj.customQuantity != null||glj.customQuantity != '')){
  352. displayQuantity = glj.customQuantity;
  353. }
  354. displayQuantity = displayQuantity&&displayQuantity!=""?parseFloat(displayQuantity):0;
  355. adjustState.push({index:stateSeq.add,content:'添'+glj.code+'量'+ displayQuantity,type:"添"+glj.code});
  356. }
  357. // to do
  358. //标准附注条件调整 + 自定义乘系数
  359. if(_.last(gljList).ID == glj.ID){//最后一个工料机的时候才生成,生成一次就可以了
  360. for(let i=0;i<coeList.length;i++){
  361. if(coeList[i].isAdjust==1){
  362. if(i==coeList.length-1){
  363. adjustState.push({index:stateSeq.cusCoe,content:getContent(coeList[i].coes)});//自定义乘系数要去掉倍数为1的
  364. }else {
  365. if(coeList[i].select_code && coeList[i].select_code!=""){
  366. _.remove(adjustState,{'content':coeList[i].original_code+'换'+coeList[i].select_code});//去掉替换工料机自动生成的调整状态
  367. adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].original_code+'换'+coeList[i].select_code});
  368. }
  369. for(let c of coeList[i].coes){
  370. if(c.coeType=='单个工料机') _.remove(adjustState,{'type':"添"+c.gljCode});//如果是单个工料机子目换算类型自动添加的,去掉前面手动生成的调整状态
  371. if(c.coeType=='替换人材机') _.remove(adjustState,{'content':c.gljCode+'换'+c.replaceCode});//如果是替换人材机子目换算类型自动添加的,去掉前面手动生成的调整状态
  372. }
  373. if(coeList[i].content) adjustState.push({index:stateSeq.coe,content:"调 : "+coeList[i].content});//coeList[i].content
  374. }
  375. }
  376. }
  377. }
  378. //自定义消耗量
  379. if(glj.createType!='add'&&glj.hasOwnProperty('customQuantity')){
  380. if(glj.customQuantity!==null&&glj.customQuantity!=""){
  381. adjustState.push({index:stateSeq.cusQuantity,content:glj.code+'量'+parseFloat(glj.customQuantity)});
  382. }
  383. }
  384. //市场单价调整
  385. if(glj.hasOwnProperty('marketPriceAdjust')&&glj.marketPriceAdjust&&glj.marketPriceAdjust!=0){
  386. //0101005价66.00
  387. adjustState.push({index:stateSeq.adjMak,content:glj.code+'价'+glj.marketPriceAdjust});
  388. }
  389. return adjustState;
  390. }
  391. function getContent(coes) {
  392. let stringList=[];
  393. let temAmount = null;
  394. let theSame = true;
  395. for(let t of coes){
  396. if(temAmount == null){
  397. temAmount = t.amount;
  398. }else if(temAmount != t.amount){
  399. theSame = false;
  400. break;
  401. }
  402. }
  403. for(let c of coes){
  404. if( c.amount&&c.amount!=1){
  405. let operator = c.operator;
  406. if(c.operator =="*"){
  407. operator = "X";
  408. }
  409. if(theSame == true && c.coeType == "定额"){
  410. stringList.push(c.coeType+operator+c.amount);
  411. break;
  412. }else
  413. if(theSame == false && c.coeType != "定额"){
  414. stringList.push(c.coeType+operator+c.amount);
  415. }
  416. }
  417. }
  418. return stringList.join(",");
  419. }
  420. function prepareAss(assList) {//处理辅助定额,支持多个辅助定额的情况
  421. for(let a of assList){
  422. if(a.groupList && a.groupList.length > 1){//组里有多个定额的情况
  423. let newList = _.sortByAll(a.groupList,[function(item){
  424. return parseFloat(item.param)
  425. }]);//先按参数排序
  426. let pre = 0;
  427. for(let n of newList){
  428. if(a.actualValue > pre && a.actualValue <= parseFloat(n.param)){//落在中间,则用组里的这条定额
  429. a._doc.param = n.param;
  430. a._doc.paramName = n.paramName;
  431. a._doc.assistCode = n.assistCode;
  432. break;
  433. }
  434. pre = parseFloat(n.param);
  435. }
  436. }
  437. }
  438. }
  439. function calculateTimes(ass){
  440. if(ass.isAdjust == 0) return 0;//打勾辅助定额才计算
  441. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  442. let r = false;
  443. if(times<0){
  444. r=true;
  445. times=times*-1;
  446. }
  447. if(ass.carryBit=='四舍五入'){
  448. times = _.round(times,ass.decimal);
  449. }else if (ass.carryBit=='进一'){
  450. times =_.ceil(times,ass.decimal);
  451. }else if(ass.carryBit == '舍一'){
  452. times = _.floor(times,ass.decimal);
  453. }
  454. if(r){
  455. times=times*-1;
  456. }
  457. return scMathUtil.roundTo(times,-6);
  458. }
  459. function calculateQuantityByCoes(quantity,coeList,glj,gljList,decimal){
  460. let coeQuantity = quantity;
  461. if(coeList.length>1){
  462. for(let i=0;i<coeList.length-1;i++){
  463. coeQuantity = everyCoe(coeQuantity,coeList[i],glj,gljList,decimal);
  464. }
  465. }
  466. return scMathUtil.roundTo(coeQuantity,-6);
  467. }
  468. function everyCoe(quantity,coe,glj,gljList,decimal) {
  469. let coeQuantity = quantity;
  470. if(coe.isAdjust==1){
  471. for(let i=0;i<coe.coes.length;i++){
  472. if(coe.coes[i].coeType=='单个工料机' &&coe.coes[i].gljCode==glj.code){//if(coe.coes[i].coeType=='单个工料机'&&coe.coes[i].gljCode==glj.code)
  473. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  474. }else if(coe.coes[i].coeType== "替换人材机" && glj.rcode == coe.coes[i].gljCode && glj.code == coe.coes[i].replaceCode){
  475. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  476. }else if(coe.coes[i].coeType== "所选人材机" && glj.rcode == coe.original_code && glj.code == coe.select_code ){
  477. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  478. } else if(coe.coes[i].coeType=='定额'){
  479. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  480. }else if(coeTypeMap[coe.coes[i].coeType]==getRootGLJType(glj.type).ID){
  481. coeQuantity = getCalculateResult(coeQuantity,coe.coes[i],coe,gljList,decimal);
  482. }
  483. }
  484. }
  485. return scMathUtil.roundTo(coeQuantity,-6);
  486. }
  487. function calculateQuantityByCustomerCoes(quantify,coe,glj) {
  488. let rationAmount = coe.coes[0].amount;
  489. if(_.every(coe.coes,'amount',rationAmount)){
  490. return getCalculateResult(quantify, coe.coes[0])
  491. }else {
  492. for(let i=1;i<coe.coes.length;i++){
  493. if(coeTypeMap[coe.coes[i].coeType]==getRootGLJType(glj.type).ID){
  494. return getCalculateResult(quantify,coe.coes[i])
  495. }
  496. }
  497. }
  498. return quantify
  499. }
  500. function getCoeSelectedGLJ(gljList,rcode,code) {
  501. if(gljList&& code && code !=""){
  502. let o_glj = _.find(gljList,{'rcode':rcode,'code':code});
  503. return o_glj;
  504. }
  505. return null;
  506. }
  507. function getCalculateResult(quantify,c,coe,gljList,decimal) {
  508. let q = quantify;
  509. let o_glj = null;
  510. switch (c.operator){
  511. case '+' :
  512. q = q + c.amount;
  513. break;
  514. case '-' :
  515. q = q - c.amount;
  516. break;
  517. case '*' :
  518. q = q * c.amount;
  519. break;
  520. case '/' :
  521. q = q / c.amount;
  522. break;
  523. case '+*' :
  524. o_glj = getCoeSelectedGLJ(gljList,coe.original_code,coe.select_code);
  525. if(o_glj){
  526. q = q + c.amount * scMathUtil.roundForObj(o_glj.rationItemQuantity,decimal);
  527. }
  528. break;
  529. case '-*' :
  530. o_glj = getCoeSelectedGLJ(gljList,coe.original_code,coe.select_code);
  531. if(o_glj){
  532. q = q - c.amount * scMathUtil.roundForObj(o_glj.rationItemQuantity,decimal);
  533. }
  534. break;
  535. case '=' :
  536. q = c.amount;
  537. break;
  538. }
  539. return q;
  540. }
  541. function getRootGLJType(id){
  542. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  543. let topTypeId = glj_type_object.getTopParentIdByItemId(id);
  544. let type = glj_type_object.getItemById(topTypeId);
  545. return type;
  546. }
  547. function getGLJTypeByID(id) {
  548. let type = getRootGLJType(id);
  549. if(type!=undefined){
  550. return type.fullName;
  551. }else {
  552. return '';
  553. }
  554. }