glhcp/server/app/common/model/Freight.php

117 lines
4.5 KiB
PHP
Raw Blame History

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
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\common\model;
use app\common\basics\Models;
use app\common\enum\FreightEnum;
use think\facade\Db;
/**
* 运费模板
*/
class Freight extends Models
{
protected $name = 'freight';
protected $autoWriteTimestamp = true;
public static function getChargeWay($type)
{
$data = [
FreightEnum::CHARGE_WAY_WEIGHT => '按重量计费',
FreightEnum::CHARGE_WAY_VOLUME => '按体积计费',
FreightEnum::CHARGE_WAY_PIECE => '按件计费',
];
if ($type === true) {
return $data;
}
return $data[$type] ?? '未知';
}
public function getChargeWayTextAttr($value, $data)
{
return self::getChargeWay($data['charge_way']);
}
public static function getNameColumn($shop_id)
{
$lists = self::where(['shop_id'=>$shop_id])->column('id,name', 'id');
return empty($lists) ? [] : $lists;
}
public function configs()
{
return $this->hasMany('freight_config', 'freight_id', 'id');
}
/**
* 根据收货地址-商品信息-运费模板id-商品数量计算运费
*/
public function sumFreight($address, $goods_item, $freight_id, $num)
{
//判断用户收货地址适用模板
$freight_config = FreightConfig::where('freight_id', $freight_id)->select()->toArray();
$freight_config_item = [];
foreach ($freight_config as $key => $value) {
$district_check = strpos($value['region'], (string)$address['district_id']);
$city_check = strpos($value['region'], (string)$address['city_id']);
$province_check = strpos($value['region'], (string)$address['province_id']);
if ($district_check !== false) {//区域匹配模板
$freight_config_item = $value;
}
if ($city_check !== false) {//市匹配模板
$freight_config_item = $value;
}
if ($province_check !== false) {//省匹配模板
$freight_config_item = $value;
}
if ($district_check == false && $city_check == false && $province_check == false) {
if ($value['region'] == 'all') {
$freight_config_item = $value;
}
}
}
$charge_way = $this->where('id', $freight_id)->value('charge_way');
$price = 0;//运费
switch ($charge_way) {
case $charge_way == 1://重量计费
$unified_num = $goods_item['weight'] * $num;
break;
case $charge_way == 2://体积计费
$unified_num = $goods_item['volume'] * $num;
break;
case $charge_way == 3://件数计费
$unified_num = $num;
break;
}
if ($unified_num <= $freight_config_item['first_unit']) {//小于首件数
$price = $freight_config_item['first_money'];
}
if ($unified_num > $freight_config_item['first_unit']) {//大于首件数 计算公式=(((商品数量-首件数)/续件数)*续件费用)+首件费用
$continue_unit = ceil(($unified_num - $freight_config_item['first_unit']) / $freight_config_item['continue_unit']);//续件数向上取整
$price = ($continue_unit * $freight_config_item['continue_money']) + $freight_config_item['first_money'];
}
return $price;
}
}