ration_glj.js 43 KB

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