|
|
@ -34,27 +34,24 @@ class ExpressLogic extends BaseObject |
|
|
|
*/ |
|
|
|
public function countShippingAmount($order) |
|
|
|
{ |
|
|
|
if ($order->shipping_type == Order::SHIPPING_TYPE_PICKED_UP) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
$this->order = $order; |
|
|
|
$allGoods = $this->findDifferentExpressTypeGoods($order);/*获取不同运费计算模式的商品*/ |
|
|
|
$allGoods = $this->findDifferentExpressTypeGoods();/*获取不同运费计算模式的商品*/ |
|
|
|
$uniformPostage = $this->countGoodsExpress($allGoods['uniformPostageGoods'], Goods::EXPRESS_TYPE_UNIFORM_POSTAGE);/*计算统一运费模式下所有商品运费*/ |
|
|
|
$expressTemplate = $this->countGoodsExpress($allGoods['uniformPostageGoods'], Goods::EXPRESS_TYPE_EXPRESS_TEMPLATE);/*计算运费模板模式下所有商品的运费*/ |
|
|
|
return $uniformPostage > $expressTemplate ? $uniformPostage : $expressTemplate;/*比较两种不同运费计算模式下,取金额大的值为最终收取运费*/ |
|
|
|
$expressTemplate = $this->countGoodsExpress($allGoods['useTemplateGoods'], Goods::EXPRESS_TYPE_EXPRESS_TEMPLATE);/*计算运费模板模式下所有商品的运费*/ |
|
|
|
$amount = $uniformPostage > $expressTemplate ? $uniformPostage : $expressTemplate;/*比较两种不同运费计算模式下,取金额大的值为最终收取运费*/ |
|
|
|
return $amount / 100; /*将金额单位[分]转化为[元]*/ |
|
|
|
} |
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------------------*/ |
|
|
|
/** |
|
|
|
* @param $order |
|
|
|
* @return array |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* 获取不同运费计算模式的商品 |
|
|
|
*/ |
|
|
|
private function findDifferentExpressTypeGoods($order) |
|
|
|
private function findDifferentExpressTypeGoods() |
|
|
|
{ |
|
|
|
$uniformPostage = $useTemplate = []; |
|
|
|
$allGoods = OrderGoods::findAll(['order_id' => $order->id, 'user_id' => Yii::$app->user->getId()]); |
|
|
|
$allGoods = OrderGoods::findAll(['order_id' => $this->order->id]); |
|
|
|
foreach ($allGoods as $orderGoods) { |
|
|
|
$goods = Goods::findOne(['id' => $orderGoods->goods_id, 'is_sale' => Goods::IS_SALE_YES, 'is_delete' => Goods::IS_DELETE_NO]); |
|
|
|
if (!$goods) { |
|
|
@ -82,38 +79,28 @@ class ExpressLogic extends BaseObject |
|
|
|
*/ |
|
|
|
private function countGoodsExpress($allGoods, $type) |
|
|
|
{ |
|
|
|
if (!$type == Goods::EXPRESS_TYPE_UNIFORM_POSTAGE) { |
|
|
|
if ($type == Goods::EXPRESS_TYPE_UNIFORM_POSTAGE) { |
|
|
|
$amount = 0; |
|
|
|
foreach ($allGoods['object'] as $goods) { |
|
|
|
$amount = $goods->uniform_postage > $amount ? $goods->uniform_postage : $amount; |
|
|
|
foreach ($allGoods as $goods) { |
|
|
|
$amount = $goods['object']->uniform_postage > $amount ? $goods['object']->uniform_postage : $amount; |
|
|
|
} |
|
|
|
return $amount; |
|
|
|
} else { |
|
|
|
$extraPrice = 0; |
|
|
|
$areasIds = []; |
|
|
|
foreach ($allGoods['object'] as $k => $goods) { |
|
|
|
foreach ($allGoods as $goods) { |
|
|
|
$areasIds[] = $this->getAreaId($goods['object']->express_template); |
|
|
|
} |
|
|
|
/*获取首重运费和对应的计费规则id*/ |
|
|
|
$basic = $this->getBasicPriceAndAreaId($areasIds); |
|
|
|
foreach ($allGoods as $goods) { |
|
|
|
/*计算增重运费*/ |
|
|
|
$extraPrice += $this->countExtraAmount($allGoods['count'][$k], $allGoods['weight'][$k], $goods->express_template); |
|
|
|
$areasIds[] = $this->getAreaId($goods->express_template); |
|
|
|
$extraPrice += $this->countExtraAmount($goods['count'], $goods['weight'], $goods['object']->express_template, $basic['id']); |
|
|
|
} |
|
|
|
$basic = $this->getBasicPriceAndAreaId($areasIds);/*获取首重运费和对应的计费规则id*/ |
|
|
|
/*返回所有按运费模板计费的商品运费(基础运费 + 所有商品增重运费 - 多余的增重运费)*/ |
|
|
|
return $basic['price'] + $extraPrice - $this->countSurplusPrice($basic['id']); |
|
|
|
return ($basic['price'] + $extraPrice); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $areaId |
|
|
|
* @return float|int |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* 计算多余(多算的)运费 |
|
|
|
* 选定作为基础运费的计费规则对应商品基础数量内的增重费用 |
|
|
|
*/ |
|
|
|
private function countSurplusPrice($areaId) |
|
|
|
{ |
|
|
|
$area = $this->findArea($areaId); |
|
|
|
return $area->extra_price * ceil($area->basic_count / $area->extra_count); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $areaIds |
|
|
@ -139,20 +126,29 @@ class ExpressLogic extends BaseObject |
|
|
|
* @param $count |
|
|
|
* @param $weight |
|
|
|
* @param $templateId |
|
|
|
* @param $areaId |
|
|
|
* @return float|int |
|
|
|
* @throws NotFoundHttpException |
|
|
|
* 计算增重运费 |
|
|
|
*/ |
|
|
|
private function countExtraAmount($count, $weight, $templateId) |
|
|
|
private function countExtraAmount($count, $weight, $templateId, $areaId) |
|
|
|
{ |
|
|
|
$area = $this->findArea($this->getAreaId($templateId)); |
|
|
|
$template = $this->findTemplate($templateId); |
|
|
|
if ($template->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) { |
|
|
|
$amount = $area->extra_price * ceil($weight / $area->extra_count); |
|
|
|
if ($area->id == $areaId) { |
|
|
|
$amount = $weight - $area->basic_count > 0 ? $weight - $area->basic_count : 0; |
|
|
|
} else { |
|
|
|
$amount = $area->extra_price * ceil($count / $area->extra_count); |
|
|
|
$amount = $weight; |
|
|
|
} |
|
|
|
return $amount; |
|
|
|
} else { |
|
|
|
if ($area->id == $areaId) { |
|
|
|
$amount = $count - $area->basic_count > 0 ? $count - $area->basic_count : 0; |
|
|
|
} else { |
|
|
|
$amount = $count; |
|
|
|
} |
|
|
|
} |
|
|
|
return $area->extra_price * ceil($amount / $area->extra_count); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -171,7 +167,7 @@ class ExpressLogic extends BaseObject |
|
|
|
return $area->id; |
|
|
|
} |
|
|
|
} |
|
|
|
throw new NotFoundHttpException('超出配送范围(无对应地区运费计算规则)'); |
|
|
|
throw new NotFoundHttpException('超出配送范围(未找到对应地区运费计算规则)'); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|