添加运费计算方法
parent
9757b5a19e
commit
41e8fbf13b
|
|
@ -3,12 +3,16 @@
|
|||
namespace App\Admin\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\ShippingService;
|
||||
use Dcat\Admin\Layout\Content;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
public function index(Content $content)
|
||||
{
|
||||
$shippingService = new ShippingService();
|
||||
$amount = $shippingService->countShippingAmount(3560, 2000, 1, 2225);
|
||||
dd($amount);
|
||||
return $content
|
||||
->header('Dashboard')
|
||||
->description('Description...');
|
||||
|
|
|
|||
|
|
@ -12,6 +12,9 @@ class ShippingRule extends Model
|
|||
use HasFactory;
|
||||
use HasDateTimeFormatter;
|
||||
|
||||
public const TYPE_FREE = 1;//包邮
|
||||
public const TYPE_WEIGHT = 2;//计算重量
|
||||
|
||||
// protected $casts = [
|
||||
// 'info' => JsonArray::class,
|
||||
// ];
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\BizException;
|
||||
use App\Models\ShippingRule;
|
||||
|
||||
class ShippingService
|
||||
{
|
||||
/**
|
||||
* 运费模板根据地址,重量, 商品金额 计算运费; 重量传g,金额传分
|
||||
*
|
||||
* @param integer $weight
|
||||
* @param integer $templateId
|
||||
* @return void
|
||||
*/
|
||||
public function countShippingAmount(int $weight, int $totalAmount, int $templateId, int $zoneId)
|
||||
{
|
||||
$shipping_amount = 0;
|
||||
//获取运费规则;
|
||||
$rules = ShippingRule::with('zones')->where('template_id', $templateId)->get();
|
||||
$canShipping = true;//是否支持寄送
|
||||
//如果该模板下无运费规则,则直接包邮
|
||||
if ($rules->count() > 0) {
|
||||
//按包邮/计重
|
||||
$rules = $rules->sortBy('type');
|
||||
foreach ($rules as $rule) {
|
||||
$canShipping = false;
|
||||
$_ruleInfo = json_decode($rule->info, true);
|
||||
|
||||
if (in_array($zoneId, $rule->zones->pluck('id')->toArray())) {
|
||||
$canShipping = true;
|
||||
if ($rule->type == ShippingRule::TYPE_FREE) {
|
||||
if ($totalAmount >= bcmul($_ruleInfo['threshold'], 100)) {//在包邮价格范围内
|
||||
break;//跳出foreach循环, 直接包邮
|
||||
}
|
||||
} elseif ($rule->type == ShippingRule::TYPE_WEIGHT) {
|
||||
$_firstWeight = (int) bcmul($_ruleInfo['first_weight'], 1000);
|
||||
$_firstWamount = (int) bcmul($_ruleInfo['first_w_amount'], 100);
|
||||
$_contiueWeight = (int) bcmul($_ruleInfo['continue_weight'], 1000);
|
||||
$_contiueWamount = (int) bcmul($_ruleInfo['continue_w_amount'], 100);
|
||||
if ($weight <= $_firstWeight) {
|
||||
$shipping_amount = $_firstWamount;
|
||||
break;
|
||||
} else {//计算续重价格
|
||||
$num = (int) bcdiv(($weight - $_firstWeight), $_contiueWeight);
|
||||
$shipping_amount = (int) bcadd($_firstWamount, bcmul($num+1, $_contiueWamount));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$canShipping) {
|
||||
throw new BizException('当前选择的地址有部分商品不支持配送');
|
||||
}
|
||||
|
||||
return $this->returnAmount($shipping_amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一返回运费格式
|
||||
*
|
||||
* @param [type] $shipping_amount
|
||||
* @return void
|
||||
*/
|
||||
protected function returnAmount($shipping_amount)
|
||||
{
|
||||
return $shipping_amount;
|
||||
// return bcdiv($shipping_amount, 100, 2);
|
||||
}
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ trait SkuInfo
|
|||
*/
|
||||
protected static function createSellPrice(float $price, float $specPrice)
|
||||
{
|
||||
return bcadd($price, $specPrice);
|
||||
return bcadd($price, $specPrice, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -41,7 +41,7 @@ trait SkuInfo
|
|||
*/
|
||||
protected static function createMarketPrice(float $price, float $specPrice)
|
||||
{
|
||||
return bcadd($price, 0);
|
||||
return bcadd($price, 0, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -53,7 +53,7 @@ trait SkuInfo
|
|||
*/
|
||||
protected static function createCostPrice(float $price, float $specPrice)
|
||||
{
|
||||
return bcadd($price, 0);
|
||||
return bcadd($price, 0, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -65,7 +65,7 @@ trait SkuInfo
|
|||
*/
|
||||
protected static function createVipPrice(?float $price, float $specPrice)
|
||||
{
|
||||
return !is_null($price) ? bcadd($price, $specPrice) : null;
|
||||
return !is_null($price) ? bcadd($price, $specPrice, 2) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ class CreateShippingRulesTable extends Migration
|
|||
Schema::create('shipping_rules', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('template_id')->comment('模板ID');
|
||||
$table->unsignedTinyInteger('type')->default(0)->comment('规则类型:0包邮,1计重');
|
||||
$table->unsignedTinyInteger('type')->default(0)->comment('规则类型:1包邮,2计重');
|
||||
$table->json('info')->nullable()->comment('规则内容');
|
||||
$table->string('remarks')->nullable()->comment('规则备注');
|
||||
$table->timestamps();
|
||||
|
|
|
|||
Loading…
Reference in New Issue