ration.js 19 KB

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