ration_glj.js 35 KB

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