volume_price.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. newData.type = '材料';
  49. newData.programID = projectInfoObj.projectInfo.property.engineering;
  50. return newData;
  51. };
  52. getBillsSortVolumePrice (billsID) {
  53. var arr = this.datas.filter(function (data) {
  54. return data[tools.owner.masterField.volumePrice] === billsID;
  55. });
  56. arr.sort(function (x, y) {
  57. return x.serialNo - y.serialNo;
  58. });
  59. return arr;
  60. };
  61. getCounterData (count) {
  62. var updateData = {'projectID': this.getProject().ID()};
  63. if (count) {
  64. updateData[this.getSourceType()] = this.maxID() + count;
  65. } else {
  66. updateData[this.getSourceType()] = this.maxID() + 1;
  67. }
  68. return updateData;
  69. };
  70. getInsertVolumePriceData (billsID, pre) {
  71. let bv = this.getBillsSortVolumePrice(billsID);
  72. let updateData = [];
  73. if (pre && bv.indexOf(pre) > -1) {
  74. let preIndex = bv.indexOf(pre), i;
  75. updateData.push({updateType: 'ut_create', updateData: this.getTempVolumePrice(this.maxID() + 1, billsID, preIndex < bv.length - 1 ? bv[preIndex + 1].serialNo : bv[preIndex].serialNo + 1)});
  76. for (i = preIndex + 1; i < bv.length; i++) {
  77. updateData.push({updateType: 'ut_update', updateData: this.getTempVolumePrice(bv[i].ID, billsID, i < bv.length - 1 ? bv[i+1].serialNo : bv[i].serialNo + 1)});
  78. }
  79. } else {
  80. updateData.push({updateType: 'ut_create', updateData: this.getTempVolumePrice(this.maxID() + 1, billsID, bv.length > 0 ? bv[bv.length - 1].serialNo + 1 : 1)});
  81. }
  82. return updateData;
  83. };
  84. insertVolumePrice (billsID, pre) {
  85. tools.owner.pushNow('insertVolumePrice', [this.getSourceType(), this.getProject().projCounter()], [this.getInsertVolumePriceData(billsID, pre), this.getCounterData()]);
  86. let bv = this.getBillsSortVolumePrice(billsID), newVP = null;
  87. if (pre && bv.indexOf(pre) > -1) {
  88. let preIndex = bv.indexOf(pre);
  89. newVP = this.getTempVolumePrice(this.getNewID(), billsID, preIndex < bv.length - 1 ? bv[preIndex + 1].serialNo : bv[preIndex].serialNo + 1);
  90. this.datas.push(newVP);
  91. for (let i = preIndex + 1; i < bv.length; i++) {
  92. bv[i].serialNo = i < bv.length - 1 ? bv [i + 1].serialNo : bv[i].serialNo + 1;
  93. }
  94. } else {
  95. newVP = this.getTempVolumePrice(this.getNewID(), billsID, bv.length > 0 ? bv[bv.length - 1].serialNo + 1 : 1);
  96. this.datas.push(newVP);
  97. }
  98. return newVP;
  99. };
  100. getDeleteData (volumePrice) {
  101. var updateData = [];
  102. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': volumePrice.ID, 'projectID': this.getProject().ID()}});
  103. return updateData;
  104. };
  105. delete (volumePrice) {
  106. this.getProject().pushNow('deleteVolumePrice', [this.getSourceType()], [this.getDeleteData(volumePrice)]);
  107. this.datas.splice(this.datas.indexOf(volumePrice), 1);
  108. };
  109. getDeleteDataByBills (nodes) {
  110. let updateData = [];
  111. for (let node of nodes) {
  112. if (node.children.length > 0) {
  113. updateData = updateData.concat(this.getDeleteDataByBills[node.children]);
  114. } else {
  115. let vps = this.getBillsSortVolumePrice(node.getID());
  116. for (let vp of vps) {
  117. updateData.push({'updateType': 'ut_delete', 'updateData': {'ID': vp.ID, 'projectID': this.getProject().ID()}});
  118. }
  119. }
  120. }
  121. return updateData;
  122. };
  123. deleteByBills (nodes) {
  124. for (let node of nodes) {
  125. if (node.children.length > 0) {
  126. this.deleteByBills([node.children]);
  127. } else {
  128. let vps = this.getBillsSortVolumePrice(node.getID());
  129. for (let vp of vps) {
  130. this.datas.splice(this.datas.indexOf(vp), 1);
  131. }
  132. }
  133. }
  134. };
  135. calculate (volumePrice) {
  136. if (!calcFees.findFee(volumePrice, 'common')) {
  137. calcFees.addFee(volumePrice, 'common');
  138. }
  139. volumePrice.feesIndex.common.totalFee = (volumePrice.feesIndex.common.unitFee * volumePrice.quantity).toDecimal(tools.owner.Decimal.common.totalFee);
  140. volumePrice.needRefresh = true;
  141. }
  142. updateField(volumePrice, field, newValue, updateNow) {
  143. calcFees.setFee(volumePrice, field, newValue);
  144. let updateData = [];
  145. let data = {'ID': volumePrice.ID, 'projectID': this.getProject().ID()};
  146. if (field === 'quantity') {
  147. data[field] = newValue;
  148. this.calculate(volumePrice);
  149. data.fees = volumePrice.fees;
  150. } else if (field === 'feesIndex.common.unitFee') {
  151. this.calculate(volumePrice);
  152. data.fees = volumePrice.fees;
  153. } else {
  154. data[field] = newValue;
  155. }
  156. updateData.push({'updateType': 'ut_update', 'updateData': data});
  157. if (updateNow) {
  158. tools.owner.pushNow('updateVolumePrice', this.getSourceType(), updateData);
  159. } else {
  160. tools.owner.push(this.getSourceType(), updateData);
  161. }
  162. }
  163. }
  164. return new volumePrice();
  165. }
  166. }