StoreStatistics.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\record;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use service\ExportService;
  15. use app\admin\model\user\UserBill;
  16. use app\admin\model\user\User;
  17. /**
  18. * Class StoreStatistics
  19. * @package app\admin\model\record
  20. */
  21. class StoreStatistics extends ModelBasic
  22. {
  23. protected $name = 'store_order';
  24. use ModelTrait;
  25. const order = 'StoreStatistics';
  26. /**
  27. * 处理金额
  28. * @param $where
  29. * @return array
  30. */
  31. public static function getOrderPrice($where)
  32. {
  33. $model = new self;
  34. $price = array();
  35. $price['pay_price_wx'] = 0;//微信支付金额
  36. $price['pay_price_yue'] = 0;//余额支付金额
  37. $price['pay_price_offline'] = 0;//线下支付金额
  38. $list = self::getTimeWhere($where, $model)->field('pay_price,total_price,deduction_price,coupon_price,total_postage,pay_type,pay_time')->select()->toArray();
  39. if (empty($list)) {
  40. $price['pay_price_wx'] = 0;
  41. $price['pay_price_yue'] = 0;
  42. $price['pay_price_offline'] = 0;
  43. }
  44. foreach ($list as $v) {
  45. if ($v['pay_type'] == 'weixin') {
  46. $price['pay_price_wx'] = bcadd($price['pay_price_wx'], $v['pay_price'], 2);
  47. } elseif ($v['pay_type'] == 'yue') {
  48. $price['pay_price_yue'] = bcadd($price['pay_price_yue'], $v['pay_price'], 2);
  49. } elseif ($v['pay_type'] == 'offline') {
  50. $price['pay_price_offline'] = bcadd($price['pay_price_offline'], $v['pay_price'], 2);
  51. }
  52. }
  53. return $price;
  54. }
  55. /**
  56. * 获取营业数据
  57. */
  58. public static function getOrderInfo($where)
  59. {
  60. $orderinfo = self::getTimeWhere($where)
  61. ->field('sum(total_price) total_price,sum(cost) cost,sum(pay_postage) pay_postage,sum(pay_price) pay_price,sum(coupon_price) coupon_price,sum(deduction_price) deduction_price,from_unixtime(pay_time,\'%Y-%m-%d\') pay_time')->order('pay_time')->group('from_unixtime(pay_time,\'%Y-%m-%d\')')->select()->toArray();
  62. $price = 0;
  63. $postage = 0;
  64. $deduction = 0;
  65. $coupon = 0;
  66. $cost = 0;
  67. foreach ($orderinfo as $info) {
  68. $price = bcadd($price, $info['total_price'], 2);//应支付
  69. $postage = bcadd($postage, $info['pay_postage'], 2);//邮费
  70. $deduction = bcadd($deduction, $info['deduction_price'], 2);//抵扣
  71. $coupon = bcadd($coupon, $info['coupon_price'], 2);//优惠券
  72. $cost = bcadd($cost, $info['cost'], 2);//成本
  73. }
  74. return compact('orderinfo', 'price', 'postage', 'deduction', 'coupon', 'cost');
  75. }
  76. /**
  77. * 处理where条件
  78. */
  79. public static function statusByWhere($status, $model = null)
  80. {
  81. if ($model == null) $model = new self;
  82. if ('' === $status)
  83. return $model;
  84. else if ($status == 'weixin')//微信支付
  85. return $model->where('pay_type', 'weixin');
  86. else if ($status == 'yue')//余额支付
  87. return $model->where('pay_type', 'yue');
  88. else if ($status == 'offline')//线下支付
  89. return $model->where('pay_type', 'offline');
  90. else
  91. return $model;
  92. }
  93. public static function getTimeWhere($where, $model = null)
  94. {
  95. return self::getTime($where)->where('paid', 1)->where('refund_status', 0);
  96. }
  97. /**
  98. * 获取时间区间
  99. */
  100. public static function getTime($where, $model = null, $prefix = 'add_time')
  101. {
  102. if ($model == null) $model = new self;
  103. if ($where['data'] == '') {
  104. switch ($where['date']) {
  105. case 'today':
  106. case 'week':
  107. case 'month':
  108. case 'year':
  109. $model = $model->whereTime($prefix, $where['date']);
  110. break;
  111. case 'quarter':
  112. list($startTime, $endTime) = User::getMonth('n');
  113. $model = $model->where($prefix, '>', strtotime($startTime));
  114. $model = $model->where($prefix, '<', strtotime($endTime));
  115. break;
  116. }
  117. } else {
  118. list($startTime, $endTime) = explode(' - ', $where['data']);
  119. $model = $model->where($prefix, '>', strtotime($startTime));
  120. $model = $model->where($prefix, '<', strtotime($endTime));
  121. }
  122. return $model;
  123. }
  124. /**
  125. * 获取新增消费
  126. */
  127. public static function getConsumption($where)
  128. {
  129. $consumption = self::getTime($where, new UserBill, 'b.add_time')->alias('a')->join('user b', 'a.uid = b.uid')
  130. ->field('sum(a.number) number')
  131. ->where('a.type', 'pay_product')->find()->toArray();
  132. return $consumption;
  133. }
  134. /**
  135. * 获取拼团商品
  136. */
  137. public static function getPink($where)
  138. {
  139. $pink = self::getTimeWhere($where)->where('pink_id', 'neq', 0)->sum('pay_price');
  140. return $pink;
  141. }
  142. /**
  143. * 获取秒杀商品
  144. */
  145. public static function getSeckill($where)
  146. {
  147. $seckill = self::getTimeWhere($where)->where('seckill_id', 'neq', 0)->sum('pay_price');
  148. return $seckill;
  149. }
  150. /**
  151. * 获取普通商品数
  152. */
  153. public static function getOrdinary($where)
  154. {
  155. $ordinary = self::getTimeWhere($where)->where('pink_id', 'eq', 0)->where('seckill_id', 'eq', '0')->sum('pay_price');
  156. return $ordinary;
  157. }
  158. /**
  159. * 获取用户充值
  160. */
  161. public static function getRecharge($where)
  162. {
  163. $Recharge = self::getTime($where, new UserBill)->where('type', 'system_add')->where('category', 'now_money')->sum('number');
  164. return $Recharge;
  165. }
  166. /**
  167. * 获取推广金
  168. */
  169. public static function getExtension($where)
  170. {
  171. $rake_back = self::getTime($where, new UserBill)->where('type', 'brokerage')->where('category', 'now_money')->sum('number');
  172. $return = self::getTime($where, new UserBill)->where('type', 'brokerage_return')->where('category', 'now_money')->sum('number');
  173. $extension = bcsub($rake_back, $return, 2);
  174. return $extension;
  175. }
  176. /**
  177. * 最近交易
  178. */
  179. public static function trans()
  180. {
  181. $trans = self::alias('a')
  182. ->join('user b', 'a.uid=b.uid')
  183. ->join('store_order_cart_info c', 'a.id=c.oid')
  184. ->join('store_product d', 'c.product_id=d.id')
  185. ->field('b.nickname,a.pay_price,d.store_name')
  186. ->order('a.add_time DESC')
  187. ->limit('6')
  188. ->select()->toArray();
  189. return $trans;
  190. }
  191. }