ration_facade.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 re_ration = await rationItemModel.findOne({rationRepId:std.rationRepId,ID:tem.rationID});
  248. if(re_ration){
  249. let template = {
  250. billID:"",
  251. fxID:"",
  252. quantity:"0",
  253. coe:"0"
  254. };
  255. template.code = re_ration.code;
  256. template.name = re_ration.name;
  257. template.type = tem.type;
  258. template.unit = re_ration.unit;
  259. template.billsLocation = tem.billsLocation;
  260. templateList.push(template)
  261. }
  262. }
  263. }
  264. if(templateList.length > 0){
  265. let ration_template = {};
  266. ration_template.ID = uuidV1();
  267. ration_template.projectID = newRation.projectID;
  268. ration_template.rationID = newRation.ID;
  269. ration_template.createLocation = 1; //默认模板子目分别放在措施项目下
  270. ration_template.templateList = templateList;
  271. await rationTemplateModel.create(ration_template);
  272. return ration_template;
  273. }
  274. return null;
  275. }
  276. async function addRationCoe(std,newRation,compilation) {
  277. let ration_coe_list = [];
  278. let seq = 0;
  279. if(std.hasOwnProperty('rationCoeList')&&std.rationCoeList.length>0){//添加标准库的工料机
  280. for(let sub of std.rationCoeList){
  281. let libCoe;
  282. if (std.type === 'std') {
  283. libCoe = await coeMolde.findOne({'libID':std.rationRepId,'ID':sub.ID,"$or": [{"isDeleted": null}, {"isDeleted": false}]});//std.rationRepId;
  284. } else {
  285. libCoe = await compleCoeModel.findOne({ID: sub.ID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]});
  286. }
  287. if(libCoe){
  288. let newCoe = {};
  289. newCoe.ID = uuidV1();
  290. newCoe.coeID = sub.ID;
  291. newCoe.seq = seq;
  292. newCoe.name = libCoe.name;
  293. newCoe.content = libCoe.content;
  294. newCoe.isAdjust=0;
  295. newCoe.coes = libCoe.coes;
  296. newCoe.rationID = newRation.ID;
  297. newCoe.projectID = newRation.projectID;
  298. seq++;
  299. ration_coe_list.push(newCoe);
  300. }
  301. }
  302. }
  303. let lastCoe = await getCustomerCoe(newRation.projectID,newRation.ID,seq,compilation);
  304. ration_coe_list.push(lastCoe);
  305. await ration_coe.insertMany(ration_coe_list);
  306. return ration_coe_list;
  307. }
  308. function getCustomerCoeData() {
  309. var coeList = [
  310. {amount:1, operator:'*', gljCode:null, coeType:'定额'},
  311. { amount:1, operator:'*', gljCode:null, coeType:'人工'},
  312. { amount:1, operator:'*', gljCode:null, coeType:'材料'},
  313. { amount:1, operator:'*', gljCode:null, coeType:'机械'},
  314. { amount:1, operator:'*', gljCode:null, coeType:'主材'},
  315. { amount:1, operator:'*', gljCode:null, coeType:'设备'}
  316. ];
  317. return coeList;
  318. };
  319. async function getCustomerCoe(projectID,rationID,seq,compilation){//取自定义乘系数,根据编办不同,内容可能不同
  320. //生成默认的自定义乘系数
  321. let lastCoe ={
  322. coeID:-1,
  323. name : '自定义系数',
  324. content:'人工×1,材料×1,机械×1,主材×1,设备×1',
  325. isAdjust:0,
  326. seq:seq,
  327. rationID : rationID,
  328. projectID : projectID
  329. };
  330. lastCoe.ID = uuidV1();
  331. lastCoe.coes = getCustomerCoeData();
  332. try {
  333. //查看编办中有没有重写路径
  334. if(compilation.overWriteUrl && compilation.overWriteUrl!=""){
  335. let overWrite = require("../../.."+compilation.overWriteUrl);
  336. if(overWrite.getCusCoeContent) lastCoe.content = overWrite.getCusCoeContent();
  337. if(overWrite.getCustomerCoeData) lastCoe.coes = overWrite.getCustomerCoeData();
  338. }
  339. return lastCoe
  340. }catch (err){
  341. console.log("读取自定义系数重写文件失败");
  342. console.log(err.message);
  343. return lastCoe
  344. }
  345. }
  346. //对于多单价,多组成物消耗量的编办,通过这个方法获取单价、组成物消耗量的字段,
  347. function getExtendData(property,compilation) {
  348. return projectDao.getExtendData(property,compilation);
  349. }
  350. async function addRationGLJ(std,newRation,compilation) {
  351. let newRationGLJList = [];
  352. let rationGLJShowList = [];
  353. let unitPriceFileId = 0;
  354. let property = await projectDao.getProjectProperty(newRation.projectID);
  355. if(property){
  356. unitPriceFileId = property.unitPriceFile !== undefined ? property.unitPriceFile.id : 0;
  357. }
  358. let ext = getExtendData(property,compilation);
  359. let first = +new Date();
  360. if(std.hasOwnProperty('rationGljList') && std.rationGljList.length > 0){
  361. let stdGLJID =[];//标准工料机ID数组
  362. let cptGLJID=[];//补充工料机ID数组
  363. //let stdGLJID = _.map(std.rationGljList,'gljId');
  364. for(let tem_g of std.rationGljList){
  365. if(tem_g.type == 'complementary'){
  366. cptGLJID.push(tem_g.gljId);
  367. }else {
  368. stdGLJID.push(tem_g.gljId);
  369. }
  370. }
  371. let stdGLJList = stdGLJID.length > 0 ? await std_glj_lib_gljList_model.find({'ID':{'$in':stdGLJID}}):[];//速度优化-------先一次性取出所有的工料机列表
  372. let stdGLJMap = _.indexBy(stdGLJList, 'ID');
  373. let cptGLJList = cptGLJID.length > 0 ? await complementary_glj_model.find({'userId':std.userId,'ID':{'$in':cptGLJID}}):[];
  374. let cptGLJMap = _.indexBy(cptGLJList, 'ID');
  375. let stdGLJMapTime = +new Date();
  376. console.log("找到工料机映射表时间-------------------------------"+(stdGLJMapTime - first));
  377. for(let sub of std.rationGljList){
  378. let newGLJ = {};
  379. newGLJ.ID = uuidV1();
  380. newGLJ.projectID = newRation.projectID;
  381. newGLJ.GLJID = sub.gljId;
  382. newGLJ.rationID = newRation.ID;
  383. newGLJ.billsItemID = newRation.billsItemID;
  384. newGLJ.rationItemQuantity = sub.consumeAmt;
  385. newGLJ.quantity = sub.consumeAmt;
  386. newGLJ.glj_repository_id = std.rationRepId;
  387. let std_glj = null;
  388. if(sub.type == 'complementary'){//有可能来自标准工料机库或补充工料机库
  389. std_glj = cptGLJMap[sub.gljId];
  390. newGLJ.from = 'cpt';
  391. }else {
  392. std_glj = stdGLJMap[sub.gljId];
  393. newGLJ.from = 'std';
  394. //多单价情况处理
  395. if(ext && ext.priceField && std_glj && std_glj.priceProperty){
  396. std_glj.basePrice = std_glj.priceProperty[ext.priceField];
  397. }
  398. }
  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. if(_.isString(ass._doc.assistCode)) ass._doc.assistCode = ass._doc.assistCode.replace("\n","");
  534. rationAssList.push(ass);
  535. }
  536. }
  537. return rationAssList;
  538. }
  539. async function CalculateQuantity (ration,billsItemID,projectID) {
  540. // calculate ration Quantity
  541. let project = await projectModel.findOne({ID:projectID});
  542. let decimalObject =await decimal_facade.getProjectDecimal(projectID,project);
  543. let quantity_decimal = (decimalObject&&decimalObject.ration&&decimalObject.ration.quantity)?decimalObject.ration.quantity:3;
  544. let pbill = await bill_model.model.findOne({projectID:projectID,ID:billsItemID});
  545. let t_unit = ration.unit?ration.unit.replace(/^\d+/,""):"";
  546. if(t_unit!=pbill.unit){//如果定额工程量的单位去除前面的数字后不等于清单单位,定额工程量保持不变
  547. return ;
  548. }
  549. let billsQuantity = pbill.quantity ? pbill.quantity : 0;
  550. let bill_decimal = await decimal_facade.getBillsQuantityDecimal(projectID,pbill.unit,project);
  551. billsQuantity=scMathUtil.roundForObj(billsQuantity,bill_decimal);
  552. ration.quantityEXP="QDL";
  553. ration.quantity = scMathUtil.roundForObj(billsQuantity / FilterNumberFromUnit(ration.unit),quantity_decimal);//不管是否打勾都做转换
  554. ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,6);
  555. };
  556. async function getProgramForProject(projectID){
  557. let project = await projectModel.findOne({ID:projectID});
  558. return project.property.engineering;
  559. }
  560. function FilterNumberFromUnit (unit) {
  561. let reg = new RegExp('^[0-9]+');
  562. if (reg.test(unit)) {
  563. return parseInt(unit.match(reg)[0]);
  564. } else {
  565. return 1;
  566. }
  567. };