聚合供应链 * @property string|null $shipped_num 已发货数量 * @property array|null $extra_data 额外信息 * @property string $unit 额外信息 * @property int $goods_subtype 商品子类型 (0:普通商品 其他:根据商品的goods_subtype而定) * */ class OrderDetail extends \common\models\base\BaseActiveRecord { /** * {@inheritdoc} */ public static function tableName() { return '{{%order_detail}}'; } /** * {@inheritdoc} */ public function rules() { return [ [['order_id', 'user_id','mall_id' ,'mch_id', 'store_id','goods_id', 'goods_attr_id', 'marketing_id', 'order_type', 'give_score', 'order_status', 'shipping_status', 'is_feedback', 'is_evaluate', 'is_virtual', 'status', 'created_at', 'updated_at', 'shipped_num', 'goods_subtype'], 'integer'], [['attr_groups_format','goods_attr_name'], 'string'], [['price', 'cost_price', 'adjust_money', 'goods_price', 'original_goods_price', 'refund_balance_money'], 'number'], [['feedback_evidence', 'num', 'extra_data', 'unit'], 'safe'], [['goods_name', 'remark'], 'string', 'max' => 200], [['sign_id',], 'string', 'max' => 255], [['pic_url'], 'string', 'max' => 255], [['marketing_type','order_source'], 'string', 'max' => 50], [['extra_info'], 'string'], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => '订单项ID', 'mall_id' => '商城ID', 'order_id' => '订单ID', 'user_id' => '用户id', 'mch_id' => '多商户ID', 'store_id' => '门店id', 'goods_id' => '商品ID', 'goods_name' => '商品名称', 'goods_attr_id' => '规格ID', 'goods_attr_name' => '规格名称', 'sign_id' => '规格ID标识', 'attr_groups_format' => '格式化规格组', 'price' => '商品价格', 'cost_price' => '商品成本价', 'num' => '购买数量', 'adjust_money' => '调整金额', 'goods_price' => '商品优惠后总价', 'original_goods_price' => '商品原本总价', 'pic_url' => '商品图片', 'marketing_id' => '促销ID', 'marketing_type' => '促销类型', 'order_type' => '订单类型', 'give_score' => '赠送积分数量', 'order_status' => '订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】', 'shipping_status' => '物流状态【1已发货 0待发货】', 'is_feedback' => '退款状态', 'remark' => '备注', 'is_evaluate' => '是否评价 【0未评价 1已评价 2已追评】', 'refund_balance_money' => '订单退款余额', 'is_virtual' => '是否包含 虚拟商品 0 不包含 1 包含', 'status' => '状态[-1:删除;0:禁用;1启用]', 'extra_info' => '额外信息', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'order_source' => '订单来源', 'shipped_num' => '已发货数量', 'goods_subtype' => '商品子类型 (0:普通商品 其他:根据商品的goods_subtype而定)', ]; } public function getUser(){ return $this->hasOne(User::class, ['id' => 'user_id']); } public function getOrder(){ return $this->hasOne(Order::class, ['id' => 'order_id']); } public function getGoods(){ return $this->hasOne(Goods::class, ['id' => 'goods_id']); } public function getRefundFinish() { return $this ->hasOne(OrderRefund::class, ['order_detail_id' => 'id']) ->select('id, order_detail_id') ->where(['step_status' => [RefundStatusEnum::STEP_4, RefundStatusEnum::STEP_6]]); } /** * 保存数据 * @Author bing * @DateTime 2021-08-24 15:34:48 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $params * @return void */ public static function setData(array $params){ try{ if(!empty($params['id'])){ $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'],'status'=>[StatusEnum::ENABLED]]); if(empty($model)) throw new Exception('订单详情不存在或已删除'); }else{ $model = new self(); $model->loadDefaultValues(); } if(!$model->load($params,'') || !$model->save()){ throw new Exception($model->getErrorMessage()); } return $model; }catch(Exception $e){ self::$static_error = $e->getMessage(); return false; } } /** * 订单商品调价 * @Author bing * @DateTime 2021-09-13 18:19:27 * @copyright: Copyright (c) 2020 广东七件事集团 * @return void */ public static function priceAdjustment($params){ //查询所有的商品订单 $detail_ids = array_column($params,'id'); $details = self::getOrderDetails([['in','id',$detail_ids]],[],false,'*','id'); $all_goods_price = 0; $all_adjust_money = 0; foreach($params as $key => $detail){ if(!isset($details[$detail['id']])) throw new Exception('订单商品不存在或已删除'); $item = $details[$detail['id']]; $item->adjust_money = $detail['adjust_money']; $all_adjust_money += $detail['adjust_money']; //$item->goods_price = $item->goods_price + $item->adjust_money; $item->save(); $all_goods_price += $item->goods_price; } $all_goods_price += $all_adjust_money; return $all_goods_price; } /** * 获取订单商品列表 * @Author bing * @DateTime 2021-08-26 13:00:11 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $cond * @param array $with * @param integer $is_array * @return array|null */ public static function getOrderDetails(array $cond=[],array $with=[],bool $is_array=true,string $select='*',$index=''){ $default_cond = [['<>','status',StatusEnum::DELETE]]; $cond = array_merge($default_cond,$cond); $query = self::find()->select($select); self::paramPack(['where'=>$cond,'with'=>$with],$query); if(!empty($index)) $query->indexBy($index); $labels = $query->asArray($is_array)->all(); return $labels; } /** * 获取订单售后数量 * @Author bing * @DateTime 2021-09-17 18:12:25 * @param [type] $order_id * @return false|int|string *@copyright: Copyright (c) 2020 广东七件事集团 */ public static function getAfterSaleCountByOrderId($order_id) { return self::find() ->select(['count(id) as count']) ->where(['status' => StatusEnum::ENABLED]) ->andWhere(['order_id' => $order_id]) ->andWhere([ 'is_feedback'=> OrderStatusEnum::FEEDBACKING]) ->scalar(); } /** * 根据订单详情ID获取订单号 * @Author: lun * @DateTime: 2023/5/27 0027 18:14 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $orderDetailIds * @return array */ public static function getOrderNoByDetailIds($orderDetailIds) { $data = []; $details = self::find()->select('id,order_id')->where(['id' => $orderDetailIds])->indexBy('id')->asArray()->all(); foreach ($details as $detail) { $order_ids[] = $detail['order_id']; } // 获取订单号 $orders = Order::find()->select('id,order_no')->where(['id' => array_unique($order_ids)])->indexBy(['id'])->asArray()->all(); foreach ($details as $detail) { $data[$detail['id']] = $orders[$detail['order_id']]['order_no']; } return $data; } }