StoreStatistics.php 8.1 KB

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