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.
 
 
 

222 lines
7.6 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\ExpressTemplate;
use backend\modules\shop\models\ars\Order;
use backend\modules\shop\models\ars\OrderGoods;
use Yii;
use yii\db\Exception;
class DeliveryManager
{
/**
* @param $order
* @param $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('缺少发货商品信息');
}
$deliveryGoods = json_decode($data['deliveryGoods']);
$delivery->load($data);
$delivery->order_id = $order->id;
if (!$delivery->save()) {
throw new Exception('保存物流信息失败');
}
$order->status = Order::STATUS_SHIPMENT_ALL;
/*发货商品数据*/
foreach ($deliveryGoods as $k => $value) {
$model = new DeliveryGoods();
$model->delivery_id = $delivery->id;
$model->order_goods_id = $value->id;
$model->goods_id = $value->goods_id;
$model->goods_name = $value->goods_name;
$model->goods_count = $value->goods_count;
if ($model->goods_count < $value->lack_number) {
$order->status = Order::STATUS_SHIPMENT_PORTION;
}
if (!$model->save()) {
throw new Exception('delivery_goods save false');
}
}
if (!$order->save()) {
throw new Exception('order shipping_status update false');
}
$transaction->commit();
return ['status' => true];
} catch (Exception $e) {
$transaction->rollBack();
return ['status' => false, 'info' => $e->getMessage()];
}
}
/**
* @param $order_id
* @return array
* @throws Exception
* 获取发货全部商品信息
*/
public static function deliveryGoodsInfo($order_id)
{
$delivery = Delivery::find()->select('id')->where(['order_id' => $order_id])->all();
/*如果该订单是首次发货*/
if (!$delivery) {
$unShippend = self::firstDelivery($order_id);
return ['unShipped' => $unShippend];
}
$deliveryIds = [];
foreach ($delivery as $value) {
$deliveryIds[] = $value->id;
}
$filter = [];
$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 $order_id
* @return array
* 首次发货直接获取orderGoods的内容
*/
private static function firstDelivery($order_id)
{
$unShippedGoods = [];
$orderGoods = OrderGoods::find()->where(['order_id' => $order_id])->all();
foreach ($orderGoods as $k => $v) {
$unShippedGoods[] = [
'id' => $v->id,
'goods_id' => $v->goods_id,
'goods_img' => $v->goods_img,
'goods_name' => $v->goods_name,
'goods_count' => $v->goods_count,
'lack_number' => $v->goods_count,
'sku_value' => $v->sku_value,
];
}
return $unShippedGoods;
}
/**
* @param $filter
* @param $order_id
* @return array
* @throws Exception
* 获取订单已发的商品和未发的商品
*/
public static function getDeliveryGoodsInfo($filter, $order_id)
{
$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 = self::getDeliveryInfo($order_id); //已发货商品信息
return ['shipped' => $shipped, 'unShipped' => $unShipped];
}
/**
* @param $order_id
* @return array
* @throws Exception
* 通过订单id获取发货信息
*/
public static function getDeliveryInfo($order_id)
{
$deliveryInfo = [];
$delivery = Delivery::findAll(['order_id' => $order_id]);
foreach ($delivery as $k => $value) {
// $expressTemplate = ExpressTemplate::find()
// ->select('name')
// ->where(['id' => $value->shipping_id])
// ->one();
// $deliveryInfo[$k]['logisticInfo'] = [
// 'exp_name'=> $expressTemplate ? $expressTemplate->name : '',
// 'invoice_no'=> $value->invoice_no
// ]; //物流公司和运单号
$deliveryInfo[$k]['goodsInfo'] = self::getDeliverGoodsInfo($value->id); //获取商品信息
}
return $deliveryInfo;
}
/**
* @param $delivery_id
* @return array
* @throws Exception
* 通过获取订单已发货的商品信息
*/
public static function getDeliverGoodsInfo($delivery_id)
{
$goodsInfo = [];
$deliveryGoods = DeliveryGoods::find()
->where(['delivery_id' => $delivery_id])
->andWhere(['>', 'goods_count', 0])
->all();
foreach ($deliveryGoods as $value) {
$goodsData = self::getOrderGoodsInfo($value->order_goods_id);
$goodsData['delivery_number'] = $value->goods_count;
$goodsInfo[] = $goodsData;
}
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');
}
}
}