StoreStatistics.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace app\admin\model\record;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use app\models\user\UserBill;
  6. use crmeb\services\PHPExcelService;
  7. class StoreStatistics extends BaseModel
  8. {
  9. /**
  10. * 数据表主键
  11. * @var string
  12. */
  13. protected $pk = 'id';
  14. /**
  15. * 模型名称
  16. * @var string
  17. */
  18. protected $name = 'store_order';
  19. use ModelTrait;
  20. /**
  21. * 处理金额
  22. * @param $where
  23. * @return array
  24. */
  25. public static function getOrderPrice($where)
  26. {
  27. $model = new self;
  28. $price = array();
  29. $price['pay_price_wx'] = 0;//微信支付金额
  30. $price['pay_price_yue'] = 0;//余额支付金额
  31. $price['pay_price_offline'] = 0;//线下支付金额
  32. $list = self::getTimeWhere($where, $model)->field('pay_price,total_price,deduction_price,coupon_price,total_postage,pay_type,pay_time')->select()->toArray();
  33. if (empty($list)) {
  34. $price['pay_price_wx'] = 0;
  35. $price['pay_price_yue'] = 0;
  36. $price['pay_price_offline'] = 0;
  37. }
  38. foreach ($list as $v) {
  39. if ($v['pay_type'] == 'weixin') {
  40. $price['pay_price_wx'] = bcadd($price['pay_price_wx'], $v['pay_price'], 2);
  41. } elseif ($v['pay_type'] == 'yue') {
  42. $price['pay_price_yue'] = bcadd($price['pay_price_yue'], $v['pay_price'], 2);
  43. } elseif ($v['pay_type'] == 'offline') {
  44. $price['pay_price_offline'] = bcadd($price['pay_price_offline'], $v['pay_price'], 2);
  45. }
  46. }
  47. return $price;
  48. }
  49. /**
  50. * 获取营业数据
  51. */
  52. public static function getOrderInfo($where)
  53. {
  54. $orderinfo = self::getTimeWhere($where)
  55. ->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')
  56. ->order('pay_time')->where('paid',1)->where('refund_status',0)
  57. ->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['date'] && !$where['data']) return $model;
  99. if ($where['data'] == '' && $where['date']) {
  100. $limitTimeList = [
  101. 'today'=>implode(' - ',[date('Y/m/d'),date('Y/m/d',strtotime('+1 day'))]),
  102. 'week'=>implode(' - ',[
  103. date('Y/m/d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)),
  104. date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600))
  105. ]),
  106. 'month'=>implode(' - ',[date('Y/m').'/01',date('Y/m').'/'.date('t')]),
  107. 'quarter'=>implode(' - ',[
  108. date('Y').'/'.(ceil((date('n'))/3)*3-3+1).'/01',
  109. date('Y').'/'.(ceil((date('n'))/3)*3).'/'.date('t',mktime(0,0,0,(ceil((date('n'))/3)*3),1,date('Y')))
  110. ]),
  111. 'year'=>implode(' - ',[
  112. date('Y').'/01/01',date('Y/m/d',strtotime(date('Y').'/01/01 + 1year -1 day'))
  113. ])
  114. ];
  115. $where['data'] = $limitTimeList[$where['date']];
  116. }
  117. list($startTime, $endTime) = explode(' - ', $where['data']);
  118. $model = $model->where($prefix, '>=', strtotime($startTime . ' 00:00:00'));
  119. $model = $model->where($prefix, '<=', strtotime($endTime . ' 23:59:59'));
  120. return $model;
  121. }
  122. /**
  123. * 获取新增消费
  124. */
  125. public static function getConsumption($where)
  126. {
  127. $consumption=self::getTime($where,new UserBill,'b.add_time')->alias('a')->join('user b','a.uid = b.uid')
  128. ->field('sum(a.number) number')
  129. ->where('a.type','pay_product')->find()->toArray();
  130. return $consumption;
  131. }
  132. /**
  133. * 获取拼团商品
  134. */
  135. public static function getPink($where)
  136. {
  137. $pink = self::getTimeWhere($where)->where('pink_id', '<>', 0)->sum('pay_price');
  138. return $pink;
  139. }
  140. /**
  141. * 获取秒杀商品
  142. */
  143. public static function getSeckill($where){
  144. $seckill=self::getTimeWhere($where)->where('seckill_id', '<>', 0)->sum('pay_price');
  145. return $seckill;
  146. }
  147. /**
  148. * 获取普通商品数
  149. */
  150. public static function getOrdinary($where)
  151. {
  152. $ordinary = self::getTimeWhere($where)->where('pink_id', 0)->where('seckill_id','0')->sum('pay_price');
  153. return $ordinary;
  154. }
  155. /**
  156. * 获取用户充值
  157. */
  158. public static function getRecharge($where)
  159. {
  160. $Recharge = self::getTime($where,new UserBill)->where('type', 'system_add')->where('category','now_money')->sum('number');
  161. return $Recharge;
  162. }
  163. /**
  164. * 获取推广金
  165. */
  166. public static function getExtension($where)
  167. {
  168. $extension = self::getTime($where,new UserBill)->where('type', 'brokerage')->where('category','now_money')->sum('number');
  169. return $extension;
  170. }
  171. /**
  172. * 最近交易
  173. */
  174. public static function trans()
  175. {
  176. $trans = self::alias('a')
  177. ->join('user b', 'a.uid=b.uid','left')
  178. ->join('store_order_cart_info c', 'a.id=c.oid')
  179. ->join('store_product d', 'c.product_id=d.id')
  180. ->field('b.nickname,a.pay_price,d.store_name')
  181. ->order('a.add_time DESC')
  182. ->limit('6')
  183. ->select()->toArray();
  184. return $trans;
  185. }
  186. /**
  187. * 导出表格
  188. */
  189. public static function systemTable($where){
  190. $orderinfos=self::getOrderInfo($where);
  191. if($where['export'] == 1){
  192. $export = [];
  193. $orderinfo=$orderinfos['orderinfo'];
  194. foreach($orderinfo as $info){
  195. $time=$info['pay_time'];
  196. $price = $info['total_price']+$info['pay_postage'];
  197. $zhichu = $info['coupon_price']+$info['deduction_price']+$info['cost'];
  198. $profit = ($info['total_price']+$info['pay_postage'])-($info['coupon_price']+$info['deduction_price']+$info['cost']);
  199. $deduction=$info['deduction_price'];//积分抵扣
  200. $coupon=$info['coupon_price'];//优惠
  201. $cost=$info['cost'];//成本
  202. $export[] = [$time,$price,$zhichu,$cost,$coupon,$deduction,$profit];
  203. }
  204. // ExportService::exportCsv($export,'统计'.time(),['时间','营业额(元)','支出(元)','成本','优惠','积分抵扣','盈利(元)']);
  205. dump($export);
  206. PHPExcelService::setExcelHeader(['时间','营业额(元)','支出(元)','成本','优惠','积分抵扣','盈利(元)'])->setExcelTile('财务统计', '财务统计',date('Y-m-d H:i:s',time()))->setExcelContent($export)->ExcelSave();
  207. }
  208. }
  209. }