ration.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var Ration = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. var tools = {
  8. formatRationUpdateData: function (data) {
  9. let uData = JSON.parse(JSON.stringify(data));
  10. delete uData.feesIndex;
  11. delete uData.flagsIndex;
  12. // if (uData.fees) {
  13. // for (let fee of uData.fees) {
  14. // fee.unitFee = fee.unitFee.toFixed(2);
  15. // fee.totalFee = fee.totalFee.toFixed(2);
  16. // fee.tenderUnitFee = fee.tenderUnitFee.toFixed(2);
  17. // fee.tenderTotalFee = fee.tenderTotalFee.toFixed(2);
  18. // }
  19. // }
  20. return uData;
  21. }
  22. };
  23. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  24. var ration = function (proj) {
  25. this.project = proj;
  26. this.datas = null;
  27. var sourceType = ModuleNames.ration;
  28. this.getSourceType = function () {
  29. return sourceType;
  30. }
  31. proj.registerModule(ModuleNames.ration, this);
  32. var maxRationID = 0;
  33. this.getNewRationID = function () {
  34. return uuid.v1();
  35. //return maxRationID += 1;
  36. };
  37. this.maxRationID = function (maxID) {
  38. if (arguments.length === 0) {
  39. return maxRationID;
  40. } else {
  41. maxRationID = Math.max(maxID, maxRationID);
  42. }
  43. };
  44. };
  45. // prototype用于定义public方法
  46. ration.prototype.loadData = function (datas) {
  47. var that = this;
  48. this.datas = datas;
  49. // generate Fees & Flags Index,For View & Calculate
  50. this.datas.forEach(function (data) {
  51. data.feesIndex = getFeeIndex(data.fees);
  52. data.flagsIndex = {};
  53. data.flags.forEach(function (flag) {
  54. data.flagsIndex[flag.fieldName] = flag;
  55. });
  56. that.maxRationID(data.ID);
  57. });
  58. };
  59. ration.prototype.setMaxID = function (ID) {
  60. this.maxRationID(ID);
  61. }
  62. ration.prototype.addDatasToList = function (new_datas) {
  63. let me = this;
  64. if(me.datas && Array.isArray(me.datas)){
  65. for(let d of new_datas){
  66. me.datas.push(d);
  67. }
  68. }else {
  69. me.datas = new_datas;
  70. }
  71. };
  72. // refresh after update
  73. ration.prototype.doAfterUpdate = function(err, data){
  74. if(data.stateRefresh){
  75. this.refreshAdjustState(data);
  76. }
  77. if(data.quantityRefresh){
  78. this.refreshQuantity(data);
  79. }
  80. };
  81. ration.prototype.refreshAdjustState = function(data){
  82. this.refreshDatas(data,'adjustState');
  83. if(data.hasOwnProperty('name')){
  84. this.refreshDatas(data,'name')
  85. }
  86. };
  87. ration.prototype.refreshQuantity = function(data){
  88. this.refreshDatas(data,'quantity');
  89. };
  90. ration.prototype.refreshDatas = function(data,fieldName){
  91. var dataIndex = _.findIndex(this.datas,function(item) {
  92. return item.ID ==data.rationID;
  93. });
  94. this.datas[dataIndex][fieldName] = data[fieldName];
  95. if(fieldName=='quantity'){
  96. this.datas[dataIndex]['isFromDetail']=1
  97. }
  98. var controller = projectObj.mainController;
  99. var selected = controller.sheet.getSelections();
  100. var col = _.findIndex(project.projSetting.main_tree_col.cols,function (col) {
  101. return col.data.field ==fieldName;
  102. });
  103. controller.sheet.getCell(selected[0].row,col).value(data[fieldName]);
  104. };
  105. ration.prototype.getContain = function (billNode,quantity) {
  106. if(billNode && billNode.data.quantity&&billNode.data.quantity!=0&&billNode.data.quantity!=""){
  107. let billQuantity = scMathUtil.roundForObj(billNode.data.quantity,getDecimal("quantity",billNode));
  108. return scMathUtil.roundForObj(parseFloat(quantity)/billQuantity,getDecimal("process"));
  109. }
  110. return 0
  111. };
  112. ration.prototype.getTempRationData = function (id, billsID, serialNo, rType,priceType) {
  113. let newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};
  114. let pEngineer = projectObj.project.projectInfo.property.projectEngineering;//量价默认使用后台设置的单位工程取费专业
  115. newData[project.masterField.ration] = billsID;
  116. newData['type'] = rType;
  117. // 插入定额的时候,定额工作内容默认取清单的第一个工作内容
  118. if (_compilationConfig.dynamicRationWorkContent) {
  119. let billsNode = projectObj.project.mainTree.nodes['id_' + billsID];
  120. let contentText = contentOprObj.getDefaultContent(billsNode);
  121. newData.jobContentText = contentText;
  122. }
  123. if (rType == rationType.volumePrice){
  124. newData['subType'] = priceType?priceType:gljType.GENERAL_MATERIAL; // 如果priceType 没传,默认的量价类型为材料
  125. };
  126. if(rType == rationType.ration){//空定额暂时不输入取费专业
  127. // newData['programID'] = projectObj.project.projectInfo.property.engineering;
  128. }else {
  129. if(pEngineer) newData['programID'] = pEngineer;
  130. }
  131. return newData;
  132. };
  133. ration.prototype.getBillsSortRation = function (billsID) { // 该方法只适用于叶子清单
  134. var rations = this.datas.filter(function (data) {
  135. return data[project.masterField.ration] === billsID;
  136. });
  137. rations.sort(function (x, y) {
  138. return x.serialNo - y.serialNo;
  139. });
  140. return rations;
  141. };
  142. // CSL, 2017-11-13 取任何清单(父清单、叶子清单)下的所有定额
  143. ration.prototype.getRationNodes = function (billNode) {
  144. if (billNode.sourceType != ModuleNames.bills) return [];
  145. let rations = [], IDs = [];
  146. function getSubBillsIDs(node) {
  147. if (!node) return;
  148. if (node.sourceType != ModuleNames.bills) return;
  149. if (!node.children || node.children.length == 0 || node.children[0].sourceType != ModuleNames.bills)
  150. IDs.push(node.data.ID)
  151. else
  152. getSubBillsIDs(node.children[0]);
  153. getSubBillsIDs(node.nextSibling);
  154. };
  155. if (billNode.source.children.length == 0)
  156. IDs.push(billNode.data.ID)
  157. else
  158. getSubBillsIDs(billNode.children[0]);
  159. for (let id of IDs){
  160. let subRations = this.datas.filter(function (data) {
  161. return data[project.masterField.ration] === id;
  162. });
  163. rations.push(...subRations);
  164. };
  165. rations.sort(function (x, y) {
  166. return x.serialNo - y.serialNo;
  167. });
  168. let rationNodes = [];
  169. for (let ration of rations){
  170. rationNodes.push(projectObj.project.mainTree.nodes['id_' + ration.ID]);
  171. }
  172. return rationNodes;
  173. };
  174. ration.prototype.getInsertRationData = function (billsID, preRation, rationType) {
  175. var br = this.getBillsSortRation(billsID);
  176. var updateData = [];
  177. if (preRation) {
  178. var preIndex = br.indexOf(preRation), i;
  179. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1, rationType)});
  180. for (i = preIndex + 1; i < br.length; i++) {
  181. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1, br[i].type)});
  182. }
  183. } else {
  184. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1, rationType)});
  185. }
  186. return updateData;
  187. };
  188. ration.prototype.getCounterData = function (count) {
  189. var updateData = {'projectID': this.project.ID()};
  190. if (count) {
  191. updateData[this.getSourceType()] = this.maxRationID() + count;
  192. } else {
  193. updateData[this.getSourceType()] = this.maxRationID() + 1;
  194. }
  195. return updateData;
  196. };
  197. ration.prototype.insertRation = function (billsID, preRation, rationType) {
  198. var br = this.getBillsSortRation(billsID);
  199. let insertData = this.getInsertRationData(billsID, preRation, rationType);
  200. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()],
  201. [insertData, this.getCounterData()]);
  202. var newRation = null;
  203. let newID = -1;
  204. for(let data of insertData){
  205. if(data.updateType === 'ut_create'){
  206. newID = data.updateData.ID;
  207. }
  208. }
  209. if (preRation) {
  210. var preIndex = br.indexOf(preRation), i;
  211. newRation = this.getTempRationData(newID, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1, rationType);
  212. this.datas.push(newRation);
  213. for (i = preIndex + 1; i < br.length; i++) {
  214. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  215. }
  216. } else {
  217. newRation = this.getTempRationData(newID, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1, rationType);
  218. this.datas.push(newRation);
  219. }
  220. return newRation;
  221. };
  222. ration.prototype.insertStdRation = function (billsID, preRation, std) {
  223. var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation, rationType.ration), newRation = null, that = this;
  224. updateData.forEach(function (data) {
  225. if (data.updateType === 'ut_create') {
  226. data.updateData.code = std.code;
  227. data.updateData.name = std.name;
  228. data.updateData.caption = std.caption;
  229. data.updateData.unit = std.unit;
  230. data.updateData.libID = std.rationRepId;
  231. data.updateData.content = std.jobContent;
  232. if (std.chapter) {
  233. data.updateData.comments = std.chapter.explanation;
  234. data.updateData.ruleText = std.chapter.ruleText;
  235. }
  236. data.updateData.from = std.type === 'complementary' ? 'cpt' : 'std';
  237. data.updateData.programID = std.feeType;
  238. data.updateData.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  239. // calculate ration Quantity
  240. that.CalculateQuantity(data.updateData);
  241. newRation = data.updateData;
  242. }
  243. });
  244. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()], [updateData, this.getCounterData()]);
  245. //newRation.ID = this.getNewRationID();
  246. if (preRation) {
  247. var preIndex = br.indexOf(preRation), i;
  248. this.datas.push(newRation);
  249. for (i = preIndex + 1; i < br.length; i++) {
  250. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  251. }
  252. } else {
  253. this.datas.push(newRation);
  254. }
  255. return newRation;
  256. };
  257. ration.prototype.getDeleteData = function (rationData) {
  258. var updateData = [];
  259. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': this.project.ID()}});
  260. return updateData;
  261. };
  262. ration.prototype.delete = function (ration) {
  263. var ration_glj =projectObj.project.ration_glj;
  264. this.project.pushNow('deleteRation', [this.getSourceType(),ration_glj.getSourceType()], [this.getDeleteData(ration),ration_glj.getDeleteDataByRation(ration)]);
  265. this.deleteSubListOfRation(ration);
  266. this.datas.splice(this.datas.indexOf(ration), 1);
  267. };
  268. ration.prototype.removeByID = function(ID){
  269. _.remove(this.datas,{'ID':ID});
  270. };
  271. ration.prototype.getDeleteDataByBill = function (nodes) {
  272. let updateData = [];
  273. for (let node of nodes) {
  274. if (node.children.length > 0) {
  275. updateData = updateData.concat(this.getDeleteDataByBill(node.children));
  276. } else {
  277. let rations = this.getBillsSortRation(node.getID());
  278. for (let r of rations) {
  279. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': r.ID, 'projectID': this.project.ID()}});
  280. }
  281. }
  282. }
  283. return updateData;
  284. };
  285. ration.prototype.deleteByBills = function (nodes) {
  286. for (let node of nodes) {
  287. if (node.children.length > 0) {
  288. this.deleteByBills(node.children);
  289. } else {
  290. let rations = this.getBillsSortRation(node.getID());
  291. for (let r of rations) {
  292. this.datas.splice(this.datas.indexOf(r), 1);
  293. }
  294. }
  295. }
  296. }
  297. ration.prototype.getChangePosUpdateData = function (ration1, ration2) {
  298. var updateData = [];
  299. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration1.ID, ration1.billsItemID, ration2.serialNo)});
  300. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration2.ID, ration2.billsItemID, ration1.serialNo)});
  301. return updateData;
  302. };
  303. ration.prototype.changePos = function (ration1, ration2) {
  304. var updateData = this.getChangePosUpdateData(ration1, ration2);
  305. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  306. var preSerialNo = ration1.serialNo;
  307. ration1.serialNo = ration2.serialNo;
  308. ration2.serialNo = preSerialNo;
  309. };
  310. ration.prototype.updateField = function (ration, field, newValue) {
  311. calcFees.setFee(ration, field, newValue);
  312. let updateData = [];
  313. let data = {'ID': ration.ID, 'projectID': this.project.ID()};
  314. if (field === 'quantity') {
  315. data[field] = newValue;
  316. data.isFromDetail=0;
  317. // to do Calculate
  318. if (ration.fees) {
  319. data.fees = ration.fees;
  320. }
  321. } else if (field === 'feesIndex.common.unitFee') {
  322. // to do Calculate
  323. if (ration.fees) {
  324. data.fees = ration.fees;
  325. }
  326. } else {
  327. data[field] = newValue;
  328. }
  329. updateBillsOprRation();
  330. updateData.push({'updateType': 'ut_update', 'updateData': data});
  331. this.project.pushNow('updateBills', this.getSourceType(), updateData);
  332. };
  333. ration.prototype.updateRation = function (ration, updateNow) {
  334. let updateData = [];
  335. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatRationUpdateData(ration)});
  336. if (updateNow) {
  337. this.project.pushNow('updateRations', this.getSourceType(), updateData);
  338. } else {
  339. this.project.push(this.getSourceType(), updateData);
  340. }
  341. };
  342. ration.prototype.FilterNumberFromUnit = function (unit) {
  343. let reg = new RegExp('^[0-9]+');
  344. if (reg.test(unit)) {
  345. return parseInt(unit.match(reg)[0]);
  346. } else {
  347. return 1;
  348. }
  349. };
  350. ration.prototype.CalculateQuantity = function (ration) {
  351. // calculate ration Quantity
  352. let quantity_decimal = getDecimal("ration.quantity");
  353. let process_decimal = getDecimal("process");
  354. if (optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')) {
  355. let billsNode = this.project.Bills.tree.findNode(ration[this.project.masterField.ration]);
  356. let billsQuantity = billsNode.data.quantity ? billsNode.data.quantity : 0;
  357. billsQuantity=scMathUtil.roundForObj(billsQuantity,quantity_decimal);
  358. ration.quantityEXP="QDL";
  359. /* if (optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToRationUnit')) {
  360. ration.quantity = (billsQuantity / this.FilterNumberFromUnit(ration.unit)).toDecimal(quantity_decimal);
  361. } else {
  362. ration.quantity = billsQuantity.toDecimal(quantity_decimal);
  363. }*/
  364. ration.quantity = (billsQuantity / this.FilterNumberFromUnit(ration.unit)).toDecimal(quantity_decimal);//改成不管是否打勾都做转换
  365. ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,process_decimal);
  366. }
  367. };
  368. ration.prototype.updateRationCodes = function (recodes,cleanzmhs = false) {
  369. let libID = rationLibObj.getCurrentStdRationLibID();
  370. let libIDs = rationLibObj.getStdRationLibIDs();
  371. let defaultLibID = rationLibObj.getDefaultStdRationLibID();
  372. let engineering = projectObj.project.projectInfo.property.engineering;
  373. let projectID = projectObj.project.projectInfo.ID;
  374. let project = projectObj.project;
  375. let mainTree = project.mainTree;
  376. let nodeInfo =[];
  377. let refershNodes = [];
  378. if(libIDs == null) return;
  379. //设置定额库的优先级, 当是清空子目换算时,使用原定额的定额库ID,其它情况再按: 默认先取选中的定额库,如果没有再取default定额库
  380. let selectedLib = (cleanzmhs == true && recodes.length > 0)? recodes[0].node.data.libID : sessionStorage.getItem("stdRationLib");
  381. selectedLib&&selectedLib!='undefined'?libIDs.unshift(selectedLib):libIDs.unshift(defaultLibID);
  382. for(let r of recodes){
  383. let needInstall = false;
  384. if(projectObj.project.isInstall()) {//如果是安装工程,要看需不需要生成安装增加费
  385. needInstall = project.Bills.isFBFX(r.node);
  386. }
  387. r.value===null||r.value===undefined?"":r.value = r.value.replace(/[\s\r\n]/g, "");//去掉空格回车换行等字符
  388. if(cleanzmhs == true||r.value != r.node.data.code){
  389. nodeInfo.push({ID:r.node.data.ID,billsItemID:r.node.data.billsItemID,newCode:r.value,needInstall:needInstall});
  390. refershNodes.push(r.node);
  391. }
  392. }
  393. let calQuantity = optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan');
  394. $.bootstrapLoading.start();
  395. CommonAjax.post("/ration/replaceRations",{nodeInfo:nodeInfo,libIDs:libIDs,defaultLibID: defaultLibID,projectID:projectID,calQuantity:calQuantity,cleanzmhs:cleanzmhs},function (data) {
  396. for(let recode of data){
  397. let node = mainTree.getNodeByID(recode.ration.ID);
  398. if(node) {
  399. for(let temkey in recode.ration){//更新缓存
  400. node.data[temkey] = recode.ration[temkey];
  401. }
  402. node.data.feesIndex = {};
  403. //删除定额节点下的主材和设备节点
  404. project.ration_glj.removeNodeByRation(recode.ration,projectObj.mainController);
  405. project.Ration.deleteSubListOfRation(recode.ration,cleanzmhs);//删除旧定额下的相关记录
  406. //添加新的记录
  407. project.Ration.addSubListOfRation(recode);
  408. project.ration_glj.addToMainTree(recode.ration_gljs);
  409. }
  410. }
  411. project.projectGLJ.loadData(function () {
  412. mbzm_obj.nodeChanged = true;//子目模板关联刷新
  413. gljOprObj.showDataIfRationSelect(projectObj.project.mainTree.selected,"-111111111");//这里第二个参数是为了使改前和改后selectedID不一样,删除了的话下方的定额工料机不会刷新
  414. project.calcProgram.calcNodesAndSave(refershNodes, function () {
  415. OVER_HEIGHT.reCalcOverHeightFee();
  416. });
  417. projectObj.mainController.refreshTreeNode(refershNodes, true);
  418. $.bootstrapLoading.end();
  419. });
  420. if(data.length < nodeInfo.length && nodeInfo[data.length].newCode!=null){//说明有部分定额编号没找到记录
  421. alert('当前库中找不到定额"' + nodeInfo[data.length].newCode + '"');
  422. }
  423. updateBillsOprRation();
  424. })
  425. };
  426. ration.prototype.addMultiRation = function (items, callback) {
  427. console.log('addMultiRation');
  428. let me = this;
  429. let project = projectObj.project, sheetController = projectObj.mainController;
  430. let engineering = projectObj.project.projectInfo.property.engineering;
  431. let selected = project.mainTree.selected, newSource = null, newNode = null,pre = null,br = null;
  432. let billItemID = null,serialNo=1,nextID=null;
  433. let needInstall = false;
  434. let newDatas = [];
  435. if (selected === null) { return; }
  436. if (selected.sourceType === project.Bills.getSourceType() && selected.depth() > 0) {
  437. if (selected.source.children.length > 0) {
  438. alert('当前清单已有清单子项,不能套用定额。');
  439. } else if (selected.data.calcBase&&selected.data.calcBase!="") {
  440. alert('当前有基数计算,不能插入定额/量价/人材机。');
  441. } else {
  442. if(selected.data.type === billType.FB){
  443. return;
  444. }
  445. billItemID = selected.source.getID();
  446. nextID = selected.tree.rootID();
  447. br = this.getBillsSortRation(billItemID);
  448. serialNo = br.length > 0 ? br[br.length - 1].serialNo + 1 : 1
  449. }
  450. } else if (selected.sourceType === project.Ration.getSourceType()) {
  451. billItemID = selected.getParentID();
  452. br = this.getBillsSortRation(billItemID);
  453. serialNo = selected.data.serialNo+1;
  454. nextID = selected.getNextSiblingID();
  455. pre = selected.source;
  456. };
  457. if(billItemID){
  458. let calQuantity = optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan');
  459. if(projectObj.project.isInstall()) {//如果是安装工程,要看需不需要生成安装增加费
  460. let billsNode = project.mainTree.getNodeByID(billItemID);
  461. needInstall = project.Bills.isFBFX(billsNode);//在分部分项插入的定额才需要定额安装增加费
  462. }
  463. for(let i = 0; i < items.length; i++){
  464. let newData = me.getTempRationData(me.getNewRationID(), billItemID, serialNo, items[i].rationType);
  465. serialNo++;
  466. let brUpdate = [];
  467. //更新兄弟节点的序列号
  468. if (i ===0 && pre) {
  469. let preIndex = br.indexOf(pre), i;
  470. for (i = preIndex + 1; i < br.length; i++) {
  471. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo + items.length - 1 : br[i].serialNo + items.length;
  472. brUpdate.push({projectID:newData.projectID,ID:br[i].ID,serialNo:br[i].serialNo});
  473. }
  474. }
  475. newDatas.push({itemQuery: items[i].itemQuery, newData: newData, defaultLibID: rationLibObj.getDefaultStdRationLibID(), calQuantity: calQuantity, brUpdate: brUpdate, needInstall: needInstall})
  476. }
  477. let showLoding = true;
  478. $.bootstrapLoading.start();
  479. //保证由于异步的关系loading界面被隐藏,比如清单指引插入清单定额时,endUpdate中提前隐藏了loading
  480. let interval =setInterval(function () {
  481. if(!$.bootstrapLoading.isLoading()&& showLoding){
  482. $.bootstrapLoading.start();
  483. clearInterval(interval);
  484. }
  485. else{
  486. clearInterval(interval);
  487. }
  488. }, 100);
  489. CommonAjax.post("/ration/addMultiRation",{newDatas: newDatas},function (rstData) {
  490. let newNodes = [];
  491. //更新缓存
  492. for(let data of rstData){
  493. me.datas.push(data.ration);
  494. me.addSubListOfRation(data);
  495. //插入树节点
  496. newSource = data.ration;
  497. newNode = project.mainTree.insert(billItemID, nextID, newSource.ID);
  498. newNodes.push(newNode);
  499. nextID = project.mainTree.selected.getNextSiblingID();
  500. newNode.source = newSource;
  501. newNode.sourceType = project.Ration.getSourceType();
  502. newNode.data = newSource;
  503. ProjectController.syncDisplayNewNode(sheetController, newNode);
  504. }
  505. project.projectGLJ.loadData(function () {
  506. for(let data of rstData){
  507. project.ration_glj.addToMainTree(data.ration_gljs);
  508. }
  509. projectObj.mainController.refreshTreeNode(newNodes, false);
  510. if(project.Bills.isFBFX(newNodes[0])) { //判断是否属于分部分项工程 ,是的话才需要做计取安装费计算
  511. project.installation_fee.calcInstallationFee(function (isChange,rations) {
  512. if(isChange){
  513. rations = rations.concat(newNodes);
  514. project.calcProgram.calcNodesAndSave(rations);
  515. }else {
  516. project.calcProgram.calcNodesAndSave(newNodes);
  517. }
  518. });
  519. }else {
  520. project.calcProgram.calcNodesAndSave(newNodes);
  521. }
  522. updateBillsOprRation();
  523. if(callback){
  524. callback();
  525. }
  526. showLoding = false;
  527. $.bootstrapLoading.end();
  528. });
  529. })
  530. }
  531. };
  532. ration.prototype.insertVolumePrice = function(type){
  533. this.addNewRation(null,rationType.volumePrice,function (newNode) {//插入人工不需要自动定位到编号列
  534. projectObj.selectColAndFocus(newNode,null);
  535. },true,type);
  536. };
  537. // 已经有数据,更新前端缓存及节点,不进行通信
  538. ration.prototype.addNewDataSimply = function (newData) {
  539. const newNodes = [];
  540. const controller = projectObj.mainController;
  541. this.addDatasToList(newData);
  542. newData.forEach(item => {
  543. const newNode = project.mainTree.insert(item.billsItemID, -1, item.ID);
  544. newNode.source = item;
  545. newNode.sourceType = project.Ration.getSourceType();
  546. newNode.data = item;
  547. controller.sheet.addRows(newNode.serialNo(), 1);
  548. newNodes.push(newNode);
  549. });
  550. TREE_SHEET_HELPER.refreshTreeNodeData(controller.setting, controller.sheet, newNodes, false);
  551. };
  552. // 数据库的数据已删除,删除前端缓存及节点,不进行通信
  553. ration.prototype.deleteDataSimply = function (IDList) {
  554. const rationNodes = projectObj.project.mainTree.items.filter(node => IDList.includes(node.data.ID));
  555. const controller = projectObj.mainController;
  556. rationNodes.forEach(node => {
  557. controller.sheet.deleteRows(node.serialNo(),1);
  558. controller.tree.delete(node);
  559. projectObj.project.Ration.removeByID(node.data.ID);
  560. projectObj.project.ration_glj.deleteByRation(node.data);
  561. });
  562. };
  563. ration.prototype.addNewRation = function (itemQuery,rationType,callback=null,isEmpty=false,priceType,needCalcAndSave=true) {//priceType 是量价类型
  564. console.log('addNewRation');
  565. let me = this;
  566. let project = projectObj.project, sheetController = projectObj.mainController;
  567. let engineering = projectObj.project.projectInfo.property.engineering;
  568. let selected = project.mainTree.selected, newSource = null, newNode = null,pre=null,br=null;
  569. let billItemID = null,serialNo=1,nextID=null;
  570. let needInstall = false;
  571. if (selected === null) { return null; }
  572. if (selected.sourceType === project.Bills.getSourceType() && selected.depth() > 0) {
  573. if(selected.data.type === billType.FB){
  574. return null;
  575. }
  576. else if (selected.source.children.length > 0) {
  577. alert('当前清单已有清单子项,不能套用定额。');
  578. } else if (selected.data.calcBase&&selected.data.calcBase!="") {
  579. alert('当前有基数计算,不能插入定额/量价/人材机。');
  580. } else {
  581. billItemID = selected.source.getID();
  582. nextID = selected.tree.rootID();
  583. br = this.getBillsSortRation(billItemID);
  584. serialNo = br.length > 0 ? br[br.length - 1].serialNo + 1 : 1
  585. }
  586. } else if (selected.sourceType === project.Ration.getSourceType()) {
  587. billItemID = selected.getParentID();
  588. br = this.getBillsSortRation(billItemID);
  589. serialNo = selected.data.serialNo+1;
  590. nextID = selected.getNextSiblingID();
  591. pre = selected.source;
  592. };
  593. if(billItemID){
  594. let newID = me.getNewRationID();
  595. let newData = me.getTempRationData(newID, billItemID, serialNo, rationType,priceType);
  596. let calQuantity = isEmpty===true?false:optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan');
  597. let brUpdate = [];
  598. //更新兄弟节点的序列号
  599. if (pre) {
  600. let preIndex = br.indexOf(pre), i;
  601. for (i = preIndex + 1; i < br.length; i++) {
  602. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  603. brUpdate.push({projectID:newData.projectID,ID:br[i].ID,serialNo:br[i].serialNo});
  604. }
  605. }
  606. if(projectObj.project.isInstall()) {//如果是安装工程,要看需不需要生成安装增加费
  607. let billsNode = project.mainTree.getNodeByID(billItemID);
  608. needInstall = project.Bills.isFBFX(billsNode);//在分部分项插入的定额才需要定额安装增加费
  609. }
  610. $.bootstrapLoading.start();
  611. CommonAjax.post("/ration/addNewRation",{itemQuery:itemQuery,newData:newData,defaultLibID: rationLibObj.getDefaultStdRationLibID(),calQuantity:calQuantity,brUpdate:brUpdate,needInstall:needInstall},function (data) {
  612. //更新缓存
  613. me.datas.push(data.ration);
  614. me.addSubListOfRation(data);
  615. //插入树节点
  616. newSource = data.ration;
  617. if(needCalcAndSave == false){
  618. syncNodeOper(data);
  619. if(callback) callback(newNode);
  620. }else {
  621. if(data.projectGLJDatas) projectObj.project.projectGLJ.refreshByDatas(data.projectGLJDatas);
  622. syncNodeOper(data);
  623. project.calcProgram.calcAndSave(newNode,function () {
  624. if(project.Bills.isFBFX(newNode)) { //判断是否属于分部分项工程 ,是的话才需要做计取安装费计算
  625. installationFeeObj.calcInstallationFee();
  626. }
  627. });
  628. //如果添加规则中,添加内容为定额子目,则更新相关清单
  629. updateBillsOprRation();
  630. if(callback) callback(newNode);
  631. }
  632. $.bootstrapLoading.end();
  633. })
  634. return newNode;
  635. }
  636. else return null;
  637. function syncNodeOper(data) {//插入后刷新节点操作
  638. newNode = project.mainTree.insert(billItemID, nextID, newSource.ID);
  639. newNode.source = newSource;
  640. newNode.sourceType = project.Ration.getSourceType();
  641. newNode.data = newSource;
  642. ProjectController.syncDisplayNewNode(sheetController, newNode);
  643. project.ration_glj.addToMainTree(data.ration_gljs);
  644. projectObj.mainController.refreshTreeNode([newNode], false);
  645. }
  646. };
  647. ration.prototype.addNewRationFast = function (rationType,callback) {
  648. console.log('addNewRationFast');
  649. let me = this;
  650. let project = projectObj.project, sheetController = projectObj.mainController;
  651. let engineering = projectObj.project.projectInfo.property.engineering;
  652. let selected = project.mainTree.selected, newSource = null, newNode = null,pre=null,br=null;
  653. let billItemID = null,serialNo=1,nextID=null;
  654. if (selected === null) { return null; }
  655. if (selected.sourceType === project.Bills.getSourceType() && selected.depth() > 0) {
  656. if(selected.data.type === billType.FB){
  657. return null;
  658. }
  659. else if (selected.source.children.length > 0) {
  660. alert('当前清单已有清单子项,不能套用定额。');
  661. } else if (selected.data.calcBase&&selected.data.calcBase!="") {
  662. alert('当前有基数计算,不能插入定额/量价/人材机。');
  663. } else {
  664. billItemID = selected.source.getID();
  665. nextID = selected.tree.rootID();
  666. br = this.getBillsSortRation(billItemID);
  667. serialNo = br.length > 0 ? br[br.length - 1].serialNo + 1 : 1
  668. }
  669. } else if (selected.sourceType === project.Ration.getSourceType()) {
  670. billItemID = selected.getParentID();
  671. br = this.getBillsSortRation(billItemID);
  672. serialNo = selected.data.serialNo+1;
  673. nextID = selected.getNextSiblingID();
  674. pre = selected.source;
  675. };
  676. if(billItemID){
  677. let newID = me.getNewRationID();
  678. let newData = me.getTempRationData(newID, billItemID, serialNo, rationType);
  679. let brUpdate = [];
  680. //更新兄弟节点的序列号
  681. if (pre) {
  682. let preIndex = br.indexOf(pre), i;
  683. for (i = preIndex + 1; i < br.length; i++) {
  684. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  685. brUpdate.push({projectID:newData.projectID,ID:br[i].ID,serialNo:br[i].serialNo});
  686. }
  687. }
  688. if(projectObj.project.isInstall()) {//如果是安装工程,要看需不需要生成安装增加费
  689. let billsNode = project.mainTree.getNodeByID(billItemID);
  690. }
  691. $.bootstrapLoading.start();
  692. newNode = project.mainTree.insert(billItemID, nextID, newID);
  693. newNode.sourceType = project.Ration.getSourceType();
  694. newNode.data = newData;
  695. CommonAjax.post("/ration/addNewRation",{itemQuery:null,newData:newData,defaultLibID: rationLibObj.getDefaultStdRationLibID(),calQuantity:false,brUpdate:brUpdate,needInstall:false},function (data) {
  696. //更新缓存
  697. me.datas.push(data.ration);
  698. me.addSubListOfRation(data);
  699. //插入树节点
  700. newSource = data.ration;
  701. newNode.source = newSource;
  702. newNode.data = newSource;
  703. })
  704. ProjectController.syncDisplayNewNode(sheetController, newNode);
  705. if(callback){
  706. callback(newNode);
  707. }
  708. $.bootstrapLoading.end();
  709. return newNode;
  710. }
  711. else return null;
  712. };
  713. ration.prototype.deleteSubListOfRation = function(ration,cleanzmhs=false){
  714. projectObj.project.ration_glj.deleteByRation(ration);
  715. projectObj.project.ration_coe.deleteByRation(ration);
  716. if(cleanzmhs == false){
  717. projectObj.project.ration_installation.deleteByRation(ration);
  718. projectObj.project.quantity_detail.deleteByRation(ration);
  719. projectObj.project.ration_template.deleteByRation(ration);
  720. }
  721. };
  722. ration.prototype.addSubListOfRation = function (data) {
  723. project.ration_glj.addDatasToList(data.ration_gljs);
  724. project.ration_coe.addDatasToList(data.ration_coes);
  725. project.ration_installation.addDatasToList(data.ration_installations);
  726. project.ration_template.addDatasToList(data.ration_templates);
  727. project.quantity_detail.addDatasToList(data.quantity_details);
  728. };
  729. ration.prototype.replaceRation = function (ration, std) {
  730. this.project.beginUpdate('replaceRation');
  731. // delete
  732. let ration_glj =projectObj.project.ration_glj;
  733. this.project.push(this.project.ration_glj.getSourceType(), this.project.ration_glj.getDeleteDataByRation(ration));
  734. this.deleteSubListOfRation(ration);
  735. // insertNewRation
  736. let updateData = [];
  737. ration.code = std.code;
  738. ration.name = std.name;
  739. ration.caption = std.caption;
  740. ration.unit = std.unit;
  741. ration.libID = std.rationRepId;
  742. ration.content = std.jobContent;
  743. ration.adjustState = '';
  744. if (std.chapter) {
  745. ration.comments = std.chapter.explanation;
  746. ration.ruleText = std.chapter.ruleText;
  747. }
  748. ration.from = std.type === 'complementary' ? 'cpt' : 'std';
  749. ration.programID = std.feeType;
  750. ration.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  751. // calculate ration Quantity
  752. this.CalculateQuantity(ration);
  753. updateData.push({updateType: 'ut_update', updateData: ration});
  754. this.project.push(this.getSourceType(), updateData);
  755. this.project.endUpdate();
  756. };
  757. ration.prototype.updateContain=function (value,node) {
  758. let me = this;
  759. if(isNaN(value)){
  760. alert("当前输入的数据类型不正确,请重新输入");
  761. projectObj.mainController.refreshTreeNode([node]);
  762. return;
  763. }
  764. if(node.data.quantityEXP=="GCLMXHJ"){//如果定额工程量是来自工程量明细
  765. hintBox.infoBox('操作确认', '已有工程量明细,是否清空明细表?', 2, function () {
  766. node.data.isFromDetail=0;
  767. project.quantity_detail.cleanQuantityDetail(node,true);
  768. me.doContainUpdate(value,node);
  769. }, function () {
  770. projectObj.mainController.refreshTreeNode([node]);
  771. },['确定','取消'],false);
  772. }else {
  773. me.doContainUpdate(value,node);
  774. }
  775. };
  776. ration.prototype.getNearRations = function (ration,callback) {
  777. if(ration&&ration.code && ration.code !=""){
  778. CommonAjax.post("/ration/getSameSectionRations",{from:ration.from,code:ration.code,libID:ration.libID},function(rations){
  779. callback(rations);
  780. })
  781. }else {
  782. callback([]);
  783. }
  784. };
  785. ration.prototype.doContainUpdate = function (value,node) {
  786. let billNode = node.parent;
  787. let contain = scMathUtil.roundForObj(value,getDecimal("process"));
  788. let billQuantity = billNode.data.quantity||billNode.data.quantity!=""?billNode.data.quantity:0;
  789. billQuantity = parseFloat(billQuantity);
  790. node.data.contain = contain;
  791. node.data.quantityEXP="QDL*"+contain;
  792. node.data.quantity=scMathUtil.roundForObj(billQuantity*contain,getDecimal("quantity",node));
  793. let times = parseInt(node.data.unit);
  794. if (!isNaN(times)) {
  795. node.data.quantityEXP+='*'+times;
  796. }
  797. // node.data.quantity = projectObj.project.quantity_detail.autoTransformQuantity(node.data.quantity,node);//按单位做转换
  798. node.changed = true;
  799. project.calcProgram.calcAndSave(node, function () {
  800. project.projectGLJ.calcQuantity();
  801. });
  802. projectObj.mainController.refreshTreeNode(node.children);//刷新子工料机树节点总消耗量
  803. };
  804. ration.prototype.addRationChecking=function(selected){
  805. if (selected) {// Vincent, 2018-01-02
  806. if(selected.sourceType === project.Ration.getSourceType()){ // 焦点行是定额/量价/工料机,有效显示。
  807. return false;
  808. }else if(selected.sourceType === project.Bills.getSourceType()){
  809. if(selected.data.type == billType.FX||selected.data.type == billType.BX){//焦点行是分项,有效显示。
  810. return false
  811. }
  812. if(selected.data.type == billType.BILL && selected.source.children.length === 0){//焦点行是清单,且没有子项,有效显示。
  813. return false
  814. }
  815. }
  816. }
  817. return true;
  818. };
  819. ration.prototype.canAdd = function (node) { // CSL.2018.07.23
  820. if (!node) return false;
  821. if (calcTools.isRationCategory(node)) return true;
  822. if (calcTools.isBill(node)){
  823. if (node.data.type == billType.FX || node.data.type == billType.BX) return true; // 分项、补项
  824. if (calcTools.isLeafBill(node)
  825. && (node.data.type != billType.DXFY)
  826. && (node.data.type != billType.FB)
  827. && project.Bills.isMeasure(node)) return true; // 叶子清单项
  828. };
  829. return false;
  830. }
  831. ration.prototype.getAllInstallTypeRation = function () {//取所有计取安装增加费生成的定额
  832. return _.filter(this.datas,{'type':rationType.install});
  833. };
  834. ration.prototype.getLabourTotalFee=function(ration){//feesIndex.labour.totalFee 取定额的人工费合价
  835. if(ration.feesIndex&&ration.feesIndex.labour&&ration.feesIndex.labour.totalFee){
  836. return ration.feesIndex.labour.totalFee;
  837. }else {
  838. return 0;
  839. }
  840. };
  841. ration.prototype.getMaterialTotalFee=function(ration){//feesIndex.material.totalFee 取定额的材料费合价
  842. if(ration.feesIndex&&ration.feesIndex.material&&ration.feesIndex.material.totalFee){
  843. return ration.feesIndex.material.totalFee;
  844. }else {
  845. return 0;
  846. }
  847. } ;
  848. ration.prototype.getMachineTotalFee=function(ration){//feesIndex.machine.totalFee 取定额的机械费合价
  849. if(ration.feesIndex&&ration.feesIndex.machine&&ration.feesIndex.machine.totalFee){
  850. return ration.feesIndex.machine.totalFee;
  851. }else {
  852. return 0;
  853. }
  854. } ;
  855. //获取定额前缀
  856. ration.prototype.getRationPrefix = function(defaultLibID, ration){
  857. if(defaultLibID !== ration.libID){
  858. return rationPrefix.borrow;
  859. }
  860. if(ration.from && ration.from === rationFrom.cpt){
  861. return rationPrefix.complementary;
  862. }
  863. return rationPrefix.none;
  864. };
  865. return new ration(project);
  866. }
  867. };
  868. function FilterNumberFromUnit (unit) {
  869. let reg = new RegExp('^[0-9]+');
  870. if (reg.test(unit)) {
  871. return parseInt(unit.match(reg)[0]);
  872. } else {
  873. return 1;
  874. }
  875. };