ration_facade.js 24 KB

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