ration_glj.js 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 (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 = projectInfoObj.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 = projectInfoObj.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':projectInfoObj.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. 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. }
  784. if(oldData.createType == 'replace' && oldData.rcode == glj.code){//如果原数据已经是替换过的,这次替换又恢复成原数据,则把类型改回来
  785. oldData.createType = 'normal'
  786. }
  787. oldData.GLJID = glj.ID;
  788. oldData.name = glj.name;
  789. oldData.code = glj.code;
  790. oldData.original_code = glj.code;
  791. oldData.unit = glj.unit;
  792. oldData.specs = glj.specs;
  793. oldData.model = glj.model;
  794. oldData.basePrice = glj.basePrice;
  795. oldData.marketPrice = glj.basePrice;
  796. oldData.repositoryId = glj.repositoryId;
  797. oldData.materialType = glj.materialType;
  798. oldData. materialCoe = glj.materialCoe;
  799. oldData.grossWeightCoe= glj.grossWeightCoe;
  800. oldData.purchaseStorageRate = glj.purchaseStorageRate;
  801. oldData.offSiteTransportLossRate = glj.offSiteTransportLossRate;
  802. oldData.handlingLossRate = glj.handlingLossRate;
  803. if (glj.hasOwnProperty("compilationId")) {
  804. oldData.from = "cpt";
  805. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  806. oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  807. }
  808. } else {
  809. oldData.from = "std";
  810. }
  811. return oldData;
  812. };
  813. ration_glj.prototype.replaceGLJ = function (selectCode, oldData, callback) {
  814. let allGLJ = gljOprObj.AllRecode;
  815. let glj = _.find(allGLJ, function (item) {
  816. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  817. return i_key == selectCode;
  818. });
  819. if (selectCode == gljOprObj.getIndex(oldData, gljKeyArray)) {
  820. return callback(null);
  821. }
  822. oldData = this.getReplaceDataByStd(oldData,glj);
  823. $.bootstrapLoading.start();
  824. CommonAjax.post("/rationGlj/replaceGLJ", oldData, callback, function () {
  825. $.bootstrapLoading.end();
  826. });
  827. };
  828. ration_glj.prototype.mReplaceGLJ = function (selectCode, oldData, callback) {
  829. let allGLJ = gljOprObj.AllRecode,tasks = [],updateMap={};
  830. let oldIndex = gljOprObj.getIndex(oldData, gljKeyArray);
  831. let glj = _.find(allGLJ, function (item) {
  832. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  833. return i_key == selectCode;
  834. });
  835. if (selectCode == oldIndex) {
  836. return callback(null);
  837. }
  838. let query = {
  839. projectID: oldData.projectID,
  840. code: oldData.code,
  841. name: oldData.name,
  842. unit: oldData.unit,
  843. type: oldData.type
  844. }
  845. if (oldData.specs && oldData.specs != '') {
  846. query.specs = oldData.specs;
  847. }
  848. let doc = {
  849. GLJID: glj.ID,
  850. createType: 'replace',
  851. rationItemQuantity: 0,
  852. name: glj.name,
  853. code: glj.code,
  854. original_code: glj.code,
  855. unit: glj.unit,
  856. specs: glj.specs,
  857. type: glj.gljType,
  858. model:glj.model,
  859. basePrice: glj.basePrice,
  860. marketPrice:glj.basePrice,
  861. repositoryId: glj.repositoryId,
  862. materialType: glj.materialType, //三材类别
  863. materialCoe: glj.materialCoe,
  864. grossWeightCoe:glj.grossWeightCoe,
  865. purchaseStorageRate:glj.purchaseStorageRate,
  866. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  867. handlingLossRate:glj.handlingLossRate,
  868. projectID: oldData.projectID
  869. };
  870. if (glj.hasOwnProperty("compilationId")) {
  871. doc.from = "cpt";
  872. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  873. doc.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  874. }
  875. } else {
  876. doc.from = "std";
  877. }
  878. for(let d of this.datas){//查询出所有需替换的工料机
  879. let tem_index = gljOprObj.getIndex(d, gljKeyArray);
  880. if(tem_index == oldIndex){
  881. let tem_doc = _.cloneDeep(doc);
  882. if(d.createType == 'replace'){
  883. tem_doc.rcode = d.rcode;
  884. }else if(d.createType == 'add'){//对于添加的类型,替换后还是添加类型
  885. tem_doc.createType = 'add';
  886. }else {
  887. tem_doc.rcode = d.code
  888. }
  889. let task = {
  890. updateOne:{
  891. filter : {ID:d.ID,rationID:d.rationID},
  892. update : tem_doc
  893. }
  894. };
  895. tasks.push(task);
  896. updateMap[d.ID] = tem_doc;
  897. }
  898. }
  899. $.bootstrapLoading.start();
  900. CommonAjax.post("/rationGlj/mReplaceGLJ", {query: query, doc: doc,tasks:tasks}, function (result) {
  901. callback(result,updateMap);
  902. }, function () {
  903. $.bootstrapLoading.end();
  904. });
  905. };
  906. ration_glj.prototype.getMainAndEquGLJ = function (rationID) {
  907. let gljList = _.filter(this.datas, function (n) {
  908. return n.rationID == rationID && (n.type == gljType.MAIN_MATERIAL || n.type == gljType.EQUIPMENT)
  909. });
  910. gljList = gljUtil.sortRationGLJ(gljList);
  911. return gljOprObj.combineWithProjectGlj(gljList);
  912. };
  913. ration_glj.prototype.transferToNodeData = function (data) {
  914. data.contain=scMathUtil.roundForObj(data.quantity,getDecimal('glj.quantity'));
  915. data.subType = data.type;
  916. };
  917. ration_glj.prototype.combineRationAndGLJ = function (ration) {
  918. if (ration) {
  919. let projectGLJData = projectObj.project.projectGLJ.datas;
  920. let projectGljs = projectGLJData.gljList;
  921. let mixRatioMap = projectGLJData.mixRatioMap;
  922. let glj = _.find(projectGljs, {'id': ration.projectGLJID});
  923. if (glj) {
  924. if(projectObj.project.projectGLJ.isEstimateType(glj.type)){
  925. ration.isEstimate = glj.is_evaluate;
  926. }
  927. ration = gljOprObj.setGLJPrice(ration,glj);
  928. ration.isAdd = glj.unit_price.is_add;
  929. let connect_index = gljOprObj.getIndex(glj, gljKeyArray);
  930. if (mixRatioMap.hasOwnProperty(connect_index)) {
  931. let mixRatios = gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
  932. ration.subList = mixRatios;
  933. }
  934. }
  935. }
  936. return ration;
  937. };
  938. ration_glj.prototype.updateFromMainSpread = function (value, node, fieldName) {
  939. if (node.data[fieldName] !== value) {
  940. if (fieldName == "marketUnitFee") {
  941. let decimal = getDecimal("glj.unitPrice");
  942. let newval = number_util.checkNumberValue(value, decimal);
  943. if (newval) {
  944. fieldName = "marketPrice";
  945. projectObj.project.projectGLJ.updatePriceFromRG(node.data, fieldName, newval);
  946. return;
  947. }
  948. } else {
  949. if(fieldName == "contain"){
  950. if(value==null){
  951. value="";
  952. }else {
  953. let decimal = getDecimal("glj.quantity");
  954. value = number_util.checkNumberValue(value, decimal);
  955. fieldName="customQuantity";//填入自定义消耗
  956. }
  957. }
  958. if (value !== undefined && value !== null) {
  959. if (fieldName == "subType") {
  960. node.data.subType = value;
  961. fieldName = "type";//转换成更新工料机类型
  962. }
  963. this.updateRationGLJByEdit(node.data, fieldName, value, node);
  964. return;
  965. }
  966. }
  967. }
  968. // node.data.subType = value;
  969. projectObj.mainController.refreshTreeNode([node]);
  970. };
  971. ration_glj.prototype.refreshTreeNodeIfNeeded = function (data) {
  972. if (this.needShowToTree(data)) {
  973. this.transferToNodeData(data);
  974. gljOprObj.refreshTreeNode({"type": ModuleNames.ration_glj, "ID": data.ID});
  975. }
  976. };
  977. ration_glj.prototype.needShowToTree = function (data) {
  978. if ((data.type == gljType.MAIN_MATERIAL || data.type == gljType.EQUIPMENT) && projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  979. return true
  980. }
  981. return false
  982. };
  983. ration_glj.prototype.findGLJNodeByID = function (ID) {
  984. return projectObj.project.mainTree.findNode(ID);
  985. };
  986. ration_glj.prototype.findRationNodeByID = function (ID) {
  987. return projectObj.project.mainTree.findNode(ID);
  988. };
  989. ration_glj.prototype.reCalcWhenGLJChange = function (ration_glj) {//当改变定额工料机时,重新计算定额以及其父节点
  990. let node = this.findRationNodeByID(ration_glj.rationID);
  991. if (node) {
  992. node.changed = true;
  993. project.calcProgram.calcAndSave(node);
  994. }
  995. };
  996. return new ration_glj(project);
  997. }
  998. };