Litestorefreight.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace addons\litestore\model;
use think\Model;
use addons\litestore\Litestore as litestore_add;
class Litestorefreight extends Model
{
// 表名
protected $name = 'litestore_freight';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
'method_text'
];
protected static function init()
{
self::afterInsert(function ($row) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
});
}
public function getMethodList()
{
return ['10' => __('Method 10'),'20' => __('Method 20')];
}
public function getMethodTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['method']) ? $data['method'] : '');
$list = $this->getMethodList();
return isset($list[$value]) ? $list[$value] : '';
}
public function rule()
{
return $this->hasMany('Litestorefreightrule');
}
public function createDeliveryRule($data)
{
$save = [];
$connt = count($data['region']);
for ($i = 0; $i < $connt; $i++) {
$save[] = [
'region' => $data['region'][$i],
'first' => $data['first'][$i],
'first_fee' => $data['first_fee'][$i],
'additional' => $data['additional'][$i],
'additional_fee' => $data['additional_fee'][$i],
];
}
$this->rule()->delete();
return $this->rule()->saveAll($save);
}
/**
* 运费模板详情
* @param $delivery_id
* @return null|static
* @throws \think\exception\DbException
*/
public function detail($delivery_id)
{
return self::get($delivery_id, ['rule']);
}
public function calcTotalFee($total_num, $total_weight, $city_id)
{
$rule = []; // 当前规则
foreach ($this['rule'] as $item) {
if (in_array($city_id, $item['region_data'])) {
$rule = $item;
break;
}
}
// 商品总数量or总重量
$total = $this['method']=== '10' ? $total_num : $total_weight;
if ($total <= $rule['first']) {
return number_format($rule['first_fee'], 2);
}
// 续件or续重 数量
$additional = $total - $rule['first'];
if ($additional <= $rule['additional']) {
return number_format($rule['first_fee'] + $rule['additional_fee'], 2);
}
// 计算续重/件金额
if ($rule['additional'] < 1) {
// 配送规则中续件为0
$additionalFee = 0.00;
} else {
$additionalFee = bcdiv($rule['additional_fee'], $rule['additional'], 2) * $additional;
}
return number_format($rule['first_fee'] + $additionalFee, 2);
}
public static function freightRule($allExpressPrice)
{
$Temp_litestore = new litestore_add();
$wxapp = $Temp_litestore->GetCfg();
$freight_rule = $wxapp['freight'];
$expressPrice = 0.00;
switch ($freight_rule) {
case '10': // 叠加
$expressPrice = array_sum($allExpressPrice);
break;
case '20': // 以最低运费结算
$expressPrice = min($allExpressPrice);
break;
case '30': // 以最高运费结算
$expressPrice = max($allExpressPrice);
break;
}
return $expressPrice;
}
public function checkAddress($city_id)
{
$cityIds = explode(',', implode(',', array_column($this['rule'], 'region')));
return in_array($city_id, $cityIds);
}
}