setProduct.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import Vue from 'vue';
  2. // 保留指定位数小数并去掉多余的0
  3. export function formatPrice(price, decimals = 4) {
  4. return price.toFixed(decimals).replace(/\.?0+$/, '');
  5. }
  6. //改变数量
  7. export function changeCount(row, countObj,noDiscountSingle) {
  8. let total = row[countObj.countKey] || 0;
  9. let data = row;
  10. let endIndex = row.packageDispositionList.findIndex(
  11. (ite) => row[countObj.unitIdKey] == ite.id
  12. );
  13. for (; 0 < endIndex; endIndex--) {
  14. total = Vue.prototype.$math.format(
  15. row.packageDispositionList[endIndex].packageCell * total,
  16. 14
  17. );
  18. }
  19. data['totalCount'] = total;
  20. data['discountSinglePrice'] = !noDiscountSingle
  21. ? data.singlePrice
  22. : data.discountSinglePrice;
  23. setWeight(data);
  24. if (row.totalCount && row.singlePrice) {
  25. data['totalPrice'] = row.totalCount * row.singlePrice;
  26. data['discountTotalPrice'] = !noDiscountSingle
  27. ? data.totalPrice
  28. : data.discountTotalPrice;
  29. }
  30. // getNotaxSinglePrice(data);
  31. return data;
  32. // if (row) {
  33. // singleWeightChange(row, index);
  34. // }
  35. // return getNumTotalPrice(arr, noDiscountSingle);
  36. }
  37. function setWeight(row) {
  38. if (row.weightUnit == row.measuringUnit) {
  39. row['totalWeight'] = row.totalCount;
  40. } else if (row.totalCount && row.singleWeight) {
  41. row['totalWeight'] = row.totalCount * row.singleWeight;
  42. } else {
  43. row['totalWeight'] = 0;
  44. }
  45. }
  46. export function getAllPrice(arr){
  47. let sum = 0;
  48. arr.forEach(item=>{
  49. if(item.totalPrice){
  50. sum+=Number(item.totalPrice)
  51. }
  52. })
  53. return sum
  54. }
  55. export function getAllQuantity(arr, countObj) {
  56. let sum = 0;
  57. arr.forEach((item) => {
  58. console.log('item', item, countObj.countKey);
  59. if (item[countObj.countKey]) {
  60. sum += Number(item[countObj.countKey]);
  61. }
  62. });
  63. return isNaN(sum) ? 0 : formatPrice(sum);
  64. }
  65. // //计算不含税单价
  66. // function getNotaxSinglePrice(row) {
  67. // if (row.singlePrice && row.taxRate) {
  68. // row['notaxSinglePrice'] = parseFloat(
  69. // (row.singlePrice / (1 + row.taxRate / 100)).toFixed(2)
  70. // );
  71. // } else {
  72. // row['notaxSinglePrice'] = '';
  73. // }
  74. // }
  75. // //计算总金额
  76. // function getNumTotalPrice(arr, noDiscountSingle) {
  77. // let sum = 0;
  78. // arr.forEach((r, index) => {
  79. // // if (r.singlePrice) {
  80. // // r.notaxSinglePrice=r.taxRate?r.singlePrice/(1-(r.taxRate/100)):r.singlePrice
  81. // // }
  82. // if (r.singlePrice && r.totalCount) {
  83. // Vue.set(
  84. // arr[index],
  85. // 'discountSinglePrice',
  86. // !noDiscountSingle ? r.singlePrice : r.discountSinglePrice
  87. // );
  88. // Vue.set(arr[index], 'totalPrice', getAllPrice(r));
  89. // Vue.set(
  90. // arr[index],
  91. // 'discountTotalPrice',
  92. // !noDiscountSingle ? r.totalPrice : r.discountTotalPrice
  93. // );
  94. // sum += Number(r.totalPrice);
  95. // } else {
  96. // Vue.set(arr[index], 'totalPrice', 0);
  97. // Vue.set(arr[index], 'discountTotalPrice',0);
  98. // }
  99. // });
  100. // return { allPrice: isNaN(sum) ? 0 : sum.toFixed(2), arr };
  101. // }
  102. // //获取合计
  103. // function getAllPrice(row) {
  104. // let num = 0;
  105. // if (row.pricingWay == 1) {
  106. // //按数量计价计算总金额
  107. // num = Number(row.singlePrice) * Number(row.totalCount);
  108. // }
  109. // if (row.pricingWay == 2) {
  110. // //按重量计价计算总金额
  111. // num =
  112. // Number(row.singlePrice) *
  113. // Number(row.totalCount) *
  114. // Number(row.singleWeight);
  115. // }
  116. // return isNaN(num) ? '' : num.toFixed(2);
  117. // }
  118. // function singleWeightChange(row) {
  119. // console.log(row, 'row');
  120. // if (row && row.singleWeight && row.totalCount) {
  121. // row.totalWeight = (row.singleWeight * row.totalCount).toFixed(2) || 0;
  122. // } else {
  123. // row.totalWeight = '';
  124. // }
  125. // }