ration_facade.js 28 KB

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