ration_glj.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. let ration_glj = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. let tools = {};
  8. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  9. let ration_glj = function (proj) {
  10. this.gljTree = cacheTree.createNew(this);
  11. // this.project = proj;
  12. this.datas = [];
  13. let sourceType = ModuleNames.ration_glj;
  14. this.getSourceType = function () {
  15. return sourceType;
  16. }
  17. proj.registerModule(ModuleNames.ration_glj, this);
  18. };
  19. // 从后台获取数据
  20. /*glj.prototype.pullData = function (){
  21. this.project.pullData(
  22. '/glj/getData',
  23. {projectID: this.project.ID},
  24. function(result){
  25. if (result.error ===0){
  26. this.loadDatas(result.data);
  27. }
  28. else {
  29. // to do: 错误处理需要细化
  30. alert(result.message);
  31. }
  32. },
  33. function (){}//to do: 错误处理需要细化
  34. )
  35. };*/
  36. // prototype用于定义public方法
  37. ration_glj.prototype.loadData = function (datas) {
  38. this.datas = datas;
  39. };
  40. ration_glj.prototype.getGLJListByRationID = function (rationID) {
  41. return _.filter(this.datas, {'rationID': rationID})
  42. };
  43. ration_glj.prototype.getGatherGljArrByRations = function (rations, needOneBill, billQuantity) {
  44. let result = [];
  45. let clone = function (obj) {
  46. if (obj === null) return null;
  47. let o = Object.prototype.toString.apply(obj) === "[object Array]" ? [] : {};
  48. for (let i in obj) {
  49. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? clone(obj[i]) : obj[i]);
  50. }
  51. return o;
  52. }
  53. let findGlj = function (sourceGlj, gljArr) {
  54. for (let glj of gljArr) {
  55. if (glj.projectGLJID === sourceGlj.projectGLJID) {
  56. return glj;
  57. }
  58. }
  59. return null;
  60. }
  61. for (let ration of rations) {
  62. if (ration.type == rationType.volumePrice || ration.type == rationType.gljRation){
  63. let glj = JSON.parse(JSON.stringify(ration));
  64. glj.type = glj.subType;
  65. glj.totalQuantity = parseFloatPlus(ration.quantity);
  66. glj.markertPrice = glj.marketUnitFee;
  67. glj.basePrice = glj.marketUnitFee;
  68. glj.adjustPrice = glj.marketUnitFee;
  69. result.push(glj);
  70. } else{
  71. let rationGljs = projectObj.project.calcProgram.getGljArrByRation(ration);
  72. for (let glj of rationGljs) {
  73. let sameGlj = findGlj(glj, result);
  74. if (!sameGlj) {
  75. sameGlj = clone(glj);
  76. sameGlj.quantity = (sameGlj.quantity * ration.quantity).toDecimal(4);
  77. result.push(sameGlj);
  78. } else {
  79. sameGlj.quantity = sameGlj.quantity + (glj.quantity * ration.quantity).toDecimal(4);
  80. sameGlj.totalQuantity = (parseFloat(sameGlj.totalQuantity) + parseFloat(glj.totalQuantity)).toDecimal(4);
  81. }
  82. }
  83. }
  84. }
  85. result = gljOprObj.combineWithProjectGlj(result);
  86. if (!needOneBill) return result;
  87. // 上面取的是清单下所有工料机的总量,我要算清单单价,所以要取单位清单的工料机数量,所以下面要除以清单数量。
  88. let oneBill = JSON.parse(JSON.stringify(result));
  89. for (let glj of oneBill){
  90. if (glj.type == rationType.volumePrice){ // 量价、工料机形式的定额要进行数据变换才能参与基数计算。
  91. glj.type = glj.subType;
  92. glj.basePrice = glj.marketUnitFee;
  93. glj.marketPrice = glj.marketUnitFee;
  94. glj.adjustPrice = glj.marketUnitFee;
  95. }
  96. else if (glj.type == rationType.gljRation){
  97. glj.type = glj.subType;
  98. };
  99. // glj.quantity = (glj.quantity / billQuantity).toDecimal(decimalObj.glj.quantity);
  100. glj.quantity = (glj.quantity / billQuantity).toDecimal(decimalObj.process); // 广联达这里没有取舍
  101. };
  102. return oneBill;
  103. }
  104. // 提交数据后返回数据处理
  105. ration_glj.prototype.doAfterUpdate = function (err, data) {
  106. let me = this;
  107. if (!err) {
  108. if (data.updateTpye == 'ut_update') {
  109. me.refreshAfterUpdate(data);
  110. } else if (data.updateTpye == 'ut_delete') {
  111. me.refreshAfterDelete(data);
  112. } else {
  113. me.refreshAfterSave(data);
  114. }
  115. }
  116. projectObj.project.projectGLJ.loadData();
  117. };
  118. ration_glj.prototype.getDataByID = function (ID) {
  119. return _.find(this.datas,{'ID':ID});
  120. };
  121. ration_glj.prototype.refreshAfterSave = function (data) {
  122. let me = projectObj.project.ration_glj;
  123. let neRecodes = [];
  124. if (data) {
  125. // neRecodes=data.newRecords;//原来是显示和缓存分开的,后来发现会导致数据不一致的问题所以改成统一的了,这里也只是会作为显示。
  126. neRecodes = data.showDatas;
  127. gljOprObj.sheetData = neRecodes;
  128. me.addDatasToList(neRecodes);
  129. }
  130. project.projectGLJ.loadData(function () {
  131. gljOprObj.showRationGLJSheetData(true);
  132. //add to mainTree;
  133. me.addToMainTree(neRecodes);
  134. let rationID = neRecodes[0].rationID;
  135. let node = project.mainTree.nodes['id_' + rationID];
  136. if(isDef(node)){
  137. project.calcProgram.calcAndSave(node);
  138. }
  139. if (activeSubSheetIsCalcProgram())
  140. calcProgramObj.refreshCalcProgram(node, 1);
  141. });
  142. };
  143. ration_glj.prototype.getGljByRationID = function(rationID){
  144. return _.filter(this.datas, {'rationID': rationID});
  145. };
  146. ration_glj.prototype.addDatasToList = function (datas) {
  147. let me = projectObj.project.ration_glj;
  148. if(datas&&datas.length>0){
  149. if (me.datas && Array.isArray(me.datas)) {
  150. me.datas = me.datas.concat(datas);
  151. } else {
  152. me.datas = datas;
  153. }
  154. }
  155. };
  156. ration_glj.prototype.updateGLJNodeAfterReplace = function(data){
  157. if(!data) return null;
  158. if (this.needShowToTree(data)) {//当替换的是主材或设备时,刷新对应的树节点
  159. var node = this.findGLJNodeByID(data.ID);
  160. if (node) {
  161. this.transferToNodeData(data);
  162. node.source = data;
  163. node.data = data;
  164. }
  165. return node;
  166. }
  167. };
  168. ration_glj.prototype.addToMainTree = function (datas) {
  169. datas = gljUtil.sortRationGLJ(datas);
  170. for (let data of datas) {
  171. if (this.needShowToTree(data)) {
  172. gljOprObj.setGLJPrice(data);
  173. this.transferToNodeData(data);
  174. let parentNode = projectObj.project.mainTree.findNode(data.rationID);
  175. if (parentNode) {
  176. let nextNodeID = null;
  177. if (parentNode.children.length > 0) {
  178. for (let br of parentNode.children) {
  179. if (compareRationGLJ(data, br.data)) {//如果有兄弟节点则找到添加位置。
  180. nextNodeID = br.getID();
  181. }
  182. }
  183. }
  184. nextNodeID = nextNodeID ? nextNodeID : parentNode.tree.rootID();
  185. let newNode = projectObj.project.mainTree.insert(parentNode.getID(), nextNodeID,data.ID);
  186. newNode.source = data;
  187. newNode.sourceType = this.getSourceType();
  188. newNode.data = data;
  189. ProjectController.syncDisplayNewRationGljNode(projectObj.mainController, newNode);
  190. }
  191. }
  192. }
  193. };
  194. ration_glj.prototype.removeNodeByRation = function(ration,controller){//删除主材或设备节点
  195. let glj_list = _.filter(projectObj.project.ration_glj.datas,{'rationID':ration.ID});
  196. let deleteNodes = [];
  197. for(let rg of glj_list){
  198. if(this.needShowToTree(rg)){
  199. let r_node = projectObj.project.mainTree.getNodeByID(rg.ID);
  200. deleteNodes.push(r_node);
  201. }
  202. }
  203. if(deleteNodes.length > 0){
  204. let rowIndex = deleteNodes[0].serialNo();
  205. if(controller.tree.m_delete(deleteNodes)){
  206. TREE_SHEET_HELPER.massOperationSheet(controller.sheet, function () {
  207. let rowCount = deleteNodes.length;
  208. controller.sheet.deleteRows(rowIndex, rowCount);
  209. });
  210. }
  211. }
  212. };
  213. ration_glj.prototype.refreshAfterUpdate = function (data) {
  214. let me = this;
  215. let rationID= me.updateCacheAfterAdjust(data);
  216. gljOprObj.showRationGLJSheetData(true);
  217. this.reCalcWhenGLJChange({rationID:rationID});
  218. };
  219. ration_glj.prototype.updateCacheAfterAdjust=function (data) {
  220. let me = this;
  221. let rationID=null;
  222. if (data.quantityRefresh) {
  223. data.glj_result.forEach(function (item) {
  224. rationID = me.refreshEachItme(item.doc, item.query);
  225. })
  226. } else {
  227. rationID = me.refreshEachItme(data.doc, data.query);
  228. }
  229. return rationID;
  230. };
  231. ration_glj.prototype.refreshEachItme = function (doc, query) {
  232. let glj = this.refreshByID(query.ID,doc);
  233. return glj.rationID;
  234. };
  235. ration_glj.prototype.refreshByID=function (ID,doc) {
  236. let glj_list = projectObj.project.ration_glj.datas;
  237. let glj_index = _.findIndex(glj_list,{"ID":ID});
  238. _.forEach(doc, function (n, key) {
  239. glj_list[glj_index][key] = n;
  240. });
  241. return glj_list[glj_index]
  242. };
  243. ration_glj.prototype.refreshAfterDelete = function (data) {
  244. let me = projectObj.project.ration_glj;
  245. let glj_list = me.datas;
  246. let oldData = _.remove(glj_list, data.query);
  247. _.remove(gljOprObj.sheetData, data.query);
  248. gljOprObj.showRationGLJSheetData();
  249. projectObj.project.projectGLJ.loadData();
  250. let rationNode = null;
  251. let next = null;
  252. let selected = projectObj.project.mainTree.selected;
  253. if (selected.sourceType == ModuleNames.ration) { //如果选中的是定额,说明是右键删除工料机操作,如果选中的是定额工料机,则说明是在造价书主界面中点击了删除按钮
  254. rationNode = selected;
  255. } else if (selected.sourceType == ModuleNames.ration_glj) {
  256. rationNode = selected.parent;
  257. next = true;
  258. }
  259. rationNode.data.adjustState = data.adjustState;
  260. projectObj.mainController.refreshTreeNode([rationNode]);
  261. for (let o of oldData) {
  262. if (this.needShowToTree(o)) {
  263. let node = me.findGLJNodeByID(o.ID); //找到对应的树节点
  264. projectObj.mainController.deleteNode(node, next);
  265. }
  266. }
  267. project.calcProgram.calcAndSave(rationNode,function () {
  268. installationFeeObj.calcInstallationFee();
  269. });
  270. };
  271. // CSL,2017.05.09
  272. ration_glj.prototype.modifyQuantity = function (data, newQuantity) {
  273. this.project.beginUpdate('modifyQuantity');
  274. data.quantity = newQuantity;
  275. data.customQuantity = newQuantity;
  276. data.updateType = 'ut_update';
  277. this.project.push(this.getSourceType, data);
  278. this.project.endUpdate();
  279. };
  280. ration_glj.prototype.modifyPrice = function (data, newPrice) {
  281. this.project.beginUpdate('modifyPrice');
  282. data.price = newPrice;
  283. data.updateType = 'ut_update';
  284. this.project.push(this.getSourceType, data);
  285. this.project.endUpdate();
  286. };
  287. ration_glj.prototype.deleteGLJ = function (data) {
  288. this.project.beginUpdate('deleteGLJ');
  289. data.customQuantity = 0;
  290. data.quantity = 0;
  291. data.rationItemQuantity = 0;
  292. data.updateType = 'ut_update';
  293. this.project.push(this.getSourceType, data);
  294. this.project.endUpdate();
  295. };
  296. ration_glj.prototype.addRationGLJ = function (newRation, data) {
  297. let souceTypeList = [];
  298. let criteriaDataList = [];
  299. if (data.hasOwnProperty('rationGljList') && data.rationGljList.length > 0) {
  300. let criteria = {};
  301. criteria.ration_glj_list = [];
  302. for (let i = 0; i < data.rationGljList.length; i++) {
  303. let temdata = data.rationGljList[i];
  304. let newGLJ = {};
  305. newGLJ.projectID = parseInt(newRation.projectID);
  306. newGLJ.GLJID = temdata.gljId;
  307. newGLJ.rationID = newRation.ID;
  308. newGLJ.billsItemID = newRation.billsItemID;
  309. newGLJ.rationItemQuantity = temdata.consumeAmt;
  310. newGLJ.quantity = temdata.consumeAmt;
  311. newGLJ.glj_repository_id = data.rationRepId;
  312. criteria.ration_glj_list.push(newGLJ);
  313. }
  314. criteria.updateType = 'ut_create';
  315. souceTypeList.push(this.getSourceType());
  316. criteriaDataList.push(criteria);
  317. }
  318. let ration_coe = projectObj.project.ration_coe;
  319. let rationCoeData = ration_coe.getRationCoedata(newRation, data);
  320. souceTypeList.push(ration_coe.getSourceType());
  321. criteriaDataList.push(rationCoeData);
  322. project.pushNow('addRationGLJAndRationCoe', souceTypeList, criteriaDataList);
  323. };
  324. ration_glj.prototype.getDeleteDataByRation = function (rationData) {
  325. let updateData = [];
  326. updateData.push({
  327. 'deleteType': 'RATION',
  328. 'updateType': 'ut_delete',
  329. 'updateData': {'ID': rationData.ID, 'projectID': rationData.projectID}
  330. });
  331. return updateData;
  332. };
  333. ration_glj.prototype.getDeleteDataByBills = function (datas) {
  334. let updateData = [];
  335. datas.forEach(function (deleteData) {
  336. if (deleteData.type == 'delete') {
  337. let billData = deleteData.data;
  338. updateData.push({
  339. 'deleteType': 'BILL',
  340. 'updateType': 'ut_delete',
  341. 'updateData': {'ID': billData.ID, 'projectID': billData.projectID}
  342. });
  343. }
  344. })
  345. return updateData;
  346. };
  347. ration_glj.prototype.deleteByRation = function (ration) {
  348. let glj_list = projectObj.project.ration_glj.datas;
  349. _.remove(glj_list,{'rationID':ration.ID});
  350. };
  351. ration_glj.prototype.deleteByBills = function (deleteData) {
  352. let rationList = projectObj.project.Ration.datas;
  353. let deleteRationList = [];
  354. for (let i = 0; i < deleteData.length; i++) {
  355. if (deleteData[i].type == 'delete') {
  356. let billID = deleteData[i].data.ID;
  357. let raList = _.filter(rationList, (ration) => {
  358. return ration.billsItemID == billID;
  359. });
  360. deleteRationList = deleteRationList.concat(raList);
  361. }
  362. }
  363. for (let i = 0; i < deleteRationList.length; i++) {
  364. this.deleteByRation(deleteRationList[i]);
  365. projectObj.project.ration_coe.deleteByRation(deleteRationList[i]);
  366. projectObj.project.quantity_detail.deleteByRation(deleteRationList[i]);
  367. projectObj.project.ration_installation.deleteByRation(deleteRationList[i]);
  368. projectObj.project.Ration.datas.splice(projectObj.project.Ration.datas.indexOf(deleteRationList[i]), 1);
  369. }
  370. }
  371. ration_glj.prototype.updataOrdelete = function (row) {
  372. let updateData = null;
  373. if (row.rationItemQuantity == 0) {
  374. /* updateData = this.getUpdateData('ut_delete', {
  375. 'ID': row.ID,
  376. 'projectID': row.projectID
  377. }, {rationID: row.rationID});
  378. project.pushNow('updateRationGLJ', [this.getSourceType()], updateData)*/
  379. this.deleteRationGLJ({ID:row.ID,rationID:row.rationID, projectID: row.projectID,type:row.type});
  380. } else {
  381. this.updateRationGLJByEdit(row, 'customQuantity', 0);
  382. //('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  383. }
  384. };
  385. ration_glj.prototype.deleteRationGLJ = async function(data){
  386. try {
  387. let result = await ajaxPost('/rationGlj/deleteRationGLJ',data);
  388. console.log(result);
  389. let oldData = this.addAndDeleteDatas([],result.deleteList);
  390. this.refreshQuantityAfterCalResult(result.glj_result);
  391. let rationNode = null;
  392. let next = null;
  393. let selected = projectObj.project.mainTree.findNode(result.rationID);
  394. if (selected.sourceType == ModuleNames.ration) { //如果选中的是定额,说明是右键删除工料机操作,如果选中的是定额工料机,则说明是在造价书主界面中点击了删除按钮
  395. rationNode = selected;
  396. } else if (selected.sourceType == ModuleNames.ration_glj) {
  397. rationNode = selected.parent;
  398. next = true;
  399. }
  400. rationNode.data.adjustState = result.adjustState;
  401. projectObj.mainController.refreshTreeNode([rationNode]);
  402. for (let o of oldData) {
  403. if (this.needShowToTree(o)) {
  404. let node = me.findGLJNodeByID(o.ID); //找到对应的树节点
  405. projectObj.mainController.deleteNode(node, next);
  406. }
  407. }
  408. project.calcProgram.calcAndSave(rationNode,function () {
  409. installationFeeObj.calcInstallationFee();
  410. });
  411. gljOprObj.refreshView();
  412. projectObj.project.projectGLJ.loadData();
  413. }catch (e){
  414. console.log(e)
  415. }
  416. };
  417. ration_glj.prototype.getUpdateData = function (type, query, doc, callfunction) {
  418. let updateData = [];
  419. let newobj = {
  420. 'updateType': type,
  421. 'query': query,
  422. }
  423. if (doc) {
  424. newobj['doc'] = doc;
  425. }
  426. if (callfunction) {
  427. newobj['updateFunction'] = callfunction;
  428. }
  429. updateData.push(newobj);
  430. return updateData;
  431. };
  432. ration_glj.prototype.updateRationGLJByEdit = function (recode, updateField, newval, node) {
  433. let me = this,doc = {};
  434. doc[updateField] = newval;
  435. me.submitChange(recode,doc,updateField,node);
  436. };
  437. ration_glj.prototype.submitChange = function(recode,updateData,updateField,node){
  438. let me = this;
  439. let query = {
  440. 'ID': recode.ID,
  441. 'projectID': recode.projectID,
  442. 'rationID': recode.rationID
  443. };
  444. let priceInfo = {
  445. base_price: recode.basePrice,
  446. market_price: recode.marketPrice
  447. };
  448. if(updateData['type'] != undefined || updateData['type'] != null){
  449. updateData.shortName = projectObj.project.projectGLJ.getShortNameByID(updateData['type']);
  450. }
  451. let callback = function (data) {
  452. if (updateField == 'customQuantity') {
  453. me.refreshAfterQuantityUpdate(data, node);
  454. } else {
  455. let doc = data.doc;
  456. for (let key in doc) {
  457. recode[key] = doc[key];
  458. }
  459. me.refreshRationAfterEdit(data,recode.rationID, node);//更新名称和定额调整状态
  460. if (node) {//如果不是在造价书页面直接编辑,则不用刷新
  461. if (updateField == "type" && !(updateData['type'] == gljType.MAIN_MATERIAL || updateData['type'] == gljType.EQUIPMENT)) {//如果改变类型后不是主材或设备,则在造价书树中移除
  462. projectObj.mainController.deleteNode(node, true);
  463. }
  464. }
  465. }
  466. projectObj.project.projectGLJ.loadData(function () {//等项目工料机加载完成后再给用户编辑
  467. me.refreshTreeNodeIfNeeded(recode);//刷新造价书上的树节点(如果需要)
  468. me.reCalcWhenGLJChange(recode);//触发计算定额以及父节点
  469. zmhs_obj.refreshStableDataIfNeeded();
  470. gljOprObj.refreshView();
  471. $.bootstrapLoading.end();
  472. installationFeeObj.calcInstallationFee();
  473. });
  474. };
  475. $.bootstrapLoading.start();
  476. CommonAjax.post("/rationGlj/updateRationGLJByEdit", {
  477. query: query,
  478. doc: updateData,
  479. priceInfo: priceInfo
  480. }, callback, function (err) {
  481. $.bootstrapLoading.end();
  482. });
  483. };
  484. ration_glj.prototype.refreshAfterQuantityUpdate = function (data, node) {
  485. this.refreshQuantityAfterCalResult(data.glj_result);
  486. this.refreshRationAfterEdit(data, data.rationID, node);
  487. };
  488. ration_glj.prototype.refreshQuantityAfterCalResult = function (glj_result) {//后端计算后,前端刷新结果
  489. let me = this;
  490. glj_result.forEach(function (item) {
  491. me.refreshEachItme(item.doc, item.query);
  492. });
  493. };
  494. ration_glj.prototype.refreshRationAfterEdit= function(data,rationID,rnode){
  495. let nodes = [];
  496. let node = this.refreshRationNode(data.name,data.adjustState,rationID);
  497. if (node) nodes.push(node);
  498. if (rnode) nodes.push(rnode);
  499. projectObj.mainController.refreshTreeNode(nodes);
  500. };
  501. ration_glj.prototype.refreshRationNode = function(name,adjustState,rationID){
  502. let node = projectObj.project.mainTree.findNode(rationID);
  503. if (!node) return null;
  504. if(gljUtil.isDef(adjustState)){
  505. node.data.adjustState = adjustState;
  506. }
  507. if(gljUtil.isDef(name)){
  508. node.data.name = name;
  509. }
  510. return node
  511. };
  512. ration_glj.prototype.getGLJData = function (cb) {
  513. let property = projectObj.project.projectInfo.property;
  514. let engineerID = property.engineering_id;
  515. CommonAjax.get('/rationGlj/getGLJData/'+engineerID, function (data) {
  516. //编办中有多单价设置
  517. if(data.datas.priceProperties && data.datas.priceProperties.length > 0){
  518. let tem = _.find(data.datas.priceProperties,{region:property.region,taxModel:parseInt(property.taxType)});
  519. if(tem){
  520. let dataCode = tem.price.dataCode;
  521. for(let glj of data.datas.stdGLJ){
  522. if(glj.priceProperty && gljUtil.isDef(glj.priceProperty[dataCode])) glj.basePrice = glj.priceProperty[dataCode];
  523. }
  524. }
  525. };
  526. cb(data);
  527. })
  528. };
  529. ration_glj.prototype.insertGLJAsRation = function (GLJSelection, selected, callback) {
  530. let gljList = [];
  531. let allGLJ = gljOprObj.AllRecode;
  532. let billsItemID = null, serialNo = 0,selectedSerialNo = null,nextNodeID = null, parentNodeID = null, billNode = null;
  533. let pEngineer = projectObj.project.projectInfo.property.projectEngineering;
  534. let children = [];
  535. if (selected.sourceType === project.Bills.getSourceType()) {
  536. billsItemID = selected.data.ID;
  537. parentNodeID = selected.getID();
  538. nextNodeID = selected.tree.rootID();
  539. billNode = selected;
  540. } else {
  541. billsItemID = selected.data.billsItemID;
  542. serialNo = selected.data.serialNo;
  543. selectedSerialNo = selected.data.serialNo;
  544. nextNodeID = selected.getNextSiblingID();
  545. parentNodeID = selected.getParentID();
  546. billNode = selected.parent;
  547. }
  548. children = project.Ration.getBillsSortRation(billsItemID);
  549. serialNo == 0 ? serialNo = children.length : "";
  550. for (let con_key of GLJSelection) {
  551. let glj = _.find(allGLJ, function (item) {
  552. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  553. return i_key == con_key;
  554. });
  555. if (glj) {
  556. serialNo += 1;
  557. let new_glj = {
  558. ID: project.Ration.getNewRationID(),
  559. projectID: parseInt(project.ID()),
  560. billsItemID: billsItemID,
  561. type: rationType.gljRation,
  562. code: glj.code,
  563. name: glj.name,
  564. quantity: 0,
  565. unit: glj.unit,
  566. specs: glj.specs,
  567. subType: glj.gljType,
  568. model:glj.model,
  569. basePrice: glj.basePrice,
  570. marketPrice:glj.basePrice,
  571. original_code: glj.code,
  572. shortName: glj.shortName,
  573. serialNo: serialNo,
  574. GLJID: glj.ID,
  575. adjCoe: glj.adjCoe,
  576. materialType:glj.materialType,
  577. materialCoe:glj.materialCoe,
  578. grossWeightCoe:glj.grossWeightCoe,
  579. purchaseStorageRate:glj.purchaseStorageRate,
  580. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  581. handlingLossRate:glj.handlingLossRate,
  582. repositoryId: glj.repositoryId
  583. };
  584. if(pEngineer) new_glj.programID = pEngineer;
  585. let belongFlag = cbTools.getBelongFlag(selected);
  586. if (belongFlag && belongFlag === fixedFlag.EQUIPMENT_ACQUISITION_FEE) {
  587. new_glj.programID = projectObj.project.calcProgram.compiledTemplateMaps['设备购置'];
  588. }
  589. if (glj.hasOwnProperty("compilationId")) {
  590. new_glj.from = "cpt";
  591. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  592. new_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  593. }
  594. }
  595. if(optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')){//需要根据清单转换工程量
  596. new_glj.quantityEXP="QDL";
  597. if(billNode.data.quantity){
  598. new_glj.quantity = scMathUtil.roundForObj(billNode.data.quantity/FilterNumberFromUnit(glj.unit),getDecimal("glj.quantity"));
  599. new_glj.contain = scMathUtil.roundForObj(new_glj.quantity/billNode.data.quantity,6);
  600. }
  601. }
  602. gljList.push(new_glj);
  603. }
  604. }
  605. if (gljList.length == 0) {
  606. return;
  607. }
  608. let postData = {
  609. gljList: gljList,
  610. projectID: parseInt(project.ID()),
  611. billsItemID: billsItemID,
  612. rationCount: project.Ration.maxRationID()
  613. }
  614. selectedSerialNo == null ? "" : postData.selectedSerialNo = selectedSerialNo;
  615. CommonAjax.post("/ration/insertGLJAsRation", postData, function (data) {
  616. // 更新兄弟节点的序列号
  617. if (selectedSerialNo != null) {
  618. let selectIndex = _.findIndex(children, {"serialNo": selectedSerialNo});
  619. if (selectIndex + 1 < children.length) {
  620. for (let i = selectIndex + 1; i < children.length; i++) {
  621. children[i].serialNo += gljList.length;
  622. }
  623. }
  624. }
  625. callback(parentNodeID,nextNodeID,data);
  626. }, function () {
  627. $.bootstrapLoading.end();
  628. });
  629. };
  630. ration_glj.prototype.getGLJDataByCodes = function (codes,callback) {
  631. if(!gljUtil.isDef(codes)||codes.length ==0) {
  632. if(callback) callback([]);
  633. return
  634. }
  635. $.bootstrapLoading.start();
  636. CommonAjax.post("/rationGlj/getGLJDataByCodes", { 'engineerID':projectObj.project.projectInfo.property.engineering_id,projectID:projectObj.project.ID(),codes:codes}, function (result) {
  637. $.bootstrapLoading.end();
  638. callback(result)
  639. });
  640. };
  641. ration_glj.prototype.getAddDataByStd = function (glj,rationID,billsItemID,projectID) {//生成添加类型的定额工料机数据
  642. let ration_glj = {
  643. projectID: projectID,
  644. GLJID: glj.ID,
  645. rationID: rationID,
  646. billsItemID: billsItemID,
  647. rationItemQuantity: 0,
  648. quantity: 0,
  649. name: glj.name,
  650. code: glj.code,
  651. original_code: glj.code,
  652. unit: glj.unit,
  653. specs: glj.specs,
  654. basePrice: glj.basePrice,
  655. marketPrice:glj.basePrice,
  656. shortName: glj.shortName,
  657. type: glj.gljType,
  658. model:glj.model,
  659. adjCoe: glj.adjCoe,
  660. createType: 'add',
  661. materialType:glj.materialType,
  662. materialCoe:glj.materialCoe,
  663. grossWeightCoe:glj.grossWeightCoe,
  664. purchaseStorageRate:glj.purchaseStorageRate,
  665. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  666. handlingLossRate:glj.handlingLossRate,
  667. repositoryId: glj.repositoryId
  668. };
  669. if (glj.hasOwnProperty("compilationId")) {
  670. ration_glj.from = "cpt";
  671. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  672. ration_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  673. }
  674. }
  675. return ration_glj;
  676. };
  677. ration_glj.prototype.addGLJByLib = function (GLJSelection, ration, callback) {
  678. let me=this,gljList = [];
  679. let allGLJ = gljOprObj.AllRecode;
  680. GLJSelection.sort();
  681. _.forEach(GLJSelection, function (g) {
  682. let glj = _.find(allGLJ, function (item) {
  683. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  684. return i_key == g;
  685. });
  686. let ration_glj = me.getAddDataByStd(glj,ration.ID,ration.billsItemID,ration.projectID);
  687. gljList.push(ration_glj);
  688. });
  689. $.bootstrapLoading.start();
  690. CommonAjax.post("/rationGlj/addGLJ", gljList, callback, function () {
  691. $.bootstrapLoading.end();
  692. });
  693. };
  694. ration_glj.prototype.updateRationGLJByChangeCode = function (recode, updateField, newval) {
  695. let me = this;
  696. let priceMap = projectObj.project.projectGLJ.datas.unitPriceMap;
  697. let priceObj = priceMap[newval];
  698. if(priceObj){
  699. if (recode.createType===undefined || recode.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  700. recode.rcode = recode.code;
  701. recode.createType = 'replace';
  702. }
  703. recode.code = priceObj.code;
  704. recode.name = priceObj.name;
  705. recode.type = priceObj.type;
  706. recode.unit = priceObj.unit;
  707. recode.shortName = priceObj.short_name;
  708. recode.specs = priceObj.specs;
  709. recode.GLJID=priceObj.glj_id;
  710. recode.original_code = priceObj.original_code;
  711. recode.basePrice = priceObj.base_price;
  712. recode.marketPrice = priceObj.market_price;
  713. }
  714. $.bootstrapLoading.start();
  715. CommonAjax.post("/rationGlj/replaceGLJ", recode, function (result) {
  716. if (result) {
  717. me.setDatasAfterReplace(result);
  718. let data = result.data;
  719. let rationNode = projectObj.project.mainTree.findNode(data.rationID);
  720. let nodes = [rationNode];
  721. gljOprObj.refreshView();
  722. if (project.ration_glj.needShowToTree(data)) {//当替换的是主材或设备时,刷新对应的树节点
  723. let node = project.ration_glj.findGLJNodeByID(data.ID);
  724. if (node) {
  725. project.ration_glj.transferToNodeData(data);
  726. node.source = data;
  727. node.data = data;
  728. }
  729. node ? nodes.push(node) : "";
  730. }
  731. rationNode.data.adjustState = result.adjustState;
  732. rationNode.data.name = result.name;
  733. projectObj.mainController.refreshTreeNode(nodes);
  734. project.calcProgram.calcAndSave(rationNode);
  735. $.bootstrapLoading.end();
  736. }
  737. }, function () {
  738. $.bootstrapLoading.end();
  739. });
  740. };
  741. ration_glj.prototype.setDatasAfterReplace = function(result){
  742. let me = this;
  743. me.addAndDeleteDatas(result.newRecodes,result.deleteList);
  744. let list_index = _.findIndex(me.datas, {'ID': result.data.ID});
  745. if(result.data) me.datas[list_index] = result.data;
  746. me.refreshQuantityAfterCalResult(result.glj_result);
  747. if(result.projectGLJ) project.projectGLJ.loadNewProjectGLJToCaches([result.projectGLJ],true);
  748. };
  749. ration_glj.prototype.addAndDeleteDatas = function (newRecodes,deleteList) {
  750. let oldData = [];
  751. if(newRecodes.length > 0 ) this.datas = this.datas.concat(newRecodes);
  752. if(deleteList.length > 0) {
  753. oldData = _.remove(this.datas,function (item) {
  754. return deleteList.indexOf(item.ID)!=-1;
  755. });
  756. }
  757. return oldData;
  758. };
  759. ration_glj.prototype.getReplaceDataByStd = function (oldData,glj) {
  760. if (oldData.createType===undefined || oldData.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  761. oldData.rcode = oldData.code;
  762. oldData.createType = 'replace';
  763. oldData.mIndexObj ={code:oldData.code,name:oldData.name,type:oldData.type,unit:oldData.unit,specs:oldData.specs};
  764. }else if(oldData.createType == 'replace' && oldData.rcode == glj.code){//如果原数据已经是替换过的,这次替换又恢复成原数据,则把类型改回来
  765. oldData.createType = 'normal';
  766. oldData.mIndexObj = null;
  767. }
  768. oldData.GLJID = glj.ID;
  769. oldData.name = glj.name;
  770. oldData.type = glj.gljType;
  771. oldData.code = glj.code;
  772. oldData.original_code = glj.code;
  773. oldData.unit = glj.unit;
  774. oldData.specs = glj.specs;
  775. oldData.model = glj.model;
  776. oldData.basePrice = glj.basePrice;
  777. oldData.shortName=glj.shortName;
  778. oldData.marketPrice = glj.basePrice;
  779. oldData.repositoryId = glj.repositoryId;
  780. oldData.materialType = glj.materialType;
  781. oldData.materialCoe = glj.materialCoe;
  782. oldData.grossWeightCoe= glj.grossWeightCoe;
  783. oldData.purchaseStorageRate = glj.purchaseStorageRate;
  784. oldData.offSiteTransportLossRate = glj.offSiteTransportLossRate;
  785. oldData.handlingLossRate = glj.handlingLossRate;
  786. if (glj.hasOwnProperty("compilationId")) {
  787. oldData.from = "cpt";
  788. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  789. oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  790. }
  791. } else {
  792. oldData.from = "std";
  793. }
  794. return oldData;
  795. };
  796. ration_glj.prototype.replaceGLJ = function (selectCode, oldData, callback) {
  797. let allGLJ = gljOprObj.AllRecode;
  798. let glj = _.find(allGLJ, function (item) {
  799. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  800. return i_key == selectCode;
  801. });
  802. if (selectCode == gljOprObj.getIndex(oldData, gljKeyArray)) {
  803. return callback(null);
  804. }
  805. //如果是从混凝土改成商品混凝土,并且混凝土的定额消耗量不为空,则原混凝土的自定义消耗改成0,插入一条新的商品混凝土自定义消耗量为原定额计算后的消耗量
  806. if(gljUtil.isAddCommercialForReplace(oldData.type,glj.gljType,oldData.rationItemQuantity)){
  807. let originalID = oldData.ID;
  808. let customQuantity = gljUtil.isDef(oldData.quantity)? oldData.quantity:0;
  809. oldData = this.getAddDataByStd(glj,oldData.rationID,oldData.billsItemID,oldData.projectID);
  810. oldData.toCommercial=true;
  811. oldData.originalID = originalID;
  812. oldData.customQuantity = customQuantity;
  813. }else {
  814. oldData = this.getReplaceDataByStd(oldData,glj);
  815. }
  816. $.bootstrapLoading.start();
  817. CommonAjax.post("/rationGlj/replaceGLJ", oldData, callback, function () {
  818. $.bootstrapLoading.end();
  819. });
  820. };
  821. ration_glj.prototype.mReplaceGLJ = function (selectCode, oldData, callback) {
  822. let allGLJ = gljOprObj.AllRecode,tasks = [],updateMap={},doc = null;
  823. let oldIndex = gljOprObj.getIndex(oldData, gljKeyArray);
  824. let glj = _.find(allGLJ, function (item) {
  825. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  826. return i_key == selectCode;
  827. });
  828. if (selectCode == oldIndex) {
  829. return callback(null);
  830. }
  831. let query = {
  832. projectID: oldData.projectID,
  833. code: oldData.code,
  834. name: oldData.name,
  835. unit: oldData.unit,
  836. type: oldData.type
  837. };
  838. if (oldData.specs && oldData.specs != '') {
  839. query.specs = oldData.specs;
  840. }
  841. for(let d of this.datas){//查询出所有需替换的工料机
  842. if(!gljOprObj.scopeSelectedIDMap[d.billsItemID]) continue; //如果不在选中范围的,跳过
  843. let tem_index = gljOprObj.getIndex(d, gljKeyArray);
  844. if(tem_index == oldIndex){
  845. let tem_doc = {};
  846. //是否从混凝土改成商品混凝土,并且混凝土的定额消耗量不为空
  847. if(gljUtil.isAddCommercialForReplace(d.type,glj.gljType,d.rationItemQuantity)){
  848. tem_doc = {'customQuantity':'0'};
  849. //插入一条新的定额,这里的更新自定义消耗量操作由task完成了,不用像单条替换那样做标记,修改等
  850. let newDoc = this.getAddDataByStd(glj,d.rationID,d.billsItemID,d.projectID);
  851. let customQuantity = gljUtil.isDef(d.quantity)? d.quantity:0;
  852. newDoc.customQuantity = customQuantity;
  853. let tTask = {insertOne:{document: newDoc}};
  854. tasks.push(tTask);
  855. doc=newDoc//后台操作: 要根据nweDoc,插入项目工料机,修改insertOne里项目工料机ID,生成新的自身ID
  856. }else {
  857. tem_doc = this.getReplaceDataByStd(d,glj);
  858. if(gljUtil.isConcreteType(d.type)||gljUtil.isCommercialConcreteType(d.type)) tem_doc.isConcrete = true;//这里是在批量替换中有可能同时存在定额下的替换和添加的替换,对于添加的替换,要走替换混凝土的逻辑,定额下的替换又走另外的逻辑
  859. doc = tem_doc;
  860. }
  861. let task = {
  862. updateOne:{
  863. filter : {ID:d.ID,rationID:d.rationID},
  864. update : tem_doc
  865. }
  866. };
  867. tasks.push(task);
  868. updateMap[d.ID] = tem_doc;
  869. }
  870. }
  871. $.bootstrapLoading.start();
  872. CommonAjax.post("/rationGlj/mReplaceGLJ", {query: query, doc: doc,tasks:tasks}, function (result) {
  873. callback(result,updateMap);
  874. }, function () {
  875. $.bootstrapLoading.end();
  876. });
  877. };
  878. ration_glj.prototype.getMainAndEquGLJ = function (rationID) {
  879. let gljList = _.filter(this.datas, function (n) {
  880. return n.rationID == rationID && (n.type == gljType.MAIN_MATERIAL || n.type == gljType.EQUIPMENT)
  881. });
  882. gljList = gljUtil.sortRationGLJ(gljList);
  883. return gljOprObj.combineWithProjectGlj(gljList);
  884. };
  885. ration_glj.prototype.transferToNodeData = function (data) {
  886. data.contain=scMathUtil.roundForObj(data.quantity,getDecimal('glj.quantity'));
  887. data.subType = data.type;
  888. };
  889. ration_glj.prototype.combineRationAndGLJ = function (ration) {
  890. if (ration) {
  891. let projectGLJData = projectObj.project.projectGLJ.datas;
  892. let projectGljs = projectGLJData.gljList;
  893. let mixRatioMap = projectGLJData.mixRatioMap;
  894. let glj = _.find(projectGljs, {'id': ration.projectGLJID});
  895. if (glj) {
  896. if(projectObj.project.projectGLJ.isEstimateType(glj.type)){
  897. ration.isEstimate = glj.is_evaluate;
  898. }
  899. ration = gljOprObj.setGLJPrice(ration,glj);
  900. ration.isAdd = glj.unit_price.is_add;
  901. let connect_index = gljOprObj.getIndex(glj, gljKeyArray);
  902. if (mixRatioMap.hasOwnProperty(connect_index)) {
  903. let mixRatios = gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
  904. ration.subList = mixRatios;
  905. }
  906. }
  907. }
  908. return ration;
  909. };
  910. ration_glj.prototype.updateFromMainSpread = function (value, node, fieldName) {
  911. if (node.data[fieldName] !== value) {
  912. if (fieldName == "marketUnitFee") {
  913. let decimal = getDecimal("glj.unitPrice");
  914. let newval = number_util.checkNumberValue(value, decimal);
  915. if (newval) {
  916. fieldName = "marketPrice";
  917. projectObj.project.projectGLJ.updatePriceFromRG(node.data, fieldName, newval);
  918. return;
  919. }
  920. } else {
  921. if(fieldName == "contain"){
  922. if(value==null){
  923. value="";
  924. }else {
  925. let decimal = getDecimal("glj.quantity");
  926. value = number_util.checkNumberValue(value, decimal);
  927. fieldName="customQuantity";//填入自定义消耗
  928. }
  929. }
  930. if (value !== undefined && value !== null) {
  931. if (fieldName == "subType") {
  932. node.data.subType = value;
  933. fieldName = "type";//转换成更新工料机类型
  934. }
  935. this.updateRationGLJByEdit(node.data, fieldName, value, node);
  936. return;
  937. }
  938. }
  939. }
  940. // node.data.subType = value;
  941. projectObj.mainController.refreshTreeNode([node]);
  942. };
  943. ration_glj.prototype.refreshTreeNodeIfNeeded = function (data) {
  944. if (this.needShowToTree(data)) {
  945. this.transferToNodeData(data);
  946. gljOprObj.refreshTreeNode({"type": ModuleNames.ration_glj, "ID": data.ID});
  947. }
  948. };
  949. ration_glj.prototype.needShowToTree = function (data) {
  950. if ((data.type == gljType.MAIN_MATERIAL || data.type == gljType.EQUIPMENT) && projectObj.project.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  951. return true
  952. }
  953. return false
  954. };
  955. ration_glj.prototype.findGLJNodeByID = function (ID) {
  956. return projectObj.project.mainTree.findNode(ID);
  957. };
  958. ration_glj.prototype.findRationNodeByID = function (ID) {
  959. return projectObj.project.mainTree.findNode(ID);
  960. };
  961. ration_glj.prototype.reCalcWhenGLJChange = function (ration_glj) {//当改变定额工料机时,重新计算定额以及其父节点
  962. let node = this.findRationNodeByID(ration_glj.rationID);
  963. if (node) {
  964. node.changed = true;
  965. project.calcProgram.calcAndSave(node);
  966. }
  967. };
  968. ration_glj.prototype.reCalcWhenVvTaxChange = function () {
  969. let rationIDs = [];
  970. for(let g of this.datas){
  971. if(g.type == gljType.GENERAL_MACHINE) rationIDs.push(g.rationID);
  972. }
  973. this.calcRationsByIDs(_.uniq(rationIDs))
  974. };
  975. ration_glj.prototype.calcRationsByIDs = function (rationIDs) {
  976. let nodes = [];
  977. for(let ID of rationIDs){
  978. let node = projectObj.project.mainTree.findNode(ID);
  979. if(node) nodes.push(node)
  980. }
  981. project.calcProgram.calcNodesAndSave(nodes);
  982. };
  983. ration_glj.prototype.updateProportion = function (proportionList, projectID, rationID) {
  984. const postData = {
  985. proportionList,
  986. projectID,
  987. rationID
  988. };
  989. $.bootstrapLoading.start();
  990. CommonAjax.post('/rationGLJ/updateProportion', postData, function (rst) {
  991. proportionList.forEach(item => {
  992. const glj = zmhs_obj.stableSheetData.find(glj => glj.ID === item.ID);
  993. if (glj) {
  994. glj.adjustProportion = item.adjustProportion;
  995. }
  996. });
  997. zmhs_obj.refreshStableDataIfNeeded();
  998. zmhs_obj.refreshAfterUpdate(rst);
  999. $.bootstrapLoading.end();
  1000. }, function () {
  1001. zmhs_obj.refreshStableDataIfNeeded();
  1002. $.bootstrapLoading.end();
  1003. });
  1004. };
  1005. return new ration_glj(project);
  1006. }
  1007. };