StoreStatistics.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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, $model)->where('paid', 1)->where('refund_status', 0);
  92. }
  93. /**
  94. * 获取时间区间
  95. */
  96. public static function getTime($where, $model = null, $prefix = 'add_time')
  97. {
  98. if ($model == null) $model = new self;
  99. if (!$where['date']) return $model;
  100. if ($where['data'] == '') {
  101. $limitTimeList = [
  102. 'today' => implode(' - ', [date('Y/m/d'), date('Y/m/d', strtotime('+1 day'))]),
  103. 'week' => implode(' - ', [
  104. date('Y/m/d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)),
  105. date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600))
  106. ]),
  107. 'month' => implode(' - ', [date('Y/m') . '/01', date('Y/m') . '/' . date('t')]),
  108. 'quarter' => implode(' - ', [
  109. date('Y') . '/' . (ceil((date('n')) / 3) * 3 - 3 + 1) . '/01',
  110. date('Y') . '/' . (ceil((date('n')) / 3) * 3) . '/' . date('t', mktime(0, 0, 0, (ceil((date('n')) / 3) * 3), 1, date('Y')))
  111. ]),
  112. 'year' => implode(' - ', [
  113. date('Y') . '/01/01', date('Y/m/d', strtotime(date('Y') . '/01/01 + 1year -1 day'))
  114. ])
  115. ];
  116. $where['data'] = $limitTimeList[$where['date']];
  117. }
  118. list($startTime, $endTime) = explode(' - ', $where['data']);
  119. $model = $model->where($prefix, '>', strtotime($startTime));
  120. $model = $model->where($prefix, '<', strtotime($endTime));
  121. return $model;
  122. }
  123. /**
  124. * 获取新增消费
  125. */
  126. public static function getConsumption($where)
  127. {
  128. $consumption = self::getTime($where, new UserBill, 'b.add_time')->alias('a')->join('user b', 'a.uid = b.uid')
  129. ->field('sum(a.number) number')
  130. ->where('a.type', 'pay_product')->find()->toArray();
  131. return $consumption;
  132. }
  133. /**
  134. * 获取拼团商品
  135. */
  136. public static function getPink($where)
  137. {
  138. $pink = self::getTimeWhere($where)->where('pink_id', '<>', 0)->sum('pay_price');
  139. return $pink;
  140. }
  141. /**
  142. * 获取秒杀商品
  143. */
  144. public static function getSeckill($where)
  145. {
  146. $seckill = self::getTimeWhere($where)->where('seckill_id', '<>', 0)->sum('pay_price');
  147. return $seckill;
  148. }
  149. /**
  150. * 获取普通商品数
  151. */
  152. public static function getOrdinary($where)
  153. {
  154. $ordinary = self::getTimeWhere($where)->where('pink_id', 0)->where('seckill_id', '0')->sum('pay_price');
  155. return $ordinary;
  156. }
  157. /**
  158. * 获取用户充值
  159. */
  160. public static function getRecharge($where)
  161. {
  162. $Recharge = self::getTime($where, new UserBill)->where('type', 'system_add')->where('category', 'now_money')->sum('number');
  163. return $Recharge;
  164. }
  165. /**
  166. * 获取推广金
  167. */
  168. public static function getExtension($where)
  169. {
  170. $extension = self::getTime($where, new UserBill)->where('type', 'brokerage')->where('category', 'now_money')->sum('number');
  171. return $extension;
  172. }
  173. /**
  174. * 最近交易
  175. */
  176. public static function trans()
  177. {
  178. $trans = self::alias('a')
  179. ->join('user b', 'a.uid=b.uid', 'left')
  180. ->join('store_order_cart_info c', 'a.id=c.oid')
  181. ->join('store_product d', 'c.product_id=d.id')
  182. ->field('b.nickname,a.pay_price,d.store_name')
  183. ->order('a.add_time DESC')
  184. ->limit('6')
  185. ->select()->toArray();
  186. return $trans;
  187. }
  188. /**
  189. * 导出表格
  190. */
  191. public static function systemTable($where)
  192. {
  193. $orderinfos = self::getOrderInfo($where);
  194. if ($where['export'] == 1) {
  195. $export = [];
  196. $orderinfo = $orderinfos['orderinfo'];
  197. foreach ($orderinfo as $info) {
  198. $time = $info['pay_time'];
  199. $price = $info['total_price'] + $info['pay_postage'];
  200. $zhichu = $info['coupon_price'] + $info['deduction_price'] + $info['cost'];
  201. $profit = ($info['total_price'] + $info['pay_postage']) - ($info['coupon_price'] + $info['deduction_price'] + $info['cost']);
  202. $deduction = $info['deduction_price'];//积分抵扣
  203. $coupon = $info['coupon_price'];//优惠
  204. $cost = $info['cost'];//成本
  205. $export[] = [$time, $price, $zhichu, $cost, $coupon, $deduction, $profit];
  206. }
  207. // ExportService::exportCsv($export,'统计'.time(),['时间','营业额(元)','支出(元)','成本','优惠','积分抵扣','盈利(元)']);
  208. dump($export);
  209. PHPExcelService::setExcelHeader(['时间', '营业额(元)', '支出(元)', '成本', '优惠', '积分抵扣', '盈利(元)'])->setExcelTile('财务统计', '财务统计', date('Y-m-d H:i:s', time()))->setExcelContent($export)->ExcelSave();
  210. }
  211. }
  212. }