FinanceModel.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace app\models\finance;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use app\models\user\UserBill;
  6. use app\models\user\User;
  7. use crmeb\services\PHPExcelService;
  8. use crmeb\repositories\ExportRepositories;
  9. /**
  10. * 数据统计处理
  11. * Class FinanceModel
  12. * @package app\models\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. }
  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. $data = ($list = self::setWhereList($where)->page((int)$where['page'], (int)$where['limit'])->select()) && count($list) ? $list->toArray() : [];
  66. $count = self::setWhereList($where)->count();
  67. return compact('count', 'data');
  68. }
  69. public static function exportData($where)
  70. {
  71. $data = ($data = self::setWhereList($where)->select()) && count($data) ? $data->toArray() : [];
  72. return $data;
  73. }
  74. public static function setWhereList($where)
  75. {
  76. $time['data'] = '';
  77. if ($where['start_time'] != '' && $where['end_time'] != '') {
  78. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  79. }
  80. $model = self::getModelTime($time, self::alias('A')
  81. ->join('user B', 'B.uid=A.uid')
  82. ->where('A.category', 'not in', 'integral')
  83. ->order('A.add_time desc'), 'A.add_time');
  84. if (trim($where['type']) != '') {
  85. $model = $model->where('A.type', $where['type']);
  86. } else {
  87. $model = $model->where('A.type', 'not in', 'gain,system_sub,deduction,sign');
  88. }
  89. if ($where['nickname'] != '') {
  90. $model = $model->where('B.nickname|B.uid', 'like', "%$where[nickname]%");
  91. }
  92. if ($where['mer_id']) {
  93. $model = $model->where('A.mer_id', $where['mer_id']);
  94. }
  95. return $model->field(['A.*', 'FROM_UNIXTIME(A.add_time,"%Y-%m-%d %H:%i:%s") as add_time', 'B.uid', 'B.nickname']);
  96. }
  97. /**
  98. * 获取营业数据
  99. */
  100. public static function getOrderInfo($where)
  101. {
  102. $orderinfo = self::getTimeWhere($where)
  103. ->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();
  104. $price = 0;
  105. $postage = 0;
  106. $deduction = 0;
  107. $coupon = 0;
  108. $cost = 0;
  109. foreach ($orderinfo as $info) {
  110. $price = bcadd($price, $info['total_price'], 2);//应支付
  111. $postage = bcadd($postage, $info['pay_postage'], 2);//邮费
  112. $deduction = bcadd($deduction, $info['deduction_price'], 2);//抵扣
  113. $coupon = bcadd($coupon, $info['coupon_price'], 2);//优惠券
  114. $cost = bcadd($cost, $info['cost'], 2);//成本
  115. }
  116. return compact('orderinfo', 'price', 'postage', 'deduction', 'coupon', 'cost');
  117. }
  118. /**
  119. * 处理where条件
  120. */
  121. public static function statusByWhere($status, $model = null)
  122. {
  123. if ($model == null) $model = new self;
  124. if ('' === $status)
  125. return $model;
  126. else if ($status == 'weixin')//微信支付
  127. return $model->where('pay_type', 'weixin');
  128. else if ($status == 'yue')//余额支付
  129. return $model->where('pay_type', 'yue');
  130. else if ($status == 'offline')//线下支付
  131. return $model->where('pay_type', 'offline');
  132. else
  133. return $model;
  134. }
  135. public static function getTimeWhere($where, $model = null)
  136. {
  137. return self::getTime($where)->where('paid', 1)->where('refund_status', 0);
  138. }
  139. /**
  140. * 获取时间区间
  141. */
  142. public static function getTime($where, $model = null, $prefix = 'add_time')
  143. {
  144. if ($model == null) $model = new self;
  145. if ($where['data'] == '') {
  146. switch ($where['date']) {
  147. case 'today':
  148. case 'week':
  149. case 'month':
  150. case 'year':
  151. $model = $model->whereTime($prefix, $where['date']);
  152. break;
  153. case 'quarter':
  154. list($startTime, $endTime) = User::getMonth('n');
  155. $model = $model->where($prefix, '>', strtotime($startTime));
  156. $model = $model->where($prefix, '<', strtotime($endTime));
  157. break;
  158. }
  159. } else {
  160. list($startTime, $endTime) = explode(' - ', $where['data']);
  161. $model = $model->where($prefix, '>', strtotime($startTime));
  162. $model = $model->where($prefix, '<', strtotime($endTime));
  163. }
  164. return $model;
  165. }
  166. /**
  167. * 获取新增消费
  168. */
  169. public static function getConsumption($where)
  170. {
  171. $consumption = self::getTime($where, new UserBill, 'b.add_time')->alias('a')->join('user b', 'a.uid = b.uid')
  172. ->field('sum(a.number) number')
  173. ->where('a.type', 'pay_product')->find()->toArray();
  174. return $consumption;
  175. }
  176. /**
  177. * 获取拼团商品
  178. */
  179. public static function getPink($where)
  180. {
  181. $pink = self::getTimeWhere($where)->where('pink_id', '<>', 0)->sum('pay_price');
  182. return $pink;
  183. }
  184. /**
  185. * 获取秒杀商品
  186. */
  187. public static function getSeckill($where)
  188. {
  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. {
  236. $orderinfos = self::getOrderInfo($where);
  237. if ($where['export'] == 1) {
  238. $export = [];
  239. $orderinfo = $orderinfos['orderinfo'];
  240. foreach ($orderinfo as $info) {
  241. $time = $info['pay_time'];
  242. $price = $info['total_price'] + $info['pay_postage'];
  243. $zhichu = $info['coupon_price'] + $info['deduction_price'] + $info['cost'];
  244. $profit = ($info['total_price'] + $info['pay_postage']) - ($info['coupon_price'] + $info['deduction_price'] + $info['cost']);
  245. $deduction = $info['deduction_price'];//积分抵扣
  246. $coupon = $info['coupon_price'];//优惠
  247. $cost = $info['cost'];//成本
  248. $export[] = [$time, $price, $zhichu, $cost, $coupon, $deduction, $profit];
  249. }
  250. // ExportService::exportCsv($export,'统计'.time(),['时间','营业额(元)','支出(元)','成本','优惠','积分抵扣','盈利(元)']);
  251. PHPExcelService::setExcelHeader(['时间', '营业额(元)', '支出(元)', '成本', '优惠', '积分抵扣', '盈利(元)'])->setExcelTile('财务统计', '财务统计', date('Y-m-d H:i:s', time()))->setExcelContent($export)->ExcelSave();
  252. }
  253. }
  254. }