OrderGoods.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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\common\model;
  13. use app\common\library\helper;
  14. use app\common\model\user\CommissionsDetail;
  15. use think\facade\Db;
  16. /**
  17. * 订单商品模型
  18. * Class OrderGoods
  19. * @package app\common\model
  20. */
  21. class OrderGoods extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'order_goods';
  25. // 定义主键
  26. protected $pk = 'order_goods_id';
  27. protected $updateTime = false;
  28. const FINANCE_CLEARING_MISS = 0;
  29. const FINANCE_CLEARING_WAIT = 1;
  30. const FINANCE_CLEARING_DONE = 2;
  31. const FINANCE_CLEARING_FREEZE = 3;
  32. /**
  33. * 追加字段
  34. * @var array
  35. */
  36. protected $append = [
  37. 'sku_name', // 规格名称
  38. 'provider_name',
  39. 'coupon_goods_money' //单件商品优惠金额
  40. ];
  41. public function getCouponGoodsMoneyAttr(){
  42. if($this->total_num>0){
  43. return helper::bcsub($this->coupon_money/$this->total_num,0,2);
  44. }else{
  45. return 0;
  46. }
  47. }
  48. //规格名称
  49. public function getSkuNameAttr(){
  50. if(isset($this->goods_props)){
  51. return $this->goods_props[0]['value']['name']??'';
  52. }
  53. return '';
  54. }
  55. public function getProviderNameAttr(){
  56. return Provider::where("provider_id",$this->provider_id)->value("provider_name");
  57. }
  58. /**
  59. * 订单商品图片
  60. * @return \think\model\relation\BelongsTo
  61. */
  62. public function image()
  63. {
  64. $model = "app\\common\\model\\UploadFile";
  65. return $this->belongsTo($model, 'image_id', 'file_id')
  66. ->bind(['goods_image' => 'preview_url']);
  67. }
  68. /**
  69. * 关联商品表
  70. * @return \think\model\relation\BelongsTo
  71. */
  72. public function goods()
  73. {
  74. return $this->belongsTo('Goods');
  75. }
  76. /**
  77. * 关联订单主表
  78. * @return \think\model\relation\BelongsTo
  79. */
  80. public function orderM()
  81. {
  82. return $this->belongsTo('Order');
  83. }
  84. /**
  85. * 关联供应商
  86. * @return \think\model\relation\BelongsTo
  87. */
  88. public function provider()
  89. {
  90. return $this->belongsTo('Provider')->bind(['provider_name']);
  91. }
  92. /**
  93. * 关联门店
  94. * @return \think\model\relation\BelongsTo
  95. */
  96. public function shop()
  97. {
  98. return $this->belongsTo('Shops','shop_id','shop_id');
  99. }
  100. /**
  101. * 关联推广人
  102. * @return \think\model\relation\BelongsTo
  103. */
  104. public function staffUser()
  105. {
  106. return $this->belongsTo('User','staff_user_id','user_id')->field('user_id,mobile,nick_name');
  107. }
  108. /**
  109. * 关联拆分包裹表
  110. * @return \think\model\relation\hasMany
  111. */
  112. public function package()
  113. {
  114. return $this->hasMany('OrderGoodsPackage');
  115. }
  116. /**
  117. * 关联分销上级
  118. * @return \think\model\relation\BelongsTo
  119. */
  120. public function commissionsUser()
  121. {
  122. return $this->belongsTo('User','staff_user_id','user_id')->bind(['staff_user_name'=>'nick_name']);
  123. }
  124. /**
  125. * 售后单记录表
  126. * @return \think\model\relation\HasOne
  127. */
  128. public function refund()
  129. {
  130. return $this->hasOne('OrderRefund')->order('order_refund_id','desc');
  131. }
  132. /**
  133. * 获取器:规格属性
  134. * @param $value
  135. * @return array
  136. */
  137. public function getGoodsPropsAttr($value)
  138. {
  139. return helper::jsonDecode($value);
  140. }
  141. /**
  142. * 设置器:规格属性
  143. * @param $value
  144. * @return string
  145. */
  146. public function setGoodsPropsAttr($value)
  147. {
  148. return $value ? helper::jsonEncode($value) : '';
  149. }
  150. /**
  151. * 发货时间
  152. * @param $value
  153. * @return false|string
  154. */
  155. public function getDeliveryTimeAttr($value)
  156. {
  157. return format_time($value);
  158. }
  159. /**
  160. * 收货时间
  161. * @param $value
  162. * @return false|string
  163. */
  164. public function getReceiptTimeAttr($value)
  165. {
  166. return format_time($value);
  167. }
  168. /**
  169. * 订单商品详情
  170. * @param $where
  171. * @return OrderGoods|null
  172. */
  173. public static function detail($where)
  174. {
  175. return static::get($where, ['image', 'refund']);
  176. }
  177. /**
  178. * 批量更新订单商品
  179. * @param $orderGoodsIds
  180. * @param $data
  181. * @return false|int
  182. */
  183. public function onBatchUpdate($orderGoodsIds, $data)
  184. {
  185. return static::updateBase($data, [['order_goods_id', 'in', $orderGoodsIds]]);
  186. }
  187. /**
  188. * 销售额 = 总价-优惠
  189. * @param $orderGoodsIds
  190. * @return int|mixed
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\DbException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. */
  195. public static function sumOrderGoodsVolume($orderGoodsIds){
  196. $volume = OrderGoods::whereIn('order_goods_id',$orderGoodsIds)
  197. ->field(Db::raw('(sum(total_pay_price) + sum(rice_card_money)) as volume'))->find();
  198. return $volume->volume??0;
  199. }
  200. /**
  201. * 统计门店销售额
  202. * @param $shopId
  203. * @param $from
  204. * @param $to
  205. * @param null $userId
  206. * @return string
  207. */
  208. public static function sumShopGiveOutOrder($shopId, $from, $to, $userId=null){
  209. /* $model = CommissionsDetail::field( 'distinct order_id');
  210. if ($userId){
  211. $model = $model->where('user_id',$userId);
  212. }
  213. $orderIds = $model->where('shop_id',$shopId)
  214. ->where('commission_level',1)//6.推荐官直接推广的订单计入奖励金,间接推广的订单不计入。
  215. ->where('clearing_status','<',2)
  216. ->whereBetweenTime('order_create_time', $from, $to)
  217. ->select()->column('order_id');
  218. $orderGoodsIds = OrderGoods::whereIn('order_id',$orderIds)
  219. ->field('order_goods_id')
  220. ->select()->column('order_goods_id');
  221. return helper::bcadd(OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to),0,2);*/
  222. if ($userId){
  223. $wheres[] = ['user_id','=',$userId];
  224. }else{
  225. $wheres[] = ['role','=',User::SHOP_SELLER];//如果不加这个条件,一个单的销售额会统计多次
  226. }
  227. $wheres[] = ['shop_id','=',$shopId];
  228. $wheres[] = ['commission_level','=',1];
  229. $wheres[] = ['order_create_time','>=',$from];
  230. $wheres[] = ['order_create_time','<=',$to];
  231. $wheres[] = ['clearing_status','<',2];
  232. return CommissionsDetail::sumOrderSaleVolume($wheres);
  233. }
  234. /**
  235. * 统计顾客在门店销售额
  236. * @param $shopId
  237. * @param $from
  238. * @param $to
  239. * @param null $buyerUserId 购买人ID
  240. * @param null $userId 推荐人ID
  241. * @return string
  242. */
  243. public static function sumShopBuyerGiveOutOrder($shopId,$from,$to,$buyerUserId,$userId=null){
  244. /* $model = CommissionsDetail::field( 'distinct order_id');
  245. if ($userId){
  246. $model->where('user_id',$userId);
  247. }
  248. $orderIds = $model->where('shop_id',$shopId)
  249. ->where('buyer_user_id',$buyerUserId)
  250. ->where('commission_level',1)//6.推荐官直接推广的订单计入奖励金,间接推广的订单不计入。
  251. ->where('clearing_status','<',2)
  252. ->whereBetweenTime('order_create_time', $from, $to)
  253. ->select()->column('order_id');
  254. $orderGoodsIds = OrderGoods::whereIn('order_id',$orderIds)
  255. ->field('order_goods_id')
  256. ->select()->column('order_goods_id');
  257. return helper::bcadd(OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to),0,2);*/
  258. $wheres[] = ['shop_id','=',$shopId];
  259. $wheres[] = ['buyer_user_id','=',$buyerUserId];
  260. $wheres[] = ['role','=',User::SHOP_SELLER];//如果不加这个条件,一个单的销售额会统计多次
  261. $wheres[] = ['commission_level','=',1];
  262. $wheres[] = ['order_create_time','>=',$from];
  263. $wheres[] = ['order_create_time','<=',$to];
  264. $wheres[] = ['clearing_status','<',2];
  265. return CommissionsDetail::sumOrderSaleVolume($wheres);
  266. }
  267. /**
  268. * 统计分销员销售额
  269. * @param $from
  270. * @param $to
  271. * @param null $buyerUserId 购买人ID
  272. * @param null $userId 推荐人ID
  273. * @return string
  274. */
  275. public static function sumComerGiveOutOrder($from,$to,$buyerUserId,$userId){
  276. /* $orderIds = CommissionsDetail::field( 'distinct order_id')
  277. ->where('buyer_user_id',$buyerUserId)
  278. ->where('user_id',$userId)
  279. ->where('role',User::COMMISSION_USER)
  280. ->whereBetweenTime('order_create_time', $from, $to)
  281. ->where('clearing_status','<',2)
  282. ->select()->column('order_id');
  283. $orderGoodsIds = OrderGoods::whereIn('order_id',$orderIds)
  284. ->field('order_goods_id')
  285. ->select()->column('order_goods_id');
  286. return helper::bcadd(OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to),0,2);*/
  287. $wheres[] = ['buyer_user_id','=',$buyerUserId];
  288. $wheres[] = ['user_id','=',$userId];
  289. $wheres[] = ['role','=',User::COMMISSION_USER];
  290. $wheres[] = ['order_create_time','>=',$from];
  291. $wheres[] = ['order_create_time','<=',$to];
  292. $wheres[] = ['clearing_status','<',2];
  293. return CommissionsDetail::sumOrderSaleVolume($wheres);
  294. }
  295. /**
  296. * 获取销售额2022-1-10zq
  297. * @param $orderGoodsIds
  298. * @param $from
  299. * @param $to
  300. * @return string
  301. * @throws \think\db\exception\DataNotFoundException
  302. * @throws \think\db\exception\DbException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. */
  305. public static function getUserSellerVolume($orderGoodsIds,$from,$to){
  306. if (!count($orderGoodsIds))return 0;
  307. $tempObj = OrderGoods::whereIn('order_goods_id',$orderGoodsIds)
  308. ->whereBetweenTime('create_time', $from, $to)
  309. ->field('sum(total_pay_price + rice_card_money) as temp')->find();
  310. $temp = $tempObj->temp;
  311. $bc = \app\console\service\OrderRefund::sumRefundPriceNew($orderGoodsIds);
  312. return helper::bcsub($temp,$bc,4);
  313. }
  314. }