ration.js 42 KB

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