FreightLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop100%开源免费商用电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
  10. // | 访问官网:https://www.likemarket.net
  11. // | 访问社区:https://home.likemarket.net
  12. // | 访问手册:http://doc.likemarket.net
  13. // | 微信公众号:好象科技
  14. // | 好象科技开发团队 版权所有 拥有最终解释权
  15. // +----------------------------------------------------------------------
  16. // | Author: LikeShopTeam
  17. // +----------------------------------------------------------------------
  18. namespace app\shopapi\logic\Order;
  19. use app\common\enum\DeliveryEnum;
  20. use app\common\enum\FreightEnum;
  21. use app\common\enum\YesNoEnum;
  22. use app\common\logic\BaseLogic;
  23. use app\common\model\Freight;
  24. use app\common\service\ConfigService;
  25. /**
  26. * 运费逻辑
  27. * Class FreightLogic
  28. * @package app\api\logic
  29. */
  30. class FreightLogic extends BaseLogic
  31. {
  32. /**
  33. * @notes 计算运费
  34. * @param $goods
  35. * @param $userAddress
  36. * @return float|int|mixed
  37. * @author 段誉
  38. * @date 2021/7/30 18:37
  39. */
  40. public static function calculateFreight($goods, $userAddress)
  41. {
  42. $expressPrice = 0;
  43. $templateList = [];
  44. $goodsExpressMoneys = [];
  45. // 配送方式配置, 如果配送方式-快递配送已关闭则不参与运费计算
  46. $expressConfig = ConfigService::get('delivery_type', 'is_express', 1);
  47. if (empty($userAddress) || $expressConfig == YesNoEnum::NO) {
  48. return ['goods'=>$goods,'express_price'=>$expressPrice];
  49. }
  50. foreach ($goods as &$good) {
  51. $good['total_weight'] = $good['weight'] * $good['goods_num'];
  52. $good['total_volume'] = $good['volume'] * $good['goods_num'];
  53. // 统一邮费
  54. if ($good['express_type'] == 2) {
  55. $good['express_price'] = round($good['express_money'] * $good['goods_num'], 2);
  56. $expressPrice += $good['express_price'];
  57. }
  58. // 指定运费模板
  59. if ($good['express_type'] == 3 && $good['express_template_id'] > 0) {
  60. $templateList[$good['express_template_id']][] = $good;
  61. }
  62. }
  63. foreach ($templateList as $templateId => $templateGoods) {
  64. $freight = FreightLogic::getFreightsByAddress($templateId, $userAddress);
  65. if (empty($freight)) {
  66. continue;
  67. }
  68. switch ($freight['charge_way']) {
  69. // 件数
  70. case FreightEnum::CHARGE_WAY_PIECE:
  71. $nums = array_sum(array_column($templateGoods, 'goods_num'));
  72. $real_count_field = 'goods_num';
  73. break;
  74. // 重量
  75. case FreightEnum::CHARGE_WAY_WEIGHT:
  76. $nums = array_sum(array_column($templateGoods, 'total_weight'));
  77. $real_count_field = 'total_weight';
  78. break;
  79. // 体积
  80. case FreightEnum::CHARGE_WAY_VOLUME:
  81. $nums = array_sum(array_column($templateGoods, 'total_volume'));
  82. $real_count_field = 'total_volume';
  83. break;
  84. default:
  85. continue 2;
  86. }
  87. // 计算运费
  88. if ($nums > $freight['first_unit'] && $freight['continue_unit'] > 0) {
  89. $left = ceil(($nums - $freight['first_unit']) / $freight['continue_unit']);
  90. $this_express_money = $freight['first_money'] + $left * $freight['continue_money'];
  91. } else {
  92. $this_express_money = $freight['first_money'];
  93. }
  94. // 计算每个商品 所占运费金额
  95. $goods_moneys = static::array_proportion($this_express_money, array_column($templateGoods, $real_count_field), 2, 'end');
  96. foreach ($templateGoods as $ko => $templateGood) {
  97. $goodsExpressMoneys[$templateGood['id']] = $goods_moneys['gets'][$ko] ?? 0;
  98. }
  99. // 总的运费
  100. $expressPrice += $this_express_money;
  101. }
  102. foreach ($goods as &$good) {
  103. $good['express_price'] = $goodsExpressMoneys[$good['id']] ?? $good['express_price'] ?? 0;
  104. }
  105. return [ 'goods' => $goods, 'express_price' => $expressPrice ];
  106. }
  107. /**
  108. * @notes 计算运费
  109. * @param $data
  110. * @param $userAddress
  111. * @return float|int|mixed
  112. * @author 段誉
  113. * @date 2021/7/30 18:37
  114. */
  115. public static function calculate($data, $userAddress)
  116. {
  117. $expressPrice = 0;
  118. $freight = FreightLogic::getFreightsByAddress($data['template_id'], $userAddress);
  119. if (empty($freight)) {
  120. return $expressPrice;
  121. }
  122. $unit = 0;
  123. //按重量计算
  124. if ($freight['charge_way'] == FreightEnum::CHARGE_WAY_WEIGHT) {
  125. $unit = $data['total_weight'];
  126. }
  127. //按件数计算
  128. if ($freight['charge_way'] == FreightEnum::CHARGE_WAY_PIECE) {
  129. $unit = $data['goods_num'];
  130. }
  131. //按体积计算
  132. if ($freight['charge_way'] == FreightEnum::CHARGE_WAY_VOLUME) {
  133. $unit = $data['total_volume'];
  134. }
  135. if ($unit > $freight['first_unit'] && $freight['continue_unit'] > 0) {
  136. $left = ceil(($unit - $freight['first_unit']) / $freight['continue_unit']);//取整
  137. return $freight['first_money'] + $left * $freight['continue_money'];
  138. } else {
  139. return $freight['first_money'];
  140. }
  141. }
  142. /**
  143. * @notes 通过用户地址获取运费模板
  144. * @param $templateId
  145. * @param $address
  146. * @return mixed
  147. * @author 段誉
  148. * @date 2021/7/30 18:36
  149. */
  150. public static function getFreightsByAddress($templateId, $address)
  151. {
  152. $districtId = $address['district_id'];
  153. $cityId = $address['city_id'];
  154. $provinceId = $address['province_id'];
  155. $freights = Freight::alias('f')
  156. ->join('freight_config c', 'c.freight_id = f.id')
  157. ->where('f.id', $templateId)
  158. ->order(['f.id' => 'desc', 'c.id' => 'desc'])
  159. ->select();
  160. $nationalFreight = [];
  161. foreach ($freights as $freight) {
  162. $regionIds = explode(',', $freight['region_id']);
  163. if (in_array($districtId, $regionIds)) {
  164. return $freight;
  165. }
  166. if (in_array($cityId, $regionIds)) {
  167. return $freight;
  168. }
  169. if (in_array($provinceId, $regionIds)) {
  170. return $freight;
  171. }
  172. //全国统一运费
  173. if (100000 == $freight['region_id']) {
  174. $nationalFreight = $freight;
  175. }
  176. }
  177. //会员的省市区id在商家的运费模板(指定地区)中找不到,查一下商家的全国运费模板
  178. return $nationalFreight;
  179. }
  180. /**
  181. * @notes 模板中指定地区id是否存在
  182. * @param $freights
  183. * @param $regionId
  184. * @return bool
  185. * @author 段誉
  186. * @date 2021/7/30 18:36
  187. */
  188. public static function isExistRegionId($freights, $regionId)
  189. {
  190. foreach ($freights as $freight) {
  191. $regionIds = explode(',', $freight['region_id']);
  192. if (in_array($regionId, $regionIds)) {
  193. return $freight;
  194. }
  195. }
  196. return false;
  197. }
  198. /**
  199. * 获取占比值
  200. * @Author lbzy
  201. * @DateTime 2020-11-29T20:48:28+0800
  202. */
  203. static function array_proportion(float $totals, array $proportions, int $point_length = 0, string $remain_deal = null, array $totals_max = []) : array
  204. {
  205. $proportions = array_values($proportions);
  206. $totals_max = array_values($totals_max);
  207. $result = [];
  208. $sums = array_sum($proportions);
  209. $point_length = max($point_length, 0);
  210. $multiple = pow(10, $point_length);
  211. $gets = [];
  212. $gets2 = 0;
  213. foreach ($proportions as $proportion) {
  214. $get = $sums == 0 ? 0 : bcadd(floor($proportion * $multiple * $totals / $sums ) / $multiple, 0, $point_length);
  215. $gets[] = $get;
  216. $gets2 = bcadd($get, $gets2, $point_length);
  217. }
  218. $gets_length = count($gets);
  219. $over = bcsub($gets2, $totals, $point_length);
  220. $remain = $over > 0 ? 0 : bcsub($totals, $gets2, $point_length);
  221. // 多出 按顺序扣减
  222. while ($over > 0) {
  223. foreach ($gets as $key => $get) {
  224. $over_must = min($get, $over);
  225. $gets[$key] = bcsub($get, $over_must, $point_length);
  226. $over = bcsub($over, $over_must, $point_length);
  227. }
  228. }
  229. // 剩下的放哪里
  230. switch ($remain_deal) {
  231. //
  232. case 'start':
  233. $gets[0] = bcadd($gets[0], $remain, $point_length);
  234. $remain = '0';
  235. break;
  236. //
  237. case 'end':
  238. $gets[$gets_length - 1] = bcadd($gets[$gets_length - 1], $remain, $point_length);
  239. $remain = '0';
  240. break;
  241. // 自动填充
  242. case 'auto':
  243. foreach ($gets as $ko => $vo) {
  244. if ($vo < $totals_max[$ko]) {
  245. $remain_must = min($remain, $totals_max[$ko] - $vo);
  246. $gets[$ko] = bcadd($gets[$ko] + $remain_must, 0, $point_length);
  247. $remain = bcsub($remain, $remain_must, $point_length);
  248. }
  249. }
  250. break;
  251. default:
  252. break;
  253. }
  254. return [ 'gets' => $gets, 'remain' => $remain ];
  255. }
  256. }