FinanceModel.php 10 KB

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