FinanceModel.php 9.6 KB

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