|
|
@ -5,7 +5,6 @@ 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\ExpressTemplate; |
|
|
|
use backend\modules\shop\models\ars\Order; |
|
|
|
use backend\modules\shop\models\ars\OrderGoods; |
|
|
|
use Yii; |
|
|
@ -15,8 +14,8 @@ use yii\helpers\ArrayHelper; |
|
|
|
class DeliveryManager |
|
|
|
{ |
|
|
|
/** |
|
|
|
* @param $order |
|
|
|
* @param $delivery |
|
|
|
* @param Order $order |
|
|
|
* @param Delivery $delivery |
|
|
|
* @return array |
|
|
|
* 订单发货 |
|
|
|
*/ |
|
|
@ -100,119 +99,64 @@ class DeliveryManager |
|
|
|
/** |
|
|
|
* @param $order_id |
|
|
|
* @return array |
|
|
|
* @throws Exception |
|
|
|
* 获取发货全部商品信息 |
|
|
|
* 查询订单发货商品信息 |
|
|
|
*/ |
|
|
|
public static function deliveryGoodsInfo($order_id) |
|
|
|
{ |
|
|
|
$delivery = Delivery::find()->select('id')->where(['order_id' => $order_id])->all(); |
|
|
|
$delivery = Delivery::findAll(['order_id' => $order_id]); |
|
|
|
/*如果该订单是首次发货*/ |
|
|
|
if (!$delivery) { |
|
|
|
$unShippedGoods = OrderGoods::find()->where(['order_id' => $order_id])->asArray()->all(); |
|
|
|
if ($delivery) { |
|
|
|
/*获取订单已发的商品和未发的商品*/ |
|
|
|
return self::getDeliveryGoodsInfo($delivery); |
|
|
|
} else { |
|
|
|
$unShippedGoods = OrderGoods::find()->where(['order_id' => $order_id])->all(); |
|
|
|
return ['unShipped' => $unShippedGoods]; |
|
|
|
} |
|
|
|
|
|
|
|
$filter = []; |
|
|
|
$deliveryIds = []; |
|
|
|
foreach ($delivery as $value) { |
|
|
|
$deliveryIds[] = $value->id; |
|
|
|
} |
|
|
|
$deliveryGoods = DeliveryGoods::find()->where(['delivery_id' => $deliveryIds])->all(); |
|
|
|
for ($i = 0; $i < count($deliveryGoods); $i++) { |
|
|
|
$orderGoodsId = $deliveryGoods[$i]['order_goods_id']; |
|
|
|
//以orderGoods的id为键名,以orderGoods的数量为键值,先保存到filter数组中
|
|
|
|
$filter[$orderGoodsId] = $deliveryGoods[$i]['goods_count']; |
|
|
|
for ($j = 0; $j < count($deliveryGoods); $j++) { |
|
|
|
//如果发货商品中有其他相同orderGoods的商品
|
|
|
|
if ($orderGoodsId == $deliveryGoods[$j]['order_goods_id'] && $i != $j) { |
|
|
|
//商品数量叠加起来并保存
|
|
|
|
$filter[$orderGoodsId] += $deliveryGoods[$j]['goods_count']; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*获取订单已发的商品和未发的商品*/ |
|
|
|
return self::getDeliveryGoodsInfo($filter, $order_id); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $filter |
|
|
|
* @param $order_id |
|
|
|
* @param $delivery |
|
|
|
* @return array |
|
|
|
* @throws Exception |
|
|
|
* 获取订单已发的商品和未发的商品 |
|
|
|
*/ |
|
|
|
public static function getDeliveryGoodsInfo($filter, $order_id) |
|
|
|
public static function getDeliveryGoodsInfo($delivery) |
|
|
|
{ |
|
|
|
$unShipped = []; //未发货商品
|
|
|
|
foreach ($filter as $k => $value) { //键名为order_goods_id,键值为对应的已发货数量
|
|
|
|
$goodsData = self::getOrderGoodsInfo($k); |
|
|
|
if ($value < $goodsData['goods_count']) { //如果已发货数量未达到order_goods里的数量
|
|
|
|
$lack_number = $goodsData['goods_count'] - $value; |
|
|
|
$goodsData['lack_number'] = $lack_number; |
|
|
|
$goodsData['goods_count'] = $lack_number; |
|
|
|
$unShipped[] = $goodsData; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$shipped = []; |
|
|
|
$delivery = Delivery::findAll(['order_id' => $order_id]); |
|
|
|
$data = []; |
|
|
|
$deliveryIds = []; |
|
|
|
foreach ($delivery as $k => $value) { |
|
|
|
|
|
|
|
$shipped[$k]['logisticInfo'] = [ |
|
|
|
'exp_name' => '', |
|
|
|
'invoice_no' => '', |
|
|
|
'created_at' => '', |
|
|
|
]; //物流公司和运单号
|
|
|
|
$shipped[$k]['goodsInfo'] = self::getDeliverGoodsInfo($value->id); //获取商品信息
|
|
|
|
$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; |
|
|
|
|
|
|
|
return ['shipped' => $shipped, 'unShipped' => $unShipped]; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $delivery_id |
|
|
|
* @return array |
|
|
|
* @throws Exception |
|
|
|
* 通过获取订单已发货的商品信息 |
|
|
|
*/ |
|
|
|
public static function getDeliverGoodsInfo($delivery_id) |
|
|
|
{ |
|
|
|
$goodsInfo = []; |
|
|
|
$deliveryGoodsData = []; |
|
|
|
$deliveryGoods = DeliveryGoods::find() |
|
|
|
->where(['delivery_id' => $delivery_id]) |
|
|
|
->select(['order_goods_id', 'goods_count']) |
|
|
|
->where(['delivery_id' => $deliveryIds]) |
|
|
|
->andWhere(['>', 'goods_count', 0]) |
|
|
|
->all(); |
|
|
|
foreach ($deliveryGoods as $value) { |
|
|
|
$goodsData = self::getOrderGoodsInfo($value->order_goods_id); |
|
|
|
$goodsData['delivery_number'] = $value->goods_count; |
|
|
|
$goodsInfo[] = $goodsData; |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
return $goodsInfo; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $order_goods_id |
|
|
|
* @return array |
|
|
|
* @throws Exception |
|
|
|
* 获取订单商品信息 |
|
|
|
*/ |
|
|
|
private static function getOrderGoodsInfo($order_goods_id) |
|
|
|
{ |
|
|
|
$orderGoods = OrderGoods::findOne($order_goods_id); //通过orderGoods_id找到订单商品
|
|
|
|
if ($orderGoods) { |
|
|
|
$goodsData = [ |
|
|
|
'id' => $orderGoods->id, |
|
|
|
'goods_id' => $orderGoods->goods_id, |
|
|
|
'goods_name' => $orderGoods->goods_name, |
|
|
|
'goods_count' => $orderGoods->goods_count, |
|
|
|
'goods_img' => $orderGoods->goods_img, |
|
|
|
'sku_value' => $orderGoods->sku_value, |
|
|
|
]; |
|
|
|
return $goodsData; |
|
|
|
} else { |
|
|
|
throw new Exception('order_goods not found'); |
|
|
|
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; |
|
|
|
} |
|
|
|
|
|
|
|
|