ration_glj.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. gljOprObj.refreshView();
  468. me.refreshTreeNodeIfNeeded(recode);//刷新造价书上的树节点(如果需要)
  469. me.reCalcWhenGLJChange(recode);//触发计算定额以及父节点
  470. $.bootstrapLoading.end();
  471. installationFeeObj.calcInstallationFee();
  472. });
  473. };
  474. $.bootstrapLoading.start();
  475. CommonAjax.post("/rationGlj/updateRationGLJByEdit", {
  476. query: query,
  477. doc: updateData,
  478. priceInfo: priceInfo
  479. }, callback, function (err) {
  480. $.bootstrapLoading.end();
  481. });
  482. };
  483. ration_glj.prototype.refreshAfterQuantityUpdate = function (data, node) {
  484. this.refreshQuantityAfterCalResult(data.glj_result);
  485. this.refreshRationAfterEdit(data, data.rationID, node);
  486. };
  487. ration_glj.prototype.refreshQuantityAfterCalResult = function (glj_result) {//后端计算后,前端刷新结果
  488. let me = this;
  489. glj_result.forEach(function (item) {
  490. me.refreshEachItme(item.doc, item.query);
  491. });
  492. };
  493. ration_glj.prototype.refreshRationAfterEdit= function(data,rationID,rnode){
  494. let nodes = [];
  495. let node = this.refreshRationNode(data.name,data.adjustState,rationID);
  496. if (node) nodes.push(node);
  497. if (rnode) nodes.push(rnode);
  498. projectObj.mainController.refreshTreeNode(nodes);
  499. };
  500. ration_glj.prototype.refreshRationNode = function(name,adjustState,rationID){
  501. let node = projectObj.project.mainTree.findNode(rationID);
  502. if (!node) return null;
  503. if(gljUtil.isDef(adjustState)){
  504. node.data.adjustState = adjustState;
  505. }
  506. if(gljUtil.isDef(name)){
  507. node.data.name = name;
  508. }
  509. return node
  510. };
  511. ration_glj.prototype.getGLJData = function (cb) {
  512. let property = projectObj.project.projectInfo.property;
  513. let engineerID = property.engineering_id;
  514. CommonAjax.get('/rationGlj/getGLJData/'+engineerID, function (data) {
  515. //编办中有多单价设置
  516. if(data.datas.priceProperties && data.datas.priceProperties.length > 0){
  517. let tem = _.find(data.datas.priceProperties,{region:property.region,taxModel:parseInt(property.taxType)});
  518. if(tem){
  519. let dataCode = tem.price.dataCode;
  520. for(let glj of data.datas.stdGLJ){
  521. if(glj.priceProperty && gljUtil.isDef(glj.priceProperty[dataCode])) glj.basePrice = glj.priceProperty[dataCode];
  522. }
  523. }
  524. };
  525. cb(data);
  526. })
  527. };
  528. ration_glj.prototype.insertGLJAsRation = function (GLJSelection, selected, callback) {
  529. let gljList = [];
  530. let allGLJ = gljOprObj.AllRecode;
  531. let billsItemID = null, serialNo = 0,selectedSerialNo = null,nextNodeID = null, parentNodeID = null, billNode = null;
  532. let pEngineer = projectObj.project.projectInfo.property.projectEngineering;
  533. let children = [];
  534. if (selected.sourceType === project.Bills.getSourceType()) {
  535. billsItemID = selected.data.ID;
  536. parentNodeID = selected.getID();
  537. nextNodeID = selected.tree.rootID();
  538. billNode = selected;
  539. } else {
  540. billsItemID = selected.data.billsItemID;
  541. serialNo = selected.data.serialNo;
  542. selectedSerialNo = selected.data.serialNo;
  543. nextNodeID = selected.getNextSiblingID();
  544. parentNodeID = selected.getParentID();
  545. billNode = selected.parent;
  546. }
  547. children = project.Ration.getBillsSortRation(billsItemID);
  548. serialNo == 0 ? serialNo = children.length : "";
  549. for (let con_key of GLJSelection) {
  550. let glj = _.find(allGLJ, function (item) {
  551. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  552. return i_key == con_key;
  553. });
  554. if (glj) {
  555. serialNo += 1;
  556. let new_glj = {
  557. ID: project.Ration.getNewRationID(),
  558. projectID: parseInt(project.ID()),
  559. billsItemID: billsItemID,
  560. type: rationType.gljRation,
  561. code: glj.code,
  562. name: glj.name,
  563. quantity: 0,
  564. unit: glj.unit,
  565. specs: glj.specs,
  566. subType: glj.gljType,
  567. model:glj.model,
  568. basePrice: glj.basePrice,
  569. marketPrice:glj.basePrice,
  570. original_code: glj.code,
  571. shortName: glj.shortName,
  572. serialNo: serialNo,
  573. GLJID: glj.ID,
  574. adjCoe: glj.adjCoe,
  575. materialType:glj.materialType,
  576. materialCoe:glj.materialCoe,
  577. grossWeightCoe:glj.grossWeightCoe,
  578. purchaseStorageRate:glj.purchaseStorageRate,
  579. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  580. handlingLossRate:glj.handlingLossRate,
  581. repositoryId: glj.repositoryId
  582. };
  583. if(pEngineer) new_glj.programID = pEngineer;
  584. let belongFlag = cbTools.getBelongFlag(selected);
  585. if (belongFlag && belongFlag === fixedFlag.EQUIPMENT_ACQUISITION_FEE) {
  586. new_glj.programID = projectObj.project.calcProgram.compiledTemplateMaps['设备购置'];
  587. }
  588. if (glj.hasOwnProperty("compilationId")) {
  589. new_glj.from = "cpt";
  590. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  591. new_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  592. }
  593. }
  594. if(optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')){//需要根据清单转换工程量
  595. new_glj.quantityEXP="QDL";
  596. if(billNode.data.quantity){
  597. new_glj.quantity = scMathUtil.roundForObj(billNode.data.quantity/FilterNumberFromUnit(glj.unit),getDecimal("glj.quantity"));
  598. new_glj.contain = scMathUtil.roundForObj(new_glj.quantity/billNode.data.quantity,6);
  599. }
  600. }
  601. gljList.push(new_glj);
  602. }
  603. }
  604. if (gljList.length == 0) {
  605. return;
  606. }
  607. let postData = {
  608. gljList: gljList,
  609. projectID: parseInt(project.ID()),
  610. billsItemID: billsItemID,
  611. rationCount: project.Ration.maxRationID()
  612. }
  613. selectedSerialNo == null ? "" : postData.selectedSerialNo = selectedSerialNo;
  614. CommonAjax.post("/ration/insertGLJAsRation", postData, function (data) {
  615. // 更新兄弟节点的序列号
  616. if (selectedSerialNo != null) {
  617. let selectIndex = _.findIndex(children, {"serialNo": selectedSerialNo});
  618. if (selectIndex + 1 < children.length) {
  619. for (let i = selectIndex + 1; i < children.length; i++) {
  620. children[i].serialNo += gljList.length;
  621. }
  622. }
  623. }
  624. callback(parentNodeID,nextNodeID,data);
  625. }, function () {
  626. $.bootstrapLoading.end();
  627. });
  628. };
  629. ration_glj.prototype.getGLJDataByCodes = function (codes,callback) {
  630. if(!gljUtil.isDef(codes)||codes.length ==0) {
  631. if(callback) callback([]);
  632. return
  633. }
  634. $.bootstrapLoading.start();
  635. CommonAjax.post("/rationGlj/getGLJDataByCodes", { 'engineerID':projectObj.project.projectInfo.property.engineering_id,projectID:projectObj.project.ID(),codes:codes}, function (result) {
  636. $.bootstrapLoading.end();
  637. callback(result)
  638. });
  639. };
  640. ration_glj.prototype.getAddDataByStd = function (glj,rationID,billsItemID,projectID) {//生成添加类型的定额工料机数据
  641. let ration_glj = {
  642. projectID: projectID,
  643. GLJID: glj.ID,
  644. rationID: rationID,
  645. billsItemID: billsItemID,
  646. rationItemQuantity: 0,
  647. quantity: 0,
  648. name: glj.name,
  649. code: glj.code,
  650. original_code: glj.code,
  651. unit: glj.unit,
  652. specs: glj.specs,
  653. basePrice: glj.basePrice,
  654. marketPrice:glj.basePrice,
  655. shortName: glj.shortName,
  656. type: glj.gljType,
  657. model:glj.model,
  658. adjCoe: glj.adjCoe,
  659. createType: 'add',
  660. materialType:glj.materialType,
  661. materialCoe:glj.materialCoe,
  662. grossWeightCoe:glj.grossWeightCoe,
  663. purchaseStorageRate:glj.purchaseStorageRate,
  664. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  665. handlingLossRate:glj.handlingLossRate,
  666. repositoryId: glj.repositoryId
  667. };
  668. if (glj.hasOwnProperty("compilationId")) {
  669. ration_glj.from = "cpt";
  670. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  671. ration_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  672. }
  673. }
  674. return ration_glj;
  675. };
  676. ration_glj.prototype.addGLJByLib = function (GLJSelection, ration, callback) {
  677. let me=this,gljList = [];
  678. let allGLJ = gljOprObj.AllRecode;
  679. GLJSelection.sort();
  680. _.forEach(GLJSelection, function (g) {
  681. let glj = _.find(allGLJ, function (item) {
  682. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  683. return i_key == g;
  684. });
  685. let ration_glj = me.getAddDataByStd(glj,ration.ID,ration.billsItemID,ration.projectID);
  686. gljList.push(ration_glj);
  687. });
  688. $.bootstrapLoading.start();
  689. CommonAjax.post("/rationGlj/addGLJ", gljList, callback, function () {
  690. $.bootstrapLoading.end();
  691. });
  692. };
  693. ration_glj.prototype.updateRationGLJByChangeCode = function (recode, updateField, newval) {
  694. let me = this;
  695. let priceMap = projectObj.project.projectGLJ.datas.unitPriceMap;
  696. let priceObj = priceMap[newval];
  697. if(priceObj){
  698. if (recode.createType===undefined || recode.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  699. recode.rcode = recode.code;
  700. recode.createType = 'replace';
  701. }
  702. recode.code = priceObj.code;
  703. recode.name = priceObj.name;
  704. recode.type = priceObj.type;
  705. recode.unit = priceObj.unit;
  706. recode.shortName = priceObj.short_name;
  707. recode.specs = priceObj.specs;
  708. recode.GLJID=priceObj.glj_id;
  709. recode.original_code = priceObj.original_code;
  710. recode.basePrice = priceObj.base_price;
  711. recode.marketPrice = priceObj.market_price;
  712. }
  713. $.bootstrapLoading.start();
  714. CommonAjax.post("/rationGlj/replaceGLJ", recode, function (result) {
  715. if (result) {
  716. me.setDatasAfterReplace(result);
  717. let data = result.data;
  718. let rationNode = projectObj.project.mainTree.findNode(data.rationID);
  719. let nodes = [rationNode];
  720. project.projectGLJ.loadData(function () {//加载完项目工料机再计算
  721. gljOprObj.refreshView();
  722. if (project.ration_glj.needShowToTree(data)) {//当替换的是主材或设备时,刷新对应的树节点
  723. let node = project.ration_glj.findGLJNodeByID(data.ID);
  724. if (node) {
  725. project.ration_glj.transferToNodeData(data);
  726. node.source = data;
  727. node.data = data;
  728. }
  729. node ? nodes.push(node) : "";
  730. }
  731. rationNode.data.adjustState = result.adjustState;
  732. rationNode.data.name = result.name;
  733. projectObj.mainController.refreshTreeNode(nodes);
  734. project.calcProgram.calcAndSave(rationNode);
  735. $.bootstrapLoading.end();
  736. });
  737. }
  738. }, function () {
  739. $.bootstrapLoading.end();
  740. });
  741. };
  742. ration_glj.prototype.setDatasAfterReplace = function(result){
  743. let me = this;
  744. me.addAndDeleteDatas(result.newRecodes,result.deleteList);
  745. let list_index = _.findIndex(me.datas, {'ID': result.data.ID});
  746. if(result.data) me.datas[list_index] = result.data;
  747. me.refreshQuantityAfterCalResult(result.glj_result);
  748. };
  749. ration_glj.prototype.addAndDeleteDatas = function (newRecodes,deleteList) {
  750. let oldData = [];
  751. if(newRecodes.length > 0 ) this.datas = this.datas.concat(newRecodes);
  752. if(deleteList.length > 0) {
  753. oldData = _.remove(this.datas,function (item) {
  754. return deleteList.indexOf(item.ID)!=-1;
  755. });
  756. }
  757. return oldData;
  758. };
  759. ration_glj.prototype.getReplaceDataByStd = function (oldData,glj) {
  760. if (oldData.createType===undefined || oldData.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  761. oldData.rcode = oldData.code;
  762. oldData.createType = 'replace';
  763. oldData.mIndexObj ={code:oldData.code,name:oldData.name,type:oldData.type,unit:oldData.unit,specs:oldData.specs};
  764. }else if(oldData.createType == 'replace' && oldData.rcode == glj.code){//如果原数据已经是替换过的,这次替换又恢复成原数据,则把类型改回来
  765. oldData.createType = 'normal';
  766. oldData.mIndexObj = null;
  767. }
  768. oldData.GLJID = glj.ID;
  769. oldData.name = glj.name;
  770. oldData.type = glj.gljType;
  771. oldData.code = glj.code;
  772. oldData.original_code = glj.code;
  773. oldData.unit = glj.unit;
  774. oldData.specs = glj.specs;
  775. oldData.model = glj.model;
  776. oldData.basePrice = glj.basePrice;
  777. oldData.shortName=glj.shortName;
  778. oldData.marketPrice = glj.basePrice;
  779. oldData.repositoryId = glj.repositoryId;
  780. oldData.materialType = glj.materialType;
  781. oldData.materialCoe = glj.materialCoe;
  782. oldData.grossWeightCoe= glj.grossWeightCoe;
  783. oldData.purchaseStorageRate = glj.purchaseStorageRate;
  784. oldData.offSiteTransportLossRate = glj.offSiteTransportLossRate;
  785. oldData.handlingLossRate = glj.handlingLossRate;
  786. if (glj.hasOwnProperty("compilationId")) {
  787. oldData.from = "cpt";
  788. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  789. oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  790. }
  791. } else {
  792. oldData.from = "std";
  793. }
  794. return oldData;
  795. };
  796. ration_glj.prototype.replaceGLJ = function (selectCode, oldData, callback) {
  797. let allGLJ = gljOprObj.AllRecode;
  798. let glj = _.find(allGLJ, function (item) {
  799. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  800. return i_key == selectCode;
  801. });
  802. if (selectCode == gljOprObj.getIndex(oldData, gljKeyArray)) {
  803. return callback(null);
  804. }
  805. //如果是从混凝土改成商品混凝土,并且混凝土的定额消耗量不为空,则原混凝土的自定义消耗改成0,插入一条新的商品混凝土自定义消耗量为原定额计算后的消耗量
  806. if(gljUtil.isAddCommercialForReplace(oldData.type,glj.gljType,oldData.rationItemQuantity)){
  807. let originalID = oldData.ID;
  808. let customQuantity = gljUtil.isDef(oldData.quantity)? oldData.quantity:0;
  809. oldData = this.getAddDataByStd(glj,oldData.rationID,oldData.billsItemID,oldData.projectID);
  810. oldData.toCommercial=true;
  811. oldData.originalID = originalID;
  812. oldData.customQuantity = customQuantity;
  813. }else {
  814. oldData = this.getReplaceDataByStd(oldData,glj);
  815. }
  816. $.bootstrapLoading.start();
  817. CommonAjax.post("/rationGlj/replaceGLJ", oldData, callback, function () {
  818. $.bootstrapLoading.end();
  819. });
  820. };
  821. ration_glj.prototype.mReplaceGLJ = function (selectCode, oldData, callback) {
  822. let allGLJ = gljOprObj.AllRecode,tasks = [],updateMap={},doc = null;
  823. let oldIndex = gljOprObj.getIndex(oldData, gljKeyArray);
  824. let glj = _.find(allGLJ, function (item) {
  825. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  826. return i_key == selectCode;
  827. });
  828. if (selectCode == oldIndex) {
  829. return callback(null);
  830. }
  831. let query = {
  832. projectID: oldData.projectID,
  833. code: oldData.code,
  834. name: oldData.name,
  835. unit: oldData.unit,
  836. type: oldData.type
  837. };
  838. if (oldData.specs && oldData.specs != '') {
  839. query.specs = oldData.specs;
  840. }
  841. for(let d of this.datas){//查询出所有需替换的工料机
  842. if(!gljOprObj.scopeSelectedIDMap[d.billsItemID]) continue; //如果不在选中范围的,跳过
  843. let tem_index = gljOprObj.getIndex(d, gljKeyArray);
  844. if(tem_index == oldIndex){
  845. let tem_doc = {};
  846. //是否从混凝土改成商品混凝土,并且混凝土的定额消耗量不为空
  847. if(gljUtil.isAddCommercialForReplace(d.type,glj.gljType,d.rationItemQuantity)){
  848. tem_doc = {'customQuantity':'0'};
  849. //插入一条新的定额,这里的更新自定义消耗量操作由task完成了,不用像单条替换那样做标记,修改等
  850. let newDoc = this.getAddDataByStd(glj,d.rationID,d.billsItemID,d.projectID);
  851. let customQuantity = gljUtil.isDef(d.quantity)? d.quantity:0;
  852. newDoc.customQuantity = customQuantity;
  853. let tTask = {insertOne:{document: newDoc}};
  854. tasks.push(tTask);
  855. doc=newDoc//后台操作: 要根据nweDoc,插入项目工料机,修改insertOne里项目工料机ID,生成新的自身ID
  856. }else {
  857. tem_doc = this.getReplaceDataByStd(d,glj);
  858. if(gljUtil.isConcreteType(d.type)||gljUtil.isCommercialConcreteType(d.type)) tem_doc.isConcrete = true;//这里是在批量替换中有可能同时存在定额下的替换和添加的替换,对于添加的替换,要走替换混凝土的逻辑,定额下的替换又走另外的逻辑
  859. doc = tem_doc;
  860. }
  861. let task = {
  862. updateOne:{
  863. filter : {ID:d.ID,rationID:d.rationID},
  864. update : tem_doc
  865. }
  866. };
  867. tasks.push(task);
  868. updateMap[d.ID] = tem_doc;
  869. }
  870. }
  871. $.bootstrapLoading.start();
  872. CommonAjax.post("/rationGlj/mReplaceGLJ", {query: query, doc: doc,tasks:tasks}, function (result) {
  873. callback(result,updateMap);
  874. }, function () {
  875. $.bootstrapLoading.end();
  876. });
  877. };
  878. ration_glj.prototype.getMainAndEquGLJ = function (rationID) {
  879. let gljList = _.filter(this.datas, function (n) {
  880. return n.rationID == rationID && (n.type == gljType.MAIN_MATERIAL || n.type == gljType.EQUIPMENT)
  881. });
  882. gljList = gljUtil.sortRationGLJ(gljList);
  883. return gljOprObj.combineWithProjectGlj(gljList);
  884. };
  885. ration_glj.prototype.transferToNodeData = function (data) {
  886. data.contain=scMathUtil.roundForObj(data.quantity,getDecimal('glj.quantity'));
  887. data.subType = data.type;
  888. };
  889. ration_glj.prototype.combineRationAndGLJ = function (ration) {
  890. if (ration) {
  891. let projectGLJData = projectObj.project.projectGLJ.datas;
  892. let projectGljs = projectGLJData.gljList;
  893. let mixRatioMap = projectGLJData.mixRatioMap;
  894. let glj = _.find(projectGljs, {'id': ration.projectGLJID});
  895. if (glj) {
  896. if(projectObj.project.projectGLJ.isEstimateType(glj.type)){
  897. ration.isEstimate = glj.is_evaluate;
  898. }
  899. ration = gljOprObj.setGLJPrice(ration,glj);
  900. ration.isAdd = glj.unit_price.is_add;
  901. let connect_index = gljOprObj.getIndex(glj, gljKeyArray);
  902. if (mixRatioMap.hasOwnProperty(connect_index)) {
  903. let mixRatios = gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
  904. ration.subList = mixRatios;
  905. }
  906. }
  907. }
  908. return ration;
  909. };
  910. ration_glj.prototype.updateFromMainSpread = function (value, node, fieldName) {
  911. if (node.data[fieldName] !== value) {
  912. if (fieldName == "marketUnitFee") {
  913. let decimal = getDecimal("glj.unitPrice");
  914. let newval = number_util.checkNumberValue(value, decimal);
  915. if (newval) {
  916. fieldName = "marketPrice";
  917. projectObj.project.projectGLJ.updatePriceFromRG(node.data, fieldName, newval);
  918. return;
  919. }
  920. } else {
  921. if(fieldName == "contain"){
  922. if(value==null){
  923. value="";
  924. }else {
  925. let decimal = getDecimal("glj.quantity");
  926. value = number_util.checkNumberValue(value, decimal);
  927. fieldName="customQuantity";//填入自定义消耗
  928. }
  929. }
  930. if (value !== undefined && value !== null) {
  931. if (fieldName == "subType") {
  932. node.data.subType = value;
  933. fieldName = "type";//转换成更新工料机类型
  934. }
  935. this.updateRationGLJByEdit(node.data, fieldName, value, node);
  936. return;
  937. }
  938. }
  939. }
  940. // node.data.subType = value;
  941. projectObj.mainController.refreshTreeNode([node]);
  942. };
  943. ration_glj.prototype.refreshTreeNodeIfNeeded = function (data) {
  944. if (this.needShowToTree(data)) {
  945. this.transferToNodeData(data);
  946. gljOprObj.refreshTreeNode({"type": ModuleNames.ration_glj, "ID": data.ID});
  947. }
  948. };
  949. ration_glj.prototype.needShowToTree = function (data) {
  950. if ((data.type == gljType.MAIN_MATERIAL || data.type == gljType.EQUIPMENT) && projectObj.project.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  951. return true
  952. }
  953. return false
  954. };
  955. ration_glj.prototype.findGLJNodeByID = function (ID) {
  956. return projectObj.project.mainTree.findNode(ID);
  957. };
  958. ration_glj.prototype.findRationNodeByID = function (ID) {
  959. return projectObj.project.mainTree.findNode(ID);
  960. };
  961. ration_glj.prototype.reCalcWhenGLJChange = function (ration_glj) {//当改变定额工料机时,重新计算定额以及其父节点
  962. let node = this.findRationNodeByID(ration_glj.rationID);
  963. if (node) {
  964. node.changed = true;
  965. project.calcProgram.calcAndSave(node);
  966. }
  967. };
  968. ration_glj.prototype.reCalcWhenVvTaxChange = function () {
  969. let rationIDs = [];
  970. for(let g of this.datas){
  971. if(g.type == gljType.GENERAL_MACHINE) rationIDs.push(g.rationID);
  972. }
  973. this.calcRationsByIDs(_.uniq(rationIDs))
  974. };
  975. ration_glj.prototype.calcRationsByIDs = function (rationIDs) {
  976. let nodes = [];
  977. for(let ID of rationIDs){
  978. let node = projectObj.project.mainTree.findNode(ID);
  979. if(node) nodes.push(node)
  980. }
  981. project.calcProgram.calcNodesAndSave(nodes);
  982. };
  983. return new ration_glj(project);
  984. }
  985. };