setProduct.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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, weightType) {
  8. let data = JSON.parse(JSON.stringify(row));
  9. // console.log('changeCount~~', data);
  10. let total = data[countObj.countKey] || 0;
  11. let _endIndex = 0; //计算单重需要
  12. if (data.packageDispositionList) {
  13. let endIndex = data.packageDispositionList.findIndex(
  14. (ite) => data[countObj.unitIdKey] == ite.id
  15. );
  16. _endIndex = endIndex;
  17. let packageData = data.packageDispositionList.find(
  18. (ite) => data[countObj.unitIdKey] == ite.id
  19. );
  20. data[countObj.unitKey] = packageData.conversionUnit;
  21. if (data.weightUnit == data.measuringUnit && data.singleWeight) {
  22. data.packageDispositionList[1].packageCell = data.singleWeight;
  23. }
  24. for (; 0 < endIndex; endIndex--) {
  25. total = Vue.prototype.$math.format(
  26. data.packageDispositionList[endIndex].packageCell * total,
  27. 14
  28. );
  29. }
  30. }
  31. data['totalCount'] = total;
  32. // data['discountSinglePrice'] = !noDiscountSingle
  33. // ? data.singlePrice
  34. // : data.discountSinglePrice;
  35. data['discountSinglePrice'] = data.singlePrice && data.discountRatio ? formatPrice(+data.singlePrice * (+data.discountRatio /100)) : data.singlePrice;
  36. // console.log('discountSinglePrice~~~', data.singlePrice, data.discountRatio);
  37. if (weightType == 'totalWeight') {
  38. console.log('weightType~~', 1);
  39. setSingleWeight(data, countObj, _endIndex);
  40. } else {
  41. console.log('weightType~~', 2);
  42. setWeight(data, countObj);
  43. }
  44. data['totalPrice'] = 0;
  45. data['discountTotalPrice'] = 0;
  46. if (data.pricingWay == 2 || data.pricingWay == 3) {
  47. if (data.totalWeight && data.singlePrice) {
  48. data['totalPrice'] = formatPrice(data.totalWeight * data.singlePrice);
  49. }
  50. } else {
  51. if (data[countObj.countKey] && data.singlePrice) {
  52. data['totalPrice'] = formatPrice(data[countObj.countKey] * data.singlePrice);
  53. }
  54. }
  55. // data['discountTotalPrice'] = !noDiscountSingle
  56. // ? data.totalPrice
  57. // : data.discountTotalPrice;
  58. data['discountTotalPrice'] = data['totalPrice'] && data.discountRatio ? formatPrice(+data['totalPrice'] * (data.discountRatio /100)) : data['totalPrice'];
  59. return data;
  60. }
  61. // 计算总重
  62. function setWeight(row, countObj) {
  63. // return
  64. if (row.weightUnit == row.measuringUnit) {
  65. row['totalWeight'] = row.totalCount;
  66. } else if (row.totalCount && row.singleWeight) {
  67. row['totalWeight'] = Number((row.totalCount * row.singleWeight).toFixed(2));
  68. } else {
  69. row['totalWeight'] = 0;
  70. }
  71. // if (row.weightUnit == row[countObj.unitKey]) {
  72. // row['totalWeight'] = row.totalCount;
  73. // } else if (row[countObj.countKey] && row.singleWeight) {
  74. // row['totalWeight'] = (row[countObj.countKey] * row.singleWeight).toFixed(2);
  75. // } else {
  76. // row['totalWeight'] = 0;
  77. // }
  78. // if (row.weightUnit == row.measuringUnit) {
  79. // row['totalCount'] = row.totalWeight;
  80. // }
  81. console.log(row['totalCount'], 'dsds');
  82. }
  83. // 计算单重
  84. function setSingleWeight(row, countObj, endIndex) {
  85. console.log('setSingleWeight~~', row);
  86. // if (row.totalWeight && row[countObj.countKey]) {
  87. // row['singleWeight'] = (row.totalWeight / row[countObj.countKey]).toFixed(2);
  88. // console.log('singleWeight~~', row['singleWeight']);
  89. // } else {
  90. // row['singleWeight'] = 0;
  91. // }
  92. // console.log(endIndex, 'endIndex');
  93. if (row.weightUnit == row.measuringUnit) {
  94. let totalWeight = row['totalWeight'];
  95. for (; 1 < endIndex; endIndex--) {
  96. totalWeight = Vue.prototype.$math.format(
  97. totalWeight / row.packageDispositionList[endIndex].packageCell,
  98. 14
  99. );
  100. }
  101. // console.log(totalWeight, 'totalWeight');
  102. row['singleWeight'] = Number(totalWeight / row[countObj.countKey]).toFixed(
  103. 2
  104. );
  105. } else if (row.totalWeight && row.totalCount) {
  106. row['singleWeight'] = Number((row.totalWeight / row.totalCount).toFixed(2));
  107. } else {
  108. row['singleWeight'] = 0;
  109. }
  110. if (row.weightUnit == row.measuringUnit) {
  111. row['totalCount'] = row.totalWeight;
  112. }
  113. if (row.weightUnit == row[countObj.unitKey]) {
  114. row[countObj.countKey] = row.totalWeight;
  115. }
  116. }
  117. export function getAllPrice(arr) {
  118. console.log('arr~~~', arr);
  119. let sum = 0;
  120. arr.forEach((item) => {
  121. if (item.totalPrice) {
  122. sum += Number(item.totalPrice);
  123. }
  124. });
  125. return isNaN(sum) ? 0 : formatPrice(sum);
  126. }
  127. export function getAllQuantity(arr, countObj) {
  128. let sum = 0;
  129. arr.forEach((item) => {
  130. console.log('item', item, countObj.countKey);
  131. if (item[countObj.countKey]) {
  132. sum += Number(item[countObj.countKey]);
  133. }
  134. });
  135. return isNaN(sum) ? 0 : formatPrice(sum);
  136. }
  137. // 获取优惠后总金额
  138. export function getAllDiscountPrice(arr) {
  139. let sum = 0;
  140. arr.forEach((item) => {
  141. if (item.discountTotalPrice) {
  142. sum += Number(item.discountTotalPrice);
  143. }
  144. });
  145. return isNaN(sum) ? 0 : formatPrice(sum);
  146. }
  147. //获取折让合计
  148. export function getDiscountTotalPrice(row) {
  149. let num = 0;
  150. num =
  151. Number(row.discountSinglePrice) * Number(row[countObj.countKey]);
  152. return isNaN(num) ? '' : formatPrice(num);
  153. }
  154. //获取折让单价
  155. export function getDiscountSinglePrice(row) {
  156. let num =
  157. Number(row.discountTotalPrice) *
  158. Number(row[countObj.countKey]);
  159. return isNaN(num) ? '' : formatPrice(num);
  160. }
  161. // //计算不含税单价
  162. // function getNotaxSinglePrice(row) {
  163. // if (row.singlePrice && row.taxRate) {
  164. // row['notaxSinglePrice'] = parseFloat(
  165. // (row.singlePrice / (1 + row.taxRate / 100)).toFixed(2)
  166. // );
  167. // } else {
  168. // row['notaxSinglePrice'] = '';
  169. // }
  170. // }
  171. // //计算总金额
  172. // function getNumTotalPrice(arr, noDiscountSingle) {
  173. // let sum = 0;
  174. // arr.forEach((r, index) => {
  175. // // if (r.singlePrice) {
  176. // // r.notaxSinglePrice=r.taxRate?r.singlePrice/(1-(r.taxRate/100)):r.singlePrice
  177. // // }
  178. // if (r.singlePrice && r.totalCount) {
  179. // Vue.set(
  180. // arr[index],
  181. // 'discountSinglePrice',
  182. // !noDiscountSingle ? r.singlePrice : r.discountSinglePrice
  183. // );
  184. // Vue.set(arr[index], 'totalPrice', getAllPrice(r));
  185. // Vue.set(
  186. // arr[index],
  187. // 'discountTotalPrice',
  188. // !noDiscountSingle ? r.totalPrice : r.discountTotalPrice
  189. // );
  190. // sum += Number(r.totalPrice);
  191. // } else {
  192. // Vue.set(arr[index], 'totalPrice', 0);
  193. // Vue.set(arr[index], 'discountTotalPrice',0);
  194. // }
  195. // });
  196. // return { allPrice: isNaN(sum) ? 0 : sum.toFixed(2), arr };
  197. // }
  198. // //获取合计
  199. // function getAllPrice(row) {
  200. // let num = 0;
  201. // if (row.pricingWay == 1) {
  202. // //按数量计价计算总金额
  203. // num = Number(row.singlePrice) * Number(row.totalCount);
  204. // }
  205. // if (row.pricingWay == 2) {
  206. // //按重量计价计算总金额
  207. // num =
  208. // Number(row.singlePrice) *
  209. // Number(row.totalCount) *
  210. // Number(row.singleWeight);
  211. // }
  212. // return isNaN(num) ? '' : num.toFixed(2);
  213. // }
  214. // function singleWeightChange(row) {
  215. // console.log(row, 'row');
  216. // if (row && row.singleWeight && row.totalCount) {
  217. // row.totalWeight = (row.singleWeight * row.totalCount).toFixed(2) || 0;
  218. // } else {
  219. // row.totalWeight = '';
  220. // }
  221. // }