FinanceLog.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class FinanceLog extends Model
  8. {
  9. public function add(){ // 弃用
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'income' => input('income/f',0),
  13. 'expenditure' => input('expenditure/f',0),
  14. 'day' => input('day','','trim'),
  15. 'org_id' => input('orgId/d',0),
  16. ];
  17. $result = validate('FinanceLog')->check($data,[],'');
  18. if(true !== $result){
  19. HelpHander::error(validate('FinanceLog')->getError());
  20. }
  21. $data['ym'] = date('Ym',strtotime($data['day']));
  22. $data['y'] = date('Y',strtotime($data['day']));
  23. $id = $data['id'];
  24. unset($data['id']);
  25. if($id > 0){
  26. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  27. }else{
  28. $ret = $this->allowField(true)->save($data);
  29. }
  30. if(!$ret){
  31. HelpHander::error('操作失败');
  32. }
  33. return true;
  34. }
  35. public function info($id){
  36. $info = $this->where('id',$id)->find();
  37. if(!$info){
  38. HelpHander::error('数据不存在');
  39. }
  40. return $info->toArray();
  41. }
  42. public function lists($page,$size,$day,$orgId){
  43. $map[] = ['org_id','=',$orgId];
  44. if($day){
  45. $map[] = ['day','=',$day];
  46. }
  47. $lists = Db::name('finance_log')
  48. ->where($map)
  49. ->page($page,$size)
  50. ->order('id desc')
  51. ->select();
  52. $total = Db::name('finance_log')->where($map)->count();
  53. $data = [
  54. 'total' => $total,
  55. 'list' => $lists?$lists:[]
  56. ];
  57. return $data;
  58. }
  59. public function del($id){
  60. $ret = $this->where('id',$id)->delete();
  61. if(!$ret){
  62. HelpHander::error('删除失败');
  63. }
  64. return true;
  65. }
  66. /**
  67. * 根据性别获取在职人数
  68. * @param $type 1=当日 2=月 3=年
  69. * @param $cate 1=收入 2=支出
  70. * @return float|string
  71. */
  72. public function financeStructure($orgId,$type,$cate){
  73. $map[] = ['org_id','=',$orgId];
  74. if($type == 1){
  75. $map[] = ['day','=',date('Y-m-d')];
  76. }else if($type == 2){
  77. $map[] = ['ym','=',date('Ym')];
  78. }else if($type == 3){
  79. $map[] = ['y','=',date('Y')];
  80. }
  81. if($cate == 1){
  82. $field = 'income';
  83. }else{
  84. $field = 'expenditure';
  85. }
  86. $total = Db::name('finance_log')
  87. ->where($map)
  88. ->sum($field);
  89. return $total;
  90. }
  91. }