FreightLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. $area_flag = ConfigService::get('shop', 'area_flag');
  51. foreach ($goods as &$good) {
  52. $good['total_weight'] = $good['weight'] * $good['goods_num'];
  53. $good['total_volume'] = $good['volume'] * $good['goods_num'];
  54. // 统一邮费
  55. if ($good['express_type'] == 2) {
  56. $good['express_price'] = round($good['express_money'] * $good['goods_num'], 2);
  57. $expressPrice += $good['express_price'];
  58. }
  59. // 指定运费模板
  60. if ($good['express_type'] == 3 && $good['express_template_id'] > 0) {
  61. $templateList[$good['express_template_id']][] = $good;
  62. }
  63. }
  64. if($area_flag == 1){
  65. $expressPrice += $good['sub_price'];//self::calculate2($goods, $userAddress);
  66. }else {
  67. foreach ($templateList as $templateId => $templateGoods) {
  68. $freight = FreightLogic::getFreightsByAddress($templateId, $userAddress);
  69. if (empty($freight)) {
  70. continue;
  71. }
  72. switch ($freight['charge_way']) {
  73. // 件数
  74. case FreightEnum::CHARGE_WAY_PIECE:
  75. $nums = array_sum(array_column($templateGoods, 'goods_num'));
  76. $real_count_field = 'goods_num';
  77. break;
  78. // 重量
  79. case FreightEnum::CHARGE_WAY_WEIGHT:
  80. $nums = array_sum(array_column($templateGoods, 'total_weight'));
  81. $real_count_field = 'total_weight';
  82. break;
  83. // 体积
  84. case FreightEnum::CHARGE_WAY_VOLUME:
  85. $nums = array_sum(array_column($templateGoods, 'total_volume'));
  86. $real_count_field = 'total_volume';
  87. break;
  88. default:
  89. continue 2;
  90. }
  91. // 计算运费
  92. if ($nums > $freight['first_unit'] && $freight['continue_unit'] > 0) {
  93. $left = ceil(($nums - $freight['first_unit']) / $freight['continue_unit']);
  94. $this_express_money = $freight['first_money'] + $left * $freight['continue_money'];
  95. } else {
  96. $this_express_money = $freight['first_money'];
  97. }
  98. // 计算每个商品 所占运费金额
  99. $goods_moneys = static::array_proportion($this_express_money, array_column($templateGoods, $real_count_field), 2, 'end');
  100. foreach ($templateGoods as $ko => $templateGood) {
  101. $goodsExpressMoneys[$templateGood['id']] = $goods_moneys['gets'][$ko] ?? 0;
  102. }
  103. // 总的运费
  104. $expressPrice += $this_express_money;
  105. }
  106. foreach ($goods as &$good) {
  107. $good['express_price'] = $goodsExpressMoneys[$good['id']] ?? $good['express_price'] ?? 0;
  108. }
  109. }
  110. return [ 'goods' => $goods, 'express_price' => $expressPrice ];
  111. }
  112. /**
  113. * @notes 计算运费
  114. * @param $data
  115. * @param $userAddress
  116. * @return float|int|mixed
  117. * @author 段誉
  118. * @date 2021/7/30 18:37
  119. */
  120. public static function calculate($data, $userAddress)
  121. {
  122. $expressPrice = 0;
  123. $freight = FreightLogic::getFreightsByAddress($data['template_id'], $userAddress);
  124. if (empty($freight)) {
  125. return $expressPrice;
  126. }
  127. $unit = 0;
  128. //按重量计算
  129. if ($freight['charge_way'] == FreightEnum::CHARGE_WAY_WEIGHT) {
  130. $unit = $data['total_weight'];
  131. }
  132. //按件数计算
  133. if ($freight['charge_way'] == FreightEnum::CHARGE_WAY_PIECE) {
  134. $unit = $data['goods_num'];
  135. }
  136. //按体积计算
  137. if ($freight['charge_way'] == FreightEnum::CHARGE_WAY_VOLUME) {
  138. $unit = $data['total_volume'];
  139. }
  140. if ($unit > $freight['first_unit'] && $freight['continue_unit'] > 0) {
  141. $left = ceil(($unit - $freight['first_unit']) / $freight['continue_unit']);//取整
  142. return $freight['first_money'] + $left * $freight['continue_money'];
  143. } else {
  144. return $freight['first_money'];
  145. }
  146. }
  147. //特定地区运费计算
  148. public static function calculate2($data, $userAddress)
  149. {
  150. $goods_price=0;
  151. foreach ($data as &$v){
  152. outFileLog($v,'fright_fee','$v');
  153. $goods_price +=isset($v['sub_price']) ?? 0;
  154. }
  155. return $goods_price;
  156. }
  157. /**
  158. * @notes 通过用户地址获取运费模板
  159. * @param $templateId
  160. * @param $address
  161. * @return mixed
  162. * @author 段誉
  163. * @date 2021/7/30 18:36
  164. */
  165. public static function getFreightsByAddress($templateId, $address)
  166. {
  167. $districtId = $address['district_id'];
  168. $cityId = $address['city_id'];
  169. $provinceId = $address['province_id'];
  170. $freights = Freight::alias('f')
  171. ->join('freight_config c', 'c.freight_id = f.id')
  172. ->where('f.id', $templateId)
  173. ->order(['f.id' => 'desc', 'c.id' => 'desc'])
  174. ->select();
  175. $nationalFreight = [];
  176. foreach ($freights as $freight) {
  177. $regionIds = explode(',', $freight['region_id']);
  178. if (in_array($districtId, $regionIds)) {
  179. return $freight;
  180. }
  181. if (in_array($cityId, $regionIds)) {
  182. return $freight;
  183. }
  184. if (in_array($provinceId, $regionIds)) {
  185. return $freight;
  186. }
  187. //全国统一运费
  188. if (100000 == $freight['region_id']) {
  189. $nationalFreight = $freight;
  190. }
  191. }
  192. //会员的省市区id在商家的运费模板(指定地区)中找不到,查一下商家的全国运费模板
  193. return $nationalFreight;
  194. }
  195. /**
  196. * @notes 模板中指定地区id是否存在
  197. * @param $freights
  198. * @param $regionId
  199. * @return bool
  200. * @author 段誉
  201. * @date 2021/7/30 18:36
  202. */
  203. public static function isExistRegionId($freights, $regionId)
  204. {
  205. foreach ($freights as $freight) {
  206. $regionIds = explode(',', $freight['region_id']);
  207. if (in_array($regionId, $regionIds)) {
  208. return $freight;
  209. }
  210. }
  211. return false;
  212. }
  213. /**
  214. * 获取占比值
  215. * @Author lbzy
  216. * @DateTime 2020-11-29T20:48:28+0800
  217. */
  218. static function array_proportion(float $totals, array $proportions, int $point_length = 0, string $remain_deal = null, array $totals_max = []) : array
  219. {
  220. $proportions = array_values($proportions);
  221. $totals_max = array_values($totals_max);
  222. $result = [];
  223. $sums = array_sum($proportions);
  224. $point_length = max($point_length, 0);
  225. $multiple = pow(10, $point_length);
  226. $gets = [];
  227. $gets2 = 0;
  228. foreach ($proportions as $proportion) {
  229. $get = $sums == 0 ? 0 : bcadd(floor($proportion * $multiple * $totals / $sums ) / $multiple, 0, $point_length);
  230. $gets[] = $get;
  231. $gets2 = bcadd($get, $gets2, $point_length);
  232. }
  233. $gets_length = count($gets);
  234. $over = bcsub($gets2, $totals, $point_length);
  235. $remain = $over > 0 ? 0 : bcsub($totals, $gets2, $point_length);
  236. // 多出 按顺序扣减
  237. while ($over > 0) {
  238. foreach ($gets as $key => $get) {
  239. $over_must = min($get, $over);
  240. $gets[$key] = bcsub($get, $over_must, $point_length);
  241. $over = bcsub($over, $over_must, $point_length);
  242. }
  243. }
  244. // 剩下的放哪里
  245. switch ($remain_deal) {
  246. //
  247. case 'start':
  248. $gets[0] = bcadd($gets[0], $remain, $point_length);
  249. $remain = '0';
  250. break;
  251. //
  252. case 'end':
  253. $gets[$gets_length - 1] = bcadd($gets[$gets_length - 1], $remain, $point_length);
  254. $remain = '0';
  255. break;
  256. // 自动填充
  257. case 'auto':
  258. foreach ($gets as $ko => $vo) {
  259. if ($vo < $totals_max[$ko]) {
  260. $remain_must = min($remain, $totals_max[$ko] - $vo);
  261. $gets[$ko] = bcadd($gets[$ko] + $remain_must, 0, $point_length);
  262. $remain = bcsub($remain, $remain_must, $point_length);
  263. }
  264. }
  265. break;
  266. default:
  267. break;
  268. }
  269. return [ 'gets' => $gets, 'remain' => $remain ];
  270. }
  271. }