ration_facade.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. const SearchDao = require('../../complementary_ration_lib/models/searchModel');
  18. const GLJListModel = require("../../glj/models/glj_list_model");
  19. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  20. let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
  21. let glj_calculate_facade = require("../../ration_glj/facade/glj_calculate_facade");
  22. let quantity_detail = require("../facade/quantity_detail_facade");
  23. let ration_glj = mongoose.model('ration_glj');
  24. let ration_coe = mongoose.model('ration_coe');
  25. let ration_model = require('../models/ration');
  26. let bill_model = require('../models/bills');
  27. let decimal_facade = require('./decimal_facade');
  28. let installationFeeModel = mongoose.model("installation_fee");
  29. let rationInstallationModel = mongoose.model('ration_installation');
  30. let rationTemplateModel = mongoose.model('ration_template');
  31. const uuidV1 = require('uuid/v1');
  32. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  33. let complementary_glj_model = mongoose.model('complementary_glj_lib');
  34. let rationItemModel = mongoose.model("std_ration_lib_ration_items");
  35. let complementaryRationModel = mongoose.model('complementary_ration_items');
  36. let coeMolde = mongoose.model('std_ration_lib_coe_list');
  37. let compleCoeModel = mongoose.model('complementary_ration_coe_list');
  38. let projectGLJModel = mongoose.model("glj_list");
  39. let mixRatioModel = mongoose.model("mix_ratio");
  40. let complementaryGljLibModel = mongoose.model('complementary_glj_lib');
  41. let counterModel = mongoose.model('counter');
  42. let unitPriceModel = mongoose.model('unit_price');
  43. let chongqingOverWrite = require("../../../web/over_write/js/chongqing_2018.js");
  44. let _= require('lodash');
  45. const projectDao = require('../../pm/models/project_model').project;
  46. let projectModel = mongoose.model('projects');
  47. async function addNewRation(data,compilation) {
  48. let query = data.itemQuery;
  49. let stdRation = null;
  50. let startTime = +new Date();
  51. if(query){
  52. let searchDao = new SearchDao();
  53. stdRation = await searchDao.getRationItem(query.userID, compilation._id, [query.rationRepId],query.code, query.ID);
  54. //data.newData.code = query.code;
  55. }
  56. let stdRationTime = +new Date();
  57. console.log("取std定额时间-------------------------------"+(stdRationTime - startTime));
  58. if(data.brUpdate.length>0){
  59. await updateSerialNo(data.brUpdate);
  60. }
  61. let newRation =await insertNewRation(data.newData,data.defaultLibID,stdRation,data.calQuantity);
  62. let addRationGLJTime = +new Date();
  63. console.log("插入新定额时间-------------------------------"+(addRationGLJTime - stdRationTime));
  64. if(stdRation){
  65. return await addRationSubList(stdRation,newRation,data.needInstall,compilation);
  66. }else {
  67. return {ration:newRation};
  68. }
  69. }
  70. async function addMultiRation(datas,compilation) {
  71. const task = [];
  72. for (const data of datas) {
  73. task.push(addNewRation(data, compilation));
  74. }
  75. return await Promise.all(task);
  76. }
  77. async function deleteMultiRation(rations) {//这里是只有删除的情况,删除定额的同时删除定额下挂的其它子项目
  78. if(rations.length > 0){//删除定额下的
  79. let rationIDS = _.map(rations,'ID');
  80. await deleteSubListByQuery({projectID:rations[0].projectID,rationID:{"$in": rationIDS}});
  81. await ration_model.model.deleteMany({ID:{"$in": rationIDS}});
  82. }
  83. }
  84. async function getSameSectionRations(data,userId,compilationId){
  85. //let userId
  86. //要先根据定额获取所属章节的ID
  87. let from = data.from; //定额类型,是标准的还是用户定义的
  88. let code = data.code;
  89. let libID = data.libID;
  90. let sectionId,rations=[];
  91. if(from == 'std'){
  92. let ration = await rationItemModel.findOne({rationRepId:libID,code:code},['sectionId']);
  93. sectionId = ration? ration.sectionId:null;
  94. }else {
  95. let ration = await complementaryRationModel.findOne({userId:userId,compilationId: compilationId,code:code},['sectionId']);
  96. sectionId = ration?ration.sectionId:null;
  97. }
  98. if(sectionId){
  99. if (from == 'std') {
  100. rations = await rationItemModel.find({sectionId: sectionId});
  101. } else {
  102. rations = await complementaryRationModel.find({userId: userId, sectionId: sectionId});
  103. }
  104. rations = _.sortBy(rations,'code');
  105. }
  106. return rations
  107. }
  108. async function updateSerialNo(serialNoUpdate){
  109. let tasks=[];
  110. for(let data of serialNoUpdate){
  111. let task={
  112. updateOne:{
  113. filter:{
  114. ID:data.ID,
  115. projectID:data.projectID
  116. },
  117. update :{
  118. serialNo:data.serialNo
  119. }
  120. }
  121. };
  122. tasks.push(task);
  123. }
  124. await ration_model.model.bulkWrite(tasks);
  125. }
  126. async function insertNewRation(newData,defaultLibID,std,calQuantity) {//插入新的定额
  127. let startTime = +new Date();
  128. if(std){
  129. newData.code = std.code;
  130. newData.name = std.name;
  131. newData.caption = std.caption;
  132. newData.unit = std.unit;
  133. newData.libID = std.rationRepId;
  134. newData.stdID = std.ID;
  135. newData.content = std.jobContent;
  136. newData.annotation = std.annotation;
  137. if (std.chapter) {
  138. newData.comments = std.chapter.explanation;
  139. newData.ruleText = std.chapter.ruleText;
  140. }
  141. newData.prefix = '';
  142. newData.from = std.type === 'complementary' ? 'cpt' : 'std';
  143. if(defaultLibID !== std.rationRepId){//定额是默认定额库中的时,只显示编号;
  144. newData.prefix = '借';//定额不是默认定额库中的、且不是补充定额库中的时, 在定额编号前显示“借”。
  145. if(newData.from === 'cpt') newData.prefix = '补';//定额是补充定额库中的时,在定额编号前显示“补”;
  146. }
  147. if(std.feeType == undefined || std.feeType == null || std.feeType ==''){//定额取费专业为空的情况下,取项目属性中的定额取费专业ID
  148. newData.programID = await getProgramForProject(newData.projectID);
  149. }else {
  150. newData.programID = std.feeType;
  151. }
  152. newData.manageFeeRate = std.manageFeeRate;
  153. newData.rationAssList = createRationAss(std);
  154. // calculate ration Quantity
  155. }
  156. if(calQuantity){
  157. await CalculateQuantity(newData,newData.billsItemID,newData.projectID);
  158. }
  159. let addRationGLJTime = +new Date();
  160. console.log("计算消耗量时间-------------------------------"+(addRationGLJTime - startTime));
  161. await ration_model.model.insertMany(newData);
  162. return newData;
  163. /*ration_model.model.create(newData);
  164. return newData;*/
  165. }
  166. async function replaceRations(userID,data,compilation) {
  167. let searchDao = new SearchDao();
  168. let recodes = [];
  169. for(let recode of data.nodeInfo){
  170. let stdRation = await searchDao.getRationItem(userID,compilation._id,data.libIDs,recode.newCode, null);
  171. let newRecode = await replaceRation(recode,stdRation,data.defaultLibID,data.projectID,data.calQuantity,compilation,data.cleanzmhs);
  172. if(newRecode){
  173. recodes.push(newRecode);
  174. }else {
  175. break;
  176. }
  177. }
  178. return recodes;
  179. }
  180. async function getDefaultProgramID(data) {
  181. let searchDao = new SearchDao();
  182. let programID;
  183. let std = await searchDao.getRationItem(data.userID,data.compilationId,[data.libID],data.code, null);
  184. if(std == null||std ==undefined || std.feeType == undefined || std.feeType == null || std.feeType ==''){//定额取费专业为空的情况下,取项目属性中的定额取费专业ID
  185. programID = await getProgramForProject(data.projectID);
  186. }else {
  187. programID = std.feeType;
  188. }
  189. return programID;
  190. }
  191. async function replaceRation(nodeInfo,stdRation,defaultLibID,projectID,calQuantity,compilation,cleanzmhs) {
  192. if(nodeInfo.newCode == null||nodeInfo.newCode ==""){//说明是删除编号,则要变成一条空定额
  193. await deleRationSubRecode(projectID,nodeInfo.ID);//删除定额下挂的各种数据,如定额工料机等
  194. return await setEmptyRation(projectID,nodeInfo.ID);
  195. }else if(stdRation){
  196. await deleRationSubRecode(projectID,nodeInfo.ID,cleanzmhs);//删除定额下挂的各种数据,如定额工料机等
  197. let newRation = await updateRation(stdRation,defaultLibID,nodeInfo.ID,nodeInfo.billsItemID,projectID,calQuantity,cleanzmhs);//生成并插入新的定额
  198. return await addRationSubList(stdRation,newRation,nodeInfo.needInstall,compilation,cleanzmhs);
  199. }else {
  200. return null;
  201. }
  202. }
  203. async function addRationSubList(stdRation,newRation,needInstall,compilation,cleanzmhs=false) {
  204. let startTime = +new Date();
  205. let [ration_gljs,projectGLJList] = await addRationGLJ(stdRation,newRation,compilation);
  206. let addRationGLJTime = +new Date();
  207. console.log("添加定额工料机时间-----"+(addRationGLJTime - startTime));
  208. let ration_coes = await addRationCoe(stdRation,newRation,compilation);
  209. let addRationCoeTime = +new Date();
  210. console.log("添加定额coe时间-----"+(addRationCoeTime - addRationGLJTime));
  211. let ration_installations = [];
  212. let ration_template = [];
  213. if(cleanzmhs == false){//清除子目换算即cleanzmh==true时 模板子目、安装增加费不用恢复成标准的
  214. if(needInstall && stdRation.type == 'std'){//只有标准的定额才有安装增加费,补充的定额没有安装增加费
  215. ration_installations = await addRationInstallFee(stdRation,newRation);
  216. }
  217. let addRationInstallFeeTime = +new Date();
  218. console.log("添加定额install时间-----"+(addRationInstallFeeTime - addRationCoeTime));
  219. //添加定额模板子目
  220. ration_template = await addRationTemplate(stdRation,newRation);
  221. }else if(newRation.areaIncreaseFee == true){//清空子目换算时,如果有面积增加费,子目调整状态要生成
  222. let t = await glj_calculate_facade.calculateQuantity({rationID:newRation.ID},true);
  223. newRation.adjustState = t.adjustState;
  224. }
  225. return {ration:newRation,ration_gljs:ration_gljs,ration_coes:ration_coes,ration_installations:ration_installations,ration_templates:ration_template?[ration_template]:[],projectGLJList:projectGLJList};
  226. }
  227. async function addRationInstallFee(std,newRation) {
  228. let install_fee_list = [];
  229. if(std.hasOwnProperty('rationInstList') && std.rationInstList.length > 0){
  230. let installFee = await installationFeeModel.findOne({'projectID': newRation.projectID});
  231. if(!installFee) return;//如果没有找到项目对应的安装增加费,则不添加
  232. for(let ri of std.rationInstList){
  233. let feeItem = _.find(installFee.installFeeItem,{'ID':ri.feeItemId});
  234. let section = _.find(installFee.installSection,{'ID':ri.sectionId});
  235. if(feeItem&&section){
  236. let tem_r_i = {
  237. libID:installFee.libID,
  238. projectID:newRation.projectID,
  239. rationID:newRation.ID,
  240. feeItemId:feeItem.ID,
  241. sectionId:section.ID,
  242. itemName:feeItem.feeItem,
  243. feeType:feeItem.feeType,
  244. sectionName:section.name,
  245. unifiedSetting:1,
  246. ruleId:''
  247. };
  248. if(feeItem.isCal==1&&section.feeRuleId&&section.feeRuleId!=''){//勾选记取时并且有规则ID时才读取
  249. let feeRule = _.find(installFee.feeRule,{'ID':section.feeRuleId});
  250. if(feeRule){
  251. tem_r_i.ruleId = feeRule.ID;
  252. }
  253. }
  254. tem_r_i.ID = uuidV1();
  255. install_fee_list.push(tem_r_i);
  256. }
  257. }
  258. if(install_fee_list.length>0){
  259. await rationInstallationModel.insertMany(install_fee_list);
  260. }
  261. }
  262. return install_fee_list;
  263. }
  264. async function addRationTemplate(std,newRation) {
  265. let templateList = [];
  266. if(std.hasOwnProperty('rationTemplateList') && std.rationTemplateList.length > 0){
  267. for(let tem of std.rationTemplateList){
  268. let re_ration = await rationItemModel.findOne({rationRepId:std.rationRepId,ID:tem.rationID});
  269. if(re_ration){
  270. let template = {
  271. billID:"",
  272. fxID:"",
  273. quantity:"0",
  274. coe:"0"
  275. };
  276. template.code = re_ration.code;
  277. template.name = re_ration.name;
  278. template.type = tem.type;
  279. template.unit = re_ration.unit;
  280. template.billsLocation = tem.billsLocation;
  281. template.defaultLocation = tem.billsLocation;
  282. templateList.push(template)
  283. }
  284. }
  285. }
  286. if(templateList.length > 0){
  287. let ration_template = {};
  288. ration_template.ID = uuidV1();
  289. ration_template.projectID = newRation.projectID;
  290. ration_template.rationID = newRation.ID;
  291. ration_template.createLocation = 1; //默认模板子目分别放在措施项目下
  292. ration_template.templateList = templateList;
  293. await rationTemplateModel.create(ration_template);
  294. return ration_template;
  295. }
  296. return null;
  297. }
  298. async function addRationCoe(std,newRation,compilation) {
  299. let ration_coe_list = [];
  300. let seq = 0;
  301. let stdCoeIDs = [];//与comCoeIDs两者不共存
  302. let comCoeIDs = [];
  303. let coeMap={};
  304. if(std.hasOwnProperty('rationCoeList')&&std.rationCoeList.length>0){//添加标准库的工料机
  305. for(let sub of std.rationCoeList){
  306. std.type === 'std'?stdCoeIDs.push(sub.ID):comCoeIDs.push(sub.ID);
  307. }
  308. let libCoeList = [];
  309. let seqLibIDs = [];
  310. if (std.type === 'std'){
  311. libCoeList = await coeMolde.find({'ID':{'$in':stdCoeIDs}}).lean();
  312. seqLibIDs = stdCoeIDs;
  313. }else{
  314. libCoeList = await compleCoeModel.find({'ID':{'$in':comCoeIDs}}).lean();
  315. seqLibIDs = comCoeIDs;
  316. }
  317. for(let lib of libCoeList){
  318. coeMap[lib.ID] = lib;
  319. }
  320. for(let ID of seqLibIDs){
  321. let libCoe = coeMap[ID];
  322. if(libCoe){
  323. let newCoe = {};
  324. newCoe.ID = uuidV1();
  325. newCoe.coeID = ID;
  326. newCoe.seq = seq;
  327. newCoe.name = libCoe.name;
  328. newCoe.content = libCoe.content;
  329. newCoe.original_code = libCoe.original_code;
  330. newCoe.option_codes = libCoe.option_codes;
  331. newCoe.option_list = libCoe.option_list;
  332. newCoe.isAdjust=0;
  333. newCoe.coes = libCoe.coes;
  334. newCoe.rationID = newRation.ID;
  335. newCoe.projectID = newRation.projectID;
  336. seq++;
  337. ration_coe_list.push(newCoe);
  338. }
  339. }
  340. }
  341. let lastCoe = await getCustomerCoe(newRation.projectID,newRation.ID,seq,compilation);//这个地方加载overWrite需要一些时间
  342. ration_coe_list.push(lastCoe);
  343. await ration_coe.insertMany(ration_coe_list);
  344. return ration_coe_list;
  345. }
  346. function getCustomerCoeData() {
  347. var coeList = [
  348. {amount:1, operator:'*', gljCode:null, coeType:'定额'},
  349. { amount:1, operator:'*', gljCode:null, coeType:'人工'},
  350. { amount:1, operator:'*', gljCode:null, coeType:'材料'},
  351. { amount:1, operator:'*', gljCode:null, coeType:'机械'},
  352. { amount:1, operator:'*', gljCode:null, coeType:'主材'},
  353. { amount:1, operator:'*', gljCode:null, coeType:'设备'}
  354. ];
  355. return coeList;
  356. };
  357. async function getCustomerCoe(projectID,rationID,seq,compilation){//取自定义乘系数,根据编办不同,内容可能不同
  358. //生成默认的自定义乘系数
  359. let lastCoe ={
  360. coeID:-1,
  361. name : '自定义系数',
  362. content:'人工×1,材料×1,机械×1,主材×1,设备×1',
  363. isAdjust:1,
  364. seq:seq,
  365. rationID : rationID,
  366. projectID : projectID
  367. };
  368. lastCoe.ID = uuidV1();
  369. lastCoe.coes = getCustomerCoeData();
  370. try {
  371. //查看编办中有没有重写路径
  372. if(compilation.overWriteUrl && compilation.overWriteUrl!=""){
  373. if(compilation.overWriteUrl.indexOf("chongqing_2018")!= -1){
  374. console.log("重庆overwrite");
  375. let overWrite = chongqingOverWrite
  376. if(overWrite.getCusCoeContent) lastCoe.content = overWrite.getCusCoeContent();
  377. if(overWrite.getCustomerCoeData) lastCoe.coes = overWrite.getCustomerCoeData();
  378. }
  379. }
  380. return lastCoe
  381. }catch (err){
  382. console.log("读取自定义系数重写文件失败");
  383. console.log(err.message);
  384. return lastCoe
  385. }
  386. }
  387. //对于多单价,多组成物消耗量的编办,通过这个方法获取单价、组成物消耗量的字段,
  388. function getExtendData(property,compilation) {
  389. return projectDao.getExtendData(property,compilation);
  390. }
  391. async function addRationGLJ(std,newRation,compilation) {
  392. let newRationGLJList = [];
  393. let rationGLJShowList = [];
  394. let projectGLJList = [];
  395. let gljKeyMap = {};
  396. let mixRatioMap={};
  397. let gljCodes=[];
  398. let unitPriceFileId = 0;
  399. let property = await projectDao.getProjectProperty(newRation.projectID);
  400. if(property){
  401. unitPriceFileId = property.unitPriceFile !== undefined ? property.unitPriceFile.id : 0;
  402. }
  403. let ext = getExtendData(property,compilation);
  404. let first = +new Date();
  405. if(std.hasOwnProperty('rationGljList') && std.rationGljList.length > 0){
  406. let stdGLJID =[];//标准工料机ID数组
  407. let cptGLJID=[];//补充工料机ID数组
  408. //let stdGLJID = _.map(std.rationGljList,'gljId');
  409. for(let tem_g of std.rationGljList){
  410. if(tem_g.type == 'complementary'){
  411. cptGLJID.push(tem_g.gljId);
  412. }else {
  413. stdGLJID.push(tem_g.gljId);
  414. }
  415. }
  416. let stdGLJList = stdGLJID.length > 0 ? await std_glj_lib_gljList_model.find({'ID':{'$in':stdGLJID}}):[];//速度优化-------先一次性取出所有的工料机列表
  417. let stdGLJMap = _.indexBy(stdGLJList, 'ID');
  418. let cptGLJList = cptGLJID.length > 0 ? await complementary_glj_model.find({'userId':std.userId,'ID':{'$in':cptGLJID}}):[];
  419. let cptGLJMap = _.indexBy(cptGLJList, 'ID');
  420. let stdGLJMapTime = +new Date();
  421. console.log("找到工料机映射表时间-------------------------------"+(stdGLJMapTime - first));
  422. for(let sub of std.rationGljList){
  423. let newGLJ = {};
  424. newGLJ.ID = uuidV1();
  425. newGLJ.projectID = newRation.projectID;
  426. newGLJ.GLJID = sub.gljId;
  427. newGLJ.rationID = newRation.ID;
  428. newGLJ.billsItemID = newRation.billsItemID;
  429. newGLJ.rationItemQuantity = sub.consumeAmt;
  430. newGLJ.quantity = sub.consumeAmt;
  431. newGLJ.glj_repository_id = std.rationRepId;
  432. let std_glj = null;
  433. if(sub.type == 'complementary'){//有可能来自标准工料机库或补充工料机库
  434. std_glj = cptGLJMap[sub.gljId];
  435. newGLJ.from = 'cpt';
  436. }else {
  437. std_glj = stdGLJMap[sub.gljId];
  438. newGLJ.from = 'std';
  439. }
  440. //多单价情况处理
  441. if(ext && ext.priceField && std_glj && std_glj.priceProperty && std_glj.priceProperty[ext.priceField] !== null && std_glj.priceProperty[ext.priceField] !== undefined){
  442. std_glj.basePrice = std_glj.priceProperty[ext.priceField];
  443. }
  444. if(std_glj){
  445. newGLJ.name = std_glj.name;
  446. newGLJ.code = std_glj.code;
  447. newGLJ.original_code = std_glj.code;
  448. newGLJ.unit = std_glj.unit;
  449. newGLJ.specs = std_glj.specs;
  450. newGLJ.model = std_glj.model;
  451. newGLJ.basePrice = std_glj.basePrice;
  452. newGLJ.marketPrice = std_glj.basePrice;
  453. newGLJ.taxRate= std_glj.taxRate;
  454. newGLJ.shortName = std_glj.shortName;
  455. newGLJ.type = std_glj.gljType;
  456. newGLJ.repositoryId = std_glj.repositoryId;
  457. newGLJ.adjCoe = std_glj.adjCoe;
  458. newGLJ.materialType = std_glj.materialType;
  459. newGLJ.materialCoe = std_glj.materialCoe;
  460. newGLJ.materialIndexType = std_glj.materialIndexType;
  461. newGLJ.materialIndexUnit = std_glj.materialIndexUnit;
  462. newGLJ.materialIndexCoe = std_glj.materialIndexCoe;
  463. newGLJ.createType = 'normal';
  464. let tindex = getIndex(newGLJ);
  465. if(std_glj.component && std_glj.component.length > 0) mixRatioMap[tindex] = std_glj.component;
  466. let tdata = ration_glj_facade.getGLJSearchInfo(newGLJ);
  467. gljKeyMap[tindex] = tdata;
  468. gljCodes.push(tdata.code);
  469. newRationGLJList.push(newGLJ);
  470. // let [info,projectGLJ] = await ration_glj_facade.getInfoFromProjectGLJ(newGLJ,unitPriceFileId,ext);
  471. // newGLJ = ration_glj_facade.createNewRecord(info);
  472. // newRationGLJList.push(newGLJ);
  473. // rationGLJShowList.push(info);
  474. // projectGLJList.push(projectGLJ)
  475. }
  476. //let InfoFromProjectGLJ = +new Date();
  477. //console.log("找到项目工料机时间-------------------------------"+(InfoFromProjectGLJ - std_gljTime));
  478. }
  479. [newRationGLJList, projectGLJList] = await getProjectGLJinfo(newRation.projectID,newRationGLJList,gljKeyMap,gljCodes,mixRatioMap,unitPriceFileId,ext);
  480. }
  481. let before = +new Date();
  482. console.log("总查询时间为-------------------------------"+(before-first));
  483. if(newRationGLJList.length>0){
  484. await ration_glj.insertMany(newRationGLJList);
  485. }
  486. let after = +new Date();
  487. console.log("实际插入时间为-------------------------------"+(after-before));
  488. console.log("总操作时间为-------------------------------"+(after-first));
  489. return [newRationGLJList,projectGLJList]
  490. }
  491. async function getProjectGLJinfo(projectID,t_newRationGLJList,gljKeyMap,gljCodes,mixRatioMap,unitPriceFileId,ext){//批量插入或查找项目工料机信息
  492. //先根据工料机编号在项目工料机中查找工料机是否存在
  493. let projectGLJMap={};
  494. let projectGLJList = [];
  495. let newProjectGLJList=[];//工料机ID要重新去取
  496. let connectKeyList = [];
  497. let newRationGLJList = [];
  498. let gljListModel = new GLJListModel();
  499. let t_projectGLJList = await projectGLJModel.find({'project_id':projectID,'code':{'$in':gljCodes}}).lean();
  500. for(let pg of t_projectGLJList){
  501. let pindex = getIndex(pg);
  502. projectGLJMap[pindex] = pg;
  503. }
  504. for(let key in gljKeyMap){
  505. if(!projectGLJMap[key]){//如果项目工料机不存在,则添加
  506. newProjectGLJList.push(gljKeyMap[key]);
  507. projectGLJMap[key] = gljKeyMap[key];
  508. }
  509. //查看组成物
  510. if(gljListModel.ownCompositionTypes.indexOf(gljKeyMap[key].type)!=-1){//有组成物的类型
  511. connectKeyList.push(key);
  512. }
  513. }
  514. let [existMixRatioMap,mixRatioInsertData,missCodeList] = await getMixRatioInfo(projectID,projectGLJMap,newProjectGLJList,mixRatioMap,connectKeyList,unitPriceFileId,ext);
  515. if(missCodeList.length > 0) gljCodes = gljCodes.concat(missCodeList);
  516. let [unitPriceMap,newUnitPriceList] = await getUnitPriceData(newProjectGLJList,gljCodes,unitPriceFileId);
  517. if(newUnitPriceList.length > 0) await unitPriceModel.insertMany(newUnitPriceList);
  518. if(mixRatioInsertData.length > 0) await mixRatioModel.insertMany(mixRatioInsertData);
  519. //插入项目工料机
  520. if(newProjectGLJList.length > 0){
  521. await setIDfromCounter("glj_list",newProjectGLJList);
  522. await projectGLJModel.insertMany(newProjectGLJList);
  523. }
  524. //组装数据
  525. for(let ration_glj of t_newRationGLJList){
  526. let rkey = getIndex(ration_glj);
  527. let pglj = projectGLJMap[rkey];
  528. let subList = [];
  529. setUnitPrice(pglj,unitPriceMap);
  530. if(existMixRatioMap[rkey]){//如果有组成物
  531. for(let m of existMixRatioMap[rkey]){
  532. let mpglj = projectGLJMap[getIndex(m)]
  533. if(mpglj){
  534. let cglj = _.clone(mpglj);
  535. setUnitPrice(cglj,unitPriceMap);
  536. cglj.ratio_data = m;
  537. subList.push(cglj);
  538. }else{
  539. throw `组成物${m.name}对应的项目工料机没有找到`;
  540. }
  541. }
  542. pglj.subList =subList;
  543. }
  544. ration_glj.projectGLJID = pglj.id;
  545. newRationGLJList.push(ration_glj_facade.createNewRecord(ration_glj));
  546. projectGLJList.push(pglj);
  547. }
  548. return [newRationGLJList, projectGLJList];
  549. function setUnitPrice(p,unitPriceMap){
  550. p.unit_price = unitPriceMap[getIndex(p)];
  551. }
  552. }
  553. async function getUnitPriceData(newProjectGLJList,gljCodes,unitPriceFileId){
  554. let unitPriceMap = {};
  555. let newUnitPriceList = [];
  556. let unitPriceList = await unitPriceModel.find({unit_price_file_id: unitPriceFileId,'code':{'$in':gljCodes}}).lean();
  557. for(let u of unitPriceList){
  558. unitPriceMap[getIndex(u)]=u;
  559. }
  560. for(let np of newProjectGLJList){
  561. let pkey = getIndex(np);
  562. if(unitPriceMap[pkey]) continue;
  563. let insertData = {
  564. code: np.code,
  565. base_price: np.base_price,
  566. market_price: np.market_price,
  567. unit_price_file_id: unitPriceFileId,
  568. name: np.name,
  569. taxRate:np.taxRate,
  570. specs:np.specs?np.specs:'',
  571. original_code:np.original_code,
  572. unit:np.unit?np.unit:'',
  573. type: np.type,
  574. short_name: np.shortName !== undefined ? np.shortName : '',
  575. glj_id: np.glj_id,
  576. is_add:0,
  577. grossWeightCoe:np.grossWeightCoe,
  578. purchaseStorageRate:np.purchaseStorageRate,
  579. offSiteTransportLossRate:np.offSiteTransportLossRate,
  580. handlingLossRate:np.handlingLossRate
  581. };
  582. if(np.from=='cpt') insertData.is_add=1;//如果是来自补充工料机,则都添加新增标记
  583. if(insertData.code != insertData.original_code) insertData.is_add=1;//添加的时候如果是复制整块来的,可能在源项目中是新增的工料机,这里也要添上(暂时可能还用不到)
  584. newUnitPriceList.push(insertData);
  585. unitPriceMap[pkey] = insertData;
  586. }
  587. if(newUnitPriceList.length > 0) await setIDfromCounter("unit_price",newUnitPriceList);
  588. return [unitPriceMap,newUnitPriceList];
  589. }
  590. async function getMixRatioInfo(projectID,projectGLJMap,newProjectGLJList,mixRatioMap,connectKeyList,unitPriceFileId,ext){//取组成物信息,得到缺少的组成物情况
  591. let missCodeList = []; //所有组成物信息的编码,用来统一查询对应的项目工料机是否存在
  592. let existMixRatioMap ={};
  593. let codeMap={};//用来去重
  594. let mixRatioInsertData = [];
  595. // 1. 先检查现在的组成物表中,是否有相关信息 - 生成映射记录
  596. if(connectKeyList.length > 0){//有组成物的话从数据库中取出组成物信息
  597. let mixRatioList = await mixRatioModel.find({'unit_price_file_id': unitPriceFileId,'connect_key': {'$in':connectKeyList}}).lean();
  598. for(let m of mixRatioList){
  599. //组成物信息分组,查看哪些是已经存在的
  600. existMixRatioMap[m.connect_key]?existMixRatioMap[m.connect_key].push(m):existMixRatioMap[m.connect_key]=[m];
  601. //查看组成物对应的项目工料机是否存在,如果不存在,要插入项目工料机
  602. let mkey = getIndex(m);
  603. if(!projectGLJMap[mkey] && !codeMap[m.code]){//如果之前查出来的项目工料机中不包含组成物的信息,要加到missCode里面再查找一次项目工料机看是否存在
  604. missCodeList.push(m.code);
  605. codeMap[m.code] = true;
  606. }
  607. }
  608. // 2 将第一步得到的映射表 与在标准库查询父工料机得到的映射表对比,得出哪些组物成还需要添加,获得库ID
  609. let stdIDs = [];
  610. let comIDs = [];
  611. let missMixRatioGroup = [];
  612. for(let ck of connectKeyList){//查看项目中组成物信息是否已经存在,如果不存在,则用插定额时获取的组成物信息从数据库中获取
  613. if(!existMixRatioMap[ck] && mixRatioMap[ck] && mixRatioMap[ck].length > 0){//组成物信息不存在
  614. let pglj = projectGLJMap[ck];//取出父数据
  615. let from = pglj.from === undefined|| pglj.from ===null || pglj.from === ""?'std' : pglj.from;
  616. for(let c of mixRatioMap[ck]){
  617. if(from == "std"){//标准的工料机只来自标准的
  618. stdIDs.push(c.ID);
  619. }else{
  620. c.isStd?stdIDs.push(c.ID):comIDs.push(c.ID);
  621. }
  622. }
  623. missMixRatioGroup.push({'connect_key':ck,'list':mixRatioMap[ck],'from':from});
  624. }
  625. }
  626. //3.统一查询所有组成物在标准库中的详细信息
  627. let stdMixMap = {};
  628. //整理需插入的组成物列表的数据
  629. //来自标准工料机
  630. if(stdIDs.length > 0){
  631. stdIDs = _.uniq(stdIDs);//去重
  632. let stdMixList = await std_glj_lib_gljList_model.find({'ID':{'$in':stdIDs}}).lean();
  633. for(let sm of stdMixList){
  634. stdMixMap[sm.ID] = sm;
  635. let skey = getIndex(sm,['code','name','specs','unit','gljType']);
  636. if(!projectGLJMap[skey] && !codeMap[sm.code]){
  637. missCodeList.push(sm.code);
  638. codeMap[sm.code] = true;
  639. }
  640. }
  641. }
  642. //来自组成物工料机
  643. let comMixMap = {};
  644. if(comIDs.length > 0){
  645. comIDs = _.uniq(comIDs);//去重
  646. let comMixList = await complementaryGljLibModel.find({'ID':{'$in':comIDs}}).lean();
  647. for(let cm of comMixList){
  648. comMixMap[cm.ID] = cm;
  649. let ckey = getIndex(cm,['code','name','specs','unit','gljType']);
  650. if(!projectGLJMap[ckey] && codeMap[cm.code]){
  651. missCodeList.push(cm.code);
  652. codeMap[cm.code] = true;
  653. }
  654. }
  655. }
  656. //4.生成需要插入组成物表的数据
  657. for(let mg of missMixRatioGroup){//整理需要插入组成物列表的数据
  658. for(let tc of mg.list){
  659. let consumpiton = tc.consumeAmt;
  660. //只有标准的工料机的组成物才会有多单价、多组成物消耗量的情况
  661. if(mg.from == 'std' && ext && ext.quantityField &&( tc.consumeAmtProperty[ext.quantityField]!= undefined && tc.consumeAmtProperty[ext.quantityField]!=null)){
  662. consumpiton = tc.consumeAmtProperty[ext.quantityField];
  663. }
  664. let mfrom = mg.from == 'std' || tc.isStd?'std':'cpt';
  665. let tmp = mfrom == 'std'?stdMixMap[tc.ID]:comMixMap[tc.ID];//取出之前库中查到的工料机
  666. let mixRatioData = {
  667. consumption: consumpiton,
  668. glj_id: tmp.ID,
  669. unit_price_file_id: unitPriceFileId,
  670. connect_key: mg.connect_key,
  671. type: tmp.gljType,
  672. code: tmp.code,
  673. specs:tmp.specs?tmp.specs:"",
  674. name:tmp.name,
  675. unit:tmp.unit?tmp.unit:'',
  676. from:mfrom
  677. };
  678. mixRatioInsertData.push(mixRatioData);
  679. }
  680. }
  681. if(mixRatioInsertData.length > 0) await setIDfromCounter("mix_ratio",mixRatioInsertData,existMixRatioMap,'connect_key');
  682. //await mixRatioModel.insertMany(mixRatioInsertData); 因为没有事务添加组成物数据要放在添加单价文件数据之后
  683. //5.查询组成物对应的项目工料机是否存在,如果不存在,生成项目工料机信息
  684. let projectGLJList = await projectGLJModel.find({'project_id':projectID,'code':{'$in':missCodeList}}).lean();
  685. for(let pg of projectGLJList){
  686. let pindex = getIndex(pg);
  687. projectGLJMap[pindex] = pg;
  688. }
  689. let lessMix = [];//组成物表存在,项目工料机不存在的数据
  690. let lessMixMap = {};//防止重复添加
  691. for(let connect_key in existMixRatioMap){
  692. let mixRatios = existMixRatioMap[connect_key];
  693. for(let m of mixRatios){
  694. let mk = getIndex(m);
  695. if(!projectGLJMap[mk] && !lessMixMap[mk]){//如果组成物对应的项目工料机不存在
  696. let nglj = null;
  697. if(m.from == 'std'){//这里有值,说明是刚添加到组成物文件中的数据
  698. nglj = stdMixMap[m.glj_id];
  699. }else if(m.from == 'cpt'){//这里有值,说明是刚添加到组成物文件中的数据
  700. nglj = comMixMap[m.glj_id];
  701. }
  702. if(nglj){
  703. nglj.from = m.from;
  704. let np = getProjectGLJNewData(nglj,projectID,ext);
  705. newProjectGLJList.push(np);
  706. projectGLJMap[mk] = np;
  707. }else{//这里没找到,说明是组成物文件里有,但是项目工料机没有的数据
  708. lessMix.push(m);
  709. }
  710. lessMixMap[mk] = true;//只要处理过一次,就不用再重新处理了,机械组成物,比如柴油这些,会出现多次
  711. }
  712. }
  713. }
  714. //6. 组成物文件里有,但是项目工料机没有的数据(共用单价文件等情况产生)
  715. let lessIDList=[];
  716. let uniqMap ={};//去重
  717. let lessStdMix = [];//防止组成物中改了名称等,但是通过glj_id取出来的是还没改前的原始数据
  718. if(lessMix.length > 0){
  719. for(let lm of lessMix){
  720. let parentglj = projectGLJMap[lm.connect_key];
  721. if(!parentglj) throw `含有组成物工料机${lm.connect_key},没有找到,添加定额失败`;
  722. if((parentglj.from == "std" || lm.from == "std") && lm.code!="80CCS"){//车船税特殊处理
  723. if(!uniqMap[lm.glj_id]){
  724. lessIDList.push(lm.glj_id);
  725. uniqMap[lm.glj_id] = lm;
  726. }
  727. lessStdMix.push(lm);
  728. }else {//来自组成物的直接设置
  729. lm.from = 'cpt';
  730. lm.gljType = lm.type;
  731. let t_mg = getProjectGLJNewData(lm,projectID);
  732. newProjectGLJList.push(t_mg);
  733. projectGLJMap[getIndex(lm)] = t_mg;
  734. }
  735. }
  736. }
  737. if(lessIDList.length > 0){
  738. let less_stds = await std_glj_lib_gljList_model.find({'ID':{'$in':lessIDList}}).lean();
  739. let less_stds_map = {};
  740. for(let les of less_stds){
  741. less_stds_map[les.ID] = les;
  742. }
  743. for(let t_l_m of lessStdMix){
  744. let t_nglj = less_stds_map[t_l_m.glj_id];
  745. t_nglj.from = 'std';
  746. //防止组成物中改了名称等,但是通过glj_id取出来的是还没改前的原始数据
  747. t_nglj.name = t_l_m.name;
  748. t_nglj.code = t_l_m.code;
  749. t_nglj.gljType = t_l_m.type;
  750. t_nglj.specs = t_l_m.specs;
  751. t_nglj.unit = t_l_m.unit;
  752. let t_np = getProjectGLJNewData(t_nglj,projectID,ext);
  753. newProjectGLJList.push(t_np);
  754. projectGLJMap[getIndex(t_l_m)] = t_np;
  755. }
  756. }
  757. }
  758. return [existMixRatioMap,mixRatioInsertData,missCodeList]
  759. }
  760. function getProjectGLJNewData(tmp,projectId,ext){
  761. let gljData = {
  762. glj_id: tmp.ID,
  763. repositoryId:tmp.repositoryId,
  764. project_id: projectId,
  765. code: tmp.code,
  766. name: tmp.name,
  767. specs: tmp.specs?tmp.specs:'',
  768. unit: tmp.unit === undefined ? '' : tmp.unit,
  769. type: tmp.gljType,
  770. adjCoe:tmp.adjCoe,
  771. original_code:tmp.code,
  772. materialType: tmp.materialType, //三材类别
  773. materialCoe: tmp.materialCoe,
  774. base_price: tmp.basePrice,
  775. market_price: tmp.basePrice,
  776. is_evaluate:0,
  777. is_eval_material:0,
  778. no_tax_eqp:0,
  779. is_adjust_price:0,
  780. is_main_material:0,
  781. is_contractor_material:0,
  782. supply_quantity:0,
  783. supply:0,
  784. from:tmp.from?tmp.from:"std"
  785. };
  786. // 现在定额库可以引用其他费用定额的,比如广东可能套用部颁的定额,因此就算广东费用定额是多单价的,也可能会引用单个单价的人材机
  787. if(gljData.from == 'std' && ext && ext.priceField &&( tmp.priceProperty && tmp.priceProperty[ext.priceField]!= undefined && tmp.priceProperty[ext.priceField]!=null)){
  788. const basePrice = scMathUtil.roundTo(tmp.priceProperty[ext.priceField],-6);
  789. gljData.base_price = basePrice;
  790. gljData.market_price = basePrice;
  791. }
  792. return gljData;
  793. }
  794. async function setIDfromCounter(name,list,map,keyfield){//map,keyfield
  795. let update = {$inc: {sequence_value: list.length}};
  796. let condition = {_id: name};
  797. let options = {new: true};
  798. // 先查找更新
  799. let counter = await counterModel.findOneAndUpdate(condition, update, options);
  800. let firstID = counter.sequence_value - (list.length - 1);
  801. for(let a of list){
  802. a.id = firstID;
  803. firstID+=1
  804. if(map && keyfield){
  805. let key = a[keyfield];
  806. map[key]?map[key].push(a):map[key]=[a]
  807. }
  808. }
  809. }
  810. async function deleRationSubRecode(projectID,rationID,cleanzmhs=false) {//删除挂在定额下的数据,如工程量明细,定额工料机等
  811. let delete_query={projectID: projectID, rationID: rationID};
  812. //删除工程量明细
  813. await deleteSubListByQuery(delete_query,cleanzmhs) ;
  814. }
  815. async function deleteSubListByQuery(delete_query,cleanzmhs=false) {
  816. if(cleanzmhs == false){//清空子目换算即cleanzmh==true时不需要清空工程量明细、模板关联子目、安装增加费
  817. await quantity_detail.deleteByQuery(delete_query) ;//删除工程量明细
  818. await rationInstallationModel.deleteMany(delete_query);//删除安装增加费
  819. await rationTemplateModel.deleteMany(delete_query);//删除模板关联子目
  820. }
  821. //to do稳定土也要删除
  822. await ration_coe.deleteMany(delete_query);//删除附注条件
  823. await ration_glj.deleteMany(delete_query);//删除定额工料机
  824. }
  825. async function updateCoeAdjust(data,compilation) {
  826. let replace = [],projectGLJList=[];
  827. await ration_coe.update({ID:data.ID},data.doc);
  828. //添加单个工料机的情况
  829. if (data.add.length > 0){
  830. let [tg,pl] = await ration_glj_facade.insertAddTypeGLJ(data.add,compilation);
  831. if(pl.length > 0) projectGLJList = projectGLJList.concat(pl);
  832. }
  833. if(data.delete.length > 0) await ration_glj_facade.deleteGLJ(data.delete);
  834. //替换工料机的情况
  835. if (data.replace.length > 0){
  836. for(let r of data.replace){
  837. let [rg,pg] = await ration_glj_facade.replaceGLJByData(r,compilation);
  838. replace.push(rg) ;
  839. projectGLJList.push(pg);
  840. }
  841. }
  842. let cal_result = await glj_calculate_facade.calculateQuantity({projectID:data.projectID,rationID:data.rationID},null,true);
  843. let coe = {
  844. query:{ID:data.ID,projectID:data.projectID},
  845. doc:data.doc
  846. };
  847. let ration_glj ={
  848. quantityRefresh:true,
  849. glj_result:cal_result.glj_result
  850. };
  851. let ration = {
  852. ID:cal_result.rationID,
  853. adjustState:cal_result.adjustState,
  854. name:cal_result.rationName
  855. };
  856. return {coe:coe,ration_glj:ration_glj,ration:ration,add:data.add,delete:data.delete,replace:replace,projectGLJList:projectGLJList}
  857. }
  858. async function updateRation(std,defaultLibID,rationID,billsItemID,projectID,calQuantity,cleanzmh=false) {
  859. // insertNewRation
  860. let ration ={};
  861. ration.code = std.code;
  862. ration.name = std.name;
  863. ration.caption = std.caption;
  864. ration.unit = std.unit;
  865. if (std.type === 'std') {
  866. ration.libID = std.rationRepId;
  867. ration.stdID = std.ID;
  868. }
  869. ration.content = std.jobContent;
  870. ration.manageFeeRate = std.manageFeeRate;
  871. ration.adjustState = '';
  872. ration.isFromDetail=0;
  873. ration.isSubcontract=false;
  874. ration.fees=[];
  875. if (std.chapter) {
  876. ration.comments = std.chapter.explanation;
  877. ration.ruleText = std.chapter.ruleText;
  878. }
  879. ration.from = std.type === 'complementary' ? 'cpt' : 'std';
  880. //定额前缀 none:0, complementary:1, borrow: 2
  881. ration.prefix = '';
  882. if(parseInt(defaultLibID) !== std.rationRepId){//定额是默认定额库中的时,只显示编号;
  883. ration.prefix = '借';//定额不是默认定额库中的、且不是补充定额库中的时, 在定额编号前显示“借”。
  884. if(ration.from === 'cpt') ration.prefix = '补';//定额是补充定额库中的时,在定额编号前显示“补”;
  885. }
  886. ration.rationAssList = createRationAss(std);//生成辅助定额
  887. if(cleanzmh==false){//如果是清空子目换算,即cleanzmh==true 保留定额工程量、工程量表达式、含量(分解系数)、取费专业(取费类别)
  888. if(std.feeType == undefined || std.feeType == null || std.feeType ==''){//定额取费专业为空的情况下,取项目属性中的定额取费专业ID
  889. ration.programID = await getProgramForProject(projectID);
  890. }else {
  891. ration.programID = std.feeType;
  892. }
  893. if(calQuantity){
  894. await CalculateQuantity(ration,billsItemID,projectID);
  895. }
  896. }
  897. let unsetObject = {
  898. "marketUnitFee":1,
  899. 'marketTotalFee':1,
  900. "maskName":1
  901. };
  902. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration,"$unset":unsetObject},{new: true});//;
  903. return newRation;
  904. }
  905. async function setEmptyRation(projectID,rationID){
  906. let ration ={};
  907. ration.code = "";
  908. ration.name = "";
  909. ration.caption = "";
  910. ration.unit = "";
  911. ration.libID = null;
  912. ration.content = "";
  913. ration.adjustState = '';
  914. ration.isFromDetail=0;
  915. ration.isSubcontract=false;
  916. ration.fees=[];
  917. ration.comments = "";
  918. ration.ruleText = "";
  919. ration.quantity="";
  920. ration.contain="";
  921. ration.quantityEXP="";
  922. ration.from = 'std';
  923. //定额前缀 none:0, complementary:1, borrow: 2
  924. ration.prefix = '';
  925. ration.rationAssList = [];
  926. ration.marketUnitFee ="";
  927. ration.marketTotalFee ="";
  928. ration.maskName = "";
  929. ration.targetTotalFee ='';
  930. ration.targetUnitFee = "";
  931. ration.deleteInfo = null;
  932. ration.quantityCoe = {};
  933. ration.rationQuantityCoe="";
  934. ration.tenderQuantity = "";
  935. ration.programID = null;
  936. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration},{new: true});//;
  937. return {ration:newRation,ration_gljs:[],ration_coes:[],ration_installs:[]};
  938. }
  939. function createRationAss(std) {
  940. let rationAssList = [];//生成辅助定额
  941. if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
  942. let assGroup = _.groupBy(std.rationAssList,'name');
  943. for(let key in assGroup){
  944. let assList = assGroup[key];
  945. let ass = assList[0];
  946. ass._doc.actualValue = ass.stdValue;
  947. ass._doc.isAdjust = 0;
  948. if(_.isString(ass._doc.assistCode)) ass._doc.assistCode = ass._doc.assistCode.replace("\n","");
  949. if(_.isString(ass._doc.thirdRationCode)) ass._doc.thirdRationCode = ass._doc.thirdRationCode.replace("\n","");
  950. if(assList.length > 1){
  951. ass._doc.groupList = JSON.parse(JSON.stringify(assList)) ;
  952. ass._doc.maxValue = assList[assList.length-1]._doc.maxValue;
  953. }
  954. rationAssList.push(ass);
  955. }
  956. }
  957. return rationAssList;
  958. }
  959. async function CalculateQuantity (ration,billsItemID,projectID) {
  960. // calculate ration Quantity
  961. let project = await projectModel.findOne({ID:projectID});
  962. let decimalObject =await decimal_facade.getProjectDecimal(projectID,project);
  963. let quantity_decimal = (decimalObject&&decimalObject.ration&&decimalObject.ration.quantity)?decimalObject.ration.quantity:3;
  964. let pbill = await bill_model.model.findOne({projectID:projectID,ID:billsItemID});
  965. let t_unit = ration.unit?ration.unit.replace(/^\d+/,""):"";
  966. if(t_unit!=pbill.unit){//如果定额工程量的单位去除前面的数字后不等于清单单位,定额工程量保持不变
  967. return ;
  968. }
  969. let billsQuantity = pbill.quantity ? pbill.quantity : 0;
  970. let bill_decimal = await decimal_facade.getBillsQuantityDecimal(projectID,pbill.unit,project);
  971. billsQuantity=scMathUtil.roundForObj(billsQuantity,bill_decimal);
  972. ration.quantityEXP="QDL";
  973. ration.quantity = scMathUtil.roundForObj(billsQuantity / FilterNumberFromUnit(ration.unit),quantity_decimal);//不管是否打勾都做转换
  974. ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,6);
  975. };
  976. async function getProgramForProject(projectID){
  977. let project = await projectModel.findOne({ID:projectID});
  978. return project.property.engineering;
  979. }
  980. function FilterNumberFromUnit (unit) {
  981. let reg = new RegExp('^[0-9]+');
  982. if (reg.test(unit)) {
  983. return parseInt(unit.match(reg)[0]);
  984. } else {
  985. return 1;
  986. }
  987. };
  988. function getIndex(obj,tpops){
  989. let pops = tpops?tpops:['code','name','specs','unit','type'];
  990. let t_index = '';
  991. let k_arr=[];
  992. for(let p of pops){
  993. let tmpK = (obj[p]==undefined||obj[p]==null||obj[p]=='')?'null':obj[p];
  994. k_arr.push(tmpK);
  995. }
  996. t_index=k_arr.join("|-|");
  997. return t_index;
  998. }