ration_facade.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. module.exports = {
  17. replaceRations: replaceRations
  18. };
  19. async function replaceRations(uerID,data) {
  20. let searchDao = new SearchDao();
  21. let recodes = [];
  22. for(let recode of data.nodeInfo){
  23. let stdRation = await searchDao.getRationItem(uerID,data.libID,recode.newCode);
  24. let newRecode = await replaceRation(recode,stdRation,data.projectID,data.calQuantity);
  25. if(newRecode){
  26. recodes.push(newRecode);
  27. }else {
  28. break;
  29. }
  30. }
  31. return recodes;
  32. }
  33. async function replaceRation(nodeInfo,stdRation,projectID,calQuantity) {
  34. if(stdRation){
  35. await deleRationSubRecode(projectID,nodeInfo.ID);
  36. let newRation = await updateRation(stdRation,nodeInfo.ID,nodeInfo.billsItemID,projectID,calQuantity);//生成并插入新的定额
  37. let ration_gljs = await addRationGLJ(stdRation,newRation);
  38. //console.log(newRation);
  39. //ration_model.model.bulkWrite(rationTask);//删除定额
  40. console.log(newRation);
  41. return newRation;
  42. }else {
  43. return null;
  44. }
  45. }
  46. async function addRationCoe(std,newRation) {
  47. let ration_coe_list = [];
  48. if(std.hasOwnProperty('rationCoeList')&&data.rationCoeList.length>0){
  49. }
  50. var criteria= {};
  51. criteria.ration_coe_list = [];
  52. var dataLength = 0;
  53. if(data.hasOwnProperty('rationCoeList')&&data.rationCoeList.length>0){
  54. dataLength = data.rationCoeList.length
  55. }
  56. for(var i=0;i<=dataLength;i++){;
  57. var newCoe = {
  58. libID :data.rationRepId,
  59. rationID:newRation.ID,
  60. projectID : newRation.projectID
  61. }
  62. if(i==dataLength){
  63. newCoe.coeID=-1;
  64. newCoe.name = '自定义系数';
  65. newCoe.content='人工×1,材料×1,机械×1,主材×1,设备×1';
  66. newCoe.isAdjust=0;
  67. newCoe.coes = this.getCustomerCoeData();
  68. }else {
  69. newCoe.coeID= data.rationCoeList[i].ID;
  70. }
  71. criteria.ration_coe_list.push(newCoe);
  72. }
  73. }
  74. async function addRationGLJ(std,newRation) {
  75. let newRationGLJList = [];
  76. let rationGLJShowList = [];
  77. if(std.hasOwnProperty('rationGljList') && std.rationGljList.length > 0){
  78. for(let sub of std.rationGljList){
  79. let newGLJ = {};
  80. newGLJ.ID = uuidV1();
  81. newGLJ.projectID = newRation.projectID;
  82. newGLJ.GLJID = sub.gljId;
  83. newGLJ.rationID = newRation.ID;
  84. newGLJ.billsItemID = newRation.billsItemID,
  85. newGLJ.rationItemQuantity = sub.consumeAmt;
  86. newGLJ.quantity = sub.consumeAmt;
  87. newGLJ.glj_repository_id = std.rationRepId;
  88. let std_glj = await std_glj_lib_gljList_model.findOne({'ID': ration_glj.GLJID});
  89. if(std_glj){
  90. newGLJ.name = std_glj.name;
  91. newGLJ.code = std_glj.code;
  92. newGLJ.original_code = std_glj.code;
  93. newGLJ.unit = std_glj.unit;
  94. newGLJ.specs = std_glj.specs;
  95. newGLJ.basePrice = std_glj.basePrice;
  96. newGLJ.shortName = std_glj.shortName;
  97. newGLJ.type = std_glj.gljType;
  98. newGLJ.repositoryId = std_glj.repositoryId;
  99. newGLJ.adjCoe = std_glj.adjCoe;
  100. let info = await ration_glj_facade.getInfoFromProjectGLJ(newGLJ);
  101. newGLJ = ration_glj_facade.createNewRecord(info);
  102. newRationGLJList.push(newGLJ);
  103. rationGLJShowList.push(info);
  104. }
  105. }
  106. }
  107. if(newRationGLJList.length>0){
  108. await ration_glj.insertMany(newRationGLJList);
  109. }
  110. return rationGLJShowList;
  111. }
  112. async function deleRationSubRecode(projectID,rationID) {//删除挂在定额下的数据,如工程量明细,定额工料机等
  113. let delete_query={projectID: projectID, rationID: rationID};
  114. //删除工程量明细
  115. await quantity_detail.deleteByQuery(delete_query) ;
  116. await ration_coe.deleteMany(delete_query);//删除附注条件
  117. await ration_glj.deleteMany(delete_query);//删除定额工料机
  118. //todo 删除安装
  119. }
  120. async function updateRation(std,rationID,billsItemID,projectID,calQuantity) {
  121. // insertNewRation
  122. let ration ={};
  123. ration.code = std.code;
  124. ration.name = std.name;
  125. ration.caption = std.caption;
  126. ration.unit = std.unit;
  127. ration.libID = std.rationRepId;
  128. ration.content = std.jobContent;
  129. ration.adjustState = '';
  130. if (std.chapter) {
  131. ration.comments = std.chapter.explanation;
  132. ration.ruleText = std.chapter.ruleText;
  133. }
  134. ration.from = std.type === 'complementary' ? 'cpt' : 'std';
  135. ration.programID = std.feeType;
  136. ration.rationAssList = [];//生成辅助定额
  137. if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
  138. for(var i=0;i<std.rationAssList.length;i++){
  139. var ass = std.rationAssList[i];
  140. ass.actualValue = ass.stdValue;
  141. ration.rationAssList.push(ass);
  142. }
  143. }
  144. if(calQuantity){
  145. await CalculateQuantity(ration,billsItemID,projectID);
  146. }
  147. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},ration,{new: true});
  148. return newRation;
  149. }
  150. async function CalculateQuantity (ration,billsItemID,projectID) {
  151. // calculate ration Quantity
  152. let decimalObject =await decimal_facade.getProjectDecimal(projectID);
  153. let quantity_decimal = (decimalObject&&decimalObject.ration&&decimalObject.ration.quantity)?decimalObject.ration.quantity:3;
  154. let pbill = await bill_model.model.findOne({projectID:projectID,ID:billsItemID});
  155. let billsQuantity = pbill.quantity ? pbill.quantity : 0;
  156. let bill_decimal = await decimal_facade.getBillsQuantityDecimal(projectID,pbill.unit);
  157. billsQuantity=scMathUtil.roundForObj(billsQuantity,bill_decimal);
  158. ration.quantityEXP="QDL";
  159. ration.quantity = scMathUtil.roundForObj(billsQuantity / FilterNumberFromUnit(ration.unit),quantity_decimal);//不管是否打勾都做转换
  160. ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,6);
  161. };
  162. function FilterNumberFromUnit (unit) {
  163. let reg = new RegExp('^[0-9]+');
  164. if (reg.test(unit)) {
  165. return parseInt(unit.match(reg)[0]);
  166. } else {
  167. return 1;
  168. }
  169. };