ration_glj.js 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. let ration_glj = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. let tools = {};
  8. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  9. let ration_glj = function (proj) {
  10. this.gljTree = cacheTree.createNew(this);
  11. // this.project = proj;
  12. this.datas = [];
  13. let sourceType = ModuleNames.ration_glj;
  14. this.getSourceType = function () {
  15. return sourceType;
  16. }
  17. proj.registerModule(ModuleNames.ration_glj, this);
  18. };
  19. // 从后台获取数据
  20. /*glj.prototype.pullData = function (){
  21. this.project.pullData(
  22. '/glj/getData',
  23. {projectID: this.project.ID},
  24. function(result){
  25. if (result.error ===0){
  26. this.loadDatas(result.data);
  27. }
  28. else {
  29. // to do: 错误处理需要细化
  30. alert(result.message);
  31. }
  32. },
  33. function (){}//to do: 错误处理需要细化
  34. )
  35. };*/
  36. // prototype用于定义public方法
  37. ration_glj.prototype.loadData = function (datas) {
  38. this.datas = datas;
  39. };
  40. ration_glj.prototype.getGLJListByRationID = function (rationID) {
  41. return _.filter(this.datas, {'rationID': rationID})
  42. };
  43. ration_glj.prototype.getGatherGljArrByRations = function (rations, needOneBill, billQuantity) {
  44. let result = [];
  45. let clone = function (obj) {
  46. if (obj === null) return null;
  47. let o = Object.prototype.toString.apply(obj) === "[object Array]" ? [] : {};
  48. for (let i in obj) {
  49. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? clone(obj[i]) : obj[i]);
  50. }
  51. return o;
  52. }
  53. let findGlj = function (sourceGlj, gljArr) {
  54. for (let glj of gljArr) {
  55. if (glj.projectGLJID === sourceGlj.projectGLJID) {
  56. return glj;
  57. }
  58. }
  59. return null;
  60. }
  61. for (let ration of rations) {
  62. if (ration.type == rationType.volumePrice || ration.type == rationType.gljRation){
  63. let glj = JSON.parse(JSON.stringify(ration));
  64. glj.type = glj.subType;
  65. glj.totalQuantity = parseFloatPlus(ration.quantity);
  66. glj.markertPrice = glj.marketUnitFee;
  67. glj.basePrice = glj.marketUnitFee;
  68. glj.adjustPrice = glj.marketUnitFee;
  69. result.push(glj);
  70. } else{
  71. let rationGljs = projectObj.project.calcProgram.getGljArrByRation(ration);
  72. for (let glj of rationGljs) {
  73. let sameGlj = findGlj(glj, result);
  74. if (!sameGlj) {
  75. sameGlj = clone(glj);
  76. sameGlj.quantity = (sameGlj.quantity * ration.quantity).toDecimal(4);
  77. result.push(sameGlj);
  78. } else {
  79. sameGlj.quantity = sameGlj.quantity + (glj.quantity * ration.quantity).toDecimal(4);
  80. sameGlj.totalQuantity = (parseFloat(sameGlj.totalQuantity) + parseFloat(glj.totalQuantity)).toDecimal(4);
  81. }
  82. }
  83. }
  84. }
  85. result = gljOprObj.combineWithProjectGlj(result);
  86. if (!needOneBill) return result;
  87. // 上面取的是清单下所有工料机的总量,我要算清单单价,所以要取单位清单的工料机数量,所以下面要除以清单数量。
  88. let oneBill = JSON.parse(JSON.stringify(result));
  89. for (let glj of oneBill){
  90. if (glj.type == rationType.volumePrice){ // 量价、工料机形式的定额要进行数据变换才能参与基数计算。
  91. glj.type = glj.subType;
  92. glj.basePrice = glj.marketUnitFee;
  93. glj.marketPrice = glj.marketUnitFee;
  94. glj.adjustPrice = glj.marketUnitFee;
  95. }
  96. else if (glj.type == rationType.gljRation){
  97. glj.type = glj.subType;
  98. };
  99. // glj.quantity = (glj.quantity / billQuantity).toDecimal(decimalObj.glj.quantity);
  100. glj.quantity = (glj.quantity / billQuantity).toDecimal(decimalObj.process); // 广联达这里没有取舍
  101. };
  102. return oneBill;
  103. }
  104. // 提交数据后返回数据处理
  105. ration_glj.prototype.doAfterUpdate = function (err, data) {
  106. let me = this;
  107. if (!err) {
  108. if (data.updateTpye == 'ut_update') {
  109. me.refreshAfterUpdate(data);
  110. } else if (data.updateTpye == 'ut_delete') {
  111. me.refreshAfterDelete(data);
  112. } else {
  113. me.refreshAfterSave(data);
  114. }
  115. }
  116. projectObj.project.projectGLJ.loadData();
  117. };
  118. ration_glj.prototype.getDataByID = function (ID) {
  119. return _.find(this.datas,{'ID':ID});
  120. };
  121. ration_glj.prototype.refreshAfterSave = function (data) {
  122. let me = projectObj.project.ration_glj;
  123. let neRecodes = [];
  124. if (data) {
  125. // neRecodes=data.newRecords;//原来是显示和缓存分开的,后来发现会导致数据不一致的问题所以改成统一的了,这里也只是会作为显示。
  126. neRecodes = data.showDatas;
  127. gljOprObj.sheetData = neRecodes;
  128. me.addDatasToList(neRecodes);
  129. }
  130. project.projectGLJ.loadData(function () {
  131. gljOprObj.showRationGLJSheetData(true);
  132. //add to mainTree;
  133. me.addToMainTree(neRecodes);
  134. let rationID = neRecodes[0].rationID;
  135. let node = project.mainTree.nodes['id_' + rationID];
  136. if(isDef(node)){
  137. project.calcProgram.calcAndSave(node);
  138. }
  139. if (activeSubSheetIsCalcProgram())
  140. calcProgramObj.refreshCalcProgram(node, 1);
  141. });
  142. };
  143. ration_glj.prototype.getGljByRationID = function(rationID){
  144. return _.filter(this.datas, {'rationID': rationID});
  145. };
  146. ration_glj.prototype.addDatasToList = function (datas) {
  147. let me = projectObj.project.ration_glj;
  148. if(datas&&datas.length>0){
  149. if (me.datas && Array.isArray(me.datas)) {
  150. me.datas = me.datas.concat(datas);
  151. } else {
  152. me.datas = datas;
  153. }
  154. }
  155. };
  156. ration_glj.prototype.updateGLJNodeAfterReplace = function(data){
  157. if(!data) return null;
  158. if (this.needShowToTree(data)) {//当替换的是主材或设备时,刷新对应的树节点
  159. var node = this.findGLJNodeByID(data.ID);
  160. if (node) {
  161. this.transferToNodeData(data);
  162. node.source = data;
  163. node.data = data;
  164. }
  165. return node;
  166. }
  167. };
  168. ration_glj.prototype.addToMainTree = function (datas) {
  169. datas = gljUtil.sortRationGLJ(datas);
  170. for (let data of datas) {
  171. if (this.needShowToTree(data)) {
  172. gljOprObj.setGLJPrice(data);
  173. this.transferToNodeData(data);
  174. let parentNode = projectObj.project.mainTree.findNode(data.rationID);
  175. if (parentNode) {
  176. let nextNodeID = null;
  177. if (parentNode.children.length > 0) {
  178. for (let br of parentNode.children) {
  179. if (compareRationGLJ(data, br.data)) {//如果有兄弟节点则找到添加位置。
  180. nextNodeID = br.getID();
  181. }
  182. }
  183. }
  184. nextNodeID = nextNodeID ? nextNodeID : parentNode.tree.rootID();
  185. let newNode = projectObj.project.mainTree.insert(parentNode.getID(), nextNodeID,data.ID);
  186. newNode.source = data;
  187. newNode.sourceType = this.getSourceType();
  188. newNode.data = data;
  189. ProjectController.syncDisplayNewRationGljNode(projectObj.mainController, newNode);
  190. }
  191. }
  192. }
  193. };
  194. ration_glj.prototype.removeNodeByRation = function(ration,controller){//删除主材或设备节点
  195. let glj_list = _.filter(projectObj.project.ration_glj.datas,{'rationID':ration.ID});
  196. let deleteNodes = [];
  197. for(let rg of glj_list){
  198. if(this.needShowToTree(rg)){
  199. let r_node = projectObj.project.mainTree.getNodeByID(rg.ID);
  200. deleteNodes.push(r_node);
  201. }
  202. }
  203. if(deleteNodes.length > 0){
  204. let rowIndex = deleteNodes[0].serialNo();
  205. if(controller.tree.m_delete(deleteNodes)){
  206. TREE_SHEET_HELPER.massOperationSheet(controller.sheet, function () {
  207. let rowCount = deleteNodes.length;
  208. controller.sheet.deleteRows(rowIndex, rowCount);
  209. });
  210. }
  211. }
  212. };
  213. ration_glj.prototype.refreshAfterUpdate = function (data) {
  214. let me = this;
  215. let rationID= me.updateCacheAfterAdjust(data);
  216. gljOprObj.showRationGLJSheetData(true);
  217. this.reCalcWhenGLJChange({rationID:rationID});
  218. };
  219. ration_glj.prototype.updateCacheAfterAdjust=function (data) {
  220. let me = this;
  221. let rationID=null;
  222. if (data.quantityRefresh) {
  223. data.glj_result.forEach(function (item) {
  224. rationID = me.refreshEachItme(item.doc, item.query);
  225. })
  226. } else {
  227. rationID = me.refreshEachItme(data.doc, data.query);
  228. }
  229. return rationID;
  230. };
  231. ration_glj.prototype.refreshEachItme = function (doc, query) {
  232. let glj = this.refreshByID(query.ID,doc);
  233. return glj.rationID;
  234. };
  235. ration_glj.prototype.refreshByID=function (ID,doc) {
  236. let glj_list = projectObj.project.ration_glj.datas;
  237. let glj_index = _.findIndex(glj_list,{"ID":ID});
  238. _.forEach(doc, function (n, key) {
  239. glj_list[glj_index][key] = n;
  240. });
  241. return glj_list[glj_index]
  242. };
  243. ration_glj.prototype.refreshAfterDelete = function (data) {
  244. let me = projectObj.project.ration_glj;
  245. let glj_list = me.datas;
  246. let oldData = _.remove(glj_list, data.query);
  247. _.remove(gljOprObj.sheetData, data.query);
  248. gljOprObj.showRationGLJSheetData();
  249. projectObj.project.projectGLJ.loadData();
  250. let rationNode = null;
  251. let next = null;
  252. let selected = projectObj.project.mainTree.selected;
  253. if (selected.sourceType == ModuleNames.ration) { //如果选中的是定额,说明是右键删除工料机操作,如果选中的是定额工料机,则说明是在造价书主界面中点击了删除按钮
  254. rationNode = selected;
  255. } else if (selected.sourceType == ModuleNames.ration_glj) {
  256. rationNode = selected.parent;
  257. next = true;
  258. }
  259. rationNode.data.adjustState = data.adjustState;
  260. projectObj.mainController.refreshTreeNode([rationNode]);
  261. for (let o of oldData) {
  262. if (this.needShowToTree(o)) {
  263. let node = me.findGLJNodeByID(o.ID); //找到对应的树节点
  264. projectObj.mainController.deleteNode(node, next);
  265. }
  266. }
  267. project.calcProgram.calcAndSave(rationNode,function () {
  268. installationFeeObj.calcInstallationFee();
  269. });
  270. };
  271. // CSL,2017.05.09
  272. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  273. this.project.beginUpdate('modifyQuantity');
  274. data.quantity = newQuantity;
  275. data.customQuantity = newQuantity;
  276. data.updateType = 'ut_update';
  277. this.project.push(this.getSourceType, data);
  278. this.project.endUpdate();
  279. };
  280. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  281. this.project.beginUpdate('modifyPrice');
  282. data.price = newPrice;
  283. data.updateType = 'ut_update';
  284. this.project.push(this.getSourceType, data);
  285. this.project.endUpdate();
  286. };
  287. ration_glj.prototype.deleteGLJ = function (data) {
  288. this.project.beginUpdate('deleteGLJ');
  289. data.customQuantity = 0;
  290. data.quantity = 0;
  291. data.rationItemQuantity = 0;
  292. data.updateType = 'ut_update';
  293. this.project.push(this.getSourceType, data);
  294. this.project.endUpdate();
  295. };
  296. ration_glj.prototype.addRationGLJ = function (newRation, data) {
  297. let souceTypeList = [];
  298. let criteriaDataList = [];
  299. if (data.hasOwnProperty('rationGljList') && data.rationGljList.length > 0) {
  300. let criteria = {};
  301. criteria.ration_glj_list = [];
  302. for (let i = 0; i < data.rationGljList.length; i++) {
  303. let temdata = data.rationGljList[i];
  304. let newGLJ = {};
  305. newGLJ.projectID = parseInt(newRation.projectID);
  306. newGLJ.GLJID = temdata.gljId;
  307. newGLJ.rationID = newRation.ID;
  308. newGLJ.billsItemID = newRation.billsItemID;
  309. newGLJ.rationItemQuantity = temdata.consumeAmt;
  310. newGLJ.quantity = temdata.consumeAmt;
  311. newGLJ.glj_repository_id = data.rationRepId;
  312. criteria.ration_glj_list.push(newGLJ);
  313. }
  314. criteria.updateType = 'ut_create';
  315. souceTypeList.push(this.getSourceType());
  316. criteriaDataList.push(criteria);
  317. }
  318. let ration_coe = projectObj.project.ration_coe;
  319. let rationCoeData = ration_coe.getRationCoedata(newRation, data);
  320. souceTypeList.push(ration_coe.getSourceType());
  321. criteriaDataList.push(rationCoeData);
  322. project.pushNow('addRationGLJAndRationCoe', souceTypeList, criteriaDataList);
  323. };
  324. ration_glj.prototype.getDeleteDataByRation = function (rationData) {
  325. let updateData = [];
  326. updateData.push({
  327. 'deleteType': 'RATION',
  328. 'updateType': 'ut_delete',
  329. 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}
  330. });
  331. return updateData;
  332. };
  333. ration_glj.prototype.getDeleteDataByBills = function (datas) {
  334. let updateData = [];
  335. datas.forEach(function (deleteData) {
  336. if (deleteData.type == 'delete') {
  337. let billData = deleteData.data;
  338. updateData.push({
  339. 'deleteType': 'BILL',
  340. 'updateType': 'ut_delete',
  341. 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}
  342. });
  343. }
  344. })
  345. return updateData;
  346. };
  347. ration_glj.prototype.deleteByRation = function (ration) {
  348. let glj_list = projectObj.project.ration_glj.datas;
  349. _.remove(glj_list,{'rationID':ration.ID});
  350. };
  351. ration_glj.prototype.deleteByBills = function (deleteData) {
  352. let rationList = projectObj.project.Ration.datas;
  353. let deleteRationList = [];
  354. for (let i = 0; i < deleteData.length; i++) {
  355. if (deleteData[i].type == 'delete') {
  356. let billID = deleteData[i].data.ID;
  357. let raList = _.filter(rationList, (ration) => {
  358. return ration.billsItemID == billID;
  359. });
  360. deleteRationList = deleteRationList.concat(raList);
  361. }
  362. }
  363. for (let i = 0; i < deleteRationList.length; i++) {
  364. this.deleteByRation(deleteRationList[i]);
  365. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  366. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  367. projectObj.project.ration_installation.deleteByRation(deleteRationList[i]);
  368. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  369. }
  370. }
  371. ration_glj.prototype.updataOrdelete = function (row) {
  372. let updateData = null;
  373. if (row.rationItemQuantity == 0) {
  374. /* updateData = this.getUpdateData('ut_delete', {
  375. 'ID': row.ID,
  376. 'projectID': row.projectID
  377. }, {rationID: row.rationID});
  378. project.pushNow('updateRationGLJ', [this.getSourceType()], updateData)*/
  379. this.deleteRationGLJ({ID:row.ID,rationID:row.rationID, projectID: row.projectID,type:row.type});
  380. } else {
  381. this.updateRationGLJByEdit(row, 'customQuantity', 0);
  382. //('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  383. }
  384. };
  385. ration_glj.prototype.deleteRationGLJ = async function(data){
  386. try {
  387. let result = await ajaxPost('/rationGlj/deleteRationGLJ',data);
  388. console.log(result);
  389. let oldData = this.addAndDeleteDatas([],result.deleteList);
  390. this.refreshQuantityAfterCalResult(result.glj_result);
  391. let rationNode = null;
  392. let next = null;
  393. let selected = projectObj.project.mainTree.findNode(result.rationID);
  394. if (selected.sourceType == ModuleNames.ration) { //如果选中的是定额,说明是右键删除工料机操作,如果选中的是定额工料机,则说明是在造价书主界面中点击了删除按钮
  395. rationNode = selected;
  396. } else if (selected.sourceType == ModuleNames.ration_glj) {
  397. rationNode = selected.parent;
  398. next = true;
  399. }
  400. rationNode.data.adjustState = result.adjustState;
  401. projectObj.mainController.refreshTreeNode([rationNode]);
  402. gljOprObj.refreshView();
  403. for (let o of oldData) {
  404. if (this.needShowToTree(o)) {
  405. let node = me.findGLJNodeByID(o.ID); //找到对应的树节点
  406. projectObj.mainController.deleteNode(node, next);
  407. }
  408. }
  409. project.calcProgram.calcAndSave(rationNode,function () {
  410. installationFeeObj.calcInstallationFee();
  411. });
  412. projectObj.project.projectGLJ.loadData();
  413. }catch (e){
  414. console.log(e)
  415. }
  416. };
  417. ration_glj.prototype.getUpdateData = function (type, query, doc, callfunction) {
  418. let updateData = [];
  419. let newobj = {
  420. 'updateType': type,
  421. 'query': query,
  422. }
  423. if (doc) {
  424. newobj['doc'] = doc;
  425. }
  426. if (callfunction) {
  427. newobj['updateFunction'] = callfunction;
  428. }
  429. updateData.push(newobj);
  430. return updateData;
  431. };
  432. ration_glj.prototype.updateRationGLJByEdit = function (recode, updateField, newval, node) {
  433. let me = this,doc = {};
  434. doc[updateField] = newval;
  435. me.submitChange(recode,doc,updateField,node);
  436. };
  437. ration_glj.prototype.submitChange = function(recode,updateData,updateField,node){
  438. let me = this;
  439. let query = {
  440. 'ID': recode.ID,
  441. 'projectID': recode.projectID,
  442. 'rationID': recode.rationID
  443. };
  444. let priceInfo = {
  445. base_price: recode.basePrice,
  446. market_price: recode.marketPrice
  447. };
  448. if(updateData['type'] != undefined || updateData['type'] != null){
  449. updateData.shortName = projectObj.project.projectGLJ.getShortNameByID(updateData['type']);
  450. }
  451. let callback = function (data) {
  452. if (updateField == 'customQuantity') {
  453. me.refreshAfterQuantityUpdate(data, node);
  454. } else {
  455. let doc = data.doc;
  456. for (let key in doc) {
  457. recode[key] = doc[key];
  458. }
  459. me.refreshRationAfterEdit(data,recode.rationID, node);//更新名称和定额调整状态
  460. if (node) {//如果不是在造价书页面直接编辑,则不用刷新
  461. if (updateField == "type" && !(updateData['type'] == gljType.MAIN_MATERIAL || updateData['type'] == gljType.EQUIPMENT)) {//如果改变类型后不是主材或设备,则在造价书树中移除
  462. projectObj.mainController.deleteNode(node, true);
  463. }
  464. }
  465. }
  466. projectObj.project.projectGLJ.loadData(function () {//等项目工料机加载完成后再给用户编辑
  467. me.refreshTreeNodeIfNeeded(recode);//刷新造价书上的树节点(如果需要)
  468. me.reCalcWhenGLJChange(recode);//触发计算定额以及父节点
  469. zmhs_obj.refreshStableDataIfNeeded(); $.bootstrapLoading.end();
  470. installationFeeObj.calcInstallationFee();
  471. });
  472. };
  473. $.bootstrapLoading.start();
  474. CommonAjax.post("/rationGlj/updateRationGLJByEdit", {
  475. query: query,
  476. doc: updateData,
  477. priceInfo: priceInfo
  478. }, callback, function (err) {
  479. $.bootstrapLoading.end();
  480. });
  481. };
  482. ration_glj.prototype.refreshAfterQuantityUpdate = function (data, node) {
  483. this.refreshQuantityAfterCalResult(data.glj_result);
  484. this.refreshRationAfterEdit(data, data.rationID, node);
  485. };
  486. ration_glj.prototype.refreshQuantityAfterCalResult = function (glj_result) {//后端计算后,前端刷新结果
  487. let me = this;
  488. glj_result.forEach(function (item) {
  489. me.refreshEachItme(item.doc, item.query);
  490. });
  491. };
  492. ration_glj.prototype.refreshRationAfterEdit= function(data,rationID,rnode){
  493. let nodes = [];
  494. let node = this.refreshRationNode(data.name,data.adjustState,rationID);
  495. if (node) nodes.push(node);
  496. if (rnode) nodes.push(rnode);
  497. projectObj.mainController.refreshTreeNode(nodes);
  498. };
  499. ration_glj.prototype.refreshRationNode = function(name,adjustState,rationID){
  500. let node = projectObj.project.mainTree.findNode(rationID);
  501. if (!node) return null;
  502. if(gljUtil.isDef(adjustState)){
  503. node.data.adjustState = adjustState;
  504. }
  505. if(gljUtil.isDef(name)){
  506. node.data.name = name;
  507. }
  508. return node
  509. };
  510. ration_glj.prototype.getGLJData = function (cb) {
  511. let property = projectObj.project.projectInfo.property;
  512. let engineerID = property.engineering_id;
  513. CommonAjax.get('/rationGlj/getGLJData/'+engineerID, function (data) {
  514. //编办中有多单价设置
  515. if(data.datas.priceProperties && data.datas.priceProperties.length > 0){
  516. let tem = _.find(data.datas.priceProperties,{region:property.region,taxModel:parseInt(property.taxType)});
  517. if(tem){
  518. let dataCode = tem.price.dataCode;
  519. for(let glj of data.datas.stdGLJ){
  520. if(glj.priceProperty && gljUtil.isDef(glj.priceProperty[dataCode])) glj.basePrice = glj.priceProperty[dataCode];
  521. }
  522. }
  523. };
  524. cb(data);
  525. })
  526. };
  527. ration_glj.prototype.insertGLJAsRation = function (GLJSelection, selected, callback) {
  528. let gljList = [];
  529. let allGLJ = gljOprObj.AllRecode;
  530. let billsItemID = null, serialNo = 0,selectedSerialNo = null,nextNodeID = null, parentNodeID = null, billNode = null;
  531. let pEngineer = projectObj.project.projectInfo.property.projectEngineering;
  532. let children = [];
  533. if (selected.sourceType === project.Bills.getSourceType()) {
  534. billsItemID = selected.data.ID;
  535. parentNodeID = selected.getID();
  536. nextNodeID = selected.tree.rootID();
  537. billNode = selected;
  538. } else {
  539. billsItemID = selected.data.billsItemID;
  540. serialNo = selected.data.serialNo;
  541. selectedSerialNo = selected.data.serialNo;
  542. nextNodeID = selected.getNextSiblingID();
  543. parentNodeID = selected.getParentID();
  544. billNode = selected.parent;
  545. }
  546. children = project.Ration.getBillsSortRation(billsItemID);
  547. serialNo == 0 ? serialNo = children.length : "";
  548. for (let con_key of GLJSelection) {
  549. let glj = _.find(allGLJ, function (item) {
  550. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  551. return i_key == con_key;
  552. });
  553. if (glj) {
  554. serialNo += 1;
  555. let new_glj = {
  556. ID: project.Ration.getNewRationID(),
  557. projectID: parseInt(project.ID()),
  558. billsItemID: billsItemID,
  559. type: rationType.gljRation,
  560. code: glj.code,
  561. name: glj.name,
  562. quantity: 0,
  563. unit: glj.unit,
  564. specs: glj.specs,
  565. subType: glj.gljType,
  566. model:glj.model,
  567. basePrice: glj.basePrice,
  568. marketPrice:glj.basePrice,
  569. original_code: glj.code,
  570. shortName: glj.shortName,
  571. serialNo: serialNo,
  572. GLJID: glj.ID,
  573. adjCoe: glj.adjCoe,
  574. materialType:glj.materialType,
  575. materialCoe:glj.materialCoe,
  576. grossWeightCoe:glj.grossWeightCoe,
  577. purchaseStorageRate:glj.purchaseStorageRate,
  578. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  579. handlingLossRate:glj.handlingLossRate,
  580. repositoryId: glj.repositoryId
  581. };
  582. if(pEngineer) new_glj.programID = pEngineer;
  583. let belongFlag = cbTools.getBelongFlag(selected);
  584. if (belongFlag && belongFlag === fixedFlag.EQUIPMENT_ACQUISITION_FEE) {
  585. new_glj.programID = projectObj.project.calcProgram.compiledTemplateMaps['设备购置'];
  586. }
  587. if (glj.hasOwnProperty("compilationId")) {
  588. new_glj.from = "cpt";
  589. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  590. new_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  591. }
  592. }
  593. if(optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')){//需要根据清单转换工程量
  594. new_glj.quantityEXP="QDL";
  595. if(billNode.data.quantity){
  596. new_glj.quantity = scMathUtil.roundForObj(billNode.data.quantity/FilterNumberFromUnit(glj.unit),getDecimal("glj.quantity"));
  597. new_glj.contain = scMathUtil.roundForObj(new_glj.quantity/billNode.data.quantity,6);
  598. }
  599. }
  600. gljList.push(new_glj);
  601. }
  602. }
  603. if (gljList.length == 0) {
  604. return;
  605. }
  606. let postData = {
  607. gljList: gljList,
  608. projectID: parseInt(project.ID()),
  609. billsItemID: billsItemID,
  610. rationCount: project.Ration.maxRationID()
  611. }
  612. selectedSerialNo == null ? "" : postData.selectedSerialNo = selectedSerialNo;
  613. CommonAjax.post("/ration/insertGLJAsRation", postData, function (data) {
  614. // 更新兄弟节点的序列号
  615. if (selectedSerialNo != null) {
  616. let selectIndex = _.findIndex(children, {"serialNo": selectedSerialNo});
  617. if (selectIndex + 1 < children.length) {
  618. for (let i = selectIndex + 1; i < children.length; i++) {
  619. children[i].serialNo += gljList.length;
  620. }
  621. }
  622. }
  623. callback(parentNodeID,nextNodeID,data);
  624. }, function () {
  625. $.bootstrapLoading.end();
  626. });
  627. };
  628. ration_glj.prototype.getGLJDataByCodes = function (codes,callback) {
  629. if(!gljUtil.isDef(codes)||codes.length ==0) {
  630. if(callback) callback([]);
  631. return
  632. }
  633. $.bootstrapLoading.start();
  634. CommonAjax.post("/rationGlj/getGLJDataByCodes", { 'engineerID':projectObj.project.projectInfo.property.engineering_id,projectID:projectObj.project.ID(),codes:codes}, function (result) {
  635. $.bootstrapLoading.end();
  636. callback(result)
  637. });
  638. };
  639. ration_glj.prototype.getAddDataByStd = function (glj,rationID,billsItemID,projectID) {//生成添加类型的定额工料机数据
  640. let ration_glj = {
  641. projectID: projectID,
  642. GLJID: glj.ID,
  643. rationID: rationID,
  644. billsItemID: billsItemID,
  645. rationItemQuantity: 0,
  646. quantity: 0,
  647. name: glj.name,
  648. code: glj.code,
  649. original_code: glj.code,
  650. unit: glj.unit,
  651. specs: glj.specs,
  652. basePrice: glj.basePrice,
  653. marketPrice:glj.basePrice,
  654. shortName: glj.shortName,
  655. type: glj.gljType,
  656. model:glj.model,
  657. adjCoe: glj.adjCoe,
  658. createType: 'add',
  659. materialType:glj.materialType,
  660. materialCoe:glj.materialCoe,
  661. grossWeightCoe:glj.grossWeightCoe,
  662. purchaseStorageRate:glj.purchaseStorageRate,
  663. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  664. handlingLossRate:glj.handlingLossRate,
  665. repositoryId: glj.repositoryId
  666. };
  667. if (glj.hasOwnProperty("compilationId")) {
  668. ration_glj.from = "cpt";
  669. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  670. ration_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  671. }
  672. }
  673. return ration_glj;
  674. };
  675. ration_glj.prototype.addGLJByLib = function (GLJSelection, ration, callback) {
  676. let me=this,gljList = [];
  677. let allGLJ = gljOprObj.AllRecode;
  678. GLJSelection.sort();
  679. _.forEach(GLJSelection, function (g) {
  680. let glj = _.find(allGLJ, function (item) {
  681. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  682. return i_key == g;
  683. });
  684. let ration_glj = me.getAddDataByStd(glj,ration.ID,ration.billsItemID,ration.projectID);
  685. gljList.push(ration_glj);
  686. });
  687. $.bootstrapLoading.start();
  688. CommonAjax.post("/rationGlj/addGLJ", gljList, callback, function () {
  689. $.bootstrapLoading.end();
  690. });
  691. };
  692. ration_glj.prototype.updateRationGLJByChangeCode = function (recode, updateField, newval) {
  693. let me = this;
  694. let priceMap = projectObj.project.projectGLJ.datas.unitPriceMap;
  695. let priceObj = priceMap[newval];
  696. if(priceObj){
  697. if (recode.createType===undefined || recode.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  698. recode.rcode = recode.code;
  699. recode.createType = 'replace';
  700. }
  701. recode.code = priceObj.code;
  702. recode.name = priceObj.name;
  703. recode.type = priceObj.type;
  704. recode.unit = priceObj.unit;
  705. recode.shortName = priceObj.short_name;
  706. recode.specs = priceObj.specs;
  707. recode.GLJID=priceObj.glj_id;
  708. recode.original_code = priceObj.original_code;
  709. recode.basePrice = priceObj.base_price;
  710. recode.marketPrice = priceObj.market_price;
  711. }
  712. $.bootstrapLoading.start();
  713. CommonAjax.post("/rationGlj/replaceGLJ", recode, function (result) {
  714. if (result) {
  715. me.setDatasAfterReplace(result);
  716. let data = result.data;
  717. let rationNode = projectObj.project.mainTree.findNode(data.rationID);
  718. let nodes = [rationNode];
  719. project.projectGLJ.loadData(function () {//加载完项目工料机再计算
  720. gljOprObj.refreshView();
  721. if (project.ration_glj.needShowToTree(data)) {//当替换的是主材或设备时,刷新对应的树节点
  722. let node = project.ration_glj.findGLJNodeByID(data.ID);
  723. if (node) {
  724. project.ration_glj.transferToNodeData(data);
  725. node.source = data;
  726. node.data = data;
  727. }
  728. node ? nodes.push(node) : "";
  729. }
  730. rationNode.data.adjustState = result.adjustState;
  731. rationNode.data.name = result.name;
  732. projectObj.mainController.refreshTreeNode(nodes);
  733. project.calcProgram.calcAndSave(rationNode);
  734. $.bootstrapLoading.end();
  735. });
  736. }
  737. }, function () {
  738. $.bootstrapLoading.end();
  739. });
  740. };
  741. ration_glj.prototype.setDatasAfterReplace = function(result){
  742. let me = this;
  743. me.addAndDeleteDatas(result.newRecodes,result.deleteList);
  744. let list_index = _.findIndex(me.datas, {'ID': result.data.ID});
  745. if(result.data) me.datas[list_index] = result.data;
  746. me.refreshQuantityAfterCalResult(result.glj_result);
  747. };
  748. ration_glj.prototype.addAndDeleteDatas = function (newRecodes,deleteList) {
  749. let oldData = [];
  750. if(newRecodes.length > 0 ) this.datas = this.datas.concat(newRecodes);
  751. if(deleteList.length > 0) {
  752. oldData = _.remove(this.datas,function (item) {
  753. return deleteList.indexOf(item.ID)!=-1;
  754. });
  755. }
  756. return oldData;
  757. };
  758. ration_glj.prototype.getReplaceDataByStd = function (oldData,glj) {
  759. if (oldData.createType===undefined || oldData.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  760. oldData.rcode = oldData.code;
  761. oldData.createType = 'replace';
  762. oldData.mIndexObj ={code:oldData.code,name:oldData.name,type:oldData.type,unit:oldData.unit,specs:oldData.specs};
  763. }else if(oldData.createType == 'replace' && oldData.rcode == glj.code){//如果原数据已经是替换过的,这次替换又恢复成原数据,则把类型改回来
  764. oldData.createType = 'normal';
  765. oldData.mIndexObj = null;
  766. }
  767. oldData.GLJID = glj.ID;
  768. oldData.name = glj.name;
  769. oldData.type = glj.gljType;
  770. oldData.code = glj.code;
  771. oldData.original_code = glj.code;
  772. oldData.unit = glj.unit;
  773. oldData.specs = glj.specs;
  774. oldData.model = glj.model;
  775. oldData.basePrice = glj.basePrice;
  776. oldData.shortName=glj.shortName;
  777. oldData.marketPrice = glj.basePrice;
  778. oldData.repositoryId = glj.repositoryId;
  779. oldData.materialType = glj.materialType;
  780. oldData.materialCoe = glj.materialCoe;
  781. oldData.grossWeightCoe= glj.grossWeightCoe;
  782. oldData.purchaseStorageRate = glj.purchaseStorageRate;
  783. oldData.offSiteTransportLossRate = glj.offSiteTransportLossRate;
  784. oldData.handlingLossRate = glj.handlingLossRate;
  785. if (glj.hasOwnProperty("compilationId")) {
  786. oldData.from = "cpt";
  787. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  788. oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  789. }
  790. } else {
  791. oldData.from = "std";
  792. }
  793. return oldData;
  794. };
  795. ration_glj.prototype.replaceGLJ = function (selectCode, oldData, callback) {
  796. let allGLJ = gljOprObj.AllRecode;
  797. let glj = _.find(allGLJ, function (item) {
  798. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  799. return i_key == selectCode;
  800. });
  801. if (selectCode == gljOprObj.getIndex(oldData, gljKeyArray)) {
  802. return callback(null);
  803. }
  804. //如果是从混凝土改成商品混凝土,并且混凝土的定额消耗量不为空,则原混凝土的自定义消耗改成0,插入一条新的商品混凝土自定义消耗量为原定额计算后的消耗量
  805. if(gljUtil.isAddCommercialForReplace(oldData.type,glj.gljType,oldData.rationItemQuantity)){
  806. let originalID = oldData.ID;
  807. let customQuantity = gljUtil.isDef(oldData.quantity)? oldData.quantity:0;
  808. oldData = this.getAddDataByStd(glj,oldData.rationID,oldData.billsItemID,oldData.projectID);
  809. oldData.toCommercial=true;
  810. oldData.originalID = originalID;
  811. oldData.customQuantity = customQuantity;
  812. }else {
  813. oldData = this.getReplaceDataByStd(oldData,glj);
  814. }
  815. $.bootstrapLoading.start();
  816. CommonAjax.post("/rationGlj/replaceGLJ", oldData, callback, function () {
  817. $.bootstrapLoading.end();
  818. });
  819. };
  820. ration_glj.prototype.mReplaceGLJ = function (selectCode, oldData, callback) {
  821. let allGLJ = gljOprObj.AllRecode,tasks = [],updateMap={},doc = null;
  822. let oldIndex = gljOprObj.getIndex(oldData, gljKeyArray);
  823. let glj = _.find(allGLJ, function (item) {
  824. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  825. return i_key == selectCode;
  826. });
  827. if (selectCode == oldIndex) {
  828. return callback(null);
  829. }
  830. let query = {
  831. projectID: oldData.projectID,
  832. code: oldData.code,
  833. name: oldData.name,
  834. unit: oldData.unit,
  835. type: oldData.type
  836. };
  837. if (oldData.specs && oldData.specs != '') {
  838. query.specs = oldData.specs;
  839. }
  840. for(let d of this.datas){//查询出所有需替换的工料机
  841. if(!gljOprObj.scopeSelectedIDMap[d.billsItemID]) continue; //如果不在选中范围的,跳过
  842. let tem_index = gljOprObj.getIndex(d, gljKeyArray);
  843. if(tem_index == oldIndex){
  844. let tem_doc = {};
  845. //是否从混凝土改成商品混凝土,并且混凝土的定额消耗量不为空
  846. if(gljUtil.isAddCommercialForReplace(d.type,glj.gljType,d.rationItemQuantity)){
  847. tem_doc = {'customQuantity':'0'};
  848. //插入一条新的定额,这里的更新自定义消耗量操作由task完成了,不用像单条替换那样做标记,修改等
  849. let newDoc = this.getAddDataByStd(glj,d.rationID,d.billsItemID,d.projectID);
  850. let customQuantity = gljUtil.isDef(d.quantity)? d.quantity:0;
  851. newDoc.customQuantity = customQuantity;
  852. let tTask = {insertOne:{document: newDoc}};
  853. tasks.push(tTask);
  854. doc=newDoc//后台操作: 要根据nweDoc,插入项目工料机,修改insertOne里项目工料机ID,生成新的自身ID
  855. }else {
  856. tem_doc = this.getReplaceDataByStd(d,glj);
  857. if(gljUtil.isConcreteType(d.type)||gljUtil.isCommercialConcreteType(d.type)) tem_doc.isConcrete = true;//这里是在批量替换中有可能同时存在定额下的替换和添加的替换,对于添加的替换,要走替换混凝土的逻辑,定额下的替换又走另外的逻辑
  858. doc = tem_doc;
  859. }
  860. let task = {
  861. updateOne:{
  862. filter : {ID:d.ID,rationID:d.rationID},
  863. update : tem_doc
  864. }
  865. };
  866. tasks.push(task);
  867. updateMap[d.ID] = tem_doc;
  868. }
  869. }
  870. $.bootstrapLoading.start();
  871. CommonAjax.post("/rationGlj/mReplaceGLJ", {query: query, doc: doc,tasks:tasks}, function (result) {
  872. callback(result,updateMap);
  873. }, function () {
  874. $.bootstrapLoading.end();
  875. });
  876. };
  877. ration_glj.prototype.getMainAndEquGLJ = function (rationID) {
  878. let gljList = _.filter(this.datas, function (n) {
  879. return n.rationID == rationID && (n.type == gljType.MAIN_MATERIAL || n.type == gljType.EQUIPMENT)
  880. });
  881. gljList = gljUtil.sortRationGLJ(gljList);
  882. return gljOprObj.combineWithProjectGlj(gljList);
  883. };
  884. ration_glj.prototype.transferToNodeData = function (data) {
  885. data.contain=scMathUtil.roundForObj(data.quantity,getDecimal('glj.quantity'));
  886. data.subType = data.type;
  887. };
  888. ration_glj.prototype.combineRationAndGLJ = function (ration) {
  889. if (ration) {
  890. let projectGLJData = projectObj.project.projectGLJ.datas;
  891. let projectGljs = projectGLJData.gljList;
  892. let mixRatioMap = projectGLJData.mixRatioMap;
  893. let glj = _.find(projectGljs, {'id': ration.projectGLJID});
  894. if (glj) {
  895. if(projectObj.project.projectGLJ.isEstimateType(glj.type)){
  896. ration.isEstimate = glj.is_evaluate;
  897. }
  898. ration = gljOprObj.setGLJPrice(ration,glj);
  899. ration.isAdd = glj.unit_price.is_add;
  900. let connect_index = gljOprObj.getIndex(glj, gljKeyArray);
  901. if (mixRatioMap.hasOwnProperty(connect_index)) {
  902. let mixRatios = gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
  903. ration.subList = mixRatios;
  904. }
  905. }
  906. }
  907. return ration;
  908. };
  909. ration_glj.prototype.updateFromMainSpread = function (value, node, fieldName) {
  910. if (node.data[fieldName] !== value) {
  911. if (fieldName == "marketUnitFee") {
  912. let decimal = getDecimal("glj.unitPrice");
  913. let newval = number_util.checkNumberValue(value, decimal);
  914. if (newval) {
  915. fieldName = "marketPrice";
  916. projectObj.project.projectGLJ.updatePriceFromRG(node.data, fieldName, newval);
  917. return;
  918. }
  919. } else {
  920. if(fieldName == "contain"){
  921. if(value==null){
  922. value="";
  923. }else {
  924. let decimal = getDecimal("glj.quantity");
  925. value = number_util.checkNumberValue(value, decimal);
  926. fieldName="customQuantity";//填入自定义消耗
  927. }
  928. }
  929. if (value !== undefined && value !== null) {
  930. if (fieldName == "subType") {
  931. node.data.subType = value;
  932. fieldName = "type";//转换成更新工料机类型
  933. }
  934. this.updateRationGLJByEdit(node.data, fieldName, value, node);
  935. return;
  936. }
  937. }
  938. }
  939. // node.data.subType = value;
  940. projectObj.mainController.refreshTreeNode([node]);
  941. };
  942. ration_glj.prototype.refreshTreeNodeIfNeeded = function (data) {
  943. if (this.needShowToTree(data)) {
  944. this.transferToNodeData(data);
  945. gljOprObj.refreshTreeNode({"type": ModuleNames.ration_glj, "ID": data.ID});
  946. }
  947. };
  948. ration_glj.prototype.needShowToTree = function (data) {
  949. if ((data.type == gljType.MAIN_MATERIAL || data.type == gljType.EQUIPMENT) && projectObj.project.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  950. return true
  951. }
  952. return false
  953. };
  954. ration_glj.prototype.findGLJNodeByID = function (ID) {
  955. return projectObj.project.mainTree.findNode(ID);
  956. };
  957. ration_glj.prototype.findRationNodeByID = function (ID) {
  958. return projectObj.project.mainTree.findNode(ID);
  959. };
  960. ration_glj.prototype.reCalcWhenGLJChange = function (ration_glj) {//当改变定额工料机时,重新计算定额以及其父节点
  961. let node = this.findRationNodeByID(ration_glj.rationID);
  962. if (node) {
  963. node.changed = true;
  964. project.calcProgram.calcAndSave(node);
  965. }
  966. };
  967. ration_glj.prototype.reCalcWhenVvTaxChange = function () {
  968. let rationIDs = [];
  969. for(let g of this.datas){
  970. if(g.type == gljType.GENERAL_MACHINE) rationIDs.push(g.rationID);
  971. }
  972. this.calcRationsByIDs(_.uniq(rationIDs))
  973. };
  974. ration_glj.prototype.calcRationsByIDs = function (rationIDs) {
  975. let nodes = [];
  976. for(let ID of rationIDs){
  977. let node = projectObj.project.mainTree.findNode(ID);
  978. if(node) nodes.push(node)
  979. }
  980. project.calcProgram.calcNodesAndSave(nodes);
  981. };
  982. ration_glj.prototype.updateProportion = function (proportionList, projectID, rationID) {
  983. const postData = {
  984. proportionList,
  985. projectID,
  986. rationID
  987. };
  988. $.bootstrapLoading.start();
  989. CommonAjax.post('/rationGLJ/updateProportion', postData, function (rst) {
  990. proportionList.forEach(item => {
  991. const glj = zmhs_obj.stableSheetData.find(glj => glj.ID === item.ID);
  992. if (glj) {
  993. glj.adjustProportion = item.adjustProportion;
  994. }
  995. });
  996. zmhs_obj.refreshStableDataIfNeeded();
  997. zmhs_obj.refreshAfterUpdate(rst);
  998. $.bootstrapLoading.end();
  999. }, function () {
  1000. zmhs_obj.refreshStableDataIfNeeded();
  1001. $.bootstrapLoading.end();
  1002. });
  1003. };
  1004. return new ration_glj(project);
  1005. }
  1006. };