FinanceModel.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace app\admin\model\finance;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use app\models\user\UserBill;
  6. use app\admin\model\user\User;
  7. use crmeb\services\PHPExcelService;
  8. /**
  9. * 数据统计处理
  10. * Class FinanceModel
  11. * @package app\admin\model\finance
  12. */
  13. class FinanceModel extends BaseModel
  14. {
  15. /**
  16. * 数据表主键
  17. * @var string
  18. */
  19. protected $pk = 'id';
  20. /**
  21. * 模型名称
  22. * @var string
  23. */
  24. protected $name = 'user_bill';
  25. use ModelTrait;
  26. /**
  27. * 处理金额
  28. * @param $where
  29. * @return array
  30. */
  31. public static function systemPage($where)
  32. {
  33. $model = new self;
  34. //翻页
  35. $limit = $where['limit'];
  36. $offset = $where['offset'];
  37. $limit = $offset . ',' . $limit;
  38. //排序
  39. $order = '';
  40. if (!empty($where['sort']) && !empty($where['sortOrder'])) {
  41. $order = $where['sort'] . ' ' . $where['sortOrder'];
  42. }
  43. unset($where['limit']);
  44. unset($where['offset']);
  45. unset($where['sort']);
  46. unset($where['sortOrder']);
  47. if (!empty($where['add_time'])) {
  48. // list($startTime, $endTime) = explode(' - ', $where['add_time']);
  49. // $where['add_time'] = array('between', [strtotime($startTime), strtotime($endTime)]);
  50. // } else {
  51. // $where['add_time'] = array('between', [strtotime(date('Y/m') . '/01'), strtotime(date('Y/m') . '/' . date('t'))]);
  52. $model = self::getModelTime($where, $model, 'add_time');
  53. }
  54. if (empty($where['title'])) {
  55. unset($where['title']);
  56. }
  57. $total = $model->where($where)->count();
  58. $rows = $model->where($where)->order($order)->limit($limit)->select()->each(function ($e) {
  59. return $e['add_time'] = date('Y-m-d H:i:s', $e['add_time']);
  60. })->toArray();
  61. return compact('total', 'rows');
  62. }
  63. public static function getBillList($where)
  64. {
  65. $data = ($data = self::setWhereList($where)->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  66. $count = self::setWhereList($where)->count();
  67. return compact('data', 'count');
  68. }
  69. public static function SaveExport($where)
  70. {
  71. $data = ($data = self::setWhereList($where)->select()) && count($data) ? $data->toArray() : [];
  72. $export = [];
  73. foreach ($data as $value) {
  74. $export[] = [
  75. $value['uid'],
  76. $value['nickname'],
  77. $value['pm'] == 0 ? '-' . $value['number'] : $value['number'],
  78. $value['title'],
  79. $value['mark'],
  80. $value['add_time'],
  81. ];
  82. }
  83. PHPExcelService::setExcelHeader(['会员ID', '昵称', '金额/积分', '类型', '备注', '创建时间'])
  84. ->setExcelTile('资金监控', '资金监控', date('Y-m-d H:i:s', time()))
  85. ->setExcelContent($export)
  86. ->ExcelSave();
  87. }
  88. public static function setWhereList($where)
  89. {
  90. $time['data'] = '';
  91. if ($where['start_time'] != '' && $where['end_time'] != '') {
  92. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  93. }
  94. $model = self::getModelTime($time, self::alias('A')
  95. ->join('user B', 'B.uid=A.uid')
  96. ->where('A.category', 'not in', 'integral')
  97. ->order('A.add_time desc'), 'A.add_time');
  98. if (trim($where['type']) != '') {
  99. $model = $model->where('A.type', $where['type']);
  100. } else {
  101. $model = $model->where('A.type', 'not in', 'gain,system_sub,deduction,sign');
  102. }
  103. if ($where['nickname'] != '') {
  104. $model = $model->where('B.nickname|B.uid', 'like', "%$where[nickname]%");
  105. }
  106. return $model->field(['A.*', 'FROM_UNIXTIME(A.add_time,"%Y-%m-%d %H:%i:%s") as add_time', 'B.uid', 'B.nickname']);
  107. }
  108. /**
  109. * 获取营业数据
  110. */
  111. public static function getOrderInfo($where)
  112. {
  113. $orderinfo = self::getTimeWhere($where)
  114. ->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')
  115. ->order('pay_time')
  116. ->group('from_unixtime(pay_time,\'%Y-%m-%d\')')
  117. ->select()
  118. ->toArray();
  119. $price = 0;
  120. $postage = 0;
  121. $deduction = 0;
  122. $coupon = 0;
  123. $cost = 0;
  124. foreach ($orderinfo as $info) {
  125. $price = bcadd($price, $info['total_price'], 2);//应支付
  126. $postage = bcadd($postage, $info['pay_postage'], 2);//邮费
  127. $deduction = bcadd($deduction, $info['deduction_price'], 2);//抵扣
  128. $coupon = bcadd($coupon, $info['coupon_price'], 2);//优惠券
  129. $cost = bcadd($cost, $info['cost'], 2);//成本
  130. }
  131. return compact('orderinfo', 'price', 'postage', 'deduction', 'coupon', 'cost');
  132. }
  133. /**
  134. * 处理where条件
  135. */
  136. public static function statusByWhere($status, $model = null)
  137. {
  138. if ($model == null) $model = new self;
  139. if ('' === $status)
  140. return $model;
  141. else if ($status == 'weixin')//微信支付
  142. return $model->where('pay_type', 'weixin');
  143. else if ($status == 'yue')//余额支付
  144. return $model->where('pay_type', 'yue');
  145. else if ($status == 'offline')//线下支付
  146. return $model->where('pay_type', 'offline');
  147. else
  148. return $model;
  149. }
  150. public static function getTimeWhere($where, $model = null)
  151. {
  152. return self::getTime($where)->where('paid', 1)->where('refund_status', 0);
  153. }
  154. /**
  155. * 获取时间区间
  156. */
  157. public static function getTime($where, $model = null, $prefix = 'add_time')
  158. {
  159. if ($model == null) $model = new self;
  160. if ($where['data'] == '') {
  161. switch ($where['date']) {
  162. case 'today':
  163. case 'week':
  164. case 'month':
  165. case 'year':
  166. $model = $model->whereTime($prefix, $where['date']);
  167. break;
  168. case 'quarter':
  169. list($startTime, $endTime) = User::getMonth('n');
  170. $model = $model->where($prefix, '>', strtotime($startTime));
  171. $model = $model->where($prefix, '<', strtotime($endTime));
  172. break;
  173. }
  174. } else {
  175. list($startTime, $endTime) = explode(' - ', $where['data']);
  176. $model = $model->where($prefix, '>', strtotime($startTime));
  177. $model = $model->where($prefix, '<', strtotime($endTime));
  178. }
  179. return $model;
  180. }
  181. /**
  182. * 获取新增消费
  183. */
  184. public static function getConsumption($where)
  185. {
  186. $consumption = self::getTime($where, new UserBill, 'b.add_time')->alias('a')->join('user b', 'a.uid = b.uid')
  187. ->field('sum(a.number) number')
  188. ->where('a.type', 'pay_product')->find()->toArray();
  189. return $consumption;
  190. }
  191. /**
  192. * 获取拼团商品
  193. */
  194. public static function getPink($where)
  195. {
  196. $pink = self::getTimeWhere($where)->where('pink_id', '<>', 0)->sum('pay_price');
  197. return $pink;
  198. }
  199. /**
  200. * 获取秒杀商品
  201. */
  202. public static function getSeckill($where)
  203. {
  204. $seckill = self::getTimeWhere($where)->where('seckill_id', '<>', 0)->sum('pay_price');
  205. return $seckill;
  206. }
  207. /**
  208. * 获取普通商品数
  209. */
  210. public static function getOrdinary($where)
  211. {
  212. $ordinary = self::getTimeWhere($where)->where('pink_id', 0)->where('seckill_id', 0)->sum('pay_price');
  213. return $ordinary;
  214. }
  215. /**
  216. * 获取用户充值
  217. */
  218. public static function getRecharge($where)
  219. {
  220. $Recharge = self::getTime($where, new UserBill)->where('type', 'system_add')->where('category', 'now_money')->sum('number');
  221. return $Recharge;
  222. }
  223. /**
  224. * 获取推广金
  225. */
  226. public static function getExtension($where)
  227. {
  228. $extension = self::getTime($where, new UserBill)->where('type', 'brokerage')->where('category', 'now_money')->sum('number');
  229. return $extension;
  230. }
  231. /**
  232. * 最近交易
  233. */
  234. public static function trans()
  235. {
  236. $trans = self::alias('a')
  237. ->join('user b', 'a.uid=b.uid')
  238. ->join('store_order_cart_info c', 'a.id=c.oid')
  239. ->join('store_product d', 'c.product_id=d.id')
  240. ->field('b.nickname,a.pay_price,d.store_name')
  241. ->order('a.add_time DESC')
  242. ->limit('6')
  243. ->select()->toArray();
  244. return $trans;
  245. }
  246. /**
  247. * 导出表格
  248. */
  249. public static function systemTable($where)
  250. {
  251. $orderinfos = self::getOrderInfo($where);
  252. if ($where['export'] == 1) {
  253. $export = [];
  254. $orderinfo = $orderinfos['orderinfo'];
  255. foreach ($orderinfo as $info) {
  256. $time = $info['pay_time'];
  257. $price = $info['total_price'] + $info['pay_postage'];
  258. $zhichu = $info['coupon_price'] + $info['deduction_price'] + $info['cost'];
  259. $profit = ($info['total_price'] + $info['pay_postage']) - ($info['coupon_price'] + $info['deduction_price'] + $info['cost']);
  260. $deduction = $info['deduction_price'];//积分抵扣
  261. $coupon = $info['coupon_price'];//优惠
  262. $cost = $info['cost'];//成本
  263. $export[] = [$time, $price, $zhichu, $cost, $coupon, $deduction, $profit];
  264. }
  265. // ExportService::exportCsv($export,'统计'.time(),['时间','营业额(元)','支出(元)','成本','优惠','积分抵扣','盈利(元)']);
  266. PHPExcelService::setExcelHeader(['时间', '营业额(元)', '支出(元)', '成本', '优惠', '积分抵扣', '盈利(元)'])->setExcelTile('财务统计', '财务统计', date('Y-m-d H:i:s', time()))->setExcelContent($export)->ExcelSave();
  267. }
  268. }
  269. }