// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\common\model; use app\common\library\helper; use app\common\model\user\CommissionsDetail; use think\facade\Db; /** * 订单商品模型 * Class OrderGoods * @package app\common\model */ class OrderGoods extends BaseModel { // 定义表名 protected $name = 'order_goods'; // 定义主键 protected $pk = 'order_goods_id'; protected $updateTime = false; const FINANCE_CLEARING_MISS = 0; const FINANCE_CLEARING_WAIT = 1; const FINANCE_CLEARING_DONE = 2; const FINANCE_CLEARING_FREEZE = 3; /** * 追加字段 * @var array */ protected $append = [ 'sku_name', // 规格名称 'provider_name', 'coupon_goods_money' //单件商品优惠金额 ]; public function getCouponGoodsMoneyAttr(){ if($this->total_num>0){ return helper::bcsub($this->coupon_money/$this->total_num,0,2); }else{ return 0; } } //规格名称 public function getSkuNameAttr(){ if(isset($this->goods_props)){ return $this->goods_props[0]['value']['name']??''; } return ''; } public function getProviderNameAttr(){ return Provider::where("provider_id",$this->provider_id)->value("provider_name"); } /** * 订单商品图片 * @return \think\model\relation\BelongsTo */ public function image() { $model = "app\\common\\model\\UploadFile"; return $this->belongsTo($model, 'image_id', 'file_id') ->bind(['goods_image' => 'preview_url']); } /** * 关联商品表 * @return \think\model\relation\BelongsTo */ public function goods() { return $this->belongsTo('Goods'); } /** * 关联订单主表 * @return \think\model\relation\BelongsTo */ public function orderM() { return $this->belongsTo('Order'); } /** * 关联供应商 * @return \think\model\relation\BelongsTo */ public function provider() { return $this->belongsTo('Provider')->bind(['provider_name']); } /** * 关联门店 * @return \think\model\relation\BelongsTo */ public function shop() { return $this->belongsTo('Shops','shop_id','shop_id'); } /** * 关联推广人 * @return \think\model\relation\BelongsTo */ public function staffUser() { return $this->belongsTo('User','staff_user_id','user_id')->field('user_id,mobile,nick_name'); } /** * 关联拆分包裹表 * @return \think\model\relation\hasMany */ public function package() { return $this->hasMany('OrderGoodsPackage'); } /** * 关联分销上级 * @return \think\model\relation\BelongsTo */ public function commissionsUser() { return $this->belongsTo('User','staff_user_id','user_id')->bind(['staff_user_name'=>'nick_name']); } /** * 售后单记录表 * @return \think\model\relation\HasOne */ public function refund() { return $this->hasOne('OrderRefund')->order('order_refund_id','desc'); } /** * 获取器:规格属性 * @param $value * @return array */ public function getGoodsPropsAttr($value) { return helper::jsonDecode($value); } /** * 设置器:规格属性 * @param $value * @return string */ public function setGoodsPropsAttr($value) { return $value ? helper::jsonEncode($value) : ''; } /** * 发货时间 * @param $value * @return false|string */ public function getDeliveryTimeAttr($value) { return format_time($value); } /** * 收货时间 * @param $value * @return false|string */ public function getReceiptTimeAttr($value) { return format_time($value); } /** * 订单商品详情 * @param $where * @return OrderGoods|null */ public static function detail($where) { return static::get($where, ['image', 'refund']); } /** * 批量更新订单商品 * @param $orderGoodsIds * @param $data * @return false|int */ public function onBatchUpdate($orderGoodsIds, $data) { return static::updateBase($data, [['order_goods_id', 'in', $orderGoodsIds]]); } /** * 销售额 = 总价-优惠 * @param $orderGoodsIds * @return int|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function sumOrderGoodsVolume($orderGoodsIds){ $volume = OrderGoods::whereIn('order_goods_id',$orderGoodsIds) ->field(Db::raw('(sum(total_pay_price) + sum(rice_card_money)) as volume'))->find(); return $volume->volume??0; } /** * 统计门店销售额 * @param $shopId * @param $from * @param $to * @param null $userId * @return string */ public static function sumShopGiveOutOrder($shopId, $from, $to, $userId=null){ /* $model = CommissionsDetail::field( 'distinct order_id'); if ($userId){ $model = $model->where('user_id',$userId); } $orderIds = $model->where('shop_id',$shopId) ->where('commission_level',1)//6.推荐官直接推广的订单计入奖励金,间接推广的订单不计入。 ->where('clearing_status','<',2) ->whereBetweenTime('order_create_time', $from, $to) ->select()->column('order_id'); $orderGoodsIds = OrderGoods::whereIn('order_id',$orderIds) ->field('order_goods_id') ->select()->column('order_goods_id'); return helper::bcadd(OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to),0,2);*/ if ($userId){ $wheres[] = ['user_id','=',$userId]; }else{ $wheres[] = ['role','=',User::SHOP_SELLER];//如果不加这个条件,一个单的销售额会统计多次 } $wheres[] = ['shop_id','=',$shopId]; $wheres[] = ['commission_level','=',1]; $wheres[] = ['order_create_time','>=',$from]; $wheres[] = ['order_create_time','<=',$to]; $wheres[] = ['clearing_status','<',2]; return CommissionsDetail::sumOrderSaleVolume($wheres); } /** * 统计顾客在门店销售额 * @param $shopId * @param $from * @param $to * @param null $buyerUserId 购买人ID * @param null $userId 推荐人ID * @return string */ public static function sumShopBuyerGiveOutOrder($shopId,$from,$to,$buyerUserId,$userId=null){ /* $model = CommissionsDetail::field( 'distinct order_id'); if ($userId){ $model->where('user_id',$userId); } $orderIds = $model->where('shop_id',$shopId) ->where('buyer_user_id',$buyerUserId) ->where('commission_level',1)//6.推荐官直接推广的订单计入奖励金,间接推广的订单不计入。 ->where('clearing_status','<',2) ->whereBetweenTime('order_create_time', $from, $to) ->select()->column('order_id'); $orderGoodsIds = OrderGoods::whereIn('order_id',$orderIds) ->field('order_goods_id') ->select()->column('order_goods_id'); return helper::bcadd(OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to),0,2);*/ $wheres[] = ['shop_id','=',$shopId]; $wheres[] = ['buyer_user_id','=',$buyerUserId]; $wheres[] = ['role','=',User::SHOP_SELLER];//如果不加这个条件,一个单的销售额会统计多次 $wheres[] = ['commission_level','=',1]; $wheres[] = ['order_create_time','>=',$from]; $wheres[] = ['order_create_time','<=',$to]; $wheres[] = ['clearing_status','<',2]; return CommissionsDetail::sumOrderSaleVolume($wheres); } /** * 统计分销员销售额 * @param $from * @param $to * @param null $buyerUserId 购买人ID * @param null $userId 推荐人ID * @return string */ public static function sumComerGiveOutOrder($from,$to,$buyerUserId,$userId){ /* $orderIds = CommissionsDetail::field( 'distinct order_id') ->where('buyer_user_id',$buyerUserId) ->where('user_id',$userId) ->where('role',User::COMMISSION_USER) ->whereBetweenTime('order_create_time', $from, $to) ->where('clearing_status','<',2) ->select()->column('order_id'); $orderGoodsIds = OrderGoods::whereIn('order_id',$orderIds) ->field('order_goods_id') ->select()->column('order_goods_id'); return helper::bcadd(OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to),0,2);*/ $wheres[] = ['buyer_user_id','=',$buyerUserId]; $wheres[] = ['user_id','=',$userId]; $wheres[] = ['role','=',User::COMMISSION_USER]; $wheres[] = ['order_create_time','>=',$from]; $wheres[] = ['order_create_time','<=',$to]; $wheres[] = ['clearing_status','<',2]; return CommissionsDetail::sumOrderSaleVolume($wheres); } /** * 获取销售额2022-1-10zq * @param $orderGoodsIds * @param $from * @param $to * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getUserSellerVolume($orderGoodsIds,$from,$to){ if (!count($orderGoodsIds))return 0; $tempObj = OrderGoods::whereIn('order_goods_id',$orderGoodsIds) ->whereBetweenTime('create_time', $from, $to) ->field('sum(total_pay_price + rice_card_money) as temp')->find(); $temp = $tempObj->temp; $bc = \app\console\service\OrderRefund::sumRefundPriceNew($orderGoodsIds); return helper::bcsub($temp,$bc,4); } }