ration_facade.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /**
  2. * Created by zhang on 2018/2/9.
  3. */
  4. //先导出后require可以解决循环引用问题
  5. module.exports = {
  6. replaceRations: replaceRations,
  7. addNewRation:addNewRation,
  8. addMultiRation: addMultiRation,
  9. deleteMultiRation:deleteMultiRation,
  10. getSameSectionRations:getSameSectionRations,
  11. getExtendData:getExtendData,
  12. getDefaultProgramID:getDefaultProgramID,
  13. deleteSubListByQuery:deleteSubListByQuery,
  14. updateCoeAdjust:updateCoeAdjust
  15. };
  16. let mongoose = require('mongoose');
  17. import SearchDao from '../../complementary_ration_lib/models/searchModel';
  18. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  19. let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
  20. let glj_calculate_facade = require("../../ration_glj/facade/glj_calculate_facade");
  21. let quantity_detail = require("../facade/quantity_detail_facade");
  22. let ration_glj = mongoose.model('ration_glj');
  23. let ration_coe = mongoose.model('ration_coe');
  24. let ration_model = require('../models/ration');
  25. let bill_model = require('../models/bills');
  26. let decimal_facade = require('./decimal_facade');
  27. let installationFeeModel = mongoose.model("installation_fee");
  28. let rationInstallationModel = mongoose.model('ration_installation');
  29. let rationTemplateModel = mongoose.model('ration_template');
  30. const uuidV1 = require('uuid/v1');
  31. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  32. let complementary_glj_model = mongoose.model('complementary_glj_lib');
  33. let rationItemModel = mongoose.model("std_ration_lib_ration_items");
  34. let complementaryRationModel = mongoose.model('complementary_ration_items');
  35. let coeMolde = mongoose.model('std_ration_lib_coe_list');
  36. let compleCoeModel = mongoose.model('complementary_ration_coe_list');
  37. let _= require('lodash');
  38. const projectDao = require('../../pm/models/project_model').project;
  39. let projectModel = mongoose.model('projects');
  40. const fs = require('fs');
  41. async function addNewRation(data,compilation) {
  42. let query = data.itemQuery;
  43. let stdRation = null;
  44. let startTime = +new Date();
  45. if(query){
  46. let searchDao = new SearchDao();
  47. stdRation = await searchDao.getRationItem(query.userID, compilation._id, [query.rationRepId],query.code, query.ID);
  48. //data.newData.code = query.code;
  49. }
  50. let stdRationTime = +new Date();
  51. console.log("取std定额时间-------------------------------"+(stdRationTime - startTime));
  52. if(data.brUpdate.length>0){
  53. await updateSerialNo(data.brUpdate);
  54. }
  55. let newRation =await insertNewRation(data.newData,data.defaultLibID,stdRation,data.calQuantity);
  56. let addRationGLJTime = +new Date();
  57. console.log("插入新定额时间-------------------------------"+(addRationGLJTime - stdRationTime));
  58. if(stdRation){
  59. return await addRationSubList(stdRation,newRation,data.needInstall,compilation);
  60. }else {
  61. return {ration:newRation};
  62. }
  63. }
  64. async function addMultiRation(datas,compilation) {
  65. let rst = [];
  66. for(let data of datas){
  67. let r = await addNewRation(data,compilation);
  68. rst.push(r);
  69. }
  70. return rst;
  71. }
  72. async function deleteMultiRation(rations) {//这里是只有删除的情况,删除定额的同时删除定额下挂的其它子项目
  73. if(rations.length > 0){//删除定额下的
  74. let rationIDS = _.map(rations,'ID');
  75. await deleteSubListByQuery({projectID:rations[0].projectID,rationID:{"$in": rationIDS}});
  76. await ration_model.model.deleteMany({ID:{"$in": rationIDS}});
  77. }
  78. }
  79. async function getSameSectionRations(data,userId,compilationId){
  80. //let userId
  81. //要先根据定额获取所属章节的ID
  82. let from = data.from; //定额类型,是标准的还是用户定义的
  83. let code = data.code;
  84. let libID = data.libID;
  85. let sectionId,rations=[];
  86. if(from == 'std'){
  87. let ration = await rationItemModel.findOne({rationRepId:libID,code:code},['sectionId']);
  88. sectionId = ration? ration.sectionId:null;
  89. }else {
  90. let ration = await complementaryRationModel.findOne({userId:userId,compilationId: compilationId,code:code},['sectionId']);
  91. sectionId = ration?ration.sectionId:null;
  92. }
  93. if(sectionId){
  94. if (from == 'std') {
  95. rations = await rationItemModel.find({sectionId: sectionId});
  96. } else {
  97. rations = await complementaryRationModel.find({userId: userId, sectionId: sectionId});
  98. }
  99. rations = _.sortBy(rations,'code');
  100. }
  101. return rations
  102. }
  103. async function updateSerialNo(serialNoUpdate){
  104. let tasks=[];
  105. for(let data of serialNoUpdate){
  106. let task={
  107. updateOne:{
  108. filter:{
  109. ID:data.ID,
  110. projectID:data.projectID
  111. },
  112. update :{
  113. serialNo:data.serialNo
  114. }
  115. }
  116. };
  117. tasks.push(task);
  118. }
  119. await ration_model.model.bulkWrite(tasks);
  120. }
  121. async function insertNewRation(newData,defaultLibID,std,calQuantity) {//插入新的定额
  122. let startTime = +new Date();
  123. if(std){
  124. newData.code = std.code;
  125. newData.name = std.name;
  126. newData.caption = std.caption;
  127. newData.unit = std.unit;
  128. newData.libID = std.rationRepId;
  129. newData.stdID = std.ID;
  130. newData.content = std.jobContent;
  131. newData.annotation = std.annotation;
  132. if (std.chapter) {
  133. newData.comments = std.chapter.explanation;
  134. newData.ruleText = std.chapter.ruleText;
  135. }
  136. newData.prefix = '';
  137. newData.from = std.type === 'complementary' ? 'cpt' : 'std';
  138. if(defaultLibID !== std.rationRepId){//借
  139. newData.prefix = '借';
  140. }
  141. else if(std.rationRepId === defaultLibID && newData.from === 'cpt') {
  142. newData.prefix = '补';
  143. }
  144. if(std.feeType == undefined || std.feeType == null || std.feeType ==''){//定额取费专业为空的情况下,取项目属性中的定额取费专业ID
  145. newData.programID = await getProgramForProject(newData.projectID);
  146. }else {
  147. newData.programID = std.feeType;
  148. }
  149. newData.rationAssList = createRationAss(std);
  150. // calculate ration Quantity
  151. }
  152. if(calQuantity){
  153. await CalculateQuantity(newData,newData.billsItemID,newData.projectID);
  154. }
  155. let addRationGLJTime = +new Date();
  156. console.log("计算消耗量时间-------------------------------"+(addRationGLJTime - startTime));
  157. let newRation = await ration_model.model.create(newData);
  158. return newRation;
  159. /*ration_model.model.create(newData);
  160. return newData;*/
  161. }
  162. async function replaceRations(userID,data,compilation) {
  163. let searchDao = new SearchDao();
  164. let recodes = [];
  165. for(let recode of data.nodeInfo){
  166. let stdRation = await searchDao.getRationItem(userID,compilation._id,data.libIDs,recode.newCode, null);
  167. let newRecode = await replaceRation(recode,stdRation,data.defaultLibID,data.projectID,data.calQuantity,compilation,data.cleanzmhs);
  168. if(newRecode){
  169. recodes.push(newRecode);
  170. }else {
  171. break;
  172. }
  173. }
  174. return recodes;
  175. }
  176. async function getDefaultProgramID(data) {
  177. let searchDao = new SearchDao();
  178. let programID;
  179. let std = await searchDao.getRationItem(data.userID,data.compilationId,[data.libID],data.code, null);
  180. if(std == null||std ==undefined || std.feeType == undefined || std.feeType == null || std.feeType ==''){//定额取费专业为空的情况下,取项目属性中的定额取费专业ID
  181. programID = await getProgramForProject(data.projectID);
  182. }else {
  183. programID = std.feeType;
  184. }
  185. return programID;
  186. }
  187. async function replaceRation(nodeInfo,stdRation,defaultLibID,projectID,calQuantity,compilation,cleanzmhs) {
  188. if(nodeInfo.newCode == null||nodeInfo.newCode ==""){//说明是删除编号,则要变成一条空定额
  189. await deleRationSubRecode(projectID,nodeInfo.ID);//删除定额下挂的各种数据,如定额工料机等
  190. return await setEmptyRation(projectID,nodeInfo.ID);
  191. }else if(stdRation){
  192. await deleRationSubRecode(projectID,nodeInfo.ID,cleanzmhs);//删除定额下挂的各种数据,如定额工料机等
  193. let newRation = await updateRation(stdRation,defaultLibID,nodeInfo.ID,nodeInfo.billsItemID,projectID,calQuantity,cleanzmhs);//生成并插入新的定额
  194. return await addRationSubList(stdRation,newRation,nodeInfo.needInstall,compilation,cleanzmhs);
  195. }else {
  196. return null;
  197. }
  198. }
  199. async function addRationSubList(stdRation,newRation,needInstall,compilation,cleanzmhs=false) {
  200. let startTime = +new Date();
  201. let ration_gljs = await addRationGLJ(stdRation,newRation,compilation);
  202. let addRationGLJTime = +new Date();
  203. console.log("添加定额工料机时间-----"+(addRationGLJTime - startTime));
  204. let ration_coes = await addRationCoe(stdRation,newRation,compilation);
  205. let addRationCoeTime = +new Date();
  206. console.log("添加定额coe时间-----"+(addRationCoeTime - addRationGLJTime));
  207. let ration_installations = [];
  208. let ration_template = [];
  209. if(cleanzmhs == false){//清除子目换算即cleanzmh==true时 模板子目、安装增加费不用恢复成标准的
  210. if(needInstall && stdRation.type == 'std'){//只有标准的定额才有安装增加费,补充的定额没有安装增加费
  211. ration_installations = await addRationInstallFee(stdRation,newRation);
  212. }
  213. let addRationInstallFeeTime = +new Date();
  214. console.log("添加定额install时间-----"+(addRationInstallFeeTime - addRationCoeTime));
  215. //添加定额模板子目
  216. ration_template = await addRationTemplate(stdRation,newRation);
  217. }
  218. return {ration:newRation,ration_gljs:ration_gljs,ration_coes:ration_coes,ration_installations:ration_installations,ration_templates:ration_template?[ration_template]:[]};
  219. }
  220. async function addRationInstallFee(std,newRation) {
  221. let install_fee_list = [];
  222. if(std.hasOwnProperty('rationInstList') && std.rationInstList.length > 0){
  223. let installFee = await installationFeeModel.findOne({'projectID': newRation.projectID});
  224. if(!installFee) return;//如果没有找到项目对应的安装增加费,则不添加
  225. for(let ri of std.rationInstList){
  226. let feeItem = _.find(installFee.installFeeItem,{'ID':ri.feeItemId});
  227. let section = _.find(installFee.installSection,{'ID':ri.sectionId});
  228. if(feeItem&&section){
  229. let tem_r_i = {
  230. libID:installFee.libID,
  231. projectID:newRation.projectID,
  232. rationID:newRation.ID,
  233. feeItemId:feeItem.ID,
  234. sectionId:section.ID,
  235. itemName:feeItem.feeItem,
  236. feeType:feeItem.feeType,
  237. sectionName:section.name,
  238. unifiedSetting:1,
  239. ruleId:''
  240. };
  241. if(feeItem.isCal==1&&section.feeRuleId&&section.feeRuleId!=''){//勾选记取时并且有规则ID时才读取
  242. let feeRule = _.find(installFee.feeRule,{'ID':section.feeRuleId});
  243. if(feeRule){
  244. tem_r_i.ruleId = feeRule.ID;
  245. }
  246. }
  247. tem_r_i.ID = uuidV1();
  248. install_fee_list.push(tem_r_i);
  249. }
  250. }
  251. if(install_fee_list.length>0){
  252. await rationInstallationModel.insertMany(install_fee_list);
  253. }
  254. }
  255. return install_fee_list;
  256. }
  257. async function addRationTemplate(std,newRation) {
  258. let templateList = [];
  259. if(std.hasOwnProperty('rationTemplateList') && std.rationTemplateList.length > 0){
  260. for(let tem of std.rationTemplateList){
  261. let re_ration = await rationItemModel.findOne({rationRepId:std.rationRepId,ID:tem.rationID});
  262. if(re_ration){
  263. let template = {
  264. billID:"",
  265. fxID:"",
  266. quantity:"0",
  267. coe:"0"
  268. };
  269. template.code = re_ration.code;
  270. template.name = re_ration.name;
  271. template.type = tem.type;
  272. template.unit = re_ration.unit;
  273. template.billsLocation = tem.billsLocation;
  274. template.defaultLocation = tem.billsLocation;
  275. templateList.push(template)
  276. }
  277. }
  278. }
  279. if(templateList.length > 0){
  280. let ration_template = {};
  281. ration_template.ID = uuidV1();
  282. ration_template.projectID = newRation.projectID;
  283. ration_template.rationID = newRation.ID;
  284. ration_template.createLocation = 1; //默认模板子目分别放在措施项目下
  285. ration_template.templateList = templateList;
  286. await rationTemplateModel.create(ration_template);
  287. return ration_template;
  288. }
  289. return null;
  290. }
  291. async function addRationCoe(std,newRation,compilation) {
  292. let ration_coe_list = [];
  293. let seq = 0;
  294. if(std.hasOwnProperty('rationCoeList')&&std.rationCoeList.length>0){//添加标准库的工料机
  295. for(let sub of std.rationCoeList){
  296. let libCoe;
  297. if (std.type === 'std') {
  298. libCoe = await coeMolde.findOne({'libID':std.rationRepId,'ID':sub.ID,"$or": [{"isDeleted": null}, {"isDeleted": false}]});//std.rationRepId;
  299. } else {
  300. libCoe = await compleCoeModel.findOne({ID: sub.ID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]});
  301. }
  302. if(libCoe){
  303. let newCoe = {};
  304. newCoe.ID = uuidV1();
  305. newCoe.coeID = sub.ID;
  306. newCoe.seq = seq;
  307. newCoe.name = libCoe.name;
  308. newCoe.content = libCoe.content;
  309. newCoe.original_code = libCoe.original_code;
  310. newCoe.option_codes = libCoe.option_codes;
  311. newCoe.option_list = libCoe.option_list;
  312. newCoe.isAdjust=0;
  313. newCoe.coes = libCoe.coes;
  314. newCoe.rationID = newRation.ID;
  315. newCoe.projectID = newRation.projectID;
  316. seq++;
  317. ration_coe_list.push(newCoe);
  318. }
  319. }
  320. }
  321. let lastCoe = await getCustomerCoe(newRation.projectID,newRation.ID,seq,compilation);
  322. ration_coe_list.push(lastCoe);
  323. await ration_coe.insertMany(ration_coe_list);
  324. return ration_coe_list;
  325. }
  326. function getCustomerCoeData() {
  327. var coeList = [
  328. {amount:1, operator:'*', gljCode:null, coeType:'定额'},
  329. { amount:1, operator:'*', gljCode:null, coeType:'人工'},
  330. { amount:1, operator:'*', gljCode:null, coeType:'材料'},
  331. { amount:1, operator:'*', gljCode:null, coeType:'机械'},
  332. { amount:1, operator:'*', gljCode:null, coeType:'主材'},
  333. { amount:1, operator:'*', gljCode:null, coeType:'设备'}
  334. ];
  335. return coeList;
  336. };
  337. async function getCustomerCoe(projectID,rationID,seq,compilation){//取自定义乘系数,根据编办不同,内容可能不同
  338. //生成默认的自定义乘系数
  339. let lastCoe ={
  340. coeID:-1,
  341. name : '自定义系数',
  342. content:'人工×1,材料×1,机械×1,主材×1,设备×1',
  343. isAdjust:1,
  344. seq:seq,
  345. rationID : rationID,
  346. projectID : projectID
  347. };
  348. lastCoe.ID = uuidV1();
  349. lastCoe.coes = getCustomerCoeData();
  350. try {
  351. //查看编办中有没有重写路径
  352. if(compilation.overWriteUrl && compilation.overWriteUrl!=""){
  353. let overWrite = require("../../.."+compilation.overWriteUrl);
  354. if(overWrite.getCusCoeContent) lastCoe.content = overWrite.getCusCoeContent();
  355. if(overWrite.getCustomerCoeData) lastCoe.coes = overWrite.getCustomerCoeData();
  356. }
  357. return lastCoe
  358. }catch (err){
  359. console.log("读取自定义系数重写文件失败");
  360. console.log(err.message);
  361. return lastCoe
  362. }
  363. }
  364. //对于多单价,多组成物消耗量的编办,通过这个方法获取单价、组成物消耗量的字段,
  365. function getExtendData(property,compilation) {
  366. return projectDao.getExtendData(property,compilation);
  367. }
  368. async function addRationGLJ(std,newRation,compilation) {
  369. let newRationGLJList = [];
  370. let rationGLJShowList = [];
  371. let unitPriceFileId = 0;
  372. let property = await projectDao.getProjectProperty(newRation.projectID);
  373. if(property){
  374. unitPriceFileId = property.unitPriceFile !== undefined ? property.unitPriceFile.id : 0;
  375. }
  376. let ext = getExtendData(property,compilation);
  377. let first = +new Date();
  378. if(std.hasOwnProperty('rationGljList') && std.rationGljList.length > 0){
  379. let stdGLJID =[];//标准工料机ID数组
  380. let cptGLJID=[];//补充工料机ID数组
  381. //let stdGLJID = _.map(std.rationGljList,'gljId');
  382. for(let tem_g of std.rationGljList){
  383. if(tem_g.type == 'complementary'){
  384. cptGLJID.push(tem_g.gljId);
  385. }else {
  386. stdGLJID.push(tem_g.gljId);
  387. }
  388. }
  389. let stdGLJList = stdGLJID.length > 0 ? await std_glj_lib_gljList_model.find({'ID':{'$in':stdGLJID}}):[];//速度优化-------先一次性取出所有的工料机列表
  390. let stdGLJMap = _.indexBy(stdGLJList, 'ID');
  391. let cptGLJList = cptGLJID.length > 0 ? await complementary_glj_model.find({'userId':std.userId,'ID':{'$in':cptGLJID}}):[];
  392. let cptGLJMap = _.indexBy(cptGLJList, 'ID');
  393. let stdGLJMapTime = +new Date();
  394. console.log("找到工料机映射表时间-------------------------------"+(stdGLJMapTime - first));
  395. for(let sub of std.rationGljList){
  396. let newGLJ = {};
  397. newGLJ.ID = uuidV1();
  398. newGLJ.projectID = newRation.projectID;
  399. newGLJ.GLJID = sub.gljId;
  400. newGLJ.rationID = newRation.ID;
  401. newGLJ.billsItemID = newRation.billsItemID;
  402. newGLJ.rationItemQuantity = sub.consumeAmt;
  403. newGLJ.quantity = sub.consumeAmt;
  404. newGLJ.glj_repository_id = std.rationRepId;
  405. let std_glj = null;
  406. if(sub.type == 'complementary'){//有可能来自标准工料机库或补充工料机库
  407. std_glj = cptGLJMap[sub.gljId];
  408. newGLJ.from = 'cpt';
  409. }else {
  410. std_glj = stdGLJMap[sub.gljId];
  411. newGLJ.from = 'std';
  412. //多单价情况处理
  413. if(ext && ext.priceField && std_glj && std_glj.priceProperty){
  414. std_glj.basePrice = std_glj.priceProperty[ext.priceField];
  415. }
  416. }
  417. if(std_glj){
  418. newGLJ.name = std_glj.name;
  419. newGLJ.code = std_glj.code;
  420. newGLJ.original_code = std_glj.code;
  421. newGLJ.unit = std_glj.unit;
  422. newGLJ.specs = std_glj.specs;
  423. newGLJ.model = std_glj.model;
  424. newGLJ.basePrice = std_glj.basePrice;
  425. newGLJ.marketPrice = std_glj.basePrice;
  426. newGLJ.shortName = std_glj.shortName;
  427. newGLJ.type = std_glj.gljType;
  428. newGLJ.repositoryId = std_glj.repositoryId;
  429. newGLJ.adjCoe = std_glj.adjCoe;
  430. newGLJ.materialType = std_glj.materialType;
  431. newGLJ.materialCoe = std_glj.materialCoe;
  432. newGLJ.createType = 'normal';
  433. let info = await ration_glj_facade.getInfoFromProjectGLJ(newGLJ,unitPriceFileId,ext);
  434. newGLJ = ration_glj_facade.createNewRecord(info);
  435. newRationGLJList.push(newGLJ);
  436. rationGLJShowList.push(info);
  437. }
  438. //let InfoFromProjectGLJ = +new Date();
  439. //console.log("找到项目工料机时间-------------------------------"+(InfoFromProjectGLJ - std_gljTime));
  440. }
  441. }
  442. let before = +new Date();
  443. console.log("总查询时间为-------------------------------"+(before-first));
  444. if(newRationGLJList.length>0){
  445. await ration_glj.insertMany(newRationGLJList);
  446. }
  447. let after = +new Date();
  448. console.log("实际插入时间为-------------------------------"+(after-before));
  449. console.log("总操作时间为-------------------------------"+(after-first));
  450. return rationGLJShowList;
  451. }
  452. async function deleRationSubRecode(projectID,rationID,cleanzmhs=false) {//删除挂在定额下的数据,如工程量明细,定额工料机等
  453. let delete_query={projectID: projectID, rationID: rationID};
  454. //删除工程量明细
  455. await deleteSubListByQuery(delete_query,cleanzmhs) ;
  456. }
  457. async function deleteSubListByQuery(delete_query,cleanzmhs=false) {
  458. if(cleanzmhs == false){//清空子目换算即cleanzmh==true时不需要清空工程量明细、模板关联子目、安装增加费
  459. await quantity_detail.deleteByQuery(delete_query) ;//删除工程量明细
  460. await rationInstallationModel.deleteMany(delete_query);//删除安装增加费
  461. await rationTemplateModel.deleteMany(delete_query);//删除模板关联子目
  462. }
  463. //to do稳定土也要删除
  464. await ration_coe.deleteMany(delete_query);//删除附注条件
  465. await ration_glj.deleteMany(delete_query);//删除定额工料机
  466. }
  467. async function updateCoeAdjust(data,compilation) {
  468. let replace = [];
  469. await ration_coe.update({ID:data.ID},data.doc);
  470. //添加单个工料机的情况
  471. if (data.add.length > 0) await ration_glj_facade.insertAddTypeGLJ(data.add,compilation);
  472. if(data.delete.length > 0) await ration_glj_facade.deleteGLJ(data.delete);
  473. //替换工料机的情况
  474. if (data.replace.length > 0){
  475. for(let r of data.replace){
  476. replace.push(await ration_glj_facade.replaceGLJByData(r,compilation)) ;
  477. }
  478. }
  479. let cal_result = await glj_calculate_facade.calculateQuantity({projectID:data.projectID,rationID:data.rationID},null,true);
  480. let coe = {
  481. query:{ID:data.ID,projectID:data.projectID},
  482. doc:data.doc
  483. };
  484. let ration_glj ={
  485. quantityRefresh:true,
  486. glj_result:cal_result.glj_result
  487. };
  488. let ration = {
  489. ID:cal_result.rationID,
  490. adjustState:cal_result.adjustState,
  491. name:cal_result.rationName
  492. };
  493. return {coe:coe,ration_glj:ration_glj,ration:ration,add:data.add,delete:data.delete,replace:replace}
  494. }
  495. async function updateRation(std,defaultLibID,rationID,billsItemID,projectID,calQuantity,cleanzmh=false) {
  496. // insertNewRation
  497. let ration ={};
  498. ration.code = std.code;
  499. ration.name = std.name;
  500. ration.caption = std.caption;
  501. ration.unit = std.unit;
  502. if (std.type === 'std') {
  503. ration.libID = std.rationRepId;
  504. ration.stdID = std.ID;
  505. }
  506. ration.content = std.jobContent;
  507. ration.adjustState = '';
  508. ration.isFromDetail=0;
  509. ration.isSubcontract=false;
  510. ration.fees=[];
  511. if (std.chapter) {
  512. ration.comments = std.chapter.explanation;
  513. ration.ruleText = std.chapter.ruleText;
  514. }
  515. ration.from = std.type === 'complementary' ? 'cpt' : 'std';
  516. //定额前缀 none:0, complementary:1, borrow: 2
  517. ration.prefix = '';
  518. //借用优先级比补充高
  519. if(std.rationRepId !== parseInt(defaultLibID)){//借用
  520. ration.prefix = '借';
  521. }
  522. else if(std.rationRepId === defaultLibID && ration.from === 'cpt') {
  523. ration.prefix = '补';
  524. }
  525. ration.rationAssList = createRationAss(std);//生成辅助定额
  526. if(cleanzmh==false){//如果是清空子目换算,即cleanzmh==true 保留定额工程量、工程量表达式、含量(分解系数)、取费专业(取费类别)
  527. if(std.feeType == undefined || std.feeType == null || std.feeType ==''){//定额取费专业为空的情况下,取项目属性中的定额取费专业ID
  528. ration.programID = await getProgramForProject(projectID);
  529. }else {
  530. ration.programID = std.feeType;
  531. }
  532. if(calQuantity){
  533. await CalculateQuantity(ration,billsItemID,projectID);
  534. }
  535. }
  536. let unsetObject = {
  537. "marketUnitFee":1,
  538. 'marketTotalFee':1,
  539. "maskName":1
  540. }
  541. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration,"$unset":unsetObject},{new: true});//;
  542. return newRation;
  543. }
  544. async function setEmptyRation(projectID,rationID){
  545. let ration ={};
  546. ration.code = "";
  547. ration.name = "";
  548. ration.caption = "";
  549. ration.unit = "";
  550. ration.libID = null;
  551. ration.content = "";
  552. ration.adjustState = '';
  553. ration.isFromDetail=0;
  554. ration.isSubcontract=false;
  555. ration.fees=[];
  556. ration.comments = "";
  557. ration.ruleText = "";
  558. ration.quantity="";
  559. ration.contain="";
  560. ration.quantityEXP="";
  561. ration.from = 'std';
  562. //定额前缀 none:0, complementary:1, borrow: 2
  563. ration.prefix = '';
  564. ration.rationAssList = [];
  565. ration.marketUnitFee ="";
  566. ration.marketTotalFee ="";
  567. ration.maskName = "";
  568. ration.targetTotalFee ='';
  569. ration.targetUnitFee = "";
  570. ration.deleteInfo = null;
  571. ration.quantityCoe = {};
  572. ration.rationQuantityCoe="";
  573. ration.tenderQuantity = "";
  574. ration.programID = null;
  575. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration},{new: true});//;
  576. return {ration:newRation,ration_gljs:[],ration_coes:[],ration_installs:[]};
  577. }
  578. function createRationAss(std) {
  579. let rationAssList = [];//生成辅助定额
  580. if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
  581. for(let i=0;i<std.rationAssList.length;i++){
  582. let ass = std.rationAssList[i];
  583. ass._doc.actualValue = ass.stdValue;
  584. if(_.isString(ass._doc.assistCode)) ass._doc.assistCode = ass._doc.assistCode.replace("\n","");
  585. ass._doc.isAdjust = 0;
  586. rationAssList.push(ass);
  587. }
  588. }
  589. return rationAssList;
  590. }
  591. async function CalculateQuantity (ration,billsItemID,projectID) {
  592. // calculate ration Quantity
  593. let project = await projectModel.findOne({ID:projectID});
  594. let decimalObject =await decimal_facade.getProjectDecimal(projectID,project);
  595. let quantity_decimal = (decimalObject&&decimalObject.ration&&decimalObject.ration.quantity)?decimalObject.ration.quantity:3;
  596. let pbill = await bill_model.model.findOne({projectID:projectID,ID:billsItemID});
  597. let t_unit = ration.unit?ration.unit.replace(/^\d+/,""):"";
  598. if(t_unit!=pbill.unit){//如果定额工程量的单位去除前面的数字后不等于清单单位,定额工程量保持不变
  599. return ;
  600. }
  601. let billsQuantity = pbill.quantity ? pbill.quantity : 0;
  602. let bill_decimal = await decimal_facade.getBillsQuantityDecimal(projectID,pbill.unit,project);
  603. billsQuantity=scMathUtil.roundForObj(billsQuantity,bill_decimal);
  604. ration.quantityEXP="QDL";
  605. ration.quantity = scMathUtil.roundForObj(billsQuantity / FilterNumberFromUnit(ration.unit),quantity_decimal);//不管是否打勾都做转换
  606. ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,6);
  607. };
  608. async function getProgramForProject(projectID){
  609. let project = await projectModel.findOne({ID:projectID});
  610. return project.property.engineering;
  611. }
  612. function FilterNumberFromUnit (unit) {
  613. let reg = new RegExp('^[0-9]+');
  614. if (reg.test(unit)) {
  615. return parseInt(unit.match(reg)[0]);
  616. } else {
  617. return 1;
  618. }
  619. };