ration_facade.js 24 KB

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