OrderGoods.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\model;
  13. use app\api\service\User as UserService;
  14. use app\common\enum\goods\GoodsType;
  15. use app\common\library\helper;
  16. use app\common\model\OrderGoods as OrderGoodsModel;
  17. use app\common\model\OrderRefund;
  18. use app\common\model\user\CommissionsDetail;
  19. use app\api\model\user\CommissionsDetail as CommissionsDetailApi;
  20. /**
  21. * 订单商品模型
  22. * Class OrderGoods
  23. * @package app\api\model
  24. */
  25. class OrderGoods extends OrderGoodsModel
  26. {
  27. const CLEARING_WAIT = 0;
  28. const CLEARING_FINISHED = 1;
  29. /**
  30. * 隐藏字段
  31. * @var array
  32. */
  33. protected $hidden = [
  34. 'content',
  35. 'store_id',
  36. 'create_time',
  37. ];
  38. protected $append = ['image_text','buyer'];
  39. /**
  40. * 获取未评价的商品
  41. * @param int $orderId 订单ID
  42. * @return \think\Collection
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public static function getNotCommentGoodsList(int $orderId)
  48. {
  49. return (new static)->with(['image'])
  50. ->where('order_id', '=', $orderId)
  51. ->where('is_comment', '=', 0)
  52. ->where('goods_type','<>',20)
  53. ->select();
  54. }
  55. /**
  56. * 销售额合计
  57. * @param $orderGoodsIds
  58. * @param $from
  59. * @param $to
  60. * @return float
  61. */
  62. public static function sumGiveOutOrder($userId){
  63. $user = User::find($userId);
  64. $month= intval(date('m'));
  65. $year = intval(date('Y'));
  66. $from = mktime(0,0,0,$month,1,$year);
  67. $to = time();
  68. //v1.3.92
  69. $wheres = [];
  70. if ($user->role == User::SHOP_BOSS || $user->role == User::SHOP_MG){
  71. $wheres[] = ['user_id','=',$userId];
  72. $wheres[] = ['order_create_time','>=',$from];
  73. $wheres[] = ['order_create_time','<=',$to];
  74. $wheres[] = ['clearing_status','<',2];
  75. }elseif($user->role == User::SHOP_SELLER){
  76. $wheres[] = ['user_id','=',$userId];
  77. $wheres[] = ['shop_id','=',$user->shop_id];
  78. $wheres[] = ['order_create_time','>=',$from];
  79. $wheres[] = ['order_create_time','<=',$to];
  80. $wheres[] = ['clearing_status','<',2];
  81. }elseif($user->role == User::COMMISSION_USER){
  82. $wheres[] = ['user_id','=',$userId];
  83. $wheres[] = ['shop_id','=',0];
  84. $wheres[] = ['commission_level','=',1];
  85. $wheres[] = ['order_create_time','>=',$from];
  86. $wheres[] = ['order_create_time','<=',$to];
  87. $wheres[] = ['clearing_status','<',2];
  88. }else{
  89. $wheres[] = ['user_id','=',$userId];
  90. $wheres[] = ['order_create_time','>=',$from];
  91. $wheres[] = ['order_create_time','<=',$to];
  92. $wheres[] = ['clearing_status','<',2];
  93. }
  94. return CommissionsDetail::sumOrderSaleVolume($wheres);
  95. /* if ($user->role == User::SHOP_BOSS || $user->role == User::SHOP_MG){
  96. //店长和老板查所有的门店的所有订单
  97. $orderGoodsIds = CommissionsDetail::field('order_goods_id')->where('user_id',$userId)
  98. ->where('is_shop_commission',0)->whereBetweenTime('create_time',$from,$to)
  99. ->where('clearing_status','<',2)->column('order_goods_id');
  100. }elseif($user->role == User::SHOP_SELLER){
  101. $orderGoodsIds = CommissionsDetail::field('order_goods_id')->where('user_id',$userId)
  102. ->where('shop_id',$user->shop_id)
  103. ->where('is_shop_commission',0)->whereBetweenTime('create_time',$from,$to)
  104. ->where('clearing_status','<',2)->column('order_goods_id');
  105. }elseif($user->role == User::COMMISSION_USER){
  106. $orderGoodsIds = CommissionsDetail::field('order_goods_id')->where('user_id',$userId)
  107. ->where('is_shop_commission',0)
  108. ->where('commission_level',1)
  109. ->whereBetweenTime('create_time',$from,$to)
  110. ->where('clearing_status','<',2)->column('order_goods_id');
  111. }else{
  112. $orderGoodsIds = CommissionsDetail::field('order_goods_id')->where('user_id',$userId)
  113. ->where('is_shop_commission',0)->whereBetweenTime('create_time',$from,$to)
  114. ->where('clearing_status','<',2)->column('order_goods_id');
  115. }
  116. $orderGoodsIds = array_unique($orderGoodsIds);
  117. return helper::bcadd(\app\console\model\OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to),0,2);*/
  118. }
  119. public function getImageTextAttr($value)
  120. {
  121. return UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_id);
  122. }
  123. public function getBuyerAttr($value)
  124. {
  125. return User::with(['avatar'])->field('user_id,nick_name,avatar_id')->find($this->user_id);
  126. }
  127. public static function countUserOrder($userId,$orderIds){
  128. return OrderGoods::alias('og')
  129. ->field('og.goods_price,og.total_num,og.distributor_goods_price,og.has_refund_act,og.order_id')
  130. ->leftJoin('order','og.order_id=order.order_id')
  131. ->whereIn('og.order_id',$orderIds)->where('order.pay_status',20)//->where('order.pay_type','=',20)todo 211213
  132. ->where('order.receipt_status',20)
  133. ->where('og.user_id',$userId)->where('og.has_refund_full',0)->count();
  134. //return self::whereIn('order_id',$orderIds)->where('user_id',$userId)->where('has_refund_full',0)->count();
  135. }
  136. public static function sumUserOrder($userId,$orderIds){
  137. $orderGoodses = OrderGoods::alias('og')
  138. ->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')
  139. ->leftJoin('order','og.order_id=order.order_id')
  140. ->whereIn('og.order_id',$orderIds)->where('order.pay_status',20)//->where('order.pay_type','=',20)todo 211213
  141. ->where('order.receipt_status',20)
  142. ->where('og.user_id',$userId)
  143. ->where('og.has_refund_full',0)->select();
  144. $sum = 0;
  145. foreach ($orderGoodses as $orderGoods){
  146. $refundNum=0;
  147. if ($orderGoods->has_refund_act == 1){
  148. ////如果有退款,并且财务已打款
  149. $refund = OrderRefund::where('order_goods_id',$orderGoods->order_goods_id)->where('finance_refund',10)->find();
  150. $refundNum = $refund?$refund->goods_num:0;
  151. }
  152. //$sum += ($orderGoods->total_num - $refundNum)*($orderGoods->goods_price - $orderGoods->distributor_goods_price);
  153. $sum += ($orderGoods->total_num - $refundNum)*($orderGoods->total_price - $orderGoods->distributor_total_money)/$orderGoods->total_num;
  154. }
  155. return helper::bcsub($sum,0,2);
  156. }
  157. public function dailySalesTj($shopId,$from,$to){
  158. //统计前一天的数据
  159. $res = $this->where('shop_id',$shopId)
  160. ->whereBetweenTime('create_time',$from,$to)//mysql 对int类型的 between and包含左右边界
  161. /* ->where('create_time','>=',$from)
  162. ->where('create_time','<=',$to)*/
  163. ->where('goods_type','<>',20)
  164. ->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')
  165. ->find();
  166. $res['total_goods_price'] = $res['total_goods_price']?:0;
  167. $res['total_pay_price'] = $res['total_pay_price']?:0;
  168. $res['coupon_money'] = $res['coupon_money']?:0;
  169. //店老板基本分佣
  170. $res['boss_comm_money'] = CommissionsDetailApi::sumUserCommission($shopId,User::SHOP_BOSS,$from,$to);
  171. //dd($comBoss);
  172. //店长基本分佣
  173. $res['mg_comm_money'] = CommissionsDetailApi::sumUserCommission($shopId,User::SHOP_MG,$from,$to);
  174. //店员基本分佣
  175. $res['seller_comm_money'] = CommissionsDetailApi::sumUserCommission($shopId,User::SHOP_SELLER,$from,$to);
  176. return $res->toArray();
  177. }
  178. /**
  179. * 添加商品销量
  180. * @param $from
  181. * @param $to
  182. * @return OrderGoods|array|\think\Model|null
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. * @author: zjwhust
  187. * @Time: 2022/3/30 16:39
  188. */
  189. public function addGoodsSalesRank($from,$to){
  190. $res = $this->whereBetweenTime('create_time',$from,$to)//mysql 对int类型的 between and包含左右边界
  191. ->field('sum(total_pay_price + rice_card_money) as sale_volume,sum(total_num) as sale_nums,goods_id')
  192. ->where('goods_type','<>',GoodsType::GIFT)
  193. ->group('goods_id')
  194. ->select();
  195. // dd(self::getLastSql());
  196. return $res;
  197. }
  198. }