ration_facade.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * Created by zhang on 2018/2/9.
  3. */
  4. let mongoose = require('mongoose');
  5. import SearchDao from '../../complementary_ration_lib/models/searchModel';
  6. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  7. let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
  8. let quantity_detail = require("../facade/quantity_detail_facade");
  9. let ration_glj = mongoose.model('ration_glj');
  10. let ration_coe = mongoose.model('ration_coe');
  11. let ration_model = require('../models/ration');
  12. let bill_model = require('../models/bills');
  13. let decimal_facade = require('./decimal_facade');
  14. import installationFeeModel from "../models/installation_fee";
  15. import rationInstallationModel from "../models/ration_installation";
  16. const uuidV1 = require('uuid/v1');
  17. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  18. let coeMolde = mongoose.model('std_ration_lib_coe_list');
  19. let _= require('lodash');
  20. module.exports = {
  21. replaceRations: replaceRations,
  22. addNewRation:addNewRation
  23. };
  24. async function addNewRation(data) {
  25. let query = data.itemQuery;
  26. let stdRation = null;
  27. if(query){
  28. let searchDao = new SearchDao();
  29. stdRation = await searchDao.getRationItem(query.userID,query.rationRepId,query.code);
  30. data.newData.code = query.code;
  31. }
  32. if(data.brUpdate.length>0){
  33. await updateSerialNo(data.brUpdate);
  34. }
  35. let newRation =await insertNewRation(data.newData,stdRation,data.calQuantity);
  36. if(stdRation){
  37. return await addRationSubList(stdRation,newRation,data.needInstall);
  38. }else {
  39. return {ration:newRation};
  40. }
  41. }
  42. async function updateSerialNo(serialNoUpdate){
  43. let tasks=[];
  44. for(let data of serialNoUpdate){
  45. let task={
  46. updateOne:{
  47. filter:{
  48. ID:data.ID,
  49. projectID:data.projectID
  50. },
  51. update :{
  52. serialNo:data.serialNo
  53. }
  54. }
  55. };
  56. tasks.push(task);
  57. }
  58. await ration_model.model.bulkWrite(tasks);
  59. }
  60. async function insertNewRation(newData,std,calQuantity) {//插入新的定额
  61. if(std){
  62. newData.name = std.name;
  63. newData.caption = std.caption;
  64. newData.unit = std.unit;
  65. newData.libID = std.rationRepId;
  66. newData.content = std.jobContent;
  67. if (std.chapter) {
  68. newData.comments = std.chapter.explanation;
  69. newData.ruleText = std.chapter.ruleText;
  70. }
  71. newData.from = std.type === 'complementary' ? 'cpt' : 'std';
  72. newData.programID = std.feeType;
  73. newData.rationAssList = createRationAss(std);
  74. // calculate ration Quantity
  75. }
  76. if(calQuantity){
  77. await CalculateQuantity(newData,newData.billsItemID,newData.projectID);
  78. }
  79. let newRation = await ration_model.model.create(newData);
  80. return newRation;
  81. }
  82. async function replaceRations(userID,data) {
  83. let searchDao = new SearchDao();
  84. let recodes = [];
  85. for(let recode of data.nodeInfo){
  86. let stdRation = await searchDao.getRationItem(userID,data.libID,recode.newCode);
  87. let newRecode = await replaceRation(recode,stdRation,data.projectID,data.calQuantity);
  88. if(newRecode){
  89. recodes.push(newRecode);
  90. }else {
  91. break;
  92. }
  93. }
  94. return recodes;
  95. }
  96. async function replaceRation(nodeInfo,stdRation,projectID,calQuantity) {
  97. if(stdRation){
  98. await deleRationSubRecode(projectID,nodeInfo.ID);
  99. let newRation = await updateRation(stdRation,nodeInfo.ID,nodeInfo.billsItemID,projectID,calQuantity);//生成并插入新的定额
  100. return await addRationSubList(stdRation,newRation,nodeInfo.needInstall);
  101. }else {
  102. return null;
  103. }
  104. }
  105. async function addRationSubList(stdRation,newRation,needInstall) {
  106. let ration_gljs = await addRationGLJ(stdRation,newRation);
  107. let ration_coes = await addRationCoe(stdRation,newRation);
  108. let ration_installs = [];
  109. if(needInstall){
  110. ration_installs = await addRationInstallFee(stdRation,newRation);
  111. }
  112. return {ration:newRation,ration_gljs:ration_gljs,ration_coes:ration_coes,ration_installs:ration_installs};
  113. }
  114. async function addRationInstallFee(std,newRation) {
  115. let install_fee_list = [];
  116. if(std.hasOwnProperty('rationInstList') && std.rationInstList.length > 0){
  117. let installFee = await installationFeeModel.findOne({'projectID': newRation.projectID});
  118. for(let ri of std.rationInstList){
  119. let feeItem = _.find(installFee.installFeeItem,{'ID':ri.feeItemId});
  120. let section = _.find(installFee.installSection,{'ID':ri.sectionId});
  121. if(feeItem&&section){
  122. let tem_r_i = {
  123. libID:installFee.libID,
  124. projectID:newRation.projectID,
  125. rationID:newRation.ID,
  126. feeItemId:feeItem.ID,
  127. sectionId:section.ID,
  128. itemName:feeItem.feeItem,
  129. feeType:feeItem.feeType,
  130. sectionName:section.name,
  131. unifiedSetting:1,
  132. ruleId:''
  133. };
  134. if(feeItem.isCal==1&&section.feeRuleId&&section.feeRuleId!=''){//勾选记取时并且有规则ID时才读取
  135. let feeRule = _.find(installFee.feeRule,{'ID':section.feeRuleId});
  136. if(feeRule){
  137. tem_r_i.ruleId = feeRule.ID;
  138. }
  139. }
  140. tem_r_i.ID = uuidV1();
  141. install_fee_list.push(tem_r_i);
  142. }
  143. }
  144. if(install_fee_list.length>0){
  145. await rationInstallationModel.insertMany(install_fee_list);
  146. }
  147. }
  148. return install_fee_list;
  149. }
  150. async function addRationCoe(std,newRation) {
  151. let ration_coe_list = [];
  152. let seq = 0;
  153. if(std.hasOwnProperty('rationCoeList')&&std.rationCoeList.length>0){//添加标准库的工料机
  154. for(let sub of std.rationCoeList){
  155. let libCoe = await coeMolde.findOne({'libID':std.rationRepId,'ID':sub.ID,"$or": [{"isDeleted": null}, {"isDeleted": false}]});//std.rationRepId;
  156. if(libCoe){
  157. let newCoe = {};
  158. newCoe.ID = uuidV1();
  159. newCoe.coeID = sub.ID;
  160. newCoe.seq = seq;
  161. newCoe.name = libCoe.name;
  162. newCoe.content = libCoe.content;
  163. newCoe.isAdjust=0;
  164. newCoe.coes = libCoe.coes;
  165. newCoe.rationID = newRation.ID;
  166. newCoe.projectID = newRation.projectID;
  167. seq++;
  168. ration_coe_list.push(newCoe);
  169. }
  170. }
  171. }
  172. let lastCoe ={
  173. coeID:-1,
  174. name : '自定义系数',
  175. content:'人工×1,材料×1,机械×1,主材×1,设备×1',
  176. isAdjust:0,
  177. seq:seq,
  178. rationID : newRation.ID,
  179. projectID : newRation.projectID
  180. };
  181. lastCoe.ID = uuidV1();
  182. lastCoe.coes = getCustomerCoeData();
  183. ration_coe_list.push(lastCoe);
  184. await ration_coe.insertMany(ration_coe_list);
  185. return ration_coe_list;
  186. }
  187. function getCustomerCoeData() {
  188. var coeList = [];
  189. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'定额'});
  190. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'人工'});
  191. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'材料'});
  192. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'机械'});
  193. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'主材'});
  194. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'设备'});
  195. return coeList;
  196. };
  197. async function addRationGLJ(std,newRation) {
  198. let newRationGLJList = [];
  199. let rationGLJShowList = [];
  200. if(std.hasOwnProperty('rationGljList') && std.rationGljList.length > 0){
  201. for(let sub of std.rationGljList){
  202. let newGLJ = {};
  203. newGLJ.ID = uuidV1();
  204. newGLJ.projectID = newRation.projectID;
  205. newGLJ.GLJID = sub.gljId;
  206. newGLJ.rationID = newRation.ID;
  207. newGLJ.billsItemID = newRation.billsItemID;
  208. newGLJ.rationItemQuantity = sub.consumeAmt;
  209. newGLJ.quantity = sub.consumeAmt;
  210. newGLJ.glj_repository_id = std.rationRepId;
  211. let std_glj = await std_glj_lib_gljList_model.findOne({'ID':sub.gljId});
  212. if(std_glj){
  213. newGLJ.name = std_glj.name;
  214. newGLJ.code = std_glj.code;
  215. newGLJ.original_code = std_glj.code;
  216. newGLJ.unit = std_glj.unit;
  217. newGLJ.specs = std_glj.specs;
  218. newGLJ.basePrice = std_glj.basePrice;
  219. newGLJ.shortName = std_glj.shortName;
  220. newGLJ.type = std_glj.gljType;
  221. newGLJ.repositoryId = std_glj.repositoryId;
  222. newGLJ.adjCoe = std_glj.adjCoe;
  223. let info = await ration_glj_facade.getInfoFromProjectGLJ(newGLJ);
  224. newGLJ = ration_glj_facade.createNewRecord(info);
  225. newRationGLJList.push(newGLJ);
  226. rationGLJShowList.push(info);
  227. }
  228. }
  229. }
  230. if(newRationGLJList.length>0){
  231. await ration_glj.insertMany(newRationGLJList);
  232. }
  233. return rationGLJShowList;
  234. }
  235. async function deleRationSubRecode(projectID,rationID) {//删除挂在定额下的数据,如工程量明细,定额工料机等
  236. let delete_query={projectID: projectID, rationID: rationID};
  237. //删除工程量明细
  238. await quantity_detail.deleteByQuery(delete_query) ;
  239. await ration_coe.deleteMany(delete_query);//删除附注条件
  240. await ration_glj.deleteMany(delete_query);//删除定额工料机
  241. await rationInstallationModel.deleteMany(delete_query);//删除安装增加费
  242. }
  243. async function updateRation(std,rationID,billsItemID,projectID,calQuantity) {
  244. // insertNewRation
  245. let ration ={};
  246. ration.code = std.code;
  247. ration.name = std.name;
  248. ration.caption = std.caption;
  249. ration.unit = std.unit;
  250. ration.libID = std.rationRepId;
  251. ration.content = std.jobContent;
  252. ration.adjustState = '';
  253. ration.isFromDetail=0;
  254. ration.isSubcontract=false;
  255. ration.fees=[];
  256. if (std.chapter) {
  257. ration.comments = std.chapter.explanation;
  258. ration.ruleText = std.chapter.ruleText;
  259. }
  260. ration.from = std.type === 'complementary' ? 'cpt' : 'std';
  261. ration.programID = std.feeType;
  262. ration.rationAssList = createRationAss(std);//生成辅助定额
  263. if(calQuantity){
  264. await CalculateQuantity(ration,billsItemID,projectID);
  265. }
  266. let unsetObject = {
  267. "marketUnitFee":1,
  268. 'marketTotalFee':1,
  269. "maskName":1
  270. }
  271. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration,"$unset":unsetObject},{new: true});//;
  272. return newRation;
  273. }
  274. function createRationAss(std) {
  275. let rationAssList = [];//生成辅助定额
  276. if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
  277. for(let i=0;i<std.rationAssList.length;i++){
  278. let ass = std.rationAssList[i];
  279. ass.actualValue = ass.stdValue;
  280. rationAssList.push(ass);
  281. }
  282. }
  283. return rationAssList;
  284. }
  285. async function CalculateQuantity (ration,billsItemID,projectID) {
  286. // calculate ration Quantity
  287. let decimalObject =await decimal_facade.getProjectDecimal(projectID);
  288. let quantity_decimal = (decimalObject&&decimalObject.ration&&decimalObject.ration.quantity)?decimalObject.ration.quantity:3;
  289. let pbill = await bill_model.model.findOne({projectID:projectID,ID:billsItemID});
  290. let billsQuantity = pbill.quantity ? pbill.quantity : 0;
  291. let bill_decimal = await decimal_facade.getBillsQuantityDecimal(projectID,pbill.unit);
  292. billsQuantity=scMathUtil.roundForObj(billsQuantity,bill_decimal);
  293. ration.quantityEXP="QDL";
  294. ration.quantity = scMathUtil.roundForObj(billsQuantity / FilterNumberFromUnit(ration.unit),quantity_decimal);//不管是否打勾都做转换
  295. ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,6);
  296. };
  297. function FilterNumberFromUnit (unit) {
  298. let reg = new RegExp('^[0-9]+');
  299. if (reg.test(unit)) {
  300. return parseInt(unit.match(reg)[0]);
  301. } else {
  302. return 1;
  303. }
  304. };