glj_calculate_facade.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 = [],
  46. assRation = null;
  47. if (impactRation && impactRation.rationAssList.length > 0) {
  48. prepareAss(impactRation.rationAssList);
  49. let temTimes = [];
  50. let thirdRationCodes = [];
  51. for (let i = 0; i < impactRation.rationAssList.length; i++) {
  52. let times = calculateTimes(impactRation.rationAssList[i]);
  53. if (times != 0) {
  54. let thirdRationCode = impactRation.rationAssList[i].thirdRationCode;
  55. if (thirdRationCode && thirdRationCode != "") {
  56. temTimes.push(times);
  57. thirdRationCodes.push(thirdRationCode);
  58. }
  59. assRation = await std_ration_lib_ration_items.findOne({
  60. rationRepId: impactRation.libID,
  61. code: impactRation.rationAssList[i].assistCode,
  62. });
  63. assList.push({ times: times, assRation: assRation });
  64. adjustState.push({
  65. index: stateSeq.ass,
  66. content:
  67. impactRation.rationAssList[i].name +
  68. " " +
  69. impactRation.rationAssList[i].actualValue +
  70. " : +" +
  71. impactRation.rationAssList[i].assistCode +
  72. "x" +
  73. times,
  74. });
  75. }
  76. }
  77. if (temTimes.length == 2 && thirdRationCodes[0] == thirdRationCodes[1]) {
  78. //说明有第三定额
  79. let times_t = temTimes[0] * temTimes[1];
  80. let tration = await std_ration_lib_ration_items.findOne({
  81. rationRepId: impactRation.libID,
  82. code: thirdRationCodes[0],
  83. });
  84. if (tration) {
  85. assList.push({ times: times_t, assRation: tration });
  86. adjustState.push({
  87. index: stateSeq.ass,
  88. content: "+" + thirdRationCodes[0] + "x" + times_t,
  89. });
  90. }
  91. }
  92. }
  93. return assList;
  94. }
  95. async function calculateQuantityFromDivide(ID, rationID) {
  96. let tasks = [],
  97. udatas = [];
  98. let divide_setting = await divideModel.findOne({ ID: ID }).lean();
  99. let impactRation = null,
  100. gljList = [],
  101. coeList = [],
  102. assList = [],
  103. adjustState = [],
  104. mixRatioMap = {};
  105. for (let r of divide_setting.divideList) {
  106. if (r.ID == rationID) {
  107. impactRation = r;
  108. break;
  109. }
  110. }
  111. if (impactRation == null) return null;
  112. for (let rg of divide_setting.ration_gljs) {
  113. if (rg.rationID == rationID) gljList.push(rg);
  114. }
  115. for (let co of divide_setting.ration_coes) {
  116. if (co.rationID == rationID) coeList.push(co);
  117. }
  118. assList = await handleAndGetAssList(impactRation, adjustState);
  119. for (let glj of gljList) {
  120. //先把混凝土,砂浆,配合比有自定义消耗的挑出来
  121. if (gljUtil.isConcreteType(glj.type))
  122. await getMixRatioMap(glj, gljList, coeList, assList, mixRatioMap);
  123. }
  124. for (let i = 0; i < gljList.length; i++) {
  125. let r = await calculateQuantityPerGLJ(
  126. gljList[i],
  127. gljList,
  128. coeList,
  129. assList,
  130. adjustState,
  131. mixRatioMap,
  132. null
  133. );
  134. if (quantityUpdateCheck(gljList[i], r) == true) {
  135. let ndoc = {};
  136. for (let key in r.doc) {
  137. ndoc["ration_gljs.$." + key] = r.doc[key];
  138. }
  139. udatas.push({
  140. ID: gljList[i].ID,
  141. model: "ration_gljs",
  142. type: "update",
  143. doc: r.doc,
  144. });
  145. tasks.push({
  146. updateOne: {
  147. filter: { ID: ID, "ration_gljs.ID": gljList[i].ID },
  148. update: ndoc,
  149. },
  150. });
  151. }
  152. }
  153. let newName = generateRationName(impactRation, gljList);
  154. udatas.push({ ID: impactRation.ID, type: "update", doc: { name: newName } });
  155. tasks.push({
  156. updateOne: {
  157. filter: { ID: ID, "divideList.ID": impactRation.ID },
  158. update: { "divideList.$.name": newName },
  159. },
  160. });
  161. return [tasks, udatas];
  162. }
  163. async function calculateQuantity(
  164. query,
  165. noNeedCal = null,
  166. refreshRationName = false
  167. ) {
  168. try {
  169. let result = {
  170. glj_result: [],
  171. rationID: query.rationID,
  172. };
  173. let impactRation = await ration.findOne({ ID: query.rationID });
  174. let gljList = await ration_glj.find(query); //{projectID:query.projectID,rationID:query.rationID}
  175. let coeList = await ration_coe
  176. .find({ rationID: query.rationID })
  177. .sort("seq")
  178. .exec();
  179. let assList = [],
  180. adjustState = [],
  181. mixRatioMap = {};
  182. if (!impactRation) {
  183. //如果定额不存在或者已删除,返回空
  184. return null;
  185. }
  186. if (
  187. impactRation._doc.hasOwnProperty("rationAssList") &&
  188. impactRation.rationAssList.length > 0
  189. ) {
  190. assList = await handleAndGetAssList(impactRation, adjustState);
  191. }
  192. // 稳定土调整状态
  193. const proportionStr = gljList
  194. .filter((glj) => glj.rationProportion)
  195. .map((glj) => glj.adjustProportion || 0)
  196. .join(":");
  197. if (proportionStr) {
  198. adjustState.push({ index: stateSeq.proportion, content: proportionStr });
  199. }
  200. for (let glj of gljList) {
  201. //先把混凝土,砂浆,配合比有自定义消耗的挑出来
  202. if (gljUtil.isConcreteType(glj.type))
  203. await getMixRatioMap(glj, gljList, coeList, assList, mixRatioMap);
  204. }
  205. gljList = gljUtil.sortRationGLJ(gljList);
  206. for (let i = 0; i < gljList.length; i++) {
  207. let r = await calculateQuantityPerGLJ(
  208. gljList[i],
  209. gljList,
  210. coeList,
  211. assList,
  212. adjustState,
  213. mixRatioMap,
  214. noNeedCal
  215. );
  216. if (quantityUpdateCheck(gljList[i], r) == true) result.glj_result.push(r);
  217. }
  218. if (noNeedCal == null && result.glj_result.length > 0) {
  219. await ration_glj.bulkWrite(generateUpdateTasks(result.glj_result));
  220. }
  221. adjustState = _.sortByOrder(adjustState, ["index"], ["asc"]);
  222. adjustState = _.map(adjustState, _.property("content"));
  223. let adjustStateString = adjustState.join(";");
  224. let setData = { adjustState: adjustStateString };
  225. if (refreshRationName == true) {
  226. //需要更新定额名称
  227. let newName = generateRationName(impactRation, gljList);
  228. setData.name = newName;
  229. result.rationName = newName;
  230. }
  231. await ration.update({ ID: query.rationID }, setData);
  232. result.adjustState = adjustStateString;
  233. return result;
  234. } catch (err) {
  235. console.log(err);
  236. throw err;
  237. }
  238. }
  239. function quantityUpdateCheck(glj, r) {
  240. //检查,有改变的才更新
  241. for (let key in r.doc) {
  242. if (glj[key] != r.doc[key]) return true;
  243. }
  244. return false;
  245. }
  246. function generateRationName(ration, gljList) {
  247. let caption = ration.caption ? ration.caption : ration.name;
  248. let replaceList = [];
  249. if (ration.rationAssList && ration.rationAssList.length > 0) {
  250. //这里要处理第三定额和多辅助定额的情况
  251. let isThird =
  252. ration.rationAssList.length == 2 &&
  253. ration.rationAssList[0].thirdRationCode &&
  254. ration.rationAssList[0].thirdRationCode != ""; //说明有第三定额的情况
  255. let adjustMatch = "",
  256. notAdjust = "";
  257. for (let ass of ration.rationAssList) {
  258. let tem = "";
  259. if (
  260. ass.isAdjust == 1 &&
  261. ass.actualValue != null &&
  262. ass.actualValue != undefined
  263. ) {
  264. tem = ass.actualValue;
  265. adjustMatch = tem;
  266. } else {
  267. tem = ass.stdValue;
  268. notAdjust = tem;
  269. }
  270. if (isThird) replaceList.push(tem);
  271. }
  272. if (replaceList.length == 0) {
  273. adjustMatch != ""
  274. ? replaceList.push(adjustMatch)
  275. : replaceList.push(notAdjust);
  276. }
  277. for (let r of replaceList) {
  278. caption = caption.replace(/%s/i, r);
  279. }
  280. }
  281. // 更新定额名称中显示的稳定土配合比
  282. const proportionStr = gljList
  283. .filter((glj) => glj.rationProportion)
  284. .map((glj) => glj.adjustProportion || 0)
  285. .join(":");
  286. if (proportionStr) {
  287. caption = caption.replace(/%p/i, proportionStr);
  288. }
  289. let reNameList = [];
  290. for (let g of gljList) {
  291. //glj._doc.createType=='replace'&&glj.rcode!=glj.code
  292. if (g.createType == "replace" && g.rcode != g.code) {
  293. //是替换工料机
  294. let reName = g.name;
  295. if (!_.isEmpty(g.specs)) reName = reName + " " + g.specs;
  296. reNameList.push(reName);
  297. }
  298. }
  299. if (reNameList.length > 0) {
  300. let reNameString = reNameList.join(" ");
  301. caption = caption + " 换为【" + reNameString + "】";
  302. }
  303. return caption;
  304. }
  305. function generateUpdateTasks(result) {
  306. let tasks = [];
  307. for (let i = 0; i < result.length; i++) {
  308. let task = {
  309. updateOne: {
  310. filter: result[i].query,
  311. update: result[i].doc,
  312. },
  313. };
  314. tasks.push(task);
  315. }
  316. return tasks;
  317. }
  318. async function calcWhenNoCustomQuantiyt(
  319. decimal,
  320. glj,
  321. gljList,
  322. coeList,
  323. assList
  324. ) {
  325. let quantity = glj.rationItemQuantity
  326. ? scMathUtil.roundTo(parseFloat(glj.rationItemQuantity), -decimal)
  327. : 0;
  328. quantity = scMathUtil.roundTo(
  329. await calculateAss(quantity, assList, glj),
  330. -decimal
  331. );
  332. if (glj.rationProportion && common_util.isDef(glj.adjustProportion)) {
  333. const proportionRate = glj.adjustProportion / glj.rationProportion;
  334. quantity *= proportionRate;
  335. }
  336. quantity = calculateQuantityByCoes(quantity, coeList, glj, gljList, decimal);
  337. return quantity;
  338. }
  339. async function calculateQuantityPerGLJ(
  340. glj,
  341. gljList,
  342. coeList,
  343. assList,
  344. adjustState,
  345. mixRatioMap,
  346. noNeedCal
  347. ) {
  348. let decimalObject = await decimal_facade.getProjectDecimal(glj.projectID);
  349. let decimal =
  350. decimalObject && decimalObject.glj && decimalObject.glj.quantity
  351. ? decimalObject.glj.quantity
  352. : 3;
  353. let quantity = scMathUtil.roundTo(parseFloat(glj.quantity), -decimal);
  354. let result = {
  355. query: {
  356. ID: glj.ID,
  357. },
  358. doc: {
  359. quantity: quantity,
  360. },
  361. };
  362. try {
  363. if (noNeedCal == null) {
  364. //计算顺序:辅助定额, 稳定土配合比、附注条件,自定义消耗(如果有就不用前计算两项),自定义乘系数
  365. if (noCustomQuantiyt(glj)) {
  366. quantity = await calcWhenNoCustomQuantiyt(
  367. decimal,
  368. glj,
  369. gljList,
  370. coeList,
  371. assList
  372. );
  373. let mIndex = gljUtil.getIndex(glj);
  374. if (mixRatioMap[mIndex]) {
  375. //组成物
  376. quantity = calcQuantityForMix(quantity, mixRatioMap[mIndex], decimal);
  377. }
  378. if (quantity < 0) quantity = 0;
  379. } else {
  380. //对于有自定义消耗量的,并且是混凝土,浆砂,配合比 这三种类型,其组成物的消耗量要跟据自定义消耗量做相应的换算
  381. quantity = glj.customQuantity;
  382. result.doc.customQuantity = glj.customQuantity;
  383. }
  384. let customerCoe = _.last(coeList);
  385. if (customerCoe && customerCoe.isAdjust == 1) {
  386. quantity = scMathUtil.roundToString(quantity, decimal);
  387. quantity = calculateQuantityByCustomerCoes(
  388. quantity,
  389. customerCoe,
  390. glj,
  391. decimal
  392. );
  393. }
  394. result.doc.quantity = scMathUtil.roundToString(quantity, decimal);
  395. //2019-01-03 需求修改中间过程的价格不参与计算
  396. //glj.quantity = quantity;//这里保存中间过程计算出来的消耗量,后面处理“+*”操作符时要用到
  397. }
  398. generateAdjustState(
  399. glj,
  400. coeList,
  401. adjustState,
  402. gljList,
  403. result.doc.quantity
  404. );
  405. return result;
  406. } catch (err) {
  407. throw err;
  408. }
  409. }
  410. function calcQuantityForMix(quantity, plist, decimal) {
  411. for (let pglj of plist) {
  412. let consumption = scMathUtil.roundForObj(pglj.consumption, decimal);
  413. let customQuantity = scMathUtil.roundForObj(pglj.customQuantity, decimal);
  414. let temQuantity = scMathUtil.roundForObj(
  415. consumption * pglj.assCoeQuantity,
  416. 6
  417. );
  418. let pcustomQuantity = scMathUtil.roundForObj(
  419. consumption * customQuantity,
  420. 6
  421. );
  422. quantity = scMathUtil.roundForObj(quantity - temQuantity, 6);
  423. quantity = scMathUtil.roundForObj(quantity + pcustomQuantity, decimal);
  424. }
  425. return quantity;
  426. }
  427. async function getMixRatioMap(glj, gljList, coeList, assList, mixRatioMap) {
  428. //生成组成物对应的父工料机的映射表
  429. let decimalObject = await decimal_facade.getProjectDecimal(glj.projectID);
  430. let decimal =
  431. decimalObject && decimalObject.glj && decimalObject.glj.quantity
  432. ? decimalObject.glj.quantity
  433. : 3;
  434. let assCoeQuantity = await calcWhenNoCustomQuantiyt(
  435. decimal,
  436. glj,
  437. gljList,
  438. coeList,
  439. assList
  440. ); //计算要用父工料机经过辅助子目换算后的量
  441. let unitPriceFileId = await commonFacade.getUnitPriceFileId(glj.projectID);
  442. let connect_key = gljUtil.getIndex(glj);
  443. let mixList = await mixRatioModel.find({
  444. unit_price_file_id: unitPriceFileId,
  445. connect_key: connect_key,
  446. });
  447. for (let m of mixList) {
  448. if (glj.createType == "replace" && glj.mIndexObj) {
  449. //如果是替换过的工料机,要造一条加的数据去补回下面的减数据
  450. //与下面的减相对应,如果有没被引用子组成物工料机,会少一条加的数据,这样,那条没被引用的消耗量会被减为0
  451. addToMixMap(m, mixRatioMap, assCoeQuantity, 0); //addToMixMap(m,mixRatioMap,glj.rationItemQuantity,0); -- 2019-04-15 和下面的减相对做修改
  452. }
  453. if (!noCustomQuantiyt(glj)) {
  454. //有自定义消耗量才做正常的计算处理
  455. addToMixMap(m, mixRatioMap, glj.customQuantity, assCoeQuantity);
  456. }
  457. }
  458. if (glj.createType == "replace" && glj.mIndexObj) {
  459. //如果这条是替换过的混凝土,原来的组成物的消耗量还要减去替换前的混凝土得到的消耗量
  460. let t_con_key = gljUtil.getIndex(glj.mIndexObj);
  461. let tmixList = await mixRatioModel.find({
  462. unit_price_file_id: unitPriceFileId,
  463. connect_key: t_con_key,
  464. });
  465. for (let tm of tmixList) {
  466. addToMixMap(tm, mixRatioMap, 0, assCoeQuantity); //addToMixMap(tm,mixRatioMap,0,glj.rationItemQuantity);
  467. // -- 2019-04-15 修改bugs 这里把glj.rationItemQuantity 改成assCoeQuantity。 定额下组成物的消耗量要减去父工料机经过辅助定额或者子目换算后的对应的增加量
  468. }
  469. }
  470. function addToMixMap(m, map, cust, ass, isReplace = false) {
  471. let mkey = gljUtil.getIndex(m);
  472. let pObj = {
  473. customQuantity: cust,
  474. assCoeQuantity: ass,
  475. consumption: m.consumption,
  476. isReplace: isReplace,
  477. };
  478. if (map[mkey]) {
  479. map[mkey].push(pObj);
  480. } else {
  481. map[mkey] = [pObj];
  482. }
  483. }
  484. }
  485. function noCustomQuantiyt(glj) {
  486. //是否有自定义消耗量
  487. return (
  488. glj.customQuantity === undefined ||
  489. glj.customQuantity === null ||
  490. glj.customQuantity == ""
  491. );
  492. }
  493. async function calculateAss(quantity, assList, glj) {
  494. for (let i = 0; i < assList.length; i++) {
  495. if (assList[i].assRation) {
  496. let assglj = null;
  497. for (let aglj of assList[i].assRation.rationGljList) {
  498. if (glj.createType == "replace") {
  499. //如果工料机是替换过的,要用原始的编码来匹配
  500. let std_glj = await std_glj_lib_gljList_model.findOne({
  501. ID: aglj.gljId,
  502. });
  503. if (glj.rcode == std_glj.code) {
  504. assglj = aglj;
  505. break;
  506. }
  507. } else if (aglj.gljId == glj.GLJID) {
  508. assglj = aglj;
  509. break;
  510. }
  511. }
  512. if (assglj) {
  513. let calQuantity = assglj.consumeAmt * assList[i].times;
  514. quantity += calQuantity;
  515. }
  516. }
  517. }
  518. return scMathUtil.roundTo(quantity, -6);
  519. }
  520. function generateAdjustState(glj, coeList, adjustState, gljList, quantity) {
  521. //替换工料机 and 添加工料机
  522. if (glj.createType == "replace" && glj.rcode != glj.code) {
  523. adjustState.push({
  524. index: stateSeq.replace,
  525. content: glj.rcode + "换" + glj.code,
  526. });
  527. } else if (glj.createType == "add") {
  528. let displayQuantity = quantity;
  529. if (
  530. glj.hasOwnProperty("customQuantity") &&
  531. (glj.customQuantity != null || glj.customQuantity != "")
  532. ) {
  533. displayQuantity = glj.customQuantity;
  534. }
  535. displayQuantity =
  536. displayQuantity && displayQuantity != ""
  537. ? parseFloat(displayQuantity)
  538. : 0;
  539. adjustState.push({
  540. index: stateSeq.add,
  541. content: "添" + glj.code + "量" + displayQuantity,
  542. type: "添" + glj.code,
  543. });
  544. }
  545. // to do
  546. //标准附注条件调整 + 自定义乘系数
  547. if (_.last(gljList).ID == glj.ID) {
  548. //最后一个工料机的时候才生成,生成一次就可以了
  549. for (let i = 0; i < coeList.length; i++) {
  550. if (coeList[i].isAdjust == 1) {
  551. if (i == coeList.length - 1) {
  552. adjustState.push({
  553. index: stateSeq.cusCoe,
  554. content: getContent(coeList[i].coes),
  555. }); //自定义乘系数要去掉倍数为1的
  556. } else {
  557. if (coeList[i].select_code && coeList[i].select_code != "") {
  558. _.remove(adjustState, {
  559. content: coeList[i].original_code + "换" + coeList[i].select_code,
  560. }); //去掉替换工料机自动生成的调整状态
  561. adjustState.push({
  562. index: stateSeq.coe,
  563. content:
  564. "调 : " +
  565. coeList[i].original_code +
  566. "换" +
  567. coeList[i].select_code,
  568. });
  569. }
  570. for (let c of coeList[i].coes) {
  571. if (c.coeType == "单个工料机")
  572. _.remove(adjustState, { type: "添" + c.gljCode }); //如果是单个工料机子目换算类型自动添加的,去掉前面手动生成的调整状态
  573. if (c.coeType == "替换人材机")
  574. _.remove(adjustState, {
  575. content: c.gljCode + "换" + c.replaceCode,
  576. }); //如果是替换人材机子目换算类型自动添加的,去掉前面手动生成的调整状态
  577. }
  578. if (coeList[i].content)
  579. adjustState.push({
  580. index: stateSeq.coe,
  581. content: "调 : " + coeList[i].content,
  582. }); //coeList[i].content
  583. }
  584. }
  585. }
  586. }
  587. //自定义消耗量
  588. if (glj.createType != "add" && glj._doc.hasOwnProperty("customQuantity")) {
  589. if (glj.customQuantity !== null && glj.customQuantity != "") {
  590. adjustState.push({
  591. index: stateSeq.cusQuantity,
  592. content: glj.code + "量" + parseFloat(glj.customQuantity),
  593. });
  594. }
  595. }
  596. //市场单价调整
  597. if (
  598. glj.hasOwnProperty("marketPriceAdjust") &&
  599. glj.marketPriceAdjust &&
  600. glj.marketPriceAdjust != 0
  601. ) {
  602. //0101005价66.00
  603. adjustState.push({
  604. index: stateSeq.adjMak,
  605. content: glj.code + "价" + glj.marketPriceAdjust,
  606. });
  607. }
  608. return adjustState;
  609. }
  610. function getContent(coes) {
  611. let stringList = [];
  612. let temAmount = null;
  613. let theSame = true;
  614. for (let t of coes) {
  615. if (temAmount == null) {
  616. temAmount = t.amount;
  617. } else if (temAmount != t.amount) {
  618. theSame = false;
  619. break;
  620. }
  621. }
  622. for (let c of coes) {
  623. if (c.amount && c.amount != 1) {
  624. let operator = c.operator;
  625. if (c.operator == "*") {
  626. operator = "X";
  627. }
  628. if (theSame == true && c.coeType == "定额") {
  629. stringList.push(c.coeType + operator + c.amount);
  630. break;
  631. } else if (theSame == false && c.coeType != "定额") {
  632. stringList.push(c.coeType + operator + c.amount);
  633. }
  634. }
  635. }
  636. return stringList.join(",");
  637. }
  638. function prepareAss(assList) {
  639. //处理辅助定额,支持多个辅助定额的情况
  640. for (let a of assList) {
  641. if (a.groupList && a.groupList.length > 1) {
  642. //组里有多个定额的情况
  643. let newList = _.sortByAll(a.groupList, [
  644. function (item) {
  645. return parseFloat(item.param);
  646. },
  647. ]); //先按参数排序
  648. let pre = 0;
  649. for (let n of newList) {
  650. if (a.actualValue > pre && a.actualValue <= parseFloat(n.param)) {
  651. //落在中间,则用组里的这条定额
  652. a._doc.param = n.param;
  653. a._doc.paramName = n.paramName;
  654. a._doc.assistCode = n.assistCode;
  655. break;
  656. }
  657. pre = parseFloat(n.param);
  658. }
  659. }
  660. }
  661. }
  662. function calculateTimes(ass) {
  663. if (ass.isAdjust == 0) return 0; //打勾辅助定额才计算
  664. let times = (ass.actualValue - ass.stdValue) / ass.stepValue;
  665. let r = false;
  666. if (times < 0) {
  667. r = true;
  668. times = times * -1;
  669. }
  670. if (ass.carryBit == "四舍五入") {
  671. times = _.round(times, ass.decimal);
  672. } else if (ass.carryBit == "进一") {
  673. times = _.ceil(times, ass.decimal);
  674. } else if (ass.carryBit == "舍一") {
  675. times = _.floor(times, ass.decimal);
  676. }
  677. if (r) {
  678. times = times * -1;
  679. }
  680. return scMathUtil.roundTo(times, -6);
  681. }
  682. function calculateQuantityByCoes(quantity, coeList, glj, gljList, decimal) {
  683. let coeQuantity = quantity;
  684. if (coeList.length > 1) {
  685. for (let i = 0; i < coeList.length - 1; i++) {
  686. coeQuantity = everyCoe(coeQuantity, coeList[i], glj, gljList, decimal);
  687. }
  688. }
  689. return scMathUtil.roundTo(coeQuantity, -6);
  690. }
  691. function everyCoe(quantity, coe, glj, gljList, decimal) {
  692. let coeQuantity = quantity;
  693. if (coe.isAdjust == 1) {
  694. for (let i = 0; i < coe.coes.length; i++) {
  695. if (
  696. coe.coes[i].coeType == "单个工料机" &&
  697. coe.coes[i].gljCode == glj.code
  698. ) {
  699. //if(coe.coes[i].coeType=='单个工料机'&&coe.coes[i].gljCode==glj.code)
  700. coeQuantity = getCalculateResult(
  701. coeQuantity,
  702. coe.coes[i],
  703. coe,
  704. gljList,
  705. decimal
  706. );
  707. } else if (
  708. coe.coes[i].coeType == "替换人材机" &&
  709. glj.rcode == coe.coes[i].gljCode &&
  710. glj.code == coe.coes[i].replaceCode
  711. ) {
  712. coeQuantity = getCalculateResult(
  713. coeQuantity,
  714. coe.coes[i],
  715. coe,
  716. gljList,
  717. decimal
  718. );
  719. } else if (
  720. coe.coes[i].coeType == "所选人材机" &&
  721. glj.rcode == coe.original_code &&
  722. glj.code == coe.select_code
  723. ) {
  724. coeQuantity = getCalculateResult(
  725. coeQuantity,
  726. coe.coes[i],
  727. coe,
  728. gljList,
  729. decimal
  730. );
  731. } else if (coe.coes[i].coeType == "定额") {
  732. coeQuantity = getCalculateResult(
  733. coeQuantity,
  734. coe.coes[i],
  735. coe,
  736. gljList,
  737. decimal
  738. );
  739. } else if (
  740. coeTypeMap[coe.coes[i].coeType] == getRootGLJType(glj.type).ID
  741. ) {
  742. coeQuantity = getCalculateResult(
  743. coeQuantity,
  744. coe.coes[i],
  745. coe,
  746. gljList,
  747. decimal
  748. );
  749. }
  750. }
  751. }
  752. return scMathUtil.roundTo(coeQuantity, -6);
  753. }
  754. function calculateQuantityByCustomerCoes(quantify, coe, glj) {
  755. let rationAmount = coe.coes[0].amount;
  756. if (_.every(coe.coes, "amount", rationAmount)) {
  757. return getCalculateResult(quantify, coe.coes[0]);
  758. } else {
  759. for (let i = 1; i < coe.coes.length; i++) {
  760. if (coeTypeMap[coe.coes[i].coeType] == getRootGLJType(glj.type).ID) {
  761. return getCalculateResult(quantify, coe.coes[i]);
  762. }
  763. }
  764. }
  765. return quantify;
  766. }
  767. function getCoeSelectedGLJ(gljList, rcode, code) {
  768. if (gljList && code && code != "") {
  769. let o_glj = _.find(gljList, { rcode: rcode, code: code });
  770. return o_glj;
  771. }
  772. return null;
  773. }
  774. function getCalculateResult(quantify, c, coe, gljList, decimal) {
  775. let q = quantify;
  776. let o_glj = null;
  777. switch (c.operator) {
  778. case "+":
  779. q = q + c.amount;
  780. break;
  781. case "-":
  782. q = q - c.amount;
  783. break;
  784. case "*":
  785. q = q * c.amount;
  786. break;
  787. case "/":
  788. q = q / c.amount;
  789. break;
  790. case "+*":
  791. o_glj = getCoeSelectedGLJ(gljList, coe.original_code, coe.select_code);
  792. if (o_glj) {
  793. q =
  794. q +
  795. c.amount * scMathUtil.roundForObj(o_glj.rationItemQuantity, decimal);
  796. }
  797. break;
  798. case "-*":
  799. o_glj = getCoeSelectedGLJ(gljList, coe.original_code, coe.select_code);
  800. if (o_glj) {
  801. q =
  802. q -
  803. c.amount * scMathUtil.roundForObj(o_glj.rationItemQuantity, decimal);
  804. }
  805. break;
  806. case "=":
  807. q = c.amount;
  808. break;
  809. }
  810. return q;
  811. }
  812. function getRootGLJType(id) {
  813. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  814. let topTypeId = glj_type_object.getTopParentIdByItemId(id);
  815. let type = glj_type_object.getItemById(topTypeId);
  816. return type;
  817. }
  818. function getGLJTypeByID(id) {
  819. let type = getRootGLJType(id);
  820. if (type != undefined) {
  821. return type.fullName;
  822. } else {
  823. return "";
  824. }
  825. }