ration_facade.js 10 KB

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