You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\base\logic;
class Freight extends Base
{
/**
* 获取商品的运费模板并进行计算
* @param int $freight_id 运费模板ID
* @param string $weight 单件商品重量
* @param int $number 商品数量
* @param string $price 商品价格
* @param int $province_id 配送地址省ID
* @param int $city_id 配送地址城市ID
* @param int $area_id 配送地址区ID
* @date 2022-11-22
*/
public function getFreight($freight_id = 0, $weight = '', $number = 0, $price = '', $province_id = 0, $city_id = 0, $area_id = 0)
{
$freight_model = new \app\base\model\freight\Freight();
if (empty($freight_id)) {
return 0;
}
// 获取运费
$freight = $freight_model->getOneData([
['id', '=', $freight_id]
]);
if (empty($freight)) {
return 0;
}
// 判断是否该商品免运费是否满足满X元包邮
if ($price * $number >= $freight['free_money']) {
return 0;
}
$start_standard = 0; //首件或首重
$start_fee = 0;//首费
$add_standard = 0;//续件或续重
$add_fee = 0;//续费
$standard = 0; //进行判断的标准
#TODO 可以将2种计费方式的字段统一到一个字段 运费规则是否符合标准逻辑
if ($freight['type'] == 1) { //计费方式 1--按件计费 2--按重计费
$start_standard = $freight['first_number'];
$start_fee = $freight['first_number_money'];
$add_standard = $freight['second_number'];
$add_fee = $freight['second_number_money'];
$standard = $number;
} else if ($freight['type'] == 2) {
$start_standard = $freight['first_weight'];
$start_fee = $freight['first_weight_money'];
$add_standard = $freight['second_weight'];
$add_fee = $freight['second_weight_money'];
$standard = $weight * $number;
}
if (!empty($freight['fee_conf'])) { //运费模板中运费信息对象,包含默认运费和指定地区运费
foreach ($freight['fee_conf'] as $key => $value) {
if ((!empty($province_id) && array_key_exists($province_id, $value['province_list'])) &&
(!empty($city_id) && array_key_exists($city_id, $value['city_list'])) &&
(!empty($area_id) && array_key_exists($area_id, $value['area_list']))
) {
$start_standard = $value['start_standard'];
$start_fee = $value['start_fee'];
$add_standard = $value['add_standard'];
$add_fee = $value['add_fee'];
break;
}
}
}
// 计算并返回金额
return $this->computedFreight($standard, $start_standard, $start_fee, $add_standard, $add_fee);
}
/**
* 计算并返回金额
* @param int $standard 进行判断的标准
* @param int $start_standard 首个 标准
* @param int $start_fee 首个 收费
* @param int $add_standard 续 标准
* @param int $add_fee 续 收费
* @date 2021-03-01
*/
public function computedFreight($standard = 0, $start_standard = 0, $start_fee = 0, $add_standard = 0, $add_fee = 0)
{
//小于等于首(件/重),运费=取首(件/重)费;以此类推。
if ($standard <= $start_standard) {
return $start_fee;
}
//超出首(件/重)后,超出部分小于等于续(件/重),运费=首重+续费,
return $start_fee + ceil(($standard - $start_standard) / $add_standard) * $add_fee;
}
}