FinanceModel.php 11 KB

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