ration.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if(data.quantityRefresh){
  56. this.refreshQuantity(data);
  57. }
  58. };
  59. ration.prototype.refreshAdjustState = function(data){
  60. this.refreshDatas(data,'adjustState');
  61. if(data.hasOwnProperty('name')){
  62. this.refreshDatas(data,'name')
  63. }
  64. };
  65. ration.prototype.refreshQuantity = function(data){
  66. this.refreshDatas(data,'quantity');
  67. };
  68. ration.prototype.refreshDatas = function(data,fieldName){
  69. var dataIndex = _.findIndex(this.datas,function(item) {
  70. return item.ID ==data.rationID;
  71. });
  72. this.datas[dataIndex][fieldName] = data[fieldName];
  73. if(fieldName=='quantity'){
  74. this.datas[dataIndex]['isFromDetail']=1
  75. }
  76. var controller = projectObj.mainController;
  77. var selected = controller.sheet.getSelections();
  78. var col = _.findIndex(BillsGridSetting.cols,function (col) {
  79. return col.data.field ==fieldName;
  80. });
  81. controller.sheet.getCell(selected[0].row,col).value(data[fieldName]);
  82. };
  83. ration.prototype.getTempRationData = function (id, billsID, serialNo) {
  84. var newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};
  85. newData[project.masterField.ration] = billsID;
  86. return newData;
  87. };
  88. ration.prototype.getBillsSortRation = function (billsID) {
  89. var rations = this.datas.filter(function (data) {
  90. return data[project.masterField.ration] === billsID;
  91. });
  92. rations.sort(function (x, y) {
  93. return x.serialNo - y.serialNo;
  94. });
  95. return rations;
  96. };
  97. ration.prototype.getInsertRationData = function (billsID, preRation) {
  98. var br = this.getBillsSortRation(billsID);
  99. var updateData = [];
  100. if (preRation) {
  101. var preIndex = br.indexOf(preRation), i;
  102. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1)});
  103. for (i = preIndex + 1; i < br.length; i++) {
  104. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1)});
  105. }
  106. } else {
  107. updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1)});
  108. }
  109. return updateData;
  110. };
  111. ration.prototype.getCounterData = function (count) {
  112. var updateData = {'projectID': this.project.ID()};
  113. if (count) {
  114. updateData[this.getSourceType()] = this.maxRationID() + count;
  115. } else {
  116. updateData[this.getSourceType()] = this.maxRationID() + 1;
  117. }
  118. return updateData;
  119. };
  120. ration.prototype.insertRation = function (billsID, preRation) {
  121. var br = this.getBillsSortRation(billsID);
  122. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()],
  123. [this.getInsertRationData(billsID, preRation), this.getCounterData()]);
  124. var newRation = null;
  125. if (preRation) {
  126. var preIndex = br.indexOf(preRation), i;
  127. newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
  128. this.datas.push(newRation);
  129. for (i = preIndex + 1; i < br.length; i++) {
  130. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  131. }
  132. } else {
  133. newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
  134. this.datas.push(newRation);
  135. }
  136. return newRation;
  137. };
  138. ration.prototype.insertStdRation = function (billsID, preRation, std) {
  139. var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation), newRation = null;
  140. updateData.forEach(function (data) {
  141. if (data.updateType === 'ut_create') {
  142. data.updateData.code = std.code;
  143. data.updateData.name = std.name;
  144. data.updateData.caption=std.caption;
  145. data.updateData.unit = std.unit;
  146. data.updateData.libID = std.rationRepId;
  147. data.updateData.rationAssList = projectObj.project.ration_ass.CreateNewAss(std);
  148. newRation = data.updateData;
  149. }
  150. });
  151. this.project.pushNow('insertRation', [this.getSourceType(), this.project.projCounter()], [updateData, this.getCounterData()]);
  152. newRation.ID = this.getNewRationID();
  153. if (preRation) {
  154. var preIndex = br.indexOf(preRation), i;
  155. this.datas.push(newRation);
  156. for (i = preIndex + 1; i < br.length; i++) {
  157. br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  158. }
  159. } else {
  160. this.datas.push(newRation);
  161. }
  162. return newRation;
  163. };
  164. ration.prototype.getDeleteData = function (rationData) {
  165. var updateData = [];
  166. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': rationData.ID, 'projectID': this.project.ID()}});
  167. return updateData;
  168. };
  169. ration.prototype.delete = function (ration) {
  170. var ration_glj =projectObj.project.ration_glj;
  171. this.project.pushNow('deleteRation', [this.getSourceType(),ration_glj.getSourceType()], [this.getDeleteData(ration),ration_glj.getDeleteDataByRation(ration)]);
  172. project.ration_glj.deleteByRation(ration);
  173. project.ration_coe.deleteByRation(ration);
  174. project.quantity_detail.deleteByRation(ration);
  175. this.datas.splice(this.datas.indexOf(ration), 1);
  176. };
  177. ration.prototype.getDeleteDataByBill = function (nodes) {
  178. let updateData = [];
  179. for (let node of nodes) {
  180. if (node.children.length > 0) {
  181. updateData = updateData.concat(this.getDeleteDataByBills[node.children]);
  182. } else {
  183. let rations = this.getBillsSortRation(node.getID());
  184. for (let r of rations) {
  185. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': r.ID, 'projectID': this.project.ID()}});
  186. }
  187. }
  188. }
  189. return updateData;
  190. };
  191. ration.prototype.deleteByBills = function (nodes) {
  192. for (let node of nodes) {
  193. if (node.children.length > 0) {
  194. this.deleteByBills([node.children]);
  195. } else {
  196. let rations = this.getBillsSortRation(node.getID());
  197. for (let r of rations) {
  198. this.datas.splice(this.datas.indexOf(r), 1);
  199. }
  200. }
  201. }
  202. }
  203. ration.prototype.getChangePosUpdateData = function (ration1, ration2) {
  204. var updateData = [];
  205. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration1.ID, ration1.billsItemID, ration2.serialNo)});
  206. updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(ration2.ID, ration2.billsItemID, ration1.serialNo)});
  207. return updateData;
  208. };
  209. ration.prototype.changePos = function (ration1, ration2) {
  210. var updateData = this.getChangePosUpdateData(ration1, ration2);
  211. this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
  212. var preSerialNo = ration1.serialNo;
  213. ration1.serialNo = ration2.serialNo;
  214. ration2.serialNo = preSerialNo;
  215. };
  216. ration.prototype.updateField = function (ration, field, newValue) {
  217. calcFees.setFee(ration, field, newValue);
  218. let updateData = [];
  219. let data = {'ID': ration.ID, 'projectID': this.project.ID()};
  220. if (field === 'quantity') {
  221. data[field] = newValue;
  222. data.isFromDetail=0;
  223. // to do Calculate
  224. if (ration.fees) {
  225. data.fees = ration.fees;
  226. }
  227. } else if (field === 'feesIndex.common.unitFee') {
  228. // to do Calculate
  229. if (ration.fees) {
  230. data.fees = ration.fees;
  231. }
  232. } else {
  233. data[field] = newValue;
  234. }
  235. updateData.push({'updateType': 'ut_update', 'updateData': data});
  236. this.project.pushNow('updateBills', this.getSourceType(), updateData);
  237. };
  238. return new ration(project);
  239. }
  240. };