FinanceModel.php 11 KB

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