setProduct.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Vue from 'vue';
  2. import _ from 'lodash';
  3. //改变数量
  4. export function changeCount(row, countObj, noDiscountSingle, quoteType) {
  5. let total = row[countObj.countKey] || 0;
  6. let data = { ...row };
  7. // 计算总数量(包装规格转换)
  8. if (row.packageDispositionList) {
  9. let endIndex = row.packageDispositionList.findIndex(
  10. (ite) => row[countObj.unitIdKey] == ite.id
  11. );
  12. // 修正包装规格转换逻辑(确保层级相乘正确)
  13. for (let i = 1; i <= endIndex; i++) {
  14. // 从1开始,避免重复计算
  15. total = Vue.prototype.$math.multiply(
  16. row.packageDispositionList[i].packageCell,
  17. total
  18. );
  19. }
  20. }
  21. data['totalCount'] = total; // 转换后的总数量
  22. // 计算重量(通用逻辑)
  23. setWeight(data);
  24. // 仅非生产加工类型(quoteType≠2)执行基于singlePrice的计算
  25. if (quoteType !== 2) {
  26. data['discountSinglePrice'] = !noDiscountSingle
  27. ? data.singlePrice
  28. : data.discountSinglePrice;
  29. // 关键修复:用转换后的totalCount计算总价,而非原始数量
  30. if (data.totalCount && data.singlePrice) {
  31. const count = Number(data.totalCount) || 0; // 使用转换后的总数量
  32. const price = Number(data.singlePrice) || 0;
  33. data['totalPrice'] = Vue.prototype.$math.round(
  34. Vue.prototype.$math.multiply(count, price),
  35. 2
  36. );
  37. data['discountTotalPrice'] = !noDiscountSingle
  38. ? data.totalPrice
  39. : Vue.prototype.$math.round(Number(data.discountTotalPrice) || 0, 2);
  40. } else {
  41. data['totalPrice'] = 0;
  42. data['discountTotalPrice'] = 0;
  43. }
  44. }
  45. return data;
  46. }
  47. function setWeight(row) {
  48. if (row.weightUnit == row.measuringUnit) {
  49. row['totalWeight'] = row.totalCount;
  50. } else if (row.totalCount && row.singleWeight) {
  51. // 使用lodash的round函数保留4位小数并向上舍入
  52. row['totalWeight'] = _.round(
  53. row.totalCount * row.singleWeight,
  54. 4,
  55. Math.ceil
  56. );
  57. } else {
  58. row['totalWeight'] = 0;
  59. }
  60. }
  61. export function getAllPrice(arr) {
  62. let sum = 0;
  63. arr.forEach((item) => {
  64. if (item.totalPrice) {
  65. // 使用加法并保持精度
  66. sum = Vue.prototype.$math.add(sum, Number(item.totalPrice) || 0);
  67. }
  68. });
  69. return Vue.prototype.$math.round(sum, 2);
  70. }
  71. // //计算不含税单价
  72. // function getNotaxSinglePrice(row) {
  73. // if (row.singlePrice && row.taxRate) {
  74. // row['notaxSinglePrice'] = parseFloat(
  75. // (row.singlePrice / (1 + row.taxRate / 100)).toFixed(2)
  76. // );
  77. // } else {
  78. // row['notaxSinglePrice'] = '';
  79. // }
  80. // }
  81. // //计算总金额
  82. // function getNumTotalPrice(arr, noDiscountSingle) {
  83. // let sum = 0;
  84. // arr.forEach((r, index) => {
  85. // // if (r.singlePrice) {
  86. // // r.notaxSinglePrice=r.taxRate?r.singlePrice/(1-(r.taxRate/100)):r.singlePrice
  87. // // }
  88. // if (r.singlePrice && r.totalCount) {
  89. // Vue.set(
  90. // arr[index],
  91. // 'discountSinglePrice',
  92. // !noDiscountSingle ? r.singlePrice : r.discountSinglePrice
  93. // );
  94. // Vue.set(arr[index], 'totalPrice', getAllPrice(r));
  95. // Vue.set(
  96. // arr[index],
  97. // 'discountTotalPrice',
  98. // !noDiscountSingle ? r.totalPrice : r.discountTotalPrice
  99. // );
  100. // sum += Number(r.totalPrice);
  101. // } else {
  102. // Vue.set(arr[index], 'totalPrice', 0);
  103. // Vue.set(arr[index], 'discountTotalPrice',0);
  104. // }
  105. // });
  106. // return { allPrice: isNaN(sum) ? 0 : sum.toFixed(2), arr };
  107. // }
  108. // //获取合计
  109. // function getAllPrice(row) {
  110. // let num = 0;
  111. // if (row.pricingWay == 1) {
  112. // //按数量计价计算总金额
  113. // num = Number(row.singlePrice) * Number(row.totalCount);
  114. // }
  115. // if (row.pricingWay == 2) {
  116. // //按重量计价计算总金额
  117. // num =
  118. // Number(row.singlePrice) *
  119. // Number(row.totalCount) *
  120. // Number(row.singleWeight);
  121. // }
  122. // return isNaN(num) ? '' : num.toFixed(2);
  123. // }
  124. // function singleWeightChange(row) {
  125. // console.log(row, 'row');
  126. // if (row && row.singleWeight && row.totalCount) {
  127. // row.totalWeight = (row.singleWeight * row.totalCount).toFixed(2) || 0;
  128. // } else {
  129. // row.totalWeight = '';
  130. // }
  131. // }