MoneyLog.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\model;
  4. use think\Model;
  5. class MoneyLog Extends Model
  6. {
  7. // 自动写入时间戳字段
  8. protected $autoWriteTimestamp = true;
  9. protected $createTime = 'createtime';
  10. protected $updateTime = 'updatetime';
  11. protected $type = [
  12. 'createtime' => 'timestamp:Y-m-d H:i',
  13. 'updatetime' => 'timestamp:Y-m-d H:i',
  14. ];
  15. const TYPE_INCOME = 1;
  16. const TYPE_EXPENDITURE = 2;
  17. const STATUS_HIDDEN= 0; //隐藏
  18. const STATUS_NORMAL = 1; //正常
  19. //
  20. public function users()
  21. {
  22. return $this->hasOne(User::class,'id','user_id')->field('id,nickname');
  23. }
  24. public function stockconfig()
  25. {
  26. return $this->hasOne(StockConfig::class,'id','variety_id')->field('id,title');
  27. }
  28. //按照类型月统计 收入- 支出
  29. public static function getCountMonthBalance(string $time = '')
  30. {
  31. $income = self::where('type', self::TYPE_INCOME)->where('status', self::STATUS_NORMAL);
  32. if(!empty($time)){
  33. $income = $income->whereMonth('create_date', $time);
  34. }
  35. $income = $income->sum('change');
  36. //支出
  37. $expenditure = self::where('type', self::TYPE_EXPENDITURE)->where('status', self::STATUS_NORMAL);
  38. if(!empty($time)){
  39. $expenditure = $expenditure->whereMonth('create_date', $time);
  40. }
  41. $expenditure = $expenditure->sum('change');
  42. $balance = $income - $expenditure;
  43. return [
  44. 'balance' => round($balance ,2),
  45. 'income' => round($income,2),
  46. 'expenditure' => round($expenditure,2)
  47. ];
  48. }
  49. //按照类型年统计 收入- 支出
  50. public static function getCountYearBalance(string $time = '')
  51. {
  52. $income = self::where('type', self::TYPE_INCOME)->where('status', self::STATUS_NORMAL);
  53. if(!empty($time)){
  54. $income = $income->whereYear('create_date', $time);
  55. }
  56. $income = $income->sum('change');
  57. //支出
  58. $expenditure = self::where('type', self::TYPE_EXPENDITURE)->where('status', self::STATUS_NORMAL);
  59. if(!empty($time)){
  60. $expenditure = $expenditure->whereYear('create_date', $time);
  61. }
  62. $expenditure = $expenditure->sum('change');
  63. $balance = $income - $expenditure;
  64. return [
  65. 'balance' => round($balance,2),
  66. 'income' => round($income,2),
  67. 'expenditure' => round($expenditure,2)
  68. ];
  69. }
  70. //按照类型天统计 收入- 支出
  71. public static function getCountDayBalance(string $time = '')
  72. {
  73. $list = self::where('status', self::STATUS_NORMAL);
  74. if(!empty($time)) $list = $list->whereMonth('create_date', $time);
  75. $list = $list->group('create_date')
  76. ->field('create_date, sum(if(type=1,`change`,0)) as income, sum(if(type=2,`change`, 0)) as expenditure')
  77. ->select();
  78. return $list;
  79. }
  80. //支收总览
  81. public static function getCountAllBalance(int $type, string $time = '', array $user_ids=[])
  82. {
  83. $list = self::where('status', self::STATUS_NORMAL);
  84. if($type == 1 && !empty($time)) $list = $list->whereMonth('create_date', $time);
  85. if($type == 2 && !empty($time)) $list = $list->whereYear('create_date', $time);
  86. if($type == 3 && !empty($time)){
  87. $expTime = explode(',', $time);
  88. $list = $list->whereTime('create_date', [$expTime[0], $expTime[1]]);
  89. }
  90. if(count($user_ids) > 0) $list = $list->whereIn('user_id', $user_ids);
  91. $list = $list
  92. ->field('type, sum(if(type=1,`change`,0)) as income, sum(if(type=2,`change`, 0)) as expenditure')
  93. ->group('type')
  94. ->select();
  95. return $list;
  96. }
  97. //统计用户余额
  98. public static function getCountAllUserBalance(int $type, string $time = '', array $user_ids=[])
  99. {
  100. $list = self::where('status', self::STATUS_NORMAL);
  101. if($type == 1 && !empty($time)) $list = $list->whereMonth('create_date', $time);
  102. if($type == 2 && !empty($time)) $list = $list->whereYear('create_date', $time);
  103. if($type == 3 && !empty($time)){
  104. $expTime = explode(',', $time);
  105. $list = $list->whereTime('create_date', [$expTime[0], $expTime[1]]);
  106. }
  107. if(count($user_ids) >0) $list = $list->whereIn('user_id', $user_ids);
  108. $list = $list
  109. ->field('bank_account, sum(if(type=1,`change`,0)) as income, sum(if(type=2,`change`, 0)) as expenditure')
  110. ->group('bank_account')
  111. ->select();
  112. return $list;
  113. }
  114. //统计所有月统计
  115. public static function getCountAllMonthBalance(int $type, string $time = '', array $user_ids=[])
  116. {
  117. $list = self::where('status', self::STATUS_NORMAL);
  118. if($type == 1 && !empty($time)) $list = $list->whereMonth('create_date', $time);
  119. //年
  120. if($type == 2 && !empty($time)) $list = $list->whereYear('create_date', $time);
  121. //自定义
  122. if($type == 3 && !empty($time)){
  123. $expTime = explode(',', $time);
  124. $list = $list->whereTime('create_date', [$expTime[0], $expTime[1]]);
  125. }
  126. if(count($user_ids)> 0) $list = $list->whereIn('user_id', $user_ids);
  127. //月
  128. $list = $list
  129. ->field('create_month, sum(if(type=1,`change`,0)) as income, sum(if(type=2,`change`, 0)) as expenditure')
  130. ->group('create_month')
  131. ->order('create_month desc')
  132. ->select();
  133. return $list;
  134. }
  135. }