setProduct.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import Vue from 'vue';
  2. // 保留指定位数小数并去掉多余的0
  3. export function formatPrice(price, decimals = 4) {
  4. return price.toFixed(decimals).replace(/\.?0+$/, '');
  5. }
  6. // 安全格式化数字(兼容 $math 未注入的场景)
  7. function safeFormat(value) {
  8. if (Vue.prototype.$math && Vue.prototype.$math.format) {
  9. return Vue.prototype.$math.format(value, 14)
  10. }
  11. return Number(value)
  12. }
  13. //改变数量
  14. export function changeCount(row, countObj, noDiscountSingle) {
  15. console.log(row, countObj, noDiscountSingle);
  16. let total = row[countObj.countKey] || 0;
  17. let data = row;
  18. if (row.packageDispositionList) {
  19. let endIndex = row.packageDispositionList.findIndex(
  20. (ite) => row[countObj.unitIdKey] == ite.id
  21. );
  22. for (; 0 < endIndex; endIndex--) {
  23. total = safeFormat(row.packageDispositionList[endIndex].packageCell * total)
  24. }
  25. }
  26. data['totalCount'] = total;
  27. data['discountSinglePrice'] = !noDiscountSingle
  28. ? data.singlePrice
  29. : data.discountSinglePrice;
  30. setWeight(data);
  31. if (row[countObj.countKey] && row.singlePrice) {
  32. data['totalPrice'] = row[countObj.countKey] * row.singlePrice;
  33. data['discountTotalPrice'] = !noDiscountSingle
  34. ? data.totalPrice
  35. : data.discountTotalPrice;
  36. } else {
  37. data['totalPrice'] = 0;
  38. data['discountTotalPrice'] = 0;
  39. }
  40. // getNotaxSinglePrice(data);
  41. return data;
  42. // if (row) {
  43. // singleWeightChange(row, index);
  44. // }
  45. // return getNumTotalPrice(arr, noDiscountSingle);
  46. }
  47. //改变数量
  48. export function changeCountNew(row, countObj, noDiscountSingle, weightType) {
  49. let data = JSON.parse(JSON.stringify(row));
  50. console.log('changeCountNew~~', data);
  51. let total = data[countObj.countKey] || 0;
  52. let _endIndex = 0; //计算单重需要
  53. if (data.packageDispositionList) {
  54. let endIndex = data.packageDispositionList.findIndex(
  55. (ite) => data[countObj.unitIdKey] == ite.id
  56. );
  57. _endIndex = endIndex;
  58. let packageData = data.packageDispositionList.find(
  59. (ite) => data[countObj.unitIdKey] == ite.id
  60. );
  61. data[countObj.unitKey] = packageData.conversionUnit;
  62. if (data.weightUnit == data.measuringUnit && data.singleWeight) {
  63. data.packageDispositionList[1].packageCell = data.singleWeight;
  64. }
  65. for (; 0 < endIndex; endIndex--) {
  66. total = safeFormat(data.packageDispositionList[endIndex].packageCell * total)
  67. }
  68. }
  69. data['totalCount'] = total;
  70. data['discountSinglePrice'] = data.singlePrice && data.discountRatio ? formatPrice(+data.singlePrice * (+data.discountRatio /100)) : data.singlePrice;
  71. if (weightType == 'totalWeight') {
  72. console.log('weightType~~', 1);
  73. setSingleWeight(data, countObj, _endIndex);
  74. } else {
  75. console.log('weightType~~', 2);
  76. setWeight(data, countObj);
  77. }
  78. data['totalPrice'] = 0;
  79. data['discountTotalPrice'] = 0;
  80. if (data.pricingWay == 2 || data.pricingWay == 3) {
  81. if (data.totalWeight && data.singlePrice) {
  82. data['totalPrice'] = formatPrice(data.totalWeight * data.singlePrice);
  83. }
  84. } else {
  85. if (data[countObj.countKey] && data.singlePrice) {
  86. data['totalPrice'] = formatPrice(data[countObj.countKey] * data.singlePrice);
  87. }
  88. }
  89. data['discountTotalPrice'] = data['totalPrice'] && data.discountRatio ? formatPrice(+data['totalPrice'] * (data.discountRatio /100)) : data['totalPrice'];
  90. return data;
  91. }
  92. function setWeight(row) {
  93. if (row.weightUnit == row.measuringUnit) {
  94. row['totalWeight'] = row.totalCount;
  95. } else if (row.totalCount && row.singleWeight) {
  96. row['totalWeight'] = Number((row.totalCount * row.singleWeight).toFixed(2));
  97. } else {
  98. row['totalWeight'] = 0;
  99. }
  100. }
  101. export function getAllPrice(arr) {
  102. let sum = 0;
  103. arr.forEach((item) => {
  104. if (item.totalPrice) {
  105. sum += Number(item.totalPrice);
  106. }
  107. });
  108. return isNaN(sum) ? 0 : sum.toFixed(2);
  109. }
  110. // //计算不含税单价
  111. // function getNotaxSinglePrice(row) {
  112. // if (row.singlePrice && row.taxRate) {
  113. // row['notaxSinglePrice'] = parseFloat(
  114. // (row.singlePrice / (1 + row.taxRate / 100)).toFixed(2)
  115. // );
  116. // } else {
  117. // row['notaxSinglePrice'] = '';
  118. // }
  119. // }
  120. // //计算总金额
  121. // function getNumTotalPrice(arr, noDiscountSingle) {
  122. // let sum = 0;
  123. // arr.forEach((r, index) => {
  124. // // if (r.singlePrice) {
  125. // // r.notaxSinglePrice=r.taxRate?r.singlePrice/(1-(r.taxRate/100)):r.singlePrice
  126. // // }
  127. // if (r.singlePrice && r.totalCount) {
  128. // Vue.set(
  129. // arr[index],
  130. // 'discountSinglePrice',
  131. // !noDiscountSingle ? r.singlePrice : r.discountSinglePrice
  132. // );
  133. // Vue.set(arr[index], 'totalPrice', getAllPrice(r));
  134. // Vue.set(
  135. // arr[index],
  136. // 'discountTotalPrice',
  137. // !noDiscountSingle ? r.totalPrice : r.discountTotalPrice
  138. // );
  139. // sum += Number(r.totalPrice);
  140. // } else {
  141. // Vue.set(arr[index], 'totalPrice', 0);
  142. // Vue.set(arr[index], 'discountTotalPrice',0);
  143. // }
  144. // });
  145. // return { allPrice: isNaN(sum) ? 0 : sum.toFixed(2), arr };
  146. // }
  147. // //获取合计
  148. // function getAllPrice(row) {
  149. // let num = 0;
  150. // if (row.pricingWay == 1) {
  151. // //按数量计价计算总金额
  152. // num = Number(row.singlePrice) * Number(row.totalCount);
  153. // }
  154. // if (row.pricingWay == 2) {
  155. // //按重量计价计算总金额
  156. // num =
  157. // Number(row.singlePrice) *
  158. // Number(row.totalCount) *
  159. // Number(row.singleWeight);
  160. // }
  161. // return isNaN(num) ? '' : num.toFixed(2);
  162. // }
  163. // function singleWeightChange(row) {
  164. // console.log(row, 'row');
  165. // if (row && row.singleWeight && row.totalCount) {
  166. // row.totalWeight = (row.singleWeight * row.totalCount).toFixed(2) || 0;
  167. // } else {
  168. // row.totalWeight = '';
  169. // }
  170. // }