ration.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. return newData;
  104. };
  105. ration.prototype.getBillsSortRation = function (billsID) { // 该方法只适用于叶子清单
  106. var rations = this.datas.filter(function (data) {
  107. return data[project.masterField.ration] === billsID;
  108. });
  109. rations.sort(function (x, y) {
  110. return x.serialNo - y.serialNo;
  111. });
  112. return rations;
  113. };
  114. // CSL, 2017-11-13 取任何清单(父清单、叶子清单)下的所有定额
  115. ration.prototype.getRationsByNode = function (billNode) {
  116. let rations = [];
  117. let sBills = 'bills';
  118. if (billNode.sourceType != sBills) return rations;
  119. let IDs = [];
  120. function getSubBillsIDs(node) {
  121. if (!node) return;
  122. if (node.sourceType != sBills) return;
  123. if (!node.children || node.children.length == 0 || node.children[0].sourceType != sBills)
  124. IDs.push(node.data.ID)
  125. else
  126. getSubBillsIDs(node.children[0]);
  127. getSubBillsIDs(node.nextSibling);
  128. };
  129. if (billNode.source.children.length == 0)
  130. IDs.push(billNode.data.ID)
  131. else
  132. getSubBillsIDs(billNode.children[0]);
  133. for (let id of IDs){
  134. let subRations = this.datas.filter(function (data) {
  135. return data[project.masterField.ration] === id;
  136. });
  137. rations.push(...subRations);
  138. };
  139. rations.sort(function (x, y) {
  140. return x.serialNo - y.serialNo;
  141. });
  142. return rations;
  143. };
  144. ration.prototype.getInsertRationData = function (billsID, preRation, rationType) {
  145. var br = this.getBillsSortRation(billsID);
  146. var updateData = [];
  147. if (preRation) {
  148. var preIndex = br.indexOf(preRation), i;
  149. 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)});
  150. for (i = preIndex + 1; i < br.length; i++) {
  151. 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)});
  152. }
  153. } else {
  154. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1, rationType)});
  155. }
  156. return updateData;
  157. };
  158. ration.prototype.getCounterData = function (count) {
  159. var updateData = {'projectID': this.project.ID()};
  160. if (count) {
  161. updateData[this.getSourceType()] = this.maxRationID() + count;
  162. } else {
  163. updateData[this.getSourceType()] = this.maxRationID() + 1;
  164. }
  165. return updateData;
  166. };
  167. ration.prototype.insertRation = function (billsID, preRation, rationType) {
  168. var br = this.getBillsSortRation(billsID);
  169. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()],
  170. [this.getInsertRationData(billsID, preRation, rationType), this.getCounterData()]);
  171. var newRation = null;
  172. if (preRation) {
  173. var preIndex = br.indexOf(preRation), i;
  174. newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1, rationType);
  175. this.datas.push(newRation);
  176. for (i = preIndex + 1; i < br.length; i++) {
  177. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  178. }
  179. } else {
  180. newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1, rationType);
  181. this.datas.push(newRation);
  182. }
  183. return newRation;
  184. };
  185. ration.prototype.insertStdRation = function (billsID, preRation, std) {
  186. var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation, rationType.ration), newRation = null, that = this;
  187. updateData.forEach(function (data) {
  188. if (data.updateType === 'ut_create') {
  189. data.updateData.code = std.code;
  190. data.updateData.name = std.name;
  191. data.updateData.caption = std.caption;
  192. data.updateData.unit = std.unit;
  193. data.updateData.libID = std.rationRepId;
  194. data.updateData.content = std.jobContent;
  195. if (std.chapter) {
  196. data.updateData.comments = std.chapter.explanation;
  197. data.updateData.ruleText = std.chapter.ruleText;
  198. }
  199. data.updateData.programID = std.feeType;
  200. data.updateData.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  201. // calculate ration Quantity
  202. that.CalculateQuantity(data.updateData);
  203. newRation = data.updateData;
  204. }
  205. });
  206. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()], [updateData, this.getCounterData()]);
  207. newRation.ID = this.getNewRationID();
  208. if (preRation) {
  209. var preIndex = br.indexOf(preRation), i;
  210. this.datas.push(newRation);
  211. for (i = preIndex + 1; i < br.length; i++) {
  212. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  213. }
  214. } else {
  215. this.datas.push(newRation);
  216. }
  217. return newRation;
  218. };
  219. ration.prototype.getDeleteData = function (rationData) {
  220. var updateData = [];
  221. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': this.project.ID()}});
  222. return updateData;
  223. };
  224. ration.prototype.delete = function (ration) {
  225. var ration_glj =projectObj.project.ration_glj;
  226. this.project.pushNow('deleteRation', [this.getSourceType(),ration_glj.getSourceType()], [this.getDeleteData(ration),ration_glj.getDeleteDataByRation(ration)]);
  227. project.ration_glj.deleteByRation(ration);
  228. project.ration_coe.deleteByRation(ration);
  229. project.quantity_detail.deleteByRation(ration);
  230. this.datas.splice(this.datas.indexOf(ration), 1);
  231. };
  232. ration.prototype.getDeleteDataByBill = function (nodes) {
  233. let updateData = [];
  234. for (let node of nodes) {
  235. if (node.children.length > 0) {
  236. updateData = updateData.concat(this.getDeleteDataByBills[node.children]);
  237. } else {
  238. let rations = this.getBillsSortRation(node.getID());
  239. for (let r of rations) {
  240. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': r.ID, 'projectID': this.project.ID()}});
  241. }
  242. }
  243. }
  244. return updateData;
  245. };
  246. ration.prototype.deleteByBills = function (nodes) {
  247. for (let node of nodes) {
  248. if (node.children.length > 0) {
  249. this.deleteByBills([node.children]);
  250. } else {
  251. let rations = this.getBillsSortRation(node.getID());
  252. for (let r of rations) {
  253. this.datas.splice(this.datas.indexOf(r), 1);
  254. }
  255. }
  256. }
  257. }
  258. ration.prototype.getChangePosUpdateData = function (ration1, ration2) {
  259. var updateData = [];
  260. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration1.ID, ration1.billsItemID, ration2.serialNo)});
  261. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration2.ID, ration2.billsItemID, ration1.serialNo)});
  262. return updateData;
  263. };
  264. ration.prototype.changePos = function (ration1, ration2) {
  265. var updateData = this.getChangePosUpdateData(ration1, ration2);
  266. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  267. var preSerialNo = ration1.serialNo;
  268. ration1.serialNo = ration2.serialNo;
  269. ration2.serialNo = preSerialNo;
  270. };
  271. ration.prototype.updateField = function (ration, field, newValue) {
  272. calcFees.setFee(ration, field, newValue);
  273. let updateData = [];
  274. let data = {'ID': ration.ID, 'projectID': this.project.ID()};
  275. if (field === 'quantity') {
  276. data[field] = newValue;
  277. data.isFromDetail=0;
  278. // to do Calculate
  279. if (ration.fees) {
  280. data.fees = ration.fees;
  281. }
  282. } else if (field === 'feesIndex.common.unitFee') {
  283. // to do Calculate
  284. if (ration.fees) {
  285. data.fees = ration.fees;
  286. }
  287. } else {
  288. data[field] = newValue;
  289. }
  290. updateData.push({'updateType': 'ut_update', 'updateData': data});
  291. this.project.pushNow('updateBills', this.getSourceType(), updateData);
  292. };
  293. ration.prototype.updateRation = function (ration, updateNow) {
  294. let updateData = [];
  295. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatRationUpdateData(ration)});
  296. if (updateNow) {
  297. this.project.pushNow('updateRations', this.getSourceType(), updateData);
  298. } else {
  299. this.project.push(this.getSourceType(), updateData);
  300. }
  301. };
  302. ration.prototype.FilterNumberFromUnit = function (unit) {
  303. let reg = new RegExp('^[0-9]+');
  304. if (reg.test(unit)) {
  305. return parseInt(unit.match(reg)[0]);
  306. } else {
  307. return 1;
  308. }
  309. };
  310. ration.prototype.CalculateQuantity = function (ration) {
  311. // calculate ration Quantity
  312. if (optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToBillsQuan')) {
  313. let billsNode = this.project.Bills.tree.findNode(ration[this.project.masterField.ration]);
  314. let billsQuantity = billsNode.data.quantity ? billsNode.data.quantity : 0;
  315. if (optionsOprObj.getOption(optionsOprObj.optionsTypes.GENERALOPTS, 'rationQuanACToRationUnit')) {
  316. ration.quantity = (billsQuantity / this.FilterNumberFromUnit(ration.unit)).toDecimal(4);
  317. } else {
  318. ration.quantity = billsQuantity.toDecimal(4);
  319. }
  320. }
  321. };
  322. ration.prototype.replaceRation = function (ration, std) {
  323. this.project.beginUpdate('replaceRation');
  324. // delete
  325. let ration_glj =projectObj.project.ration_glj;
  326. this.project.push(this.project.ration_glj.getSourceType(), this.project.ration_glj.getDeleteDataByRation(ration));
  327. this.project.ration_glj.deleteByRation(ration);
  328. this.project.ration_coe.deleteByRation(ration);
  329. this.project.quantity_detail.deleteByRation(ration);
  330. // insertNewRation
  331. let updateData = [];
  332. ration.code = std.code;
  333. ration.name = std.name;
  334. ration.caption = std.caption;
  335. ration.unit = std.unit;
  336. ration.libID = std.rationRepId;
  337. ration.content = std.jobContent;
  338. if (std.chapter) {
  339. ration.comments = std.chapter.explanation;
  340. ration.ruleText = std.chapter.ruleText;
  341. }
  342. ration.programID = std.feeType;
  343. ration.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  344. // calculate ration Quantity
  345. this.CalculateQuantity(ration);
  346. updateData.push({updateType: 'ut_update', updateData: ration});
  347. this.project.push(this.getSourceType(), updateData);
  348. this.project.endUpdate();
  349. };
  350. return new ration(project);
  351. }
  352. };