| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace common\models\order;
- use common\enums\OrderStatusEnum;
- use common\enums\RefundStatusEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\user\User;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "{{%order_detail}}".
- * @property int $id 订单项ID
- * @property int|null $mall_id 商城ID
- * @property int|null $order_id 订单ID
- * @property int|null $user_id 用户id
- * @property int|null $mch_id 多商户ID
- * @property int|null $store_id 门店ID
- * @property int|null $goods_id 商品ID
- * @property string|null $goods_name 商品名称
- * @property int|null $goods_attr_id 规格ID
- * @property int|null $goods_attr_name 规格ID
- * @property string $sign_id 规格ID标识
- * @property string|null $attr_groups_format 格式化规格组
- * @property float|null $price 商品价格
- * @property float|null $cost_price 商品成本价
- * @property int|null $num 购买数量
- * @property float|null $adjust_money 调整金额
- * @property float|null $goods_price 商品优惠后总价
- * @property float|null $original_goods_price 商品原本总价
- * @property string|null $pic_url 商品图片
- * @property int|null $marketing_id 促销ID
- * @property string|null $marketing_type 促销类型
- * @property int|null $order_type 订单类型
- * @property int|null $give_score 赠送积分数量
- * @property int|null $order_status 订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】
- * @property int|null $shipping_status 物流状态【1已发货 0待发货】
- * @property int|null $is_feedback 退款状态
- * @property string|null $remark 备注
- * @property int|null $is_evaluate 是否评价 【0未评价 1已评价 2已追评】
- * @property float|null $refund_balance_money 订单退款余额
- * @property int|null $is_virtual 是否包含 虚拟商品 0 不包含 1 包含
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property string|null extra_info 额外信息
- * @property int|null $created_at
- * @property int|null $updated_at
- * @property string|null $order_source 订单来源:JuheShop=>聚合供应链
- * @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;
- }
- }
|