FinanceModel.php 10 KB

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