ration_facade.js 28 KB

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