ration_glj.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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.addToMainTree = function (datas) {
  178. datas = sortRationGLJ(datas);
  179. for (let data of datas) {
  180. if (this.needShowToTree(data)) {
  181. gljOprObj.setGLJPrice(data);
  182. this.transferToNodeData(data);
  183. let parentNode = projectObj.project.mainTree.findNode(data.rationID);
  184. if (parentNode) {
  185. let nextNodeID = null;
  186. if (parentNode.children.length > 0) {
  187. for (let br of parentNode.children) {
  188. if (compareRationGLJ(data, br.data)) {//如果有兄弟节点则找到添加位置。
  189. nextNodeID = br.getID();
  190. }
  191. }
  192. }
  193. nextNodeID = nextNodeID ? nextNodeID : parentNode.tree.rootID();
  194. let newNode = projectObj.project.mainTree.insert(parentNode.getID(), nextNodeID,data.ID);
  195. newNode.source = data;
  196. newNode.sourceType = this.getSourceType();
  197. newNode.data = data;
  198. ProjectController.syncDisplayNewRationGljNode(projectObj.mainController, newNode);
  199. }
  200. }
  201. }
  202. };
  203. ration_glj.prototype.removeNodeByRation = function(ration,controller){//删除主材或设备节点
  204. let glj_list = _.filter(projectObj.project.ration_glj.datas,{'rationID':ration.ID});
  205. let deleteNodes = [];
  206. for(let rg of glj_list){
  207. if(this.needShowToTree(rg)){
  208. let r_node = projectObj.project.mainTree.getNodeByID(rg.ID);
  209. deleteNodes.push(r_node);
  210. }
  211. }
  212. if(deleteNodes.length > 0){
  213. let rowIndex = deleteNodes[0].serialNo();
  214. if(controller.tree.m_delete(deleteNodes)){
  215. TREE_SHEET_HELPER.massOperationSheet(controller.sheet, function () {
  216. let rowCount = deleteNodes.length;
  217. controller.sheet.deleteRows(rowIndex, rowCount);
  218. });
  219. }
  220. }
  221. };
  222. ration_glj.prototype.refreshAfterUpdate = function (data) {
  223. let me = this;
  224. let rationID= me.updateCacheAfterAdjust(data);
  225. gljOprObj.showRationGLJSheetData(true);
  226. this.reCalcWhenGLJChange({rationID:rationID});
  227. };
  228. ration_glj.prototype.updateCacheAfterAdjust=function (data) {
  229. let me = this;
  230. let rationID=null;
  231. if (data.quantityRefresh) {
  232. data.glj_result.forEach(function (item) {
  233. rationID = me.refreshEachItme(item.doc, item.query);
  234. })
  235. } else {
  236. rationID = me.refreshEachItme(data.doc, data.query);
  237. }
  238. return rationID;
  239. };
  240. ration_glj.prototype.refreshEachItme = function (doc, query) {
  241. let glj = this.refreshByID(query.ID,doc);
  242. return glj.rationID;
  243. };
  244. ration_glj.prototype.refreshByID=function (ID,doc) {
  245. let glj_list = projectObj.project.ration_glj.datas;
  246. let glj_index = _.findIndex(glj_list,{"ID":ID});
  247. _.forEach(doc, function (n, key) {
  248. glj_list[glj_index][key] = n;
  249. });
  250. return glj_list[glj_index]
  251. };
  252. ration_glj.prototype.refreshAfterDelete = function (data) {
  253. let me = projectObj.project.ration_glj;
  254. let glj_list = me.datas;
  255. let oldData = _.remove(glj_list, data.query);
  256. _.remove(gljOprObj.sheetData, data.query);
  257. gljOprObj.showRationGLJSheetData();
  258. projectObj.project.projectGLJ.loadData();
  259. let rationNode = null;
  260. let next = null;
  261. let selected = projectObj.project.mainTree.selected;
  262. if (selected.sourceType == ModuleNames.ration) { //如果选中的是定额,说明是右键删除工料机操作,如果选中的是定额工料机,则说明是在造价书主界面中点击了删除按钮
  263. rationNode = selected;
  264. } else if (selected.sourceType == ModuleNames.ration_glj) {
  265. rationNode = selected.parent;
  266. next = true;
  267. }
  268. rationNode.data.adjustState = data.adjustState;
  269. projectObj.mainController.refreshTreeNode([rationNode]);
  270. for (let o of oldData) {
  271. if (this.needShowToTree(o)) {
  272. let node = me.findGLJNodeByID(o.ID); //找到对应的树节点
  273. projectObj.mainController.deleteNode(node, next);
  274. }
  275. }
  276. project.calcProgram.calcAndSave(rationNode,function () {
  277. installationFeeObj.calcInstallationFee();
  278. });
  279. };
  280. // CSL,2017.05.09
  281. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  282. this.project.beginUpdate('modifyQuantity');
  283. data.quantity = newQuantity;
  284. data.customQuantity = newQuantity;
  285. data.updateType = 'ut_update';
  286. this.project.push(this.getSourceType, data);
  287. this.project.endUpdate();
  288. };
  289. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  290. this.project.beginUpdate('modifyPrice');
  291. data.price = newPrice;
  292. data.updateType = 'ut_update';
  293. this.project.push(this.getSourceType, data);
  294. this.project.endUpdate();
  295. };
  296. ration_glj.prototype.deleteGLJ = function (data) {
  297. this.project.beginUpdate('deleteGLJ');
  298. data.customQuantity = 0;
  299. data.quantity = 0;
  300. data.rationItemQuantity = 0;
  301. data.updateType = 'ut_update';
  302. this.project.push(this.getSourceType, data);
  303. this.project.endUpdate();
  304. };
  305. ration_glj.prototype.addRationGLJ = function (newRation, data) {
  306. let souceTypeList = [];
  307. let criteriaDataList = [];
  308. if (data.hasOwnProperty('rationGljList') && data.rationGljList.length > 0) {
  309. let criteria = {};
  310. criteria.ration_glj_list = [];
  311. for (let i = 0; i < data.rationGljList.length; i++) {
  312. let temdata = data.rationGljList[i];
  313. let newGLJ = {};
  314. newGLJ.projectID = parseInt(newRation.projectID);
  315. newGLJ.GLJID = temdata.gljId;
  316. newGLJ.rationID = newRation.ID;
  317. newGLJ.billsItemID = newRation.billsItemID;
  318. newGLJ.rationItemQuantity = temdata.consumeAmt;
  319. newGLJ.quantity = temdata.consumeAmt;
  320. newGLJ.glj_repository_id = data.rationRepId;
  321. criteria.ration_glj_list.push(newGLJ);
  322. }
  323. criteria.updateType = 'ut_create';
  324. souceTypeList.push(this.getSourceType());
  325. criteriaDataList.push(criteria);
  326. }
  327. let ration_coe = projectObj.project.ration_coe;
  328. let rationCoeData = ration_coe.getRationCoedata(newRation, data);
  329. souceTypeList.push(ration_coe.getSourceType());
  330. criteriaDataList.push(rationCoeData);
  331. project.pushNow('addRationGLJAndRationCoe', souceTypeList, criteriaDataList);
  332. };
  333. ration_glj.prototype.getDeleteDataByRation = function (rationData) {
  334. let updateData = [];
  335. updateData.push({
  336. 'deleteType': 'RATION',
  337. 'updateType': 'ut_delete',
  338. 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}
  339. });
  340. return updateData;
  341. };
  342. ration_glj.prototype.getDeleteDataByBills = function (datas) {
  343. let updateData = [];
  344. datas.forEach(function (deleteData) {
  345. if (deleteData.type == 'delete') {
  346. let billData = deleteData.data;
  347. updateData.push({
  348. 'deleteType': 'BILL',
  349. 'updateType': 'ut_delete',
  350. 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}
  351. });
  352. }
  353. })
  354. return updateData;
  355. };
  356. ration_glj.prototype.deleteByRation = function (ration) {
  357. let glj_list = projectObj.project.ration_glj.datas;
  358. _.remove(glj_list,{'rationID':ration.ID});
  359. };
  360. ration_glj.prototype.deleteByBills = function (deleteData) {
  361. let rationList = projectObj.project.Ration.datas;
  362. let deleteRationList = [];
  363. for (let i = 0; i < deleteData.length; i++) {
  364. if (deleteData[i].type == 'delete') {
  365. let billID = deleteData[i].data.ID;
  366. let raList = _.filter(rationList, (ration) => {
  367. return ration.billsItemID == billID;
  368. });
  369. deleteRationList = deleteRationList.concat(raList);
  370. }
  371. }
  372. for (let i = 0; i < deleteRationList.length; i++) {
  373. this.deleteByRation(deleteRationList[i]);
  374. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  375. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  376. projectObj.project.ration_installation.deleteByRation(deleteRationList[i]);
  377. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  378. }
  379. }
  380. ration_glj.prototype.updataOrdelete = function (row) {
  381. let updateData = null;
  382. if (row.rationItemQuantity == 0) {
  383. updateData = this.getUpdateData('ut_delete', {
  384. 'ID': row.ID,
  385. 'projectID': row.projectID
  386. }, {rationID: row.rationID});
  387. project.pushNow('updateRationGLJ', [this.getSourceType()], updateData)
  388. } else {
  389. this.updateRationGLJByEdit(row, 'customQuantity', 0);
  390. //('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  391. }
  392. };
  393. ration_glj.prototype.getUpdateData = function (type, query, doc, callfunction) {
  394. let updateData = [];
  395. let newobj = {
  396. 'updateType': type,
  397. 'query': query,
  398. }
  399. if (doc) {
  400. newobj['doc'] = doc;
  401. }
  402. if (callfunction) {
  403. newobj['updateFunction'] = callfunction;
  404. }
  405. updateData.push(newobj);
  406. return updateData;
  407. };
  408. ration_glj.prototype.updateRationGLJByEdit = function (recode, updateField, newval, node) {
  409. let me = this,doc = {};
  410. doc[updateField] = newval;
  411. me.submitChange(recode,doc,updateField,node);
  412. };
  413. ration_glj.prototype.submitChange = function(recode,updateData,updateField,node){
  414. let me = this;
  415. let query = {
  416. 'ID': recode.ID,
  417. 'projectID': recode.projectID,
  418. 'rationID': recode.rationID
  419. };
  420. let priceInfo = {
  421. base_price: recode.basePrice,
  422. market_price: recode.marketPrice
  423. };
  424. if(updateData['type'] != undefined || updateData['type'] != null){
  425. updateData.shortName = projectObj.project.projectGLJ.getShortNameByID(updateData['type']);
  426. }
  427. let callback = function (data) {
  428. if (updateField == 'customQuantity') {
  429. me.refreshAfterQuantityUpdate(data, node);
  430. } else {
  431. let doc = data.doc;
  432. for (let key in doc) {
  433. recode[key] = doc[key];
  434. }
  435. me.refreshRationAfterEdit(data,recode.rationID, node);//更新名称和定额调整状态
  436. if (node) {//如果不是在造价书页面直接编辑,则不用刷新
  437. if (updateField == "type" && !(updateData['type'] == gljType.MAIN_MATERIAL || updateData['type'] == gljType.EQUIPMENT)) {//如果改变类型后不是主材或设备,则在造价书树中移除
  438. projectObj.mainController.deleteNode(node, true);
  439. }
  440. }
  441. }
  442. projectObj.project.projectGLJ.loadData(function () {//等项目工料机加载完成后再给用户编辑
  443. gljOprObj.refreshView();
  444. me.refreshTreeNodeIfNeeded(recode);//刷新造价书上的树节点(如果需要)
  445. me.reCalcWhenGLJChange(recode);//触发计算定额以及父节点
  446. $.bootstrapLoading.end();
  447. installationFeeObj.calcInstallationFee();
  448. });
  449. };
  450. $.bootstrapLoading.start();
  451. CommonAjax.post("/rationGlj/updateRationGLJByEdit", {
  452. query: query,
  453. doc: updateData,
  454. priceInfo: priceInfo
  455. }, callback, function (err) {
  456. $.bootstrapLoading.end();
  457. });
  458. };
  459. ration_glj.prototype.refreshAfterQuantityUpdate = function (data, node) {
  460. let me = this;
  461. data.glj_result.forEach(function (item) {
  462. me.refreshEachItme(item.doc, item.query);
  463. });
  464. me.refreshRationAfterEdit(data, data.rationID, node);
  465. };
  466. ration_glj.prototype.refreshRationAfterEdit= function(data,rationID,rnode){
  467. let nodes = [];
  468. let node = this.refreshRationNode(data.name,data.adjustState,rationID);
  469. if (node) nodes.push(node);
  470. if (rnode) nodes.push(rnode);
  471. projectObj.mainController.refreshTreeNode(nodes);
  472. };
  473. ration_glj.prototype.refreshRationNode = function(name,adjustState,rationID){
  474. let node = projectObj.project.mainTree.findNode(rationID);
  475. if (!node) return null;
  476. if(gljUtil.isDef(adjustState)){
  477. node.data.adjustState = adjustState;
  478. }
  479. if(gljUtil.isDef(name)){
  480. node.data.name = name;
  481. }
  482. return node
  483. };
  484. ration_glj.prototype.getGLJData = function (cb) {
  485. let property = projectInfoObj.projectInfo.property;
  486. let engineerID = property.engineering_id;
  487. CommonAjax.get('/rationGlj/getGLJData/'+engineerID, function (data) {
  488. //编办中有多单价设置
  489. if(data.datas.priceProperties && data.datas.priceProperties.length > 0){
  490. let tem = _.find(data.datas.priceProperties,{region:property.region,taxModel:parseInt(property.taxType)});
  491. if(tem){
  492. let dataCode = tem.price.dataCode;
  493. for(let glj of data.datas.stdGLJ){
  494. if(glj.priceProperty && gljUtil.isDef(glj.priceProperty[dataCode])) glj.basePrice = glj.priceProperty[dataCode];
  495. }
  496. }
  497. };
  498. cb(data);
  499. })
  500. };
  501. ration_glj.prototype.insertGLJAsRation = function (GLJSelection, selected, callback) {
  502. let gljList = [];
  503. let allGLJ = gljOprObj.AllRecode;
  504. let billsItemID = null, serialNo = 0,selectedSerialNo = null,nextNodeID = null, parentNodeID = null, billNode = null;
  505. let pEngineer = projectInfoObj.projectInfo.property.projectEngineering;
  506. let children = [];
  507. if (selected.sourceType === project.Bills.getSourceType()) {
  508. billsItemID = selected.data.ID;
  509. parentNodeID = selected.getID();
  510. nextNodeID = selected.tree.rootID();
  511. billNode = selected;
  512. } else {
  513. billsItemID = selected.data.billsItemID;
  514. serialNo = selected.data.serialNo;
  515. selectedSerialNo = selected.data.serialNo;
  516. nextNodeID = selected.getNextSiblingID();
  517. parentNodeID = selected.getParentID();
  518. billNode = selected.parent;
  519. }
  520. children = project.Ration.getBillsSortRation(billsItemID);
  521. serialNo == 0 ? serialNo = children.length : "";
  522. for (let con_key of GLJSelection) {
  523. let glj = _.find(allGLJ, function (item) {
  524. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  525. return i_key == con_key;
  526. });
  527. if (glj) {
  528. serialNo += 1;
  529. let new_glj = {
  530. ID: project.Ration.getNewRationID(),
  531. projectID: parseInt(project.ID()),
  532. billsItemID: billsItemID,
  533. type: rationType.gljRation,
  534. code: glj.code,
  535. name: glj.name,
  536. quantity: 0,
  537. unit: glj.unit,
  538. specs: glj.specs,
  539. subType: glj.gljType,
  540. model:glj.model,
  541. basePrice: glj.basePrice,
  542. marketPrice:glj.basePrice,
  543. original_code: glj.code,
  544. shortName: glj.shortName,
  545. serialNo: serialNo,
  546. GLJID: glj.ID,
  547. adjCoe: glj.adjCoe,
  548. materialType:glj.materialType,
  549. materialCoe:glj.materialCoe,
  550. repositoryId: glj.repositoryId
  551. };
  552. if(pEngineer) new_glj.programID = pEngineer;
  553. if (glj.hasOwnProperty("compilationId")) {
  554. new_glj.from = "cpt";
  555. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  556. new_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  557. }
  558. }
  559. if(optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')){//需要根据清单转换工程量
  560. new_glj.quantityEXP="QDL";
  561. if(billNode.data.quantity){
  562. new_glj.quantity = scMathUtil.roundForObj(billNode.data.quantity/FilterNumberFromUnit(glj.unit),getDecimal("glj.quantity"));
  563. new_glj.contain = scMathUtil.roundForObj(new_glj.quantity/billNode.data.quantity,6);
  564. }
  565. }
  566. gljList.push(new_glj);
  567. }
  568. }
  569. if (gljList.length == 0) {
  570. return;
  571. }
  572. let postData = {
  573. gljList: gljList,
  574. projectID: parseInt(project.ID()),
  575. billsItemID: billsItemID,
  576. rationCount: project.Ration.maxRationID()
  577. }
  578. selectedSerialNo == null ? "" : postData.selectedSerialNo = selectedSerialNo;
  579. CommonAjax.post("/ration/insertGLJAsRation", postData, function (data) {
  580. // 更新兄弟节点的序列号
  581. if (selectedSerialNo != null) {
  582. let selectIndex = _.findIndex(children, {"serialNo": selectedSerialNo});
  583. if (selectIndex + 1 < children.length) {
  584. for (let i = selectIndex + 1; i < children.length; i++) {
  585. children[i].serialNo += gljList.length;
  586. }
  587. }
  588. }
  589. callback(parentNodeID,nextNodeID,data);
  590. }, function () {
  591. $.bootstrapLoading.end();
  592. });
  593. };
  594. ration_glj.prototype.getGLJDataByCodes = function (codes,callback) {
  595. if(!gljUtil.isDef(codes)||codes.length ==0) {
  596. if(callback) callback([]);
  597. return
  598. }
  599. $.bootstrapLoading.start();
  600. CommonAjax.post("/rationGlj/getGLJDataByCodes", { 'engineerID':projectInfoObj.projectInfo.property.engineering_id,projectID:projectObj.project.ID(),codes:codes}, function (result) {
  601. $.bootstrapLoading.end();
  602. callback(result)
  603. });
  604. };
  605. ration_glj.prototype.getAddDataByStd = function (glj,rationID,billsItemID,projectID) {//生成添加类型的定额工料机数据
  606. let ration_glj = {
  607. projectID: projectID,
  608. GLJID: glj.ID,
  609. rationID: rationID,
  610. billsItemID: billsItemID,
  611. rationItemQuantity: 0,
  612. quantity: 0,
  613. name: glj.name,
  614. code: glj.code,
  615. original_code: glj.code,
  616. unit: glj.unit,
  617. specs: glj.specs,
  618. basePrice: glj.basePrice,
  619. marketPrice:glj.basePrice,
  620. shortName: glj.shortName,
  621. type: glj.gljType,
  622. model:glj.model,
  623. adjCoe: glj.adjCoe,
  624. createType: 'add',
  625. materialType:glj.materialType,
  626. materialCoe:glj.materialCoe,
  627. repositoryId: glj.repositoryId
  628. };
  629. if (glj.hasOwnProperty("compilationId")) {
  630. ration_glj.from = "cpt";
  631. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  632. ration_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  633. }
  634. }
  635. return ration_glj;
  636. };
  637. ration_glj.prototype.addGLJByLib = function (GLJSelection, ration, callback) {
  638. let me=this,gljList = [];
  639. let allGLJ = gljOprObj.AllRecode;
  640. GLJSelection.sort();
  641. _.forEach(GLJSelection, function (g) {
  642. let glj = _.find(allGLJ, function (item) {
  643. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  644. return i_key == g;
  645. });
  646. let ration_glj = me.getAddDataByStd(glj,ration.ID,ration.billsItemID,ration.projectID);
  647. gljList.push(ration_glj);
  648. });
  649. $.bootstrapLoading.start();
  650. CommonAjax.post("/rationGlj/addGLJ", gljList, callback, function () {
  651. $.bootstrapLoading.end();
  652. });
  653. };
  654. ration_glj.prototype.updateRationGLJByChangeCode = function (recode, updateField, newval) {
  655. let me = this;
  656. let priceMap = projectObj.project.projectGLJ.datas.unitPriceMap;
  657. let priceObj = priceMap[newval];
  658. if(priceObj){
  659. if (recode.createType===undefined || recode.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  660. recode.rcode = recode.code;
  661. recode.createType = 'replace';
  662. }
  663. recode.code = priceObj.code;
  664. recode.name = priceObj.name;
  665. recode.type = priceObj.type;
  666. recode.unit = priceObj.unit;
  667. recode.shortName = priceObj.short_name;
  668. recode.specs = priceObj.specs;
  669. recode.GLJID=priceObj.glj_id;
  670. recode.original_code = priceObj.original_code;
  671. recode.basePrice = priceObj.base_price;
  672. recode.marketPrice = priceObj.market_price;
  673. }
  674. $.bootstrapLoading.start();
  675. CommonAjax.post("/rationGlj/replaceGLJ", recode, function (result) {
  676. if (result) {
  677. //result.adjustState;
  678. let glj_list = me.datas;
  679. let data = result.data;
  680. let list_index = _.findIndex(glj_list, {'ID': data.ID});
  681. let rationNode = projectObj.project.mainTree.findNode(data.rationID);
  682. let nodes = [rationNode];
  683. glj_list[list_index] = data;
  684. project.projectGLJ.loadData(function () {//加载完项目工料机再计算
  685. gljOprObj.refreshView();
  686. if (project.ration_glj.needShowToTree(data)) {//当替换的是主材或设备时,刷新对应的树节点
  687. let node = project.ration_glj.findGLJNodeByID(data.ID);
  688. if (node) {
  689. project.ration_glj.transferToNodeData(data);
  690. node.source = data;
  691. node.data = data;
  692. }
  693. node ? nodes.push(node) : "";
  694. }
  695. rationNode.data.adjustState = result.adjustState;
  696. rationNode.data.name = result.name;
  697. projectObj.mainController.refreshTreeNode(nodes);
  698. project.calcProgram.calcAndSave(rationNode);
  699. $.bootstrapLoading.end();
  700. });
  701. }
  702. }, function () {
  703. $.bootstrapLoading.end();
  704. });
  705. };
  706. ration_glj.prototype.replaceGLJ = function (selectCode, oldData, callback) {
  707. let allGLJ = gljOprObj.AllRecode;
  708. let glj = _.find(allGLJ, function (item) {
  709. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  710. return i_key == selectCode;
  711. });
  712. if (selectCode == gljOprObj.getIndex(oldData, gljKeyArray)) {
  713. return callback(null);
  714. }
  715. if (oldData.createType===undefined || oldData.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  716. oldData.rcode = oldData.code;
  717. oldData.createType = 'replace';
  718. }
  719. oldData.GLJID = glj.ID;
  720. oldData.name = glj.name;
  721. oldData.code = glj.code;
  722. oldData.original_code = glj.code;
  723. oldData.unit = glj.unit;
  724. oldData.specs = glj.specs;
  725. oldData.model = glj.model;
  726. oldData.basePrice = glj.basePrice;
  727. oldData.marketPrice = glj.basePrice;
  728. oldData.repositoryId = glj.repositoryId;
  729. oldData.materialType = glj.materialType;
  730. oldData. materialCoe = glj.materialCoe;
  731. if (glj.hasOwnProperty("compilationId")) {
  732. oldData.from = "cpt";
  733. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  734. oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  735. }
  736. } else {
  737. oldData.from = "std";
  738. }
  739. $.bootstrapLoading.start();
  740. CommonAjax.post("/rationGlj/replaceGLJ", oldData, callback, function () {
  741. $.bootstrapLoading.end();
  742. });
  743. };
  744. ration_glj.prototype.mReplaceGLJ = function (selectCode, oldData, callback) {
  745. let allGLJ = gljOprObj.AllRecode,tasks = [],updateMap={};
  746. let oldIndex = gljOprObj.getIndex(oldData, gljKeyArray);
  747. let glj = _.find(allGLJ, function (item) {
  748. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  749. return i_key == selectCode;
  750. });
  751. if (selectCode == oldIndex) {
  752. return callback(null);
  753. }
  754. let query = {
  755. projectID: oldData.projectID,
  756. code: oldData.code,
  757. name: oldData.name,
  758. unit: oldData.unit,
  759. type: oldData.type
  760. }
  761. if (oldData.specs && oldData.specs != '') {
  762. query.specs = oldData.specs;
  763. }
  764. let doc = {
  765. GLJID: glj.ID,
  766. createType: 'replace',
  767. rationItemQuantity: 0,
  768. name: glj.name,
  769. code: glj.code,
  770. original_code: glj.code,
  771. unit: glj.unit,
  772. specs: glj.specs,
  773. type: glj.gljType,
  774. model:glj.model,
  775. basePrice: glj.basePrice,
  776. marketPrice:glj.basePrice,
  777. repositoryId: glj.repositoryId,
  778. materialType: glj.materialType, //三材类别
  779. materialCoe: glj.materialCoe,
  780. projectID: oldData.projectID
  781. };
  782. if (glj.hasOwnProperty("compilationId")) {
  783. doc.from = "cpt";
  784. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  785. doc.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  786. }
  787. } else {
  788. doc.from = "std";
  789. }
  790. for(let d of this.datas){//查询出所有需替换的工料机
  791. let tem_index = gljOprObj.getIndex(d, gljKeyArray);
  792. if(tem_index == oldIndex){
  793. let tem_doc = _.cloneDeep(doc);
  794. if(d.createType == 'replace'){
  795. tem_doc.rcode = d.rcode;
  796. }else if(d.createType == 'add'){//对于添加的类型,替换后还是添加类型
  797. tem_doc.createType = 'add';
  798. }else {
  799. tem_doc.rcode = d.code
  800. }
  801. let task = {
  802. updateOne:{
  803. filter : {ID:d.ID},
  804. update : tem_doc
  805. }
  806. };
  807. tasks.push(task);
  808. updateMap[d.ID] = tem_doc;
  809. }
  810. }
  811. $.bootstrapLoading.start();
  812. CommonAjax.post("/rationGlj/mReplaceGLJ", {query: query, doc: doc,tasks:tasks}, function (result) {
  813. callback(result,updateMap);
  814. }, function () {
  815. $.bootstrapLoading.end();
  816. });
  817. };
  818. ration_glj.prototype.getMainAndEquGLJ = function (rationID) {
  819. let gljList = _.filter(this.datas, function (n) {
  820. return n.rationID == rationID && (n.type == gljType.MAIN_MATERIAL || n.type == gljType.EQUIPMENT)
  821. });
  822. gljList = sortRationGLJ(gljList);
  823. return gljOprObj.combineWithProjectGlj(gljList);
  824. };
  825. ration_glj.prototype.transferToNodeData = function (data) {
  826. data.contain=scMathUtil.roundForObj(data.quantity,getDecimal('glj.quantity'));
  827. data.subType = data.type;
  828. };
  829. ration_glj.prototype.combineRationAndGLJ = function (ration) {
  830. if (ration) {
  831. let projectGLJData = projectObj.project.projectGLJ.datas;
  832. let projectGljs = projectGLJData.gljList;
  833. let mixRatioMap = projectGLJData.mixRatioMap;
  834. let glj = _.find(projectGljs, {'id': ration.projectGLJID});
  835. if (glj) {
  836. if(projectObj.project.projectGLJ.isEstimateType(glj.type)){
  837. ration.isEstimate = glj.is_evaluate;
  838. }
  839. ration = gljOprObj.setGLJPrice(ration,glj);
  840. ration.isAdd = glj.unit_price.is_add;
  841. let connect_index = gljOprObj.getIndex(glj, gljKeyArray);
  842. if (mixRatioMap.hasOwnProperty(connect_index)) {
  843. let mixRatios = gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
  844. ration.subList = mixRatios;
  845. }
  846. }
  847. }
  848. return ration;
  849. };
  850. ration_glj.prototype.updateFromMainSpread = function (value, node, fieldName) {
  851. if (node.data[fieldName] !== value) {
  852. if (fieldName == "marketUnitFee") {
  853. let decimal = getDecimal("glj.unitPrice");
  854. let newval = number_util.checkNumberValue(value, decimal);
  855. if (newval) {
  856. fieldName = "marketPrice";
  857. projectObj.project.projectGLJ.updatePriceFromRG(node.data, fieldName, newval);
  858. return;
  859. }
  860. } else {
  861. if(fieldName == "contain"){
  862. if(value==null){
  863. value="";
  864. }else {
  865. let decimal = getDecimal("glj.quantity");
  866. value = number_util.checkNumberValue(value, decimal);
  867. fieldName="customQuantity";//填入自定义消耗
  868. }
  869. }
  870. if (value !== undefined && value !== null) {
  871. if (fieldName == "subType") {
  872. node.data.subType = value;
  873. fieldName = "type";//转换成更新工料机类型
  874. }
  875. this.updateRationGLJByEdit(node.data, fieldName, value, node);
  876. return;
  877. }
  878. }
  879. }
  880. // node.data.subType = value;
  881. projectObj.mainController.refreshTreeNode([node]);
  882. };
  883. ration_glj.prototype.refreshTreeNodeIfNeeded = function (data) {
  884. if (this.needShowToTree(data)) {
  885. this.transferToNodeData(data);
  886. gljOprObj.refreshTreeNode({"type": ModuleNames.ration_glj, "ID": data.ID});
  887. }
  888. };
  889. ration_glj.prototype.needShowToTree = function (data) {
  890. if ((data.type == gljType.MAIN_MATERIAL || data.type == gljType.EQUIPMENT) && projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  891. return true
  892. }
  893. return false
  894. };
  895. ration_glj.prototype.findGLJNodeByID = function (ID) {
  896. return projectObj.project.mainTree.findNode(ID);
  897. };
  898. ration_glj.prototype.findRationNodeByID = function (ID) {
  899. return projectObj.project.mainTree.findNode(ID);
  900. };
  901. ration_glj.prototype.reCalcWhenGLJChange = function (ration_glj) {//当改变定额工料机时,重新计算定额以及其父节点
  902. let node = this.findRationNodeByID(ration_glj.rationID);
  903. if (node) {
  904. node.changed = true;
  905. project.calcProgram.calcAndSave(node);
  906. }
  907. };
  908. return new ration_glj(project);
  909. }
  910. };