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