ration.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var Ration = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. var tools = {
  8. formatRationUpdateData: function (data) {
  9. let uData = JSON.parse(JSON.stringify(data));
  10. delete uData.feesIndex;
  11. delete uData.flagsIndex;
  12. // if (uData.fees) {
  13. // for (let fee of uData.fees) {
  14. // fee.unitFee = fee.unitFee.toFixed(2);
  15. // fee.totalFee = fee.totalFee.toFixed(2);
  16. // fee.tenderUnitFee = fee.tenderUnitFee.toFixed(2);
  17. // fee.tenderTotalFee = fee.tenderTotalFee.toFixed(2);
  18. // }
  19. // }
  20. return uData;
  21. }
  22. };
  23. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  24. var ration = function (proj) {
  25. this.project = proj;
  26. this.datas = null;
  27. var sourceType = ModuleNames.ration;
  28. this.getSourceType = function () {
  29. return sourceType;
  30. }
  31. proj.registerModule(ModuleNames.ration, this);
  32. var maxRationID = 0;
  33. this.getNewRationID = function () {
  34. return uuid.v1();
  35. //return maxRationID += 1;
  36. };
  37. this.maxRationID = function (maxID) {
  38. if (arguments.length === 0) {
  39. return maxRationID;
  40. } else {
  41. maxRationID = Math.max(maxID, maxRationID);
  42. }
  43. };
  44. };
  45. // prototype用于定义public方法
  46. ration.prototype.loadData = function (datas) {
  47. var that = this;
  48. this.datas = datas;
  49. // generate Fees & Flags Index,For View & Calculate
  50. this.datas.forEach(function (data) {
  51. data.feesIndex = {};
  52. data.fees.forEach(function (fee) {
  53. data.feesIndex[fee.fieldName] = fee;
  54. });
  55. data.flagsIndex = {};
  56. data.flags.forEach(function (flag) {
  57. data.flagsIndex[flag.fieldName] = flag;
  58. });
  59. that.maxRationID(data.ID);
  60. });
  61. };
  62. ration.prototype.setMaxID = function (ID) {
  63. this.maxRationID(ID);
  64. }
  65. // refresh after update
  66. ration.prototype.doAfterUpdate = function(err, data){
  67. if(data.stateRefresh){
  68. this.refreshAdjustState(data);
  69. }
  70. if(data.quantityRefresh){
  71. this.refreshQuantity(data);
  72. }
  73. };
  74. ration.prototype.refreshAdjustState = function(data){
  75. this.refreshDatas(data,'adjustState');
  76. if(data.hasOwnProperty('name')){
  77. this.refreshDatas(data,'name')
  78. }
  79. };
  80. ration.prototype.refreshQuantity = function(data){
  81. this.refreshDatas(data,'quantity');
  82. };
  83. ration.prototype.refreshDatas = function(data,fieldName){
  84. var dataIndex = _.findIndex(this.datas,function(item) {
  85. return item.ID ==data.rationID;
  86. });
  87. this.datas[dataIndex][fieldName] = data[fieldName];
  88. if(fieldName=='quantity'){
  89. this.datas[dataIndex]['isFromDetail']=1
  90. }
  91. var controller = projectObj.mainController;
  92. var selected = controller.sheet.getSelections();
  93. var col = _.findIndex(project.projSetting.main_tree_col.cols,function (col) {
  94. return col.data.field ==fieldName;
  95. });
  96. controller.sheet.getCell(selected[0].row,col).value(data[fieldName]);
  97. };
  98. ration.prototype.getTempRationData = function (id, billsID, serialNo, rType) {
  99. var newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};
  100. newData[project.masterField.ration] = billsID;
  101. newData['type'] = rType;
  102. if (rType == rationType.volumePrice){
  103. newData['subType'] = gljType.GENERAL_MATERIAL; // 默认的量价类型为材料
  104. newData['programID'] = projectInfoObj.projectInfo.property.engineering;
  105. };
  106. return newData;
  107. };
  108. ration.prototype.getBillsSortRation = function (billsID) { // 该方法只适用于叶子清单
  109. var rations = this.datas.filter(function (data) {
  110. return data[project.masterField.ration] === billsID;
  111. });
  112. rations.sort(function (x, y) {
  113. return x.serialNo - y.serialNo;
  114. });
  115. return rations;
  116. };
  117. // CSL, 2017-11-13 取任何清单(父清单、叶子清单)下的所有定额
  118. ration.prototype.getRationsByNode = function (billNode) {
  119. let rations = [];
  120. let sBills = 'bills';
  121. if (billNode.sourceType != sBills) return rations;
  122. let IDs = [];
  123. function getSubBillsIDs(node) {
  124. if (!node) return;
  125. if (node.sourceType != sBills) return;
  126. if (!node.children || node.children.length == 0 || node.children[0].sourceType != sBills)
  127. IDs.push(node.data.ID)
  128. else
  129. getSubBillsIDs(node.children[0]);
  130. getSubBillsIDs(node.nextSibling);
  131. };
  132. if (billNode.source.children.length == 0)
  133. IDs.push(billNode.data.ID)
  134. else
  135. getSubBillsIDs(billNode.children[0]);
  136. for (let id of IDs){
  137. let subRations = this.datas.filter(function (data) {
  138. return data[project.masterField.ration] === id;
  139. });
  140. rations.push(...subRations);
  141. };
  142. rations.sort(function (x, y) {
  143. return x.serialNo - y.serialNo;
  144. });
  145. return rations;
  146. };
  147. ration.prototype.getInsertRationData = function (billsID, preRation, rationType) {
  148. var br = this.getBillsSortRation(billsID);
  149. var updateData = [];
  150. if (preRation) {
  151. var preIndex = br.indexOf(preRation), i;
  152. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1, rationType)});
  153. for (i = preIndex + 1; i < br.length; i++) {
  154. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1, rationType)});
  155. }
  156. } else {
  157. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1, rationType)});
  158. }
  159. return updateData;
  160. };
  161. ration.prototype.getCounterData = function (count) {
  162. var updateData = {'projectID': this.project.ID()};
  163. if (count) {
  164. updateData[this.getSourceType()] = this.maxRationID() + count;
  165. } else {
  166. updateData[this.getSourceType()] = this.maxRationID() + 1;
  167. }
  168. return updateData;
  169. };
  170. ration.prototype.insertRation = function (billsID, preRation, rationType) {
  171. var br = this.getBillsSortRation(billsID);
  172. let insertData = this.getInsertRationData(billsID, preRation, rationType);
  173. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()],
  174. [insertData, this.getCounterData()]);
  175. var newRation = null;
  176. let newID = -1;
  177. for(let data of insertData){
  178. if(data.updateType === 'ut_create'){
  179. newID = data.updateData.ID;
  180. }
  181. }
  182. if (preRation) {
  183. var preIndex = br.indexOf(preRation), i;
  184. newRation = this.getTempRationData(newID, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1, rationType);
  185. this.datas.push(newRation);
  186. for (i = preIndex + 1; i < br.length; i++) {
  187. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  188. }
  189. } else {
  190. newRation = this.getTempRationData(newID, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1, rationType);
  191. this.datas.push(newRation);
  192. }
  193. return newRation;
  194. };
  195. ration.prototype.insertStdRation = function (billsID, preRation, std) {
  196. var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation, rationType.ration), newRation = null, that = this;
  197. updateData.forEach(function (data) {
  198. if (data.updateType === 'ut_create') {
  199. data.updateData.code = std.code;
  200. data.updateData.name = std.name;
  201. data.updateData.caption = std.caption;
  202. data.updateData.unit = std.unit;
  203. data.updateData.libID = std.rationRepId;
  204. data.updateData.content = std.jobContent;
  205. if (std.chapter) {
  206. data.updateData.comments = std.chapter.explanation;
  207. data.updateData.ruleText = std.chapter.ruleText;
  208. }
  209. data.updateData.from = std.type === 'complementary' ? 'cpt' : 'std';
  210. data.updateData.programID = std.feeType;
  211. data.updateData.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  212. // calculate ration Quantity
  213. that.CalculateQuantity(data.updateData);
  214. newRation = data.updateData;
  215. }
  216. });
  217. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()], [updateData, this.getCounterData()]);
  218. //newRation.ID = this.getNewRationID();
  219. if (preRation) {
  220. var preIndex = br.indexOf(preRation), i;
  221. this.datas.push(newRation);
  222. for (i = preIndex + 1; i < br.length; i++) {
  223. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  224. }
  225. } else {
  226. this.datas.push(newRation);
  227. }
  228. return newRation;
  229. };
  230. ration.prototype.getDeleteData = function (rationData) {
  231. var updateData = [];
  232. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': this.project.ID()}});
  233. return updateData;
  234. };
  235. ration.prototype.delete = function (ration) {
  236. var ration_glj =projectObj.project.ration_glj;
  237. this.project.pushNow('deleteRation', [this.getSourceType(),ration_glj.getSourceType()], [this.getDeleteData(ration),ration_glj.getDeleteDataByRation(ration)]);
  238. project.ration_glj.deleteByRation(ration);
  239. project.ration_coe.deleteByRation(ration);
  240. project.quantity_detail.deleteByRation(ration);
  241. this.datas.splice(this.datas.indexOf(ration), 1);
  242. };
  243. ration.prototype.getDeleteDataByBill = function (nodes) {
  244. let updateData = [];
  245. for (let node of nodes) {
  246. if (node.children.length > 0) {
  247. updateData = updateData.concat(this.getDeleteDataByBill(node.children));
  248. } else {
  249. let rations = this.getBillsSortRation(node.getID());
  250. for (let r of rations) {
  251. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': r.ID, 'projectID': this.project.ID()}});
  252. }
  253. }
  254. }
  255. return updateData;
  256. };
  257. ration.prototype.deleteByBills = function (nodes) {
  258. for (let node of nodes) {
  259. if (node.children.length > 0) {
  260. this.deleteByBills(node.children);
  261. } else {
  262. let rations = this.getBillsSortRation(node.getID());
  263. for (let r of rations) {
  264. this.datas.splice(this.datas.indexOf(r), 1);
  265. }
  266. }
  267. }
  268. }
  269. ration.prototype.getChangePosUpdateData = function (ration1, ration2) {
  270. var updateData = [];
  271. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration1.ID, ration1.billsItemID, ration2.serialNo)});
  272. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration2.ID, ration2.billsItemID, ration1.serialNo)});
  273. return updateData;
  274. };
  275. ration.prototype.changePos = function (ration1, ration2) {
  276. var updateData = this.getChangePosUpdateData(ration1, ration2);
  277. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  278. var preSerialNo = ration1.serialNo;
  279. ration1.serialNo = ration2.serialNo;
  280. ration2.serialNo = preSerialNo;
  281. };
  282. ration.prototype.updateField = function (ration, field, newValue) {
  283. calcFees.setFee(ration, field, newValue);
  284. let updateData = [];
  285. let data = {'ID': ration.ID, 'projectID': this.project.ID()};
  286. if (field === 'quantity') {
  287. data[field] = newValue;
  288. data.isFromDetail=0;
  289. // to do Calculate
  290. if (ration.fees) {
  291. data.fees = ration.fees;
  292. }
  293. } else if (field === 'feesIndex.common.unitFee') {
  294. // to do Calculate
  295. if (ration.fees) {
  296. data.fees = ration.fees;
  297. }
  298. } else {
  299. data[field] = newValue;
  300. }
  301. updateData.push({'updateType': 'ut_update', 'updateData': data});
  302. this.project.pushNow('updateBills', this.getSourceType(), updateData);
  303. };
  304. ration.prototype.updateRation = function (ration, updateNow) {
  305. let updateData = [];
  306. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatRationUpdateData(ration)});
  307. if (updateNow) {
  308. this.project.pushNow('updateRations', this.getSourceType(), updateData);
  309. } else {
  310. this.project.push(this.getSourceType(), updateData);
  311. }
  312. };
  313. ration.prototype.FilterNumberFromUnit = function (unit) {
  314. let reg = new RegExp('^[0-9]+');
  315. if (reg.test(unit)) {
  316. return parseInt(unit.match(reg)[0]);
  317. } else {
  318. return 1;
  319. }
  320. };
  321. ration.prototype.CalculateQuantity = function (ration) {
  322. // calculate ration Quantity
  323. let quantity_decimal = getDecimal("ration.quantity");
  324. let process_decimal = getDecimal("process");
  325. if (optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')) {
  326. let billsNode = this.project.Bills.tree.findNode(ration[this.project.masterField.ration]);
  327. let billsQuantity = billsNode.data.quantity ? billsNode.data.quantity : 0;
  328. ration.contain = 1;
  329. ration.quantityEXP="QDL";
  330. if (optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToRationUnit')) {
  331. ration.quantity = (billsQuantity / this.FilterNumberFromUnit(ration.unit)).toDecimal(quantity_decimal);
  332. } else {
  333. ration.quantity = billsQuantity.toDecimal(quantity_decimal);
  334. }
  335. }
  336. };
  337. ration.prototype.replaceRation = function (ration, std) {
  338. this.project.beginUpdate('replaceRation');
  339. // delete
  340. let ration_glj =projectObj.project.ration_glj;
  341. this.project.push(this.project.ration_glj.getSourceType(), this.project.ration_glj.getDeleteDataByRation(ration));
  342. this.project.ration_glj.deleteByRation(ration);
  343. this.project.ration_coe.deleteByRation(ration);
  344. this.project.quantity_detail.deleteByRation(ration);
  345. // insertNewRation
  346. let updateData = [];
  347. ration.code = std.code;
  348. ration.name = std.name;
  349. ration.caption = std.caption;
  350. ration.unit = std.unit;
  351. ration.libID = std.rationRepId;
  352. ration.content = std.jobContent;
  353. ration.adjustState = '';
  354. if (std.chapter) {
  355. ration.comments = std.chapter.explanation;
  356. ration.ruleText = std.chapter.ruleText;
  357. }
  358. ration.from = std.type === 'complementary' ? 'cpt' : 'std';
  359. ration.programID = std.feeType;
  360. ration.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  361. // calculate ration Quantity
  362. this.CalculateQuantity(ration);
  363. updateData.push({updateType: 'ut_update', updateData: ration});
  364. this.project.push(this.getSourceType(), updateData);
  365. this.project.endUpdate();
  366. };
  367. ration.prototype.updateContain=function (value,node) {
  368. if(isNaN(value)){
  369. alert("当前输入的数据类型不正确,请重新输入");
  370. projectObj.mainController.refreshTreeNode([node]);
  371. return;
  372. }
  373. let billNode = node.parent;
  374. let contain = scMathUtil.roundForObj(value,getDecimal("process"));
  375. if(node.data.quantityEXP=="GCLMXHJ"){//如果定额工程量是来自工程量明细
  376. var c = confirm('已有工程量明细,是否清空明细表,采用手工输入的表达式?');
  377. if(c){
  378. node.data.isFromDetail=0;
  379. project.quantity_detail.cleanQuantityDetail(node,true);
  380. }else {
  381. projectObj.mainController.refreshTreeNode([node]);
  382. return;
  383. }
  384. }
  385. let billQuantity = billNode.data.quantity||billNode.data.quantity!=""?billNode.data.quantity:0;
  386. billQuantity = parseFloat(billQuantity);
  387. node.data.contain = contain;
  388. node.data.quantityEXP="QDL*"+contain;
  389. node.data.quantity=scMathUtil.roundForObj(billQuantity*contain,getDecimal("quantity"),node);
  390. node.data.quantity = projectObj.project.quantity_detail.autoTransformQuantity(node.data.quantity,node);//按单位做转换
  391. node.changed = true;
  392. project.calcProgram.calcAndSave(node);
  393. projectObj.mainController.refreshTreeNode(node.children);//刷新子工料机树节点总消耗量
  394. };
  395. ration.prototype.addRationChecking=function(selected){
  396. if (selected) {// Vincent, 2018-01-02
  397. if(selected.sourceType === project.Ration.getSourceType()){ // 焦点行是定额/量价/工料机,有效显示。
  398. return false;
  399. }else if(selected.sourceType === project.Bills.getSourceType()){
  400. if(selected.data.type == billType.FX){//焦点行是分项,有效显示。
  401. return false
  402. }
  403. if(selected.data.type == billType.BILL && selected.source.children.length === 0){//焦点行是清单,且没有子项,有效显示。
  404. return false
  405. }
  406. }
  407. }
  408. return true;
  409. };
  410. return new ration(project);
  411. }
  412. };