FreightLogic.php 11 KB

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