123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\api\model;
- use app\api\service\User as UserService;
- use app\common\enum\goods\GoodsType;
- use app\common\library\helper;
- use app\common\model\OrderGoods as OrderGoodsModel;
- use app\common\model\OrderRefund;
- use app\common\model\user\CommissionsDetail;
- use app\api\model\user\CommissionsDetail as CommissionsDetailApi;
- /**
- * 订单商品模型
- * Class OrderGoods
- * @package app\api\model
- */
- class OrderGoods extends OrderGoodsModel
- {
- const CLEARING_WAIT = 0;
- const CLEARING_FINISHED = 1;
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'content',
- 'store_id',
- 'create_time',
- ];
- protected $append = ['image_text','buyer'];
- /**
- * 获取未评价的商品
- * @param int $orderId 订单ID
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getNotCommentGoodsList(int $orderId)
- {
- return (new static)->with(['image'])
- ->where('order_id', '=', $orderId)
- ->where('is_comment', '=', 0)
- ->where('goods_type','<>',20)
- ->select();
- }
- /**
- * 销售额合计
- * @param $orderGoodsIds
- * @param $from
- * @param $to
- * @return float
- */
- public static function sumGiveOutOrder($userId){
- $user = User::find($userId);
- $month= intval(date('m'));
- $year = intval(date('Y'));
- $from = mktime(0,0,0,$month,1,$year);
- $to = time();
- //v1.3.92
- $wheres = [];
- if ($user->role == User::SHOP_BOSS || $user->role == User::SHOP_MG){
- $wheres[] = ['user_id','=',$userId];
- $wheres[] = ['order_create_time','>=',$from];
- $wheres[] = ['order_create_time','<=',$to];
- $wheres[] = ['clearing_status','<',2];
- }elseif($user->role == User::SHOP_SELLER){
- $wheres[] = ['user_id','=',$userId];
- $wheres[] = ['shop_id','=',$user->shop_id];
- $wheres[] = ['order_create_time','>=',$from];
- $wheres[] = ['order_create_time','<=',$to];
- $wheres[] = ['clearing_status','<',2];
- }elseif($user->role == User::COMMISSION_USER){
- $wheres[] = ['user_id','=',$userId];
- $wheres[] = ['shop_id','=',0];
- $wheres[] = ['commission_level','=',1];
- $wheres[] = ['order_create_time','>=',$from];
- $wheres[] = ['order_create_time','<=',$to];
- $wheres[] = ['clearing_status','<',2];
- }else{
- $wheres[] = ['user_id','=',$userId];
- $wheres[] = ['order_create_time','>=',$from];
- $wheres[] = ['order_create_time','<=',$to];
- $wheres[] = ['clearing_status','<',2];
- }
- return CommissionsDetail::sumOrderSaleVolume($wheres);
- /* if ($user->role == User::SHOP_BOSS || $user->role == User::SHOP_MG){
- //店长和老板查所有的门店的所有订单
- $orderGoodsIds = CommissionsDetail::field('order_goods_id')->where('user_id',$userId)
- ->where('is_shop_commission',0)->whereBetweenTime('create_time',$from,$to)
- ->where('clearing_status','<',2)->column('order_goods_id');
- }elseif($user->role == User::SHOP_SELLER){
- $orderGoodsIds = CommissionsDetail::field('order_goods_id')->where('user_id',$userId)
- ->where('shop_id',$user->shop_id)
- ->where('is_shop_commission',0)->whereBetweenTime('create_time',$from,$to)
- ->where('clearing_status','<',2)->column('order_goods_id');
- }elseif($user->role == User::COMMISSION_USER){
- $orderGoodsIds = CommissionsDetail::field('order_goods_id')->where('user_id',$userId)
- ->where('is_shop_commission',0)
- ->where('commission_level',1)
- ->whereBetweenTime('create_time',$from,$to)
- ->where('clearing_status','<',2)->column('order_goods_id');
- }else{
- $orderGoodsIds = CommissionsDetail::field('order_goods_id')->where('user_id',$userId)
- ->where('is_shop_commission',0)->whereBetweenTime('create_time',$from,$to)
- ->where('clearing_status','<',2)->column('order_goods_id');
- }
- $orderGoodsIds = array_unique($orderGoodsIds);
- return helper::bcadd(\app\console\model\OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to),0,2);*/
- }
- public function getImageTextAttr($value)
- {
- return UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_id);
- }
- public function getBuyerAttr($value)
- {
- return User::with(['avatar'])->field('user_id,nick_name,avatar_id')->find($this->user_id);
- }
- public static function countUserOrder($userId,$orderIds){
- return OrderGoods::alias('og')
- ->field('og.goods_price,og.total_num,og.distributor_goods_price,og.has_refund_act,og.order_id')
- ->leftJoin('order','og.order_id=order.order_id')
- ->whereIn('og.order_id',$orderIds)->where('order.pay_status',20)//->where('order.pay_type','=',20)todo 211213
- ->where('order.receipt_status',20)
- ->where('og.user_id',$userId)->where('og.has_refund_full',0)->count();
- //return self::whereIn('order_id',$orderIds)->where('user_id',$userId)->where('has_refund_full',0)->count();
- }
- public static function sumUserOrder($userId,$orderIds){
- $orderGoodses = OrderGoods::alias('og')
- ->field('og.goods_price,og.total_num,og.distributor_goods_price,og.has_refund_act,og.order_id,og.total_price,og.distributor_total_money')
- ->leftJoin('order','og.order_id=order.order_id')
- ->whereIn('og.order_id',$orderIds)->where('order.pay_status',20)//->where('order.pay_type','=',20)todo 211213
- ->where('order.receipt_status',20)
- ->where('og.user_id',$userId)
- ->where('og.has_refund_full',0)->select();
- $sum = 0;
- foreach ($orderGoodses as $orderGoods){
- $refundNum=0;
- if ($orderGoods->has_refund_act == 1){
- ////如果有退款,并且财务已打款
- $refund = OrderRefund::where('order_goods_id',$orderGoods->order_goods_id)->where('finance_refund',10)->find();
- $refundNum = $refund?$refund->goods_num:0;
- }
- //$sum += ($orderGoods->total_num - $refundNum)*($orderGoods->goods_price - $orderGoods->distributor_goods_price);
- $sum += ($orderGoods->total_num - $refundNum)*($orderGoods->total_price - $orderGoods->distributor_total_money)/$orderGoods->total_num;
- }
- return helper::bcsub($sum,0,2);
- }
- public function dailySalesTj($shopId,$from,$to){
- //统计前一天的数据
- $res = $this->where('shop_id',$shopId)
- ->whereBetweenTime('create_time',$from,$to)//mysql 对int类型的 between and包含左右边界
- /* ->where('create_time','>=',$from)
- ->where('create_time','<=',$to)*/
- ->where('goods_type','<>',20)
- ->field('sum(total_price) as total_goods_price,sum(total_pay_price + rice_card_money) as total_pay_price,sum(coupon_money) as coupon_money,count(distinct order_id) as order_count')
- ->find();
- $res['total_goods_price'] = $res['total_goods_price']?:0;
- $res['total_pay_price'] = $res['total_pay_price']?:0;
- $res['coupon_money'] = $res['coupon_money']?:0;
- //店老板基本分佣
- $res['boss_comm_money'] = CommissionsDetailApi::sumUserCommission($shopId,User::SHOP_BOSS,$from,$to);
- //dd($comBoss);
- //店长基本分佣
- $res['mg_comm_money'] = CommissionsDetailApi::sumUserCommission($shopId,User::SHOP_MG,$from,$to);
- //店员基本分佣
- $res['seller_comm_money'] = CommissionsDetailApi::sumUserCommission($shopId,User::SHOP_SELLER,$from,$to);
- return $res->toArray();
- }
- /**
- * 添加商品销量
- * @param $from
- * @param $to
- * @return OrderGoods|array|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author: zjwhust
- * @Time: 2022/3/30 16:39
- */
- public function addGoodsSalesRank($from,$to){
- $res = $this->whereBetweenTime('create_time',$from,$to)//mysql 对int类型的 between and包含左右边界
- ->field('sum(total_pay_price + rice_card_money) as sale_volume,sum(total_num) as sale_nums,goods_id')
- ->where('goods_type','<>',GoodsType::GIFT)
- ->group('goods_id')
- ->select();
- // dd(self::getLastSql());
- return $res;
- }
- }
|