ration_glj.js 45 KB

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