ration_glj.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var ration_glj = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. var tools = {};
  8. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  9. var ration_glj = function (proj) {
  10. this.gljTree = cacheTree.createNew(this);
  11. // this.project = proj;
  12. this.datas = [];
  13. var 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 (rationID) {
  41. let result = this.datas.filter(function (data) {
  42. return data.rationID === rationID;
  43. })
  44. result = gljOprObj.combineWithProjectGlj(result);
  45. return result;
  46. };
  47. ration_glj.prototype.getGatherGljArrByRations = function (rations) {
  48. let result = [];
  49. let clone = function (obj) {
  50. if (obj === null) return null;
  51. var o = Object.prototype.toString.apply(obj) === "[object Array]" ? [] : {};
  52. for (var i in obj) {
  53. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? clone(obj[i]) : obj[i]);
  54. }
  55. return o;
  56. }
  57. let findGlj = function (sourceGlj, gljArr) {
  58. for (let glj of gljArr) {
  59. if (glj.projectGLJID === sourceGlj.projectGLJID) {
  60. return glj;
  61. }
  62. }
  63. return null;
  64. }
  65. for (let ration of rations) {
  66. let rationGljs = this.getGljArrByRation(ration.ID);
  67. for (let glj of rationGljs) {
  68. let sameGlj = findGlj(glj, result);
  69. if (!sameGlj) {
  70. sameGlj = clone(glj);
  71. sameGlj.quantity = (sameGlj.quantity * ration.quantity).toDecimal(4);
  72. result.push(sameGlj);
  73. } else {
  74. sameGlj.quantity = sameGlj.quantity + (glj.quantity * ration.quantity).toDecimal(4);
  75. }
  76. }
  77. }
  78. ;
  79. result = gljOprObj.combineWithProjectGlj(result);
  80. return result;
  81. }
  82. // 提交数据后返回数据处理
  83. ration_glj.prototype.doAfterUpdate = function (err, data) {
  84. var me = this;
  85. if (!err) {
  86. if (data.updateTpye == 'ut_update') {
  87. me.refreshAfterUpdate(data);
  88. } else if (data.updateTpye == 'ut_delete') {
  89. me.refreshAfterDelete(data);
  90. } else {
  91. me.refreshAfterSave(data);
  92. }
  93. }
  94. projectObj.project.projectGLJ.loadData();
  95. };
  96. ration_glj.prototype.refreshAfterSave = function (data) {
  97. let me = projectObj.project.ration_glj;
  98. let neRecodes = [];
  99. if (data) {
  100. // neRecodes=data.newRecords;//原来是显示和缓存分开的,后来发现会导致数据不一致的问题所以改成统一的了,这里也只是会作为显示。
  101. neRecodes = data.showDatas;
  102. gljOprObj.sheetData = neRecodes;
  103. }
  104. if (me.datas && Array.isArray(me.datas)) {
  105. if (data) {
  106. me.datas = me.datas.concat(neRecodes);
  107. }
  108. } else {
  109. me.datas = neRecodes;
  110. }
  111. project.projectGLJ.loadData(function () {
  112. gljOprObj.showRationGLJSheetData(true);
  113. //add to mainTree;
  114. me.addToMainTree(neRecodes);
  115. let node = project.mainTree.selected;
  116. project.calcProgram.calcAndSave(node);
  117. if (activeSubSheetIs(subSheetIndex.ssiCalcProgram)) {
  118. calcProgramObj.showData(node, false);
  119. }
  120. });
  121. };
  122. ration_glj.prototype.addToMainTree = function (datas) {
  123. datas = sortRationGLJ(datas);
  124. for (let data of datas) {
  125. if (this.needShowToTree(data)) {
  126. this.transferToNodeData(data);
  127. let parentNode = _.find(projectObj.project.mainTree.items, function (n) {//找父节点
  128. return n.sourceType == ModuleNames.ration && n.data.ID == data.rationID;
  129. });
  130. if (parentNode) {
  131. let nextNodeID = null;
  132. if (parentNode.children.length > 0) {
  133. for (let br of parentNode.children) {
  134. if (compareRationGLJ(data, br.data)) {//如果有兄弟节点则找到添加位置。
  135. nextNodeID = br.getID();
  136. }
  137. }
  138. }
  139. nextNodeID = nextNodeID ? nextNodeID : parentNode.tree.rootID();
  140. let newNode = projectObj.project.mainTree.insert(parentNode.getID(), nextNodeID);
  141. newNode.source = data;
  142. newNode.sourceType = this.getSourceType();
  143. newNode.data = data;
  144. ProjectController.syncDisplayNewRationGljNode(projectObj.mainController, newNode);
  145. }
  146. }
  147. }
  148. };
  149. ration_glj.prototype.refreshAfterUpdate = function (data) {
  150. var me = this;
  151. var rationID=null;
  152. if (data.quantityRefresh) {
  153. data.glj_result.forEach(function (item) {
  154. rationID = me.refreshEachItme(item.doc, item.query);
  155. })
  156. } else {
  157. rationID = me.refreshEachItme(data.doc, data.query);
  158. }
  159. gljOprObj.showRationGLJSheetData(true);
  160. this.reCalcWhenGLJChange({rationID:rationID});
  161. };
  162. ration_glj.prototype.refreshEachItme = function (doc, query) {
  163. var glj_list = projectObj.project.ration_glj.datas;
  164. var glj_index = _.findIndex(glj_list, (glj) => {
  165. return glj.ID == query.ID;
  166. })
  167. /*var sheet_index= _.findIndex(gljOprObj.sheetData,(sd)=>{
  168. return sd.ID==query.ID;
  169. })*/
  170. _.forEach(doc, function (n, key) {
  171. glj_list[glj_index][key] = n;
  172. });
  173. return glj_list[glj_index].rationID;
  174. };
  175. ration_glj.prototype.refreshAfterDelete = function (data) {
  176. var me = projectObj.project.ration_glj;
  177. var glj_list = me.datas;
  178. var oldData = _.remove(glj_list, data.query);
  179. _.remove(gljOprObj.sheetData, data.query);
  180. gljOprObj.showRationGLJSheetData();
  181. projectObj.project.projectGLJ.loadData();
  182. var rationNode = null;
  183. var next = null;
  184. var selected = projectObj.project.mainTree.selected;
  185. if (selected.sourceType == ModuleNames.ration) { //如果选中的是定额,说明是右键删除工料机操作,如果选中的是定额工料机,则说明是在造价书主界面中点击了删除按钮
  186. rationNode = selected;
  187. } else if (selected.sourceType == ModuleNames.ration_glj) {
  188. rationNode = selected.parent;
  189. next = true;
  190. }
  191. rationNode.data.adjustState = data.adjustState;
  192. projectObj.mainController.refreshTreeNode([rationNode]);
  193. for (let o of oldData) {
  194. if (this.needShowToTree(o)) {
  195. let node = me.findGLJNodeByID(o.ID); //找到对应的树节点
  196. projectObj.mainController.deleteNode(node, next);
  197. }
  198. }
  199. };
  200. // CSL,2017.05.09
  201. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  202. this.project.beginUpdate('modifyQuantity');
  203. data.quantity = newQuantity;
  204. data.customQuantity = newQuantity;
  205. data.updateType = 'ut_update';
  206. this.project.push(this.getSourceType, data);
  207. this.project.endUpdate();
  208. };
  209. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  210. this.project.beginUpdate('modifyPrice');
  211. data.price = newPrice;
  212. data.updateType = 'ut_update';
  213. this.project.push(this.getSourceType, data);
  214. this.project.endUpdate();
  215. };
  216. ration_glj.prototype.deleteGLJ = function (data) {
  217. this.project.beginUpdate('deleteGLJ');
  218. data.customQuantity = 0;
  219. data.quantity = 0;
  220. data.rationItemQuantity = 0;
  221. data.updateType = 'ut_update';
  222. this.project.push(this.getSourceType, data);
  223. this.project.endUpdate();
  224. };
  225. ration_glj.prototype.addRationGLJ = function (newRation, data) {
  226. var souceTypeList = [];
  227. var criteriaDataList = [];
  228. if (data.hasOwnProperty('rationGljList') && data.rationGljList.length > 0) {
  229. let criteria = {};
  230. criteria.ration_glj_list = [];
  231. for (let i = 0; i < data.rationGljList.length; i++) {
  232. let temdata = data.rationGljList[i];
  233. let newGLJ = {};
  234. newGLJ.projectID = parseInt(newRation.projectID);
  235. newGLJ.GLJID = temdata.gljId;
  236. newGLJ.rationID = newRation.ID;
  237. newGLJ.billsItemID = newRation.billsItemID,
  238. newGLJ.rationItemQuantity = temdata.consumeAmt;
  239. newGLJ.quantity = temdata.consumeAmt;
  240. newGLJ.glj_repository_id = data.rationRepId;
  241. criteria.ration_glj_list.push(newGLJ);
  242. }
  243. criteria.updateType = 'ut_create';
  244. souceTypeList.push(this.getSourceType());
  245. criteriaDataList.push(criteria);
  246. }
  247. var ration_coe = projectObj.project.ration_coe;
  248. var rationCoeData = ration_coe.getRationCoedata(newRation, data);
  249. souceTypeList.push(ration_coe.getSourceType());
  250. criteriaDataList.push(rationCoeData);
  251. project.pushNow('addRationGLJAndRationCoe', souceTypeList, criteriaDataList);
  252. };
  253. ration_glj.prototype.getDeleteDataByRation = function (rationData) {
  254. var updateData = [];
  255. updateData.push({
  256. 'deleteType': 'RATION',
  257. 'updateType': 'ut_delete',
  258. 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}
  259. });
  260. return updateData;
  261. };
  262. ration_glj.prototype.getDeleteDataByBills = function (datas) {
  263. var updateData = [];
  264. datas.forEach(function (deleteData) {
  265. if (deleteData.type == 'delete') {
  266. var billData = deleteData.data;
  267. updateData.push({
  268. 'deleteType': 'BILL',
  269. 'updateType': 'ut_delete',
  270. 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}
  271. });
  272. }
  273. })
  274. return updateData;
  275. };
  276. ration_glj.prototype.deleteByRation = function (ration) {
  277. var glj_list = projectObj.project.ration_glj.datas;
  278. var newList = _.filter(glj_list, (glj) => {
  279. return glj.rationID != ration.ID;
  280. });
  281. if (newList != undefined) {
  282. projectObj.project.ration_glj.datas = newList;
  283. }
  284. };
  285. ration_glj.prototype.deleteByBills = function (deleteData) {
  286. var rationList = projectObj.project.Ration.datas;
  287. var deleteRationList = [];
  288. for (var i = 0; i < deleteData.length; i++) {
  289. if (deleteData[i].type == 'delete') {
  290. var billID = deleteData[i].data.ID;
  291. var raList = _.filter(rationList, (ration) => {
  292. return ration.billsItemID == billID;
  293. });
  294. deleteRationList = deleteRationList.concat(raList);
  295. }
  296. }
  297. for (var i = 0; i < deleteRationList.length; i++) {
  298. this.deleteByRation(deleteRationList[i]);
  299. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  300. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  301. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  302. }
  303. }
  304. ration_glj.prototype.updataOrdelete = function (row) {
  305. var updateData = null;
  306. if (row.rationItemQuantity == 0) {
  307. updateData = this.getUpdateData('ut_delete', {
  308. 'ID': row.ID,
  309. 'projectID': row.projectID
  310. }, {rationID: row.rationID});
  311. project.pushNow('updateRationGLJ', [this.getSourceType()], updateData)
  312. } else {
  313. this.updateRationGLJByEdit(row, 'customQuantity', 0);
  314. //('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  315. }
  316. };
  317. ration_glj.prototype.getUpdateData = function (type, query, doc, callfunction) {
  318. var updateData = [];
  319. var newobj = {
  320. 'updateType': type,
  321. 'query': query,
  322. }
  323. if (doc) {
  324. newobj['doc'] = doc;
  325. }
  326. if (callfunction) {
  327. newobj['updateFunction'] = callfunction;
  328. }
  329. updateData.push(newobj);
  330. return updateData;
  331. };
  332. ration_glj.prototype.updateRationGLJByEdit = function (recode, updateField, newval, node) {
  333. var me = this;
  334. $.bootstrapLoading.start();
  335. var callback = function (data) {
  336. let initShow = false;//是否需要表格初始化显示
  337. if (updateField == 'customQuantity') {
  338. me.refreshAfterQuantityUpdate(data, node);
  339. } else {
  340. var doc = data.doc;
  341. for (var key in doc) {
  342. recode[key] = doc[key];
  343. }
  344. if (data.hasOwnProperty('adjustState')) {//更新定额调整状态
  345. me.updateRationAdjustState(data.adjustState, recode.rationID, node);
  346. }
  347. if (recode.subList && recode.subList.length > 0) {
  348. initShow = true;
  349. }
  350. if (node) {//如果不是在造价书页面直接编辑,则不用刷新
  351. if (updateField == "type" && !(newval == gljType.MAIN_MATERIAL || newval == gljType.EQUIPMENT)) {//如果改变类型后不是主材或设备,则在造价书树中移除
  352. projectObj.mainController.deleteNode(node, true);
  353. }
  354. }
  355. }
  356. me.refreshTreeNodeIfNeeded(recode);//刷新造价书上的树节点(如果需要)
  357. if (initShow == false) {//不需要初始化,只需耍新当前显示就可以了
  358. gljOprObj.showRationGLJSheetData();
  359. }
  360. projectObj.project.projectGLJ.loadData(function () {//等项目工料机加载完成后再给用户编辑
  361. me.reCalcWhenGLJChange(recode);//触发计算定额以及父节点
  362. if (initShow == true) {
  363. gljOprObj.refreshView();
  364. }
  365. $.bootstrapLoading.end();
  366. });
  367. }
  368. var query = {
  369. 'ID': recode.ID,
  370. 'projectID': recode.projectID,
  371. 'rationID': recode.rationID
  372. };
  373. var priceInfo = {
  374. base_price: recode.basePrice,
  375. market_price: recode.marketPrice
  376. }
  377. var doc = {};
  378. doc[updateField] = newval;
  379. if (updateField == "type") {
  380. doc.shortName = projectObj.project.projectGLJ.getShortNameByID[newval];
  381. }
  382. CommonAjax.post("/rationGlj/updateRationGLJByEdit", {
  383. query: query,
  384. doc: doc,
  385. priceInfo: priceInfo
  386. }, callback, function (err) {
  387. $.bootstrapLoading.end();
  388. });
  389. }
  390. ration_glj.prototype.refreshAfterQuantityUpdate = function (data, node) {
  391. var me = this;
  392. data.glj_result.forEach(function (item) {
  393. me.refreshEachItme(item.doc, item.query);
  394. })
  395. me.updateRationAdjustState(data.adjustState, data.rationID, node);
  396. };
  397. ration_glj.prototype.updateRationAdjustState = function (adjustState, rationID, rnode) {
  398. var nodes = [];
  399. var node = _.find(projectObj.project.mainTree.items, function (n) {
  400. return n.sourceType == ModuleNames.ration && n.data.ID == rationID;
  401. })
  402. if (node) {
  403. node.data.adjustState = adjustState;
  404. nodes.push(node);
  405. }
  406. if (rnode) {
  407. nodes.push(rnode);
  408. }
  409. projectObj.mainController.refreshTreeNode(nodes);
  410. };
  411. ration_glj.prototype.getGLJData = function (cb) {
  412. CommonAjax.get('/rationGlj/getGLJData', function (data) {
  413. cb(data);
  414. })
  415. };
  416. ration_glj.prototype.insertGLJAsRation = function (GLJSelection, selected, callback) {
  417. let gljList = [];
  418. let allGLJ = gljOprObj.AllRecode;
  419. let billsItemID = null;
  420. let serialNo = 0;
  421. let selectedSerialNo = null;
  422. let nextNodeID = null;
  423. let parentNodeID = null;
  424. let children = [];
  425. if (selected.sourceType === project.Bills.getSourceType()) {
  426. billsItemID = selected.data.ID;
  427. parentNodeID = selected.getID();
  428. nextNodeID = selected.tree.rootID();
  429. } else {
  430. billsItemID = selected.data.billsItemID;
  431. serialNo = selected.data.serialNo;
  432. selectedSerialNo = selected.data.serialNo;
  433. nextNodeID = selected.getNextSiblingID();
  434. parentNodeID = selected.getParentID();
  435. }
  436. children = project.Ration.getBillsSortRation(billsItemID);
  437. serialNo == 0 ? serialNo = children.length : "";
  438. for (let con_key of GLJSelection) {
  439. var glj = _.find(allGLJ, function (item) {
  440. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  441. return i_key == con_key;
  442. });
  443. if (glj) {
  444. serialNo += 1;
  445. let new_glj = {
  446. ID: project.Ration.getNewRationID(),
  447. projectID: parseInt(project.ID()),
  448. billsItemID: billsItemID,
  449. type: rationType.gljRation,
  450. code: glj.code,
  451. name: glj.name,
  452. quantity: 0,
  453. unit: glj.unit,
  454. specs: glj.specs,
  455. subType: glj.gljType,
  456. basePrice: glj.basePrice,
  457. original_code: glj.code,
  458. shortName: glj.shortName,
  459. serialNo: serialNo,
  460. GLJID: glj.ID,
  461. adjCoe: glj.adjCoe,
  462. repositoryId: glj.repositoryId
  463. }
  464. if (glj.hasOwnProperty("compilationId")) {
  465. new_glj.from = "cpt";
  466. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  467. new_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  468. }
  469. }
  470. gljList.push(new_glj);
  471. }
  472. }
  473. if (gljList.length == 0) {
  474. return;
  475. }
  476. let postData = {
  477. gljList: gljList,
  478. projectID: parseInt(project.ID()),
  479. billsItemID: billsItemID,
  480. rationCount: project.Ration.maxRationID()
  481. }
  482. selectedSerialNo == null ? "" : postData.selectedSerialNo = selectedSerialNo;
  483. $.bootstrapLoading.start();
  484. CommonAjax.post("/ration/insertGLJAsRation", postData, function (data) {
  485. // 更新兄弟节点的序列号
  486. if (selectedSerialNo != null) {
  487. let selectIndex = _.findIndex(children, {"serialNo": selectedSerialNo});
  488. if (selectIndex + 1 < children.length) {
  489. for (let i = selectIndex + 1; i < children.length; i++) {
  490. children[i].serialNo += gljList.length;
  491. }
  492. }
  493. }
  494. let newNode=null;
  495. for (let r_glj of data) {
  496. r_glj.marketUnitFee = r_glj.marketPrice;
  497. r_glj.quantity = r_glj.quantity + "";
  498. project.Ration.datas.push(r_glj);
  499. newNode = project.mainTree.insert(parentNodeID, nextNodeID, r_glj.ID);
  500. newNode.source = r_glj;
  501. newNode.sourceType = project.Ration.getSourceType();
  502. newNode.data = r_glj;
  503. ProjectController.syncDisplayNewNode(projectObj.mainController, newNode);
  504. }
  505. let parentNode = project.mainTree.nodes[project.mainTree.prefix + parentNodeID];
  506. project.calcProgram.calcLeafAndSave(parentNode);//计算父级清单的所有子节点
  507. //this.nodes[this.prefix + parentID];
  508. callback(newNode);
  509. $.bootstrapLoading.end();
  510. }, function () {
  511. $.bootstrapLoading.end();
  512. });
  513. };
  514. ration_glj.prototype.addGLJByLib = function (GLJSelection, ration, callback) {
  515. var gljList = [];
  516. var allGLJ = gljOprObj.AllRecode;
  517. GLJSelection.sort();
  518. _.forEach(GLJSelection, function (g) {
  519. var glj = _.find(allGLJ, function (item) {
  520. var i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  521. return i_key == g;
  522. });
  523. var ration_glj = {
  524. projectID: ration.projectID,
  525. GLJID: glj.ID,
  526. rationID: ration.ID,
  527. billsItemID: ration.billsItemID,
  528. rationItemQuantity: 0,
  529. quantity: 0,
  530. name: glj.name,
  531. code: glj.code,
  532. original_code: glj.code,
  533. unit: glj.unit,
  534. specs: glj.specs,
  535. basePrice: glj.basePrice,
  536. shortName: glj.shortName,
  537. type: glj.gljType,
  538. adjCoe: glj.adjCoe,
  539. createType: 'add',
  540. repositoryId: glj.repositoryId
  541. }
  542. if (glj.hasOwnProperty("compilationId")) {
  543. ration_glj.from = "cpt";
  544. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  545. ration_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  546. }
  547. }
  548. gljList.push(ration_glj);
  549. });
  550. $.bootstrapLoading.start();
  551. CommonAjax.post("/rationGlj/addGLJ", gljList, callback, function () {
  552. $.bootstrapLoading.end();
  553. });
  554. };
  555. ration_glj.prototype.replaceGLJ = function (selectCode, oldData, callback) {
  556. var allGLJ = gljOprObj.AllRecode;
  557. var glj = _.find(allGLJ, function (item) {
  558. var i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  559. return i_key == selectCode;
  560. });
  561. if (selectCode == gljOprObj.getIndex(oldData, gljKeyArray)) {
  562. return callback(null);
  563. }
  564. if (oldData.createType != 'replace') {
  565. oldData.rcode = oldData.code;
  566. oldData.createType = 'replace';
  567. }
  568. oldData.GLJID = glj.ID;
  569. oldData.name = glj.name;
  570. oldData.code = glj.code;
  571. oldData.original_code = glj.code;
  572. oldData.unit = glj.unit;
  573. oldData.specs = glj.specs;
  574. oldData.basePrice = glj.basePrice;
  575. oldData.repositoryId = glj.repositoryId;
  576. if (glj.hasOwnProperty("compilationId")) {
  577. oldData.from = "cpt";
  578. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  579. oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  580. }
  581. } else {
  582. oldData.from = "std";
  583. }
  584. $.bootstrapLoading.start();
  585. CommonAjax.post("/rationGlj/replaceGLJ", oldData, callback, function () {
  586. $.bootstrapLoading.end();
  587. });
  588. };
  589. ration_glj.prototype.mReplaceGLJ = function (selectCode, oldData, callback) {
  590. var allGLJ = gljOprObj.AllRecode;
  591. var glj = _.find(allGLJ, function (item) {
  592. var i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  593. return i_key == selectCode;
  594. });
  595. if (selectCode == gljOprObj.getIndex(oldData, gljKeyArray)) {
  596. return callback(null);
  597. }
  598. var query = {
  599. projectID: oldData.projectID,
  600. code: oldData.code,
  601. name: oldData.name,
  602. unit: oldData.unit,
  603. type: oldData.type
  604. }
  605. if (oldData.specs && oldData.specs != '') {
  606. query.specs = oldData.specs;
  607. }
  608. var doc = {
  609. GLJID: glj.ID,
  610. createType: 'replace',
  611. rationItemQuantity: 0,
  612. name: glj.name,
  613. code: glj.code,
  614. original_code: glj.code,
  615. unit: glj.unit,
  616. specs: glj.specs,
  617. type: glj.gljType,
  618. basePrice: glj.basePrice,
  619. repositoryId: glj.repositoryId,
  620. projectID: oldData.projectID
  621. }
  622. if (oldData.createType == 'replace') {
  623. doc.rcode = oldData.rcode;
  624. } else {
  625. doc.rcode = oldData.code;
  626. }
  627. if (glj.hasOwnProperty("compilationId")) {
  628. doc.from = "cpt";
  629. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  630. doc.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  631. }
  632. } else {
  633. doc.from = "std";
  634. }
  635. $.bootstrapLoading.start();
  636. CommonAjax.post("/rationGlj/mReplaceGLJ", {query: query, doc: doc}, callback, function () {
  637. $.bootstrapLoading.end();
  638. });
  639. };
  640. ration_glj.prototype.getMainAndEquGLJ = function (rationID) {
  641. var gljList = _.filter(this.datas, function (n) {
  642. return n.rationID == rationID && (n.type == gljType.MAIN_MATERIAL || n.type == gljType.EQUIPMENT)
  643. });
  644. gljList = sortRationGLJ(gljList);
  645. return gljOprObj.combineWithProjectGlj(gljList);
  646. };
  647. ration_glj.prototype.transferToNodeData = function (data) {
  648. data.contain=scMathUtil.roundForObj(data.quantity,getDecimal('glj.quantity'));
  649. data.subType = data.type;
  650. data.marketUnitFee = data.marketPrice;
  651. };
  652. ration_glj.prototype.combineRationAndGLJ = function (ration) {
  653. if (ration) {
  654. var projectGLJData = projectObj.project.projectGLJ.datas;
  655. var projectGljs = projectGLJData.gljList;
  656. var mixRatioMap = projectGLJData.mixRatioMap;
  657. var glj = _.find(projectGljs, {'id': ration.projectGLJID});
  658. if (glj) {
  659. let typeString = glj.type + "";
  660. if (typeString.indexOf("2") != -1) {//只有材料类型才显示是否暂估
  661. ration.isEstimate = glj.is_evaluate;
  662. }
  663. ration.basePrice = glj.unit_price.base_price;
  664. ration.marketUnitFee = glj.unit_price.market_price;
  665. ration.isAdd = glj.unit_price.is_add;
  666. var connect_index = gljOprObj.getIndex(glj, gljKeyArray);
  667. if (mixRatioMap.hasOwnProperty(connect_index)) {
  668. var mixRatios = gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
  669. ration.subList = mixRatios;
  670. }
  671. }
  672. }
  673. return ration;
  674. };
  675. ration_glj.prototype.updateFromMainSpread = function (value, node, fieldName) {
  676. if (node.data[fieldName] === value) {
  677. return;
  678. }
  679. if (fieldName == "marketUnitFee") {
  680. var decimal = getDecimal("glj.unitPrice");
  681. var newval = number_util.checkNumberValue(value, decimal);
  682. if (newval) {
  683. fieldName = "marketPrice";
  684. projectObj.project.projectGLJ.updatePriceFromRG(node.data, fieldName, newval);
  685. return;
  686. }
  687. } else {
  688. if(fieldName == "contain"){
  689. if(value==null){
  690. value="";
  691. }else {
  692. var decimal = getDecimal("glj.quantity");
  693. value = number_util.checkNumberValue(value, decimal);
  694. fieldName="customQuantity";//填入自定义消耗
  695. }
  696. }
  697. if (value !== undefined && value !== null) {
  698. if (fieldName == "subType") {
  699. node.data.subType = value;
  700. fieldName = "type";//转换成更新工料机类型
  701. }
  702. this.updateRationGLJByEdit(node.data, fieldName, value, node);
  703. return;
  704. }
  705. }
  706. // node.data.subType = value;
  707. projectObj.mainController.refreshTreeNode([node]);
  708. };
  709. ration_glj.prototype.refreshTreeNodeIfNeeded = function (data) {
  710. if (this.needShowToTree(data)) {
  711. this.transferToNodeData(data);
  712. gljOprObj.refreshTreeNode({"type": ModuleNames.ration_glj, "ID": data.ID});
  713. }
  714. };
  715. ration_glj.prototype.needShowToTree = function (data) {
  716. if ((data.type == gljType.MAIN_MATERIAL || data.type == gljType.EQUIPMENT) && projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  717. return true
  718. }
  719. return false
  720. };
  721. ration_glj.prototype.findGLJNodeByID = function (ID) {
  722. let node = _.find(projectObj.project.mainTree.items, function (n) {//找到对应的树节点
  723. return n.sourceType == ModuleNames.ration_glj && n.data.ID == ID;
  724. });
  725. return node;
  726. };
  727. ration_glj.prototype.findRationNodeByID = function (ID) {
  728. let node = _.find(projectObj.project.mainTree.items, function (n) {//找到对应定额的树节点
  729. return n.sourceType == ModuleNames.ration && n.data.ID == ID;
  730. });
  731. return node;
  732. };
  733. ration_glj.prototype.reCalcWhenGLJChange = function (ration_glj) {//当改变定额工料机时,重新计算定额以及其父节点
  734. let node = this.findRationNodeByID(ration_glj.rationID);
  735. if (node) {
  736. node.changed = true;
  737. project.calcProgram.calcAndSave(node);
  738. }
  739. };
  740. return new ration_glj(project);
  741. }
  742. };