ration_facade.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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?[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. template.defaultLocation = tem.billsLocation;
  263. templateList.push(template)
  264. }
  265. }
  266. }
  267. if(templateList.length > 0){
  268. let ration_template = {};
  269. ration_template.ID = uuidV1();
  270. ration_template.projectID = newRation.projectID;
  271. ration_template.rationID = newRation.ID;
  272. ration_template.createLocation = 1; //默认模板子目分别放在措施项目下
  273. ration_template.templateList = templateList;
  274. await rationTemplateModel.create(ration_template);
  275. return ration_template;
  276. }
  277. return null;
  278. }
  279. async function addRationCoe(std,newRation,compilation) {
  280. let ration_coe_list = [];
  281. let seq = 0;
  282. if(std.hasOwnProperty('rationCoeList')&&std.rationCoeList.length>0){//添加标准库的工料机
  283. for(let sub of std.rationCoeList){
  284. let libCoe;
  285. if (std.type === 'std') {
  286. libCoe = await coeMolde.findOne({'libID':std.rationRepId,'ID':sub.ID,"$or": [{"isDeleted": null}, {"isDeleted": false}]});//std.rationRepId;
  287. } else {
  288. libCoe = await compleCoeModel.findOne({ID: sub.ID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]});
  289. }
  290. if(libCoe){
  291. let newCoe = {};
  292. newCoe.ID = uuidV1();
  293. newCoe.coeID = sub.ID;
  294. newCoe.seq = seq;
  295. newCoe.name = libCoe.name;
  296. newCoe.content = libCoe.content;
  297. newCoe.original_code = libCoe.original_code;
  298. newCoe.option_codes = libCoe.option_codes;
  299. newCoe.option_list = libCoe.option_list;
  300. newCoe.isAdjust=0;
  301. newCoe.coes = libCoe.coes;
  302. newCoe.rationID = newRation.ID;
  303. newCoe.projectID = newRation.projectID;
  304. seq++;
  305. ration_coe_list.push(newCoe);
  306. }
  307. }
  308. }
  309. let lastCoe = await getCustomerCoe(newRation.projectID,newRation.ID,seq,compilation);
  310. ration_coe_list.push(lastCoe);
  311. await ration_coe.insertMany(ration_coe_list);
  312. return ration_coe_list;
  313. }
  314. function getCustomerCoeData() {
  315. var coeList = [
  316. {amount:1, operator:'*', gljCode:null, coeType:'定额'},
  317. { amount:1, operator:'*', gljCode:null, coeType:'人工'},
  318. { amount:1, operator:'*', gljCode:null, coeType:'材料'},
  319. { amount:1, operator:'*', gljCode:null, coeType:'机械'},
  320. { amount:1, operator:'*', gljCode:null, coeType:'主材'},
  321. { amount:1, operator:'*', gljCode:null, coeType:'设备'}
  322. ];
  323. return coeList;
  324. };
  325. async function getCustomerCoe(projectID,rationID,seq,compilation){//取自定义乘系数,根据编办不同,内容可能不同
  326. //生成默认的自定义乘系数
  327. let lastCoe ={
  328. coeID:-1,
  329. name : '自定义系数',
  330. content:'人工×1,材料×1,机械×1,主材×1,设备×1',
  331. isAdjust:1,
  332. seq:seq,
  333. rationID : rationID,
  334. projectID : projectID
  335. };
  336. lastCoe.ID = uuidV1();
  337. lastCoe.coes = getCustomerCoeData();
  338. try {
  339. //查看编办中有没有重写路径
  340. if(compilation.overWriteUrl && compilation.overWriteUrl!=""){
  341. let overWrite = require("../../.."+compilation.overWriteUrl);
  342. if(overWrite.getCusCoeContent) lastCoe.content = overWrite.getCusCoeContent();
  343. if(overWrite.getCustomerCoeData) lastCoe.coes = overWrite.getCustomerCoeData();
  344. }
  345. return lastCoe
  346. }catch (err){
  347. console.log("读取自定义系数重写文件失败");
  348. console.log(err.message);
  349. return lastCoe
  350. }
  351. }
  352. //对于多单价,多组成物消耗量的编办,通过这个方法获取单价、组成物消耗量的字段,
  353. function getExtendData(property,compilation) {
  354. return projectDao.getExtendData(property,compilation);
  355. }
  356. async function addRationGLJ(std,newRation,compilation) {
  357. let newRationGLJList = [];
  358. let rationGLJShowList = [];
  359. let unitPriceFileId = 0;
  360. let property = await projectDao.getProjectProperty(newRation.projectID);
  361. if(property){
  362. unitPriceFileId = property.unitPriceFile !== undefined ? property.unitPriceFile.id : 0;
  363. }
  364. let ext = getExtendData(property,compilation);
  365. let first = +new Date();
  366. if(std.hasOwnProperty('rationGljList') && std.rationGljList.length > 0){
  367. let stdGLJID =[];//标准工料机ID数组
  368. let cptGLJID=[];//补充工料机ID数组
  369. //let stdGLJID = _.map(std.rationGljList,'gljId');
  370. for(let tem_g of std.rationGljList){
  371. if(tem_g.type == 'complementary'){
  372. cptGLJID.push(tem_g.gljId);
  373. }else {
  374. stdGLJID.push(tem_g.gljId);
  375. }
  376. }
  377. let stdGLJList = stdGLJID.length > 0 ? await std_glj_lib_gljList_model.find({'ID':{'$in':stdGLJID}}):[];//速度优化-------先一次性取出所有的工料机列表
  378. let stdGLJMap = _.indexBy(stdGLJList, 'ID');
  379. let cptGLJList = cptGLJID.length > 0 ? await complementary_glj_model.find({'userId':std.userId,'ID':{'$in':cptGLJID}}):[];
  380. let cptGLJMap = _.indexBy(cptGLJList, 'ID');
  381. let stdGLJMapTime = +new Date();
  382. console.log("找到工料机映射表时间-------------------------------"+(stdGLJMapTime - first));
  383. for(let sub of std.rationGljList){
  384. let newGLJ = {};
  385. newGLJ.ID = uuidV1();
  386. newGLJ.projectID = newRation.projectID;
  387. newGLJ.GLJID = sub.gljId;
  388. newGLJ.rationID = newRation.ID;
  389. newGLJ.billsItemID = newRation.billsItemID;
  390. newGLJ.rationItemQuantity = sub.consumeAmt;
  391. newGLJ.quantity = sub.consumeAmt;
  392. newGLJ.glj_repository_id = std.rationRepId;
  393. let std_glj = null;
  394. if(sub.type == 'complementary'){//有可能来自标准工料机库或补充工料机库
  395. std_glj = cptGLJMap[sub.gljId];
  396. newGLJ.from = 'cpt';
  397. }else {
  398. std_glj = stdGLJMap[sub.gljId];
  399. newGLJ.from = 'std';
  400. //多单价情况处理
  401. if(ext && ext.priceField && std_glj && std_glj.priceProperty){
  402. std_glj.basePrice = std_glj.priceProperty[ext.priceField];
  403. }
  404. }
  405. if(std_glj){
  406. newGLJ.name = std_glj.name;
  407. newGLJ.code = std_glj.code;
  408. newGLJ.original_code = std_glj.code;
  409. newGLJ.unit = std_glj.unit;
  410. newGLJ.specs = std_glj.specs;
  411. newGLJ.model = std_glj.model;
  412. newGLJ.basePrice = std_glj.basePrice;
  413. newGLJ.marketPrice = std_glj.basePrice;
  414. newGLJ.shortName = std_glj.shortName;
  415. newGLJ.type = std_glj.gljType;
  416. newGLJ.repositoryId = std_glj.repositoryId;
  417. newGLJ.adjCoe = std_glj.adjCoe;
  418. newGLJ.materialType = std_glj.materialType;
  419. newGLJ.materialCoe = std_glj.materialCoe;
  420. newGLJ.createType = 'normal';
  421. let info = await ration_glj_facade.getInfoFromProjectGLJ(newGLJ,unitPriceFileId,ext);
  422. newGLJ = ration_glj_facade.createNewRecord(info);
  423. newRationGLJList.push(newGLJ);
  424. rationGLJShowList.push(info);
  425. }
  426. //let InfoFromProjectGLJ = +new Date();
  427. //console.log("找到项目工料机时间-------------------------------"+(InfoFromProjectGLJ - std_gljTime));
  428. }
  429. }
  430. let before = +new Date();
  431. console.log("总查询时间为-------------------------------"+(before-first));
  432. if(newRationGLJList.length>0){
  433. await ration_glj.insertMany(newRationGLJList);
  434. }
  435. let after = +new Date();
  436. console.log("实际插入时间为-------------------------------"+(after-before));
  437. console.log("总操作时间为-------------------------------"+(after-first));
  438. return rationGLJShowList;
  439. }
  440. async function deleRationSubRecode(projectID,rationID) {//删除挂在定额下的数据,如工程量明细,定额工料机等
  441. let delete_query={projectID: projectID, rationID: rationID};
  442. //删除工程量明细
  443. await deleteSubListByQuery(delete_query) ;
  444. }
  445. async function deleteSubListByQuery(delete_query) {
  446. await quantity_detail.deleteByQuery(delete_query) ;//删除工程量明细
  447. await ration_coe.deleteMany(delete_query);//删除附注条件
  448. await ration_glj.deleteMany(delete_query);//删除定额工料机
  449. await rationInstallationModel.deleteMany(delete_query);//删除安装增加费
  450. await rationTemplateModel.deleteMany(delete_query);//删除模板关联子目
  451. }
  452. async function updateCoeAdjust(data,compilation) {
  453. let replace = [];
  454. await ration_coe.update({ID:data.ID},data.doc);
  455. //添加单个工料机的情况
  456. if (data.add.length > 0) await ration_glj_facade.insertAddTypeGLJ(data.add,compilation);
  457. if(data.delete.length > 0) await ration_glj_facade.deleteGLJ(data.delete);
  458. //替换工料机的情况
  459. if (data.replace.length > 0){
  460. for(let r of data.replace){
  461. replace.push(await ration_glj_facade.replaceGLJByData(r,compilation)) ;
  462. }
  463. }
  464. let cal_result = await glj_calculate_facade.calculateQuantity({projectID:data.projectID,rationID:data.rationID},null,true);
  465. let coe = {
  466. query:{ID:data.ID,projectID:data.projectID},
  467. doc:data.doc
  468. };
  469. let ration_glj ={
  470. quantityRefresh:true,
  471. glj_result:cal_result.glj_result
  472. };
  473. let ration = {
  474. ID:cal_result.rationID,
  475. adjustState:cal_result.adjustState,
  476. name:cal_result.rationName
  477. };
  478. return {coe:coe,ration_glj:ration_glj,ration:ration,add:data.add,delete:data.delete,replace:replace}
  479. }
  480. async function updateRation(std,defaultLibID,rationID,billsItemID,projectID,calQuantity) {
  481. // insertNewRation
  482. let ration ={};
  483. ration.code = std.code;
  484. ration.name = std.name;
  485. ration.caption = std.caption;
  486. ration.unit = std.unit;
  487. if (std.type === 'std') {
  488. ration.libID = std.rationRepId;
  489. ration.stdID = std.ID;
  490. }
  491. ration.content = std.jobContent;
  492. ration.adjustState = '';
  493. ration.isFromDetail=0;
  494. ration.isSubcontract=false;
  495. ration.fees=[];
  496. if (std.chapter) {
  497. ration.comments = std.chapter.explanation;
  498. ration.ruleText = std.chapter.ruleText;
  499. }
  500. ration.from = std.type === 'complementary' ? 'cpt' : 'std';
  501. //定额前缀 none:0, complementary:1, borrow: 2
  502. ration.prefix = '';
  503. //借用优先级比补充高
  504. if(std.rationRepId !== parseInt(defaultLibID)){//借用
  505. ration.prefix = '借';
  506. }
  507. else if(std.rationRepId === defaultLibID && ration.from === 'cpt') {
  508. ration.prefix = '补';
  509. }
  510. if(std.feeType == undefined || std.feeType == null || std.feeType ==''){//定额取费专业为空的情况下,取项目属性中的定额取费专业ID
  511. ration.programID = await getProgramForProject(projectID);
  512. }else {
  513. ration.programID = std.feeType;
  514. }
  515. ration.rationAssList = createRationAss(std);//生成辅助定额
  516. if(calQuantity){
  517. await CalculateQuantity(ration,billsItemID,projectID);
  518. }
  519. let unsetObject = {
  520. "marketUnitFee":1,
  521. 'marketTotalFee':1,
  522. "maskName":1
  523. }
  524. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration,"$unset":unsetObject},{new: true});//;
  525. return newRation;
  526. }
  527. async function setEmptyRation(projectID,rationID){
  528. let ration ={};
  529. ration.code = "";
  530. ration.name = "";
  531. ration.caption = "";
  532. ration.unit = "";
  533. ration.libID = null;
  534. ration.content = "";
  535. ration.adjustState = '';
  536. ration.isFromDetail=0;
  537. ration.isSubcontract=false;
  538. ration.fees=[];
  539. ration.comments = "";
  540. ration.ruleText = "";
  541. ration.quantity="";
  542. ration.contain="";
  543. ration.quantityEXP="";
  544. ration.from = 'std';
  545. //定额前缀 none:0, complementary:1, borrow: 2
  546. ration.prefix = '';
  547. ration.rationAssList = [];
  548. ration.marketUnitFee ="";
  549. ration.marketTotalFee ="";
  550. ration.maskName = "";
  551. ration.targetTotalFee ='';
  552. ration.targetUnitFee = "";
  553. ration.deleteInfo = null;
  554. ration.quantityCoe = {};
  555. ration.rationQuantityCoe="";
  556. ration.tenderQuantity = "";
  557. ration.programID = null;
  558. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration},{new: true});//;
  559. return {ration:newRation,ration_gljs:[],ration_coes:[],ration_installs:[]};
  560. }
  561. function createRationAss(std) {
  562. let rationAssList = [];//生成辅助定额
  563. if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
  564. for(let i=0;i<std.rationAssList.length;i++){
  565. let ass = std.rationAssList[i];
  566. ass._doc.actualValue = ass.stdValue;
  567. if(_.isString(ass._doc.assistCode)) ass._doc.assistCode = ass._doc.assistCode.replace("\n","");
  568. rationAssList.push(ass);
  569. }
  570. }
  571. return rationAssList;
  572. }
  573. async function CalculateQuantity (ration,billsItemID,projectID) {
  574. // calculate ration Quantity
  575. let project = await projectModel.findOne({ID:projectID});
  576. let decimalObject =await decimal_facade.getProjectDecimal(projectID,project);
  577. let quantity_decimal = (decimalObject&&decimalObject.ration&&decimalObject.ration.quantity)?decimalObject.ration.quantity:3;
  578. let pbill = await bill_model.model.findOne({projectID:projectID,ID:billsItemID});
  579. let t_unit = ration.unit?ration.unit.replace(/^\d+/,""):"";
  580. if(t_unit!=pbill.unit){//如果定额工程量的单位去除前面的数字后不等于清单单位,定额工程量保持不变
  581. return ;
  582. }
  583. let billsQuantity = pbill.quantity ? pbill.quantity : 0;
  584. let bill_decimal = await decimal_facade.getBillsQuantityDecimal(projectID,pbill.unit,project);
  585. billsQuantity=scMathUtil.roundForObj(billsQuantity,bill_decimal);
  586. ration.quantityEXP="QDL";
  587. ration.quantity = scMathUtil.roundForObj(billsQuantity / FilterNumberFromUnit(ration.unit),quantity_decimal);//不管是否打勾都做转换
  588. ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,6);
  589. };
  590. async function getProgramForProject(projectID){
  591. let project = await projectModel.findOne({ID:projectID});
  592. return project.property.engineering;
  593. }
  594. function FilterNumberFromUnit (unit) {
  595. let reg = new RegExp('^[0-9]+');
  596. if (reg.test(unit)) {
  597. return parseInt(unit.match(reg)[0]);
  598. } else {
  599. return 1;
  600. }
  601. };