ration.js 15 KB

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