You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
5.8 KiB
163 lines
5.8 KiB
<?php
|
|
|
|
|
|
namespace backend\modules\shop\logic\delivery;
|
|
|
|
use backend\modules\shop\models\ars\Delivery;
|
|
use backend\modules\shop\models\ars\DeliveryGoods;
|
|
use backend\modules\shop\models\ars\Order;
|
|
use backend\modules\shop\models\ars\OrderGoods;
|
|
use Yii;
|
|
use yii\db\Exception;
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
class DeliveryManager
|
|
{
|
|
/**
|
|
* @param Order $order
|
|
* @param Delivery $delivery
|
|
* @return array
|
|
* 订单发货
|
|
*/
|
|
public static function orderDelivery($order, $delivery)
|
|
{
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
try {
|
|
$data = Yii::$app->request->post();
|
|
if (empty($data['deliveryGoods'])) {
|
|
throw new Exception('缺少发货商品信息');
|
|
}
|
|
$delivery->load($data);
|
|
$delivery->order_id = $order->id;
|
|
if (!$delivery->save()) {
|
|
throw new Exception('保存物流信息失败');
|
|
}
|
|
|
|
/*发货商品数据*/
|
|
$orderStatus = self::saveDeliveryGoods($data['deliveryGoods'], $delivery->id);
|
|
$order->status = $orderStatus;
|
|
if (!$order->save()) {
|
|
throw new Exception('order shipping_status update false');
|
|
}
|
|
|
|
if ($orderStatus == Order::STATUS_SHIPMENT_PORTION || Delivery::findOne(['!=', 'id', $delivery->id])) {
|
|
$delivery->type = Delivery::TYPE_SHIPMENT_PORTION;
|
|
} else {
|
|
$delivery->type = Delivery::TYPE_SHIPMENT_ALL;
|
|
}
|
|
if (!$delivery->save()) {
|
|
throw new Exception('保存物流信息失败');
|
|
}
|
|
|
|
$transaction->commit();
|
|
return ['status' => true];
|
|
} catch (Exception $e) {
|
|
$transaction->rollBack();
|
|
return ['status' => false, 'info' => $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $deliveryGoods
|
|
* @param $delivery_id
|
|
* @return int
|
|
* @throws Exception
|
|
* 保存发货商品信息
|
|
*/
|
|
private static function saveDeliveryGoods($deliveryGoods, $delivery_id)
|
|
{
|
|
$status = Order::STATUS_SHIPMENT_ALL;
|
|
foreach ($deliveryGoods as $id => $goodsCount) {
|
|
if ($goodsCount < 0) {
|
|
throw new Exception('操作异常,发货数量不能小于0');
|
|
} elseif ($goodsCount == 0) {
|
|
continue;
|
|
}
|
|
$orderGoods = OrderGoods::findOne($id);
|
|
|
|
/*如果是发货数量不是全部,订单状态为部分发货*/
|
|
$deliveryGoodsCount = DeliveryGoods::find()->where(['order_goods_id' => $id])->sum('goods_count');
|
|
if (($deliveryGoodsCount + $goodsCount) < $orderGoods->goods_count) { //如果已发货数量未达到order_goods里的数量
|
|
$status = Order::STATUS_SHIPMENT_PORTION;
|
|
} elseif (($deliveryGoodsCount + $goodsCount) > $orderGoods->goods_count) {
|
|
throw new Exception('操作异常,现发货数量超过之前缺少的发货数量');
|
|
}
|
|
|
|
$model = new DeliveryGoods();
|
|
$model->delivery_id = $delivery_id;
|
|
$model->order_goods_id = $orderGoods->id;
|
|
$model->goods_id = $orderGoods->goods_id;
|
|
$model->goods_name = $orderGoods->goods_name;
|
|
$model->goods_count = $goodsCount;
|
|
if (!$model->save()) {
|
|
throw new Exception('delivery_goods save false');
|
|
}
|
|
}
|
|
return $status;
|
|
}
|
|
|
|
/**
|
|
* @param $order_id
|
|
* @return array
|
|
* 查询订单发货商品信息
|
|
*/
|
|
public static function deliveryGoodsInfo($order_id)
|
|
{
|
|
$delivery = Delivery::findAll(['order_id' => $order_id]);
|
|
/*如果该订单是首次发货*/
|
|
if ($delivery) {
|
|
/*获取订单已发的商品和未发的商品*/
|
|
return self::getDeliveryGoodsInfo($delivery);
|
|
} else {
|
|
$unShippedGoods = OrderGoods::find()->where(['order_id' => $order_id])->all();
|
|
return ['unShipped' => $unShippedGoods];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $delivery
|
|
* @return array
|
|
* 获取订单已发的商品和未发的商品
|
|
*/
|
|
public static function getDeliveryGoodsInfo($delivery)
|
|
{
|
|
$data = [];
|
|
$deliveryIds = [];
|
|
foreach ($delivery as $k => $value) {
|
|
$deliveryIds[] = $value->id;
|
|
$orderGoodsIds = DeliveryGoods::find()
|
|
->select('order_goods_id')
|
|
->where(['delivery_id' => $value->id])
|
|
->andWhere(['>', 'goods_count', 0])
|
|
->column();
|
|
$value->deliveryGoods = OrderGoods::findAll($orderGoodsIds);
|
|
}
|
|
$data['shipped'] = $delivery;
|
|
|
|
$deliveryGoodsData = [];
|
|
$deliveryGoods = DeliveryGoods::find()
|
|
->select(['order_goods_id', 'goods_count'])
|
|
->where(['delivery_id' => $deliveryIds])
|
|
->andWhere(['>', 'goods_count', 0])
|
|
->all();
|
|
foreach ($deliveryGoods as $k => $value) {
|
|
$orderGoods = $value->order_goods_id;
|
|
if (isset($deliveryGoodsData[$orderGoods])) {
|
|
$deliveryGoodsData[$orderGoods] += $value->goods_count;
|
|
} else {
|
|
$deliveryGoodsData[$orderGoods] = $value->goods_count;
|
|
}
|
|
}
|
|
foreach ($deliveryGoodsData as $k => $value) { //键名为order_goods_id,键值为对应的已发货数量
|
|
$orderGoods = OrderGoods::findOne($k); //通过orderGoods_id找到订单商品
|
|
if ($value < $orderGoods->goods_count) { //如果已发货数量未达到order_goods里的数量
|
|
$orderGoods->lack_number = $orderGoods->goods_count - $value;
|
|
$data['unShipped'][] = $orderGoods;
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
|
|
}
|