ration_facade.js 25 KB

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