ration_glj.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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 = gljUtil.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. grossWeightCoe:glj.grossWeightCoe,
  562. purchaseStorageRate:glj.purchaseStorageRate,
  563. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  564. handlingLossRate:glj.handlingLossRate,
  565. repositoryId: glj.repositoryId
  566. };
  567. if(pEngineer) new_glj.programID = pEngineer;
  568. if (glj.hasOwnProperty("compilationId")) {
  569. new_glj.from = "cpt";
  570. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  571. new_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  572. }
  573. }
  574. if(optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')){//需要根据清单转换工程量
  575. new_glj.quantityEXP="QDL";
  576. if(billNode.data.quantity){
  577. new_glj.quantity = scMathUtil.roundForObj(billNode.data.quantity/FilterNumberFromUnit(glj.unit),getDecimal("glj.quantity"));
  578. new_glj.contain = scMathUtil.roundForObj(new_glj.quantity/billNode.data.quantity,6);
  579. }
  580. }
  581. gljList.push(new_glj);
  582. }
  583. }
  584. if (gljList.length == 0) {
  585. return;
  586. }
  587. let postData = {
  588. gljList: gljList,
  589. projectID: parseInt(project.ID()),
  590. billsItemID: billsItemID,
  591. rationCount: project.Ration.maxRationID()
  592. }
  593. selectedSerialNo == null ? "" : postData.selectedSerialNo = selectedSerialNo;
  594. CommonAjax.post("/ration/insertGLJAsRation", postData, function (data) {
  595. // 更新兄弟节点的序列号
  596. if (selectedSerialNo != null) {
  597. let selectIndex = _.findIndex(children, {"serialNo": selectedSerialNo});
  598. if (selectIndex + 1 < children.length) {
  599. for (let i = selectIndex + 1; i < children.length; i++) {
  600. children[i].serialNo += gljList.length;
  601. }
  602. }
  603. }
  604. callback(parentNodeID,nextNodeID,data);
  605. }, function () {
  606. $.bootstrapLoading.end();
  607. });
  608. };
  609. ration_glj.prototype.getGLJDataByCodes = function (codes,callback) {
  610. if(!gljUtil.isDef(codes)||codes.length ==0) {
  611. if(callback) callback([]);
  612. return
  613. }
  614. $.bootstrapLoading.start();
  615. CommonAjax.post("/rationGlj/getGLJDataByCodes", { 'engineerID':projectInfoObj.projectInfo.property.engineering_id,projectID:projectObj.project.ID(),codes:codes}, function (result) {
  616. $.bootstrapLoading.end();
  617. callback(result)
  618. });
  619. };
  620. ration_glj.prototype.getAddDataByStd = function (glj,rationID,billsItemID,projectID) {//生成添加类型的定额工料机数据
  621. let ration_glj = {
  622. projectID: projectID,
  623. GLJID: glj.ID,
  624. rationID: rationID,
  625. billsItemID: billsItemID,
  626. rationItemQuantity: 0,
  627. quantity: 0,
  628. name: glj.name,
  629. code: glj.code,
  630. original_code: glj.code,
  631. unit: glj.unit,
  632. specs: glj.specs,
  633. basePrice: glj.basePrice,
  634. marketPrice:glj.basePrice,
  635. shortName: glj.shortName,
  636. type: glj.gljType,
  637. model:glj.model,
  638. adjCoe: glj.adjCoe,
  639. createType: 'add',
  640. materialType:glj.materialType,
  641. materialCoe:glj.materialCoe,
  642. grossWeightCoe:glj.grossWeightCoe,
  643. purchaseStorageRate:glj.purchaseStorageRate,
  644. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  645. handlingLossRate:glj.handlingLossRate,
  646. repositoryId: glj.repositoryId
  647. };
  648. if (glj.hasOwnProperty("compilationId")) {
  649. ration_glj.from = "cpt";
  650. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  651. ration_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  652. }
  653. }
  654. return ration_glj;
  655. };
  656. ration_glj.prototype.addGLJByLib = function (GLJSelection, ration, callback) {
  657. let me=this,gljList = [];
  658. let allGLJ = gljOprObj.AllRecode;
  659. GLJSelection.sort();
  660. _.forEach(GLJSelection, function (g) {
  661. let glj = _.find(allGLJ, function (item) {
  662. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  663. return i_key == g;
  664. });
  665. let ration_glj = me.getAddDataByStd(glj,ration.ID,ration.billsItemID,ration.projectID);
  666. gljList.push(ration_glj);
  667. });
  668. $.bootstrapLoading.start();
  669. CommonAjax.post("/rationGlj/addGLJ", gljList, callback, function () {
  670. $.bootstrapLoading.end();
  671. });
  672. };
  673. ration_glj.prototype.updateRationGLJByChangeCode = function (recode, updateField, newval) {
  674. let me = this;
  675. let priceMap = projectObj.project.projectGLJ.datas.unitPriceMap;
  676. let priceObj = priceMap[newval];
  677. if(priceObj){
  678. if (recode.createType===undefined || recode.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  679. recode.rcode = recode.code;
  680. recode.createType = 'replace';
  681. }
  682. recode.code = priceObj.code;
  683. recode.name = priceObj.name;
  684. recode.type = priceObj.type;
  685. recode.unit = priceObj.unit;
  686. recode.shortName = priceObj.short_name;
  687. recode.specs = priceObj.specs;
  688. recode.GLJID=priceObj.glj_id;
  689. recode.original_code = priceObj.original_code;
  690. recode.basePrice = priceObj.base_price;
  691. recode.marketPrice = priceObj.market_price;
  692. }
  693. $.bootstrapLoading.start();
  694. CommonAjax.post("/rationGlj/replaceGLJ", recode, function (result) {
  695. if (result) {
  696. //result.adjustState;
  697. let glj_list = me.datas;
  698. let data = result.data;
  699. let list_index = _.findIndex(glj_list, {'ID': data.ID});
  700. let rationNode = projectObj.project.mainTree.findNode(data.rationID);
  701. let nodes = [rationNode];
  702. glj_list[list_index] = data;
  703. project.projectGLJ.loadData(function () {//加载完项目工料机再计算
  704. gljOprObj.refreshView();
  705. if (project.ration_glj.needShowToTree(data)) {//当替换的是主材或设备时,刷新对应的树节点
  706. let node = project.ration_glj.findGLJNodeByID(data.ID);
  707. if (node) {
  708. project.ration_glj.transferToNodeData(data);
  709. node.source = data;
  710. node.data = data;
  711. }
  712. node ? nodes.push(node) : "";
  713. }
  714. rationNode.data.adjustState = result.adjustState;
  715. rationNode.data.name = result.name;
  716. projectObj.mainController.refreshTreeNode(nodes);
  717. project.calcProgram.calcAndSave(rationNode);
  718. $.bootstrapLoading.end();
  719. });
  720. }
  721. }, function () {
  722. $.bootstrapLoading.end();
  723. });
  724. };
  725. ration_glj.prototype.getReplaceDataByStd = function (oldData,glj) {
  726. if (oldData.createType===undefined || oldData.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  727. oldData.rcode = oldData.code;
  728. oldData.createType = 'replace';
  729. }
  730. if(oldData.createType == 'replace' && oldData.rcode == glj.code){//如果原数据已经是替换过的,这次替换又恢复成原数据,则把类型改回来
  731. oldData.createType = 'normal'
  732. }
  733. oldData.GLJID = glj.ID;
  734. oldData.name = glj.name;
  735. oldData.code = glj.code;
  736. oldData.original_code = glj.code;
  737. oldData.unit = glj.unit;
  738. oldData.specs = glj.specs;
  739. oldData.model = glj.model;
  740. oldData.basePrice = glj.basePrice;
  741. oldData.marketPrice = glj.basePrice;
  742. oldData.repositoryId = glj.repositoryId;
  743. oldData.materialType = glj.materialType;
  744. oldData. materialCoe = glj.materialCoe;
  745. oldData.grossWeightCoe= glj.grossWeightCoe;
  746. oldData.purchaseStorageRate = glj.purchaseStorageRate;
  747. oldData.offSiteTransportLossRate = glj.offSiteTransportLossRate;
  748. oldData.handlingLossRate = glj.handlingLossRate;
  749. if (glj.hasOwnProperty("compilationId")) {
  750. oldData.from = "cpt";
  751. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  752. oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  753. }
  754. } else {
  755. oldData.from = "std";
  756. }
  757. return oldData;
  758. };
  759. ration_glj.prototype.replaceGLJ = function (selectCode, oldData, callback) {
  760. let allGLJ = gljOprObj.AllRecode;
  761. let glj = _.find(allGLJ, function (item) {
  762. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  763. return i_key == selectCode;
  764. });
  765. if (selectCode == gljOprObj.getIndex(oldData, gljKeyArray)) {
  766. return callback(null);
  767. }
  768. oldData = this.getReplaceDataByStd(oldData,glj);
  769. $.bootstrapLoading.start();
  770. CommonAjax.post("/rationGlj/replaceGLJ", oldData, callback, function () {
  771. $.bootstrapLoading.end();
  772. });
  773. };
  774. ration_glj.prototype.mReplaceGLJ = function (selectCode, oldData, callback) {
  775. let allGLJ = gljOprObj.AllRecode,tasks = [],updateMap={};
  776. let oldIndex = gljOprObj.getIndex(oldData, gljKeyArray);
  777. let glj = _.find(allGLJ, function (item) {
  778. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  779. return i_key == selectCode;
  780. });
  781. if (selectCode == oldIndex) {
  782. return callback(null);
  783. }
  784. let query = {
  785. projectID: oldData.projectID,
  786. code: oldData.code,
  787. name: oldData.name,
  788. unit: oldData.unit,
  789. type: oldData.type
  790. }
  791. if (oldData.specs && oldData.specs != '') {
  792. query.specs = oldData.specs;
  793. }
  794. let doc = {
  795. GLJID: glj.ID,
  796. createType: 'replace',
  797. rationItemQuantity: 0,
  798. name: glj.name,
  799. code: glj.code,
  800. original_code: glj.code,
  801. unit: glj.unit,
  802. specs: glj.specs,
  803. type: glj.gljType,
  804. model:glj.model,
  805. basePrice: glj.basePrice,
  806. marketPrice:glj.basePrice,
  807. repositoryId: glj.repositoryId,
  808. materialType: glj.materialType, //三材类别
  809. materialCoe: glj.materialCoe,
  810. grossWeightCoe:glj.grossWeightCoe,
  811. purchaseStorageRate:glj.purchaseStorageRate,
  812. offSiteTransportLossRate:glj.offSiteTransportLossRate,
  813. handlingLossRate:glj.handlingLossRate,
  814. projectID: oldData.projectID
  815. };
  816. if (glj.hasOwnProperty("compilationId")) {
  817. doc.from = "cpt";
  818. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  819. doc.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  820. }
  821. } else {
  822. doc.from = "std";
  823. }
  824. for(let d of this.datas){//查询出所有需替换的工料机
  825. let tem_index = gljOprObj.getIndex(d, gljKeyArray);
  826. if(tem_index == oldIndex){
  827. let tem_doc = _.cloneDeep(doc);
  828. if(d.createType == 'replace'){
  829. tem_doc.rcode = d.rcode;
  830. }else if(d.createType == 'add'){//对于添加的类型,替换后还是添加类型
  831. tem_doc.createType = 'add';
  832. }else {
  833. tem_doc.rcode = d.code
  834. }
  835. let task = {
  836. updateOne:{
  837. filter : {ID:d.ID},
  838. update : tem_doc
  839. }
  840. };
  841. tasks.push(task);
  842. updateMap[d.ID] = tem_doc;
  843. }
  844. }
  845. $.bootstrapLoading.start();
  846. CommonAjax.post("/rationGlj/mReplaceGLJ", {query: query, doc: doc,tasks:tasks}, function (result) {
  847. callback(result,updateMap);
  848. }, function () {
  849. $.bootstrapLoading.end();
  850. });
  851. };
  852. ration_glj.prototype.getMainAndEquGLJ = function (rationID) {
  853. let gljList = _.filter(this.datas, function (n) {
  854. return n.rationID == rationID && (n.type == gljType.MAIN_MATERIAL || n.type == gljType.EQUIPMENT)
  855. });
  856. gljList = gljUtil.sortRationGLJ(gljList);
  857. return gljOprObj.combineWithProjectGlj(gljList);
  858. };
  859. ration_glj.prototype.transferToNodeData = function (data) {
  860. data.contain=scMathUtil.roundForObj(data.quantity,getDecimal('glj.quantity'));
  861. data.subType = data.type;
  862. };
  863. ration_glj.prototype.combineRationAndGLJ = function (ration) {
  864. if (ration) {
  865. let projectGLJData = projectObj.project.projectGLJ.datas;
  866. let projectGljs = projectGLJData.gljList;
  867. let mixRatioMap = projectGLJData.mixRatioMap;
  868. let glj = _.find(projectGljs, {'id': ration.projectGLJID});
  869. if (glj) {
  870. if(projectObj.project.projectGLJ.isEstimateType(glj.type)){
  871. ration.isEstimate = glj.is_evaluate;
  872. }
  873. ration = gljOprObj.setGLJPrice(ration,glj);
  874. ration.isAdd = glj.unit_price.is_add;
  875. let connect_index = gljOprObj.getIndex(glj, gljKeyArray);
  876. if (mixRatioMap.hasOwnProperty(connect_index)) {
  877. let mixRatios = gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
  878. ration.subList = mixRatios;
  879. }
  880. }
  881. }
  882. return ration;
  883. };
  884. ration_glj.prototype.updateFromMainSpread = function (value, node, fieldName) {
  885. if (node.data[fieldName] !== value) {
  886. if (fieldName == "marketUnitFee") {
  887. let decimal = getDecimal("glj.unitPrice");
  888. let newval = number_util.checkNumberValue(value, decimal);
  889. if (newval) {
  890. fieldName = "marketPrice";
  891. projectObj.project.projectGLJ.updatePriceFromRG(node.data, fieldName, newval);
  892. return;
  893. }
  894. } else {
  895. if(fieldName == "contain"){
  896. if(value==null){
  897. value="";
  898. }else {
  899. let decimal = getDecimal("glj.quantity");
  900. value = number_util.checkNumberValue(value, decimal);
  901. fieldName="customQuantity";//填入自定义消耗
  902. }
  903. }
  904. if (value !== undefined && value !== null) {
  905. if (fieldName == "subType") {
  906. node.data.subType = value;
  907. fieldName = "type";//转换成更新工料机类型
  908. }
  909. this.updateRationGLJByEdit(node.data, fieldName, value, node);
  910. return;
  911. }
  912. }
  913. }
  914. // node.data.subType = value;
  915. projectObj.mainController.refreshTreeNode([node]);
  916. };
  917. ration_glj.prototype.refreshTreeNodeIfNeeded = function (data) {
  918. if (this.needShowToTree(data)) {
  919. this.transferToNodeData(data);
  920. gljOprObj.refreshTreeNode({"type": ModuleNames.ration_glj, "ID": data.ID});
  921. }
  922. };
  923. ration_glj.prototype.needShowToTree = function (data) {
  924. if ((data.type == gljType.MAIN_MATERIAL || data.type == gljType.EQUIPMENT) && projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  925. return true
  926. }
  927. return false
  928. };
  929. ration_glj.prototype.findGLJNodeByID = function (ID) {
  930. return projectObj.project.mainTree.findNode(ID);
  931. };
  932. ration_glj.prototype.findRationNodeByID = function (ID) {
  933. return projectObj.project.mainTree.findNode(ID);
  934. };
  935. ration_glj.prototype.reCalcWhenGLJChange = function (ration_glj) {//当改变定额工料机时,重新计算定额以及其父节点
  936. let node = this.findRationNodeByID(ration_glj.rationID);
  937. if (node) {
  938. node.changed = true;
  939. project.calcProgram.calcAndSave(node);
  940. }
  941. };
  942. return new ration_glj(project);
  943. }
  944. };