Fare.php
1.14 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
<?php
//运费计算类
class Fare{
	private $weight = 0;
	public function __construct($weight=0){
		$this->weight = $weight;
	}
	public function calculate($address_id){
		$total = 0;
		$model = new model("fare");
		$fare = $model->where("is_default=1")->find();
		if($fare){
			$addr = $model->table('address')->where("id=$address_id")->find();
			if($addr){
				$city = $addr['city'];
				$first_price = $fare['first_price'];
				$second_price = $fare['second_price'];
				$first_weight = $fare['first_weight'];
				$second_weight = $fare['second_weight'];
				$zoning = unserialize($fare['zoning']);
				foreach ($zoning as $zon) {
					if(preg_match(','.$city.',', ','.$zon['area'].',')>0){
						$first_price = $zon['f_price'];
						$second_price = $zon['s_price'];
						$first_weight = $zon['f_weight'];
						$second_weight = $zon['s_weight'];
						break;
					}
				}
				if($this->weight<=$first_weight) $total = $first_price;
				else{
					$weight = $this->weight - $first_weight;
					$total = $first_price + ceil($weight/$second_weight)*$second_price;
				}
			}
			
		}
		return sprintf("%01.2f",$total);
	}
}