volume_price.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Created by Mai on 2017/7/25.
  3. */
  4. var VolumePrice = {
  5. createNew: function (project) {
  6. let tools = {
  7. owner: project
  8. };
  9. class volumePrice {
  10. constructor () {
  11. this.datas = [];
  12. let maxID = 0;
  13. this.getNewID = function () {
  14. return maxID += 1;
  15. };
  16. this.maxID = function (ID) {
  17. if (arguments.length === 0) {
  18. return maxID;
  19. } else {
  20. maxID = Math.max(ID, maxID);
  21. }
  22. };
  23. tools.owner.registerModule(ModuleNames.volume_price, this);
  24. };
  25. getProject () {
  26. return tools.owner;
  27. };
  28. getSourceType () {
  29. return ModuleNames.volume_price;
  30. };
  31. loadData (datas) {
  32. this.datas = datas;
  33. // generate Fees & Flags Index,For View & Calculate
  34. for (let data of datas) {
  35. data.feesIndex = {};
  36. data.fees.forEach(function (fee) {
  37. data.feesIndex[fee.fieldName] = fee;
  38. });
  39. this.maxID(data.ID);
  40. }
  41. };
  42. setMaxID (ID) {
  43. this.maxID(ID);
  44. }
  45. getTempVolumePrice (newID, billsID, serialNo) {
  46. var newData = {'ID': newID, 'serialNo': serialNo, projectID: tools.owner.ID()};
  47. newData[project.masterField.volumePrice] = billsID;
  48. return newData;
  49. };
  50. getBillsSortVolumePrice (billsID) {
  51. var arr = this.datas.filter(function (data) {
  52. return data[tools.owner.masterField.volumePrice] === billsID;
  53. });
  54. arr.sort(function (x, y) {
  55. return x.serialNo - y.serialNo;
  56. });
  57. return arr;
  58. };
  59. getCounterData (count) {
  60. var updateData = {'projectID': this.getProject().ID()};
  61. if (count) {
  62. updateData[this.getSourceType()] = this.maxID() + count;
  63. } else {
  64. updateData[this.getSourceType()] = this.maxID() + 1;
  65. }
  66. return updateData;
  67. };
  68. getInsertVolumePriceData (billsID, pre) {
  69. let bv = this.getBillsSortVolumePrice(billsID);
  70. let updateData = [];
  71. if (pre) {
  72. let preIndex = bv.indexOf(pre), i;
  73. updateData.push({updateType: 'ut_create', updateData: this.getTempVolumePrice(this.maxID() + 1, billsID, preIndex < bv.length - 1 ? bv[preIndex + 1].serialNo : bv[preIndex].serialNo + 1)});
  74. for (i = preIndex + 1; i < bv.length; i++) {
  75. updateData.push({updateType: 'ut_update', updateData: this.getTempVolumePrice(bv[i].ID, billsID, i < bv.length - 1 ? bv[i+1].serialNo : bv[i].serialNo + 1)});
  76. }
  77. } else {
  78. updateData.push({updateType: 'ut_create', updateData: this.getTempVolumePrice(this.maxID() + 1, billsID, bv.length > 0 ? bv[bv.length - 1].serialNo + 1 : 1)});
  79. }
  80. return updateData;
  81. };
  82. insertVolumePrice (billsID, pre) {
  83. tools.owner.pushNow('insertVolumePrice', [this.getSourceType(), this.getProject().projCounter()], [this.getInsertVolumePriceData(billsID, pre), this.getCounterData()]);
  84. let bv = this.getBillsSortVolumePrice(billsID), newVP = null;
  85. if (pre) {
  86. let preIndex = bv.indexOf(pre);
  87. newVP = this.getTempVolumePrice(this.getNewID(), billsID, preIndex < bv.length - 1 ? bv[preIndex + 1].serialNo : bv[preIndex].serialNo + 1);
  88. this.datas.push(newVP);
  89. for (let i = preIndex + 1; i < bv.length; i++) {
  90. bv[i].serialNo = i < bv.length - 1 ? bv [i + 1].serialNo : bv[i].serialNo + 1;
  91. }
  92. } else {
  93. newVP = this.getTempVolumePrice(this.getNewID(), billsID, bv.length > 0 ? bv[bv.length - 1].serialNo + 1 : 1);
  94. this.datas.push(newVP);
  95. }
  96. return newVP;
  97. };
  98. getDeleteData (volumePrice) {
  99. var updateData = [];
  100. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': volumePrice.ID, 'projectID': this.getProject().ID()}});
  101. return updateData;
  102. };
  103. delete (volumePrice) {
  104. this.getProject().pushNow('deleteVolumePrice', [this.getSourceType()], [this.getDeleteData(volumePrice)]);
  105. this.datas.splice(this.datas.indexOf(volumePrice), 1);
  106. };
  107. getDeleteDataByBills (nodes) {
  108. let updateData = [];
  109. for (let node of nodes) {
  110. if (node.children.length > 0) {
  111. updateData = updateData.concat(this.getDeleteDataByBills[node.children]);
  112. } else {
  113. let vps = this.getBillsSortVolumePrice(node.getID());
  114. for (let vp of vps) {
  115. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': vp.ID, 'projectID': this.getProject().ID()}});
  116. }
  117. }
  118. }
  119. return updateData;
  120. };
  121. deleteByBills (nodes) {
  122. for (let node of nodes) {
  123. if (node.children.length > 0) {
  124. this.deleteByBills([node.children]);
  125. } else {
  126. let vps = this.getBillsSortVolumePrice(node.getID());
  127. for (let vp of vps) {
  128. this.datas.splice(this.datas.indexOf(vp), 1);
  129. }
  130. }
  131. }
  132. };
  133. calculate (volumePrice) {
  134. if (!calcFees.findFee(volumePrice, 'common')) {
  135. calcFees.addFee(volumePrice, 'common');
  136. }
  137. volumePrice.feesIndex.common.totalFee = (volumePrice.feesIndex.common.unitFee * volumePrice.quantity).toDecimal(tools.owner.Decimal.common.totalFee);
  138. volumePrice.needRefresh = true;
  139. }
  140. updateField(volumePrice, field, newValue, updateNow) {
  141. calcFees.setFee(volumePrice, field, newValue);
  142. let updateData = [];
  143. let data = {'ID': volumePrice.ID, 'projectID': this.getProject().ID()};
  144. if (field === 'quantity') {
  145. data[field] = newValue;
  146. this.calculate(volumePrice);
  147. data.fees = volumePrice.fees;
  148. } else if (field === 'feesIndex.common.unitFee') {
  149. this.calculate(volumePrice);
  150. data.fees = volumePrice.fees;
  151. } else {
  152. data[field] = newValue;
  153. }
  154. updateData.push({'updateType': 'ut_update', 'updateData': data});
  155. if (updateNow) {
  156. tools.owner.pushNow('updateVolumePrice', this.getSourceType(), updateData);
  157. } else {
  158. tools.owner.push(this.getSourceType(), updateData);
  159. }
  160. }
  161. }
  162. return new volumePrice();
  163. }
  164. }