ration.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var Ration = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. var tools = {
  8. };
  9. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  10. var ration = function (proj) {
  11. this.project = proj;
  12. this.datas = null;
  13. var sourceType = ModuleNames.ration;
  14. this.getSourceType = function () {
  15. return sourceType;
  16. }
  17. proj.registerModule(ModuleNames.ration, this);
  18. var maxRationID = 0;
  19. this.getNewRationID = function () {
  20. return maxRationID += 1;
  21. };
  22. this.maxRationID = function (maxID) {
  23. if (arguments.length === 0) {
  24. return maxRationID;
  25. } else {
  26. maxRationID = Math.max(maxID, maxRationID);
  27. }
  28. };
  29. };
  30. // prototype用于定义public方法
  31. ration.prototype.loadData = function (datas) {
  32. var that = this;
  33. this.datas = datas;
  34. // generate Fees & Flags Index,For View & Calculate
  35. this.datas.forEach(function (data) {
  36. data.feesIndex = {};
  37. data.fees.forEach(function (fee) {
  38. data.feesIndex[fee.fieldName] = fee;
  39. });
  40. data.flagsIndex = {};
  41. data.flags.forEach(function (flag) {
  42. data.flagsIndex[flag.fieldName] = flag;
  43. });
  44. that.maxRationID(data.ID);
  45. });
  46. };
  47. ration.prototype.setMaxID = function (ID) {
  48. this.maxRationID(ID);
  49. }
  50. // refresh after update
  51. ration.prototype.doAfterUpdate = function(err, data){
  52. if(data.stateRefresh){
  53. this.refreshAdjustState(data);
  54. }
  55. };
  56. ration.prototype.refreshAdjustState = function(data){
  57. var controller = projectObj.mainController;
  58. var dataIndex = _.findIndex(this.datas,function(item) {
  59. return item.ID ==data.rationID;
  60. });
  61. this.datas[dataIndex].adjustState = data.adjustState;
  62. var selected = controller.sheet.getSelections();
  63. var col = _.findIndex(BillsGridSetting.cols,function (col) {
  64. return col.data.field =='adjustState';
  65. });
  66. controller.sheet.getCell(selected[0].row,col).value(data.adjustState);
  67. if(data.hasOwnProperty('name')){
  68. this.datas[dataIndex].name = data.name;
  69. var nameCol = _.findIndex(BillsGridSetting.cols,function (col) {
  70. return col.data.field =='name';
  71. })
  72. controller.sheet.getCell(selected[0].row,nameCol).value(data.name);
  73. }
  74. }
  75. ration.prototype.getTempRationData = function (id, billsID, serialNo) {
  76. var newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};
  77. newData[project.masterField.ration] = billsID;
  78. return newData;
  79. };
  80. ration.prototype.getBillsSortRation = function (billsID) {
  81. var rations = this.datas.filter(function (data) {
  82. return data[project.masterField.ration] === billsID;
  83. });
  84. rations.sort(function (x, y) {
  85. return x.serialNo - y.serialNo;
  86. });
  87. return rations;
  88. };
  89. ration.prototype.getInsertRationData = function (billsID, preRation) {
  90. var br = this.getBillsSortRation(billsID);
  91. var updateData = [];
  92. if (preRation) {
  93. var preIndex = br.indexOf(preRation), i;
  94. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1)});
  95. for (i = preIndex + 1; i < br.length; i++) {
  96. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1)});
  97. }
  98. } else {
  99. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1)});
  100. }
  101. return updateData;
  102. };
  103. ration.prototype.getCounterData = function (count) {
  104. var updateData = {'projectID': this.project.ID()};
  105. if (count) {
  106. updateData[this.getSourceType()] = this.maxRationID() + count;
  107. } else {
  108. updateData[this.getSourceType()] = this.maxRationID() + 1;
  109. }
  110. return updateData;
  111. };
  112. ration.prototype.insertRation = function (billsID, preRation) {
  113. var br = this.getBillsSortRation(billsID);
  114. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()],
  115. [this.getInsertRationData(billsID, preRation), this.getCounterData()]);
  116. var newRation = null;
  117. if (preRation) {
  118. var preIndex = br.indexOf(preRation), i;
  119. newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
  120. this.datas.push(newRation);
  121. for (i = preIndex + 1; i < br.length; i++) {
  122. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  123. }
  124. } else {
  125. newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
  126. this.datas.push(newRation);
  127. }
  128. return newRation;
  129. };
  130. ration.prototype.insertStdRation = function (billsID, preRation, std) {
  131. var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation), newRation = null;
  132. updateData.forEach(function (data) {
  133. if (data.updateType === 'ut_create') {
  134. data.updateData.code = std.code;
  135. data.updateData.name = std.name;
  136. data.updateData.caption=std.caption;
  137. data.updateData.unit = std.unit;
  138. data.updateData.libID = std.rationRepId;
  139. data.updateData.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  140. newRation = data.updateData;
  141. }
  142. });
  143. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  144. newRation.ID = this.getNewRationID();
  145. if (preRation) {
  146. var preIndex = br.indexOf(preRation), i;
  147. this.datas.push(newRation);
  148. for (i = preIndex + 1; i < br.length; i++) {
  149. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  150. }
  151. } else {
  152. this.datas.push(newRation);
  153. }
  154. return newRation;
  155. };
  156. ration.prototype.getDeleteData = function (rationData) {
  157. var updateData = [];
  158. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': this.project.ID()}});
  159. return updateData;
  160. };
  161. ration.prototype.getDeleteDataByBill = function (node) {
  162. console.log(node);
  163. };
  164. ration.prototype.delete = function (ration) {
  165. var ration_glj =projectObj.project.ration_glj;
  166. this.project.pushNow('deleteRation', [this.getSourceType(),ration_glj.getSourceType()], [this.getDeleteData(ration),ration_glj.getDeleteDataByRation(ration)]);
  167. this.datas.splice(this.datas.indexOf(ration), 1);
  168. };
  169. ration.prototype.getChangePosUpdateData = function (ration1, ration2) {
  170. var updateData = [];
  171. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration1.ID, ration1.billsItemID, ration2.serialNo)});
  172. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration2.ID, ration2.billsItemID, ration1.serialNo)});
  173. return updateData;
  174. };
  175. ration.prototype.changePos = function (ration1, ration2) {
  176. var updateData = this.getChangePosUpdateData(ration1, ration2);
  177. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  178. var preSerialNo = ration1.serialNo;
  179. ration1.serialNo = ration2.serialNo;
  180. ration2.serialNo = preSerialNo;
  181. };
  182. return new ration(project);
  183. }
  184. };