123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- 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);
- }
- }
|