ration_glj.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. }
  71. else{
  72. let rationGljs = projectObj.project.calcProgram.getGljArrByRation(ration);
  73. for (let glj of rationGljs) {
  74. let sameGlj = findGlj(glj, result);
  75. if (!sameGlj) {
  76. sameGlj = clone(glj);
  77. sameGlj.quantity = (sameGlj.quantity * ration.quantity).toDecimal(4);
  78. result.push(sameGlj);
  79. } else {
  80. sameGlj.quantity = sameGlj.quantity + (glj.quantity * ration.quantity).toDecimal(4);
  81. sameGlj.totalQuantity = (parseFloat(sameGlj.totalQuantity) + parseFloat(glj.totalQuantity)).toDecimal(4);
  82. }
  83. }
  84. }
  85. }
  86. result = gljOprObj.combineWithProjectGlj(result);
  87. if (!needOneBill) return result;
  88. // 上面取的是清单下所有工料机的总量,我要算清单单价,所以要取单位清单的工料机数量,所以下面要除以清单数量。
  89. let oneBill = JSON.parse(JSON.stringify(result));
  90. for (let glj of oneBill){
  91. if (glj.type == rationType.volumePrice){ // 量价、工料机形式的定额要进行数据变换才能参与基数计算。
  92. glj.type = glj.subType;
  93. glj.basePrice = glj.marketUnitFee;
  94. glj.marketPrice = glj.marketUnitFee;
  95. glj.adjustPrice = glj.marketUnitFee;
  96. }
  97. else if (glj.type == rationType.gljRation){
  98. glj.type = glj.subType;
  99. };
  100. // glj.quantity = (glj.quantity / billQuantity).toDecimal(decimalObj.glj.quantity);
  101. glj.quantity = (glj.quantity / billQuantity).toDecimal(decimalObj.process); // 广联达这里没有取舍
  102. };
  103. return oneBill;
  104. }
  105. // 提交数据后返回数据处理
  106. ration_glj.prototype.doAfterUpdate = function (err, data) {
  107. let me = this;
  108. if (!err) {
  109. if (data.updateTpye == 'ut_update') {
  110. me.refreshAfterUpdate(data);
  111. } else if (data.updateTpye == 'ut_delete') {
  112. me.refreshAfterDelete(data);
  113. } else {
  114. me.refreshAfterSave(data);
  115. }
  116. }
  117. projectObj.project.projectGLJ.loadData();
  118. };
  119. ration_glj.prototype.getDataByID = function (ID) {
  120. return _.find(this.datas,{'ID':ID});
  121. };
  122. ration_glj.prototype.refreshAfterSave = function (data) {
  123. let me = projectObj.project.ration_glj;
  124. let neRecodes = [];
  125. if (data) {
  126. // neRecodes=data.newRecords;//原来是显示和缓存分开的,后来发现会导致数据不一致的问题所以改成统一的了,这里也只是会作为显示。
  127. neRecodes = data.showDatas;
  128. gljOprObj.sheetData = neRecodes;
  129. me.addDatasToList(neRecodes);
  130. }
  131. project.projectGLJ.loadData(function () {
  132. gljOprObj.showRationGLJSheetData(true);
  133. //add to mainTree;
  134. me.addToMainTree(neRecodes);
  135. let rationID = neRecodes[0].rationID;
  136. let node = project.mainTree.nodes['id_' + rationID];
  137. if(isDef(node)){
  138. project.calcProgram.calcAndSave(node);
  139. }
  140. if (activeSubSheetIsCalcProgram())
  141. calcProgramObj.refreshCalcProgram(node, 1);
  142. });
  143. };
  144. ration_glj.prototype.getGljByRationID = function(rationID){
  145. return _.filter(this.datas, {'rationID': rationID});
  146. };
  147. ration_glj.prototype.addDatasToList = function (datas) {
  148. let me = projectObj.project.ration_glj;
  149. if(datas&&datas.length>0){
  150. if (me.datas && Array.isArray(me.datas)) {
  151. me.datas = me.datas.concat(datas);
  152. } else {
  153. me.datas = datas;
  154. }
  155. }
  156. };
  157. ration_glj.prototype.updateGLJNodeAfterReplace = function(data){
  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. } else {
  380. this.updateRationGLJByEdit(row, 'customQuantity', 0);
  381. //('ut_update',{'ID': row.ID, 'projectID': row.projectID},{'quantity':0,'customQuantity':0});
  382. }
  383. };
  384. ration_glj.prototype.getUpdateData = function (type, query, doc, callfunction) {
  385. let updateData = [];
  386. let newobj = {
  387. 'updateType': type,
  388. 'query': query,
  389. }
  390. if (doc) {
  391. newobj['doc'] = doc;
  392. }
  393. if (callfunction) {
  394. newobj['updateFunction'] = callfunction;
  395. }
  396. updateData.push(newobj);
  397. return updateData;
  398. };
  399. ration_glj.prototype.updateRationGLJByEdit = function (recode, updateField, newval, node) {
  400. let me = this,doc = {};
  401. doc[updateField] = newval;
  402. me.submitChange(recode,doc,updateField,node);
  403. };
  404. ration_glj.prototype.submitChange = function(recode,updateData,updateField,node){
  405. let me = this;
  406. let query = {
  407. 'ID': recode.ID,
  408. 'projectID': recode.projectID,
  409. 'rationID': recode.rationID
  410. };
  411. let priceInfo = {
  412. base_price: recode.basePrice,
  413. market_price: recode.marketPrice
  414. };
  415. if(updateData['type'] != undefined || updateData['type'] != null){
  416. updateData.shortName = projectObj.project.projectGLJ.getShortNameByID(updateData['type']);
  417. }
  418. let callback = function (data) {
  419. if (updateField == 'customQuantity') {
  420. me.refreshAfterQuantityUpdate(data, node);
  421. } else {
  422. let doc = data.doc;
  423. for (let key in doc) {
  424. recode[key] = doc[key];
  425. }
  426. me.refreshRationAfterEdit(data,recode.rationID, node);//更新名称和定额调整状态
  427. if (node) {//如果不是在造价书页面直接编辑,则不用刷新
  428. if (updateField == "type" && !(updateData['type'] == gljType.MAIN_MATERIAL || updateData['type'] == gljType.EQUIPMENT)) {//如果改变类型后不是主材或设备,则在造价书树中移除
  429. projectObj.mainController.deleteNode(node, true);
  430. }
  431. }
  432. }
  433. projectObj.project.projectGLJ.loadData(function () {//等项目工料机加载完成后再给用户编辑
  434. gljOprObj.refreshView();
  435. me.refreshTreeNodeIfNeeded(recode);//刷新造价书上的树节点(如果需要)
  436. me.reCalcWhenGLJChange(recode);//触发计算定额以及父节点
  437. $.bootstrapLoading.end();
  438. installationFeeObj.calcInstallationFee();
  439. });
  440. };
  441. $.bootstrapLoading.start();
  442. CommonAjax.post("/rationGlj/updateRationGLJByEdit", {
  443. query: query,
  444. doc: updateData,
  445. priceInfo: priceInfo
  446. }, callback, function (err) {
  447. $.bootstrapLoading.end();
  448. });
  449. };
  450. ration_glj.prototype.refreshAfterQuantityUpdate = function (data, node) {
  451. let me = this;
  452. data.glj_result.forEach(function (item) {
  453. me.refreshEachItme(item.doc, item.query);
  454. });
  455. me.refreshRationAfterEdit(data, data.rationID, node);
  456. };
  457. ration_glj.prototype.refreshRationAfterEdit= function(data,rationID,rnode){
  458. let nodes = [];
  459. let node = this.refreshRationNode(data.name,data.adjustState,rationID);
  460. if (node) nodes.push(node);
  461. if (rnode) nodes.push(rnode);
  462. projectObj.mainController.refreshTreeNode(nodes);
  463. };
  464. ration_glj.prototype.refreshRationNode = function(name,adjustState,rationID){
  465. let node = projectObj.project.mainTree.findNode(rationID);
  466. if (!node) return null;
  467. if(gljUtil.isDef(adjustState)){
  468. node.data.adjustState = adjustState;
  469. }
  470. if(gljUtil.isDef(name)){
  471. node.data.name = name;
  472. }
  473. return node
  474. };
  475. ration_glj.prototype.getGLJData = function (cb) {
  476. let property = projectObj.project.projectInfo.property;
  477. let engineerID = property.engineering_id;
  478. CommonAjax.get('/rationGlj/getGLJData/'+engineerID, function (data) {
  479. //编办中有多单价设置
  480. if(data.datas.priceProperties && data.datas.priceProperties.length > 0){
  481. let tem = _.find(data.datas.priceProperties,{region:property.region,taxModel:parseInt(property.taxType)});
  482. if(tem){
  483. let dataCode = tem.price.dataCode;
  484. for(let glj of data.datas.stdGLJ){
  485. if(glj.priceProperty && gljUtil.isDef(glj.priceProperty[dataCode])) glj.basePrice = glj.priceProperty[dataCode];
  486. }
  487. }
  488. };
  489. cb(data);
  490. })
  491. };
  492. ration_glj.prototype.insertGLJAsRation = function (GLJSelection, selected, callback) {
  493. let gljList = [];
  494. let allGLJ = gljOprObj.AllRecode;
  495. let billsItemID = null, serialNo = 0,selectedSerialNo = null,nextNodeID = null, parentNodeID = null, billNode = null;
  496. let pEngineer = projectObj.project.projectInfo.property.projectEngineering;
  497. let children = [];
  498. if (selected.sourceType === project.Bills.getSourceType()) {
  499. billsItemID = selected.data.ID;
  500. parentNodeID = selected.getID();
  501. nextNodeID = selected.tree.rootID();
  502. billNode = selected;
  503. } else {
  504. billsItemID = selected.data.billsItemID;
  505. serialNo = selected.data.serialNo;
  506. selectedSerialNo = selected.data.serialNo;
  507. nextNodeID = selected.getNextSiblingID();
  508. parentNodeID = selected.getParentID();
  509. billNode = selected.parent;
  510. }
  511. children = project.Ration.getBillsSortRation(billsItemID);
  512. serialNo == 0 ? serialNo = children.length : "";
  513. for (let con_key of GLJSelection) {
  514. let glj = _.find(allGLJ, function (item) {
  515. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  516. return i_key == con_key;
  517. });
  518. if (glj) {
  519. serialNo += 1;
  520. let new_glj = {
  521. ID: project.Ration.getNewRationID(),
  522. projectID: parseInt(project.ID()),
  523. billsItemID: billsItemID,
  524. type: rationType.gljRation,
  525. code: glj.code,
  526. name: glj.name,
  527. quantity: 0,
  528. unit: glj.unit,
  529. specs: glj.specs,
  530. subType: glj.gljType,
  531. model:glj.model,
  532. basePrice: glj.basePrice,
  533. marketPrice:glj.basePrice,
  534. original_code: glj.code,
  535. shortName: glj.shortName,
  536. serialNo: serialNo,
  537. GLJID: glj.ID,
  538. adjCoe: glj.adjCoe,
  539. materialType:glj.materialType,
  540. materialCoe:glj.materialCoe,
  541. materialIndexType:glj.materialIndexType,
  542. materialIndexUnit:glj.materialIndexUnit,
  543. materialIndexCoe:glj.materialIndexCoe,
  544. repositoryId: glj.repositoryId
  545. };
  546. if(pEngineer) new_glj.programID = pEngineer;
  547. if (glj.hasOwnProperty("compilationId")) {
  548. new_glj.from = "cpt";
  549. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  550. new_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  551. }
  552. }
  553. if(optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')){//需要根据清单转换工程量
  554. new_glj.quantityEXP="QDL";
  555. if(billNode.data.quantity){
  556. new_glj.quantity = scMathUtil.roundForObj(billNode.data.quantity/FilterNumberFromUnit(glj.unit),getDecimal("glj.quantity"));
  557. new_glj.contain = scMathUtil.roundForObj(new_glj.quantity/billNode.data.quantity,6);
  558. }
  559. }
  560. gljList.push(new_glj);
  561. }
  562. }
  563. if (gljList.length == 0) {
  564. return;
  565. }
  566. let postData = {
  567. gljList: gljList,
  568. projectID: parseInt(project.ID()),
  569. billsItemID: billsItemID,
  570. rationCount: project.Ration.maxRationID()
  571. }
  572. selectedSerialNo == null ? "" : postData.selectedSerialNo = selectedSerialNo;
  573. CommonAjax.post("/ration/insertGLJAsRation", postData, function (data) {
  574. // 更新兄弟节点的序列号
  575. if (selectedSerialNo != null) {
  576. let selectIndex = _.findIndex(children, {"serialNo": selectedSerialNo});
  577. if (selectIndex + 1 < children.length) {
  578. for (let i = selectIndex + 1; i < children.length; i++) {
  579. children[i].serialNo += gljList.length;
  580. }
  581. }
  582. }
  583. callback(parentNodeID,nextNodeID,data);
  584. }, function () {
  585. $.bootstrapLoading.end();
  586. });
  587. };
  588. ration_glj.prototype.getGLJDataByCodes = function (codes,callback) {
  589. if(!gljUtil.isDef(codes)||codes.length ==0) {
  590. if(callback) callback([]);
  591. return
  592. }
  593. $.bootstrapLoading.start();
  594. CommonAjax.post("/rationGlj/getGLJDataByCodes", { 'engineerID':projectObj.project.projectInfo.property.engineering_id,projectID:projectObj.project.ID(),codes:codes}, function (result) {
  595. $.bootstrapLoading.end();
  596. callback(result)
  597. });
  598. };
  599. ration_glj.prototype.getAddDataByStd = function (glj,rationID,billsItemID,projectID) {//生成添加类型的定额工料机数据
  600. let ration_glj = {
  601. projectID: projectID,
  602. GLJID: glj.ID,
  603. rationID: rationID,
  604. billsItemID: billsItemID,
  605. rationItemQuantity: 0,
  606. quantity: 0,
  607. name: glj.name,
  608. code: glj.code,
  609. original_code: glj.code,
  610. unit: glj.unit,
  611. specs: glj.specs,
  612. basePrice: glj.basePrice,
  613. marketPrice:glj.basePrice,
  614. shortName: glj.shortName,
  615. type: glj.gljType,
  616. model:glj.model,
  617. adjCoe: glj.adjCoe,
  618. createType: 'add',
  619. materialType:glj.materialType,
  620. materialCoe:glj.materialCoe,
  621. materialIndexType:glj.materialIndexType,
  622. materialIndexUnit:glj.materialIndexUnit,
  623. materialIndexCoe:glj.materialIndexCoe,
  624. repositoryId: glj.repositoryId
  625. };
  626. if (glj.hasOwnProperty("compilationId")) {
  627. ration_glj.from = "cpt";
  628. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改名称、规格、型号等保存到补充工料机库的
  629. ration_glj.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  630. }
  631. }
  632. return ration_glj;
  633. };
  634. ration_glj.prototype.addGLJByLib = function (GLJSelection, ration, callback) {
  635. let me=this,gljList = [];
  636. let allGLJ = gljOprObj.AllRecode;
  637. GLJSelection.sort();
  638. _.forEach(GLJSelection, function (g) {
  639. let glj = _.find(allGLJ, function (item) {
  640. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  641. return i_key == g;
  642. });
  643. let ration_glj = me.getAddDataByStd(glj,ration.ID,ration.billsItemID,ration.projectID);
  644. gljList.push(ration_glj);
  645. });
  646. $.bootstrapLoading.start();
  647. CommonAjax.post("/rationGlj/addGLJ", gljList, callback, function () {
  648. $.bootstrapLoading.end();
  649. });
  650. };
  651. ration_glj.prototype.updateRationGLJByChangeCode = function (recode, updateField, newval) {
  652. let me = this;
  653. let priceMap = projectObj.project.projectGLJ.datas.unitPriceMap;
  654. let priceObj = priceMap[newval];
  655. if(priceObj){
  656. if (recode.createType===undefined || recode.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  657. recode.rcode = recode.code;
  658. recode.createType = 'replace';
  659. }
  660. recode.code = priceObj.code;
  661. recode.name = priceObj.name;
  662. recode.type = priceObj.type;
  663. recode.unit = priceObj.unit;
  664. recode.shortName = priceObj.short_name;
  665. recode.specs = priceObj.specs;
  666. recode.GLJID=priceObj.glj_id;
  667. recode.original_code = priceObj.original_code;
  668. recode.basePrice = priceObj.base_price;
  669. recode.marketPrice = priceObj.market_price;
  670. }
  671. $.bootstrapLoading.start();
  672. CommonAjax.post("/rationGlj/replaceGLJ", recode, function (result) {
  673. if (result) {
  674. //result.adjustState;
  675. let glj_list = me.datas;
  676. let data = result.data;
  677. let list_index = _.findIndex(glj_list, {'ID': data.ID});
  678. let rationNode = projectObj.project.mainTree.findNode(data.rationID);
  679. let nodes = [rationNode];
  680. glj_list[list_index] = data;
  681. project.projectGLJ.loadData(function () {//加载完项目工料机再计算
  682. gljOprObj.refreshView();
  683. if (project.ration_glj.needShowToTree(data)) {//当替换的是主材或设备时,刷新对应的树节点
  684. let node = project.ration_glj.findGLJNodeByID(data.ID);
  685. if (node) {
  686. project.ration_glj.transferToNodeData(data);
  687. node.source = data;
  688. node.data = data;
  689. }
  690. node ? nodes.push(node) : "";
  691. }
  692. rationNode.data.adjustState = result.adjustState;
  693. rationNode.data.name = result.name;
  694. projectObj.mainController.refreshTreeNode(nodes);
  695. project.calcProgram.calcAndSave(rationNode);
  696. $.bootstrapLoading.end();
  697. });
  698. }
  699. }, function () {
  700. $.bootstrapLoading.end();
  701. });
  702. };
  703. ration_glj.prototype.getReplaceDataByStd = function (oldData,glj) {
  704. if (oldData.createType===undefined || oldData.createType == 'normal') {// createTypel 默认是normal 只有是定额下带的工料机才需把类型改成替换,其它的保持不变
  705. oldData.rcode = oldData.code;
  706. oldData.createType = 'replace';
  707. }
  708. if(oldData.createType == 'replace' && oldData.rcode == glj.code){//如果原数据已经是替换过的,这次替换又恢复成原数据,则把类型改回来
  709. oldData.createType = 'normal'
  710. }
  711. oldData.GLJID = glj.ID;
  712. oldData.name = glj.name;
  713. oldData.code = glj.code;
  714. oldData.original_code = glj.code;
  715. oldData.unit = glj.unit;
  716. oldData.specs = glj.specs;
  717. oldData.model = glj.model;
  718. oldData.basePrice = glj.basePrice;
  719. oldData.marketPrice = glj.basePrice;
  720. oldData.repositoryId = glj.repositoryId;
  721. oldData.materialType = glj.materialType;
  722. oldData. materialCoe = glj.materialCoe;
  723. oldData. materialIndexType = glj.materialIndexType;
  724. oldData. materialIndexUnit = glj.materialIndexUnit;
  725. oldData. materialIndexCoe = glj.materialIndexCoe;
  726. if (glj.hasOwnProperty("compilationId")) {
  727. oldData.from = "cpt";
  728. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  729. oldData.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  730. }
  731. } else {
  732. oldData.from = "std";
  733. }
  734. return oldData;
  735. };
  736. ration_glj.prototype.replaceGLJ = function (selectCode, oldData, callback) {
  737. let allGLJ = gljOprObj.AllRecode;
  738. let glj = _.find(allGLJ, function (item) {
  739. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  740. return i_key == selectCode;
  741. });
  742. if (selectCode == gljOprObj.getIndex(oldData, gljKeyArray)) {
  743. return callback(null);
  744. }
  745. oldData = this.getReplaceDataByStd(oldData,glj);
  746. $.bootstrapLoading.start();
  747. CommonAjax.post("/rationGlj/replaceGLJ", oldData, callback, function () {
  748. $.bootstrapLoading.end();
  749. });
  750. };
  751. ration_glj.prototype.mReplaceGLJ = function (selectCode, oldData, callback) {
  752. let allGLJ = gljOprObj.AllRecode,tasks = [],updateMap={};
  753. let oldIndex = gljOprObj.getIndex(oldData, gljKeyArray);
  754. let glj = _.find(allGLJ, function (item) {
  755. let i_key = gljOprObj.getIndex(item, gljLibKeyArray);
  756. return i_key == selectCode;
  757. });
  758. if (selectCode == oldIndex) {
  759. return callback(null);
  760. }
  761. let query = {
  762. projectID: oldData.projectID,
  763. code: oldData.code,
  764. name: oldData.name,
  765. unit: oldData.unit,
  766. type: oldData.type
  767. }
  768. if (oldData.specs && oldData.specs != '') {
  769. query.specs = oldData.specs;
  770. }
  771. let doc = {
  772. GLJID: glj.ID,
  773. createType: 'replace',
  774. rationItemQuantity: 0,
  775. name: glj.name,
  776. code: glj.code,
  777. original_code: glj.code,
  778. unit: glj.unit,
  779. specs: glj.specs,
  780. type: glj.gljType,
  781. model:glj.model,
  782. basePrice: glj.basePrice,
  783. marketPrice:glj.basePrice,
  784. repositoryId: glj.repositoryId,
  785. materialType: glj.materialType, //三材类别
  786. materialCoe: glj.materialCoe,
  787. materialIndexType: glj.materialIndexType,
  788. materialIndexUnit: glj.materialIndexUnit,
  789. materialIndexCoe: glj.materialIndexCoe,
  790. projectID: oldData.projectID
  791. };
  792. if (glj.hasOwnProperty("compilationId")) {
  793. doc.from = "cpt";
  794. if (glj.code.indexOf('-') != -1) {//这条工料机是用户通过修改包称、规格、型号等保存到补充工料机库的
  795. doc.original_code = glj.code.split('-')[0];//取-前的编号作为原始编号
  796. }
  797. } else {
  798. doc.from = "std";
  799. }
  800. for(let d of this.datas){//查询出所有需替换的工料机
  801. if(!gljOprObj.scopeSelectedIDMap[d.billsItemID]) continue; //如果不在选中范围的,跳过
  802. let tem_index = gljOprObj.getIndex(d, gljKeyArray);
  803. if(tem_index == oldIndex){
  804. let tem_doc = _.cloneDeep(doc);
  805. if(d.createType == 'replace'){
  806. tem_doc.rcode = d.rcode;
  807. }else if(d.createType == 'add'){//对于添加的类型,替换后还是添加类型
  808. tem_doc.createType = 'add';
  809. }else {
  810. tem_doc.rcode = d.code
  811. }
  812. let task = {
  813. updateOne:{
  814. filter : {ID:d.ID},
  815. update : tem_doc
  816. }
  817. };
  818. tasks.push(task);
  819. updateMap[d.ID] = tem_doc;
  820. }
  821. }
  822. $.bootstrapLoading.start();
  823. CommonAjax.post("/rationGlj/mReplaceGLJ", {query: query, doc: doc,tasks:tasks}, function (result) {
  824. callback(result,updateMap);
  825. }, function () {
  826. $.bootstrapLoading.end();
  827. });
  828. };
  829. ration_glj.prototype.getMainAndEquGLJ = function (rationID) {
  830. let gljList = _.filter(this.datas, function (n) {
  831. return n.rationID == rationID && (n.type == gljType.MAIN_MATERIAL || n.type == gljType.EQUIPMENT)
  832. });
  833. gljList = gljUtil.sortRationGLJ(gljList);
  834. return gljOprObj.combineWithProjectGlj(gljList);
  835. };
  836. ration_glj.prototype.transferToNodeData = function (data) {
  837. data.contain=scMathUtil.roundForObj(data.quantity,getDecimal('glj.quantity'));
  838. data.subType = data.type;
  839. };
  840. ration_glj.prototype.combineRationAndGLJ = function (ration) {
  841. if (ration) {
  842. let projectGLJData = projectObj.project.projectGLJ.datas;
  843. let projectGljs = projectGLJData.gljList;
  844. let mixRatioMap = projectGLJData.mixRatioMap;
  845. let glj = _.find(projectGljs, {'id': ration.projectGLJID});
  846. if (glj) {
  847. if(projectObj.project.projectGLJ.isEstimateType(glj.type)){
  848. ration.isEstimate = glj.is_evaluate;
  849. }
  850. ration = gljOprObj.setGLJPrice(ration,glj);
  851. ration.isAdd = glj.unit_price.is_add;
  852. let connect_index = gljOprObj.getIndex(glj, gljKeyArray);
  853. if (mixRatioMap.hasOwnProperty(connect_index)) {
  854. let mixRatios = gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
  855. ration.subList = mixRatios;
  856. }
  857. }
  858. }
  859. return ration;
  860. };
  861. ration_glj.prototype.updateFromMainSpread = function (value, node, fieldName) {
  862. if (node.data[fieldName] !== value) {
  863. if (fieldName == "marketUnitFee") {
  864. let decimal = getDecimal("glj.unitPrice");
  865. let newval = number_util.checkNumberValue(value, decimal);
  866. if (newval) {
  867. fieldName = "marketPrice";
  868. projectObj.project.projectGLJ.updatePriceFromRG(node.data, fieldName, newval);
  869. return;
  870. }
  871. } else {
  872. if(fieldName == "contain"){
  873. if(value==null){
  874. value="";
  875. }else {
  876. let decimal = getDecimal("glj.quantity");
  877. value = number_util.checkNumberValue(value, decimal);
  878. fieldName="customQuantity";//填入自定义消耗
  879. }
  880. }
  881. if (value !== undefined && value !== null) {
  882. if (fieldName == "subType") {
  883. node.data.subType = value;
  884. fieldName = "type";//转换成更新工料机类型
  885. }
  886. this.updateRationGLJByEdit(node.data, fieldName, value, node);
  887. return;
  888. }
  889. }
  890. }
  891. // node.data.subType = value;
  892. projectObj.mainController.refreshTreeNode([node]);
  893. };
  894. ration_glj.prototype.refreshTreeNodeIfNeeded = function (data) {
  895. if (this.needShowToTree(data)) {
  896. this.transferToNodeData(data);
  897. gljOprObj.refreshTreeNode({"type": ModuleNames.ration_glj, "ID": data.ID});
  898. }
  899. };
  900. ration_glj.prototype.needShowToTree = function (data) {
  901. if ((data.type == gljType.MAIN_MATERIAL || data.type == gljType.EQUIPMENT) && projectObj.project.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  902. return true
  903. }
  904. return false
  905. };
  906. ration_glj.prototype.findGLJNodeByID = function (ID) {
  907. return projectObj.project.mainTree.findNode(ID);
  908. };
  909. ration_glj.prototype.findRationNodeByID = function (ID) {
  910. return projectObj.project.mainTree.findNode(ID);
  911. };
  912. ration_glj.prototype.reCalcWhenGLJChange = function (ration_glj) {//当改变定额工料机时,重新计算定额以及其父节点
  913. let node = this.findRationNodeByID(ration_glj.rationID);
  914. if (node) {
  915. node.changed = true;
  916. project.calcProgram.calcAndSave(node);
  917. }
  918. };
  919. return new ration_glj(project);
  920. }
  921. };