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