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