|
@ -59,6 +59,9 @@ class OrderLogic extends Component |
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
$tra = Yii::$app->db->beginTransaction(); |
|
|
try { |
|
|
try { |
|
|
$order = new Order(); |
|
|
$order = new Order(); |
|
|
|
|
|
$order->user_id = Yii::$app->user->getId(); |
|
|
|
|
|
$order->type = Order::TYPE_SHOPPING; |
|
|
|
|
|
$order->order_sn = Helper::timeRandomNum(3, 'W'); |
|
|
if (!$order->save()) { |
|
|
if (!$order->save()) { |
|
|
throw new ServerErrorHttpException('服务器创建订单失败'); |
|
|
throw new ServerErrorHttpException('服务器创建订单失败'); |
|
|
} |
|
|
} |
|
@ -67,7 +70,11 @@ class OrderLogic extends Component |
|
|
} else { |
|
|
} else { |
|
|
$this->addGoodsByCart($cartIds, $order->id);/*通过购物车添加订单商品*/ |
|
|
$this->addGoodsByCart($cartIds, $order->id);/*通过购物车添加订单商品*/ |
|
|
} |
|
|
} |
|
|
|
|
|
$this->getShippingType($order); |
|
|
$this->saveGoodsInfo($order);/*保存订单商品信息*/ |
|
|
$this->saveGoodsInfo($order);/*保存订单商品信息*/ |
|
|
|
|
|
if (!$order->save()) { |
|
|
|
|
|
throw new Exception('服务器创建订单失败'); |
|
|
|
|
|
} |
|
|
$tra->commit(); |
|
|
$tra->commit(); |
|
|
Helper::createdResponse($order, $this->viewAction); |
|
|
Helper::createdResponse($order, $this->viewAction); |
|
|
return $order; |
|
|
return $order; |
|
@ -110,6 +117,8 @@ class OrderLogic extends Component |
|
|
case self::TYPE_CHANGE_TAKING_SITE: |
|
|
case self::TYPE_CHANGE_TAKING_SITE: |
|
|
$this->changeTakingSite($order, $data); |
|
|
$this->changeTakingSite($order, $data); |
|
|
break; |
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); |
|
|
} |
|
|
} |
|
|
if (!$order->save()) { |
|
|
if (!$order->save()) { |
|
|
throw new ServerErrorHttpException('服务器更新订单失败'); |
|
|
throw new ServerErrorHttpException('服务器更新订单失败'); |
|
@ -123,6 +132,25 @@ class OrderLogic extends Component |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*----------------------------------------- 华丽的分割线 -----------------------------------------*/ |
|
|
/*----------------------------------------- 华丽的分割线 -----------------------------------------*/ |
|
|
|
|
|
/** |
|
|
|
|
|
* @param $order |
|
|
|
|
|
* @return int |
|
|
|
|
|
* @throws BadRequestHttpException |
|
|
|
|
|
* @throws NotFoundHttpException |
|
|
|
|
|
*/ |
|
|
|
|
|
private function getShippingType($order) |
|
|
|
|
|
{ |
|
|
|
|
|
$allOrderGoods = $this->findOrderGoods($order->id); |
|
|
|
|
|
$goods = $this->findGoods($allOrderGoods[0]->goods_id); |
|
|
|
|
|
if ($goods->is_express) { |
|
|
|
|
|
return Order::SHIPPING_TYPE_EXPRESS; |
|
|
|
|
|
} |
|
|
|
|
|
if ($goods->is_taking) { |
|
|
|
|
|
return Order::SHIPPING_TYPE_PICKED_UP; |
|
|
|
|
|
} |
|
|
|
|
|
throw new BadRequestHttpException('配送方式异常'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param Order $order |
|
|
* @param Order $order |
|
|
* @param array $data |
|
|
* @param array $data |
|
@ -138,6 +166,9 @@ class OrderLogic extends Component |
|
|
if ($order->shipping_type !== Order::SHIPPING_TYPE_EXPRESS) { |
|
|
if ($order->shipping_type !== Order::SHIPPING_TYPE_EXPRESS) { |
|
|
throw new BadRequestHttpException('配送方式异常'); |
|
|
throw new BadRequestHttpException('配送方式异常'); |
|
|
} |
|
|
} |
|
|
|
|
|
if ($order->status !== Order::STATUS_UNCONFIRMED) { |
|
|
|
|
|
throw new BadRequestHttpException('该状态下不允许更改配送方式'); |
|
|
|
|
|
} |
|
|
$address = Address::findOne(['id' => $data['address_id'], 'user_id' => Yii::$app->user->getId()]); |
|
|
$address = Address::findOne(['id' => $data['address_id'], 'user_id' => Yii::$app->user->getId()]); |
|
|
if (!$address) { |
|
|
if (!$address) { |
|
|
throw new NotFoundHttpException('收货地址未找到'); |
|
|
throw new NotFoundHttpException('收货地址未找到'); |
|
@ -165,6 +196,9 @@ class OrderLogic extends Component |
|
|
if ($order->shipping_type !== Order::SHIPPING_TYPE_PICKED_UP) { |
|
|
if ($order->shipping_type !== Order::SHIPPING_TYPE_PICKED_UP) { |
|
|
throw new BadRequestHttpException('配送方式异常'); |
|
|
throw new BadRequestHttpException('配送方式异常'); |
|
|
} |
|
|
} |
|
|
|
|
|
if ($order->status !== Order::STATUS_UNCONFIRMED) { |
|
|
|
|
|
throw new BadRequestHttpException('该状态下不允许更改提货地址方式'); |
|
|
|
|
|
} |
|
|
$site = TakingSite::findOne($data['taking_site_id']); |
|
|
$site = TakingSite::findOne($data['taking_site_id']); |
|
|
if (!$site) { |
|
|
if (!$site) { |
|
|
throw new NotFoundHttpException('自提点未找到'); |
|
|
throw new NotFoundHttpException('自提点未找到'); |
|
@ -181,12 +215,14 @@ class OrderLogic extends Component |
|
|
*/ |
|
|
*/ |
|
|
private function switchShippingType($order, $data) |
|
|
private function switchShippingType($order, $data) |
|
|
{ |
|
|
{ |
|
|
if (!isset($data['shipping_type'])) { |
|
|
|
|
|
|
|
|
if (!isset($data['shipping_type']) || |
|
|
|
|
|
!in_array($data['shipping_type'], [Order::SHIPPING_TYPE_PICKED_UP, Order::SHIPPING_TYPE_EXPRESS])) { |
|
|
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); |
|
|
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); |
|
|
} |
|
|
} |
|
|
if ($order->status !== Order::STATUS_UNCONFIRMED) { |
|
|
if ($order->status !== Order::STATUS_UNCONFIRMED) { |
|
|
throw new BadRequestHttpException('订单状态异常'); |
|
|
|
|
|
|
|
|
throw new BadRequestHttpException('该状态下不允许更改配送方式'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$order->shipping_type = $data['shipping_type']; |
|
|
$order->shipping_type = $data['shipping_type']; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -251,7 +287,7 @@ class OrderLogic extends Component |
|
|
private function cancel($order) |
|
|
private function cancel($order) |
|
|
{ |
|
|
{ |
|
|
if ($order->status !== Order::STATUS_NONPAYMENT) { |
|
|
if ($order->status !== Order::STATUS_NONPAYMENT) { |
|
|
throw new BadRequestHttpException('订单状态异常'); |
|
|
|
|
|
|
|
|
throw new BadRequestHttpException('该状态下无法取消订单'); |
|
|
} |
|
|
} |
|
|
$order->status = Order::STATUS_CANCEL; |
|
|
$order->status = Order::STATUS_CANCEL; |
|
|
$this->updateStock($order, self::TYPE_ADD_STOCK);/*更新库存*/ |
|
|
$this->updateStock($order, self::TYPE_ADD_STOCK);/*更新库存*/ |
|
@ -265,7 +301,7 @@ class OrderLogic extends Component |
|
|
private function take($order) |
|
|
private function take($order) |
|
|
{ |
|
|
{ |
|
|
if ($order->status !== Order::STATUS_SHIPMENT_ALL) { |
|
|
if ($order->status !== Order::STATUS_SHIPMENT_ALL) { |
|
|
throw new BadRequestHttpException('订单状态异常'); |
|
|
|
|
|
|
|
|
throw new BadRequestHttpException('该状态下无法确认收货'); |
|
|
} |
|
|
} |
|
|
$order->status = Order::STATUS_FINISH; |
|
|
$order->status = Order::STATUS_FINISH; |
|
|
} |
|
|
} |
|
@ -283,11 +319,13 @@ class OrderLogic extends Component |
|
|
$allOrderGoods = $this->findOrderGoods($id); |
|
|
$allOrderGoods = $this->findOrderGoods($id); |
|
|
foreach ($allOrderGoods as $orderGoods) { |
|
|
foreach ($allOrderGoods as $orderGoods) { |
|
|
/*检查库存*/ |
|
|
/*检查库存*/ |
|
|
Helper::checkStock($orderGoods->goods_id, $orderGoods->goods_count, $orderGoods->sku_id); |
|
|
|
|
|
|
|
|
if (Helper::checkStock($orderGoods->goods_id, $orderGoods->goods_count, $orderGoods->sku_id)) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
if ($orderGoods->sku_id) { |
|
|
if ($orderGoods->sku_id) { |
|
|
$goods = GoodsSku::findOne($orderGoods->sku_id); |
|
|
$goods = GoodsSku::findOne($orderGoods->sku_id); |
|
|
} else { |
|
|
} else { |
|
|
$goods = Goods::findOne($orderGoods->goods_id); |
|
|
|
|
|
|
|
|
$goods = $this->findGoods($orderGoods->goods_id); |
|
|
} |
|
|
} |
|
|
if ($type) { |
|
|
if ($type) { |
|
|
$count = $orderGoods->goods_count; |
|
|
$count = $orderGoods->goods_count; |
|
@ -320,7 +358,6 @@ class OrderLogic extends Component |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param Order $order |
|
|
* @param Order $order |
|
|
* @throws Exception |
|
|
|
|
|
* 保存商品总价格和总数量信息到 |
|
|
* 保存商品总价格和总数量信息到 |
|
|
*/ |
|
|
*/ |
|
|
private function saveGoodsInfo($order) |
|
|
private function saveGoodsInfo($order) |
|
@ -330,9 +367,6 @@ class OrderLogic extends Component |
|
|
$order->goods_amount += $goods->price; |
|
|
$order->goods_amount += $goods->price; |
|
|
$order->goods_count += $goods->goods_count; |
|
|
$order->goods_count += $goods->goods_count; |
|
|
} |
|
|
} |
|
|
if (!$order->save()) { |
|
|
|
|
|
throw new Exception('服务器创建订单失败'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -347,7 +381,7 @@ class OrderLogic extends Component |
|
|
*/ |
|
|
*/ |
|
|
private function addGoods($id, $goodsId, $skuId, $count) |
|
|
private function addGoods($id, $goodsId, $skuId, $count) |
|
|
{ |
|
|
{ |
|
|
$goods = Goods::findOne($goodsId); |
|
|
|
|
|
|
|
|
$goods = $this->findGoods($goodsId); |
|
|
$orderGoods = new OrderGoods(); |
|
|
$orderGoods = new OrderGoods(); |
|
|
$orderGoods->order_id = $id; |
|
|
$orderGoods->order_id = $id; |
|
|
$orderGoods->goods_id = $goodsId; |
|
|
$orderGoods->goods_id = $goodsId; |
|
@ -364,6 +398,12 @@ class OrderLogic extends Component |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param $skuId |
|
|
|
|
|
* @return int |
|
|
|
|
|
* @throws NotFoundHttpException |
|
|
|
|
|
* 获取sku重量 |
|
|
|
|
|
*/ |
|
|
private function skuWeight($skuId) |
|
|
private function skuWeight($skuId) |
|
|
{ |
|
|
{ |
|
|
$sku = GoodsSku::findOne($skuId); |
|
|
$sku = GoodsSku::findOne($skuId); |
|
@ -398,5 +438,22 @@ class OrderLogic extends Component |
|
|
->all(); |
|
|
->all(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param int $goodsId |
|
|
|
|
|
* @return Goods|null |
|
|
|
|
|
* @throws NotFoundHttpException |
|
|
|
|
|
*/ |
|
|
|
|
|
private function findGoods($goodsId) |
|
|
|
|
|
{ |
|
|
|
|
|
$goods = Goods::find() |
|
|
|
|
|
->where(['id' => $goodsId]) |
|
|
|
|
|
->andWhere(['is_delete' => Goods::IS_DELETE_NO]) |
|
|
|
|
|
->andWhere(['is_sale' => Goods::IS_SALE_YES]) |
|
|
|
|
|
->one(); |
|
|
|
|
|
if (!$goods) { |
|
|
|
|
|
throw new NotFoundHttpException("商品[{$goodsId}]未找到"); |
|
|
|
|
|
} |
|
|
|
|
|
return $goods; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |