bills.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var Bills = {
  5. createNew: function (project) {
  6. var billsTreeSetting = {
  7. id: 'ID',
  8. pid: 'ParentID',
  9. nid: 'NextSiblingID',
  10. rootId: -1,
  11. autoUpdate: true
  12. };
  13. // 用户定义private方法
  14. var tools = {
  15. coverseTreeUpdateData: function (datas, projectID) {
  16. var updateDatas = [];
  17. datas.forEach(function (data) {
  18. var updateData = {};
  19. data.data.projectID = projectID;
  20. if (data.type === idTree.updateType.new) {
  21. updateData.updateType = 'ut_create';
  22. updateData.updateData = data.data;
  23. } else if (data.type === idTree.updateType.update) {
  24. updateData.updateType = 'ut_update';
  25. updateData.updateData = data.data;
  26. } else if (data.type === idTree.updateType.delete) {
  27. updateData.updateType = 'ut_delete';
  28. updateData.updateData = data.data;
  29. }
  30. updateDatas.push(updateData);
  31. });
  32. return updateDatas;
  33. }
  34. };
  35. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  36. var bills = function (proj) {
  37. this.project = proj;
  38. this.datas = null;
  39. this.tree = idTree.createNew(billsTreeSetting);
  40. var sourceType = ModuleNames.bills;
  41. this.getSourceType = function () {
  42. return sourceType;
  43. }
  44. proj.registerModule(ModuleNames.bills, this);
  45. };
  46. // 从后台获取数据
  47. /*bills.prototype.pullData = function (){
  48. this.project.pullData(
  49. '/bills/getData',
  50. {projectID: this.project.ID},
  51. function(result){
  52. if (result.error ===0){
  53. this.loadDatas(result.data);
  54. }
  55. else {
  56. // to do: ?错误处理需要细化
  57. alert(result.message);
  58. }
  59. },
  60. function (){}// to do: 错误处理需要细化
  61. )
  62. };*/
  63. // prototype用于定义public方法
  64. bills.prototype.loadData = function (datas) {
  65. this.datas = datas;
  66. // generate Fees & Flags Index, For View & Calculate
  67. this.datas.forEach(function (data) {
  68. data.feesIndex = {};
  69. if (data.fees) {
  70. data.fees.forEach(function (fee) {
  71. data.feesIndex[fee.fieldName] = fee;
  72. });
  73. }
  74. data.flagsIndex = {};
  75. if (data.flags) {
  76. data.flags.forEach(function (flag) {
  77. data.flagsIndex[flag.fieldName] = flag;
  78. });
  79. }
  80. });
  81. // datas load to Tree
  82. this.tree.loadDatas(this.datas);
  83. };
  84. bills.prototype.setMaxID = function (ID) {
  85. this.tree.maxNodeID(ID);
  86. };
  87. // 提交数据后的错误处理方法
  88. bills.prototype.doAfterUpdate = function(err, data){
  89. console.log(data)
  90. if(data.quantityRefresh){
  91. this.refreshDatas(data,'quantity');
  92. }
  93. };
  94. bills.prototype.refreshDatas = function(data,fieldName){
  95. var dataIndex = _.findIndex(this.datas,function(item) {
  96. return item.ID ==data.billID;
  97. });
  98. this.datas[dataIndex][fieldName] = data[fieldName];
  99. if(fieldName=='quantity'){
  100. this.datas[dataIndex]['isFromDetail']=1
  101. }
  102. var controller = projectObj.mainController;
  103. var selected = controller.sheet.getSelections();
  104. var col = _.findIndex(BillsGridSetting.cols,function (col) {
  105. return col.data.field ==fieldName;
  106. });
  107. controller.sheet.getCell(selected[0].row,col).value(data[fieldName]);
  108. };
  109. bills.prototype.getCounterData = function (count) {
  110. var updateData = {'projectID': this.project.ID()};
  111. if (count) {
  112. updateData[this.getSourceType()] = this.tree.maxNodeID() + count;
  113. } else {
  114. updateData[this.getSourceType()] = this.tree.maxNodeID() + 1;
  115. }
  116. return updateData;
  117. };
  118. bills.prototype.insertBills = function (parentId, nextSiblingId) {
  119. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  120. var that = this, newData = null;
  121. insertData.forEach(function (data) {
  122. if (data.type === idTree.updateType.new) {
  123. newData = data.data;
  124. }
  125. });
  126. this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()],
  127. [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
  128. //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
  129. this.datas.push(newData);
  130. return this.tree.insert(parentId, nextSiblingId);
  131. };
  132. bills.prototype.insertStdBills = function (parentId, nextSiblingId, stdBillsData) {
  133. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  134. var newData = null, that = this;
  135. insertData.forEach(function (data) {
  136. if (data.type === idTree.updateType.new) {
  137. data.data.code = stdBillsData.code;
  138. data.data.name = stdBillsData.name;
  139. data.data.unit = stdBillsData.unit;
  140. newData = data.data;
  141. }
  142. });
  143. this.project.pushNow('insertStdBills', [this.getSourceType(), this.project.projCounter()],
  144. [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
  145. this.datas.push(newData);
  146. return this.tree.insertByData(newData, parentId, nextSiblingId);
  147. };
  148. bills.prototype.deleteBills = function (node) {
  149. let deleteNode = function (node) {
  150. this.project.Ration.deleteByBills([node]);
  151. this.project.VolumePrice.deleteByBills([node]);
  152. return this.tree.delete(node);
  153. }
  154. var deleteData = this.tree.getDeleteData(node);
  155. var ration_glj =projectObj.project.ration_glj;
  156. let modules =[ModuleNames.bills, ModuleNames.ration, ModuleNames.ration_glj, ModuleNames.volume_price];
  157. let deleteDatas=[tools.coverseTreeUpdateData(deleteData, this.project.ID()),
  158. this.project.Ration.getDeleteDataByBill([node]), ration_glj.getDeleteDataByBills(deleteData),
  159. this.project.VolumePrice.getDeleteDataByBills([node])
  160. ];
  161. project.ration_glj.deleteByBills(deleteData);
  162. project.quantity_detail.deleteByBills(deleteData);
  163. project.pushNow('deleteBills', modules, deleteDatas);
  164. return this.tree.delete(node);
  165. };
  166. bills.prototype.upMoveBills = function (node) {
  167. var upMoveData = node.getUpMoveData();
  168. project.pushNow('upMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(upMoveData, this.project.ID()));
  169. return node.upMove();
  170. };
  171. bills.prototype.downMoveBills = function (node) {
  172. var downMoveData = node.getDownMoveData();
  173. project.pushNow('downMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(downMoveData, this.project.ID()));
  174. return node.downMove();
  175. };
  176. bills.prototype.upLevelBills = function (node) {
  177. var upLevelData = node.getUpLevelData();
  178. project.pushNow('upLevelBills', this.getSourceType(), tools.coverseTreeUpdateData(upLevelData, this.project.ID()));
  179. return node.upLevel();
  180. };
  181. bills.prototype.downLevelBills = function (node) {
  182. var downLevelData = node.getDownLevelData();
  183. project.pushNow('downLevelBills', [this.getSourceType()], [tools.coverseTreeUpdateData(downLevelData, this.project.ID())]);
  184. return node.downLevel();
  185. };
  186. bills.prototype.updateField = function (node, field, newValue) {
  187. calcFees.setFee(node.data, field, newValue);
  188. let updateData = [];
  189. let data = {'ID': node.getID(), 'projectID': this.project.ID()};
  190. if (field === 'quantity') {
  191. data[field] = newValue;
  192. // to do Calculate
  193. if (node.data.fees) {
  194. data.fees = node.data.fees;
  195. }
  196. } else if (field === 'feesIndex.common.unitFee') {
  197. // to do Calculate
  198. if (node.data.fees) {
  199. data.fees = node.data.fees;
  200. }
  201. } else {
  202. data[field] = newValue;
  203. }
  204. updateData.push({'updateType': 'ut_update', 'updateData': data});
  205. this.project.pushNow('updateBills', this.getSourceType(), updateData);
  206. };
  207. return new bills(project);
  208. }
  209. };